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 | |
| 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; |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 37 | import android.content.res.Configuration; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 38 | import android.os.Bundle; |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame^] | 39 | import android.view.Window; |
| 40 | import android.view.WindowManager; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 41 | |
| 42 | public 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 |
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 | |
| 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 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame^] | 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 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 225 | } |