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; |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 30 | import android.view.Window; |
| 31 | import android.view.WindowManager; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 32 | |
Owen Lin | 4bb5912 | 2012-03-07 17:39:56 +0800 | [diff] [blame^] | 33 | import com.android.gallery3d.R; |
| 34 | import com.android.gallery3d.data.DataManager; |
| 35 | import com.android.gallery3d.ui.BitmapPool; |
| 36 | import com.android.gallery3d.ui.GLRoot; |
| 37 | import com.android.gallery3d.ui.GLRootView; |
| 38 | import com.android.gallery3d.util.ThreadPool; |
| 39 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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; |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 45 | |
| 46 | private AlertDialog mAlertDialog = null; |
| 47 | private BroadcastReceiver mMountReceiver = new BroadcastReceiver() { |
| 48 | @Override |
| 49 | public void onReceive(Context context, Intent intent) { |
| 50 | if (getExternalCacheDir() != null) onStorageReady(); |
| 51 | } |
| 52 | }; |
| 53 | private IntentFilter mMountFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED); |
| 54 | |
| 55 | @Override |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 56 | protected void onCreate(Bundle savedInstanceState) { |
| 57 | super.onCreate(savedInstanceState); |
| 58 | toggleStatusBarByOrientation(); |
| 59 | } |
| 60 | |
| 61 | @Override |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 62 | protected void onSaveInstanceState(Bundle outState) { |
| 63 | mGLRootView.lockRenderThread(); |
| 64 | try { |
| 65 | super.onSaveInstanceState(outState); |
| 66 | getStateManager().saveState(outState); |
| 67 | } finally { |
| 68 | mGLRootView.unlockRenderThread(); |
| 69 | } |
| 70 | } |
| 71 | |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 72 | @Override |
| 73 | public void onConfigurationChanged(Configuration config) { |
| 74 | super.onConfigurationChanged(config); |
| 75 | mStateManager.onConfigurationChange(config); |
Ray Chen | 108667b | 2011-11-09 10:49:09 +0800 | [diff] [blame] | 76 | invalidateOptionsMenu(); |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 77 | toggleStatusBarByOrientation(); |
Owen Lin | 8e56570 | 2011-09-09 13:33:44 +0800 | [diff] [blame] | 78 | } |
| 79 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 80 | public Context getAndroidContext() { |
| 81 | return this; |
| 82 | } |
| 83 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 84 | public DataManager getDataManager() { |
| 85 | return ((GalleryApp) getApplication()).getDataManager(); |
| 86 | } |
| 87 | |
| 88 | public ThreadPool getThreadPool() { |
| 89 | return ((GalleryApp) getApplication()).getThreadPool(); |
| 90 | } |
| 91 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 92 | public synchronized StateManager getStateManager() { |
| 93 | if (mStateManager == null) { |
| 94 | mStateManager = new StateManager(this); |
| 95 | } |
| 96 | return mStateManager; |
| 97 | } |
| 98 | |
| 99 | public GLRoot getGLRoot() { |
| 100 | return mGLRootView; |
| 101 | } |
| 102 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 103 | @Override |
| 104 | public void setContentView(int resId) { |
| 105 | super.setContentView(resId); |
| 106 | mGLRootView = (GLRootView) findViewById(R.id.gl_root_view); |
| 107 | } |
| 108 | |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 109 | protected void onStorageReady() { |
| 110 | if (mAlertDialog != null) { |
| 111 | mAlertDialog.dismiss(); |
| 112 | mAlertDialog = null; |
| 113 | unregisterReceiver(mMountReceiver); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | protected void onStart() { |
| 119 | super.onStart(); |
| 120 | if (getExternalCacheDir() == null) { |
| 121 | OnCancelListener onCancel = new OnCancelListener() { |
| 122 | @Override |
| 123 | public void onCancel(DialogInterface dialog) { |
| 124 | finish(); |
| 125 | } |
| 126 | }; |
| 127 | OnClickListener onClick = new OnClickListener() { |
| 128 | @Override |
| 129 | public void onClick(DialogInterface dialog, int which) { |
| 130 | dialog.cancel(); |
| 131 | } |
| 132 | }; |
| 133 | mAlertDialog = new AlertDialog.Builder(this) |
| 134 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 135 | .setTitle("No Storage") |
| 136 | .setMessage("No external storage available.") |
| 137 | .setNegativeButton(android.R.string.cancel, onClick) |
| 138 | .setOnCancelListener(onCancel) |
| 139 | .show(); |
| 140 | registerReceiver(mMountReceiver, mMountFilter); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected void onStop() { |
| 146 | super.onStop(); |
| 147 | if (mAlertDialog != null) { |
| 148 | unregisterReceiver(mMountReceiver); |
| 149 | mAlertDialog.dismiss(); |
| 150 | mAlertDialog = null; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | protected void onResume() { |
| 156 | super.onResume(); |
| 157 | mGLRootView.lockRenderThread(); |
| 158 | try { |
| 159 | getStateManager().resume(); |
| 160 | getDataManager().resume(); |
| 161 | } finally { |
| 162 | mGLRootView.unlockRenderThread(); |
| 163 | } |
| 164 | mGLRootView.onResume(); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | protected void onPause() { |
| 169 | super.onPause(); |
| 170 | mGLRootView.onPause(); |
| 171 | mGLRootView.lockRenderThread(); |
| 172 | try { |
| 173 | getStateManager().pause(); |
| 174 | getDataManager().pause(); |
| 175 | } finally { |
| 176 | mGLRootView.unlockRenderThread(); |
| 177 | } |
Owen Lin | 4bb5912 | 2012-03-07 17:39:56 +0800 | [diff] [blame^] | 178 | BitmapPool.clear(); |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 183 | mGLRootView.lockRenderThread(); |
| 184 | try { |
| 185 | getStateManager().notifyActivityResult( |
| 186 | requestCode, resultCode, data); |
| 187 | } finally { |
| 188 | mGLRootView.unlockRenderThread(); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public GalleryActionBar getGalleryActionBar() { |
| 194 | return null; |
| 195 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 196 | |
| 197 | // Shows status bar in portrait view, hide in landscape view |
| 198 | private void toggleStatusBarByOrientation() { |
Ray Chen | fa011a2 | 2012-02-07 20:23:44 +0800 | [diff] [blame] | 199 | Window win = getWindow(); |
| 200 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) { |
| 201 | win.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 202 | } else { |
| 203 | win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 204 | } |
Pin Ting | 4d50f73 | 2012-02-03 18:35:00 +0800 | [diff] [blame] | 205 | } |
Owen Lin | f9a0a43 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 206 | } |