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