• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.photoeditor;
18 
19 import android.content.Context;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.ImageButton;
25 import android.widget.ViewSwitcher;
26 
27 /**
28  * Action bar that contains buttons such as undo, redo, save, etc. and listens to stack changes for
29  * enabling/disabling buttons.
30  */
31 public class ActionBar extends ViewSwitcher implements FilterStack.StackListener {
32 
33     /**
34      * Listener of action button clicked.
35      */
36     public interface ActionBarListener {
37 
onQuickview(boolean on)38         void onQuickview(boolean on);
39 
onUndo()40         void onUndo();
41 
onRedo()42         void onRedo();
43 
onSave()44         void onSave();
45     }
46 
47     private static final int ENABLE_BUTTON = 1;
48     private static final int ENABLED_ALPHA = 255;
49     private static final int DISABLED_ALPHA = 120;
50 
51     private final Handler handler;
52     private ImageButton save;
53     private ImageButton undo;
54     private ImageButton redo;
55     private ImageButton quickview;
56 
ActionBar(Context context, AttributeSet attrs)57     public ActionBar(Context context, AttributeSet attrs) {
58         super(context, attrs);
59 
60         handler = new Handler() {
61 
62             @Override
63             public void handleMessage(Message msg) {
64                 switch (msg.what) {
65                     case ENABLE_BUTTON:
66                         boolean canUndo = (msg.arg1 > 0);
67                         boolean canRedo = (msg.arg2 > 0);
68                         enableButton(quickview, canUndo);
69                         enableButton(save, canUndo);
70                         enableButton(undo, canUndo);
71                         enableButton(redo, canRedo);
72                         break;
73                 }
74             }
75         };
76     }
77 
78     /**
79      * Initializes with a non-null ActionBarListener.
80      */
initialize(final ActionBarListener listener)81     public void initialize(final ActionBarListener listener) {
82         save = (ImageButton) findViewById(R.id.save_button);
83         save.setOnClickListener(new OnClickListener() {
84 
85             @Override
86             public void onClick(View v) {
87                 if (isEnabled()) {
88                     listener.onSave();
89                 }
90             }
91         });
92 
93         undo = (ImageButton) findViewById(R.id.undo_button);
94         undo.setOnClickListener(new OnClickListener() {
95 
96             @Override
97             public void onClick(View v) {
98                 if (isEnabled()) {
99                     listener.onUndo();
100                 }
101             }
102         });
103 
104         redo = (ImageButton) findViewById(R.id.redo_button);
105         redo.setOnClickListener(new OnClickListener() {
106 
107             @Override
108             public void onClick(View v) {
109                 if (isEnabled()) {
110                     listener.onRedo();
111                 }
112             }
113         });
114 
115         quickview = (ImageButton) findViewById(R.id.quickview_button);
116         quickview.setOnClickListener(new OnClickListener() {
117 
118             @Override
119             public void onClick(View v) {
120                 if (isEnabled()) {
121                     ActionBar.this.showNext();
122                     listener.onQuickview(true);
123                 }
124             }
125         });
126 
127         View quickviewOn = findViewById(R.id.quickview_on_button);
128         quickviewOn.setOnClickListener(new OnClickListener() {
129 
130             @Override
131             public void onClick(View v) {
132                 if (isEnabled()) {
133                     ActionBar.this.showNext();
134                     listener.onQuickview(false);
135                 }
136             }
137         });
138 
139         resetButtons();
140     }
141 
resetButtons()142     public void resetButtons() {
143         // Disable buttons immediately instead of waiting for ENABLE_BUTTON messages which may
144         // happen some time later after stack changes.
145         enableButton(save, false);
146         enableButton(undo, false);
147         enableButton(redo, false);
148         enableButton(quickview, false);
149     }
150 
disableSave()151     public void disableSave() {
152         enableButton(save, false);
153     }
154 
enableButton(ImageButton button, boolean enabled)155     private void enableButton(ImageButton button, boolean enabled) {
156         button.setEnabled(enabled);
157         button.setAlpha(enabled ? ENABLED_ALPHA : DISABLED_ALPHA);
158     }
159 
160     @Override
onStackChanged(boolean canUndo, boolean canRedo)161     public void onStackChanged(boolean canUndo, boolean canRedo) {
162         // Listens to stack changes that may come from the worker thread; send messages to enable
163         // buttons only in the UI thread.
164         handler.sendMessage(handler.obtainMessage(ENABLE_BUTTON, canUndo ? 1 : 0, canRedo ? 1 : 0));
165     }
166 }
167