Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 19 | import android.app.Activity; |
| 20 | import android.app.AlertDialog; |
| 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.DialogInterface; |
| 24 | import android.content.DialogInterface.OnCancelListener; |
| 25 | import android.content.DialogInterface.OnClickListener; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 28 | import android.content.res.Configuration; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 29 | import android.os.Bundle; |
Ray Chen | 655f63f | 2012-03-16 14:38:04 +0800 | [diff] [blame] | 30 | import android.view.MenuItem; |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 31 | import android.view.Window; |
| 32 | import android.view.WindowManager; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 33 | |
Owen Lin | 4bb5912 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 34 | import com.android.gallery3d.R; |
| 35 | import com.android.gallery3d.data.DataManager; |
Owen Lin | d8fb81f | 2012-03-29 14:27:58 +0800 | [diff] [blame] | 36 | import com.android.gallery3d.data.MediaItem; |
Owen Lin | 4bb5912 | 2012-03-07 17:39:56 +0800 | [diff] [blame] | 37 | import com.android.gallery3d.ui.GLRoot; |
| 38 | import com.android.gallery3d.ui.GLRootView; |
| 39 | import com.android.gallery3d.util.ThreadPool; |
| 40 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 41 | public class AbstractGalleryActivity extends Activity implements GalleryActivity { |
| 42 | @SuppressWarnings("unused") |
| 43 | private static final String TAG = "AbstractGalleryActivity"; |
| 44 | private GLRootView mGLRootView; |
| 45 | private StateManager mStateManager; |
Ray Chen | 8cab3e8 | 2012-03-20 16:32:57 +0800 | [diff] [blame] | 46 | private GalleryActionBar mActionBar; |
Wu-cheng Li | b1db75c | 2012-04-24 18:33:10 +0800 | [diff] [blame^] | 47 | private boolean mDisableToggleStatusBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 59 | protected void onCreate(Bundle savedInstanceState) { |
| 60 | super.onCreate(savedInstanceState); |
| 61 | toggleStatusBarByOrientation(); |
| 62 | } |
| 63 | |
| 64 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 75 | @Override |
| 76 | public void onConfigurationChanged(Configuration config) { |
| 77 | super.onConfigurationChanged(config); |
| 78 | mStateManager.onConfigurationChange(config); |
Ray Chen | 108667b | 2011-11-09 10:49:09 +0800 | [diff] [blame] | 79 | invalidateOptionsMenu(); |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 80 | toggleStatusBarByOrientation(); |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 81 | } |
| 82 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 83 | public Context getAndroidContext() { |
| 84 | return this; |
| 85 | } |
| 86 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 87 | public DataManager getDataManager() { |
| 88 | return ((GalleryApp) getApplication()).getDataManager(); |
| 89 | } |
| 90 | |
| 91 | public ThreadPool getThreadPool() { |
| 92 | return ((GalleryApp) getApplication()).getThreadPool(); |
| 93 | } |
| 94 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 106 | @Override |
| 107 | public void setContentView(int resId) { |
| 108 | super.setContentView(resId); |
| 109 | mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); |
| 110 | } |
| 111 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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 | } |
Owen Lin | d8fb81f | 2012-03-29 14:27:58 +0800 | [diff] [blame] | 181 | MediaItem.getMicroThumbPool().clear(); |
Chih-Chung Chang | b8be1e0 | 2012-04-17 20:35:14 +0800 | [diff] [blame] | 182 | MediaItem.getThumbPool().clear(); |
Owen Lin | cafd30f | 2012-04-09 10:15:35 +0800 | [diff] [blame] | 183 | MediaItem.getBytesBufferPool().clear(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | @Override |
Wu-cheng Li | 3957efd | 2012-04-03 19:08:35 +0800 | [diff] [blame] | 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 |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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() { |
Ray Chen | 8cab3e8 | 2012-03-20 16:32:57 +0800 | [diff] [blame] | 210 | if (mActionBar == null) { |
| 211 | mActionBar = new GalleryActionBar(this); |
| 212 | } |
| 213 | return mActionBar; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 214 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 215 | |
Ray Chen | 655f63f | 2012-03-16 14:38:04 +0800 | [diff] [blame] | 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 | |
Wu-cheng Li | b1db75c | 2012-04-24 18:33:10 +0800 | [diff] [blame^] | 227 | protected void disableToggleStatusBar() { |
| 228 | mDisableToggleStatusBar = true; |
| 229 | } |
| 230 | |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 231 | // Shows status bar in portrait view, hide in landscape view |
| 232 | private void toggleStatusBarByOrientation() { |
Wu-cheng Li | b1db75c | 2012-04-24 18:33:10 +0800 | [diff] [blame^] | 233 | if (mDisableToggleStatusBar) return; |
| 234 | |
Ray Chen | fa011a2 | 2012-02-07 20:23:44 +0800 | [diff] [blame] | 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 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 241 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 242 | } |