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