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; |
| 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 |
| 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 | |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 67 | @Override |
| 68 | public void onConfigurationChanged(Configuration config) { |
| 69 | super.onConfigurationChanged(config); |
| 70 | mStateManager.onConfigurationChange(config); |
Ray Chen | 108667b | 2011-11-09 10:49:09 +0800 | [diff] [blame^] | 71 | invalidateOptionsMenu(); |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 72 | } |
| 73 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 74 | public Context getAndroidContext() { |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | public ImageCacheService getImageCacheService() { |
| 79 | return ((GalleryApp) getApplication()).getImageCacheService(); |
| 80 | } |
| 81 | |
| 82 | public DataManager getDataManager() { |
| 83 | return ((GalleryApp) getApplication()).getDataManager(); |
| 84 | } |
| 85 | |
| 86 | public ThreadPool getThreadPool() { |
| 87 | return ((GalleryApp) getApplication()).getThreadPool(); |
| 88 | } |
| 89 | |
| 90 | public GalleryApp getGalleryApplication() { |
| 91 | return (GalleryApp) getApplication(); |
| 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 | public PositionRepository getPositionRepository() { |
| 106 | return mPositionRepository; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public void setContentView(int resId) { |
| 111 | super.setContentView(resId); |
| 112 | mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); |
| 113 | } |
| 114 | |
| 115 | public int getActionBarHeight() { |
| 116 | ActionBar actionBar = getActionBar(); |
| 117 | return actionBar != null ? actionBar.getHeight() : 0; |
| 118 | } |
| 119 | |
| 120 | protected void onStorageReady() { |
| 121 | if (mAlertDialog != null) { |
| 122 | mAlertDialog.dismiss(); |
| 123 | mAlertDialog = null; |
| 124 | unregisterReceiver(mMountReceiver); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected void onStart() { |
| 130 | super.onStart(); |
| 131 | if (getExternalCacheDir() == null) { |
| 132 | OnCancelListener onCancel = new OnCancelListener() { |
| 133 | @Override |
| 134 | public void onCancel(DialogInterface dialog) { |
| 135 | finish(); |
| 136 | } |
| 137 | }; |
| 138 | OnClickListener onClick = new OnClickListener() { |
| 139 | @Override |
| 140 | public void onClick(DialogInterface dialog, int which) { |
| 141 | dialog.cancel(); |
| 142 | } |
| 143 | }; |
| 144 | mAlertDialog = new AlertDialog.Builder(this) |
| 145 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 146 | .setTitle("No Storage") |
| 147 | .setMessage("No external storage available.") |
| 148 | .setNegativeButton(android.R.string.cancel, onClick) |
| 149 | .setOnCancelListener(onCancel) |
| 150 | .show(); |
| 151 | registerReceiver(mMountReceiver, mMountFilter); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | protected void onStop() { |
| 157 | super.onStop(); |
| 158 | if (mAlertDialog != null) { |
| 159 | unregisterReceiver(mMountReceiver); |
| 160 | mAlertDialog.dismiss(); |
| 161 | mAlertDialog = null; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Override |
| 166 | protected void onResume() { |
| 167 | super.onResume(); |
| 168 | mGLRootView.lockRenderThread(); |
| 169 | try { |
| 170 | getStateManager().resume(); |
| 171 | getDataManager().resume(); |
| 172 | } finally { |
| 173 | mGLRootView.unlockRenderThread(); |
| 174 | } |
| 175 | mGLRootView.onResume(); |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | protected void onPause() { |
| 180 | super.onPause(); |
| 181 | mGLRootView.onPause(); |
| 182 | mGLRootView.lockRenderThread(); |
| 183 | try { |
| 184 | getStateManager().pause(); |
| 185 | getDataManager().pause(); |
| 186 | } finally { |
| 187 | mGLRootView.unlockRenderThread(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | @Override |
| 192 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 193 | mGLRootView.lockRenderThread(); |
| 194 | try { |
| 195 | getStateManager().notifyActivityResult( |
| 196 | requestCode, resultCode, data); |
| 197 | } finally { |
| 198 | mGLRootView.unlockRenderThread(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public GalleryActionBar getGalleryActionBar() { |
| 204 | return null; |
| 205 | } |
| 206 | } |