blob: c72ad8eaa2e89492b51a6f50ceac4a41a0395377 [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;
39import com.android.browser.GearsShortcutDialog;
40import com.android.browser.GearsFilePickerDialog;
41
42/**
43 * Native dialog Activity used by gears
44 * TODO: rename in GearsNativeDialogActivity
45 * @hide
46 */
47public class GearsNativeDialog extends Activity {
48
49 private static final String TAG = "GearsNativeDialog";
50
51 private String mDialogArguments;
52
53 private String mGearsVersion = null;
54
55 private boolean mDebug = false;
56
57 private int mDialogType;
58 private final int SETTINGS_DIALOG = 1;
59 private final int PERMISSION_DIALOG = 2;
60 private final int SHORTCUT_DIALOG = 3;
61 private final int LOCATION_DIALOG = 4;
62 private final int FILEPICKER_DIALOG = 5;
63
64 private final String VERSION_STRING = "version";
65 private final String SETTINGS_DIALOG_STRING = "settings_dialog";
66 private final String PERMISSION_DIALOG_STRING = "permissions_dialog";
67 private final String SHORTCUT_DIALOG_STRING = "shortcuts_dialog";
68 private final String LOCATION_DIALOG_STRING = "locations_dialog";
69 private final String FILEPICKER_DIALOG_STRING = "filepicker_dialog";
70
71 private boolean mDialogDismissed = false;
72
73 GearsBaseDialog dialog;
74
75 // Handler for callbacks to the UI thread
76 final Handler mHandler = new Handler() {
77 public void handleMessage(Message msg) {
78 if (msg.what == GearsBaseDialog.NEW_ICON) {
79 BaseAdapter adapter = (BaseAdapter) msg.obj;
80 adapter.notifyDataSetChanged();
81 } else if (msg.what == GearsBaseDialog.UPDATE_ICON) {
82 dialog.updateIcon();
83 } else if (msg.what == GearsBaseDialog.ALWAYS_DENY) {
84 closeDialog(GearsBaseDialog.ALWAYS_DENY);
85 } else if (msg.what == GearsBaseDialog.ALLOW) {
86 closeDialog(GearsBaseDialog.ALLOW);
87 } else if (msg.what == GearsBaseDialog.DENY) {
88 closeDialog(GearsBaseDialog.DENY);
89 }
90 super.handleMessage(msg);
91 }
92 };
93
94 @Override
95 public void onCreate(Bundle icicle) {
The Android Open Source Projected217d92008-12-17 18:05:52 -080096 getArguments();
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -080097 if (mDialogType == SETTINGS_DIALOG) {
98 setTheme(android.R.style.Theme);
99 }
100 super.onCreate(icicle);
101 if (mDialogType != SETTINGS_DIALOG) {
102 requestWindowFeature(Window.FEATURE_NO_TITLE);
103 setContentView(R.layout.gears_dialog);
104 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800105
106 switch (mDialogType) {
107 case SETTINGS_DIALOG:
108 dialog = new GearsSettingsDialog(this, mHandler, mDialogArguments);
109 dialog.setGearsVersion(mGearsVersion);
110 break;
111 case PERMISSION_DIALOG:
112 dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
113 break;
114 case SHORTCUT_DIALOG:
115 dialog = new GearsShortcutDialog(this, mHandler, mDialogArguments);
116 break;
117 case LOCATION_DIALOG:
118 dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
119 break;
120 case FILEPICKER_DIALOG:
121 dialog = new GearsFilePickerDialog(this, mHandler, mDialogArguments);
122 break;
123 default:
124 dialog = new GearsBaseDialog(this, mHandler, mDialogArguments);
125 }
126 dialog.setDebug(mDebug);
127 dialog.setup();
128 }
129
130 /**
131 * Get the arguments for the dialog
132 *
133 * The dialog needs a json string as an argument, as
134 * well as a dialogType. In debug mode the arguments
135 * are mocked.
136 */
137 private void getArguments() {
138 if (mDebug) {
139 mDialogType = FILEPICKER_DIALOG +1;
140 mockArguments();
141
142 return;
143 }
144
145 Intent intent = getIntent();
146 mDialogArguments = intent.getStringExtra("dialogArguments");
147 String dialogTypeString = intent.getStringExtra("dialogType");
148 if (dialogTypeString == null) {
149 return;
150 }
151
152 if (Config.LOGV) {
153 Log.v(TAG, "dialogtype: " + dialogTypeString);
154 }
155
156 if (dialogTypeString.equalsIgnoreCase(SETTINGS_DIALOG_STRING)) {
157 mDialogType = SETTINGS_DIALOG;
158 mGearsVersion = intent.getStringExtra(VERSION_STRING);
159 } else if (dialogTypeString.equalsIgnoreCase(PERMISSION_DIALOG_STRING)) {
160 mDialogType = PERMISSION_DIALOG;
161 } else if (dialogTypeString.equalsIgnoreCase(SHORTCUT_DIALOG_STRING)) {
162 mDialogType = SHORTCUT_DIALOG;
163 } else if (dialogTypeString.equalsIgnoreCase(LOCATION_DIALOG_STRING)) {
164 mDialogType = LOCATION_DIALOG;
165 } else if (dialogTypeString.equalsIgnoreCase(FILEPICKER_DIALOG_STRING)) {
166 mDialogType = FILEPICKER_DIALOG;
167 }
168 }
169
170 /**
171 * Utility method for debugging the dialog.
172 *
173 * Set mock arguments.
174 */
175 private void mockArguments() {
176 String argumentsShortcuts = "{ locale: \"en-US\","
177 + "name: \"My Application\", link: \"http://www.google.com/\","
178 + "description: \"This application does things does things!\","
179 + "icon16x16: \"http://google-gears.googlecode.com/"
180 + "svn/trunk/gears/test/manual/shortcuts/16.png\","
181 + "icon32x32: \"http://google-gears.googlecode.com/"
182 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
183 + "icon48x48: \"http://google-gears.googlecode.com/"
184 + "svn/trunk/gears/test/manual/shortcuts/48.png\","
185 + "icon128x128: \"http://google-gears.googlecode.com/"
186 + "svn/trunk/gears/test/manual/shortcuts/128.png\"}";
187
188 String argumentsPermissions = "{ locale: \"en-US\", "
189 + "origin: \"http://www.google.com\", dialogType: \"localData\","
190 + "customIcon: \"http://google-gears.googlecode.com/"
191 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
192 + "customName: \"My Application\","
193 + "customMessage: \"Press the button to enable my "
194 + "application to run offline!\" };";
195
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800196 String argumentsPermissions2 = "{ locale: \"en-US\", "
197 + "origin: \"http://www.google.com\", dialogType: \"localData\" };";
198
The Android Open Source Projected217d92008-12-17 18:05:52 -0800199 String argumentsLocation = "{ locale: \"en-US\", "
200 + "origin: \"http://www.google.com\", dialogType: \"locationData\","
201 + "customIcon: \"http://google-gears.googlecode.com/"
202 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
203 + "customName: \"My Application\","
204 + "customMessage: \"Press the button to enable my "
205 + "application to run offline!\" };";
206
207 String argumentsSettings = "{ locale: \"en-US\", permissions: [ { "
208 + "name: \"http://www.google.com\", "
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800209 + "localStorage: { permissionState: 0 }, "
210 + "locationData: { permissionState: 1 } }, "
The Android Open Source Projected217d92008-12-17 18:05:52 -0800211 + "{ name: \"http://www.aaronboodman.com\", "
212 + "localStorage: { permissionState: 1 }, "
213 + "locationData: { permissionState: 2 } }, "
214 + "{ name: \"http://www.evil.org\", "
215 + "localStorage: { permissionState: 2 }, "
216 + "locationData: { permissionState: 2 } } ] }";
217
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800218 String argumentsFilePicker = "{ \"cameraMode\" : \"OFF\", \"filters\""
219 + ": [ \"text/html\", \".txt\" ], \"mode\" : \"MULTIPLE_FILES\" }\"";
220
221 String argumentsFilePicker2 = "{ \"cameraMode\" : \"OFF\", \"filters\""
222 + ": [ \"text/html\", \".txt\" ], \"mode\" : \"SINGLE_FILE\" }\"";
223
The Android Open Source Projected217d92008-12-17 18:05:52 -0800224 switch (mDialogType) {
225 case SHORTCUT_DIALOG:
226 mDialogArguments = argumentsShortcuts;
227 break;
228 case PERMISSION_DIALOG:
229 mDialogArguments = argumentsPermissions;
230 break;
231 case LOCATION_DIALOG:
232 mDialogArguments = argumentsLocation;
233 break;
234 case SETTINGS_DIALOG:
235 mDialogArguments = argumentsSettings;
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800236 break;
237 case FILEPICKER_DIALOG:
238 mDialogArguments = argumentsFilePicker2;
The Android Open Source Projected217d92008-12-17 18:05:52 -0800239 }
240 }
241
242 /**
243 * Close the dialog and set the return string value.
244 */
245 private void closeDialog(int closingType) {
246 String ret = dialog.closeDialog(closingType);
247
248 if (mDebug) {
249 Log.v(TAG, "closeDialog ret value: " + ret);
250 }
251
252 NativeDialog.closeDialog(ret);
253 notifyEndOfDialog();
254 finish();
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800255
256 // If the dialog sets a notification, we display it.
257 int notification = dialog.notification();
258 if (notification != 0) {
259 Toast toast = Toast.makeText(this, notification, Toast.LENGTH_LONG);
260 toast.setGravity(Gravity.BOTTOM, 0, 0);
261 toast.show();
262 }
The Android Open Source Projected217d92008-12-17 18:05:52 -0800263 }
264
265 @Override
266 public void onDestroy() {
267 super.onDestroy();
268 // In case we reach this point without
269 // notifying NativeDialog, we do it now.
270 if (!mDialogDismissed) {
271 notifyEndOfDialog();
272 }
273 }
274
275 @Override
276 public void onPause(){
277 super.onPause();
278 if (!mDialogDismissed) {
279 closeDialog(GearsBaseDialog.CANCEL);
280 }
281 }
282
283 /**
284 * Signal to NativeDialog that we are done.
285 */
286 private void notifyEndOfDialog() {
287 NativeDialog.signalFinishedDialog();
288 mDialogDismissed = true;
289 }
290
291 /**
292 * Intercepts the back key to immediately notify
293 * NativeDialog that we are done.
294 */
295 public boolean dispatchKeyEvent(KeyEvent event) {
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800296 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
297 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
298 if (!dialog.handleBackButton()) {
299 // if the dialog doesn't do anything with the back button
300 closeDialog(GearsBaseDialog.CANCEL);
301 }
302 return true; // event consumed
The Android Open Source Projected217d92008-12-17 18:05:52 -0800303 }
304 return super.dispatchKeyEvent(event);
305 }
306
The Android Open Source Projectfbb2a3a2009-02-13 12:57:52 -0800307 /**
308 * If the dialog call showDialog() on ourself, we let
309 * it handle the creation of this secondary dialog.
310 * It is used in GearsSettingsDialog, to create the confirmation
311 * dialog when the user click on "Remove this site from Gears"
312 */
313 @Override
314 protected Dialog onCreateDialog(int id) {
315 return dialog.onCreateDialog(id);
316 }
317
The Android Open Source Projected217d92008-12-17 18:05:52 -0800318}