blob: 0b9461d9a3a8ee2cac2245683f49f2884b29300c [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;
Wu-cheng Lib1db75c2012-04-24 18:33:10 +080047 private boolean mDisableToggleStatusBar;
Owen Linf9a0a432011-08-17 22:07:43 +080048
49 private AlertDialog mAlertDialog = null;
50 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
51 @Override
52 public void onReceive(Context context, Intent intent) {
53 if (getExternalCacheDir() != null) onStorageReady();
54 }
55 };
56 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
57
58 @Override
Pin Ting4d50f732012-02-03 18:35:00 +080059 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61 toggleStatusBarByOrientation();
62 }
63
64 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080065 protected void onSaveInstanceState(Bundle outState) {
66 mGLRootView.lockRenderThread();
67 try {
68 super.onSaveInstanceState(outState);
69 getStateManager().saveState(outState);
70 } finally {
71 mGLRootView.unlockRenderThread();
72 }
73 }
74
Owen Lin8e565702011-09-09 13:33:44 +080075 @Override
76 public void onConfigurationChanged(Configuration config) {
77 super.onConfigurationChanged(config);
78 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080079 invalidateOptionsMenu();
Pin Ting4d50f732012-02-03 18:35:00 +080080 toggleStatusBarByOrientation();
Owen Lin8e565702011-09-09 13:33:44 +080081 }
82
Owen Linf9a0a432011-08-17 22:07:43 +080083 public Context getAndroidContext() {
84 return this;
85 }
86
Owen Linf9a0a432011-08-17 22:07:43 +080087 public DataManager getDataManager() {
88 return ((GalleryApp) getApplication()).getDataManager();
89 }
90
91 public ThreadPool getThreadPool() {
92 return ((GalleryApp) getApplication()).getThreadPool();
93 }
94
Owen Linf9a0a432011-08-17 22:07:43 +080095 public synchronized StateManager getStateManager() {
96 if (mStateManager == null) {
97 mStateManager = new StateManager(this);
98 }
99 return mStateManager;
100 }
101
102 public GLRoot getGLRoot() {
103 return mGLRootView;
104 }
105
Owen Linf9a0a432011-08-17 22:07:43 +0800106 @Override
107 public void setContentView(int resId) {
108 super.setContentView(resId);
109 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
110 }
111
Owen Linf9a0a432011-08-17 22:07:43 +0800112 protected void onStorageReady() {
113 if (mAlertDialog != null) {
114 mAlertDialog.dismiss();
115 mAlertDialog = null;
116 unregisterReceiver(mMountReceiver);
117 }
118 }
119
120 @Override
121 protected void onStart() {
122 super.onStart();
123 if (getExternalCacheDir() == null) {
124 OnCancelListener onCancel = new OnCancelListener() {
125 @Override
126 public void onCancel(DialogInterface dialog) {
127 finish();
128 }
129 };
130 OnClickListener onClick = new OnClickListener() {
131 @Override
132 public void onClick(DialogInterface dialog, int which) {
133 dialog.cancel();
134 }
135 };
136 mAlertDialog = new AlertDialog.Builder(this)
137 .setIcon(android.R.drawable.ic_dialog_alert)
138 .setTitle("No Storage")
139 .setMessage("No external storage available.")
140 .setNegativeButton(android.R.string.cancel, onClick)
141 .setOnCancelListener(onCancel)
142 .show();
143 registerReceiver(mMountReceiver, mMountFilter);
144 }
145 }
146
147 @Override
148 protected void onStop() {
149 super.onStop();
150 if (mAlertDialog != null) {
151 unregisterReceiver(mMountReceiver);
152 mAlertDialog.dismiss();
153 mAlertDialog = null;
154 }
155 }
156
157 @Override
158 protected void onResume() {
159 super.onResume();
160 mGLRootView.lockRenderThread();
161 try {
162 getStateManager().resume();
163 getDataManager().resume();
164 } finally {
165 mGLRootView.unlockRenderThread();
166 }
167 mGLRootView.onResume();
168 }
169
170 @Override
171 protected void onPause() {
172 super.onPause();
173 mGLRootView.onPause();
174 mGLRootView.lockRenderThread();
175 try {
176 getStateManager().pause();
177 getDataManager().pause();
178 } finally {
179 mGLRootView.unlockRenderThread();
180 }
Owen Lind8fb81f2012-03-29 14:27:58 +0800181 MediaItem.getMicroThumbPool().clear();
Chih-Chung Changb8be1e02012-04-17 20:35:14 +0800182 MediaItem.getThumbPool().clear();
Owen Lincafd30f2012-04-09 10:15:35 +0800183 MediaItem.getBytesBufferPool().clear();
Owen Linf9a0a432011-08-17 22:07:43 +0800184 }
185
186 @Override
Wu-cheng Li3957efd2012-04-03 19:08:35 +0800187 protected void onDestroy() {
188 super.onDestroy();
189 mGLRootView.lockRenderThread();
190 try {
191 getStateManager().destroy();
192 } finally {
193 mGLRootView.unlockRenderThread();
194 }
195 }
196
197 @Override
Owen Linf9a0a432011-08-17 22:07:43 +0800198 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
199 mGLRootView.lockRenderThread();
200 try {
201 getStateManager().notifyActivityResult(
202 requestCode, resultCode, data);
203 } finally {
204 mGLRootView.unlockRenderThread();
205 }
206 }
207
208 @Override
209 public GalleryActionBar getGalleryActionBar() {
Ray Chen8cab3e82012-03-20 16:32:57 +0800210 if (mActionBar == null) {
211 mActionBar = new GalleryActionBar(this);
212 }
213 return mActionBar;
Owen Linf9a0a432011-08-17 22:07:43 +0800214 }
Pin Ting4d50f732012-02-03 18:35:00 +0800215
Ray Chen655f63f2012-03-16 14:38:04 +0800216 @Override
217 public boolean onOptionsItemSelected(MenuItem item) {
218 GLRoot root = getGLRoot();
219 root.lockRenderThread();
220 try {
221 return getStateManager().itemSelected(item);
222 } finally {
223 root.unlockRenderThread();
224 }
225 }
226
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800227 protected void disableToggleStatusBar() {
228 mDisableToggleStatusBar = true;
229 }
230
Pin Ting4d50f732012-02-03 18:35:00 +0800231 // Shows status bar in portrait view, hide in landscape view
232 private void toggleStatusBarByOrientation() {
Wu-cheng Lib1db75c2012-04-24 18:33:10 +0800233 if (mDisableToggleStatusBar) return;
234
Ray Chenfa011a22012-02-07 20:23:44 +0800235 Window win = getWindow();
236 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
237 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
238 } else {
239 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
240 }
Pin Ting4d50f732012-02-03 18:35:00 +0800241 }
Owen Linf9a0a432011-08-17 22:07:43 +0800242}