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