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