blob: ecf166dfba40abe6995b5d0cad50a0df43e8595a [file] [log] [blame]
The Android Open Source Projected217d92008-12-17 18:05:52 -08001/*
2 * Copyright (C) 2008 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;
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080020import android.app.Dialog;
The Android Open Source Projected217d92008-12-17 18:05:52 -080021import android.content.Intent;
22import android.net.Uri;
23import android.os.Bundle;
24import android.os.Handler;
25import android.os.Message;
26import android.util.Config;
27import android.util.Log;
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080028import android.view.Gravity;
The Android Open Source Projected217d92008-12-17 18:05:52 -080029import android.view.KeyEvent;
30import android.view.Window;
31import android.widget.BaseAdapter;
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080032import android.widget.Toast;
The Android Open Source Projected217d92008-12-17 18:05:52 -080033
34import android.webkit.gears.NativeDialog;
35
36import com.android.browser.GearsBaseDialog;
37import com.android.browser.GearsPermissionsDialog;
38import com.android.browser.GearsSettingsDialog;
The Android Open Source Projected217d92008-12-17 18:05:52 -080039
40/**
41 * Native dialog Activity used by gears
42 * TODO: rename in GearsNativeDialogActivity
43 * @hide
44 */
45public class GearsNativeDialog extends Activity {
46
47 private static final String TAG = "GearsNativeDialog";
48
49 private String mDialogArguments;
50
51 private String mGearsVersion = null;
52
53 private boolean mDebug = false;
54
55 private int mDialogType;
56 private final int SETTINGS_DIALOG = 1;
57 private final int PERMISSION_DIALOG = 2;
The Android Open Source Projectaec52de2009-03-02 22:54:38 -080058 private final int LOCATION_DIALOG = 3;
The Android Open Source Projected217d92008-12-17 18:05:52 -080059
60 private final String VERSION_STRING = "version";
61 private final String SETTINGS_DIALOG_STRING = "settings_dialog";
62 private final String PERMISSION_DIALOG_STRING = "permissions_dialog";
The Android Open Source Projected217d92008-12-17 18:05:52 -080063 private final String LOCATION_DIALOG_STRING = "locations_dialog";
The Android Open Source Projected217d92008-12-17 18:05:52 -080064
65 private boolean mDialogDismissed = false;
66
67 GearsBaseDialog dialog;
68
69 // Handler for callbacks to the UI thread
70 final Handler mHandler = new Handler() {
71 public void handleMessage(Message msg) {
72 if (msg.what == GearsBaseDialog.NEW_ICON) {
73 BaseAdapter adapter = (BaseAdapter) msg.obj;
74 adapter.notifyDataSetChanged();
75 } else if (msg.what == GearsBaseDialog.UPDATE_ICON) {
76 dialog.updateIcon();
77 } else if (msg.what == GearsBaseDialog.ALWAYS_DENY) {
78 closeDialog(GearsBaseDialog.ALWAYS_DENY);
79 } else if (msg.what == GearsBaseDialog.ALLOW) {
80 closeDialog(GearsBaseDialog.ALLOW);
81 } else if (msg.what == GearsBaseDialog.DENY) {
82 closeDialog(GearsBaseDialog.DENY);
83 }
84 super.handleMessage(msg);
85 }
86 };
87
88 @Override
89 public void onCreate(Bundle icicle) {
The Android Open Source Projected217d92008-12-17 18:05:52 -080090 getArguments();
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080091 if (mDialogType == SETTINGS_DIALOG) {
92 setTheme(android.R.style.Theme);
93 }
94 super.onCreate(icicle);
95 if (mDialogType != SETTINGS_DIALOG) {
96 requestWindowFeature(Window.FEATURE_NO_TITLE);
97 setContentView(R.layout.gears_dialog);
98 }
The Android Open Source Projected217d92008-12-17 18:05:52 -080099
100 switch (mDialogType) {
101 case SETTINGS_DIALOG:
102 dialog = new GearsSettingsDialog(this, mHandler, mDialogArguments);
103 dialog.setGearsVersion(mGearsVersion);
104 break;
105 case PERMISSION_DIALOG:
106 dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
107 break;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800108 case LOCATION_DIALOG:
109 dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
110 break;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800111 default:
112 dialog = new GearsBaseDialog(this, mHandler, mDialogArguments);
113 }
114 dialog.setDebug(mDebug);
115 dialog.setup();
116 }
117
118 /**
119 * Get the arguments for the dialog
120 *
121 * The dialog needs a json string as an argument, as
122 * well as a dialogType. In debug mode the arguments
123 * are mocked.
124 */
125 private void getArguments() {
126 if (mDebug) {
The Android Open Source Projectaec52de2009-03-02 22:54:38 -0800127 mDialogType = LOCATION_DIALOG +1;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800128 mockArguments();
129
130 return;
131 }
132
133 Intent intent = getIntent();
134 mDialogArguments = intent.getStringExtra("dialogArguments");
135 String dialogTypeString = intent.getStringExtra("dialogType");
136 if (dialogTypeString == null) {
137 return;
138 }
139
140 if (Config.LOGV) {
141 Log.v(TAG, "dialogtype: " + dialogTypeString);
142 }
143
144 if (dialogTypeString.equalsIgnoreCase(SETTINGS_DIALOG_STRING)) {
145 mDialogType = SETTINGS_DIALOG;
146 mGearsVersion = intent.getStringExtra(VERSION_STRING);
147 } else if (dialogTypeString.equalsIgnoreCase(PERMISSION_DIALOG_STRING)) {
148 mDialogType = PERMISSION_DIALOG;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800149 } else if (dialogTypeString.equalsIgnoreCase(LOCATION_DIALOG_STRING)) {
150 mDialogType = LOCATION_DIALOG;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800151 }
152 }
153
154 /**
155 * Utility method for debugging the dialog.
156 *
157 * Set mock arguments.
158 */
159 private void mockArguments() {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800160
161 String argumentsPermissions = "{ locale: \"en-US\", "
162 + "origin: \"http://www.google.com\", dialogType: \"localData\","
163 + "customIcon: \"http://google-gears.googlecode.com/"
164 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
165 + "customName: \"My Application\","
166 + "customMessage: \"Press the button to enable my "
167 + "application to run offline!\" };";
168
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800169 String argumentsPermissions2 = "{ locale: \"en-US\", "
170 + "origin: \"http://www.google.com\", dialogType: \"localData\" };";
171
The Android Open Source Projected217d92008-12-17 18:05:52 -0800172 String argumentsLocation = "{ locale: \"en-US\", "
173 + "origin: \"http://www.google.com\", dialogType: \"locationData\","
174 + "customIcon: \"http://google-gears.googlecode.com/"
175 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
176 + "customName: \"My Application\","
177 + "customMessage: \"Press the button to enable my "
178 + "application to run offline!\" };";
179
180 String argumentsSettings = "{ locale: \"en-US\", permissions: [ { "
181 + "name: \"http://www.google.com\", "
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800182 + "localStorage: { permissionState: 0 }, "
183 + "locationData: { permissionState: 1 } }, "
The Android Open Source Projected217d92008-12-17 18:05:52 -0800184 + "{ name: \"http://www.aaronboodman.com\", "
185 + "localStorage: { permissionState: 1 }, "
186 + "locationData: { permissionState: 2 } }, "
187 + "{ name: \"http://www.evil.org\", "
188 + "localStorage: { permissionState: 2 }, "
189 + "locationData: { permissionState: 2 } } ] }";
190
191 switch (mDialogType) {
The Android Open Source Projected217d92008-12-17 18:05:52 -0800192 case PERMISSION_DIALOG:
193 mDialogArguments = argumentsPermissions;
194 break;
195 case LOCATION_DIALOG:
196 mDialogArguments = argumentsLocation;
197 break;
198 case SETTINGS_DIALOG:
199 mDialogArguments = argumentsSettings;
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800200 break;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800201 }
202 }
203
204 /**
205 * Close the dialog and set the return string value.
206 */
207 private void closeDialog(int closingType) {
208 String ret = dialog.closeDialog(closingType);
209
210 if (mDebug) {
211 Log.v(TAG, "closeDialog ret value: " + ret);
212 }
213
214 NativeDialog.closeDialog(ret);
215 notifyEndOfDialog();
216 finish();
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800217
218 // If the dialog sets a notification, we display it.
219 int notification = dialog.notification();
220 if (notification != 0) {
221 Toast toast = Toast.makeText(this, notification, Toast.LENGTH_LONG);
222 toast.setGravity(Gravity.BOTTOM, 0, 0);
223 toast.show();
224 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800225 }
226
227 @Override
228 public void onDestroy() {
229 super.onDestroy();
230 // In case we reach this point without
231 // notifying NativeDialog, we do it now.
232 if (!mDialogDismissed) {
233 notifyEndOfDialog();
234 }
235 }
236
237 @Override
238 public void onPause(){
239 super.onPause();
240 if (!mDialogDismissed) {
241 closeDialog(GearsBaseDialog.CANCEL);
242 }
243 }
244
245 /**
246 * Signal to NativeDialog that we are done.
247 */
248 private void notifyEndOfDialog() {
249 NativeDialog.signalFinishedDialog();
250 mDialogDismissed = true;
251 }
252
253 /**
254 * Intercepts the back key to immediately notify
255 * NativeDialog that we are done.
256 */
257 public boolean dispatchKeyEvent(KeyEvent event) {
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800258 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
259 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
260 if (!dialog.handleBackButton()) {
261 // if the dialog doesn't do anything with the back button
262 closeDialog(GearsBaseDialog.CANCEL);
263 }
264 return true; // event consumed
The Android Open Source Projected217d92008-12-17 18:05:52 -0800265 }
266 return super.dispatchKeyEvent(event);
267 }
268
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800269 /**
270 * If the dialog call showDialog() on ourself, we let
271 * it handle the creation of this secondary dialog.
272 * It is used in GearsSettingsDialog, to create the confirmation
273 * dialog when the user click on "Remove this site from Gears"
274 */
275 @Override
276 protected Dialog onCreateDialog(int id) {
277 return dialog.onCreateDialog(id);
278 }
279
The Android Open Source Projected217d92008-12-17 18:05:52 -0800280}