2 * Copyright (C) 2009 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.browser;
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;
32 import android.view.View;
33 import android.view.ViewGroup;
34 import android.webkit.WebIconDatabase;
35 import android.webkit.WebStorage;
36 import android.widget.ArrayAdapter;
37 import android.widget.AdapterView;
38 import android.widget.AdapterView.OnItemClickListener;
39 import android.widget.ImageView;
40 import android.widget.TextView;
42 import java.util.HashMap;
43 import java.util.HashSet;
44 import java.util.Iterator;
47 import java.util.Vector;
50 * Manage the settings for an origin.
51 * We use it to keep track of the 'HTML5' settings, i.e. database (webstorage)
54 public class WebsiteSettingsActivity extends ListActivity {
56 private String LOGTAG = "WebsiteSettingsActivity";
57 private static String sMBStored = null;
58 private SiteAdapter mAdapter = null;
61 private String mOrigin;
62 private String mTitle;
64 private int mFeatures;
66 // These constants provide the set of features that a site may support
67 // They must be consecutive. To add a new feature, add a new FEATURE_XXX
68 // variable with value equal to the current value of FEATURE_COUNT, then
69 // increment FEATURE_COUNT.
70 private final static int FEATURE_WEB_STORAGE = 0;
71 // The number of features available.
72 private final static int FEATURE_COUNT = 1;
74 public Site(String origin) {
81 public void addFeature(int feature) {
82 mFeatures |= (1 << feature);
85 public boolean hasFeature(int feature) {
86 return (mFeatures & (1 << feature)) != 0;
90 * Gets the number of features supported by this site.
92 public int getFeatureCount() {
94 for (int i = 0; i < FEATURE_COUNT; ++i) {
95 count += hasFeature(i) ? 1 : 0;
101 * Gets the ID of the nth (zero-based) feature supported by this site.
102 * The return value is a feature ID - one of the FEATURE_XXX values.
103 * This is required to determine which feature is displayed at a given
104 * position in the list of features for this site. This is used both
105 * when populateing the view and when responding to clicks on the list.
107 public int getFeatureByIndex(int n) {
109 for (int i = 0; i < FEATURE_COUNT; ++i) {
110 j += hasFeature(i) ? 1 : 0;
118 public String getOrigin() {
122 public void setTitle(String title) {
126 public void setIcon(Bitmap icon) {
130 public Bitmap getIcon() {
134 public String getPrettyOrigin() {
135 return mTitle == null ? null : hideHttp(mOrigin);
138 public String getPrettyTitle() {
139 return mTitle == null ? hideHttp(mOrigin) : mTitle;
142 private String hideHttp(String str) {
143 Uri uri = Uri.parse(str);
144 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
148 class SiteAdapter extends ArrayAdapter<Site>
149 implements AdapterView.OnItemClickListener {
150 private int mResource;
151 private LayoutInflater mInflater;
152 private Bitmap mDefaultIcon;
153 private Site mCurrentSite;
155 public SiteAdapter(Context context, int rsc) {
158 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
159 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
160 R.drawable.ic_launcher_shortcut_browser_bookmark);
165 * Adds the specified feature to the site corresponding to supplied
166 * origin in the map. Creates the site if it does not already exist.
168 private void addFeatureToSite(Map sites, String origin, int feature) {
170 if (sites.containsKey(origin)) {
171 site = (Site) sites.get(origin);
173 site = new Site(origin);
174 sites.put(origin, site);
176 site.addFeature(feature);
179 public void populateOrigins() {
182 // Get the list of origins we want to display.
183 // All 'HTML 5 modules' (Database, Geolocation etc) form these
184 // origin strings using WebCore::SecurityOrigin::toString(), so it's
185 // safe to group origins here. Note that WebCore::SecurityOrigin
186 // uses 0 (which is not printed) for the port if the port is the
187 // default for the protocol. Eg http://www.google.com and
188 // http://www.google.com:80 both record a port of 0 and hence
189 // toString() == 'http://www.google.com' for both.
190 Set origins = WebStorage.getInstance().getOrigins();
191 Map sites = new HashMap<String, Site>();
192 if (origins != null) {
193 Iterator<String> iter = origins.iterator();
194 while (iter.hasNext()) {
195 addFeatureToSite(sites, iter.next(), Site.FEATURE_WEB_STORAGE);
199 // Create a map from host to origin. This is used to add metadata
200 // (title, icon) for this origin from the bookmarks DB.
201 HashMap hosts = new HashMap<String, Set<Site> >();
202 Set keys = sites.keySet();
203 Iterator<String> originIter = keys.iterator();
204 while (originIter.hasNext()) {
205 String origin = originIter.next();
206 Site site = (Site) sites.get(origin);
207 String host = Uri.parse(origin).getHost();
208 Set hostSites = null;
209 if (hosts.containsKey(host)) {
210 hostSites = (Set) hosts.get(host);
212 hostSites = new HashSet<Site>();
213 hosts.put(host, hostSites);
218 // Check the bookmark DB. If we have data for a host used by any of
219 // our origins, use it to set their title and favicon
220 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
221 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
222 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
224 if ((c != null) && c.moveToFirst()) {
225 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
226 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
227 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
229 String url = c.getString(urlIndex);
230 String host = Uri.parse(url).getHost();
231 if (hosts.containsKey(host)) {
232 String title = c.getString(titleIndex);
234 byte[] data = c.getBlob(faviconIndex);
236 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
238 Set matchingSites = (Set) hosts.get(host);
239 Iterator<Site> sitesIter = matchingSites.iterator();
240 while (sitesIter.hasNext()) {
241 Site site = sitesIter.next();
242 site.setTitle(title);
248 } while (c.moveToNext());
251 // We can now simply populate our array with Site instances
252 keys = sites.keySet();
253 originIter = keys.iterator();
254 while (originIter.hasNext()) {
255 String origin = originIter.next();
256 Site site = (Site) sites.get(origin);
260 if (getCount() == 0) {
261 finish(); // we close the screen
265 public int getCount() {
266 if (mCurrentSite == null) {
267 return super.getCount();
269 return mCurrentSite.getFeatureCount();
272 public String sizeValueToString(long value) {
273 float mb = (float) value / (1024.0F * 1024.0F);
274 int val = (int) (mb * 10);
275 float ret = (float) (val / 10.0F);
279 return String.valueOf(ret);
283 * If we receive the back event and are displaying
284 * site's settings, we want to go back to the main
285 * list view. If not, we just do nothing (see
286 * dispatchKeyEvent() below).
288 public boolean backKeyPressed() {
289 if (mCurrentSite != null) {
292 notifyDataSetChanged();
298 public View getView(int position, View convertView, ViewGroup parent) {
304 if (convertView == null) {
305 view = mInflater.inflate(mResource, parent, false);
310 title = (TextView) view.findViewById(R.id.title);
311 subtitle = (TextView) view.findViewById(R.id.subtitle);
312 icon = (ImageView) view.findViewById(R.id.icon);
314 if (mCurrentSite == null) {
315 Site site = getItem(position);
316 title.setText(site.getPrettyTitle());
317 subtitle.setText(site.getPrettyOrigin());
318 icon.setVisibility(View.VISIBLE);
319 Bitmap bmp = site.getIcon();
323 icon.setImageBitmap(bmp);
324 // We set the site as the view's tag,
325 // so that we can get it in onItemClick()
328 icon.setVisibility(View.GONE);
329 String origin = mCurrentSite.getOrigin();
330 switch (mCurrentSite.getFeatureByIndex(position)) {
331 case Site.FEATURE_WEB_STORAGE:
332 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
333 String usage = sizeValueToString(usageValue) + " " + sMBStored;
335 title.setText(R.string.webstorage_clear_data_title);
336 subtitle.setText(usage);
344 public void onItemClick(AdapterView<?> parent,
348 if (mCurrentSite != null) {
349 switch (mCurrentSite.getFeatureByIndex(position)) {
350 case Site.FEATURE_WEB_STORAGE:
351 new AlertDialog.Builder(getContext())
352 .setTitle(R.string.webstorage_clear_data_dialog_title)
353 .setMessage(R.string.webstorage_clear_data_dialog_message)
354 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
355 new AlertDialog.OnClickListener() {
356 public void onClick(DialogInterface dlg, int which) {
357 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
360 notifyDataSetChanged();
362 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
363 .setIcon(android.R.drawable.ic_dialog_alert)
368 mCurrentSite = (Site) view.getTag();
369 notifyDataSetChanged();
375 * Intercepts the back key to immediately notify
376 * NativeDialog that we are done.
378 public boolean dispatchKeyEvent(KeyEvent event) {
379 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
380 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
381 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
382 return true; // event consumed
385 return super.dispatchKeyEvent(event);
389 protected void onCreate(Bundle icicle) {
390 super.onCreate(icicle);
391 if (sMBStored == null) {
392 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
394 mAdapter = new SiteAdapter(this, R.layout.application);
395 setListAdapter(mAdapter);
396 getListView().setOnItemClickListener(mAdapter);