AbstractGalleryActivity.java revision 840ae396bac4ff8ff34e88c8d2a1773cb112a4b6
1/*
2 * Copyright (C) 2010 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
17package com.android.gallery3d.app;
18
19import android.annotation.TargetApi;
20import android.app.Activity;
21import android.app.AlertDialog;
22import android.content.BroadcastReceiver;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.content.DialogInterface.OnCancelListener;
26import android.content.DialogInterface.OnClickListener;
27import android.content.Intent;
28import android.content.IntentFilter;
29import android.content.res.Configuration;
30import android.os.Bundle;
31import android.view.Menu;
32import android.view.MenuItem;
33import android.view.Window;
34import android.view.WindowManager;
35
36import com.android.gallery3d.R;
37import com.android.gallery3d.common.ApiHelper;
38import com.android.gallery3d.data.BitmapPool;
39import com.android.gallery3d.data.DataManager;
40import com.android.gallery3d.data.MediaItem;
41import com.android.gallery3d.ui.GLRoot;
42import com.android.gallery3d.ui.GLRootView;
43import com.android.gallery3d.util.ThreadPool;
44
45public class AbstractGalleryActivity extends Activity implements GalleryActivity {
46    @SuppressWarnings("unused")
47    private static final String TAG = "AbstractGalleryActivity";
48    private GLRootView mGLRootView;
49    private StateManager mStateManager;
50    private GalleryActionBar mActionBar;
51    private OrientationManager mOrientationManager;
52    private TransitionStore mTransitionStore = new TransitionStore();
53    private boolean mDisableToggleStatusBar;
54
55    private AlertDialog mAlertDialog = null;
56    private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
57        @Override
58        public void onReceive(Context context, Intent intent) {
59            if (getExternalCacheDir() != null) onStorageReady();
60        }
61    };
62    private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
63
64    @Override
65    protected void onCreate(Bundle savedInstanceState) {
66        super.onCreate(savedInstanceState);
67        mOrientationManager = new OrientationManager(this);
68        toggleStatusBarByOrientation();
69        getWindow().setBackgroundDrawable(null);
70    }
71
72    @Override
73    protected void onSaveInstanceState(Bundle outState) {
74        mGLRootView.lockRenderThread();
75        try {
76            super.onSaveInstanceState(outState);
77            getStateManager().saveState(outState);
78        } finally {
79            mGLRootView.unlockRenderThread();
80        }
81    }
82
83    @Override
84    public void onConfigurationChanged(Configuration config) {
85        super.onConfigurationChanged(config);
86        mStateManager.onConfigurationChange(config);
87        invalidateOptionsMenu();
88        toggleStatusBarByOrientation();
89    }
90
91    private Menu mOptionsMenu;
92
93    @Override
94    public boolean onCreateOptionsMenu(Menu menu) {
95        mOptionsMenu = menu;
96        super.onCreateOptionsMenu(menu);
97        return getStateManager().createOptionsMenu(menu);
98    }
99
100    @Override
101    @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
102    public void invalidateOptionsMenu() {
103        if (ApiHelper.HAS_ACTIVITY_INVALIDATE_OPTIONS_MENU) {
104            super.invalidateOptionsMenu();
105        } else if (mOptionsMenu != null) {
106            mOptionsMenu.clear();
107            getStateManager().createOptionsMenu(mOptionsMenu);
108        }
109    }
110
111    @Override
112    public Context getAndroidContext() {
113        return this;
114    }
115
116    @Override
117    public DataManager getDataManager() {
118        return ((GalleryApp) getApplication()).getDataManager();
119    }
120
121    @Override
122    public ThreadPool getThreadPool() {
123        return ((GalleryApp) getApplication()).getThreadPool();
124    }
125
126    @Override
127    public synchronized StateManager getStateManager() {
128        if (mStateManager == null) {
129            mStateManager = new StateManager(this);
130        }
131        return mStateManager;
132    }
133
134    @Override
135    public GLRoot getGLRoot() {
136        return mGLRootView;
137    }
138
139    @Override
140    public OrientationManager getOrientationManager() {
141        return mOrientationManager;
142    }
143
144    @Override
145    public void setContentView(int resId) {
146        super.setContentView(resId);
147        mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
148    }
149
150    protected void onStorageReady() {
151        if (mAlertDialog != null) {
152            mAlertDialog.dismiss();
153            mAlertDialog = null;
154            unregisterReceiver(mMountReceiver);
155        }
156    }
157
158    @Override
159    protected void onStart() {
160        super.onStart();
161        if (getExternalCacheDir() == null) {
162            OnCancelListener onCancel = new OnCancelListener() {
163                @Override
164                public void onCancel(DialogInterface dialog) {
165                    finish();
166                }
167            };
168            OnClickListener onClick = new OnClickListener() {
169                @Override
170                public void onClick(DialogInterface dialog, int which) {
171                    dialog.cancel();
172                }
173            };
174            AlertDialog.Builder builder = new AlertDialog.Builder(this)
175                    .setTitle(R.string.no_external_storage_title)
176                    .setMessage(R.string.no_external_storage)
177                    .setNegativeButton(android.R.string.cancel, onClick)
178                    .setOnCancelListener(onCancel);
179            if (ApiHelper.HAS_SET_ICON_ATTRIBUTE) {
180                setAlertDialogIconAttribute(builder);
181            } else {
182                builder.setIcon(android.R.drawable.ic_dialog_alert);
183            }
184            mAlertDialog = builder.show();
185            registerReceiver(mMountReceiver, mMountFilter);
186        }
187    }
188
189    @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB)
190    private static void setAlertDialogIconAttribute(
191            AlertDialog.Builder builder) {
192        builder.setIconAttribute(android.R.attr.alertDialogIcon);
193    }
194
195    @Override
196    protected void onStop() {
197        super.onStop();
198        if (mAlertDialog != null) {
199            unregisterReceiver(mMountReceiver);
200            mAlertDialog.dismiss();
201            mAlertDialog = null;
202        }
203    }
204
205    @Override
206    protected void onResume() {
207        super.onResume();
208        mGLRootView.lockRenderThread();
209        try {
210            getStateManager().resume();
211            getDataManager().resume();
212        } finally {
213            mGLRootView.unlockRenderThread();
214        }
215        mGLRootView.onResume();
216        mOrientationManager.resume();
217    }
218
219    @Override
220    protected void onPause() {
221        super.onPause();
222        mOrientationManager.pause();
223        mGLRootView.onPause();
224        mGLRootView.lockRenderThread();
225        try {
226            getStateManager().pause();
227            getDataManager().pause();
228        } finally {
229            mGLRootView.unlockRenderThread();
230        }
231        clearBitmapPool(MediaItem.getMicroThumbPool());
232        clearBitmapPool(MediaItem.getThumbPool());
233
234        MediaItem.getBytesBufferPool().clear();
235    }
236
237    private static void clearBitmapPool(BitmapPool pool) {
238        if (pool != null) pool.clear();
239    }
240
241    @Override
242    protected void onDestroy() {
243        super.onDestroy();
244        mGLRootView.lockRenderThread();
245        try {
246            getStateManager().destroy();
247        } finally {
248            mGLRootView.unlockRenderThread();
249        }
250    }
251
252    @Override
253    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
254        mGLRootView.lockRenderThread();
255        try {
256            getStateManager().notifyActivityResult(
257                    requestCode, resultCode, data);
258        } finally {
259            mGLRootView.unlockRenderThread();
260        }
261    }
262
263    @Override
264    public void onBackPressed() {
265        // send the back event to the top sub-state
266        GLRoot root = getGLRoot();
267        root.lockRenderThread();
268        try {
269            getStateManager().onBackPressed();
270        } finally {
271            root.unlockRenderThread();
272        }
273    }
274
275    @Override
276    public GalleryActionBar getGalleryActionBar() {
277        if (mActionBar == null) {
278            mActionBar = new GalleryActionBar(this);
279        }
280        return mActionBar;
281    }
282
283    @Override
284    public boolean onOptionsItemSelected(MenuItem item) {
285        GLRoot root = getGLRoot();
286        root.lockRenderThread();
287        try {
288            return getStateManager().itemSelected(item);
289        } finally {
290            root.unlockRenderThread();
291        }
292    }
293
294    protected void disableToggleStatusBar() {
295        mDisableToggleStatusBar = true;
296    }
297
298    // Shows status bar in portrait view, hide in landscape view
299    private void toggleStatusBarByOrientation() {
300        if (mDisableToggleStatusBar) return;
301
302        Window win = getWindow();
303        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
304            win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
305        } else {
306            win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
307        }
308    }
309
310    @Override
311    public TransitionStore getTransitionStore() {
312        return mTransitionStore;
313    }
314}
315