AbstractGalleryActivity.java revision 3957efd020f2e80d79cd3fed019f2883668a1c9e
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.ui.BitmapPool; 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 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 58 protected void onCreate(Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 toggleStatusBarByOrientation(); 61 } 62 63 @Override 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 74 @Override 75 public void onConfigurationChanged(Configuration config) { 76 super.onConfigurationChanged(config); 77 mStateManager.onConfigurationChange(config); 78 invalidateOptionsMenu(); 79 toggleStatusBarByOrientation(); 80 } 81 82 public Context getAndroidContext() { 83 return this; 84 } 85 86 public DataManager getDataManager() { 87 return ((GalleryApp) getApplication()).getDataManager(); 88 } 89 90 public ThreadPool getThreadPool() { 91 return ((GalleryApp) getApplication()).getThreadPool(); 92 } 93 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 105 @Override 106 public void setContentView(int resId) { 107 super.setContentView(resId); 108 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); 109 } 110 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 } 180 BitmapPool.clear(); 181 } 182 183 @Override 184 protected void onDestroy() { 185 super.onDestroy(); 186 mGLRootView.lockRenderThread(); 187 try { 188 getStateManager().destroy(); 189 } finally { 190 mGLRootView.unlockRenderThread(); 191 } 192 } 193 194 @Override 195 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 196 mGLRootView.lockRenderThread(); 197 try { 198 getStateManager().notifyActivityResult( 199 requestCode, resultCode, data); 200 } finally { 201 mGLRootView.unlockRenderThread(); 202 } 203 } 204 205 @Override 206 public GalleryActionBar getGalleryActionBar() { 207 if (mActionBar == null) { 208 mActionBar = new GalleryActionBar(this); 209 } 210 return mActionBar; 211 } 212 213 @Override 214 public boolean onOptionsItemSelected(MenuItem item) { 215 GLRoot root = getGLRoot(); 216 root.lockRenderThread(); 217 try { 218 return getStateManager().itemSelected(item); 219 } finally { 220 root.unlockRenderThread(); 221 } 222 } 223 224 // Shows status bar in portrait view, hide in landscape view 225 private void toggleStatusBarByOrientation() { 226 Window win = getWindow(); 227 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 228 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 229 } else { 230 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 231 } 232 } 233} 234