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.Iterator;
45 import java.util.Vector;
48 * Manage the settings for an origin.
49 * We use it to keep track of the HTML5 settings, i.e. database (webstorage).
51 public class WebsiteSettingsActivity extends ListActivity {
53 private String LOGTAG = "WebsiteSettingsActivity";
54 private static String sMBStored = null;
55 private SiteAdapter mAdapter = null;
58 private String mOrigin;
59 private String mTitle;
62 public Site(String origin, String title, Bitmap icon) {
68 public String getOrigin() {
72 public void setTitle(String title) {
76 public String getTitle() {
80 public void setIcon(Bitmap icon) {
84 public Bitmap getIcon() {
89 class SiteAdapter extends ArrayAdapter<Site>
90 implements AdapterView.OnItemClickListener {
91 private int mResource;
92 private LayoutInflater mInflater;
93 private Bitmap mDefaultIcon;
94 private Site mCurrentSite;
95 private final static int STORED_DATA = 0;
97 public SiteAdapter(Context context, int rsc) {
100 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
101 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
102 R.drawable.ic_launcher_shortcut_browser_bookmark);
106 public void populateOrigins() {
109 // Get the list of origins we want to display
110 HashMap<String, Site> uris = new HashMap<String, Site>();
111 Vector origins = WebStorage.getInstance().getOrigins();
112 if (origins != null) {
113 for (int i = 0; i < origins.size(); i++) {
114 String origin = (String) origins.get(i);
115 Site site = new Site(origin, origin, null);
116 uris.put(Uri.parse(origin).getHost(), site);
120 // Check the bookmark db -- if one of our origin matches,
121 // we set its title and favicon
122 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
123 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
124 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
126 if ((c != null) && c.moveToFirst()) {
127 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
128 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
129 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
131 String url = c.getString(urlIndex);
132 String host = Uri.parse(url).getHost();
133 if (uris.containsKey(host)) {
134 String title = c.getString(titleIndex);
135 Site site = uris.get(host);
136 site.setTitle(title);
137 byte[] data = c.getBlob(faviconIndex);
139 Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
145 } while (c.moveToNext());
148 // We can now simply populate our array with Site instances
149 Set keys = uris.keySet();
150 Iterator iter = keys.iterator();
151 while (iter.hasNext()) {
152 String origin = (String) iter.next();
153 Site site = uris.get(origin);
157 if (getCount() == 0) {
158 finish(); // we close the screen
162 public int getCount() {
163 if (mCurrentSite == null) {
164 return super.getCount();
169 public String sizeValueToString(long value) {
170 float mb = (float) value / (1024.0F * 1024.0F);
171 int val = (int) (mb * 10);
172 float ret = (float) (val / 10.0F);
176 return String.valueOf(ret);
180 * If we receive the back event and are displaying
181 * site's settings, we want to go back to the main
182 * list view. If not, we just do nothing (see
183 * dispatchKeyEvent() below).
185 public boolean backKeyPressed() {
186 if (mCurrentSite != null) {
189 notifyDataSetChanged();
195 public View getView(int position, View convertView, ViewGroup parent) {
201 if (convertView == null) {
202 view = mInflater.inflate(mResource, parent, false);
207 title = (TextView) view.findViewById(R.id.title);
208 subtitle = (TextView) view.findViewById(R.id.subtitle);
209 icon = (ImageView) view.findViewById(R.id.icon);
211 if (mCurrentSite == null) {
212 Site site = getItem(position);
213 title.setText(site.getTitle());
214 subtitle.setText(site.getOrigin());
215 icon.setVisibility(View.VISIBLE);
216 Bitmap bmp = site.getIcon();
220 icon.setImageBitmap(bmp);
221 // We set the site as the view's tag,
222 // so that we can get it in onItemClick()
225 icon.setVisibility(View.GONE);
226 if (position == STORED_DATA) {
227 String origin = mCurrentSite.getOrigin();
228 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
229 String usage = sizeValueToString(usageValue) + " " + sMBStored;
231 title.setText(R.string.webstorage_clear_data_title);
232 subtitle.setText(usage);
239 public void onItemClick(AdapterView<?> parent,
243 if (mCurrentSite != null) {
244 if (position == STORED_DATA) {
245 new AlertDialog.Builder(getContext())
246 .setTitle(R.string.webstorage_clear_data_dialog_title)
247 .setMessage(R.string.webstorage_clear_data_dialog_message)
248 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
249 new AlertDialog.OnClickListener() {
250 public void onClick(DialogInterface dlg, int which) {
251 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
254 notifyDataSetChanged();
256 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
257 .setIcon(android.R.drawable.ic_dialog_alert)
261 mCurrentSite = (Site) view.getTag();
262 notifyDataSetChanged();
268 * Intercepts the back key to immediately notify
269 * NativeDialog that we are done.
271 public boolean dispatchKeyEvent(KeyEvent event) {
272 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
273 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
274 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
275 return true; // event consumed
278 return super.dispatchKeyEvent(event);
282 protected void onCreate(Bundle icicle) {
283 super.onCreate(icicle);
284 if (sMBStored == null) {
285 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
287 mAdapter = new SiteAdapter(this, R.layout.application);
288 setListAdapter(mAdapter);
289 getListView().setOnItemClickListener(mAdapter);