Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.preference.ListPreference; |
| 20 | import android.content.Context; |
| 21 | import android.preference.PreferenceScreen; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.util.Log; |
| 24 | import android.view.View; |
| 25 | import android.webkit.WebStorage; |
| 26 | |
Nicolas Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 27 | import java.util.Vector; |
| 28 | |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 29 | /** |
| 30 | * Utility class to display and manage the choosen quota |
| 31 | * for an origin (HTML5 WebStorage feature) |
| 32 | */ |
| 33 | class 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 Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 49 | /** |
| 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 Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 55 | CharSequence[] values = getEntryValues(); |
Nicolas Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 56 | Vector<CharSequence> listEntries = new Vector<CharSequence>(); |
| 57 | Vector<CharSequence> listValues = new Vector<CharSequence>(); |
| 58 | long usage = 0; |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 59 | if (mOrigin != null) { |
Nicolas Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 60 | usage = mOrigin.getUsage(); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 61 | } |
| 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 Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 65 | if (value >= usage) { |
| 66 | listEntries.add(entries[i]); |
| 67 | listValues.add(values[i]); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 68 | } |
| 69 | } |
Nicolas Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 70 | 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 Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected View onCreateDialogView() { |
Nicolas Roard | 6f48042 | 2009-05-12 16:31:33 +0100 | [diff] [blame] | 83 | setQuotaList(); |
Nicolas Roard | 78a98e4 | 2009-05-11 13:34:17 +0100 | [diff] [blame] | 84 | 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 | } |