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