blob: 7d9d72b13a7f857918043732578d81730f50acbf [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;
Pin Ting4d50f732012-02-03 18:35:00 +080030import android.view.Window;
31import android.view.WindowManager;
Owen Linf9a0a432011-08-17 22:07:43 +080032
Owen Lin4bb59122012-03-07 17:39:56 +080033import com.android.gallery3d.R;
34import com.android.gallery3d.data.DataManager;
35import com.android.gallery3d.ui.BitmapPool;
36import com.android.gallery3d.ui.GLRoot;
37import com.android.gallery3d.ui.GLRootView;
38import com.android.gallery3d.util.ThreadPool;
39
Owen Linf9a0a432011-08-17 22:07:43 +080040public class AbstractGalleryActivity extends Activity implements GalleryActivity {
41 @SuppressWarnings("unused")
42 private static final String TAG = "AbstractGalleryActivity";
43 private GLRootView mGLRootView;
44 private StateManager mStateManager;
Owen Linf9a0a432011-08-17 22:07:43 +080045
46 private AlertDialog mAlertDialog = null;
47 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
48 @Override
49 public void onReceive(Context context, Intent intent) {
50 if (getExternalCacheDir() != null) onStorageReady();
51 }
52 };
53 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
54
55 @Override
Pin Ting4d50f732012-02-03 18:35:00 +080056 protected void onCreate(Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
58 toggleStatusBarByOrientation();
59 }
60
61 @Override
Owen Linf9a0a432011-08-17 22:07:43 +080062 protected void onSaveInstanceState(Bundle outState) {
63 mGLRootView.lockRenderThread();
64 try {
65 super.onSaveInstanceState(outState);
66 getStateManager().saveState(outState);
67 } finally {
68 mGLRootView.unlockRenderThread();
69 }
70 }
71
Owen Lin8e565702011-09-09 13:33:44 +080072 @Override
73 public void onConfigurationChanged(Configuration config) {
74 super.onConfigurationChanged(config);
75 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080076 invalidateOptionsMenu();
Pin Ting4d50f732012-02-03 18:35:00 +080077 toggleStatusBarByOrientation();
Owen Lin8e565702011-09-09 13:33:44 +080078 }
79
Owen Linf9a0a432011-08-17 22:07:43 +080080 public Context getAndroidContext() {
81 return this;
82 }
83
Owen Linf9a0a432011-08-17 22:07:43 +080084 public DataManager getDataManager() {
85 return ((GalleryApp) getApplication()).getDataManager();
86 }
87
88 public ThreadPool getThreadPool() {
89 return ((GalleryApp) getApplication()).getThreadPool();
90 }
91
Owen Linf9a0a432011-08-17 22:07:43 +080092 public synchronized StateManager getStateManager() {
93 if (mStateManager == null) {
94 mStateManager = new StateManager(this);
95 }
96 return mStateManager;
97 }
98
99 public GLRoot getGLRoot() {
100 return mGLRootView;
101 }
102
Owen Linf9a0a432011-08-17 22:07:43 +0800103 @Override
104 public void setContentView(int resId) {
105 super.setContentView(resId);
106 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
107 }
108
Owen Linf9a0a432011-08-17 22:07:43 +0800109 protected void onStorageReady() {
110 if (mAlertDialog != null) {
111 mAlertDialog.dismiss();
112 mAlertDialog = null;
113 unregisterReceiver(mMountReceiver);
114 }
115 }
116
117 @Override
118 protected void onStart() {
119 super.onStart();
120 if (getExternalCacheDir() == null) {
121 OnCancelListener onCancel = new OnCancelListener() {
122 @Override
123 public void onCancel(DialogInterface dialog) {
124 finish();
125 }
126 };
127 OnClickListener onClick = new OnClickListener() {
128 @Override
129 public void onClick(DialogInterface dialog, int which) {
130 dialog.cancel();
131 }
132 };
133 mAlertDialog = new AlertDialog.Builder(this)
134 .setIcon(android.R.drawable.ic_dialog_alert)
135 .setTitle("No Storage")
136 .setMessage("No external storage available.")
137 .setNegativeButton(android.R.string.cancel, onClick)
138 .setOnCancelListener(onCancel)
139 .show();
140 registerReceiver(mMountReceiver, mMountFilter);
141 }
142 }
143
144 @Override
145 protected void onStop() {
146 super.onStop();
147 if (mAlertDialog != null) {
148 unregisterReceiver(mMountReceiver);
149 mAlertDialog.dismiss();
150 mAlertDialog = null;
151 }
152 }
153
154 @Override
155 protected void onResume() {
156 super.onResume();
157 mGLRootView.lockRenderThread();
158 try {
159 getStateManager().resume();
160 getDataManager().resume();
161 } finally {
162 mGLRootView.unlockRenderThread();
163 }
164 mGLRootView.onResume();
165 }
166
167 @Override
168 protected void onPause() {
169 super.onPause();
170 mGLRootView.onPause();
171 mGLRootView.lockRenderThread();
172 try {
173 getStateManager().pause();
174 getDataManager().pause();
175 } finally {
176 mGLRootView.unlockRenderThread();
177 }
Owen Lin4bb59122012-03-07 17:39:56 +0800178 BitmapPool.clear();
Owen Linf9a0a432011-08-17 22:07:43 +0800179 }
180
181 @Override
182 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
183 mGLRootView.lockRenderThread();
184 try {
185 getStateManager().notifyActivityResult(
186 requestCode, resultCode, data);
187 } finally {
188 mGLRootView.unlockRenderThread();
189 }
190 }
191
192 @Override
193 public GalleryActionBar getGalleryActionBar() {
194 return null;
195 }
Pin Ting4d50f732012-02-03 18:35:00 +0800196
197 // Shows status bar in portrait view, hide in landscape view
198 private void toggleStatusBarByOrientation() {
Ray Chenfa011a22012-02-07 20:23:44 +0800199 Window win = getWindow();
200 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
201 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
202 } else {
203 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
204 }
Pin Ting4d50f732012-02-03 18:35:00 +0800205 }
Owen Linf9a0a432011-08-17 22:07:43 +0800206}