GalleryActionBar.java revision 8cab3e872dd95e55ba34fdb94269a0c5069e72ae
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
17package com.android.gallery3d.app;
18
19import android.app.ActionBar;
20import android.app.Activity;
21import android.app.AlertDialog;
22import android.app.ActionBar.OnMenuVisibilityListener;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.view.LayoutInflater;
26import android.view.Menu;
27import android.view.MenuItem;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.BaseAdapter;
31import android.widget.ShareActionProvider;
32import android.widget.TextView;
33
34import com.android.gallery3d.R;
35
36import java.util.ArrayList;
37
38public class GalleryActionBar implements ActionBar.OnNavigationListener {
39    private static final String TAG = "GalleryActionBar";
40
41    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
51    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;
59        public int spinnerTitle;
60        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
68        public ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle,
69                int dialogTitle, int clusterBy) {
70            this.action = action;
71            this.enabled = enabled;
72            this.spinnerTitle = spinnerTitle;
73            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
92    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) {
108                convertView = mInflater.inflate(R.layout.action_bar_text,
109                        parent, false);
110            }
111            TextView view = (TextView) convertView;
112            view.setText(sClusterItems[position].spinnerTitle);
113            return convertView;
114        }
115    }
116
117    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    }
134
135    public GalleryActionBar(GalleryActivity activity) {
136        mActionBar = ((Activity) activity).getActionBar();
137        mContext = activity.getAndroidContext();
138        mActivity = activity;
139        mInflater = ((Activity) mActivity).getLayoutInflater();
140        mCurrentIndex = 0;
141    }
142
143    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
156    public int getHeight() {
157        return mActionBar != null ? mActionBar.getHeight() : 0;
158    }
159
160    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() {
179        return sClusterItems[mCurrentIndex].action;
180    }
181
182    public void enableClusterMenu(int action, ClusterRunner runner) {
183        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        }
191    }
192
193    // 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) {
197        if (mActionBar != null) {
198            mClusterRunner = null;
199            if (hideMenu) {
200                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
201            }
202        }
203    }
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
216    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);
220            mActionBar.setDisplayOptions(options,
221                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
222            mActionBar.setHomeButtonEnabled(displayHomeAsUp);
223        }
224    }
225
226    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
238    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
254    public boolean setSelectedAction(int type) {
255        if (mActionBar == null) return false;
256
257        for (int i = 0, n = sClusterItems.length; i < n; i++) {
258            ActionItem item = sClusterItems[i];
259            if (item.action == type) {
260                mActionBar.setSelectedNavigationItem(i);
261                mCurrentIndex = i;
262                return true;
263            }
264        }
265        return false;
266    }
267
268    @Override
269    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
270        if (itemPosition != mCurrentIndex && mClusterRunner != null) {
271            mActivity.getGLRoot().lockRenderThread();
272            try {
273                mClusterRunner.doCluster(sClusterItems[itemPosition].action);
274            } finally {
275                mActivity.getGLRoot().unlockRenderThread();
276            }
277        }
278        return false;
279    }
280}
281