GalleryActionBar.java revision f98aafb7eb65b889b0f831d607828ca4f8af64b6
1f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin/*
2f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * Copyright (C) 2011 The Android Open Source Project
3f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *
4f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * Licensed under the Apache License, Version 2.0 (the "License");
5f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * you may not use this file except in compliance with the License.
6f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * You may obtain a copy of the License at
7f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *
8f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *      http://www.apache.org/licenses/LICENSE-2.0
9f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin *
10f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * Unless required by applicable law or agreed to in writing, software
11f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * distributed under the License is distributed on an "AS IS" BASIS,
12f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * See the License for the specific language governing permissions and
14f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin * limitations under the License.
15f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin */
16f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
17f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linpackage com.android.gallery3d.app;
18f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
19cc8e7ab7ae0912186beff20e7f042fd6c1e78b35Owen Linimport android.annotation.TargetApi;
20f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.app.Activity;
21f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.app.AlertDialog;
22f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.content.Context;
23f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.content.DialogInterface;
242bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Linimport android.content.Intent;
25b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.view.LayoutInflater;
26b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.view.View;
27b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.view.ViewGroup;
28b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.widget.BaseAdapter;
29b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.widget.TextView;
306cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin
31b21b8e58a604f6c701245d84b141b5b87663192bOwen Linimport com.actionbarsherlock.app.ActionBar;
32b21b8e58a604f6c701245d84b141b5b87663192bOwen Linimport com.actionbarsherlock.app.ActionBar.OnMenuVisibilityListener;
33b21b8e58a604f6c701245d84b141b5b87663192bOwen Linimport com.actionbarsherlock.app.ActionBar.OnNavigationListener;
34b21b8e58a604f6c701245d84b141b5b87663192bOwen Linimport com.actionbarsherlock.view.Menu;
35b21b8e58a604f6c701245d84b141b5b87663192bOwen Linimport com.actionbarsherlock.view.MenuItem;
36b21b8e58a604f6c701245d84b141b5b87663192bOwen Linimport com.actionbarsherlock.widget.ShareActionProvider;
372b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport com.android.gallery3d.R;
38cc8e7ab7ae0912186beff20e7f042fd6c1e78b35Owen Linimport com.android.gallery3d.common.ApiHelper;
392b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Lin
406cf807453fb58ccd4cc513ff187b306c7090e67cOwen Linimport java.util.ArrayList;
41f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
42b21b8e58a604f6c701245d84b141b5b87663192bOwen Linpublic class GalleryActionBar implements OnNavigationListener {
437817979db0c52ffeacb951625b1e821eba303285Ahbong Chang    @SuppressWarnings("unused")
44f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static final String TAG = "GalleryActionBar";
45f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
468cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ClusterRunner mClusterRunner;
478cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private CharSequence[] mTitles;
488cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ArrayList<Integer> mActions;
498cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private Context mContext;
508cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private LayoutInflater mInflater;
51b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    private AbstractGalleryActivity mActivity;
52b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    private ActionBar mActionBar;
538cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private int mCurrentIndex;
548cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ClusterAdapter mAdapter = new ClusterAdapter();
558cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
56f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public interface ClusterRunner {
57f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public void doCluster(int id);
58f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
59f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
60f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static class ActionItem {
61f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public int action;
62f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public boolean enabled;
63f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public boolean visible;
64b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public int spinnerTitle;
65f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public int dialogTitle;
66f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public int clusterBy;
67f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
68f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public ActionItem(int action, boolean applied, boolean enabled, int title,
69f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                int clusterBy) {
70f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this(action, applied, enabled, title, title, clusterBy);
71f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
72f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
73b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle,
74f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                int dialogTitle, int clusterBy) {
75f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.action = action;
76f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.enabled = enabled;
77b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            this.spinnerTitle = spinnerTitle;
78f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.dialogTitle = dialogTitle;
79f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.clusterBy = clusterBy;
80f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.visible = true;
81f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
82f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
83f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
84f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static final ActionItem[] sClusterItems = new ActionItem[] {
85f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_ALBUM, true, false, R.string.albums,
86f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.group_by_album),
87f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_LOCATION, true, false,
88f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.locations, R.string.location, R.string.group_by_location),
89f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_TIME, true, false, R.string.times,
90f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.time, R.string.group_by_time),
91f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_FACE, true, false, R.string.people,
92f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.group_by_faces),
93f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_TAG, true, false, R.string.tags,
94f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.group_by_tags)
95f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    };
96f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
97b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    private class ClusterAdapter extends BaseAdapter {
98b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
997817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
100b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public int getCount() {
101b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return sClusterItems.length;
102b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
103b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1047817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
105b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public Object getItem(int position) {
106b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return sClusterItems[position];
107b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
108b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1097817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
110b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public long getItemId(int position) {
111b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return sClusterItems[position].action;
112b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
113b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1147817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
115b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public View getView(int position, View convertView, ViewGroup parent) {
116b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            if (convertView == null) {
1175ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin                convertView = mInflater.inflate(R.layout.action_bar_text,
118b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen                        parent, false);
119b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            }
120b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            TextView view = (TextView) convertView;
121b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            view.setText(sClusterItems[position].spinnerTitle);
122b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return convertView;
123b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
124b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    }
125b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1268cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public static String getClusterByTypeString(Context context, int type) {
1278cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        for (ActionItem item : sClusterItems) {
1288cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            if (item.action == type) {
1298cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen                return context.getString(item.clusterBy);
1308cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            }
1318cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        }
1328cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        return null;
1338cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
1348cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
135b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    public GalleryActionBar(AbstractGalleryActivity activity) {
136b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        mActionBar = activity.getSupportActionBar();
1375ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mContext = activity.getAndroidContext();
1385ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mActivity = activity;
1395ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mInflater = ((Activity) mActivity).getLayoutInflater();
140b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        mCurrentIndex = 0;
141f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
142f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
143f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private void createDialogData() {
144f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        ArrayList<CharSequence> titles = new ArrayList<CharSequence>();
145f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mActions = new ArrayList<Integer>();
146f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        for (ActionItem item : sClusterItems) {
147f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (item.enabled && item.visible) {
148f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                titles.add(mContext.getString(item.dialogTitle));
149f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                mActions.add(item.action);
150f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
151f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
152f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mTitles = new CharSequence[titles.size()];
153f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        titles.toArray(mTitles);
154f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
155f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
1568cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public int getHeight() {
1578cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        return mActionBar != null ? mActionBar.getHeight() : 0;
1588cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
1598cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
160f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setClusterItemEnabled(int id, boolean enabled) {
161f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        for (ActionItem item : sClusterItems) {
162f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (item.action == id) {
163f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                item.enabled = enabled;
164f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                return;
165f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
166f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
167f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
168f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
169f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setClusterItemVisibility(int id, boolean visible) {
170f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        for (ActionItem item : sClusterItems) {
171f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (item.action == id) {
172f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                item.visible = visible;
173f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                return;
174f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
175f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
176f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
177f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
178f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public int getClusterTypeAction() {
179b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        return sClusterItems[mCurrentIndex].action;
180f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
181f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
182fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    public void enableClusterMenu(int action, ClusterRunner runner) {
1838cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) {
1848cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            // Don't set cluster runner until action bar is ready.
1858cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mClusterRunner = null;
1868cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mActionBar.setListNavigationCallbacks(mAdapter, this);
187b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
1888cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            setSelectedAction(action);
1898cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mClusterRunner = runner;
1908cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        }
191f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
192f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
193fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    // The only use case not to hideMenu in this method is to ensure
194fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    // all elements disappear at the same time when exiting gallery.
195fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    // hideMenu should always be true in all other cases.
196fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    public void disableClusterMenu(boolean hideMenu) {
1978cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) {
1988cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mClusterRunner = null;
1998cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            if (hideMenu) {
200b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
2018cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            }
202fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen        }
203f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
204f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
205f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void showClusterDialog(final ClusterRunner clusterRunner) {
206f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        createDialogData();
207f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        final ArrayList<Integer> actions = mActions;
208f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new AlertDialog.Builder(mContext).setTitle(R.string.group_by).setItems(
209f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                mTitles, new DialogInterface.OnClickListener() {
2107817979db0c52ffeacb951625b1e821eba303285Ahbong Chang            @Override
211f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            public void onClick(DialogInterface dialog, int which) {
212677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                // Need to lock rendering when operations invoked by system UI (main thread) are
213677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                // modifying slot data used in GL thread for rendering.
214677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                mActivity.getGLRoot().lockRenderThread();
215677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                try {
216677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                    clusterRunner.doCluster(actions.get(which).intValue());
217677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                } finally {
218677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                    mActivity.getGLRoot().unlockRenderThread();
219677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                }
220f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
221f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }).create().show();
222f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
223f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
224cc8e7ab7ae0912186beff20e7f042fd6c1e78b35Owen Lin    @TargetApi(ApiHelper.VERSION_CODES.ICE_CREAM_SANDWICH)
225cc8e7ab7ae0912186beff20e7f042fd6c1e78b35Owen Lin    private void setHomeButtonEnabled(boolean enabled) {
226b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        if (mActionBar != null) mActionBar.setHomeButtonEnabled(enabled);
227cc8e7ab7ae0912186beff20e7f042fd6c1e78b35Owen Lin    }
228cc8e7ab7ae0912186beff20e7f042fd6c1e78b35Owen Lin
22984c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen    public void setDisplayOptions(boolean displayHomeAsUp, boolean showTitle) {
2302bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin        if (mActionBar == null) return;
2312bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin        int options = 0;
232b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        if (displayHomeAsUp) options |= ActionBar.DISPLAY_HOME_AS_UP;
233b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        if (showTitle) options |= ActionBar.DISPLAY_SHOW_TITLE;
2342bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin
2352bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin        mActionBar.setDisplayOptions(options,
236b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
2372bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin        mActionBar.setHomeButtonEnabled(displayHomeAsUp);
23884c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen    }
23984c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen
240f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setTitle(String title) {
241f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (mActionBar != null) mActionBar.setTitle(title);
242f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
243f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
244f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setTitle(int titleId) {
2452bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin        if (mActionBar != null) {
2462bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin            mActionBar.setTitle(mContext.getString(titleId));
2472bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin        }
248f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
249f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
250f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setSubtitle(String title) {
251f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (mActionBar != null) mActionBar.setSubtitle(title);
252f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
253f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
2548cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void show() {
2558cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.show();
2568cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2578cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
2588cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void hide() {
2598cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.hide();
2608cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2618cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
2628cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
2638cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.addOnMenuVisibilityListener(listener);
2648cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2658cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
2668cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
2678cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.removeOnMenuVisibilityListener(listener);
2688cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2698cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
270b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    public boolean setSelectedAction(int type) {
2718cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar == null) return false;
2728cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
273b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        for (int i = 0, n = sClusterItems.length; i < n; i++) {
2746cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin            ActionItem item = sClusterItems[i];
27500b5f3c66113d14a7fc6f37d3a4e9d0c9b566eaaRay Chen            if (item.action == type) {
276b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen                mActionBar.setSelectedNavigationItem(i);
277b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen                mCurrentIndex = i;
2786cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin                return true;
2796cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin            }
2806cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin        }
2816cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin        return false;
2826cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin    }
283b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
2845ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin    @Override
285b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
286b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        if (itemPosition != mCurrentIndex && mClusterRunner != null) {
287677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang            // Need to lock rendering when operations invoked by system UI (main thread) are
288677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang            // modifying slot data used in GL thread for rendering.
2895ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            mActivity.getGLRoot().lockRenderThread();
2905ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            try {
2915ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin                mClusterRunner.doCluster(sClusterItems[itemPosition].action);
2925ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            } finally {
2935ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin                mActivity.getGLRoot().unlockRenderThread();
2945ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            }
295b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
296b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        return false;
297b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    }
2982bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin
299b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    private Menu mActionBarMenu;
3005172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware    private MenuItem mSharePanoramaMenuItem;
301b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    private MenuItem mShareMenuItem;
3025172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware    private ShareActionProvider mSharePanoramaActionProvider;
3035172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware    private ShareActionProvider mShareActionProvider;
3042bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin
305b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    public void createActionBarMenu(int menuRes, Menu menu) {
306b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        mActivity.getSupportMenuInflater().inflate(menuRes, menu);
307b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        mActionBarMenu = menu;
3085172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware
3095172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        mSharePanoramaMenuItem = menu.findItem(R.id.action_share_panorama);
3105172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        mSharePanoramaActionProvider = (ShareActionProvider)
3115172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware            mSharePanoramaMenuItem.getActionProvider();
3125172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        mSharePanoramaActionProvider.setShareHistoryFileName("panorama_share_history.xml");
3135172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware
314b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        mShareMenuItem = menu.findItem(R.id.action_share);
3155172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        mShareActionProvider = (ShareActionProvider)
3165172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware            mShareMenuItem.getActionProvider();
3175172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        mShareActionProvider.setShareHistoryFileName("share_history.xml");
3182bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin    }
3192bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin
320b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin    public Menu getMenu() {
321b21b8e58a604f6c701245d84b141b5b87663192bOwen Lin        return mActionBarMenu;
3222bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin    }
3232bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin
3245172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware    public void setShareIntents(Intent sharePanoramaIntent, Intent shareIntent) {
3255172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        // if panorama sharing is enabled, rename share to share as photo,
3265172dee697c05235ddf932827d75e6fa0b81d382Mangesh Ghiware        // and move it to overflow
327f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware        if (mSharePanoramaMenuItem != null) {
328f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware            if (sharePanoramaIntent != null) {
329f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                mActivity.invalidateOptionsMenu();
330f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                mShareMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
331f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                mShareMenuItem.setTitle(
332f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                        mContext.getResources().getString(R.string.share_as_photo));
333f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware            } else {
334f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                mSharePanoramaMenuItem.setVisible(false);
335f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                mShareMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
336f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                mShareMenuItem.setTitle(
337f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware                        mContext.getResources().getString(R.string.share));
338f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware            }
339f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware            mSharePanoramaActionProvider.setShareIntent(sharePanoramaIntent);
340f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware        }
341f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware        if (mShareMenuItem != null) {
342f98aafb7eb65b889b0f831d607828ca4f8af64b6Mangesh Ghiware            mShareActionProvider.setShareIntent(shareIntent);
34324081fe26ca25448ddb9c8f9cc7f406d26344decOwen Lin        }
3442bb717f1ea38e2ce33dd102a23afe6bfacb5675cOwen Lin    }
345f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin}
346