Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
| 17 | package com.android.gallery3d.app; |
| 18 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.DialogInterface.OnCancelListener; |
| 25 | import android.content.DialogInterface.OnClickListener; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 28 | import android.content.res.Configuration; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 29 | import android.os.Bundle; |
Ray Chen | 655f63f | 2012-03-16 14:38:04 +0800 | [diff] [blame] | 30 | import android.view.MenuItem; |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 31 | import android.view.Window; |
| 32 | import android.view.WindowManager; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 33 | |
Owen Lin | 4bb5912 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 34 | import com.android.gallery3d.R; |
| 35 | import com.android.gallery3d.data.DataManager; |
Owen Lin | d8fb81f | 2012-03-29 14:27:58 +0800 | [diff] [blame] | 36 | import com.android.gallery3d.data.MediaItem; |
Owen Lin | 4bb5912 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 37 | import com.android.gallery3d.ui.GLRoot; |
| 38 | import com.android.gallery3d.ui.GLRootView; |
| 39 | import com.android.gallery3d.util.ThreadPool; |
| 40 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 41 | public class AbstractGalleryActivity extends Activity implements GalleryActivity { |
| 42 | @SuppressWarnings("unused") |
| 43 | private static final String TAG = "AbstractGalleryActivity"; |
| 44 | private GLRootView mGLRootView; |
| 45 | private StateManager mStateManager; |
Ray Chen | 8cab3e8 | 2012-03-20 16:32:57 +0800 | [diff] [blame] | 46 | private GalleryActionBar mActionBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 58 | protected void onCreate(Bundle savedInstanceState) { |
| 59 | super.onCreate(savedInstanceState); |
| 60 | toggleStatusBarByOrientation(); |
| 61 | } |
| 62 | |
| 63 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 74 | @Override |
| 75 | public void onConfigurationChanged(Configuration config) { |
| 76 | super.onConfigurationChanged(config); |
| 77 | mStateManager.onConfigurationChange(config); |
Ray Chen | 108667b | 2011-11-09 10:49:09 +0800 | [diff] [blame] | 78 | invalidateOptionsMenu(); |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 79 | toggleStatusBarByOrientation(); |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 80 | } |
| 81 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 82 | public Context getAndroidContext() { |
| 83 | return this; |
| 84 | } |
| 85 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 86 | public DataManager getDataManager() { |
| 87 | return ((GalleryApp) getApplication()).getDataManager(); |
| 88 | } |
| 89 | |
| 90 | public ThreadPool getThreadPool() { |
| 91 | return ((GalleryApp) getApplication()).getThreadPool(); |
| 92 | } |
| 93 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 105 | @Override |
| 106 | public void setContentView(int resId) { |
| 107 | super.setContentView(resId); |
| 108 | mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); |
| 109 | } |
| 110 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | } |
Owen Lin | d8fb81f | 2012-03-29 14:27:58 +0800 | [diff] [blame] | 180 | MediaItem.getMicroThumbPool().clear(); |
Chih-Chung Chang | b8be1e0 | 2012-04-17 20:35:14 +0800 | [diff] [blame^] | 181 | MediaItem.getThumbPool().clear(); |
Owen Lin | cafd30f | 2012-04-09 10:15:35 +0800 | [diff] [blame] | 182 | MediaItem.getBytesBufferPool().clear(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
Wu-cheng Li | 3957efd | 2012-04-03 19:08:35 +0800 | [diff] [blame] | 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 |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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() { |
Ray Chen | 8cab3e8 | 2012-03-20 16:32:57 +0800 | [diff] [blame] | 209 | if (mActionBar == null) { |
| 210 | mActionBar = new GalleryActionBar(this); |
| 211 | } |
| 212 | return mActionBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 213 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 214 | |
Ray Chen | 655f63f | 2012-03-16 14:38:04 +0800 | [diff] [blame] | 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 | |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 226 | // Shows status bar in portrait view, hide in landscape view |
| 227 | private void toggleStatusBarByOrientation() { |
Ray Chen | fa011a2 | 2012-02-07 20:23:44 +0800 | [diff] [blame] | 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 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 234 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 235 | } |