AbstractGalleryActivity.java revision 4d50f7373a6205e197ffc1b73cdc5ffcbeb79fca
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 com.android.gallery3d.R; 20import com.android.gallery3d.data.DataManager; 21import com.android.gallery3d.data.ImageCacheService; 22import com.android.gallery3d.ui.GLRoot; 23import com.android.gallery3d.ui.GLRootView; 24import com.android.gallery3d.ui.PositionRepository; 25import com.android.gallery3d.util.ThreadPool; 26 27import android.app.ActionBar; 28import android.app.Activity; 29import android.app.AlertDialog; 30import android.content.BroadcastReceiver; 31import android.content.Context; 32import android.content.DialogInterface; 33import android.content.DialogInterface.OnCancelListener; 34import android.content.DialogInterface.OnClickListener; 35import android.content.Intent; 36import android.content.IntentFilter; 37import android.content.res.Configuration; 38import android.os.Bundle; 39import android.view.Window; 40import android.view.WindowManager; 41 42public class AbstractGalleryActivity extends Activity implements GalleryActivity { 43 @SuppressWarnings("unused") 44 private static final String TAG = "AbstractGalleryActivity"; 45 private GLRootView mGLRootView; 46 private StateManager mStateManager; 47 private PositionRepository mPositionRepository = new PositionRepository(); 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 ImageCacheService getImageCacheService() { 88 return ((GalleryApp) getApplication()).getImageCacheService(); 89 } 90 91 public DataManager getDataManager() { 92 return ((GalleryApp) getApplication()).getDataManager(); 93 } 94 95 public ThreadPool getThreadPool() { 96 return ((GalleryApp) getApplication()).getThreadPool(); 97 } 98 99 public GalleryApp getGalleryApplication() { 100 return (GalleryApp) getApplication(); 101 } 102 103 public synchronized StateManager getStateManager() { 104 if (mStateManager == null) { 105 mStateManager = new StateManager(this); 106 } 107 return mStateManager; 108 } 109 110 public GLRoot getGLRoot() { 111 return mGLRootView; 112 } 113 114 public PositionRepository getPositionRepository() { 115 return mPositionRepository; 116 } 117 118 @Override 119 public void setContentView(int resId) { 120 super.setContentView(resId); 121 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); 122 } 123 124 public int getActionBarHeight() { 125 ActionBar actionBar = getActionBar(); 126 return actionBar != null ? actionBar.getHeight() : 0; 127 } 128 129 protected void onStorageReady() { 130 if (mAlertDialog != null) { 131 mAlertDialog.dismiss(); 132 mAlertDialog = null; 133 unregisterReceiver(mMountReceiver); 134 } 135 } 136 137 @Override 138 protected void onStart() { 139 super.onStart(); 140 if (getExternalCacheDir() == null) { 141 OnCancelListener onCancel = new OnCancelListener() { 142 @Override 143 public void onCancel(DialogInterface dialog) { 144 finish(); 145 } 146 }; 147 OnClickListener onClick = new OnClickListener() { 148 @Override 149 public void onClick(DialogInterface dialog, int which) { 150 dialog.cancel(); 151 } 152 }; 153 mAlertDialog = new AlertDialog.Builder(this) 154 .setIcon(android.R.drawable.ic_dialog_alert) 155 .setTitle("No Storage") 156 .setMessage("No external storage available.") 157 .setNegativeButton(android.R.string.cancel, onClick) 158 .setOnCancelListener(onCancel) 159 .show(); 160 registerReceiver(mMountReceiver, mMountFilter); 161 } 162 } 163 164 @Override 165 protected void onStop() { 166 super.onStop(); 167 if (mAlertDialog != null) { 168 unregisterReceiver(mMountReceiver); 169 mAlertDialog.dismiss(); 170 mAlertDialog = null; 171 } 172 } 173 174 @Override 175 protected void onResume() { 176 super.onResume(); 177 mGLRootView.lockRenderThread(); 178 try { 179 getStateManager().resume(); 180 getDataManager().resume(); 181 } finally { 182 mGLRootView.unlockRenderThread(); 183 } 184 mGLRootView.onResume(); 185 } 186 187 @Override 188 protected void onPause() { 189 super.onPause(); 190 mGLRootView.onPause(); 191 mGLRootView.lockRenderThread(); 192 try { 193 getStateManager().pause(); 194 getDataManager().pause(); 195 } finally { 196 mGLRootView.unlockRenderThread(); 197 } 198 } 199 200 @Override 201 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 202 mGLRootView.lockRenderThread(); 203 try { 204 getStateManager().notifyActivityResult( 205 requestCode, resultCode, data); 206 } finally { 207 mGLRootView.unlockRenderThread(); 208 } 209 } 210 211 @Override 212 public GalleryActionBar getGalleryActionBar() { 213 return null; 214 } 215 216 // Shows status bar in portrait view, hide in landscape view 217 private void toggleStatusBarByOrientation() { 218 Window win = getWindow(); 219 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { 220 win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 221 } else { 222 win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 223 } 224 } 225} 226