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 | |
| 27 | /** |
| 28 | * Utility class to display and manage the choosen quota |
| 29 | * for an origin (HTML5 WebStorage feature) |
| 30 | */ |
| 31 | class BrowserQuotaPreference extends ListPreference { |
| 32 | |
| 33 | private String TAG = "BrowserQuotaPreference"; |
| 34 | private OriginSettings mOrigin = null; |
| 35 | private static long sOneMB = 1024 * 1024; |
| 36 | |
| 37 | // This is the constructor called by the inflater |
| 38 | public BrowserQuotaPreference(Context context, AttributeSet attrs) { |
| 39 | super(context, attrs); |
| 40 | } |
| 41 | |
| 42 | public BrowserQuotaPreference(Context context, OriginSettings origin) { |
| 43 | super(context); |
| 44 | mOrigin = origin; |
| 45 | } |
| 46 | |
| 47 | public void setCurrentIndex () { |
| 48 | CharSequence[] values = getEntryValues(); |
| 49 | long quota = 0; |
| 50 | if (mOrigin != null) { |
| 51 | quota = mOrigin.getQuota(); |
| 52 | } |
| 53 | for (int i = 0; i < values.length; i++) { |
| 54 | long value = Long.parseLong(values[i].toString()); |
| 55 | value *= sOneMB; // the string array is expressed in MB |
| 56 | if (value >= quota) { |
| 57 | setValueIndex(i); |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected View onCreateDialogView() { |
| 65 | setCurrentIndex(); |
| 66 | return super.onCreateDialogView(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected void onDialogClosed(boolean positiveResult) { |
| 71 | super.onDialogClosed(positiveResult); |
| 72 | if (mOrigin == null) { |
| 73 | return; |
| 74 | } |
| 75 | if (positiveResult) { |
| 76 | long quota = Long.parseLong(getValue()); |
| 77 | quota *= sOneMB; // getValue() is in MB |
| 78 | mOrigin.setQuota(quota); |
| 79 | } |
| 80 | } |
| 81 | } |