ActivityState.java revision 8e565703e8d482421714383a8ad40a67929ce2da
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 com.android.gallery3d.ui.GLView;
20
21import android.app.ActionBar;
22import android.app.Activity;
23import android.content.Intent;
24import android.content.res.Configuration;
25import android.os.Bundle;
26import android.view.Menu;
27import android.view.MenuItem;
28import android.view.View;
29import android.view.WindowManager;
30
31abstract public class ActivityState {
32    public static final int FLAG_HIDE_ACTION_BAR = 1;
33    public static final int FLAG_HIDE_STATUS_BAR = 2;
34
35    protected GalleryActivity mActivity;
36    protected Bundle mData;
37    protected int mFlags;
38
39    protected ResultEntry mReceivedResults;
40    protected ResultEntry mResult;
41
42    protected static class ResultEntry {
43        public int requestCode;
44        public int resultCode = Activity.RESULT_CANCELED;
45        public Intent resultData;
46        ResultEntry next;
47    }
48
49    protected ActivityState() {
50    }
51
52    protected void setContentPane(GLView content) {
53        mActivity.getGLRoot().setContentPane(content);
54    }
55
56    void initialize(GalleryActivity activity, Bundle data) {
57        mActivity = activity;
58        mData = data;
59    }
60
61    public Bundle getData() {
62        return mData;
63    }
64
65    protected void onBackPressed() {
66        mActivity.getStateManager().finishState(this);
67    }
68
69    protected void setStateResult(int resultCode, Intent data) {
70        if (mResult == null) return;
71        mResult.resultCode = resultCode;
72        mResult.resultData = data;
73    }
74
75    protected void onConfigurationChanged(Configuration config) {
76    }
77
78    protected void onSaveState(Bundle outState) {
79    }
80
81    protected void onStateResult(int requestCode, int resultCode, Intent data) {
82    }
83
84    protected void onCreate(Bundle data, Bundle storedState) {
85    }
86
87    protected void onPause() {
88    }
89
90    // should only be called by StateManager
91    void resume() {
92        Activity activity = (Activity) mActivity;
93        ActionBar actionBar = activity.getActionBar();
94        if (actionBar != null) {
95            if ((mFlags & FLAG_HIDE_ACTION_BAR) != 0) {
96                actionBar.hide();
97            } else {
98                actionBar.show();
99            }
100            int stateCount = mActivity.getStateManager().getStateCount();
101            actionBar.setDisplayOptions(
102                    stateCount == 1 ? 0 : ActionBar.DISPLAY_HOME_AS_UP,
103                    ActionBar.DISPLAY_HOME_AS_UP);
104            actionBar.setHomeButtonEnabled(true);
105        }
106
107        activity.invalidateOptionsMenu();
108
109        if ((mFlags & FLAG_HIDE_STATUS_BAR) != 0) {
110            WindowManager.LayoutParams params = ((Activity) mActivity).getWindow().getAttributes();
111            params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
112            ((Activity) mActivity).getWindow().setAttributes(params);
113        } else {
114            WindowManager.LayoutParams params = ((Activity) mActivity).getWindow().getAttributes();
115            params.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
116            ((Activity) mActivity).getWindow().setAttributes(params);
117        }
118
119        ResultEntry entry = mReceivedResults;
120        if (entry != null) {
121            mReceivedResults = null;
122            onStateResult(entry.requestCode, entry.resultCode, entry.resultData);
123        }
124        onResume();
125    }
126
127    // a subclass of ActivityState should override the method to resume itself
128    protected void onResume() {
129    }
130
131    protected boolean onCreateActionBar(Menu menu) {
132        // TODO: we should return false if there is no menu to show
133        //       this is a workaround for a bug in system
134        return true;
135    }
136
137    protected boolean onItemSelected(MenuItem item) {
138        return false;
139    }
140
141    protected void onDestroy() {
142    }
143}
144