AbstractGalleryActivity.java revision 655f63f04638dfe31045eab1b6b4c39cc2db10db
1/*
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 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;
28import android.content.res.Configuration;
29import android.os.Bundle;
30import android.view.MenuItem;
31import android.view.Window;
32import android.view.WindowManager;
33
34import 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
41public class AbstractGalleryActivity extends Activity implements GalleryActivity {
42    @SuppressWarnings("unused")
43    private static final String TAG = "AbstractGalleryActivity";
44    private GLRootView mGLRootView;
45    private StateManager mStateManager;
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 onCreate(Bundle savedInstanceState) {
58        super.onCreate(savedInstanceState);
59        toggleStatusBarByOrientation();
60    }
61
62    @Override
63    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
73    @Override
74    public void onConfigurationChanged(Configuration config) {
75        super.onConfigurationChanged(config);
76        mStateManager.onConfigurationChange(config);
77        invalidateOptionsMenu();
78        toggleStatusBarByOrientation();
79    }
80
81    public Context getAndroidContext() {
82        return this;
83    }
84
85    public DataManager getDataManager() {
86        return ((GalleryApp) getApplication()).getDataManager();
87    }
88
89    public ThreadPool getThreadPool() {
90        return ((GalleryApp) getApplication()).getThreadPool();
91    }
92
93    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
104    @Override
105    public void setContentView(int resId) {
106        super.setContentView(resId);
107        mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
108    }
109
110    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        }
179        BitmapPool.clear();
180    }
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    }
197
198    @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
209    // Shows status bar in portrait view, hide in landscape view
210    private void toggleStatusBarByOrientation() {
211        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        }
217    }
218}
219