blob: 2c63e3409ab6043bb1c103a13f9e3b320a114cf1 [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();
64 }
65
66 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080067 protected void onSaveInstanceState(Bundle outState) {
68 mGLRootView.lockRenderThread();
69 try {
70 super.onSaveInstanceState(outState);
71 getStateManager().saveState(outState);
72 } finally {
73 mGLRootView.unlockRenderThread();
74 }
75 }
76
Owen Lin8e565702011-09-09 13:33:44 +080077 @Override
78 public void onConfigurationChanged(Configuration config) {
79 super.onConfigurationChanged(config);
80 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080081 invalidateOptionsMenu();
Pin Ting4d50f732012-02-03 18:35:00 +080082 toggleStatusBarByOrientation();
Owen Lin8e565702011-09-09 13:33:44 +080083 }
84
Owen Linf9a0a432011-08-17 22:07:43 +080085 public Context getAndroidContext() {
86 return this;
87 }
88
Owen Linf9a0a432011-08-17 22:07:43 +080089 public DataManager getDataManager() {
90 return ((GalleryApp) getApplication()).getDataManager();
91 }
92
93 public ThreadPool getThreadPool() {
94 return ((GalleryApp) getApplication()).getThreadPool();
95 }
96
Owen Linf9a0a432011-08-17 22:07:43 +080097 public synchronized StateManager getStateManager() {
98 if (mStateManager == null) {
99 mStateManager = new StateManager(this);
100 }
101 return mStateManager;
102 }
103
104 public GLRoot getGLRoot() {
105 return mGLRootView;
106 }
107
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800108 public OrientationManager getOrientationManager() {
109 return mOrientationManager;
110 }
111
Owen Linf9a0a432011-08-17 22:07:43 +0800112 @Override
113 public void setContentView(int resId) {
114 super.setContentView(resId);
115 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
116 }
117
Owen Linf9a0a432011-08-17 22:07:43 +0800118 protected void onStorageReady() {
119 if (mAlertDialog != null) {
120 mAlertDialog.dismiss();
121 mAlertDialog = null;
122 unregisterReceiver(mMountReceiver);
123 }
124 }
125
126 @Override
127 protected void onStart() {
128 super.onStart();
129 if (getExternalCacheDir() == null) {
130 OnCancelListener onCancel = new OnCancelListener() {
131 @Override
132 public void onCancel(DialogInterface dialog) {
133 finish();
134 }
135 };
136 OnClickListener onClick = new OnClickListener() {
137 @Override
138 public void onClick(DialogInterface dialog, int which) {
139 dialog.cancel();
140 }
141 };
142 mAlertDialog = new AlertDialog.Builder(this)
143 .setIcon(android.R.drawable.ic_dialog_alert)
144 .setTitle("No Storage")
145 .setMessage("No external storage available.")
146 .setNegativeButton(android.R.string.cancel, onClick)
147 .setOnCancelListener(onCancel)
148 .show();
149 registerReceiver(mMountReceiver, mMountFilter);
150 }
151 }
152
153 @Override
154 protected void onStop() {
155 super.onStop();
156 if (mAlertDialog != null) {
157 unregisterReceiver(mMountReceiver);
158 mAlertDialog.dismiss();
159 mAlertDialog = null;
160 }
161 }
162
163 @Override
164 protected void onResume() {
165 super.onResume();
166 mGLRootView.lockRenderThread();
167 try {
168 getStateManager().resume();
169 getDataManager().resume();
170 } finally {
171 mGLRootView.unlockRenderThread();
172 }
173 mGLRootView.onResume();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800174 mOrientationManager.resume();
Owen Linf9a0a432011-08-17 22:07:43 +0800175 }
176
177 @Override
178 protected void onPause() {
179 super.onPause();
Chih-Chung Changbd141b52012-04-26 10:10:49 +0800180 mOrientationManager.pause();
Owen Linf9a0a432011-08-17 22:07:43 +0800181 mGLRootView.onPause();
182 mGLRootView.lockRenderThread();
183 try {
184 getStateManager().pause();
185 getDataManager().pause();
186 } finally {
187 mGLRootView.unlockRenderThread();
188 }
Owen Lind8fb81f2012-03-29 14:27:58 +0800189 MediaItem.getMicroThumbPool().clear();
Chih-Chung Changb8be1e02012-04-17 20:35:14 +0800190 MediaItem.getThumbPool().clear();
Owen Lincafd30f2012-04-09 10:15:35 +0800191 MediaItem.getBytesBufferPool().clear();
Owen Linf9a0a432011-08-17 22:07:43 +0800192 }
193
194 @Override
Wu-cheng Li3957efd2012-04-03 19:08:35 +0800195 protected void onDestroy() {
196 super.onDestroy();
197 mGLRootView.lockRenderThread();
198 try {
199 getStateManager().destroy();
200 } finally {
201 mGLRootView.unlockRenderThread();
202 }
203 }
204
205 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800206 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
207 mGLRootView.lockRenderThread();
208 try {
209 getStateManager().notifyActivityResult(
210 requestCode, resultCode, data);
211 } finally {
212 mGLRootView.unlockRenderThread();
213 }
214 }
215
216 @Override
Chih-Chung Chang2c617382012-04-20 20:06:19 +0800217 public void onBackPressed() {
218 // send the back event to the top sub-state
219 GLRoot root = getGLRoot();
220 root.lockRenderThread();
221 try {
222 getStateManager().onBackPressed();
223 } finally {
224 root.unlockRenderThread();
225 }
226 }
227
228 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800229 public GalleryActionBar getGalleryActionBar() {
Ray Chen8cab3e82012-03-20 16:32:57 +0800230 if (mActionBar == null) {
231 mActionBar = new GalleryActionBar(this);
232 }
233 return mActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +0800234 }
Pin Ting4d50f732012-02-03 18:35:00 +0800235
Ray Chen655f63f2012-03-16 14:38:04 +0800236 @Override
237 public boolean onOptionsItemSelected(MenuItem item) {
238 GLRoot root = getGLRoot();
239 root.lockRenderThread();
240 try {
241 return getStateManager().itemSelected(item);
242 } finally {
243 root.unlockRenderThread();
244 }
245 }
246
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800247 protected void disableToggleStatusBar() {
248 mDisableToggleStatusBar = true;
249 }
250
Pin Ting4d50f732012-02-03 18:35:00 +0800251 // Shows status bar in portrait view, hide in landscape view
252 private void toggleStatusBarByOrientation() {
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800253 if (mDisableToggleStatusBar) return;
254
Ray Chenfa011a22012-02-07 20:23:44 +0800255 Window win = getWindow();
256 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
257 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
258 } else {
259 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
260 }
Pin Ting4d50f732012-02-03 18:35:00 +0800261 }
Owen Linf9a0a432011-08-17 22:07:43 +0800262}