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