blob: d25f60ebe718c4655e945ec27abef9e33baba53d [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
19import com.android.gallery3d.R;
20import com.android.gallery3d.data.DataManager;
21import com.android.gallery3d.data.ImageCacheService;
22import com.android.gallery3d.ui.GLRoot;
23import com.android.gallery3d.ui.GLRootView;
24import com.android.gallery3d.ui.PositionRepository;
25import com.android.gallery3d.util.ThreadPool;
26
27import android.app.ActionBar;
28import android.app.Activity;
29import android.app.AlertDialog;
30import android.content.BroadcastReceiver;
31import android.content.Context;
32import android.content.DialogInterface;
33import android.content.DialogInterface.OnCancelListener;
34import android.content.DialogInterface.OnClickListener;
35import android.content.Intent;
36import android.content.IntentFilter;
Owen Lin8e565702011-09-09 13:33:44 +080037import android.content.res.Configuration;
Owen Linf9a0a432011-08-17 22:07:43 +080038import android.os.Bundle;
39
40public class AbstractGalleryActivity extends Activity implements GalleryActivity {
41 @SuppressWarnings("unused")
42 private static final String TAG = "AbstractGalleryActivity";
43 private GLRootView mGLRootView;
44 private StateManager mStateManager;
45 private PositionRepository mPositionRepository = new PositionRepository();
46
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
57 protected void onSaveInstanceState(Bundle outState) {
58 mGLRootView.lockRenderThread();
59 try {
60 super.onSaveInstanceState(outState);
61 getStateManager().saveState(outState);
62 } finally {
63 mGLRootView.unlockRenderThread();
64 }
65 }
66
Owen Lin8e565702011-09-09 13:33:44 +080067 @Override
68 public void onConfigurationChanged(Configuration config) {
69 super.onConfigurationChanged(config);
70 mStateManager.onConfigurationChange(config);
Ray Chen108667b2011-11-09 10:49:09 +080071 invalidateOptionsMenu();
Owen Lin8e565702011-09-09 13:33:44 +080072 }
73
Owen Linf9a0a432011-08-17 22:07:43 +080074 public Context getAndroidContext() {
75 return this;
76 }
77
78 public ImageCacheService getImageCacheService() {
79 return ((GalleryApp) getApplication()).getImageCacheService();
80 }
81
82 public DataManager getDataManager() {
83 return ((GalleryApp) getApplication()).getDataManager();
84 }
85
86 public ThreadPool getThreadPool() {
87 return ((GalleryApp) getApplication()).getThreadPool();
88 }
89
90 public GalleryApp getGalleryApplication() {
91 return (GalleryApp) getApplication();
92 }
93
94 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
105 public PositionRepository getPositionRepository() {
106 return mPositionRepository;
107 }
108
109 @Override
110 public void setContentView(int resId) {
111 super.setContentView(resId);
112 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
113 }
114
115 public int getActionBarHeight() {
116 ActionBar actionBar = getActionBar();
117 return actionBar != null ? actionBar.getHeight() : 0;
118 }
119
120 protected void onStorageReady() {
121 if (mAlertDialog != null) {
122 mAlertDialog.dismiss();
123 mAlertDialog = null;
124 unregisterReceiver(mMountReceiver);
125 }
126 }
127
128 @Override
129 protected void onStart() {
130 super.onStart();
131 if (getExternalCacheDir() == null) {
132 OnCancelListener onCancel = new OnCancelListener() {
133 @Override
134 public void onCancel(DialogInterface dialog) {
135 finish();
136 }
137 };
138 OnClickListener onClick = new OnClickListener() {
139 @Override
140 public void onClick(DialogInterface dialog, int which) {
141 dialog.cancel();
142 }
143 };
144 mAlertDialog = new AlertDialog.Builder(this)
145 .setIcon(android.R.drawable.ic_dialog_alert)
146 .setTitle("No Storage")
147 .setMessage("No external storage available.")
148 .setNegativeButton(android.R.string.cancel, onClick)
149 .setOnCancelListener(onCancel)
150 .show();
151 registerReceiver(mMountReceiver, mMountFilter);
152 }
153 }
154
155 @Override
156 protected void onStop() {
157 super.onStop();
158 if (mAlertDialog != null) {
159 unregisterReceiver(mMountReceiver);
160 mAlertDialog.dismiss();
161 mAlertDialog = null;
162 }
163 }
164
165 @Override
166 protected void onResume() {
167 super.onResume();
168 mGLRootView.lockRenderThread();
169 try {
170 getStateManager().resume();
171 getDataManager().resume();
172 } finally {
173 mGLRootView.unlockRenderThread();
174 }
175 mGLRootView.onResume();
176 }
177
178 @Override
179 protected void onPause() {
180 super.onPause();
181 mGLRootView.onPause();
182 mGLRootView.lockRenderThread();
183 try {
184 getStateManager().pause();
185 getDataManager().pause();
186 } finally {
187 mGLRootView.unlockRenderThread();
188 }
189 }
190
191 @Override
192 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
193 mGLRootView.lockRenderThread();
194 try {
195 getStateManager().notifyActivityResult(
196 requestCode, resultCode, data);
197 } finally {
198 mGLRootView.unlockRenderThread();
199 }
200 }
201
202 @Override
203 public GalleryActionBar getGalleryActionBar() {
204 return null;
205 }
206}