ActivityState.java revision 915c2c5b2c367df71599370613af0924bd7c4887
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 17package com.android.gallery3d.app; 18 19import android.app.ActionBar; 20import android.app.Activity; 21import android.content.BroadcastReceiver; 22import android.content.ContentResolver; 23import android.content.Context; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.content.res.Configuration; 27import android.os.BatteryManager; 28import android.os.Bundle; 29import android.provider.Settings; 30import android.provider.Settings.SettingNotFoundException; 31import android.view.Menu; 32import android.view.MenuItem; 33import android.view.Window; 34import android.view.WindowManager; 35 36import com.android.gallery3d.R; 37import com.android.gallery3d.actionbar.ActionBarInterface; 38import com.android.gallery3d.actionbar.ActionBarUtils; 39import com.android.gallery3d.ui.GLView; 40import com.android.gallery3d.util.GalleryUtils; 41 42abstract public class ActivityState { 43 protected static final int FLAG_HIDE_ACTION_BAR = 1; 44 protected static final int FLAG_HIDE_STATUS_BAR = 2; 45 protected static final int FLAG_SCREEN_ON_WHEN_PLUGGED = 4; 46 protected static final int FLAG_SCREEN_ON_ALWAYS = 8; 47 48 private static final int SCREEN_ON_FLAGS = ( 49 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON 50 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON 51 | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED 52 ); 53 54 protected GalleryActivity mActivity; 55 protected Bundle mData; 56 protected int mFlags; 57 58 protected ResultEntry mReceivedResults; 59 protected ResultEntry mResult; 60 61 protected static class ResultEntry { 62 public int requestCode; 63 public int resultCode = Activity.RESULT_CANCELED; 64 public Intent resultData; 65 } 66 67 protected boolean mHapticsEnabled; 68 private ContentResolver mContentResolver; 69 70 private boolean mDestroyed = false; 71 private boolean mPlugged = false; 72 boolean mIsFinishing = false; 73 74 protected ActivityState() { 75 } 76 77 protected void setContentPane(GLView content) { 78 mActivity.getGLRoot().setContentPane(content); 79 } 80 81 void initialize(GalleryActivity activity, Bundle data) { 82 mActivity = activity; 83 mData = data; 84 mContentResolver = activity.getAndroidContext().getContentResolver(); 85 } 86 87 public Bundle getData() { 88 return mData; 89 } 90 91 protected void onBackPressed() { 92 mActivity.getStateManager().finishState(this); 93 } 94 95 protected void setStateResult(int resultCode, Intent data) { 96 if (mResult == null) return; 97 mResult.resultCode = resultCode; 98 mResult.resultData = data; 99 } 100 101 protected void onConfigurationChanged(Configuration config) { 102 } 103 104 protected void onSaveState(Bundle outState) { 105 } 106 107 protected void onStateResult(int requestCode, int resultCode, Intent data) { 108 } 109 110 protected float[] mBackgroundColor; 111 112 protected int getBackgroundColorId() { 113 return R.color.default_background; 114 } 115 116 protected float[] getBackgroundColor() { 117 return mBackgroundColor; 118 } 119 120 protected void onCreate(Bundle data, Bundle storedState) { 121 mBackgroundColor = GalleryUtils.intColorToFloatARGBArray( 122 mActivity.getResources().getColor(getBackgroundColorId())); 123 } 124 125 protected void clearStateResult() { 126 } 127 128 BroadcastReceiver mPowerIntentReceiver = new BroadcastReceiver() { 129 @Override 130 public void onReceive(Context context, Intent intent) { 131 final String action = intent.getAction(); 132 if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 133 boolean plugged = (0 != intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0)); 134 135 if (plugged != mPlugged) { 136 mPlugged = plugged; 137 setScreenOnFlags(); 138 } 139 } 140 } 141 }; 142 143 void setScreenOnFlags() { 144 final Window win = ((Activity) mActivity).getWindow(); 145 final WindowManager.LayoutParams params = win.getAttributes(); 146 if ((0 != (mFlags & FLAG_SCREEN_ON_ALWAYS)) || 147 (mPlugged && 0 != (mFlags & FLAG_SCREEN_ON_WHEN_PLUGGED))) { 148 params.flags |= SCREEN_ON_FLAGS; 149 } else { 150 params.flags &= ~SCREEN_ON_FLAGS; 151 } 152 win.setAttributes(params); 153 } 154 155 protected void onPause() { 156 if (0 != (mFlags & FLAG_SCREEN_ON_WHEN_PLUGGED)) { 157 ((Activity) mActivity).unregisterReceiver(mPowerIntentReceiver); 158 } 159 } 160 161 // should only be called by StateManager 162 void resume() { 163 AbstractGalleryActivity activity = (AbstractGalleryActivity) mActivity; 164 ActionBarInterface actionBar = ActionBarUtils.getActionBar(activity); 165 if (actionBar != null) { 166 if ((mFlags & FLAG_HIDE_ACTION_BAR) != 0) { 167 actionBar.hide(); 168 } else { 169 actionBar.show(); 170 } 171 int stateCount = mActivity.getStateManager().getStateCount(); 172 mActivity.getGalleryActionBar().setDisplayOptions(stateCount > 1, true); 173 // Default behavior, this can be overridden in ActivityState's onResume. 174 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); 175 } 176 177 activity.invalidateOptionsMenu(); 178 179 setScreenOnFlags(); 180 181 boolean lightsOut = ((mFlags & FLAG_HIDE_STATUS_BAR) != 0); 182 mActivity.getGLRoot().setLightsOutMode(lightsOut); 183 184 ResultEntry entry = mReceivedResults; 185 if (entry != null) { 186 mReceivedResults = null; 187 onStateResult(entry.requestCode, entry.resultCode, entry.resultData); 188 } 189 190 if (0 != (mFlags & FLAG_SCREEN_ON_WHEN_PLUGGED)) { 191 // we need to know whether the device is plugged in to do this correctly 192 final IntentFilter filter = new IntentFilter(); 193 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 194 activity.registerReceiver(mPowerIntentReceiver, filter); 195 } 196 197 try { 198 mHapticsEnabled = Settings.System.getInt(mContentResolver, 199 Settings.System.HAPTIC_FEEDBACK_ENABLED) != 0; 200 } catch (SettingNotFoundException e) { 201 mHapticsEnabled = false; 202 } 203 204 onResume(); 205 206 // the transition store should be cleared after resume; 207 mActivity.getTransitionStore().clear(); 208 } 209 210 // a subclass of ActivityState should override the method to resume itself 211 protected void onResume() { 212 } 213 214 protected boolean onCreateActionBar(Menu menu) { 215 // TODO: we should return false if there is no menu to show 216 // this is a workaround for a bug in system 217 return true; 218 } 219 220 protected boolean onItemSelected(MenuItem item) { 221 return false; 222 } 223 224 protected void onDestroy() { 225 mDestroyed = true; 226 } 227 228 boolean isDestroyed() { 229 return mDestroyed; 230 } 231 232 public boolean isFinishing() { 233 return mIsFinishing; 234 } 235} 236