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