summaryrefslogtreecommitdiffstats
path: root/src/com/android/browser/PermissionDialog.java
blob: b71261a5bec5d920e0a69594f39b33cac30a68bc (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
/*
 * Copyright (C) 2009 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.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Permission dialog for HTML5
 * @hide
 */
public class PermissionDialog extends Activity {

    private static final String TAG = "PermissionDialog";
    public static final String PARAM_ORIGIN = "origin";
    public static final String PARAM_QUOTA = "quota";

    private String mWebStorageOrigin;
    private long mWebStorageQuota = 0;
    private int mNotification = 0;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        getParameters();
        setupDialog();
    }

    private void getParameters() {
        Intent intent = getIntent();
        mWebStorageOrigin = intent.getStringExtra(PARAM_ORIGIN);
        mWebStorageQuota = intent.getLongExtra(PARAM_QUOTA, 0);
    }

    private void setupDialog() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.permission_dialog);

        setIcon(R.id.icon, android.R.drawable.ic_popup_disk_full);
        setText(R.id.dialog_title, R.string.query_storage_quota_prompt);
        setText(R.id.dialog_message, R.string.query_storage_quota_message);
        setCharSequence(R.id.origin, mWebStorageOrigin);

        setupButton(R.id.button_allow, R.string.permission_button_allow,
            new View.OnClickListener() {
                public void onClick(View v) { allow(); }
            });
        setupButton(R.id.button_alwaysdeny, R.string.permission_button_alwaysdeny,
            new View.OnClickListener() {
                public void onClick(View v) { alwaysdeny(); }
            });
        setupButton(R.id.button_deny, R.string.permission_button_deny,
            new View.OnClickListener() {
                public void onClick(View v) { deny(); }
            });
    }

    private void setText(int viewID, int stringID) {
        setCharSequence(viewID, getString(stringID));
    }

    private void setCharSequence(int viewID, CharSequence string) {
        View view = findViewById(viewID);
        if (view == null) {
            return;
        }
        view.setVisibility(View.VISIBLE);
        TextView textView = (TextView) view;
        textView.setText(string);
    }

    private void setIcon(int viewID, int imageID) {
        View view = findViewById(viewID);
        if (view == null) {
            return;
        }
        view.setVisibility(View.VISIBLE);
        ImageView icon = (ImageView) view;
        icon.setImageResource(imageID);
    }

    private void setupButton(int viewID, int stringID,
                             View.OnClickListener listener) {
        View view = findViewById(viewID);
        if (view == null) {
            return;
        }
        setText(viewID, stringID);
        view.setOnClickListener(listener);
    }

    private void useNextQuota() {
        CharSequence[] values = getResources().getTextArray(
            R.array.webstorage_quota_entries_values);
        for (int i=0; i<values.length; i++) {
            long value = Long.parseLong(values[i].toString());
            value *= (1024 * 1024); // the string array is expressed in MB
            if (value > mWebStorageQuota) {
                mWebStorageQuota = value;
                break;
            }
        }
    }

    private void allow() {
        // If somehow there is no "next quota" in the ladder,
        // we'll add 1MB anyway.
        mWebStorageQuota += 1024*1024;
        useNextQuota();
        mNotification = R.string.webstorage_notification;
        closeDialog();
    }

    private void alwaysdeny() {
        // Setting the quota to 0 will prevent any new data to be
        // added, but the existing data will not be deleted.
        mWebStorageQuota = 0;
        mNotification = R.string.webstorage_notification;
        closeDialog();
    }

    private void deny() {
        closeDialog();
    }

    private void closeDialog() {
        Intent intent = new Intent();
        intent.putExtra(PARAM_QUOTA, mWebStorageQuota);
        setResult(RESULT_OK, intent);
        showToast();
        finish();
    }

    private void showToast() {
        if (mNotification != 0) {
            Toast toast = Toast.makeText(this, mNotification, Toast.LENGTH_LONG);
            toast.setGravity(Gravity.BOTTOM, 0, 0);
            toast.show();
        }
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
              && (event.getAction() == KeyEvent.ACTION_DOWN)) {
            closeDialog();
            return true; // event consumed
        }
        return super.dispatchKeyEvent(event);
    }

}