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