blob: b71261a5bec5d920e0a69594f39b33cac30a68bc [file] [log] [blame]
Nicolas Roard78a98e42009-05-11 13:34:17 +01001/*
2 * Copyright (C) 2009 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.app.Dialog;
21import android.content.Intent;
22import android.os.Bundle;
23import android.util.Log;
24import android.view.Gravity;
25import android.view.KeyEvent;
26import android.view.View;
27import android.view.Window;
28import android.widget.ImageView;
29import android.widget.TextView;
30import android.widget.Toast;
31
32/**
33 * Permission dialog for HTML5
34 * @hide
35 */
36public class PermissionDialog extends Activity {
37
38 private static final String TAG = "PermissionDialog";
39 public static final String PARAM_ORIGIN = "origin";
40 public static final String PARAM_QUOTA = "quota";
41
42 private String mWebStorageOrigin;
43 private long mWebStorageQuota = 0;
44 private int mNotification = 0;
45
46 @Override
47 public void onCreate(Bundle icicle) {
48 super.onCreate(icicle);
49 getParameters();
50 setupDialog();
51 }
52
53 private void getParameters() {
54 Intent intent = getIntent();
55 mWebStorageOrigin = intent.getStringExtra(PARAM_ORIGIN);
56 mWebStorageQuota = intent.getLongExtra(PARAM_QUOTA, 0);
57 }
58
59 private void setupDialog() {
60 requestWindowFeature(Window.FEATURE_NO_TITLE);
61 setContentView(R.layout.permission_dialog);
62
63 setIcon(R.id.icon, android.R.drawable.ic_popup_disk_full);
64 setText(R.id.dialog_title, R.string.query_storage_quota_prompt);
65 setText(R.id.dialog_message, R.string.query_storage_quota_message);
66 setCharSequence(R.id.origin, mWebStorageOrigin);
67
68 setupButton(R.id.button_allow, R.string.permission_button_allow,
69 new View.OnClickListener() {
70 public void onClick(View v) { allow(); }
71 });
72 setupButton(R.id.button_alwaysdeny, R.string.permission_button_alwaysdeny,
73 new View.OnClickListener() {
74 public void onClick(View v) { alwaysdeny(); }
75 });
76 setupButton(R.id.button_deny, R.string.permission_button_deny,
77 new View.OnClickListener() {
78 public void onClick(View v) { deny(); }
79 });
80 }
81
82 private void setText(int viewID, int stringID) {
83 setCharSequence(viewID, getString(stringID));
84 }
85
86 private void setCharSequence(int viewID, CharSequence string) {
87 View view = findViewById(viewID);
88 if (view == null) {
89 return;
90 }
91 view.setVisibility(View.VISIBLE);
92 TextView textView = (TextView) view;
93 textView.setText(string);
94 }
95
96 private void setIcon(int viewID, int imageID) {
97 View view = findViewById(viewID);
98 if (view == null) {
99 return;
100 }
101 view.setVisibility(View.VISIBLE);
102 ImageView icon = (ImageView) view;
103 icon.setImageResource(imageID);
104 }
105
106 private void setupButton(int viewID, int stringID,
107 View.OnClickListener listener) {
108 View view = findViewById(viewID);
109 if (view == null) {
110 return;
111 }
112 setText(viewID, stringID);
113 view.setOnClickListener(listener);
114 }
115
116 private void useNextQuota() {
117 CharSequence[] values = getResources().getTextArray(
118 R.array.webstorage_quota_entries_values);
119 for (int i=0; i<values.length; i++) {
120 long value = Long.parseLong(values[i].toString());
121 value *= (1024 * 1024); // the string array is expressed in MB
122 if (value > mWebStorageQuota) {
123 mWebStorageQuota = value;
124 break;
125 }
126 }
127 }
128
129 private void allow() {
130 // If somehow there is no "next quota" in the ladder,
131 // we'll add 1MB anyway.
132 mWebStorageQuota += 1024*1024;
133 useNextQuota();
134 mNotification = R.string.webstorage_notification;
135 closeDialog();
136 }
137
138 private void alwaysdeny() {
139 // Setting the quota to 0 will prevent any new data to be
140 // added, but the existing data will not be deleted.
141 mWebStorageQuota = 0;
142 mNotification = R.string.webstorage_notification;
143 closeDialog();
144 }
145
146 private void deny() {
147 closeDialog();
148 }
149
150 private void closeDialog() {
151 Intent intent = new Intent();
152 intent.putExtra(PARAM_QUOTA, mWebStorageQuota);
153 setResult(RESULT_OK, intent);
154 showToast();
155 finish();
156 }
157
158 private void showToast() {
159 if (mNotification != 0) {
160 Toast toast = Toast.makeText(this, mNotification, Toast.LENGTH_LONG);
161 toast.setGravity(Gravity.BOTTOM, 0, 0);
162 toast.show();
163 }
164 }
165
166 public boolean dispatchKeyEvent(KeyEvent event) {
167 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
168 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
169 closeDialog();
170 return true; // event consumed
171 }
172 return super.dispatchKeyEvent(event);
173 }
174
175}