BrowserActivity.java revision d3e4d5b4ffdf374b836ec9d4d3e315040c8c3779
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.content.Context;
21import android.content.Intent;
22import android.content.res.Configuration;
23import android.os.Bundle;
24import android.util.Log;
25import android.view.ActionMode;
26import android.view.ContextMenu;
27import android.view.ContextMenu.ContextMenuInfo;
28import android.view.KeyEvent;
29import android.view.Menu;
30import android.view.MenuItem;
31import android.view.View;
32import android.view.Window;
33import android.view.WindowManager;
34import android.view.accessibility.AccessibilityManager;
35
36import com.google.common.annotations.VisibleForTesting;
37
38public class BrowserActivity extends Activity {
39
40    public static final String ACTION_SHOW_BOOKMARKS = "show_bookmarks";
41    public static final String ACTION_SHOW_BROWSER = "show_browser";
42    public static final String ACTION_RESTART = "--restart--";
43    private static final String EXTRA_STATE = "state";
44
45    private final static String LOGTAG = "browser";
46
47    private final static boolean LOGV_ENABLED =
48            com.android.browser.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");
57        }
58        super.onCreate(icicle);
59
60        BrowserSettings settings = BrowserSettings.getInstance();
61
62        // render the browser in OpenGL
63        if (settings.isHardwareAccelerated()) {
64            // Set the flag in the activity's window
65            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
66                    WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
67        } else {
68            // Clear the flag in the activity's window
69            this.getWindow().setFlags(0, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
70        }
71
72        // If this was a web search request, pass it on to the default web
73        // search provider and finish this activity.
74        if (IntentHandler.handleWebSearchIntent(this, null, getIntent())) {
75            finish();
76            return;
77        }
78
79        AccessibilityManager accessibilityManager = (AccessibilityManager)
80                getSystemService(ACCESSIBILITY_SERVICE);
81        if (accessibilityManager != null && accessibilityManager.isEnabled()) {
82            setDefaultKeyMode(DEFAULT_KEYS_DISABLE);
83        }
84
85        mController = new Controller(this);
86        boolean xlarge = isTablet(this);
87        if (xlarge) {
88            mUi = new XLargeUi(this, mController);
89        } else {
90            mUi = new PhoneUi(this, mController);
91        }
92        mController.setUi(mUi);
93
94        Bundle state = getIntent().getBundleExtra(EXTRA_STATE);
95        if (state != null && icicle == null) {
96            icicle = state;
97        }
98
99        mController.start(icicle, getIntent());
100    }
101
102    public static boolean isTablet(Context context) {
103        return context.getResources().getBoolean(R.bool.isTablet);
104    }
105
106    @VisibleForTesting
107    Controller getController() {
108        return mController;
109    }
110
111    @Override
112    protected void onNewIntent(Intent intent) {
113        if (ACTION_RESTART.equals(intent.getAction())) {
114            Bundle outState = new Bundle();
115            mController.onSaveInstanceState(outState, true);
116            finish();
117            getApplicationContext().startActivity(
118                    new Intent(getApplicationContext(), BrowserActivity.class)
119                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
120                    .putExtra(EXTRA_STATE, outState));
121            return;
122        }
123        mController.handleNewIntent(intent);
124    }
125
126    @Override
127    protected void onResume() {
128        super.onResume();
129        if (LOGV_ENABLED) {
130            Log.v(LOGTAG, "BrowserActivity.onResume: this=" + this);
131        }
132        if (mController != null) {
133            mController.onResume();
134        }
135    }
136
137    @Override
138    public boolean onMenuOpened(int featureId, Menu menu) {
139        if (Window.FEATURE_OPTIONS_PANEL == featureId) {
140            mController.onMenuOpened(featureId, menu);
141        }
142        return true;
143    }
144
145    @Override
146    public void onOptionsMenuClosed(Menu menu) {
147        mController.onOptionsMenuClosed(menu);
148    }
149
150    @Override
151    public void onContextMenuClosed(Menu menu) {
152        super.onContextMenuClosed(menu);
153        mController.onContextMenuClosed(menu);
154    }
155
156    /**
157     *  onSaveInstanceState(Bundle map)
158     *  onSaveInstanceState is called right before onStop(). The map contains
159     *  the saved state.
160     */
161    @Override
162    protected void onSaveInstanceState(Bundle outState) {
163        if (LOGV_ENABLED) {
164            Log.v(LOGTAG, "BrowserActivity.onSaveInstanceState: this=" + this);
165        }
166        mController.onSaveInstanceState(outState, true);
167    }
168
169    @Override
170    protected void onPause() {
171        if (mController != null) {
172            mController.onPause();
173        }
174        super.onPause();
175    }
176
177    @Override
178    protected void onDestroy() {
179        if (LOGV_ENABLED) {
180            Log.v(LOGTAG, "BrowserActivity.onDestroy: this=" + this);
181        }
182        super.onDestroy();
183        if (mController != null) {
184            mController.onDestroy();
185        }
186        mUi = null;
187        mController = null;
188    }
189
190    @Override
191    public void onConfigurationChanged(Configuration newConfig) {
192        super.onConfigurationChanged(newConfig);
193        mController.onConfgurationChanged(newConfig);
194    }
195
196    @Override
197    public void onLowMemory() {
198        super.onLowMemory();
199        mController.onLowMemory();
200    }
201
202    @Override
203    public boolean onCreateOptionsMenu(Menu menu) {
204        super.onCreateOptionsMenu(menu);
205        return mController.onCreateOptionsMenu(menu);
206    }
207
208    @Override
209    public boolean onPrepareOptionsMenu(Menu menu) {
210        super.onPrepareOptionsMenu(menu);
211        return mController.onPrepareOptionsMenu(menu);
212    }
213
214    @Override
215    public boolean onOptionsItemSelected(MenuItem item) {
216        if (!mController.onOptionsItemSelected(item)) {
217            return super.onOptionsItemSelected(item);
218        }
219        return true;
220    }
221
222    @Override
223    public void onCreateContextMenu(ContextMenu menu, View v,
224            ContextMenuInfo menuInfo) {
225        mController.onCreateContextMenu(menu, v, menuInfo);
226    }
227
228    @Override
229    public boolean onContextItemSelected(MenuItem item) {
230        return mController.onContextItemSelected(item);
231    }
232
233    @Override
234    public boolean onKeyDown(int keyCode, KeyEvent event) {
235        return mController.onKeyDown(keyCode, event) ||
236            super.onKeyDown(keyCode, event);
237    }
238
239    @Override
240    public boolean onKeyLongPress(int keyCode, KeyEvent event) {
241        return mController.onKeyLongPress(keyCode, event) ||
242            super.onKeyLongPress(keyCode, event);
243    }
244
245    @Override
246    public boolean onKeyUp(int keyCode, KeyEvent event) {
247        return mController.onKeyUp(keyCode, event) ||
248            super.onKeyUp(keyCode, event);
249    }
250
251    @Override
252    public void onActionModeStarted(ActionMode mode) {
253        super.onActionModeStarted(mode);
254        mController.onActionModeStarted(mode);
255    }
256
257    @Override
258    public void onActionModeFinished(ActionMode mode) {
259        super.onActionModeFinished(mode);
260        mController.onActionModeFinished(mode);
261    }
262
263    @Override
264    protected void onActivityResult(int requestCode, int resultCode,
265            Intent intent) {
266        mController.onActivityResult(requestCode, resultCode, intent);
267    }
268
269    @Override
270    public boolean onSearchRequested() {
271        return mController.onSearchRequested();
272    }
273
274}
275