AbstractGalleryActivity.java revision 5d4b8fd9fd2e601b780c027ef912c8fabfe3ee1e
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 getGalleryActionBar().onConfigurationChanged(); 88 invalidateOptionsMenu(); 89 toggleStatusBarByOrientation(); 90 } 91 92 @Override 93 public boolean onCreateOptionsMenu(Menu menu) { 94 super.onCreateOptionsMenu(menu); 95 return getStateManager().createOptionsMenu(menu); 96 } 97 98 @Override 99 public Context getAndroidContext() { 100 return this; 101 } 102 103 @Override 104 public DataManager getDataManager() { 105 return ((GalleryApp) getApplication()).getDataManager(); 106 } 107 108 @Override 109 public ThreadPool getThreadPool() { 110 return ((GalleryApp) getApplication()).getThreadPool(); 111 } 112 113 public synchronized StateManager getStateManager() { 114 if (mStateManager == null) { 115 mStateManager = new StateManager(this); 116 } 117 return mStateManager; 118 } 119 120 public GLRoot getGLRoot() { 121 return mGLRootView; 122 } 123 124 public OrientationManager getOrientationManager() { 125 return mOrientationManager; 126 } 127 128 @Override 129 public void setContentView(int resId) { 130 super.setContentView(resId); 131 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); 132 } 133 134 protected void onStorageReady() { 135 if (mAlertDialog != null) { 136 mAlertDialog.dismiss(); 137 mAlertDialog = null; 138 unregisterReceiver(mMountReceiver); 139 } 140 } 141 142 @Override 143 protected void onStart() { 144 super.onStart(); 145 if (getExternalCacheDir() == null) { 146 OnCancelListener onCancel = new OnCancelListener() { 147 @Override 148 public void onCancel(DialogInterface dialog) { 149 finish(); 150 } 151 }; 152 OnClickListener onClick = new OnClickListener() { 153 @Override 154 public void onClick(DialogInterface dialog, int which) { 155 dialog.cancel(); 156 } 157 }; 158 AlertDialog.Builder builder = new AlertDialog.Builder(this) 159 .setTitle(R.string.no_external_storage_title) 160 .setMessage(R.string.no_external_storage) 161 .setNegativeButton(android.R.string.cancel, onClick) 162 .setOnCancelListener(onCancel); 163 if (ApiHelper.HAS_SET_ICON_ATTRIBUTE) { 164 setAlertDialogIconAttribute(builder); 165 } else { 166 builder.setIcon(android.R.drawable.ic_dialog_alert); 167 } 168 mAlertDialog = builder.show(); 169 registerReceiver(mMountReceiver, mMountFilter); 170 } 171 } 172 173 @TargetApi(ApiHelper.VERSION_CODES.HONEYCOMB) 174 private static void setAlertDialogIconAttribute( 175 AlertDialog.Builder builder) { 176 builder.setIconAttribute(android.R.attr.alertDialogIcon); 177 } 178 179 @Override 180 protected void onStop() { 181 super.onStop(); 182 if (mAlertDialog != null) { 183 unregisterReceiver(mMountReceiver); 184 mAlertDialog.dismiss(); 185 mAlertDialog = null; 186 } 187 } 188 189 @Override 190 protected void onResume() { 191 super.onResume(); 192 mGLRootView.lockRenderThread(); 193 try { 194 getStateManager().resume(); 195 getDataManager().resume(); 196 } finally { 197 mGLRootView.unlockRenderThread(); 198 } 199 mGLRootView.onResume(); 200 mOrientationManager.resume(); 201 } 202 203 @Override 204 protected void onPause() { 205 super.onPause(); 206 mOrientationManager.pause(); 207 mGLRootView.onPause(); 208 mGLRootView.lockRenderThread(); 209 try { 210 getStateManager().pause(); 211 getDataManager().pause(); 212 } finally { 213 mGLRootView.unlockRenderThread(); 214 } 215 clearBitmapPool(MediaItem.getMicroThumbPool()); 216 clearBitmapPool(MediaItem.getThumbPool()); 217 218 MediaItem.getBytesBufferPool().clear(); 219 } 220 221 private static void clearBitmapPool(BitmapPool pool) { 222 if (pool != null) pool.clear(); 223 } 224 225 @Override 226 protected void onDestroy() { 227 super.onDestroy(); 228 mGLRootView.lockRenderThread(); 229 try { 230 getStateManager().destroy(); 231 } finally { 232 mGLRootView.unlockRenderThread(); 233 } 234 } 235 236 @Override 237 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 238 mGLRootView.lockRenderThread(); 239 try { 240 getStateManager().notifyActivityResult( 241 requestCode, resultCode, data); 242 } finally { 243 mGLRootView.unlockRenderThread(); 244 } 245 } 246 247 @Override 248 public void onBackPressed() { 249 // send the back event to the top sub-state 250 GLRoot root = getGLRoot(); 251 root.lockRenderThread(); 252 try { 253 getStateManager().onBackPressed(); 254 } finally { 255 root.unlockRenderThread(); 256 } 257 } 258 259 public GalleryActionBar getGalleryActionBar() { 260 if (mActionBar == null) { 261 mActionBar = new GalleryActionBar(this); 262 } 263 return mActionBar; 264 } 265 266 @Override 267 public boolean onOptionsItemSelected(MenuItem item) { 268 GLRoot root = getGLRoot(); 269 root.lockRenderThread(); 270 try { 271 return getStateManager().itemSelected(item); 272 } finally { 273 root.unlockRenderThread(); 274 } 275 } 276 277 protected void disableToggleStatusBar() { 278 mDisableToggleStatusBar = true; 279 } 280 281 // Shows status bar in portrait view, hide in landscape view 282 private void toggleStatusBarByOrientation() { 283 if (mDisableToggleStatusBar) return; 284 285 Window win = getWindow(); 286 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 287 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 288 } else { 289 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 290 } 291 } 292 293 public TransitionStore getTransitionStore() { 294 return mTransitionStore; 295 } 296} 297