AbstractGalleryActivity.java revision b8be1e0ad76b6abc0da7ead39f7a9811195d001e
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.data.MediaItem;
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    private GalleryActionBar mActionBar;
47
48    private AlertDialog mAlertDialog = null;
49    private BroadcastReceiver mMountReceiver = new BroadcastReceiver() {
50        @Override
51        public void onReceive(Context context, Intent intent) {
52            if (getExternalCacheDir() != null) onStorageReady();
53        }
54    };
55    private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
56
57    @Override
58    protected void onCreate(Bundle savedInstanceState) {
59        super.onCreate(savedInstanceState);
60        toggleStatusBarByOrientation();
61    }
62
63    @Override
64    protected void onSaveInstanceState(Bundle outState) {
65        mGLRootView.lockRenderThread();
66        try {
67            super.onSaveInstanceState(outState);
68            getStateManager().saveState(outState);
69        } finally {
70            mGLRootView.unlockRenderThread();
71        }
72    }
73
74    @Override
75    public void onConfigurationChanged(Configuration config) {
76        super.onConfigurationChanged(config);
77        mStateManager.onConfigurationChange(config);
78        invalidateOptionsMenu();
79        toggleStatusBarByOrientation();
80    }
81
82    public Context getAndroidContext() {
83        return this;
84    }
85
86    public DataManager getDataManager() {
87        return ((GalleryApp) getApplication()).getDataManager();
88    }
89
90    public ThreadPool getThreadPool() {
91        return ((GalleryApp) getApplication()).getThreadPool();
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    @Override
106    public void setContentView(int resId) {
107        super.setContentView(resId);
108        mGLRootView = (GLRootView) findViewById(R.id.gl_root_view);
109    }
110
111    protected void onStorageReady() {
112        if (mAlertDialog != null) {
113            mAlertDialog.dismiss();
114            mAlertDialog = null;
115            unregisterReceiver(mMountReceiver);
116        }
117    }
118
119    @Override
120    protected void onStart() {
121        super.onStart();
122        if (getExternalCacheDir() == null) {
123            OnCancelListener onCancel = new OnCancelListener() {
124                @Override
125                public void onCancel(DialogInterface dialog) {
126                    finish();
127                }
128            };
129            OnClickListener onClick = new OnClickListener() {
130                @Override
131                public void onClick(DialogInterface dialog, int which) {
132                    dialog.cancel();
133                }
134            };
135            mAlertDialog = new AlertDialog.Builder(this)
136                    .setIcon(android.R.drawable.ic_dialog_alert)
137                    .setTitle("No Storage")
138                    .setMessage("No external storage available.")
139                    .setNegativeButton(android.R.string.cancel, onClick)
140                    .setOnCancelListener(onCancel)
141                    .show();
142            registerReceiver(mMountReceiver, mMountFilter);
143        }
144    }
145
146    @Override
147    protected void onStop() {
148        super.onStop();
149        if (mAlertDialog != null) {
150            unregisterReceiver(mMountReceiver);
151            mAlertDialog.dismiss();
152            mAlertDialog = null;
153        }
154    }
155
156    @Override
157    protected void onResume() {
158        super.onResume();
159        mGLRootView.lockRenderThread();
160        try {
161            getStateManager().resume();
162            getDataManager().resume();
163        } finally {
164            mGLRootView.unlockRenderThread();
165        }
166        mGLRootView.onResume();
167    }
168
169    @Override
170    protected void onPause() {
171        super.onPause();
172        mGLRootView.onPause();
173        mGLRootView.lockRenderThread();
174        try {
175            getStateManager().pause();
176            getDataManager().pause();
177        } finally {
178            mGLRootView.unlockRenderThread();
179        }
180        MediaItem.getMicroThumbPool().clear();
181        MediaItem.getThumbPool().clear();
182        MediaItem.getBytesBufferPool().clear();
183    }
184
185    @Override
186    protected void onDestroy() {
187        super.onDestroy();
188        mGLRootView.lockRenderThread();
189        try {
190            getStateManager().destroy();
191        } finally {
192            mGLRootView.unlockRenderThread();
193        }
194    }
195
196    @Override
197    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
198        mGLRootView.lockRenderThread();
199        try {
200            getStateManager().notifyActivityResult(
201                    requestCode, resultCode, data);
202        } finally {
203            mGLRootView.unlockRenderThread();
204        }
205    }
206
207    @Override
208    public GalleryActionBar getGalleryActionBar() {
209        if (mActionBar == null) {
210            mActionBar = new GalleryActionBar(this);
211        }
212        return mActionBar;
213    }
214
215    @Override
216    public boolean onOptionsItemSelected(MenuItem item) {
217        GLRoot root = getGLRoot();
218        root.lockRenderThread();
219        try {
220            return getStateManager().itemSelected(item);
221        } finally {
222            root.unlockRenderThread();
223        }
224    }
225
226    // Shows status bar in portrait view, hide in landscape view
227    private void toggleStatusBarByOrientation() {
228        Window win = getWindow();
229        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
230            win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
231        } else {
232            win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
233        }
234    }
235}
236