AbstractGalleryActivity.java revision 1336062985c9824d7fd796b1cebd5eaa8e4163ca
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.annotation.TargetApi; 20import android.app.Activity; 21import android.app.AlertDialog; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.DialogInterface; 25import android.content.DialogInterface.OnCancelListener; 26import android.content.DialogInterface.OnClickListener; 27import android.content.Intent; 28import android.content.IntentFilter; 29import android.content.res.Configuration; 30import android.os.Bundle; 31import android.view.Menu; 32import android.view.MenuItem; 33import android.view.Window; 34import android.view.WindowManager; 35 36import com.android.gallery3d.R; 37import com.android.gallery3d.common.ApiHelper; 38import com.android.gallery3d.data.BitmapPool; 39import com.android.gallery3d.data.DataManager; 40import com.android.gallery3d.data.MediaItem; 41import com.android.gallery3d.ui.GLRoot; 42import com.android.gallery3d.ui.GLRootView; 43import com.android.gallery3d.util.ThreadPool; 44 45public class AbstractGalleryActivity extends Activity implements GalleryContext { 46 @SuppressWarnings("unused") 47 private static final String TAG = "AbstractGalleryActivity"; 48 private GLRootView mGLRootView; 49 private StateManager mStateManager; 50 private GalleryActionBar mActionBar; 51 private OrientationManager mOrientationManager; 52 private TransitionStore mTransitionStore = new TransitionStore(); 53 private boolean mDisableToggleStatusBar; 54 55 private AlertDialog mAlertDialog = null; 56 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() { 57 @Override 58 public void onReceive(Context context, Intent intent) { 59 if (getExternalCacheDir() != null) onStorageReady(); 60 } 61 }; 62 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); 63 64 @Override 65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 mOrientationManager = new OrientationManager(this); 68 toggleStatusBarByOrientation(); 69 getWindow().setBackgroundDrawable(null); 70 } 71 72 @Override 73 protected void onSaveInstanceState(Bundle outState) { 74 mGLRootView.lockRenderThread(); 75 try { 76 super.onSaveInstanceState(outState); 77 getStateManager().saveState(outState); 78 } finally { 79 mGLRootView.unlockRenderThread(); 80 } 81 } 82 83 @Override 84 public void onConfigurationChanged(Configuration config) { 85 super.onConfigurationChanged(config); 86 mStateManager.onConfigurationChange(config); 87 invalidateOptionsMenu(); 88 toggleStatusBarByOrientation(); 89 } 90 91 @Override 92 public boolean onCreateOptionsMenu(Menu menu) { 93 super.onCreateOptionsMenu(menu); 94 return getStateManager().createOptionsMenu(menu); 95 } 96 97 @Override 98 public Context getAndroidContext() { 99 return this; 100 } 101 102 @Override 103 public DataManager getDataManager() { 104 return ((GalleryApp) getApplication()).getDataManager(); 105 } 106 107 @Override 108 public ThreadPool getThreadPool() { 109 return ((GalleryApp) getApplication()).getThreadPool(); 110 } 111 112 public synchronized StateManager getStateManager() { 113 if (mStateManager == null) { 114 mStateManager = new StateManager(this); 115 } 116 return mStateManager; 117 } 118 119 public GLRoot getGLRoot() { 120 return mGLRootView; 121 } 122 123 public OrientationManager getOrientationManager() { 124 return mOrientationManager; 125 } 126 127 @Override 128 public void setContentView(int resId) { 129 super.setContentView(resId); 130 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); 131 } 132 133 protected void onStorageReady() { 134 if (mAlertDialog != null) { 135 mAlertDialog.dismiss(); 136 mAlertDialog = null; 137 unregisterReceiver(mMountReceiver); 138 } 139 } 140 141 @Override 142 protected void onStart() { 143 super.onStart(); 144 if (getExternalCacheDir() == null) { 145 OnCancelListener onCancel = new OnCancelListener() { 146 @Override 147 public void onCancel(DialogInterface dialog) { 148 finish(); 149 } 150 }; 151 OnClickListener onClick = new OnClickListener() { 152 @Override 153 public void onClick(DialogInterface dialog, int which) { 154 dialog.cancel(); 155 } 156 }; 157 AlertDialog.Builder builder = new AlertDialog.Builder(this) 158 .setTitle(R.string.no_external_storage_title) 159 .setMessage(R.string.no_external_storage) 160 .setNegativeButton(android.R.string.cancel, onClick) 161 .setOnCancelListener(onCancel); 162 if (ApiHelper.HAS_SET_ICON_ATTRIBUTE) { 163 setAlertDialogIconAttribute(builder); 164 } else { 165 builder.setIcon(android.R.drawable.ic_dialog_alert); 166 } 167 mAlertDialog = builder.show(); 168 registerReceiver(mMountReceiver, mMountFilter); 169 } 170 } 171 172 @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB) 173 private static void setAlertDialogIconAttribute( 174 AlertDialog.Builder builder) { 175 builder.setIconAttribute(android.R.attr.alertDialogIcon); 176 } 177 178 @Override 179 protected void onStop() { 180 super.onStop(); 181 if (mAlertDialog != null) { 182 unregisterReceiver(mMountReceiver); 183 mAlertDialog.dismiss(); 184 mAlertDialog = null; 185 } 186 } 187 188 @Override 189 protected void onResume() { 190 super.onResume(); 191 mGLRootView.lockRenderThread(); 192 try { 193 getStateManager().resume(); 194 getDataManager().resume(); 195 } finally { 196 mGLRootView.unlockRenderThread(); 197 } 198 mGLRootView.onResume(); 199 mOrientationManager.resume(); 200 } 201 202 @Override 203 protected void onPause() { 204 super.onPause(); 205 mOrientationManager.pause(); 206 mGLRootView.onPause(); 207 mGLRootView.lockRenderThread(); 208 try { 209 getStateManager().pause(); 210 getDataManager().pause(); 211 } finally { 212 mGLRootView.unlockRenderThread(); 213 } 214 clearBitmapPool(MediaItem.getMicroThumbPool()); 215 clearBitmapPool(MediaItem.getThumbPool()); 216 217 MediaItem.getBytesBufferPool().clear(); 218 } 219 220 private static void clearBitmapPool(BitmapPool pool) { 221 if (pool != null) pool.clear(); 222 } 223 224 @Override 225 protected void onDestroy() { 226 super.onDestroy(); 227 mGLRootView.lockRenderThread(); 228 try { 229 getStateManager().destroy(); 230 } finally { 231 mGLRootView.unlockRenderThread(); 232 } 233 } 234 235 @Override 236 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 237 mGLRootView.lockRenderThread(); 238 try { 239 getStateManager().notifyActivityResult( 240 requestCode, resultCode, data); 241 } finally { 242 mGLRootView.unlockRenderThread(); 243 } 244 } 245 246 @Override 247 public void onBackPressed() { 248 // send the back event to the top sub-state 249 GLRoot root = getGLRoot(); 250 root.lockRenderThread(); 251 try { 252 getStateManager().onBackPressed(); 253 } finally { 254 root.unlockRenderThread(); 255 } 256 } 257 258 public GalleryActionBar getGalleryActionBar() { 259 if (mActionBar == null) { 260 mActionBar = new GalleryActionBar(this); 261 } 262 return mActionBar; 263 } 264 265 @Override 266 public boolean onOptionsItemSelected(MenuItem item) { 267 GLRoot root = getGLRoot(); 268 root.lockRenderThread(); 269 try { 270 return getStateManager().itemSelected(item); 271 } finally { 272 root.unlockRenderThread(); 273 } 274 } 275 276 protected void disableToggleStatusBar() { 277 mDisableToggleStatusBar = true; 278 } 279 280 // Shows status bar in portrait view, hide in landscape view 281 private void toggleStatusBarByOrientation() { 282 if (mDisableToggleStatusBar) return; 283 284 Window win = getWindow(); 285 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 286 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 287 } else { 288 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 289 } 290 } 291 292 public TransitionStore getTransitionStore() { 293 return mTransitionStore; 294 } 295} 296