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 com.android.gallery3d.R; |
| 20 | |
| 21 | import android.app.ActionBar; |
| 22 | import android.app.ActionBar.Tab; |
| 23 | import android.app.Activity; |
| 24 | import android.app.AlertDialog; |
| 25 | import android.app.FragmentTransaction; |
| 26 | import android.content.Context; |
| 27 | import android.content.DialogInterface; |
| 28 | import android.view.Menu; |
| 29 | import android.view.MenuItem; |
| 30 | import android.widget.ShareActionProvider; |
Owen Lin | 6cf8074 | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 31 | |
| 32 | import java.util.ArrayList; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 33 | |
| 34 | public class GalleryActionBar implements ActionBar.TabListener { |
| 35 | private static final String TAG = "GalleryActionBar"; |
| 36 | |
| 37 | public interface ClusterRunner { |
| 38 | public void doCluster(int id); |
| 39 | } |
| 40 | |
| 41 | private static class ActionItem { |
| 42 | public int action; |
| 43 | public boolean enabled; |
| 44 | public boolean visible; |
| 45 | public int tabTitle; |
| 46 | public int dialogTitle; |
| 47 | public int clusterBy; |
| 48 | |
| 49 | public ActionItem(int action, boolean applied, boolean enabled, int title, |
| 50 | int clusterBy) { |
| 51 | this(action, applied, enabled, title, title, clusterBy); |
| 52 | } |
| 53 | |
| 54 | public ActionItem(int action, boolean applied, boolean enabled, int tabTitle, |
| 55 | int dialogTitle, int clusterBy) { |
| 56 | this.action = action; |
| 57 | this.enabled = enabled; |
| 58 | this.tabTitle = tabTitle; |
| 59 | this.dialogTitle = dialogTitle; |
| 60 | this.clusterBy = clusterBy; |
| 61 | this.visible = true; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private static final ActionItem[] sClusterItems = new ActionItem[] { |
| 66 | new ActionItem(FilterUtils.CLUSTER_BY_ALBUM, true, false, R.string.albums, |
| 67 | R.string.group_by_album), |
| 68 | new ActionItem(FilterUtils.CLUSTER_BY_LOCATION, true, false, |
| 69 | R.string.locations, R.string.location, R.string.group_by_location), |
| 70 | new ActionItem(FilterUtils.CLUSTER_BY_TIME, true, false, R.string.times, |
| 71 | R.string.time, R.string.group_by_time), |
| 72 | new ActionItem(FilterUtils.CLUSTER_BY_FACE, true, false, R.string.people, |
| 73 | R.string.group_by_faces), |
| 74 | new ActionItem(FilterUtils.CLUSTER_BY_TAG, true, false, R.string.tags, |
| 75 | R.string.group_by_tags) |
| 76 | }; |
| 77 | |
| 78 | private ClusterRunner mClusterRunner; |
| 79 | private CharSequence[] mTitles; |
| 80 | private ArrayList<Integer> mActions; |
| 81 | private Context mContext; |
| 82 | private ActionBar mActionBar; |
| 83 | // We need this because ActionBar.getSelectedTab() doesn't work when |
| 84 | // ActionBar is hidden. |
| 85 | private Tab mCurrentTab; |
| 86 | |
| 87 | public GalleryActionBar(Activity activity) { |
| 88 | mActionBar = activity.getActionBar(); |
| 89 | mContext = activity; |
| 90 | |
| 91 | for (ActionItem item : sClusterItems) { |
| 92 | mActionBar.addTab(mActionBar.newTab().setText(item.tabTitle). |
| 93 | setTag(item).setTabListener(this)); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public static int getHeight(Activity activity) { |
| 98 | ActionBar actionBar = activity.getActionBar(); |
| 99 | return actionBar != null ? actionBar.getHeight() : 0; |
| 100 | } |
| 101 | |
| 102 | private void createDialogData() { |
| 103 | ArrayList<CharSequence> titles = new ArrayList<CharSequence>(); |
| 104 | mActions = new ArrayList<Integer>(); |
| 105 | for (ActionItem item : sClusterItems) { |
| 106 | if (item.enabled && item.visible) { |
| 107 | titles.add(mContext.getString(item.dialogTitle)); |
| 108 | mActions.add(item.action); |
| 109 | } |
| 110 | } |
| 111 | mTitles = new CharSequence[titles.size()]; |
| 112 | titles.toArray(mTitles); |
| 113 | } |
| 114 | |
| 115 | public void setClusterItemEnabled(int id, boolean enabled) { |
| 116 | for (ActionItem item : sClusterItems) { |
| 117 | if (item.action == id) { |
| 118 | item.enabled = enabled; |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | public void setClusterItemVisibility(int id, boolean visible) { |
| 125 | for (ActionItem item : sClusterItems) { |
| 126 | if (item.action == id) { |
| 127 | item.visible = visible; |
| 128 | return; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | public int getClusterTypeAction() { |
| 134 | if (mCurrentTab != null) { |
| 135 | ActionItem item = (ActionItem) mCurrentTab.getTag(); |
| 136 | return item.action; |
| 137 | } |
| 138 | // By default, it's group-by-album |
| 139 | return FilterUtils.CLUSTER_BY_ALBUM; |
| 140 | } |
| 141 | |
| 142 | public static String getClusterByTypeString(Context context, int type) { |
| 143 | for (ActionItem item : sClusterItems) { |
| 144 | if (item.action == type) { |
| 145 | return context.getString(item.clusterBy); |
| 146 | } |
| 147 | } |
| 148 | return null; |
| 149 | } |
| 150 | |
| 151 | public static ShareActionProvider initializeShareActionProvider(Menu menu) { |
| 152 | MenuItem item = menu.findItem(R.id.action_share); |
| 153 | ShareActionProvider shareActionProvider = null; |
| 154 | if (item != null) { |
| 155 | shareActionProvider = (ShareActionProvider) item.getActionProvider(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 156 | } |
| 157 | return shareActionProvider; |
| 158 | } |
| 159 | |
| 160 | public void showClusterTabs(ClusterRunner runner) { |
Ray Chen | e9ae772 | 2011-08-30 16:34:18 +0800 | [diff] [blame] | 161 | Log.v(TAG, "showClusterTabs: runner=" + runner); |
Ray Chen | eab374b | 2011-09-09 11:20:58 +0800 | [diff] [blame^] | 162 | // setNavigationMode will trigger onTabSelected, so we should avoid |
| 163 | // triggering any callback here |
| 164 | mClusterRunner = null; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 165 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); |
| 166 | mClusterRunner = runner; |
| 167 | } |
| 168 | |
| 169 | public void hideClusterTabs() { |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 170 | mClusterRunner = null; |
Ray Chen | eab374b | 2011-09-09 11:20:58 +0800 | [diff] [blame^] | 171 | mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); |
Ray Chen | e9ae772 | 2011-08-30 16:34:18 +0800 | [diff] [blame] | 172 | Log.v(TAG, "hideClusterTabs: runner=" + mClusterRunner); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void showClusterDialog(final ClusterRunner clusterRunner) { |
| 176 | createDialogData(); |
| 177 | final ArrayList<Integer> actions = mActions; |
| 178 | new AlertDialog.Builder(mContext).setTitle(R.string.group_by).setItems( |
| 179 | mTitles, new DialogInterface.OnClickListener() { |
| 180 | public void onClick(DialogInterface dialog, int which) { |
| 181 | clusterRunner.doCluster(actions.get(which).intValue()); |
| 182 | } |
| 183 | }).create().show(); |
| 184 | } |
| 185 | |
| 186 | public void setTitle(String title) { |
| 187 | if (mActionBar != null) mActionBar.setTitle(title); |
| 188 | } |
| 189 | |
| 190 | public void setTitle(int titleId) { |
| 191 | if (mActionBar != null) mActionBar.setTitle(titleId); |
| 192 | } |
| 193 | |
| 194 | public void setSubtitle(String title) { |
| 195 | if (mActionBar != null) mActionBar.setSubtitle(title); |
| 196 | } |
| 197 | |
| 198 | public void setNavigationMode(int mode) { |
| 199 | if (mActionBar != null) mActionBar.setNavigationMode(mode); |
| 200 | } |
| 201 | |
| 202 | public int getHeight() { |
| 203 | return mActionBar == null ? 0 : mActionBar.getHeight(); |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onTabSelected(Tab tab, FragmentTransaction ft) { |
| 208 | if (mCurrentTab == tab) return; |
| 209 | mCurrentTab = tab; |
| 210 | ActionItem item = (ActionItem) tab.getTag(); |
Ray Chen | e9ae772 | 2011-08-30 16:34:18 +0800 | [diff] [blame] | 211 | Log.v(TAG, "onTabSelected: clusterrRunner=" + mClusterRunner); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 212 | if (mClusterRunner != null) mClusterRunner.doCluster(item.action); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void onTabUnselected(Tab tab, FragmentTransaction ft) { |
| 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onTabReselected(Tab tab, FragmentTransaction ft) { |
| 221 | } |
Owen Lin | 6cf8074 | 2011-08-28 10:50:21 +0800 | [diff] [blame] | 222 | |
| 223 | public boolean setSelectedTab(int type) { |
| 224 | for (int i = 0, n = sClusterItems.length; i < n; ++i) { |
| 225 | ActionItem item = sClusterItems[i]; |
| 226 | if (item.visible && item.action == type) { |
| 227 | mActionBar.selectTab(mActionBar.getTabAt(i)); |
| 228 | return true; |
| 229 | } |
| 230 | } |
| 231 | return false; |
| 232 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 233 | } |