ActivityState.java revision 57cbaa1ef40d2215c6400f5ae4af3a09e67abb2d
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    private boolean mDestroyed = false;
50
51    protected ActivityState() {
52    }
53
54    protected void setContentPane(GLView content) {
55        mActivity.getGLRoot().setContentPane(content);
56    }
57
58    void initialize(GalleryActivity activity, Bundle data) {
59        mActivity = activity;
60        mData = data;
61    }
62
63    public Bundle getData() {
64        return mData;
65    }
66
67    protected void onBackPressed() {
68        mActivity.getStateManager().finishState(this);
69    }
70
71    protected void setStateResult(int resultCode, Intent data) {
72        if (mResult == null) return;
73        mResult.resultCode = resultCode;
74        mResult.resultData = data;
75    }
76
77    protected void onConfigurationChanged(Configuration config) {
78    }
79
80    protected void onSaveState(Bundle outState) {
81    }
82
83    protected void onStateResult(int requestCode, int resultCode, Intent data) {
84    }
85
86    protected void onCreate(Bundle data, Bundle storedState) {
87    }
88
89    protected void onPause() {
90    }
91
92    // should only be called by StateManager
93    void resume() {
94        Activity activity = (Activity) mActivity;
95        ActionBar actionBar = activity.getActionBar();
96        if (actionBar != null) {
97            if ((mFlags & FLAG_HIDE_ACTION_BAR) != 0) {
98                actionBar.hide();
99            } else {
100                actionBar.show();
101            }
102            int stateCount = mActivity.getStateManager().getStateCount();
103            actionBar.setDisplayOptions(
104                    stateCount == 1 ? 0 : ActionBar.DISPLAY_HOME_AS_UP,
105                    ActionBar.DISPLAY_HOME_AS_UP);
106            actionBar.setHomeButtonEnabled(true);
107        }
108
109        activity.invalidateOptionsMenu();
110
111        if ((mFlags & FLAG_HIDE_STATUS_BAR) != 0) {
112            WindowManager.LayoutParams params = ((Activity) mActivity).getWindow().getAttributes();
113            params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
114            ((Activity) mActivity).getWindow().setAttributes(params);
115        } else {
116            WindowManager.LayoutParams params = ((Activity) mActivity).getWindow().getAttributes();
117            params.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE;
118            ((Activity) mActivity).getWindow().setAttributes(params);
119        }
120
121        ResultEntry entry = mReceivedResults;
122        if (entry != null) {
123            mReceivedResults = null;
124            onStateResult(entry.requestCode, entry.resultCode, entry.resultData);
125        }
126        onResume();
127    }
128
129    // a subclass of ActivityState should override the method to resume itself
130    protected void onResume() {
131    }
132
133    protected boolean onCreateActionBar(Menu menu) {
134        // TODO: we should return false if there is no menu to show
135        //       this is a workaround for a bug in system
136        return true;
137    }
138
139    protected boolean onItemSelected(MenuItem item) {
140        return false;
141    }
142
143    protected void onDestroy() {
144        mDestroyed = true;
145    }
146
147    boolean isDestroyed() {
148        return mDestroyed;
149    }
150}
151