Nicolas Roard | e46990e | 2009-06-19 16:27:49 +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.app.AlertDialog; |
| 20 | import android.app.ListActivity; |
| 21 | import android.content.Context; |
| 22 | import android.content.DialogInterface; |
| 23 | import android.database.Cursor; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.BitmapFactory; |
| 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
| 28 | import android.provider.Browser; |
| 29 | import android.util.Log; |
| 30 | import android.view.KeyEvent; |
| 31 | import android.view.LayoutInflater; |
Ben Murdoch | b9daacb | 2009-09-14 10:48:24 +0100 | [diff] [blame] | 32 | import android.view.Menu; |
| 33 | import android.view.MenuInflater; |
| 34 | import android.view.MenuItem; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 35 | import android.view.View; |
| 36 | import android.view.ViewGroup; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 37 | import android.webkit.GeolocationPermissions; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 38 | import android.webkit.WebIconDatabase; |
| 39 | import android.webkit.WebStorage; |
| 40 | import android.widget.ArrayAdapter; |
| 41 | import android.widget.AdapterView; |
| 42 | import android.widget.AdapterView.OnItemClickListener; |
| 43 | import android.widget.ImageView; |
| 44 | import android.widget.TextView; |
| 45 | |
| 46 | import java.util.HashMap; |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 47 | import java.util.HashSet; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 48 | import java.util.Iterator; |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 49 | import java.util.Map; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 50 | import java.util.Set; |
| 51 | import java.util.Vector; |
| 52 | |
| 53 | /** |
| 54 | * Manage the settings for an origin. |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 55 | * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage) |
| 56 | * and Geolocation. |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 57 | */ |
| 58 | public class WebsiteSettingsActivity extends ListActivity { |
| 59 | |
| 60 | private String LOGTAG = "WebsiteSettingsActivity"; |
| 61 | private static String sMBStored = null; |
| 62 | private SiteAdapter mAdapter = null; |
| 63 | |
| 64 | class Site { |
| 65 | private String mOrigin; |
| 66 | private String mTitle; |
| 67 | private Bitmap mIcon; |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 68 | private int mFeatures; |
| 69 | |
| 70 | // These constants provide the set of features that a site may support |
| 71 | // They must be consecutive. To add a new feature, add a new FEATURE_XXX |
| 72 | // variable with value equal to the current value of FEATURE_COUNT, then |
| 73 | // increment FEATURE_COUNT. |
| 74 | private final static int FEATURE_WEB_STORAGE = 0; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 75 | private final static int FEATURE_GEOLOCATION = 1; |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 76 | // The number of features available. |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 77 | private final static int FEATURE_COUNT = 2; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 78 | |
Steve Block | 1ad98cf | 2009-07-28 11:07:10 +0100 | [diff] [blame] | 79 | public Site(String origin) { |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 80 | mOrigin = origin; |
Steve Block | 1ad98cf | 2009-07-28 11:07:10 +0100 | [diff] [blame] | 81 | mTitle = null; |
| 82 | mIcon = null; |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 83 | mFeatures = 0; |
| 84 | } |
| 85 | |
| 86 | public void addFeature(int feature) { |
| 87 | mFeatures |= (1 << feature); |
| 88 | } |
| 89 | |
| 90 | public boolean hasFeature(int feature) { |
| 91 | return (mFeatures & (1 << feature)) != 0; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Gets the number of features supported by this site. |
| 96 | */ |
| 97 | public int getFeatureCount() { |
| 98 | int count = 0; |
| 99 | for (int i = 0; i < FEATURE_COUNT; ++i) { |
| 100 | count += hasFeature(i) ? 1 : 0; |
| 101 | } |
| 102 | return count; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Gets the ID of the nth (zero-based) feature supported by this site. |
| 107 | * The return value is a feature ID - one of the FEATURE_XXX values. |
| 108 | * This is required to determine which feature is displayed at a given |
| 109 | * position in the list of features for this site. This is used both |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 110 | * when populating the view and when responding to clicks on the list. |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 111 | */ |
| 112 | public int getFeatureByIndex(int n) { |
| 113 | int j = -1; |
| 114 | for (int i = 0; i < FEATURE_COUNT; ++i) { |
| 115 | j += hasFeature(i) ? 1 : 0; |
| 116 | if (j == n) { |
| 117 | return i; |
| 118 | } |
| 119 | } |
| 120 | return -1; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | public String getOrigin() { |
| 124 | return mOrigin; |
| 125 | } |
| 126 | |
| 127 | public void setTitle(String title) { |
| 128 | mTitle = title; |
| 129 | } |
| 130 | |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 131 | public void setIcon(Bitmap icon) { |
| 132 | mIcon = icon; |
| 133 | } |
| 134 | |
| 135 | public Bitmap getIcon() { |
| 136 | return mIcon; |
| 137 | } |
Steve Block | 1ad98cf | 2009-07-28 11:07:10 +0100 | [diff] [blame] | 138 | |
| 139 | public String getPrettyOrigin() { |
| 140 | return mTitle == null ? null : hideHttp(mOrigin); |
| 141 | } |
| 142 | |
| 143 | public String getPrettyTitle() { |
| 144 | return mTitle == null ? hideHttp(mOrigin) : mTitle; |
| 145 | } |
| 146 | |
| 147 | private String hideHttp(String str) { |
| 148 | Uri uri = Uri.parse(str); |
| 149 | return "http".equals(uri.getScheme()) ? str.substring(7) : str; |
| 150 | } |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | class SiteAdapter extends ArrayAdapter<Site> |
| 154 | implements AdapterView.OnItemClickListener { |
| 155 | private int mResource; |
| 156 | private LayoutInflater mInflater; |
| 157 | private Bitmap mDefaultIcon; |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 158 | private Bitmap mUsageEmptyIcon; |
| 159 | private Bitmap mUsageLowIcon; |
| 160 | private Bitmap mUsageMediumIcon; |
| 161 | private Bitmap mUsageHighIcon; |
| 162 | private Bitmap mLocationIcon; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 163 | private Site mCurrentSite; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 164 | |
| 165 | public SiteAdapter(Context context, int rsc) { |
| 166 | super(context, rsc); |
| 167 | mResource = rsc; |
| 168 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 169 | mDefaultIcon = BitmapFactory.decodeResource(getResources(), |
| 170 | R.drawable.ic_launcher_shortcut_browser_bookmark); |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 171 | mUsageEmptyIcon = BitmapFactory.decodeResource(getResources(), |
| 172 | R.drawable.usage_empty); |
| 173 | mUsageLowIcon = BitmapFactory.decodeResource(getResources(), |
| 174 | R.drawable.usage_low); |
| 175 | mUsageMediumIcon = BitmapFactory.decodeResource(getResources(), |
| 176 | R.drawable.usage_medium); |
| 177 | mUsageHighIcon = BitmapFactory.decodeResource(getResources(), |
| 178 | R.drawable.usage_high); |
| 179 | mLocationIcon = BitmapFactory.decodeResource(getResources(), |
| 180 | R.drawable.location); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 181 | populateOrigins(); |
| 182 | } |
| 183 | |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 184 | /** |
| 185 | * Adds the specified feature to the site corresponding to supplied |
| 186 | * origin in the map. Creates the site if it does not already exist. |
| 187 | */ |
| 188 | private void addFeatureToSite(Map sites, String origin, int feature) { |
| 189 | Site site = null; |
| 190 | if (sites.containsKey(origin)) { |
| 191 | site = (Site) sites.get(origin); |
| 192 | } else { |
| 193 | site = new Site(origin); |
| 194 | sites.put(origin, site); |
| 195 | } |
| 196 | site.addFeature(feature); |
| 197 | } |
| 198 | |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 199 | public void populateOrigins() { |
| 200 | clear(); |
| 201 | |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 202 | // Get the list of origins we want to display. |
| 203 | // All 'HTML 5 modules' (Database, Geolocation etc) form these |
| 204 | // origin strings using WebCore::SecurityOrigin::toString(), so it's |
| 205 | // safe to group origins here. Note that WebCore::SecurityOrigin |
| 206 | // uses 0 (which is not printed) for the port if the port is the |
| 207 | // default for the protocol. Eg http://www.google.com and |
| 208 | // http://www.google.com:80 both record a port of 0 and hence |
| 209 | // toString() == 'http://www.google.com' for both. |
Andrei Popescu | 824faeb | 2009-07-21 18:24:06 +0100 | [diff] [blame] | 210 | Set origins = WebStorage.getInstance().getOrigins(); |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 211 | Map sites = new HashMap<String, Site>(); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 212 | if (origins != null) { |
Andrei Popescu | 824faeb | 2009-07-21 18:24:06 +0100 | [diff] [blame] | 213 | Iterator<String> iter = origins.iterator(); |
| 214 | while (iter.hasNext()) { |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 215 | addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 216 | } |
| 217 | } |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 218 | origins = GeolocationPermissions.getInstance().getOrigins(); |
| 219 | if (origins != null) { |
| 220 | Iterator<String> iter = origins.iterator(); |
| 221 | while (iter.hasNext()) { |
| 222 | addFeatureToSite(sites, iter.next(), Site.FEATURE_GEOLOCATION); |
| 223 | } |
| 224 | } |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 225 | |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 226 | // Create a map from host to origin. This is used to add metadata |
| 227 | // (title, icon) for this origin from the bookmarks DB. |
| 228 | HashMap hosts = new HashMap<String, Set<Site> >(); |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 229 | Set keys = sites.keySet(); |
| 230 | Iterator<String> originIter = keys.iterator(); |
| 231 | while (originIter.hasNext()) { |
| 232 | String origin = originIter.next(); |
| 233 | Site site = (Site) sites.get(origin); |
| 234 | String host = Uri.parse(origin).getHost(); |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 235 | Set hostSites = null; |
| 236 | if (hosts.containsKey(host)) { |
| 237 | hostSites = (Set) hosts.get(host); |
| 238 | } else { |
| 239 | hostSites = new HashSet<Site>(); |
| 240 | hosts.put(host, hostSites); |
| 241 | } |
| 242 | hostSites.add(site); |
| 243 | } |
| 244 | |
| 245 | // Check the bookmark DB. If we have data for a host used by any of |
| 246 | // our origins, use it to set their title and favicon |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 247 | Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI, |
| 248 | new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE, |
| 249 | Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null); |
| 250 | |
| 251 | if ((c != null) && c.moveToFirst()) { |
| 252 | int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL); |
| 253 | int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE); |
| 254 | int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON); |
| 255 | do { |
| 256 | String url = c.getString(urlIndex); |
| 257 | String host = Uri.parse(url).getHost(); |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 258 | if (hosts.containsKey(host)) { |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 259 | String title = c.getString(titleIndex); |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 260 | Bitmap bmp = null; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 261 | byte[] data = c.getBlob(faviconIndex); |
| 262 | if (data != null) { |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 263 | bmp = BitmapFactory.decodeByteArray(data, 0, data.length); |
| 264 | } |
| 265 | Set matchingSites = (Set) hosts.get(host); |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 266 | Iterator<Site> sitesIter = matchingSites.iterator(); |
Steve Block | ee0d639 | 2009-07-28 13:49:15 +0100 | [diff] [blame] | 267 | while (sitesIter.hasNext()) { |
| 268 | Site site = sitesIter.next(); |
| 269 | site.setTitle(title); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 270 | if (bmp != null) { |
| 271 | site.setIcon(bmp); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } while (c.moveToNext()); |
| 276 | } |
| 277 | |
| 278 | // We can now simply populate our array with Site instances |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 279 | keys = sites.keySet(); |
| 280 | originIter = keys.iterator(); |
| 281 | while (originIter.hasNext()) { |
| 282 | String origin = originIter.next(); |
| 283 | Site site = (Site) sites.get(origin); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 284 | add(site); |
| 285 | } |
| 286 | |
| 287 | if (getCount() == 0) { |
| 288 | finish(); // we close the screen |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | public int getCount() { |
| 293 | if (mCurrentSite == null) { |
| 294 | return super.getCount(); |
| 295 | } |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 296 | return mCurrentSite.getFeatureCount(); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 297 | } |
| 298 | |
Steve Block | 764f0c9 | 2009-09-10 15:01:37 +0100 | [diff] [blame] | 299 | public String sizeValueToString(long bytes) { |
| 300 | // We display the size in MB, to 1dp, rounding up to the next 0.1MB. |
| 301 | // bytes should always be greater than zero. |
| 302 | if (bytes <= 0) { |
| 303 | Log.e(LOGTAG, "sizeValueToString called with non-positive value"); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 304 | return "0"; |
| 305 | } |
Steve Block | 764f0c9 | 2009-09-10 15:01:37 +0100 | [diff] [blame] | 306 | float megabytes = (float) bytes / (1024.0F * 1024.0F); |
| 307 | int truncated = (int) Math.ceil(megabytes * 10.0F); |
| 308 | float result = (float) (truncated / 10.0F); |
| 309 | return String.valueOf(result); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
| 313 | * If we receive the back event and are displaying |
| 314 | * site's settings, we want to go back to the main |
| 315 | * list view. If not, we just do nothing (see |
| 316 | * dispatchKeyEvent() below). |
| 317 | */ |
| 318 | public boolean backKeyPressed() { |
| 319 | if (mCurrentSite != null) { |
| 320 | mCurrentSite = null; |
| 321 | populateOrigins(); |
| 322 | notifyDataSetChanged(); |
| 323 | return true; |
| 324 | } |
| 325 | return false; |
| 326 | } |
| 327 | |
| 328 | public View getView(int position, View convertView, ViewGroup parent) { |
| 329 | View view; |
| 330 | TextView title; |
| 331 | TextView subtitle; |
| 332 | ImageView icon; |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 333 | ImageView usageIcon; |
| 334 | ImageView locationIcon; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 335 | |
| 336 | if (convertView == null) { |
| 337 | view = mInflater.inflate(mResource, parent, false); |
| 338 | } else { |
| 339 | view = convertView; |
| 340 | } |
| 341 | |
| 342 | title = (TextView) view.findViewById(R.id.title); |
| 343 | subtitle = (TextView) view.findViewById(R.id.subtitle); |
| 344 | icon = (ImageView) view.findViewById(R.id.icon); |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 345 | usageIcon = (ImageView) view.findViewById(R.id.usage_icon); |
| 346 | locationIcon = (ImageView) view.findViewById(R.id.location_icon); |
| 347 | usageIcon.setVisibility(View.GONE); |
| 348 | locationIcon.setVisibility(View.GONE); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 349 | |
| 350 | if (mCurrentSite == null) { |
Steve Block | 4d055a5 | 2009-07-31 08:38:58 +0100 | [diff] [blame] | 351 | setTitle(getString(R.string.pref_extras_website_settings)); |
| 352 | |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 353 | Site site = getItem(position); |
Steve Block | 1ad98cf | 2009-07-28 11:07:10 +0100 | [diff] [blame] | 354 | title.setText(site.getPrettyTitle()); |
| 355 | subtitle.setText(site.getPrettyOrigin()); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 356 | icon.setVisibility(View.VISIBLE); |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 357 | usageIcon.setVisibility(View.INVISIBLE); |
| 358 | locationIcon.setVisibility(View.INVISIBLE); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 359 | Bitmap bmp = site.getIcon(); |
| 360 | if (bmp == null) { |
| 361 | bmp = mDefaultIcon; |
| 362 | } |
| 363 | icon.setImageBitmap(bmp); |
| 364 | // We set the site as the view's tag, |
| 365 | // so that we can get it in onItemClick() |
| 366 | view.setTag(site); |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 367 | |
| 368 | if (site.hasFeature(Site.FEATURE_WEB_STORAGE)) { |
| 369 | String origin = site.getOrigin(); |
| 370 | long usageInBytes = WebStorage.getInstance().getUsageForOrigin(origin); |
| 371 | float usageInMegabytes = (float) usageInBytes / (1024.0F * 1024.0F); |
| 372 | usageIcon.setVisibility(View.VISIBLE); |
| 373 | |
| 374 | // We set the correct icon: |
| 375 | // 0 < empty < 0.1MB |
| 376 | // 0.1MB < low < 3MB |
| 377 | // 3MB < medium < 6MB |
| 378 | // 6MB < high |
| 379 | if (usageInMegabytes <= 0.1) { |
| 380 | usageIcon.setImageBitmap(mUsageEmptyIcon); |
| 381 | } else if (usageInMegabytes > 0.1 && usageInMegabytes <= 3) { |
| 382 | usageIcon.setImageBitmap(mUsageLowIcon); |
| 383 | } else if (usageInMegabytes > 3 && usageInMegabytes <= 6) { |
| 384 | usageIcon.setImageBitmap(mUsageMediumIcon); |
| 385 | } else if (usageInMegabytes > 6) { |
| 386 | usageIcon.setImageBitmap(mUsageHighIcon); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if (site.hasFeature(Site.FEATURE_GEOLOCATION)) { |
| 391 | locationIcon.setVisibility(View.VISIBLE); |
| 392 | locationIcon.setImageBitmap(mLocationIcon); |
| 393 | } |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 394 | } else { |
Steve Block | 4d055a5 | 2009-07-31 08:38:58 +0100 | [diff] [blame] | 395 | setTitle(mCurrentSite.getPrettyTitle()); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 396 | icon.setVisibility(View.GONE); |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 397 | String origin = mCurrentSite.getOrigin(); |
| 398 | switch (mCurrentSite.getFeatureByIndex(position)) { |
| 399 | case Site.FEATURE_WEB_STORAGE: |
| 400 | long usageValue = WebStorage.getInstance().getUsageForOrigin(origin); |
| 401 | String usage = sizeValueToString(usageValue) + " " + sMBStored; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 402 | |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 403 | title.setText(R.string.webstorage_clear_data_title); |
| 404 | subtitle.setText(usage); |
| 405 | break; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 406 | case Site.FEATURE_GEOLOCATION: |
| 407 | title.setText(R.string.geolocation_settings_page_title); |
| 408 | boolean allowed = GeolocationPermissions.getInstance().getAllowed(origin); |
| 409 | subtitle.setText(allowed ? |
| 410 | R.string.geolocation_settings_page_summary_allowed : |
| 411 | R.string.geolocation_settings_page_summary_not_allowed); |
| 412 | break; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | return view; |
| 417 | } |
| 418 | |
| 419 | public void onItemClick(AdapterView<?> parent, |
| 420 | View view, |
| 421 | int position, |
| 422 | long id) { |
| 423 | if (mCurrentSite != null) { |
Steve Block | 089ce3a | 2009-07-29 17:16:01 +0100 | [diff] [blame] | 424 | switch (mCurrentSite.getFeatureByIndex(position)) { |
| 425 | case Site.FEATURE_WEB_STORAGE: |
| 426 | new AlertDialog.Builder(getContext()) |
| 427 | .setTitle(R.string.webstorage_clear_data_dialog_title) |
| 428 | .setMessage(R.string.webstorage_clear_data_dialog_message) |
| 429 | .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button, |
| 430 | new AlertDialog.OnClickListener() { |
| 431 | public void onClick(DialogInterface dlg, int which) { |
| 432 | WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin()); |
| 433 | mCurrentSite = null; |
| 434 | populateOrigins(); |
| 435 | notifyDataSetChanged(); |
| 436 | }}) |
| 437 | .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null) |
| 438 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 439 | .show(); |
| 440 | break; |
Steve Block | f344d03 | 2009-07-30 10:50:45 +0100 | [diff] [blame] | 441 | case Site.FEATURE_GEOLOCATION: |
| 442 | new AlertDialog.Builder(getContext()) |
| 443 | .setTitle(R.string.geolocation_settings_page_dialog_title) |
| 444 | .setMessage(R.string.geolocation_settings_page_dialog_message) |
| 445 | .setPositiveButton(R.string.geolocation_settings_page_dialog_ok_button, |
| 446 | new AlertDialog.OnClickListener() { |
| 447 | public void onClick(DialogInterface dlg, int which) { |
| 448 | GeolocationPermissions.getInstance().clear(mCurrentSite.getOrigin()); |
| 449 | mCurrentSite = null; |
| 450 | populateOrigins(); |
| 451 | notifyDataSetChanged(); |
| 452 | }}) |
| 453 | .setNegativeButton(R.string.geolocation_settings_page_dialog_cancel_button, null) |
| 454 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 455 | .show(); |
| 456 | break; |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 457 | } |
| 458 | } else { |
| 459 | mCurrentSite = (Site) view.getTag(); |
| 460 | notifyDataSetChanged(); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Intercepts the back key to immediately notify |
| 467 | * NativeDialog that we are done. |
| 468 | */ |
| 469 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 470 | if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) |
| 471 | && (event.getAction() == KeyEvent.ACTION_DOWN)) { |
| 472 | if ((mAdapter != null) && (mAdapter.backKeyPressed())){ |
| 473 | return true; // event consumed |
| 474 | } |
| 475 | } |
| 476 | return super.dispatchKeyEvent(event); |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | protected void onCreate(Bundle icicle) { |
| 481 | super.onCreate(icicle); |
| 482 | if (sMBStored == null) { |
| 483 | sMBStored = getString(R.string.webstorage_origin_summary_mb_stored); |
| 484 | } |
Nicolas Roard | c570232 | 2009-09-14 20:39:08 +0100 | [diff] [blame^] | 485 | mAdapter = new SiteAdapter(this, R.layout.website_settings_row); |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 486 | setListAdapter(mAdapter); |
| 487 | getListView().setOnItemClickListener(mAdapter); |
| 488 | } |
Ben Murdoch | b9daacb | 2009-09-14 10:48:24 +0100 | [diff] [blame] | 489 | |
| 490 | @Override |
| 491 | public boolean onCreateOptionsMenu(Menu menu) { |
| 492 | MenuInflater inflater = getMenuInflater(); |
| 493 | inflater.inflate(R.menu.websitesettings, menu); |
| 494 | return true; |
| 495 | } |
| 496 | |
| 497 | @Override |
| 498 | public boolean onPrepareOptionsMenu(Menu menu) { |
| 499 | // If we aren't listing any sites hide the clear all button (and hence the menu). |
| 500 | return mAdapter.getCount() > 0; |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public boolean onOptionsItemSelected(MenuItem item) { |
| 505 | switch (item.getItemId()) { |
| 506 | case R.id.website_settings_menu_clear_all: |
| 507 | // Show the prompt to clear all origins of their data and geolocation permissions. |
| 508 | new AlertDialog.Builder(this) |
| 509 | .setTitle(R.string.website_settings_clear_all_dialog_title) |
| 510 | .setMessage(R.string.website_settings_clear_all_dialog_message) |
| 511 | .setPositiveButton(R.string.website_settings_clear_all_dialog_ok_button, |
| 512 | new AlertDialog.OnClickListener() { |
| 513 | public void onClick(DialogInterface dlg, int which) { |
| 514 | WebStorage.getInstance().deleteAllData(); |
| 515 | GeolocationPermissions.getInstance().clearAll(); |
| 516 | mAdapter.populateOrigins(); |
| 517 | mAdapter.notifyDataSetChanged(); |
| 518 | }}) |
| 519 | .setNegativeButton(R.string.website_settings_clear_all_dialog_cancel_button, null) |
| 520 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 521 | .show(); |
| 522 | return true; |
| 523 | } |
| 524 | return false; |
| 525 | } |
Nicolas Roard | e46990e | 2009-06-19 16:27:49 +0100 | [diff] [blame] | 526 | } |