Owen Lin | f9a0a43 | 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 | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.app.ActionBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 20 | import android.app.Activity; |
| 21 | import android.app.AlertDialog; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 25 | import android.view.Menu; |
| 26 | import android.view.MenuItem; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 27 | import android.view.View; |
| 28 | import android.view.ViewGroup; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 29 | import android.widget.BaseAdapter; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 30 | import android.widget.ShareActionProvider; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 31 | import android.widget.TextView; |
Owen Lin | 6cf8074 | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 32 | |
Owen Lin | 2b3ee0e | 2012-03-14 17:27:24 +0800 | [diff] [blame^] | 33 | import com.android.gallery3d.R; |
| 34 | |
Owen Lin | 6cf8074 | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 35 | import java.util.ArrayList; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 36 | |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 37 | public class GalleryActionBar implements ActionBar.OnNavigationListener { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 38 | private static final String TAG = "GalleryActionBar"; |
| 39 | |
| 40 | public interface ClusterRunner { |
| 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; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 48 | public int spinnerTitle; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 49 | public int dialogTitle; |
| 50 | public int clusterBy; |
| 51 | |
| 52 | public ActionItem(int action, boolean applied, boolean enabled, int title, |
| 53 | int clusterBy) { |
| 54 | this(action, applied, enabled, title, title, clusterBy); |
| 55 | } |
| 56 | |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 57 | public ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle, |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 58 | int dialogTitle, int clusterBy) { |
| 59 | this.action = action; |
| 60 | this.enabled = enabled; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 61 | this.spinnerTitle = spinnerTitle; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 81 | private class ClusterAdapter extends BaseAdapter { |
| 82 | |
| 83 | public int getCount() { |
| 84 | return sClusterItems.length; |
| 85 | } |
| 86 | |
| 87 | public Object getItem(int position) { |
| 88 | return sClusterItems[position]; |
| 89 | } |
| 90 | |
| 91 | public long getItemId(int position) { |
| 92 | return sClusterItems[position].action; |
| 93 | } |
| 94 | |
| 95 | public View getView(int position, View convertView, ViewGroup parent) { |
| 96 | if (convertView == null) { |
Owen Lin | 5ccc96a | 2011-09-15 21:30:16 +0800 | [diff] [blame] | 97 | convertView = mInflater.inflate(R.layout.action_bar_text, |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 98 | parent, false); |
| 99 | } |
| 100 | TextView view = (TextView) convertView; |
| 101 | view.setText(sClusterItems[position].spinnerTitle); |
| 102 | return convertView; |
| 103 | } |
| 104 | } |
| 105 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 106 | private ClusterRunner mClusterRunner; |
| 107 | private CharSequence[] mTitles; |
| 108 | private ArrayList<Integer> mActions; |
| 109 | private Context mContext; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 110 | private LayoutInflater mInflater; |
Owen Lin | 5ccc96a | 2011-09-15 21:30:16 +0800 | [diff] [blame] | 111 | private GalleryActivity mActivity; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 112 | private ActionBar mActionBar; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 113 | private int mCurrentIndex; |
| 114 | private ClusterAdapter mAdapter = new ClusterAdapter(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 115 | |
Owen Lin | 5ccc96a | 2011-09-15 21:30:16 +0800 | [diff] [blame] | 116 | public GalleryActionBar(GalleryActivity activity) { |
| 117 | mActionBar = ((Activity) activity).getActionBar(); |
| 118 | mContext = activity.getAndroidContext(); |
| 119 | mActivity = activity; |
| 120 | mInflater = ((Activity) mActivity).getLayoutInflater(); |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 121 | mCurrentIndex = 0; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | public static int getHeight(Activity activity) { |
| 125 | ActionBar actionBar = activity.getActionBar(); |
| 126 | return actionBar != null ? actionBar.getHeight() : 0; |
| 127 | } |
| 128 | |
| 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 | |
| 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 | |
| 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 | |
| 160 | public int getClusterTypeAction() { |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 161 | return sClusterItems[mCurrentIndex].action; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 162 | } |
| 163 | |
| 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 | |
| 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(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 178 | } |
| 179 | return shareActionProvider; |
| 180 | } |
| 181 | |
Ray Chen | fe1625c | 2012-02-17 13:12:32 +0800 | [diff] [blame] | 182 | public void enableClusterMenu(int action, ClusterRunner runner) { |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 183 | Log.v(TAG, "showClusterMenu: runner=" + runner); |
| 184 | // Don't set cluster runner until action bar is ready. |
Ray Chen | eab374b | 2011-09-09 11:20:58 +0800 | [diff] [blame] | 185 | mClusterRunner = null; |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 186 | mActionBar.setListNavigationCallbacks(mAdapter, this); |
| 187 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); |
| 188 | setSelectedAction(action); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 189 | mClusterRunner = runner; |
| 190 | } |
| 191 | |
Ray Chen | fe1625c | 2012-02-17 13:12:32 +0800 | [diff] [blame] | 192 | // The only use case not to hideMenu in this method is to ensure |
| 193 | // all elements disappear at the same time when exiting gallery. |
| 194 | // hideMenu should always be true in all other cases. |
| 195 | public void disableClusterMenu(boolean hideMenu) { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 196 | mClusterRunner = null; |
Ray Chen | fe1625c | 2012-02-17 13:12:32 +0800 | [diff] [blame] | 197 | if (hideMenu) { |
| 198 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
| 199 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | public void showClusterDialog(final ClusterRunner clusterRunner) { |
| 203 | createDialogData(); |
| 204 | final ArrayList<Integer> actions = mActions; |
| 205 | new AlertDialog.Builder(mContext).setTitle(R.string.group_by).setItems( |
| 206 | mTitles, new DialogInterface.OnClickListener() { |
| 207 | public void onClick(DialogInterface dialog, int which) { |
| 208 | clusterRunner.doCluster(actions.get(which).intValue()); |
| 209 | } |
| 210 | }).create().show(); |
| 211 | } |
| 212 | |
| 213 | public void setTitle(String title) { |
| 214 | if (mActionBar != null) mActionBar.setTitle(title); |
| 215 | } |
| 216 | |
| 217 | public void setTitle(int titleId) { |
| 218 | if (mActionBar != null) mActionBar.setTitle(titleId); |
| 219 | } |
| 220 | |
| 221 | public void setSubtitle(String title) { |
| 222 | if (mActionBar != null) mActionBar.setSubtitle(title); |
| 223 | } |
| 224 | |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 225 | public boolean setSelectedAction(int type) { |
| 226 | for (int i = 0, n = sClusterItems.length; i < n; i++) { |
Owen Lin | 6cf8074 | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 227 | ActionItem item = sClusterItems[i]; |
Ray Chen | 00b5f3c | 2011-11-18 15:00:32 +0800 | [diff] [blame] | 228 | if (item.action == type) { |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 229 | mActionBar.setSelectedNavigationItem(i); |
| 230 | mCurrentIndex = i; |
Owen Lin | 6cf8074 | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | return false; |
| 235 | } |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 236 | |
Owen Lin | 5ccc96a | 2011-09-15 21:30:16 +0800 | [diff] [blame] | 237 | @Override |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 238 | public boolean onNavigationItemSelected(int itemPosition, long itemId) { |
| 239 | if (itemPosition != mCurrentIndex && mClusterRunner != null) { |
Owen Lin | 5ccc96a | 2011-09-15 21:30:16 +0800 | [diff] [blame] | 240 | mActivity.getGLRoot().lockRenderThread(); |
| 241 | try { |
| 242 | mClusterRunner.doCluster(sClusterItems[itemPosition].action); |
| 243 | } finally { |
| 244 | mActivity.getGLRoot().unlockRenderThread(); |
| 245 | } |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 246 | } |
Ray Chen | b5f8d90 | 2011-09-13 18:35:28 +0800 | [diff] [blame] | 247 | return false; |
| 248 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 249 | } |