blob: bfacc5484e97635b3774c0ba56697deb75913fb0 [file] [log] [blame]
Owen Linf9a0a432011-08-17 22:07:43 +08001/*
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 }
101
102 activity.invalidateOptionsMenu();
103
104 if ((mFlags & FLAG_HIDE_STATUS_BAR) != 0) {
105 WindowManager.LayoutParams params = ((Activity) mActivity).getWindow().getAttributes();
106 params.systemUiVisibility = View.STATUS_BAR_HIDDEN;
107 ((Activity) mActivity).getWindow().setAttributes(params);
108 } else {
109 WindowManager.LayoutParams params = ((Activity) mActivity).getWindow().getAttributes();
110 params.systemUiVisibility = View.STATUS_BAR_VISIBLE;
111 ((Activity) mActivity).getWindow().setAttributes(params);
112 }
113
114 ResultEntry entry = mReceivedResults;
115 if (entry != null) {
116 mReceivedResults = null;
117 onStateResult(entry.requestCode, entry.resultCode, entry.resultData);
118 }
119 onResume();
120 }
121
122 // a subclass of ActivityState should override the method to resume itself
123 protected void onResume() {
124 }
125
126 protected boolean onCreateActionBar(Menu menu) {
127 return false;
128 }
129
130 protected boolean onItemSelected(MenuItem item) {
131 return false;
132 }
133
134 protected void onDestroy() {
135 }
136}