blob: c99cd60d793c9538b4695c5ea8c4f5f1f35623e6 [file] [log] [blame]
Owen Lina2fba682011-08-17 22:07:43 +08001/*
2 * Copyright (C) 2011 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
17package com.android.gallery3d.app;
18
Owen Lina2fba682011-08-17 22:07:43 +080019import android.app.ActionBar;
Owen Lina2fba682011-08-17 22:07:43 +080020import android.app.Activity;
21import android.app.AlertDialog;
Ray Chen9af4a902012-03-20 16:32:57 +080022import android.app.ActionBar.OnMenuVisibilityListener;
Owen Lina2fba682011-08-17 22:07:43 +080023import android.content.Context;
24import android.content.DialogInterface;
Ray Chenf95aed12011-09-13 18:35:28 +080025import android.view.LayoutInflater;
Owen Lina2fba682011-08-17 22:07:43 +080026import android.view.Menu;
27import android.view.MenuItem;
Ray Chenf95aed12011-09-13 18:35:28 +080028import android.view.View;
29import android.view.ViewGroup;
Ray Chenf95aed12011-09-13 18:35:28 +080030import android.widget.BaseAdapter;
Owen Lina2fba682011-08-17 22:07:43 +080031import android.widget.ShareActionProvider;
Ray Chenf95aed12011-09-13 18:35:28 +080032import android.widget.TextView;
Owen Lin44aac4b2011-08-28 10:50:21 +080033
Owen Lin73a04ff2012-03-14 17:27:24 +080034import com.android.gallery3d.R;
35
Owen Lin44aac4b2011-08-28 10:50:21 +080036import java.util.ArrayList;
Owen Lina2fba682011-08-17 22:07:43 +080037
Ray Chenf95aed12011-09-13 18:35:28 +080038public class GalleryActionBar implements ActionBar.OnNavigationListener {
Owen Lina2fba682011-08-17 22:07:43 +080039 private static final String TAG = "GalleryActionBar";
40
Ray Chen9af4a902012-03-20 16:32:57 +080041 private ClusterRunner mClusterRunner;
42 private CharSequence[] mTitles;
43 private ArrayList<Integer> mActions;
44 private Context mContext;
45 private LayoutInflater mInflater;
46 private GalleryActivity mActivity;
47 private ActionBar mActionBar;
48 private int mCurrentIndex;
49 private ClusterAdapter mAdapter = new ClusterAdapter();
50
Owen Lina2fba682011-08-17 22:07:43 +080051 public interface ClusterRunner {
52 public void doCluster(int id);
53 }
54
55 private static class ActionItem {
56 public int action;
57 public boolean enabled;
58 public boolean visible;
Ray Chenf95aed12011-09-13 18:35:28 +080059 public int spinnerTitle;
Owen Lina2fba682011-08-17 22:07:43 +080060 public int dialogTitle;
61 public int clusterBy;
62
63 public ActionItem(int action, boolean applied, boolean enabled, int title,
64 int clusterBy) {
65 this(action, applied, enabled, title, title, clusterBy);
66 }
67
Ray Chenf95aed12011-09-13 18:35:28 +080068 public ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle,
Owen Lina2fba682011-08-17 22:07:43 +080069 int dialogTitle, int clusterBy) {
70 this.action = action;
71 this.enabled = enabled;
Ray Chenf95aed12011-09-13 18:35:28 +080072 this.spinnerTitle = spinnerTitle;
Owen Lina2fba682011-08-17 22:07:43 +080073 this.dialogTitle = dialogTitle;
74 this.clusterBy = clusterBy;
75 this.visible = true;
76 }
77 }
78
79 private static final ActionItem[] sClusterItems = new ActionItem[] {
80 new ActionItem(FilterUtils.CLUSTER_BY_ALBUM, true, false, R.string.albums,
81 R.string.group_by_album),
82 new ActionItem(FilterUtils.CLUSTER_BY_LOCATION, true, false,
83 R.string.locations, R.string.location, R.string.group_by_location),
84 new ActionItem(FilterUtils.CLUSTER_BY_TIME, true, false, R.string.times,
85 R.string.time, R.string.group_by_time),
86 new ActionItem(FilterUtils.CLUSTER_BY_FACE, true, false, R.string.people,
87 R.string.group_by_faces),
88 new ActionItem(FilterUtils.CLUSTER_BY_TAG, true, false, R.string.tags,
89 R.string.group_by_tags)
90 };
91
Ray Chenf95aed12011-09-13 18:35:28 +080092 private class ClusterAdapter extends BaseAdapter {
93
94 public int getCount() {
95 return sClusterItems.length;
96 }
97
98 public Object getItem(int position) {
99 return sClusterItems[position];
100 }
101
102 public long getItemId(int position) {
103 return sClusterItems[position].action;
104 }
105
106 public View getView(int position, View convertView, ViewGroup parent) {
107 if (convertView == null) {
Owen Linb591b412011-09-15 21:30:16 +0800108 convertView = mInflater.inflate(R.layout.action_bar_text,
Ray Chenf95aed12011-09-13 18:35:28 +0800109 parent, false);
110 }
111 TextView view = (TextView) convertView;
112 view.setText(sClusterItems[position].spinnerTitle);
113 return convertView;
114 }
115 }
116
Ray Chen9af4a902012-03-20 16:32:57 +0800117 public static String getClusterByTypeString(Context context, int type) {
118 for (ActionItem item : sClusterItems) {
119 if (item.action == type) {
120 return context.getString(item.clusterBy);
121 }
122 }
123 return null;
124 }
125
126 public static ShareActionProvider initializeShareActionProvider(Menu menu) {
127 MenuItem item = menu.findItem(R.id.action_share);
128 ShareActionProvider shareActionProvider = null;
129 if (item != null) {
130 shareActionProvider = (ShareActionProvider) item.getActionProvider();
131 }
132 return shareActionProvider;
133 }
Owen Lina2fba682011-08-17 22:07:43 +0800134
Owen Linb591b412011-09-15 21:30:16 +0800135 public GalleryActionBar(GalleryActivity activity) {
136 mActionBar = ((Activity) activity).getActionBar();
137 mContext = activity.getAndroidContext();
138 mActivity = activity;
139 mInflater = ((Activity) mActivity).getLayoutInflater();
Ray Chenf95aed12011-09-13 18:35:28 +0800140 mCurrentIndex = 0;
Owen Lina2fba682011-08-17 22:07:43 +0800141 }
142
Owen Lina2fba682011-08-17 22:07:43 +0800143 private void createDialogData() {
144 ArrayList<CharSequence> titles = new ArrayList<CharSequence>();
145 mActions = new ArrayList<Integer>();
146 for (ActionItem item : sClusterItems) {
147 if (item.enabled && item.visible) {
148 titles.add(mContext.getString(item.dialogTitle));
149 mActions.add(item.action);
150 }
151 }
152 mTitles = new CharSequence[titles.size()];
153 titles.toArray(mTitles);
154 }
155
Ray Chen9af4a902012-03-20 16:32:57 +0800156 public int getHeight() {
157 return mActionBar != null ? mActionBar.getHeight() : 0;
158 }
159
Owen Lina2fba682011-08-17 22:07:43 +0800160 public void setClusterItemEnabled(int id, boolean enabled) {
161 for (ActionItem item : sClusterItems) {
162 if (item.action == id) {
163 item.enabled = enabled;
164 return;
165 }
166 }
167 }
168
169 public void setClusterItemVisibility(int id, boolean visible) {
170 for (ActionItem item : sClusterItems) {
171 if (item.action == id) {
172 item.visible = visible;
173 return;
174 }
175 }
176 }
177
178 public int getClusterTypeAction() {
Ray Chenf95aed12011-09-13 18:35:28 +0800179 return sClusterItems[mCurrentIndex].action;
Owen Lina2fba682011-08-17 22:07:43 +0800180 }
181
Ray Chenc3026d72012-02-17 13:12:32 +0800182 public void enableClusterMenu(int action, ClusterRunner runner) {
Ray Chen9af4a902012-03-20 16:32:57 +0800183 if (mActionBar != null) {
184 // Don't set cluster runner until action bar is ready.
185 mClusterRunner = null;
186 mActionBar.setListNavigationCallbacks(mAdapter, this);
187 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
188 setSelectedAction(action);
189 mClusterRunner = runner;
190 }
Owen Lina2fba682011-08-17 22:07:43 +0800191 }
192
Ray Chenc3026d72012-02-17 13:12:32 +0800193 // The only use case not to hideMenu in this method is to ensure
194 // all elements disappear at the same time when exiting gallery.
195 // hideMenu should always be true in all other cases.
196 public void disableClusterMenu(boolean hideMenu) {
Ray Chen9af4a902012-03-20 16:32:57 +0800197 if (mActionBar != null) {
198 mClusterRunner = null;
199 if (hideMenu) {
200 mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
201 }
Ray Chenc3026d72012-02-17 13:12:32 +0800202 }
Owen Lina2fba682011-08-17 22:07:43 +0800203 }
204
205 public void showClusterDialog(final ClusterRunner clusterRunner) {
206 createDialogData();
207 final ArrayList<Integer> actions = mActions;
208 new AlertDialog.Builder(mContext).setTitle(R.string.group_by).setItems(
209 mTitles, new DialogInterface.OnClickListener() {
210 public void onClick(DialogInterface dialog, int which) {
211 clusterRunner.doCluster(actions.get(which).intValue());
212 }
213 }).create().show();
214 }
215
Ray Chenf3f7f562012-03-06 17:24:28 +0800216 public void setDisplayOptions(boolean displayHomeAsUp, boolean showTitle) {
217 if (mActionBar != null) {
218 int options = (displayHomeAsUp ? ActionBar.DISPLAY_HOME_AS_UP : 0) |
219 (showTitle ? ActionBar.DISPLAY_SHOW_TITLE : 0);
Ray Chen9af4a902012-03-20 16:32:57 +0800220 mActionBar.setDisplayOptions(options,
Ray Chenf3f7f562012-03-06 17:24:28 +0800221 ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
222 mActionBar.setHomeButtonEnabled(displayHomeAsUp);
223 }
224 }
225
Owen Lina2fba682011-08-17 22:07:43 +0800226 public void setTitle(String title) {
227 if (mActionBar != null) mActionBar.setTitle(title);
228 }
229
230 public void setTitle(int titleId) {
231 if (mActionBar != null) mActionBar.setTitle(titleId);
232 }
233
234 public void setSubtitle(String title) {
235 if (mActionBar != null) mActionBar.setSubtitle(title);
236 }
237
Ray Chen9af4a902012-03-20 16:32:57 +0800238 public void show() {
239 if (mActionBar != null) mActionBar.show();
240 }
241
242 public void hide() {
243 if (mActionBar != null) mActionBar.hide();
244 }
245
246 public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
247 if (mActionBar != null) mActionBar.addOnMenuVisibilityListener(listener);
248 }
249
250 public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
251 if (mActionBar != null) mActionBar.removeOnMenuVisibilityListener(listener);
252 }
253
Ray Chenf95aed12011-09-13 18:35:28 +0800254 public boolean setSelectedAction(int type) {
Ray Chen9af4a902012-03-20 16:32:57 +0800255 if (mActionBar == null) return false;
256
Ray Chenf95aed12011-09-13 18:35:28 +0800257 for (int i = 0, n = sClusterItems.length; i < n; i++) {
Owen Lin44aac4b2011-08-28 10:50:21 +0800258 ActionItem item = sClusterItems[i];
Ray Chen98020262011-11-18 15:00:32 +0800259 if (item.action == type) {
Ray Chenf95aed12011-09-13 18:35:28 +0800260 mActionBar.setSelectedNavigationItem(i);
261 mCurrentIndex = i;
Owen Lin44aac4b2011-08-28 10:50:21 +0800262 return true;
263 }
264 }
265 return false;
266 }
Ray Chenf95aed12011-09-13 18:35:28 +0800267
Owen Linb591b412011-09-15 21:30:16 +0800268 @Override
Ray Chenf95aed12011-09-13 18:35:28 +0800269 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
270 if (itemPosition != mCurrentIndex && mClusterRunner != null) {
Owen Linb591b412011-09-15 21:30:16 +0800271 mActivity.getGLRoot().lockRenderThread();
272 try {
273 mClusterRunner.doCluster(sClusterItems[itemPosition].action);
274 } finally {
275 mActivity.getGLRoot().unlockRenderThread();
276 }
Ray Chenf95aed12011-09-13 18:35:28 +0800277 }
Ray Chenf95aed12011-09-13 18:35:28 +0800278 return false;
279 }
Owen Lina2fba682011-08-17 22:07:43 +0800280}