blob: d0d7b0fad0b6707c8889ff1cb9196674f5c7f055 [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;
37import android.os.Bundle;
38
39public class AbstractGalleryActivity extends Activity implements GalleryActivity {
40 @SuppressWarnings("unused")
41 private static final String TAG = "AbstractGalleryActivity";
42 private GLRootView mGLRootView;
43 private StateManager mStateManager;
44 private PositionRepository mPositionRepository = new PositionRepository();
45
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
56 protected void onSaveInstanceState(Bundle outState) {
57 mGLRootView.lockRenderThread();
58 try {
59 super.onSaveInstanceState(outState);
60 getStateManager().saveState(outState);
61 } finally {
62 mGLRootView.unlockRenderThread();
63 }
64 }
65
66 public Context getAndroidContext() {
67 return this;
68 }
69
70 public ImageCacheService getImageCacheService() {
71 return ((GalleryApp) getApplication()).getImageCacheService();
72 }
73
74 public DataManager getDataManager() {
75 return ((GalleryApp) getApplication()).getDataManager();
76 }
77
78 public ThreadPool getThreadPool() {
79 return ((GalleryApp) getApplication()).getThreadPool();
80 }
81
82 public GalleryApp getGalleryApplication() {
83 return (GalleryApp) getApplication();
84 }
85
86 public synchronized StateManager getStateManager() {
87 if (mStateManager == null) {
88 mStateManager = new StateManager(this);
89 }
90 return mStateManager;
91 }
92
93 public GLRoot getGLRoot() {
94 return mGLRootView;
95 }
96
97 public PositionRepository getPositionRepository() {
98 return mPositionRepository;
99 }
100
101 @Override
102 public void setContentView(int resId) {
103 super.setContentView(resId);
104 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
105 }
106
107 public int getActionBarHeight() {
108 ActionBar actionBar = getActionBar();
109 return actionBar != null ? actionBar.getHeight() : 0;
110 }
111
112 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 }
181 }
182
183 @Override
184 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
185 mGLRootView.lockRenderThread();
186 try {
187 getStateManager().notifyActivityResult(
188 requestCode, resultCode, data);
189 } finally {
190 mGLRootView.unlockRenderThread();
191 }
192 }
193
194 @Override
195 public GalleryActionBar getGalleryActionBar() {
196 return null;
197 }
198}