blob: 03f522f48d6d9848ba914e511e45beea99443a5a [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;
36import com.android.gallery3d.ui.BitmapPool;
37import 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;
Owen Linf9a0a432011-08-17 22:07:43 +080046
47 private AlertDialog mAlertDialog = null;
48 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
49 @Override
50 public void onReceive(Context context, Intent intent) {
51 if (getExternalCacheDir() != null) onStorageReady();
52 }
53 };
54 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
55
56 @Override
Pin Ting4d50f732012-02-03 18:35:00 +080057 protected void onCreate(Bundle savedInstanceState) {
58 super.onCreate(savedInstanceState);
59 toggleStatusBarByOrientation();
60 }
61
62 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080063 protected void onSaveInstanceState(Bundle outState) {
64 mGLRootView.lockRenderThread();
65 try {
66 super.onSaveInstanceState(outState);
67 getStateManager().saveState(outState);
68 } finally {
69 mGLRootView.unlockRenderThread();
70 }
71 }
72
Owen Lin8e565702011-09-09 13:33:44 +080073 @Override
74 public void onConfigurationChanged(Configuration config) {
75 super.onConfigurationChanged(config);
76 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080077 invalidateOptionsMenu();
Pin Ting4d50f732012-02-03 18:35:00 +080078 toggleStatusBarByOrientation();
Owen Lin8e565702011-09-09 13:33:44 +080079 }
80
Owen Linf9a0a432011-08-17 22:07:43 +080081 public Context getAndroidContext() {
82 return this;
83 }
84
Owen Linf9a0a432011-08-17 22:07:43 +080085 public DataManager getDataManager() {
86 return ((GalleryApp) getApplication()).getDataManager();
87 }
88
89 public ThreadPool getThreadPool() {
90 return ((GalleryApp) getApplication()).getThreadPool();
91 }
92
Owen Linf9a0a432011-08-17 22:07:43 +080093 public synchronized StateManager getStateManager() {
94 if (mStateManager == null) {
95 mStateManager = new StateManager(this);
96 }
97 return mStateManager;
98 }
99
100 public GLRoot getGLRoot() {
101 return mGLRootView;
102 }
103
Owen Linf9a0a432011-08-17 22:07:43 +0800104 @Override
105 public void setContentView(int resId) {
106 super.setContentView(resId);
107 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
108 }
109
Owen Linf9a0a432011-08-17 22:07:43 +0800110 protected void onStorageReady() {
111 if (mAlertDialog != null) {
112 mAlertDialog.dismiss();
113 mAlertDialog = null;
114 unregisterReceiver(mMountReceiver);
115 }
116 }
117
118 @Override
119 protected void onStart() {
120 super.onStart();
121 if (getExternalCacheDir() == null) {
122 OnCancelListener onCancel = new OnCancelListener() {
123 @Override
124 public void onCancel(DialogInterface dialog) {
125 finish();
126 }
127 };
128 OnClickListener onClick = new OnClickListener() {
129 @Override
130 public void onClick(DialogInterface dialog, int which) {
131 dialog.cancel();
132 }
133 };
134 mAlertDialog = new AlertDialog.Builder(this)
135 .setIcon(android.R.drawable.ic_dialog_alert)
136 .setTitle("No Storage")
137 .setMessage("No external storage available.")
138 .setNegativeButton(android.R.string.cancel, onClick)
139 .setOnCancelListener(onCancel)
140 .show();
141 registerReceiver(mMountReceiver, mMountFilter);
142 }
143 }
144
145 @Override
146 protected void onStop() {
147 super.onStop();
148 if (mAlertDialog != null) {
149 unregisterReceiver(mMountReceiver);
150 mAlertDialog.dismiss();
151 mAlertDialog = null;
152 }
153 }
154
155 @Override
156 protected void onResume() {
157 super.onResume();
158 mGLRootView.lockRenderThread();
159 try {
160 getStateManager().resume();
161 getDataManager().resume();
162 } finally {
163 mGLRootView.unlockRenderThread();
164 }
165 mGLRootView.onResume();
166 }
167
168 @Override
169 protected void onPause() {
170 super.onPause();
171 mGLRootView.onPause();
172 mGLRootView.lockRenderThread();
173 try {
174 getStateManager().pause();
175 getDataManager().pause();
176 } finally {
177 mGLRootView.unlockRenderThread();
178 }
Owen Lin4bb59122012-03-07 17:39:56 +0800179 BitmapPool.clear();
Owen Linf9a0a432011-08-17 22:07:43 +0800180 }
181
182 @Override
183 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
184 mGLRootView.lockRenderThread();
185 try {
186 getStateManager().notifyActivityResult(
187 requestCode, resultCode, data);
188 } finally {
189 mGLRootView.unlockRenderThread();
190 }
191 }
192
193 @Override
194 public GalleryActionBar getGalleryActionBar() {
195 return null;
196 }
Pin Ting4d50f732012-02-03 18:35:00 +0800197
Ray Chen655f63f2012-03-16 14:38:04 +0800198 @Override
199 public boolean onOptionsItemSelected(MenuItem item) {
200 GLRoot root = getGLRoot();
201 root.lockRenderThread();
202 try {
203 return getStateManager().itemSelected(item);
204 } finally {
205 root.unlockRenderThread();
206 }
207 }
208
Pin Ting4d50f732012-02-03 18:35:00 +0800209 // Shows status bar in portrait view, hide in landscape view
210 private void toggleStatusBarByOrientation() {
Ray Chenfa011a22012-02-07 20:23:44 +0800211 Window win = getWindow();
212 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
213 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
214 } else {
215 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
216 }
Pin Ting4d50f732012-02-03 18:35:00 +0800217 }
Owen Linf9a0a432011-08-17 22:07:43 +0800218}