blob: e635bb26870266bb2ca5b129edebb534db64e44a [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.preference.ListPreference;
20import android.content.Context;
21import android.preference.PreferenceScreen;
22import android.util.AttributeSet;
23import android.util.Log;
24import android.view.View;
25import android.webkit.WebStorage;
26
Nicolas Roard6f480422009-05-12 16:31:33 +010027import java.util.Vector;
28
Nicolas Roard78a98e42009-05-11 13:34:17 +010029/**
30 * Utility class to display and manage the choosen quota
31 * for an origin (HTML5 WebStorage feature)
32 */
33class BrowserQuotaPreference extends ListPreference {
34
35 private String TAG = "BrowserQuotaPreference";
36 private OriginSettings mOrigin = null;
37 private static long sOneMB = 1024 * 1024;
38
39 // This is the constructor called by the inflater
40 public BrowserQuotaPreference(Context context, AttributeSet attrs) {
41 super(context, attrs);
42 }
43
44 public BrowserQuotaPreference(Context context, OriginSettings origin) {
45 super(context);
46 mOrigin = origin;
47 }
48
Nicolas Roard6f480422009-05-12 16:31:33 +010049 /**
50 * Find the minimum quota fitting the current usage
51 * and only show larger quotas in the list
52 */
53 public void setQuotaList () {
54 CharSequence[] entries = getEntries();
Nicolas Roard78a98e42009-05-11 13:34:17 +010055 CharSequence[] values = getEntryValues();
Nicolas Roard6f480422009-05-12 16:31:33 +010056 Vector<CharSequence> listEntries = new Vector<CharSequence>();
57 Vector<CharSequence> listValues = new Vector<CharSequence>();
58 long usage = 0;
Nicolas Roard78a98e42009-05-11 13:34:17 +010059 if (mOrigin != null) {
Nicolas Roard6f480422009-05-12 16:31:33 +010060 usage = mOrigin.getUsage();
Nicolas Roard78a98e42009-05-11 13:34:17 +010061 }
62 for (int i = 0; i < values.length; i++) {
63 long value = Long.parseLong(values[i].toString());
64 value *= sOneMB; // the string array is expressed in MB
Nicolas Roard6f480422009-05-12 16:31:33 +010065 if (value >= usage) {
66 listEntries.add(entries[i]);
67 listValues.add(values[i]);
Nicolas Roard78a98e42009-05-11 13:34:17 +010068 }
69 }
Nicolas Roard6f480422009-05-12 16:31:33 +010070 CharSequence[] newEntries = new CharSequence[listEntries.size()];
71 CharSequence[] newValues = new CharSequence[listValues.size()];
72 for (int i = 0; i < listEntries.size(); i++) {
73 newEntries[i] = listEntries.get(i);
74 newValues[i] = listValues.get(i);
75 }
76 setEntries(newEntries);
77 setEntryValues(newValues);
78 setValueIndex(0);
Nicolas Roard78a98e42009-05-11 13:34:17 +010079 }
80
81 @Override
82 protected View onCreateDialogView() {
Nicolas Roard6f480422009-05-12 16:31:33 +010083 setQuotaList();
Nicolas Roard78a98e42009-05-11 13:34:17 +010084 return super.onCreateDialogView();
85 }
86
87 @Override
88 protected void onDialogClosed(boolean positiveResult) {
89 super.onDialogClosed(positiveResult);
90 if (mOrigin == null) {
91 return;
92 }
93 if (positiveResult) {
94 long quota = Long.parseLong(getValue());
95 quota *= sOneMB; // getValue() is in MB
96 mOrigin.setQuota(quota);
97 }
98 }
99}