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 com.android.gallery3d.R; 20 import com.android.gallery3d.data.DataManager; 21 import com.android.gallery3d.data.ImageCacheService; 22 import com.android.gallery3d.ui.GLRoot; 23 import com.android.gallery3d.ui.GLRootView; 24 import com.android.gallery3d.ui.PositionRepository; 25 import com.android.gallery3d.util.ThreadPool; 26 27 import android.app.ActionBar; 28 import android.app.Activity; 29 import android.app.AlertDialog; 30 import android.content.BroadcastReceiver; 31 import android.content.Context; 32 import android.content.DialogInterface; 33 import android.content.DialogInterface.OnCancelListener; 34 import android.content.DialogInterface.OnClickListener; 35 import android.content.Intent; 36 import android.content.IntentFilter; 37 import android.content.res.Configuration; 38 import android.os.Bundle; 39 40 public class AbstractGalleryActivity extends Activity implements GalleryActivity { 41 @SuppressWarnings("unused") 42 private static final String TAG = "AbstractGalleryActivity"; 43 private GLRootView mGLRootView; 44 private StateManager mStateManager; 45 private PositionRepository mPositionRepository = new PositionRepository(); 46 47 private AlertDialog mAlertDialog = null; 48 private BroadcastReceiver mMountReceiver = new BroadcastReceiver() { 49 @Override 50 public void onReceive(Context context, Intent intent) { 51 if (getExternalCacheDir() != null) onStorageReady(); 52 } 53 }; 54 private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); 55 56 @Override onSaveInstanceState(Bundle outState)57 protected void onSaveInstanceState(Bundle outState) { 58 mGLRootView.lockRenderThread(); 59 try { 60 super.onSaveInstanceState(outState); 61 getStateManager().saveState(outState); 62 } finally { 63 mGLRootView.unlockRenderThread(); 64 } 65 } 66 67 @Override onConfigurationChanged(Configuration config)68 public void onConfigurationChanged(Configuration config) { 69 super.onConfigurationChanged(config); 70 mStateManager.onConfigurationChange(config); 71 } 72 getAndroidContext()73 public Context getAndroidContext() { 74 return this; 75 } 76 getImageCacheService()77 public ImageCacheService getImageCacheService() { 78 return ((GalleryApp) getApplication()).getImageCacheService(); 79 } 80 getDataManager()81 public DataManager getDataManager() { 82 return ((GalleryApp) getApplication()).getDataManager(); 83 } 84 getThreadPool()85 public ThreadPool getThreadPool() { 86 return ((GalleryApp) getApplication()).getThreadPool(); 87 } 88 getGalleryApplication()89 public GalleryApp getGalleryApplication() { 90 return (GalleryApp) getApplication(); 91 } 92 getStateManager()93 public synchronized StateManager getStateManager() { 94 if (mStateManager == null) { 95 mStateManager = new StateManager(this); 96 } 97 return mStateManager; 98 } 99 getGLRoot()100 public GLRoot getGLRoot() { 101 return mGLRootView; 102 } 103 getPositionRepository()104 public PositionRepository getPositionRepository() { 105 return mPositionRepository; 106 } 107 108 @Override setContentView(int resId)109 public void setContentView(int resId) { 110 super.setContentView(resId); 111 mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); 112 } 113 getActionBarHeight()114 public int getActionBarHeight() { 115 ActionBar actionBar = getActionBar(); 116 return actionBar != null ? actionBar.getHeight() : 0; 117 } 118 onStorageReady()119 protected void onStorageReady() { 120 if (mAlertDialog != null) { 121 mAlertDialog.dismiss(); 122 mAlertDialog = null; 123 unregisterReceiver(mMountReceiver); 124 } 125 } 126 127 @Override onStart()128 protected void onStart() { 129 super.onStart(); 130 if (getExternalCacheDir() == null) { 131 OnCancelListener onCancel = new OnCancelListener() { 132 @Override 133 public void onCancel(DialogInterface dialog) { 134 finish(); 135 } 136 }; 137 OnClickListener onClick = new OnClickListener() { 138 @Override 139 public void onClick(DialogInterface dialog, int which) { 140 dialog.cancel(); 141 } 142 }; 143 mAlertDialog = new AlertDialog.Builder(this) 144 .setIcon(android.R.drawable.ic_dialog_alert) 145 .setTitle("No Storage") 146 .setMessage("No external storage available.") 147 .setNegativeButton(android.R.string.cancel, onClick) 148 .setOnCancelListener(onCancel) 149 .show(); 150 registerReceiver(mMountReceiver, mMountFilter); 151 } 152 } 153 154 @Override onStop()155 protected void onStop() { 156 super.onStop(); 157 if (mAlertDialog != null) { 158 unregisterReceiver(mMountReceiver); 159 mAlertDialog.dismiss(); 160 mAlertDialog = null; 161 } 162 } 163 164 @Override onResume()165 protected void onResume() { 166 super.onResume(); 167 mGLRootView.lockRenderThread(); 168 try { 169 getStateManager().resume(); 170 getDataManager().resume(); 171 } finally { 172 mGLRootView.unlockRenderThread(); 173 } 174 mGLRootView.onResume(); 175 } 176 177 @Override onPause()178 protected void onPause() { 179 super.onPause(); 180 mGLRootView.onPause(); 181 mGLRootView.lockRenderThread(); 182 try { 183 getStateManager().pause(); 184 getDataManager().pause(); 185 } finally { 186 mGLRootView.unlockRenderThread(); 187 } 188 } 189 190 @Override onActivityResult(int requestCode, int resultCode, Intent data)191 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 192 mGLRootView.lockRenderThread(); 193 try { 194 getStateManager().notifyActivityResult( 195 requestCode, resultCode, data); 196 } finally { 197 mGLRootView.unlockRenderThread(); 198 } 199 } 200 201 @Override getGalleryActionBar()202 public GalleryActionBar getGalleryActionBar() { 203 return null; 204 } 205 } 206