BrowserActivity.java revision 2b552f5154fb582ddfd3296eaa8ec35675edb613
1/*
2 * Copyright (C) 2006 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.browser;
18
19import android.app.Activity;
20import android.app.KeyguardManager;
21import android.content.Context;
22import android.content.Intent;
23import android.content.res.Configuration;
24import android.os.Bundle;
25import android.os.PowerManager;
26import android.util.Log;
27import android.view.ActionMode;
28import android.view.ContextMenu;
29import android.view.ContextMenu.ContextMenuInfo;
30import android.view.KeyEvent;
31import android.view.Menu;
32import android.view.MenuItem;
33import android.view.MotionEvent;
34import android.view.View;
35import android.view.Window;
36
37import com.google.common.annotations.VisibleForTesting;
38
39public class BrowserActivity extends Activity {
40
41    public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
42    public static final String ACTION_SHOW_BROWSER = "show_browser";
43    public static final String ACTION_RESTART = "--restart--";
44    private static final String EXTRA_STATE = "state";
45
46    private final static String LOGTAG = "browser";
47
48    private final static boolean LOGV_ENABLED = Browser.LOGV_ENABLED;
49
50    private Controller mController;
51    private UI mUi;
52
53    @Override
54    public void onCreate(Bundle icicle) {
55        if (LOGV_ENABLED) {
56            Log.v(LOGTAG, this + " onStart, has state: "
57                    + (icicle == null ? "false" : "true"));
58        }
59        super.onCreate(icicle);
60
61        if (shouldIgnoreIntents()) {
62            finish();
63            return;
64        }
65
66        // If this was a web search request, pass it on to the default web
67        // search provider and finish this activity.
68        if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
69            finish();
70            return;
71        }
72        mController = new Controller(this);
73        boolean xlarge = isTablet(this);
74        if (xlarge) {
75            mUi = new XLargeUi(this, mController);
76        } else {
77            mUi = new PhoneUi(this, mController);
78        }
79        mController.setUi(mUi);
80
81        Intent intent = (icicle == null) ? getIntent() : null;
82        mController.start(intent);
83    }
84
85    public static boolean isTablet(Context context) {
86        return context.getResources().getBoolean(R.bool.isTablet);
87    }
88
89    @VisibleForTesting
90    Controller getController() {
91        return mController;
92    }
93
94    @Override
95    protected void onNewIntent(Intent intent) {
96        if (shouldIgnoreIntents()) return;
97        if (ACTION_RESTART.equals(intent.getAction())) {
98            Bundle outState = new Bundle();
99            mController.onSaveInstanceState(outState);
100            finish();
101            getApplicationContext().startActivity(
102                    new Intent(getApplicationContext(), BrowserActivity.class)
103                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
104                    .putExtra(EXTRA_STATE, outState));
105            return;
106        }
107        mController.handleNewIntent(intent);
108    }
109
110    private KeyguardManager mKeyguardManager;
111    private PowerManager mPowerManager;
112    private boolean shouldIgnoreIntents() {
113        // Only process intents if the screen is on and the device is unlocked
114        // aka, if we will be user-visible
115        if (mKeyguardManager == null) {
116            mKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
117        }
118        if (mPowerManager == null) {
119            mPowerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
120        }
121        boolean ignore = !mPowerManager.isScreenOn();
122        ignore |= mKeyguardManager.inKeyguardRestrictedInputMode();
123        if (LOGV_ENABLED) {
124            Log.v(LOGTAG, "ignore intents: " + ignore);
125        }
126        return ignore;
127    }
128
129    @Override
130    protected void onResume() {
131        super.onResume();
132        if (LOGV_ENABLED) {
133            Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
134        }
135        if (mController != null) {
136            mController.onResume();
137        }
138    }
139
140    @Override
141    public boolean onMenuOpened(int featureId, Menu menu) {
142        if (Window.FEATURE_OPTIONS_PANEL == featureId) {
143            mController.onMenuOpened(featureId, menu);
144        }
145        return true;
146    }
147
148    @Override
149    public void onOptionsMenuClosed(Menu menu) {
150        mController.onOptionsMenuClosed(menu);
151    }
152
153    @Override
154    public void onContextMenuClosed(Menu menu) {
155        super.onContextMenuClosed(menu);
156        mController.onContextMenuClosed(menu);
157    }
158
159    /**
160     *  onSaveInstanceState(Bundle map)
161     *  onSaveInstanceState is called right before onStop(). The map contains
162     *  the saved state.
163     */
164    @Override
165    protected void onSaveInstanceState(Bundle outState) {
166        if (LOGV_ENABLED) {
167            Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
168        }
169        mController.onSaveInstanceState(outState);
170    }
171
172    @Override
173    protected void onPause() {
174        if (mController != null) {
175            mController.onPause();
176        }
177        super.onPause();
178    }
179
180    @Override
181    protected void onDestroy() {
182        if (LOGV_ENABLED) {
183            Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
184        }
185        super.onDestroy();
186        if (mController != null) {
187            mController.onDestroy();
188        }
189        mUi = null;
190        mController = null;
191    }
192
193    @Override
194    public void onConfigurationChanged(Configuration newConfig) {
195        super.onConfigurationChanged(newConfig);
196        if (mController != null) {
197            mController.onConfgurationChanged(newConfig);
198        }
199    }
200
201    @Override
202    public void onLowMemory() {
203        super.onLowMemory();
204        mController.onLowMemory();
205    }
206
207    @Override
208    public boolean onCreateOptionsMenu(Menu menu) {
209        super.onCreateOptionsMenu(menu);
210        return (mController != null) && mController.onCreateOptionsMenu(menu);
211    }
212
213    @Override
214    public boolean onPrepareOptionsMenu(Menu menu) {
215        super.onPrepareOptionsMenu(menu);
216        return mController.onPrepareOptionsMenu(menu);
217    }
218
219    @Override
220    public boolean onOptionsItemSelected(MenuItem item) {
221        if (!mController.onOptionsItemSelected(item)) {
222            return super.onOptionsItemSelected(item);
223        }
224        return true;
225    }
226
227    @Override
228    public void onCreateContextMenu(ContextMenu menu, View v,
229            ContextMenuInfo menuInfo) {
230        mController.onCreateContextMenu(menu, v, menuInfo);
231    }
232
233    @Override
234    public boolean onContextItemSelected(MenuItem item) {
235        return mController.onContextItemSelected(item);
236    }
237
238    @Override
239    public boolean onKeyDown(int keyCode, KeyEvent event) {
240        return mController.onKeyDown(keyCode, event) ||
241            super.onKeyDown(keyCode, event);
242    }
243
244    @Override
245    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
246        return mController.onKeyLongPress(keyCode, event) ||
247            super.onKeyLongPress(keyCode, event);
248    }
249
250    @Override
251    public boolean onKeyUp(int keyCode, KeyEvent event) {
252        return mController.onKeyUp(keyCode, event) ||
253            super.onKeyUp(keyCode, event);
254    }
255
256    @Override
257    public void onActionModeStarted(ActionMode mode) {
258        super.onActionModeStarted(mode);
259        mController.onActionModeStarted(mode);
260    }
261
262    @Override
263    public void onActionModeFinished(ActionMode mode) {
264        super.onActionModeFinished(mode);
265        mController.onActionModeFinished(mode);
266    }
267
268    @Override
269    protected void onActivityResult(int requestCode, int resultCode,
270            Intent intent) {
271        mController.onActivityResult(requestCode, resultCode, intent);
272    }
273
274    @Override
275    public boolean onSearchRequested() {
276        return mController.onSearchRequested();
277    }
278
279    @Override
280    public boolean dispatchKeyEvent(KeyEvent event) {
281        return mController.dispatchKeyEvent(event)
282                || super.dispatchKeyEvent(event);
283    }
284
285    @Override
286    public boolean dispatchKeyShortcutEvent(KeyEvent event) {
287        return mController.dispatchKeyShortcutEvent(event)
288                || super.dispatchKeyShortcutEvent(event);
289    }
290
291    @Override
292    public boolean dispatchTouchEvent(MotionEvent ev) {
293        return mController.dispatchTouchEvent(ev)
294                || super.dispatchTouchEvent(ev);
295    }
296
297    @Override
298    public boolean dispatchTrackballEvent(MotionEvent ev) {
299        return mController.dispatchTrackballEvent(ev)
300                || super.dispatchTrackballEvent(ev);
301    }
302
303    @Override
304    public boolean dispatchGenericMotionEvent(MotionEvent ev) {
305        return mController.dispatchGenericMotionEvent(ev) ||
306                super.dispatchGenericMotionEvent(ev);
307    }
308
309}
310