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