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