summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/GearsNativeDialog.java
blob: b44ec2a885c6cf8319deffbee44a3046479f9cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.browser;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.Toast;

import android.webkit.gears.NativeDialog;

import com.android.browser.GearsBaseDialog;
import com.android.browser.GearsPermissionsDialog;
import com.android.browser.GearsSettingsDialog;

/**
 * Native dialog Activity used by gears
 * TODO: rename in GearsNativeDialogActivity
 * @hide
 */
public class GearsNativeDialog extends Activity {

  private static final String TAG = "GearsNativeDialog";

  private String mDialogArguments;

  private String mGearsVersion = null;

  private boolean mDebug = false;

  private int mDialogType;
  private final int SETTINGS_DIALOG = 1;
  private final int PERMISSION_DIALOG = 2;
  private final int LOCATION_DIALOG = 3;

  private final String VERSION_STRING = "version";
  private final String SETTINGS_DIALOG_STRING = "settings_dialog";
  private final String PERMISSION_DIALOG_STRING = "permissions_dialog";
  private final String LOCATION_DIALOG_STRING = "locations_dialog";

  private boolean mDialogDismissed = false;

  GearsBaseDialog dialog;

  // Handler for callbacks to the UI thread
  final Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
      if (msg.what == GearsBaseDialog.NEW_ICON) {
        BaseAdapter adapter = (BaseAdapter) msg.obj;
        adapter.notifyDataSetChanged();
      } else if (msg.what == GearsBaseDialog.UPDATE_ICON) {
        dialog.updateIcon();
      } else if (msg.what == GearsBaseDialog.ALWAYS_DENY) {
        closeDialog(GearsBaseDialog.ALWAYS_DENY);
      } else if (msg.what == GearsBaseDialog.ALLOW) {
        closeDialog(GearsBaseDialog.ALLOW);
      } else if (msg.what == GearsBaseDialog.DENY) {
        closeDialog(GearsBaseDialog.DENY);
      }
      super.handleMessage(msg);
    }
  };

  @Override
  public void onCreate(Bundle icicle) {
    getArguments();
    if (mDialogType == SETTINGS_DIALOG) {
      setTheme(android.R.style.Theme);
    }
    super.onCreate(icicle);
    if (mDialogType != SETTINGS_DIALOG) {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      setContentView(R.layout.gears_dialog);
    }

    switch (mDialogType) {
      case SETTINGS_DIALOG:
        dialog = new GearsSettingsDialog(this, mHandler, mDialogArguments);
        dialog.setGearsVersion(mGearsVersion);
        break;
      case PERMISSION_DIALOG:
        dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
        break;
      case LOCATION_DIALOG:
        dialog = new GearsPermissionsDialog(this, mHandler, mDialogArguments);
        break;
      default:
        dialog = new GearsBaseDialog(this, mHandler, mDialogArguments);
    }
    dialog.setDebug(mDebug);
    dialog.setup();
  }

  /**
   * Get the arguments for the dialog
   *
   * The dialog needs a json string as an argument, as
   * well as a dialogType. In debug mode the arguments
   * are mocked.
   */
  private void getArguments() {
    if (mDebug) {
      mDialogType = LOCATION_DIALOG +1;
      mockArguments();

      return;
    }

    Intent intent = getIntent();
    mDialogArguments = intent.getStringExtra("dialogArguments");
    String dialogTypeString = intent.getStringExtra("dialogType");
    if (dialogTypeString == null) {
      return;
    }

    if (Browser.LOGV_ENABLED) {
      Log.v(TAG, "dialogtype: " + dialogTypeString);
    }

    if (dialogTypeString.equalsIgnoreCase(SETTINGS_DIALOG_STRING)) {
      mDialogType = SETTINGS_DIALOG;
      mGearsVersion = intent.getStringExtra(VERSION_STRING);
    } else if (dialogTypeString.equalsIgnoreCase(PERMISSION_DIALOG_STRING)) {
      mDialogType = PERMISSION_DIALOG;
    } else if (dialogTypeString.equalsIgnoreCase(LOCATION_DIALOG_STRING)) {
      mDialogType = LOCATION_DIALOG;
    }
  }

  /**
   * Utility method for debugging the dialog.
   *
   * Set mock arguments.
   */
  private void mockArguments() {

    String argumentsPermissions = "{ locale: \"en-US\", "
        + "origin: \"http://www.google.com\", dialogType: \"localData\","
        + "customIcon: \"http://google-gears.googlecode.com/"
        + "svn/trunk/gears/test/manual/shortcuts/32.png\","
        + "customName: \"My Application\","
        + "customMessage: \"Press the button to enable my "
        + "application to run offline!\" };";

    String argumentsPermissions2 = "{ locale: \"en-US\", "
        + "origin: \"http://www.google.com\", dialogType: \"localData\" };";

    String argumentsLocation = "{ locale: \"en-US\", "
        + "origin: \"http://www.google.com\", dialogType: \"locationData\","
        + "customIcon: \"http://google-gears.googlecode.com/"
        + "svn/trunk/gears/test/manual/shortcuts/32.png\","
        + "customName: \"My Application\","
        + "customMessage: \"Press the button to enable my "
        + "application to run offline!\" };";

    String argumentsSettings = "{ locale: \"en-US\", permissions: [ { "
        + "name: \"http://www.google.com\", "
        + "localStorage: { permissionState: 0 }, "
        + "locationData: { permissionState: 1 } }, "
        + "{ name: \"http://www.aaronboodman.com\", "
        + "localStorage: { permissionState: 1 }, "
        + "locationData: { permissionState: 2 } }, "
        + "{ name: \"http://www.evil.org\", "
        + "localStorage: { permissionState: 2 }, "
        + "locationData: { permissionState: 2 } } ] }";

    switch (mDialogType) {
      case PERMISSION_DIALOG:
        mDialogArguments = argumentsPermissions;
        break;
      case LOCATION_DIALOG:
        mDialogArguments = argumentsLocation;
        break;
      case SETTINGS_DIALOG:
        mDialogArguments = argumentsSettings;
        break;
    }
  }

  /**
   * Close the dialog and set the return string value.
   */
  private void closeDialog(int closingType) {
    String ret = dialog.closeDialog(closingType);

    if (mDebug) {
      Log.v(TAG, "closeDialog ret value: " + ret);
    }

    NativeDialog.closeDialog(ret);
    notifyEndOfDialog();
    finish();

    // If the dialog sets a notification, we display it.
    int notification = dialog.notification();
    if (notification != 0) {
      Toast toast = Toast.makeText(this, notification, Toast.LENGTH_LONG);
      toast.setGravity(Gravity.BOTTOM, 0, 0);
      toast.show();
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    // In case we reach this point without
    // notifying NativeDialog, we do it now.
    if (!mDialogDismissed) {
      notifyEndOfDialog();
    }
  }

  @Override
  public void onPause(){
    super.onPause();
    if (!mDialogDismissed) {
      closeDialog(GearsBaseDialog.CANCEL);
    }
  }

  /**
   * Signal to NativeDialog that we are done.
   */
  private void notifyEndOfDialog() {
    NativeDialog.signalFinishedDialog();
    mDialogDismissed = true;
  }

  /**
   * Intercepts the back key to immediately notify
   * NativeDialog that we are done.
   */
  public boolean dispatchKeyEvent(KeyEvent event) {
    if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
      && (event.getAction() == KeyEvent.ACTION_DOWN)) {
      if (!dialog.handleBackButton()) {
        // if the dialog doesn't do anything with the back button
        closeDialog(GearsBaseDialog.CANCEL);
      }
      return true; // event consumed
    }
    return super.dispatchKeyEvent(event);
  }

  /**
   * If the dialog call showDialog() on ourself, we let
   * it handle the creation of this secondary dialog.
   * It is used in GearsSettingsDialog, to create the confirmation
   * dialog when the user click on "Remove this site from Gears"
   */
  @Override
  protected Dialog onCreateDialog(int id) {
    return dialog.onCreateDialog(id);
  }

}