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