• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
17 package com.android.gallery3d.app;
18 
19 import com.android.gallery3d.R;
20 
21 import android.app.ActionBar;
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.content.Context;
25 import android.content.DialogInterface;
26 import android.view.LayoutInflater;
27 import android.view.Menu;
28 import android.view.MenuItem;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.BaseAdapter;
32 import android.widget.ShareActionProvider;
33 import android.widget.TextView;
34 
35 import java.util.ArrayList;
36 
37 public class GalleryActionBar implements ActionBar.OnNavigationListener {
38     private static final String TAG = "GalleryActionBar";
39 
40     public interface ClusterRunner {
doCluster(int id)41         public void doCluster(int id);
42     }
43 
44     private static class ActionItem {
45         public int action;
46         public boolean enabled;
47         public boolean visible;
48         public int spinnerTitle;
49         public int dialogTitle;
50         public int clusterBy;
51 
ActionItem(int action, boolean applied, boolean enabled, int title, int clusterBy)52         public ActionItem(int action, boolean applied, boolean enabled, int title,
53                 int clusterBy) {
54             this(action, applied, enabled, title, title, clusterBy);
55         }
56 
ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle, int dialogTitle, int clusterBy)57         public ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle,
58                 int dialogTitle, int clusterBy) {
59             this.action = action;
60             this.enabled = enabled;
61             this.spinnerTitle = spinnerTitle;
62             this.dialogTitle = dialogTitle;
63             this.clusterBy = clusterBy;
64             this.visible = true;
65         }
66     }
67 
68     private static final ActionItem[] sClusterItems = new ActionItem[] {
69         new ActionItem(FilterUtils.CLUSTER_BY_ALBUM, true, false, R.string.albums,
70                 R.string.group_by_album),
71         new ActionItem(FilterUtils.CLUSTER_BY_LOCATION, true, false,
72                 R.string.locations, R.string.location, R.string.group_by_location),
73         new ActionItem(FilterUtils.CLUSTER_BY_TIME, true, false, R.string.times,
74                 R.string.time, R.string.group_by_time),
75         new ActionItem(FilterUtils.CLUSTER_BY_FACE, true, false, R.string.people,
76                 R.string.group_by_faces),
77         new ActionItem(FilterUtils.CLUSTER_BY_TAG, true, false, R.string.tags,
78                 R.string.group_by_tags)
79     };
80 
81     private class ClusterAdapter extends BaseAdapter {
82 
getCount()83         public int getCount() {
84             return sClusterItems.length;
85         }
86 
getItem(int position)87         public Object getItem(int position) {
88             return sClusterItems[position];
89         }
90 
getItemId(int position)91         public long getItemId(int position) {
92             return sClusterItems[position].action;
93         }
94 
getView(int position, View convertView, ViewGroup parent)95         public View getView(int position, View convertView, ViewGroup parent) {
96             if (convertView == null) {
97                 convertView = mInflater.inflate(R.layout.action_bar_text,
98                         parent, false);
99             }
100             TextView view = (TextView) convertView;
101             view.setText(sClusterItems[position].spinnerTitle);
102             return convertView;
103         }
104     }
105 
106     private ClusterRunner mClusterRunner;
107     private CharSequence[] mTitles;
108     private ArrayList<Integer> mActions;
109     private Context mContext;
110     private LayoutInflater mInflater;
111     private GalleryActivity mActivity;
112     private ActionBar mActionBar;
113     private int mCurrentIndex;
114     private ClusterAdapter mAdapter = new ClusterAdapter();
115 
GalleryActionBar(GalleryActivity activity)116     public GalleryActionBar(GalleryActivity activity) {
117         mActionBar = ((Activity) activity).getActionBar();
118         mContext = activity.getAndroidContext();
119         mActivity = activity;
120         mInflater = ((Activity) mActivity).getLayoutInflater();
121         mCurrentIndex = 0;
122     }
123 
getHeight(Activity activity)124     public static int getHeight(Activity activity) {
125         ActionBar actionBar = activity.getActionBar();
126         return actionBar != null ? actionBar.getHeight() : 0;
127     }
128 
createDialogData()129     private void createDialogData() {
130         ArrayList<CharSequence> titles = new ArrayList<CharSequence>();
131         mActions = new ArrayList<Integer>();
132         for (ActionItem item : sClusterItems) {
133             if (item.enabled && item.visible) {
134                 titles.add(mContext.getString(item.dialogTitle));
135                 mActions.add(item.action);
136             }
137         }
138         mTitles = new CharSequence[titles.size()];
139         titles.toArray(mTitles);
140     }
141 
setClusterItemEnabled(int id, boolean enabled)142     public void setClusterItemEnabled(int id, boolean enabled) {
143         for (ActionItem item : sClusterItems) {
144             if (item.action == id) {
145                 item.enabled = enabled;
146                 return;
147             }
148         }
149     }
150 
setClusterItemVisibility(int id, boolean visible)151     public void setClusterItemVisibility(int id, boolean visible) {
152         for (ActionItem item : sClusterItems) {
153             if (item.action == id) {
154                 item.visible = visible;
155                 return;
156             }
157         }
158     }
159 
getClusterTypeAction()160     public int getClusterTypeAction() {
161         return sClusterItems[mCurrentIndex].action;
162     }
163 
getClusterByTypeString(Context context, int type)164     public static String getClusterByTypeString(Context context, int type) {
165         for (ActionItem item : sClusterItems) {
166             if (item.action == type) {
167                 return context.getString(item.clusterBy);
168             }
169         }
170         return null;
171     }
172 
initializeShareActionProvider(Menu menu)173     public static ShareActionProvider initializeShareActionProvider(Menu menu) {
174         MenuItem item = menu.findItem(R.id.action_share);
175         ShareActionProvider shareActionProvider = null;
176         if (item != null) {
177             shareActionProvider = (ShareActionProvider) item.getActionProvider();
178         }
179         return shareActionProvider;
180     }
181 
showClusterMenu(int action, ClusterRunner runner)182     public void showClusterMenu(int action, ClusterRunner runner) {
183         Log.v(TAG, "showClusterMenu: runner=" + runner);
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     }
191 
hideClusterMenu()192     public void hideClusterMenu() {
193         mClusterRunner = null;
194         mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
195     }
196 
showClusterDialog(final ClusterRunner clusterRunner)197     public void showClusterDialog(final ClusterRunner clusterRunner) {
198         createDialogData();
199         final ArrayList<Integer> actions = mActions;
200         new AlertDialog.Builder(mContext).setTitle(R.string.group_by).setItems(
201                 mTitles, new DialogInterface.OnClickListener() {
202             public void onClick(DialogInterface dialog, int which) {
203                 clusterRunner.doCluster(actions.get(which).intValue());
204             }
205         }).create().show();
206     }
207 
setTitle(String title)208     public void setTitle(String title) {
209         if (mActionBar != null) mActionBar.setTitle(title);
210     }
211 
setTitle(int titleId)212     public void setTitle(int titleId) {
213         if (mActionBar != null) mActionBar.setTitle(titleId);
214     }
215 
setSubtitle(String title)216     public void setSubtitle(String title) {
217         if (mActionBar != null) mActionBar.setSubtitle(title);
218     }
219 
setNavigationMode(int mode)220     public void setNavigationMode(int mode) {
221         if (mActionBar != null) mActionBar.setNavigationMode(mode);
222     }
223 
getHeight()224     public int getHeight() {
225         return mActionBar == null ? 0 : mActionBar.getHeight();
226     }
227 
setSelectedAction(int type)228     public boolean setSelectedAction(int type) {
229         for (int i = 0, n = sClusterItems.length; i < n; i++) {
230             ActionItem item = sClusterItems[i];
231             if (item.visible && item.action == type) {
232                 mActionBar.setSelectedNavigationItem(i);
233                 mCurrentIndex = i;
234                 return true;
235             }
236         }
237         return false;
238     }
239 
240     @Override
onNavigationItemSelected(int itemPosition, long itemId)241     public boolean onNavigationItemSelected(int itemPosition, long itemId) {
242         if (itemPosition != mCurrentIndex && mClusterRunner != null) {
243             mActivity.getGLRoot().lockRenderThread();
244             try {
245                 mClusterRunner.doCluster(sClusterItems[itemPosition].action);
246             } finally {
247                 mActivity.getGLRoot().unlockRenderThread();
248             }
249         }
250         return false;
251     }
252 }
253