blob: c8ae741813a8dce5b35865826ffdb8acea9d6372 [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;
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.os.Handler;
24import android.os.Message;
25import android.util.Config;
26import android.util.Log;
27import android.view.KeyEvent;
28import android.view.Window;
29import android.widget.BaseAdapter;
30
31import android.webkit.gears.NativeDialog;
32
33import com.android.browser.GearsBaseDialog;
34import com.android.browser.GearsPermissionsDialog;
35import com.android.browser.GearsSettingsDialog;
36import com.android.browser.GearsShortcutDialog;
37import com.android.browser.GearsFilePickerDialog;
38
39/**
40 * Native dialog Activity used by gears
41 * TODO: rename in GearsNativeDialogActivity
42 * @hide
43 */
44public class GearsNativeDialog extends Activity {
45
46 private static final String TAG = "GearsNativeDialog";
47
48 private String mDialogArguments;
49
50 private String mGearsVersion = null;
51
52 private boolean mDebug = false;
53
54 private int mDialogType;
55 private final int SETTINGS_DIALOG = 1;
56 private final int PERMISSION_DIALOG = 2;
57 private final int SHORTCUT_DIALOG = 3;
58 private final int LOCATION_DIALOG = 4;
59 private final int FILEPICKER_DIALOG = 5;
60
61 private final String VERSION_STRING = "version";
62 private final String SETTINGS_DIALOG_STRING = "settings_dialog";
63 private final String PERMISSION_DIALOG_STRING = "permissions_dialog";
64 private final String SHORTCUT_DIALOG_STRING = "shortcuts_dialog";
65 private final String LOCATION_DIALOG_STRING = "locations_dialog";
66 private final String FILEPICKER_DIALOG_STRING = "filepicker_dialog";
67
68 private boolean mDialogDismissed = false;
69
70 GearsBaseDialog dialog;
71
72 // Handler for callbacks to the UI thread
73 final Handler mHandler = new Handler() {
74 public void handleMessage(Message msg) {
75 if (msg.what == GearsBaseDialog.NEW_ICON) {
76 BaseAdapter adapter = (BaseAdapter) msg.obj;
77 adapter.notifyDataSetChanged();
78 } else if (msg.what == GearsBaseDialog.UPDATE_ICON) {
79 dialog.updateIcon();
80 } else if (msg.what == GearsBaseDialog.ALWAYS_DENY) {
81 closeDialog(GearsBaseDialog.ALWAYS_DENY);
82 } else if (msg.what == GearsBaseDialog.ALLOW) {
83 closeDialog(GearsBaseDialog.ALLOW);
84 } else if (msg.what == GearsBaseDialog.DENY) {
85 closeDialog(GearsBaseDialog.DENY);
86 }
87 super.handleMessage(msg);
88 }
89 };
90
91 @Override
92 public void onCreate(Bundle icicle) {
93 super.onCreate(icicle);
94 requestWindowFeature(Window.FEATURE_NO_TITLE);
95 setContentView(R.layout.gears_dialog);
96 getArguments();
97
98 switch (mDialogType) {
99 case SETTINGS_DIALOG:
100 dialog = new GearsSettingsDialog(this, mHandler, mDialogArguments);
101 dialog.setGearsVersion(mGearsVersion);
102 break;
103 case PERMISSION_DIALOG:
104 dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
105 break;
106 case SHORTCUT_DIALOG:
107 dialog = new GearsShortcutDialog(this, mHandler, mDialogArguments);
108 break;
109 case LOCATION_DIALOG:
110 dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
111 break;
112 case FILEPICKER_DIALOG:
113 dialog = new GearsFilePickerDialog(this, mHandler, mDialogArguments);
114 break;
115 default:
116 dialog = new GearsBaseDialog(this, mHandler, mDialogArguments);
117 }
118 dialog.setDebug(mDebug);
119 dialog.setup();
120 }
121
122 /**
123 * Get the arguments for the dialog
124 *
125 * The dialog needs a json string as an argument, as
126 * well as a dialogType. In debug mode the arguments
127 * are mocked.
128 */
129 private void getArguments() {
130 if (mDebug) {
131 mDialogType = FILEPICKER_DIALOG +1;
132 mockArguments();
133
134 return;
135 }
136
137 Intent intent = getIntent();
138 mDialogArguments = intent.getStringExtra("dialogArguments");
139 String dialogTypeString = intent.getStringExtra("dialogType");
140 if (dialogTypeString == null) {
141 return;
142 }
143
144 if (Config.LOGV) {
145 Log.v(TAG, "dialogtype: " + dialogTypeString);
146 }
147
148 if (dialogTypeString.equalsIgnoreCase(SETTINGS_DIALOG_STRING)) {
149 mDialogType = SETTINGS_DIALOG;
150 mGearsVersion = intent.getStringExtra(VERSION_STRING);
151 } else if (dialogTypeString.equalsIgnoreCase(PERMISSION_DIALOG_STRING)) {
152 mDialogType = PERMISSION_DIALOG;
153 } else if (dialogTypeString.equalsIgnoreCase(SHORTCUT_DIALOG_STRING)) {
154 mDialogType = SHORTCUT_DIALOG;
155 } else if (dialogTypeString.equalsIgnoreCase(LOCATION_DIALOG_STRING)) {
156 mDialogType = LOCATION_DIALOG;
157 } else if (dialogTypeString.equalsIgnoreCase(FILEPICKER_DIALOG_STRING)) {
158 mDialogType = FILEPICKER_DIALOG;
159 }
160 }
161
162 /**
163 * Utility method for debugging the dialog.
164 *
165 * Set mock arguments.
166 */
167 private void mockArguments() {
168 String argumentsShortcuts = "{ locale: \"en-US\","
169 + "name: \"My Application\", link: \"http://www.google.com/\","
170 + "description: \"This application does things does things!\","
171 + "icon16x16: \"http://google-gears.googlecode.com/"
172 + "svn/trunk/gears/test/manual/shortcuts/16.png\","
173 + "icon32x32: \"http://google-gears.googlecode.com/"
174 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
175 + "icon48x48: \"http://google-gears.googlecode.com/"
176 + "svn/trunk/gears/test/manual/shortcuts/48.png\","
177 + "icon128x128: \"http://google-gears.googlecode.com/"
178 + "svn/trunk/gears/test/manual/shortcuts/128.png\"}";
179
180 String argumentsPermissions = "{ locale: \"en-US\", "
181 + "origin: \"http://www.google.com\", dialogType: \"localData\","
182 + "customIcon: \"http://google-gears.googlecode.com/"
183 + "svn/trunk/gears/test/manual/shortcuts/32.png\","
184 + "customName: \"My Application\","
185 + "customMessage: \"Press the button to enable my "
186 + "application to run offline!\" };";
187
188 String argumentsLocation = "{ locale: \"en-US\", "
189 + "origin: \"http://www.google.com\", dialogType: \"locationData\","
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
196 String argumentsSettings = "{ locale: \"en-US\", permissions: [ { "
197 + "name: \"http://www.google.com\", "
198 + "localStorage: { permissionState: 1 }, "
199 + "locationData: { permissionState: 0 } }, "
200 + "{ name: \"http://www.aaronboodman.com\", "
201 + "localStorage: { permissionState: 1 }, "
202 + "locationData: { permissionState: 2 } }, "
203 + "{ name: \"http://www.evil.org\", "
204 + "localStorage: { permissionState: 2 }, "
205 + "locationData: { permissionState: 2 } } ] }";
206
207 switch (mDialogType) {
208 case SHORTCUT_DIALOG:
209 mDialogArguments = argumentsShortcuts;
210 break;
211 case PERMISSION_DIALOG:
212 mDialogArguments = argumentsPermissions;
213 break;
214 case LOCATION_DIALOG:
215 mDialogArguments = argumentsLocation;
216 break;
217 case SETTINGS_DIALOG:
218 mDialogArguments = argumentsSettings;
219 }
220 }
221
222 /**
223 * Close the dialog and set the return string value.
224 */
225 private void closeDialog(int closingType) {
226 String ret = dialog.closeDialog(closingType);
227
228 if (mDebug) {
229 Log.v(TAG, "closeDialog ret value: " + ret);
230 }
231
232 NativeDialog.closeDialog(ret);
233 notifyEndOfDialog();
234 finish();
235 }
236
237 @Override
238 public void onDestroy() {
239 super.onDestroy();
240 // In case we reach this point without
241 // notifying NativeDialog, we do it now.
242 if (!mDialogDismissed) {
243 notifyEndOfDialog();
244 }
245 }
246
247 @Override
248 public void onPause(){
249 super.onPause();
250 if (!mDialogDismissed) {
251 closeDialog(GearsBaseDialog.CANCEL);
252 }
253 }
254
255 /**
256 * Signal to NativeDialog that we are done.
257 */
258 private void notifyEndOfDialog() {
259 NativeDialog.signalFinishedDialog();
260 mDialogDismissed = true;
261 }
262
263 /**
264 * Intercepts the back key to immediately notify
265 * NativeDialog that we are done.
266 */
267 public boolean dispatchKeyEvent(KeyEvent event) {
268 if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isDown()) {
269 closeDialog(GearsBaseDialog.CANCEL);
270 }
271 return super.dispatchKeyEvent(event);
272 }
273
274}