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