blob: dead4f247c2c98f9633901a993a4b149bbb8b8e8 [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.Context;
21import android.os.Handler;
22import android.util.Log;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.view.ViewGroup;
26import android.widget.ArrayAdapter;
27import android.widget.Button;
28import android.widget.CompoundButton;
29import android.widget.ImageView;
30import android.widget.ListAdapter;
31import android.widget.ListView;
32import android.widget.RadioButton;
33import android.widget.TextView;
34
35import com.android.browser.GearsPermissions.OriginPermissions;
36import com.android.browser.GearsPermissions.PermissionsChangesListener;
37import com.android.browser.GearsPermissions.PermissionType;
38
39import java.util.Vector;
40import java.util.List;
41
42import org.json.JSONArray;
43import org.json.JSONException;
44import org.json.JSONObject;
45
46/**
47 * Gears Settings dialog
48 */
49class GearsSettingsDialog extends GearsBaseDialog
50 implements PermissionsChangesListener {
51
52 private static final String TAG = "GearsPermissionsDialog";
53 private Vector<OriginPermissions> mSitesPermissions = null;
54 private Vector<OriginPermissions> mOriginalPermissions = null;
55 private Vector<OriginPermissions> mCurrentPermissions = null;
56
57 private Vector<PermissionType> mPermissions;
58
59 // We declare the permissions globally to simplify the code
60 private final PermissionType LOCAL_STORAGE =
61 new PermissionType(LOCAL_STORAGE_STRING);
62 private final PermissionType LOCATION_DATA =
63 new PermissionType(LOCATION_DATA_STRING);
64
65 private boolean mChanges = false;
66
67
68 public GearsSettingsDialog(Activity activity,
69 Handler handler,
70 String arguments) {
71 super (activity, handler, arguments);
72 }
73
74 public void setup() {
75 // First let's add the permissions' resources
76 LOCAL_STORAGE.setResources(R.id.local_storage_choice,
77 R.id.local_storage_allowed,
78 R.id.local_storage_denied);
79
80 LOCATION_DATA.setResources(R.id.location_data_choice,
81 R.id.location_data_allowed,
82 R.id.location_data_denied);
83
84 // add the permissions to the list of permissions.
85 mPermissions = new Vector<PermissionType>();
86 mPermissions.add(LOCAL_STORAGE);
87 mPermissions.add(LOCATION_DATA);
88 OriginPermissions.setListener(this);
89
90
91 inflate(R.layout.gears_dialog_settings, R.id.panel_content);
92 setupDialog();
93 setupButtons(0,
94 R.string.settings_button_allow,
95 R.string.settings_button_deny);
96
97 // by default disable the allow button (it will get enabled if
98 // something is changed by the user)
99 View buttonView = findViewById(R.id.button_allow);
100 if (buttonView != null) {
101 Button button = (Button) buttonView;
102 button.setEnabled(false);
103 }
104
105 View gearsVersionView = findViewById(R.id.gears_version);
106 if (gearsVersionView != null) {
107 TextView gearsVersion = (TextView) gearsVersionView;
108 gearsVersion.setText(mGearsVersion);
109 }
110
111 // We manage the permissions using three vectors, mSitesPermissions,
112 // mOriginalPermissions and mCurrentPermissions.
113 // The dialog's arguments are parsed and a list of permissions is
114 // generated and stored in those three vectors.
115 // mOriginalPermissions is a separate copy and will not be modified;
116 // mSitesPermissions contains the current permissions _only_ --
117 // if an origin is removed, it is also removed from mSitesPermissions.
118 // Finally, mCurrentPermissions contains the current permissions and
119 // is a clone of mSitesPermissions, but removed sites aren't removed,
120 // their permissions are simply set to PERMISSION_NOT_SET. This
121 // allows us to easily generate the final difference between the
122 // original permissions and the final permissions, while directly
123 // using mSitesPermissions for the listView adapter (SettingsAdapter).
124
125 mSitesPermissions = new Vector<OriginPermissions>();
126 mOriginalPermissions = new Vector<OriginPermissions>();
127
128 try {
129 JSONObject json = new JSONObject(mDialogArguments);
130 if (json.has("permissions")) {
131 JSONArray jsonArray = json.getJSONArray("permissions");
132 for (int i = 0; i < jsonArray.length(); i++) {
133 JSONObject infos = jsonArray.getJSONObject(i);
134 String name = null;
135 int localStorage = PermissionType.PERMISSION_NOT_SET;
136 int locationData = PermissionType.PERMISSION_NOT_SET;
137 if (infos.has("name")) {
138 name = infos.getString("name");
139 }
140 if (infos.has(LOCAL_STORAGE_STRING)) {
141 JSONObject perm = infos.getJSONObject(LOCAL_STORAGE_STRING);
142 if (perm.has("permissionState")) {
143 localStorage = perm.getInt("permissionState");
144 }
145 }
146 if (infos.has(LOCATION_DATA_STRING)) {
147 JSONObject perm = infos.getJSONObject(LOCATION_DATA_STRING);
148 if (perm.has("permissionState")) {
149 locationData = perm.getInt("permissionState");
150 }
151 }
152 OriginPermissions perms = new OriginPermissions(name);
153 perms.setPermission(LOCAL_STORAGE, localStorage);
154 perms.setPermission(LOCATION_DATA, locationData);
155
156 mSitesPermissions.add(perms);
157 mOriginalPermissions.add(new OriginPermissions(perms));
158 }
159 }
160 } catch (JSONException e) {
161 Log.e(TAG, "JSON exception ", e);
162 }
163 mCurrentPermissions = (Vector<OriginPermissions>)mSitesPermissions.clone();
164
165 View listView = findViewById(R.id.sites_list);
166 if (listView != null) {
167 ListView list = (ListView) listView;
168 list.setAdapter(new SettingsAdapter(mActivity, mSitesPermissions));
169 }
170 if (mDebug) {
171 printPermissions();
172 }
173 }
174
175 public void setupDialog() {
176 View dialogTitleView = findViewById(R.id.dialog_title);
177 if (dialogTitleView != null) {
178 TextView dialogTitle = (TextView) dialogTitleView;
179 dialogTitle.setText(R.string.settings_title);
180 dialogTitle.setVisibility(View.VISIBLE);
181 }
182 View dialogSubtitleView = findViewById(R.id.dialog_subtitle);
183 if (dialogSubtitleView != null) {
184 TextView dialogSubtitle = (TextView) dialogSubtitleView;
185 dialogSubtitle.setText(R.string.settings_message);
186 dialogSubtitle.setVisibility(View.VISIBLE);
187 }
188 View iconView = findViewById(R.id.icon);
189 if (iconView != null) {
190 ImageView icon = (ImageView) iconView;
191 icon.setImageResource(R.drawable.gears_icon_32x32);
192 }
193 }
194
195 /**
196 * GearsPermissions.PermissionsChangesListener delegate
197 */
198 public boolean setPermission(PermissionType type, int perm) {
199 if (mChanges == false) {
200 signalChanges();
201 }
202 return mChanges;
203 }
204
205 /**
206 * Controller class for binding the model (OriginPermissions) with
207 * the UI.
208 */
209 class PermissionController {
210 final static int ALLOWED_BUTTON = 1;
211 final static int DENIED_BUTTON = 2;
212 private int mButtonType;
213 private PermissionType mPermissionType;
214 private OriginPermissions mPermissions;
215
216 PermissionController(PermissionType permissionType, int buttonType,
217 OriginPermissions permissions) {
218 mPermissionType = permissionType;
219 mButtonType = buttonType;
220 mPermissions = permissions;
221 }
222
223 public boolean isChecked() {
224 boolean checked = false;
225
226 switch (mButtonType) {
227 case ALLOWED_BUTTON:
228 if (mPermissions.getPermission(mPermissionType) ==
229 PermissionType.PERMISSION_ALLOWED) {
230 checked = true;
231 } break;
232 case DENIED_BUTTON:
233 if (mPermissions.getPermission(mPermissionType) ==
234 PermissionType.PERMISSION_DENIED) {
235 checked = true;
236 }
237 }
238 return checked;
239 }
240
241 public String print() {
242 return printType() + " for " + mPermissions.getOrigin();
243 }
244
245 private String printType() {
246 switch (mButtonType) {
247 case ALLOWED_BUTTON:
248 return "ALLOWED_BUTTON";
249 case DENIED_BUTTON:
250 return "DENIED_BUTTON";
251 }
252 return "UNKNOWN BUTTON";
253 }
254
255 public void changed(boolean isChecked) {
256 if (isChecked == isChecked()) {
257 return; // already set
258 }
259
260 switch (mButtonType) {
261 case ALLOWED_BUTTON:
262 mPermissions.setPermission(mPermissionType,
263 PermissionType.PERMISSION_ALLOWED);
264 break;
265 case DENIED_BUTTON:
266 mPermissions.setPermission(mPermissionType,
267 PermissionType.PERMISSION_DENIED);
268 break;
269 }
270 }
271 }
272
273
274
275 /**
276 * Adapter class for the list view in the settings dialog
277 *
278 * Every row in the settings dialog display the permissions
279 * for a given origin. For every type of permission
280 * (location, local data...) there is two radio buttons to
281 * authorize or deny the permission.
282 * A remove button is also present to let the user remove
283 * all the authorization of an origin in one step.
284 */
285 class SettingsAdapter extends ArrayAdapter {
286 private Activity mContext;
287 private List mItems;
288
289 SettingsAdapter(Activity context, List items) {
290 super(context, R.layout.gears_dialog_settings_row, items);
291 mContext = context;
292 mItems = items;
293 }
294
295 /*
296 * setup the necessary listeners for the radiobuttons
297 * When the buttons are clicked the permissions change.
298 */
299 private void createAndSetButtonListener(View buttonView,
300 OriginPermissions perms, PermissionType permissionType,
301 int buttonType) {
302 if (buttonView == null) {
303 return;
304 }
305 RadioButton button = (RadioButton) buttonView;
306
307 button.setOnCheckedChangeListener(null);
308 PermissionController p = new PermissionController(permissionType,
309 buttonType, perms);
310 button.setTag(p);
311
312 CompoundButton.OnCheckedChangeListener listener =
313 new CompoundButton.OnCheckedChangeListener() {
314 public void onCheckedChanged(CompoundButton buttonView,
315 boolean isChecked) {
316 PermissionController perm = (PermissionController)buttonView.getTag();
317 perm.changed(isChecked);
318 }
319 };
320
321 button.setOnCheckedChangeListener(listener);
322
323 if (p.isChecked() != button.isChecked()) {
324 button.setChecked(p.isChecked());
325 }
326 }
327
328 /*
329 * setup the remove button for an origin: each row has a global
330 * remove button in addition to the radio buttons controlling the
331 * permissions.
332 */
333 private void setRemoveButton(Button button, OriginPermissions perms) {
334 Button.OnClickListener listener = new Button.OnClickListener() {
335 public void onClick(View buttonView) {
336 if (mChanges == false) {
337 signalChanges();
338 }
339 OriginPermissions perm = (OriginPermissions) buttonView.getTag();
340 perm.setPermission(LOCAL_STORAGE, PermissionType.PERMISSION_NOT_SET);
341 perm.setPermission(LOCATION_DATA, PermissionType.PERMISSION_NOT_SET);
342 mSitesPermissions.remove(perm);
343
344 View view = findViewById(R.id.sites_list);
345 if (view != null) {
346 ListView listView = (ListView) view;
347 ListAdapter listAdapter = listView.getAdapter();
348 if (listAdapter != null) {
349 SettingsAdapter settingsAdapter = (SettingsAdapter) listAdapter;
350 settingsAdapter.notifyDataSetChanged();
351 }
352 }
353 }
354 };
355 button.setTag(perms);
356 button.setOnClickListener(listener);
357 }
358
359 public View getView(int position, View convertView, ViewGroup parent) {
360 View row = convertView;
361 if (row == null) { // no cached view, we create one
362 LayoutInflater inflater = (LayoutInflater) getSystemService(
363 Context.LAYOUT_INFLATER_SERVICE);
364 row = inflater.inflate(R.layout.gears_dialog_settings_row, null);
365 }
366
367 OriginPermissions perms = (OriginPermissions) mItems.get(position);
368
369 View nameView = row.findViewById(R.id.origin_name);
370 if (nameView != null) {
371 TextView originName = (TextView) nameView;
372 originName.setText(perms.getOrigin());
373 }
374
375 View removeButtonView = row.findViewById(R.id.origin_remove);
376 if (removeButtonView != null) {
377 Button removeButton = (Button) removeButtonView;
378 setRemoveButton(removeButton, perms);
379 }
380
381 for (int i = 0; i < mPermissions.size(); i++) {
382 PermissionType type = mPermissions.get(i);
383 int rowRsc = type.getRowRsc();
384 int allowedButtonRsc = type.getAllowedButtonRsc();
385 int deniedButtonRsc = type.getDeniedButtonRsc();
386
387 View rowView = row.findViewById(rowRsc);
388 if (rowView != null) {
389 int perm = perms.getPermission(type);
390 if (perm != PermissionType.PERMISSION_NOT_SET) {
391 createAndSetButtonListener(row.findViewById(allowedButtonRsc),
392 perms, type, PermissionController.ALLOWED_BUTTON);
393 createAndSetButtonListener(row.findViewById(deniedButtonRsc),
394 perms, type, PermissionController.DENIED_BUTTON);
395 rowView.setVisibility(View.VISIBLE);
396 } else {
397 rowView.setVisibility(View.GONE);
398 }
399 }
400 }
401
402 return row;
403 }
404 }
405
406 /**
407 * Utility method used in debug mode to print the list of
408 * permissions (original values and current values).
409 */
410 public void printPermissions() {
411 Log.v(TAG, "Original Permissions: ");
412 for (int i = 0; i < mOriginalPermissions.size(); i++) {
413 OriginPermissions p = mOriginalPermissions.get(i);
414 p.print();
415 }
416 Log.v(TAG, "Current Permissions: ");
417 for (int i = 0; i < mSitesPermissions.size(); i++) {
418 OriginPermissions p = mSitesPermissions.get(i);
419 p.print();
420 }
421 }
422
423 /**
424 * Utility method used by the settings dialog, signaling
425 * the user the settings have been modified.
426 * We reflect this by enabling the Allow button (disabled
427 * by default).
428 */
429 public void signalChanges() {
430 View view = findViewById(R.id.button_allow);
431 if (view != null) {
432 Button button = (Button) view;
433 button.setEnabled(true);
434 }
435 mChanges = true;
436 }
437
438 /**
439 * Computes the difference between the original permissions and the
440 * current ones. Returns a json-formatted string.
441 * It is used by the Settings dialog.
442 */
443 public String computeDiff(boolean modif) {
444 String ret = null;
445 try {
446 JSONObject results = new JSONObject();
447 JSONArray permissions = new JSONArray();
448
449 for (int i = 0; modif && i < mOriginalPermissions.size(); i++) {
450 OriginPermissions original = mOriginalPermissions.get(i);
451 OriginPermissions current = mCurrentPermissions.get(i);
452 JSONObject permission = new JSONObject();
453 boolean modifications = false;
454
455 for (int j = 0; j < mPermissions.size(); j++) {
456 PermissionType type = mPermissions.get(j);
457
458 if (current.getPermission(type) != original.getPermission(type)) {
459 JSONObject state = new JSONObject();
460 state.put("permissionState", current.getPermission(type));
461 permission.put(type.getName(), state);
462 modifications = true;
463 }
464 }
465
466 if (modifications) {
467 permission.put("name", current.getOrigin());
468 permissions.put(permission);
469 }
470 }
471 results.put("modifiedOrigins", permissions);
472 ret = results.toString();
473 } catch (JSONException e) {
474 Log.e(TAG, "JSON exception ", e);
475 }
476 return ret;
477 }
478
479 public String closeDialog(int closingType) {
480 String ret = null;
481 switch (closingType) {
482 case ALWAYS_DENY:
483 ret = "{\"allow\": false }";
484 break;
485 case ALLOW:
486 ret = computeDiff(true);
487 break;
488 case DENY:
489 ret = computeDiff(false);
490 break;
491 }
492
493 if (mDebug) {
494 printPermissions();
495 }
496
497 return ret;
498 }
499
500}