blob: 4ee7403603509276fbd64dc29d8a2bb341302456 [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;
Wu-cheng Lib1db75c2012-04-24 18:33:10 +080048 private boolean mDisableToggleStatusBar;
Owen Linf9a0a432011-08-17 22:07:43 +080049
50 private AlertDialog mAlertDialog = null;
51 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
52 @Override
53 public void onReceive(Context context, Intent intent) {
54 if (getExternalCacheDir() != null) onStorageReady();
55 }
56 };
57 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
58
59 @Override
Pin Ting4d50f732012-02-03 18:35:00 +080060 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
Chih-Chung Changbd141b52012-04-26 10:10:49 +080062 mOrientationManager = new OrientationManager(this);
Pin Ting4d50f732012-02-03 18:35:00 +080063 toggleStatusBarByOrientation();
Owen Lin7896b2c2012-05-15 17:52:09 -070064 getWindow().setBackgroundDrawable(null);
Pin Ting4d50f732012-02-03 18:35:00 +080065 }
66
67 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080068 protected void onSaveInstanceState(Bundle outState) {
69 mGLRootView.lockRenderThread();
70 try {
71 super.onSaveInstanceState(outState);
72 getStateManager().saveState(outState);
73 } finally {
74 mGLRootView.unlockRenderThread();
75 }
76 }
77
Owen Lin8e565702011-09-09 13:33:44 +080078 @Override
79 public void onConfigurationChanged(Configuration config) {
80 super.onConfigurationChanged(config);
81 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080082 invalidateOptionsMenu();
Pin Ting4d50f732012-02-03 18:35:00 +080083 toggleStatusBarByOrientation();
Owen Lin8e565702011-09-09 13:33:44 +080084 }
85
Owen Linf9a0a432011-08-17 22:07:43 +080086 public Context getAndroidContext() {
87 return this;
88 }
89
Owen Linf9a0a432011-08-17 22:07:43 +080090 public DataManager getDataManager() {
91 return ((GalleryApp) getApplication()).getDataManager();
92 }
93
94 public ThreadPool getThreadPool() {
95 return ((GalleryApp) getApplication()).getThreadPool();
96 }
97
Owen Linf9a0a432011-08-17 22:07:43 +080098 public synchronized StateManager getStateManager() {
99 if (mStateManager == null) {
100 mStateManager = new StateManager(this);
101 }
102 return mStateManager;
103 }
104
105 public GLRoot getGLRoot() {
106 return mGLRootView;
107 }
108
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800109 public OrientationManager getOrientationManager() {
110 return mOrientationManager;
111 }
112
Owen Linf9a0a432011-08-17 22:07:43 +0800113 @Override
114 public void setContentView(int resId) {
115 super.setContentView(resId);
116 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
117 }
118
Owen Linf9a0a432011-08-17 22:07:43 +0800119 protected void onStorageReady() {
120 if (mAlertDialog != null) {
121 mAlertDialog.dismiss();
122 mAlertDialog = null;
123 unregisterReceiver(mMountReceiver);
124 }
125 }
126
127 @Override
128 protected void onStart() {
129 super.onStart();
130 if (getExternalCacheDir() == null) {
131 OnCancelListener onCancel = new OnCancelListener() {
132 @Override
133 public void onCancel(DialogInterface dialog) {
134 finish();
135 }
136 };
137 OnClickListener onClick = new OnClickListener() {
138 @Override
139 public void onClick(DialogInterface dialog, int which) {
140 dialog.cancel();
141 }
142 };
143 mAlertDialog = new AlertDialog.Builder(this)
144 .setIcon(android.R.drawable.ic_dialog_alert)
145 .setTitle("No Storage")
146 .setMessage("No external storage available.")
147 .setNegativeButton(android.R.string.cancel, onClick)
148 .setOnCancelListener(onCancel)
149 .show();
150 registerReceiver(mMountReceiver, mMountFilter);
151 }
152 }
153
154 @Override
155 protected void onStop() {
156 super.onStop();
157 if (mAlertDialog != null) {
158 unregisterReceiver(mMountReceiver);
159 mAlertDialog.dismiss();
160 mAlertDialog = null;
161 }
162 }
163
164 @Override
165 protected void onResume() {
166 super.onResume();
167 mGLRootView.lockRenderThread();
168 try {
169 getStateManager().resume();
170 getDataManager().resume();
171 } finally {
172 mGLRootView.unlockRenderThread();
173 }
174 mGLRootView.onResume();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800175 mOrientationManager.resume();
Owen Linf9a0a432011-08-17 22:07:43 +0800176 }
177
178 @Override
179 protected void onPause() {
180 super.onPause();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800181 mOrientationManager.pause();
Owen Linf9a0a432011-08-17 22:07:43 +0800182 mGLRootView.onPause();
183 mGLRootView.lockRenderThread();
184 try {
185 getStateManager().pause();
186 getDataManager().pause();
187 } finally {
188 mGLRootView.unlockRenderThread();
189 }
Owen Lind8fb81f2012-03-29 14:27:58 +0800190 MediaItem.getMicroThumbPool().clear();
Chih-Chung Changb8be1e02012-04-17 20:35:14 +0800191 MediaItem.getThumbPool().clear();
Owen Lincafd30f2012-04-09 10:15:35 +0800192 MediaItem.getBytesBufferPool().clear();
Owen Linf9a0a432011-08-17 22:07:43 +0800193 }
194
195 @Override
Wu-cheng Li3957efd2012-04-03 19:08:35 +0800196 protected void onDestroy() {
197 super.onDestroy();
198 mGLRootView.lockRenderThread();
199 try {
200 getStateManager().destroy();
201 } finally {
202 mGLRootView.unlockRenderThread();
203 }
204 }
205
206 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800207 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
208 mGLRootView.lockRenderThread();
209 try {
210 getStateManager().notifyActivityResult(
211 requestCode, resultCode, data);
212 } finally {
213 mGLRootView.unlockRenderThread();
214 }
215 }
216
217 @Override
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800218 public void onBackPressed() {
219 // send the back event to the top sub-state
220 GLRoot root = getGLRoot();
221 root.lockRenderThread();
222 try {
223 getStateManager().onBackPressed();
224 } finally {
225 root.unlockRenderThread();
226 }
227 }
228
229 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800230 public GalleryActionBar getGalleryActionBar() {
Ray Chen8cab3e82012-03-20 16:32:57 +0800231 if (mActionBar == null) {
232 mActionBar = new GalleryActionBar(this);
233 }
234 return mActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +0800235 }
Pin Ting4d50f732012-02-03 18:35:00 +0800236
Ray Chen655f63f2012-03-16 14:38:04 +0800237 @Override
238 public boolean onOptionsItemSelected(MenuItem item) {
239 GLRoot root = getGLRoot();
240 root.lockRenderThread();
241 try {
242 return getStateManager().itemSelected(item);
243 } finally {
244 root.unlockRenderThread();
245 }
246 }
247
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800248 protected void disableToggleStatusBar() {
249 mDisableToggleStatusBar = true;
250 }
251
Pin Ting4d50f732012-02-03 18:35:00 +0800252 // Shows status bar in portrait view, hide in landscape view
253 private void toggleStatusBarByOrientation() {
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800254 if (mDisableToggleStatusBar) return;
255
Ray Chenfa011a22012-02-07 20:23:44 +0800256 Window win = getWindow();
257 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
258 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
259 } else {
260 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
261 }
Pin Ting4d50f732012-02-03 18:35:00 +0800262 }
Owen Linf9a0a432011-08-17 22:07:43 +0800263}