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