ActivityState.java revision b1eb01cd02d1e75e6b9c3e593f6ff34335e2ca8a
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.Window;
31import android.view.WindowManager;
32
33import com.android.gallery3d.ui.GLView;
34
35abstract public class ActivityState {
36    protected static final int FLAG_HIDE_ACTION_BAR = 1;
37    protected static final int FLAG_HIDE_STATUS_BAR = 2;
38    protected static final int FLAG_SCREEN_ON_WHEN_PLUGGED = 4;
39    protected static final int FLAG_SCREEN_ON_ALWAYS = 8;
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 ((0 != (mFlags & FLAG_SCREEN_ON_ALWAYS)) ||
121                (mPlugged && 0 != (mFlags & FLAG_SCREEN_ON_WHEN_PLUGGED))) {
122            params.flags |= SCREEN_ON_FLAGS;
123        } else {
124            params.flags &= ~SCREEN_ON_FLAGS;
125        }
126        win.setAttributes(params);
127    }
128
129    protected void onPause() {
130        if (0 != (mFlags & FLAG_SCREEN_ON_WHEN_PLUGGED)) {
131            ((Activity) mActivity).unregisterReceiver(mPowerIntentReceiver);
132        }
133    }
134
135    // should only be called by StateManager
136    void resume() {
137        AbstractGalleryActivity activity = (AbstractGalleryActivity) mActivity;
138        ActionBar actionBar = activity.getActionBar();
139        if (actionBar != null) {
140            if ((mFlags & FLAG_HIDE_ACTION_BAR) != 0) {
141                actionBar.hide();
142            } else {
143                actionBar.show();
144            }
145            int stateCount = mActivity.getStateManager().getStateCount();
146            mActivity.getGalleryActionBar().setDisplayOptions(stateCount > 1, true);
147            // Default behavior, this can be overridden in ActivityState's onResume.
148            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
149        }
150
151        activity.invalidateOptionsMenu();
152
153        setScreenOnFlags();
154
155        boolean lightsOut = ((mFlags & FLAG_HIDE_STATUS_BAR) != 0);
156        mActivity.getGLRoot().setLightsOutMode(lightsOut);
157
158        ResultEntry entry = mReceivedResults;
159        if (entry != null) {
160            mReceivedResults = null;
161            onStateResult(entry.requestCode, entry.resultCode, entry.resultData);
162        }
163
164        if (0 != (mFlags & FLAG_SCREEN_ON_WHEN_PLUGGED)) {
165            // we need to know whether the device is plugged in to do this correctly
166            final IntentFilter filter = new IntentFilter();
167            filter.addAction(Intent.ACTION_BATTERY_CHANGED);
168            activity.registerReceiver(mPowerIntentReceiver, filter);
169        }
170        onResume();
171
172        // the transition store should be cleared after resume;
173        mActivity.getTransitionStore().clear();
174    }
175
176    // a subclass of ActivityState should override the method to resume itself
177    protected void onResume() {
178    }
179
180    protected boolean onCreateActionBar(Menu menu) {
181        // TODO: we should return false if there is no menu to show
182        //       this is a workaround for a bug in system
183        return true;
184    }
185
186    protected boolean onItemSelected(MenuItem item) {
187        return false;
188    }
189
190    protected void onDestroy() {
191        mDestroyed = true;
192    }
193
194    boolean isDestroyed() {
195        return mDestroyed;
196    }
197
198    public boolean isFinishing() {
199        return mIsFinishing;
200    }
201}
202