blob: 0987ac39f529f9b9755134921fa189c725cd1110 [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
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
Owen Linf9a0a432011-08-17 22:07:43 +080019import android.app.Activity;
20import android.app.AlertDialog;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.DialogInterface;
24import android.content.DialogInterface.OnCancelListener;
25import android.content.DialogInterface.OnClickListener;
26import android.content.Intent;
27import android.content.IntentFilter;
Owen Lin8e565702011-09-09 13:33:44 +080028import android.content.res.Configuration;
Owen Linf9a0a432011-08-17 22:07:43 +080029import android.os.Bundle;
Ray Chen655f63f2012-03-16 14:38:04 +080030import android.view.MenuItem;
Pin Ting4d50f732012-02-03 18:35:00 +080031import android.view.Window;
32import android.view.WindowManager;
Owen Linf9a0a432011-08-17 22:07:43 +080033
Owen Lin4bb59122012-03-07 17:39:56 +080034import com.android.gallery3d.R;
35import com.android.gallery3d.data.DataManager;
Owen Lind8fb81f2012-03-29 14:27:58 +080036import com.android.gallery3d.data.MediaItem;
Owen Lin4bb59122012-03-07 17:39:56 +080037import com.android.gallery3d.ui.GLRoot;
38import com.android.gallery3d.ui.GLRootView;
39import com.android.gallery3d.util.ThreadPool;
40
Owen Linf9a0a432011-08-17 22:07:43 +080041public class AbstractGalleryActivity extends Activity implements GalleryActivity {
42 @SuppressWarnings("unused")
43 private static final String TAG = "AbstractGalleryActivity";
44 private GLRootView mGLRootView;
45 private StateManager mStateManager;
Ray Chen8cab3e82012-03-20 16:32:57 +080046 private GalleryActionBar mActionBar;
Chih-Chung Changbd141b52012-04-26 10:10:49 +080047 private OrientationManager mOrientationManager;
Owen Lin616a70f2012-05-07 16:35:53 +080048 private TransitionStore mTransitionStore = new TransitionStore();
Wu-cheng Lib1db75c2012-04-24 18:33:10 +080049 private boolean mDisableToggleStatusBar;
Owen Linf9a0a432011-08-17 22:07:43 +080050
51 private AlertDialog mAlertDialog = null;
52 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
53 @Override
54 public void onReceive(Context context, Intent intent) {
55 if (getExternalCacheDir() != null) onStorageReady();
56 }
57 };
58 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
59
60 @Override
Pin Ting4d50f732012-02-03 18:35:00 +080061 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
Chih-Chung Changbd141b52012-04-26 10:10:49 +080063 mOrientationManager = new OrientationManager(this);
Pin Ting4d50f732012-02-03 18:35:00 +080064 toggleStatusBarByOrientation();
Owen Lin7896b2c2012-05-15 17:52:09 -070065 getWindow().setBackgroundDrawable(null);
Pin Ting4d50f732012-02-03 18:35:00 +080066 }
67
68 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080069 protected void onSaveInstanceState(Bundle outState) {
70 mGLRootView.lockRenderThread();
71 try {
72 super.onSaveInstanceState(outState);
73 getStateManager().saveState(outState);
74 } finally {
75 mGLRootView.unlockRenderThread();
76 }
77 }
78
Owen Lin8e565702011-09-09 13:33:44 +080079 @Override
80 public void onConfigurationChanged(Configuration config) {
81 super.onConfigurationChanged(config);
82 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080083 invalidateOptionsMenu();
Pin Ting4d50f732012-02-03 18:35:00 +080084 toggleStatusBarByOrientation();
Owen Lin8e565702011-09-09 13:33:44 +080085 }
86
Owen Linf9a0a432011-08-17 22:07:43 +080087 public Context getAndroidContext() {
88 return this;
89 }
90
Owen Linf9a0a432011-08-17 22:07:43 +080091 public DataManager getDataManager() {
92 return ((GalleryApp) getApplication()).getDataManager();
93 }
94
95 public ThreadPool getThreadPool() {
96 return ((GalleryApp) getApplication()).getThreadPool();
97 }
98
Owen Linf9a0a432011-08-17 22:07:43 +080099 public synchronized StateManager getStateManager() {
100 if (mStateManager == null) {
101 mStateManager = new StateManager(this);
102 }
103 return mStateManager;
104 }
105
106 public GLRoot getGLRoot() {
107 return mGLRootView;
108 }
109
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800110 public OrientationManager getOrientationManager() {
111 return mOrientationManager;
112 }
113
Owen Linf9a0a432011-08-17 22:07:43 +0800114 @Override
115 public void setContentView(int resId) {
116 super.setContentView(resId);
117 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
118 }
119
Owen Linf9a0a432011-08-17 22:07:43 +0800120 protected void onStorageReady() {
121 if (mAlertDialog != null) {
122 mAlertDialog.dismiss();
123 mAlertDialog = null;
124 unregisterReceiver(mMountReceiver);
125 }
126 }
127
128 @Override
129 protected void onStart() {
130 super.onStart();
131 if (getExternalCacheDir() == null) {
132 OnCancelListener onCancel = new OnCancelListener() {
133 @Override
134 public void onCancel(DialogInterface dialog) {
135 finish();
136 }
137 };
138 OnClickListener onClick = new OnClickListener() {
139 @Override
140 public void onClick(DialogInterface dialog, int which) {
141 dialog.cancel();
142 }
143 };
144 mAlertDialog = new AlertDialog.Builder(this)
Björn Lundén23b602e2012-05-31 23:51:07 +0200145 .setIconAttribute(android.R.attr.alertDialogIcon)
Owen Linfd3071d2012-06-21 17:05:10 +0800146 .setTitle(R.string.no_external_storage_title)
147 .setMessage(R.string.no_external_storage)
Owen Linf9a0a432011-08-17 22:07:43 +0800148 .setNegativeButton(android.R.string.cancel, onClick)
149 .setOnCancelListener(onCancel)
150 .show();
151 registerReceiver(mMountReceiver, mMountFilter);
152 }
153 }
154
155 @Override
156 protected void onStop() {
157 super.onStop();
158 if (mAlertDialog != null) {
159 unregisterReceiver(mMountReceiver);
160 mAlertDialog.dismiss();
161 mAlertDialog = null;
162 }
163 }
164
165 @Override
166 protected void onResume() {
167 super.onResume();
168 mGLRootView.lockRenderThread();
169 try {
170 getStateManager().resume();
171 getDataManager().resume();
172 } finally {
173 mGLRootView.unlockRenderThread();
174 }
175 mGLRootView.onResume();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800176 mOrientationManager.resume();
Owen Linf9a0a432011-08-17 22:07:43 +0800177 }
178
179 @Override
180 protected void onPause() {
181 super.onPause();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800182 mOrientationManager.pause();
Owen Linf9a0a432011-08-17 22:07:43 +0800183 mGLRootView.onPause();
184 mGLRootView.lockRenderThread();
185 try {
186 getStateManager().pause();
187 getDataManager().pause();
188 } finally {
189 mGLRootView.unlockRenderThread();
190 }
Owen Lind8fb81f2012-03-29 14:27:58 +0800191 MediaItem.getMicroThumbPool().clear();
Chih-Chung Changb8be1e02012-04-17 20:35:14 +0800192 MediaItem.getThumbPool().clear();
Owen Lincafd30f2012-04-09 10:15:35 +0800193 MediaItem.getBytesBufferPool().clear();
Owen Linf9a0a432011-08-17 22:07:43 +0800194 }
195
196 @Override
Wu-cheng Li3957efd2012-04-03 19:08:35 +0800197 protected void onDestroy() {
198 super.onDestroy();
199 mGLRootView.lockRenderThread();
200 try {
201 getStateManager().destroy();
202 } finally {
203 mGLRootView.unlockRenderThread();
204 }
205 }
206
207 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800208 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
209 mGLRootView.lockRenderThread();
210 try {
211 getStateManager().notifyActivityResult(
212 requestCode, resultCode, data);
213 } finally {
214 mGLRootView.unlockRenderThread();
215 }
216 }
217
218 @Override
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800219 public void onBackPressed() {
220 // send the back event to the top sub-state
221 GLRoot root = getGLRoot();
222 root.lockRenderThread();
223 try {
224 getStateManager().onBackPressed();
225 } finally {
226 root.unlockRenderThread();
227 }
228 }
229
230 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800231 public GalleryActionBar getGalleryActionBar() {
Ray Chen8cab3e82012-03-20 16:32:57 +0800232 if (mActionBar == null) {
233 mActionBar = new GalleryActionBar(this);
234 }
235 return mActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +0800236 }
Pin Ting4d50f732012-02-03 18:35:00 +0800237
Ray Chen655f63f2012-03-16 14:38:04 +0800238 @Override
239 public boolean onOptionsItemSelected(MenuItem item) {
240 GLRoot root = getGLRoot();
241 root.lockRenderThread();
242 try {
243 return getStateManager().itemSelected(item);
244 } finally {
245 root.unlockRenderThread();
246 }
247 }
248
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800249 protected void disableToggleStatusBar() {
250 mDisableToggleStatusBar = true;
251 }
252
Pin Ting4d50f732012-02-03 18:35:00 +0800253 // Shows status bar in portrait view, hide in landscape view
254 private void toggleStatusBarByOrientation() {
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800255 if (mDisableToggleStatusBar) return;
256
Ray Chenfa011a22012-02-07 20:23:44 +0800257 Window win = getWindow();
258 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
259 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
260 } else {
261 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
262 }
Pin Ting4d50f732012-02-03 18:35:00 +0800263 }
Owen Lin616a70f2012-05-07 16:35:53 +0800264
265 @Override
266 public TransitionStore getTransitionStore() {
267 return mTransitionStore;
268 }
Owen Linf9a0a432011-08-17 22:07:43 +0800269}