GalleryActionBar.java revision 7817979db0c52ffeacb951625b1e821eba303285
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
19f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.app.ActionBar;
20f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.app.Activity;
21f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.app.AlertDialog;
228cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chenimport android.app.ActionBar.OnMenuVisibilityListener;
23f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.content.Context;
24f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.content.DialogInterface;
25b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.view.LayoutInflater;
26f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.view.Menu;
27f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.view.MenuItem;
28b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.view.View;
29b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.view.ViewGroup;
30b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.widget.BaseAdapter;
31f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Linimport android.widget.ShareActionProvider;
32b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenimport android.widget.TextView;
336cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin
342b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Linimport com.android.gallery3d.R;
352b3ee0ea07246b859a5b75d8a6102a7cce7ec838Owen Lin
366cf807453fb58ccd4cc513ff187b306c7090e67cOwen Linimport java.util.ArrayList;
37f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
38b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chenpublic class GalleryActionBar implements ActionBar.OnNavigationListener {
397817979db0c52ffeacb951625b1e821eba303285Ahbong Chang    @SuppressWarnings("unused")
40f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static final String TAG = "GalleryActionBar";
41f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
428cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ClusterRunner mClusterRunner;
438cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private CharSequence[] mTitles;
448cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ArrayList<Integer> mActions;
458cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private Context mContext;
468cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private LayoutInflater mInflater;
478cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private GalleryActivity mActivity;
488cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ActionBar mActionBar;
498cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private int mCurrentIndex;
508cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    private ClusterAdapter mAdapter = new ClusterAdapter();
518cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
52f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public interface ClusterRunner {
53f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public void doCluster(int id);
54f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
55f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
56f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static class ActionItem {
57f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public int action;
58f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public boolean enabled;
59f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public boolean visible;
60b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public int spinnerTitle;
61f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public int dialogTitle;
62f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public int clusterBy;
63f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
64f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        public ActionItem(int action, boolean applied, boolean enabled, int title,
65f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                int clusterBy) {
66f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this(action, applied, enabled, title, title, clusterBy);
67f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
68f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
69b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public ActionItem(int action, boolean applied, boolean enabled, int spinnerTitle,
70f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                int dialogTitle, int clusterBy) {
71f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.action = action;
72f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.enabled = enabled;
73b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            this.spinnerTitle = spinnerTitle;
74f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.dialogTitle = dialogTitle;
75f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.clusterBy = clusterBy;
76f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            this.visible = true;
77f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
78f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
79f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
80f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private static final ActionItem[] sClusterItems = new ActionItem[] {
81f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_ALBUM, true, false, R.string.albums,
82f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.group_by_album),
83f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_LOCATION, true, false,
84f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.locations, R.string.location, R.string.group_by_location),
85f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_TIME, true, false, R.string.times,
86f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.time, R.string.group_by_time),
87f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_FACE, true, false, R.string.people,
88f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.group_by_faces),
89f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new ActionItem(FilterUtils.CLUSTER_BY_TAG, true, false, R.string.tags,
90f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                R.string.group_by_tags)
91f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    };
92f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
93b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    private class ClusterAdapter extends BaseAdapter {
94b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
957817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
96b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public int getCount() {
97b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return sClusterItems.length;
98b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
99b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1007817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
101b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public Object getItem(int position) {
102b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return sClusterItems[position];
103b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
104b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1057817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
106b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public long getItemId(int position) {
107b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return sClusterItems[position].action;
108b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
109b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1107817979db0c52ffeacb951625b1e821eba303285Ahbong Chang        @Override
111b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        public View getView(int position, View convertView, ViewGroup parent) {
112b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            if (convertView == null) {
1135ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin                convertView = mInflater.inflate(R.layout.action_bar_text,
114b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen                        parent, false);
115b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            }
116b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            TextView view = (TextView) convertView;
117b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            view.setText(sClusterItems[position].spinnerTitle);
118b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen            return convertView;
119b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
120b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    }
121b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
1228cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public static String getClusterByTypeString(Context context, int type) {
1238cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        for (ActionItem item : sClusterItems) {
1248cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            if (item.action == type) {
1258cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen                return context.getString(item.clusterBy);
1268cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            }
1278cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        }
1288cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        return null;
1298cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
1308cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
1318cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public static ShareActionProvider initializeShareActionProvider(Menu menu) {
1328cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        MenuItem item = menu.findItem(R.id.action_share);
1338cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        ShareActionProvider shareActionProvider = null;
1348cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (item != null) {
1358cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            shareActionProvider = (ShareActionProvider) item.getActionProvider();
1368cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        }
1378cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        return shareActionProvider;
1388cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
139f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
1405ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin    public GalleryActionBar(GalleryActivity activity) {
1415ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mActionBar = ((Activity) activity).getActionBar();
1425ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mContext = activity.getAndroidContext();
1435ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mActivity = activity;
1445ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin        mInflater = ((Activity) mActivity).getLayoutInflater();
145b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        mCurrentIndex = 0;
146f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
147f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
148f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    private void createDialogData() {
149f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        ArrayList<CharSequence> titles = new ArrayList<CharSequence>();
150f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mActions = new ArrayList<Integer>();
151f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        for (ActionItem item : sClusterItems) {
152f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (item.enabled && item.visible) {
153f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                titles.add(mContext.getString(item.dialogTitle));
154f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                mActions.add(item.action);
155f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
156f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
157f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        mTitles = new CharSequence[titles.size()];
158f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        titles.toArray(mTitles);
159f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
160f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
1618cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public int getHeight() {
1628cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        return mActionBar != null ? mActionBar.getHeight() : 0;
1638cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
1648cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
165f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setClusterItemEnabled(int id, boolean enabled) {
166f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        for (ActionItem item : sClusterItems) {
167f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (item.action == id) {
168f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                item.enabled = enabled;
169f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                return;
170f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
171f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
172f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
173f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
174f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setClusterItemVisibility(int id, boolean visible) {
175f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        for (ActionItem item : sClusterItems) {
176f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            if (item.action == id) {
177f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                item.visible = visible;
178f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                return;
179f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
180f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }
181f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
182f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
183f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public int getClusterTypeAction() {
184b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        return sClusterItems[mCurrentIndex].action;
185f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
186f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
187fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    public void enableClusterMenu(int action, ClusterRunner runner) {
1888cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) {
1898cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            // Don't set cluster runner until action bar is ready.
1908cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mClusterRunner = null;
1918cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mActionBar.setListNavigationCallbacks(mAdapter, this);
1928cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
1938cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            setSelectedAction(action);
1948cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mClusterRunner = runner;
1958cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        }
196f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
197f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
198fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    // The only use case not to hideMenu in this method is to ensure
199fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    // all elements disappear at the same time when exiting gallery.
200fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    // hideMenu should always be true in all other cases.
201fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen    public void disableClusterMenu(boolean hideMenu) {
2028cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) {
2038cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mClusterRunner = null;
2048cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            if (hideMenu) {
2058cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen                mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
2068cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            }
207fe1625c46f8258be4cd5e5e1ba69b584c1fe1a49Ray Chen        }
208f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
209f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
210f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void showClusterDialog(final ClusterRunner clusterRunner) {
211f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        createDialogData();
212f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        final ArrayList<Integer> actions = mActions;
213f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        new AlertDialog.Builder(mContext).setTitle(R.string.group_by).setItems(
214f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin                mTitles, new DialogInterface.OnClickListener() {
2157817979db0c52ffeacb951625b1e821eba303285Ahbong Chang            @Override
216f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            public void onClick(DialogInterface dialog, int which) {
217677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                // Need to lock rendering when operations invoked by system UI (main thread) are
218677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                // modifying slot data used in GL thread for rendering.
219677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                mActivity.getGLRoot().lockRenderThread();
220677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                try {
221677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                    clusterRunner.doCluster(actions.get(which).intValue());
222677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                } finally {
223677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                    mActivity.getGLRoot().unlockRenderThread();
224677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang                }
225f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin            }
226f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        }).create().show();
227f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
228f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
22984c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen    public void setDisplayOptions(boolean displayHomeAsUp, boolean showTitle) {
23084c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen        if (mActionBar != null) {
23184c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen            int options = (displayHomeAsUp ? ActionBar.DISPLAY_HOME_AS_UP : 0) |
23284c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen                    (showTitle ? ActionBar.DISPLAY_SHOW_TITLE : 0);
2338cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen            mActionBar.setDisplayOptions(options,
23484c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen                    ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
23584c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen            mActionBar.setHomeButtonEnabled(displayHomeAsUp);
23684c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen        }
23784c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen    }
23884c220f10a643927c8a2126de8a755d8d7f7ec9eRay Chen
239f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setTitle(String title) {
240f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (mActionBar != null) mActionBar.setTitle(title);
241f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
242f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
243f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setTitle(int titleId) {
244f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (mActionBar != null) mActionBar.setTitle(titleId);
245f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
246f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
247f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    public void setSubtitle(String title) {
248f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin        if (mActionBar != null) mActionBar.setSubtitle(title);
249f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin    }
250f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin
2518cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void show() {
2528cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.show();
2538cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2548cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
2558cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void hide() {
2568cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.hide();
2578cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2588cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
2598cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void addOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
2608cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.addOnMenuVisibilityListener(listener);
2618cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2628cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
2638cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    public void removeOnMenuVisibilityListener(OnMenuVisibilityListener listener) {
2648cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar != null) mActionBar.removeOnMenuVisibilityListener(listener);
2658cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen    }
2668cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
267b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    public boolean setSelectedAction(int type) {
2688cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen        if (mActionBar == null) return false;
2698cab3e872dd95e55ba34fdb94269a0c5069e72aeRay Chen
270b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        for (int i = 0, n = sClusterItems.length; i < n; i++) {
2716cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin            ActionItem item = sClusterItems[i];
27200b5f3c66113d14a7fc6f37d3a4e9d0c9b566eaaRay Chen            if (item.action == type) {
273b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen                mActionBar.setSelectedNavigationItem(i);
274b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen                mCurrentIndex = i;
2756cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin                return true;
2766cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin            }
2776cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin        }
2786cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin        return false;
2796cf807453fb58ccd4cc513ff187b306c7090e67cOwen Lin    }
280b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen
2815ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin    @Override
282b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    public boolean onNavigationItemSelected(int itemPosition, long itemId) {
283b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        if (itemPosition != mCurrentIndex && mClusterRunner != null) {
284677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang            // Need to lock rendering when operations invoked by system UI (main thread) are
285677584c7242d21ef2ca8adbedb5f9e3f016b54b3Yuli Huang            // modifying slot data used in GL thread for rendering.
2865ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            mActivity.getGLRoot().lockRenderThread();
2875ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            try {
2885ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin                mClusterRunner.doCluster(sClusterItems[itemPosition].action);
2895ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            } finally {
2905ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin                mActivity.getGLRoot().unlockRenderThread();
2915ccc96ac39f9513f13977de4419d814112c23ab8Owen Lin            }
292b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        }
293b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen        return false;
294b5f8d908c5dd287052cd49eddaf1f82d858cca03Ray Chen    }
295f9a0a4306d589b4a4e20554fed512a603426bfa1Owen Lin}
296