blob: f91879fb0d90fcdb7388f72a52f82985f8047a05 [file] [log] [blame]
Nicolas Roarde46990e2009-06-19 16:27:49 +01001/*
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.WebIconDatabase;
35import android.webkit.WebStorage;
36import android.widget.ArrayAdapter;
37import android.widget.AdapterView;
38import android.widget.AdapterView.OnItemClickListener;
39import android.widget.ImageView;
40import android.widget.TextView;
41
42import java.util.HashMap;
Steve Blockee0d6392009-07-28 13:49:15 +010043import java.util.HashSet;
Nicolas Roarde46990e2009-06-19 16:27:49 +010044import java.util.Iterator;
45import java.util.Set;
46import java.util.Vector;
47
48/**
49 * Manage the settings for an origin.
50 * We use it to keep track of the HTML5 settings, i.e. database (webstorage).
51 */
52public class WebsiteSettingsActivity extends ListActivity {
53
54 private String LOGTAG = "WebsiteSettingsActivity";
55 private static String sMBStored = null;
56 private SiteAdapter mAdapter = null;
57
58 class Site {
59 private String mOrigin;
60 private String mTitle;
61 private Bitmap mIcon;
62
Steve Block1ad98cf2009-07-28 11:07:10 +010063 public Site(String origin) {
Nicolas Roarde46990e2009-06-19 16:27:49 +010064 mOrigin = origin;
Steve Block1ad98cf2009-07-28 11:07:10 +010065 mTitle = null;
66 mIcon = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +010067 }
68
69 public String getOrigin() {
70 return mOrigin;
71 }
72
73 public void setTitle(String title) {
74 mTitle = title;
75 }
76
Nicolas Roarde46990e2009-06-19 16:27:49 +010077 public void setIcon(Bitmap icon) {
78 mIcon = icon;
79 }
80
81 public Bitmap getIcon() {
82 return mIcon;
83 }
Steve Block1ad98cf2009-07-28 11:07:10 +010084
85 public String getPrettyOrigin() {
86 return mTitle == null ? null : hideHttp(mOrigin);
87 }
88
89 public String getPrettyTitle() {
90 return mTitle == null ? hideHttp(mOrigin) : mTitle;
91 }
92
93 private String hideHttp(String str) {
94 Uri uri = Uri.parse(str);
95 return "http".equals(uri.getScheme()) ? str.substring(7) : str;
96 }
Nicolas Roarde46990e2009-06-19 16:27:49 +010097 }
98
99 class SiteAdapter extends ArrayAdapter<Site>
100 implements AdapterView.OnItemClickListener {
101 private int mResource;
102 private LayoutInflater mInflater;
103 private Bitmap mDefaultIcon;
104 private Site mCurrentSite;
105 private final static int STORED_DATA = 0;
106
107 public SiteAdapter(Context context, int rsc) {
108 super(context, rsc);
109 mResource = rsc;
110 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
111 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
112 R.drawable.ic_launcher_shortcut_browser_bookmark);
113 populateOrigins();
114 }
115
116 public void populateOrigins() {
117 clear();
118
119 // Get the list of origins we want to display
Andrei Popescu824faeb2009-07-21 18:24:06 +0100120 Set origins = WebStorage.getInstance().getOrigins();
Steve Blockee0d6392009-07-28 13:49:15 +0100121 Set sites = new HashSet<Site>();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100122 if (origins != null) {
Andrei Popescu824faeb2009-07-21 18:24:06 +0100123 Iterator<String> iter = origins.iterator();
124 while (iter.hasNext()) {
125 String origin = iter.next();
Steve Block1ad98cf2009-07-28 11:07:10 +0100126 Site site = new Site(origin);
Steve Blockee0d6392009-07-28 13:49:15 +0100127 sites.add(site);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100128 }
129 }
130
Steve Blockee0d6392009-07-28 13:49:15 +0100131 // Create a map from host to origin. This is used to add metadata
132 // (title, icon) for this origin from the bookmarks DB.
133 HashMap hosts = new HashMap<String, Set<Site> >();
134 Iterator<Site> sitesIter = sites.iterator();
135 while (sitesIter.hasNext()) {
136 Site site = sitesIter.next();
137 String host = Uri.parse(site.getOrigin()).getHost();
138 Set hostSites = null;
139 if (hosts.containsKey(host)) {
140 hostSites = (Set) hosts.get(host);
141 } else {
142 hostSites = new HashSet<Site>();
143 hosts.put(host, hostSites);
144 }
145 hostSites.add(site);
146 }
147
148 // Check the bookmark DB. If we have data for a host used by any of
149 // our origins, use it to set their title and favicon
Nicolas Roarde46990e2009-06-19 16:27:49 +0100150 Cursor c = getContext().getContentResolver().query(Browser.BOOKMARKS_URI,
151 new String[] { Browser.BookmarkColumns.URL, Browser.BookmarkColumns.TITLE,
152 Browser.BookmarkColumns.FAVICON }, "bookmark = 1", null, null);
153
154 if ((c != null) && c.moveToFirst()) {
155 int urlIndex = c.getColumnIndex(Browser.BookmarkColumns.URL);
156 int titleIndex = c.getColumnIndex(Browser.BookmarkColumns.TITLE);
157 int faviconIndex = c.getColumnIndex(Browser.BookmarkColumns.FAVICON);
158 do {
159 String url = c.getString(urlIndex);
160 String host = Uri.parse(url).getHost();
Steve Blockee0d6392009-07-28 13:49:15 +0100161 if (hosts.containsKey(host)) {
Nicolas Roarde46990e2009-06-19 16:27:49 +0100162 String title = c.getString(titleIndex);
Steve Blockee0d6392009-07-28 13:49:15 +0100163 Bitmap bmp = null;
Nicolas Roarde46990e2009-06-19 16:27:49 +0100164 byte[] data = c.getBlob(faviconIndex);
165 if (data != null) {
Steve Blockee0d6392009-07-28 13:49:15 +0100166 bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
167 }
168 Set matchingSites = (Set) hosts.get(host);
169 sitesIter = matchingSites.iterator();
170 while (sitesIter.hasNext()) {
171 Site site = sitesIter.next();
172 site.setTitle(title);
Nicolas Roarde46990e2009-06-19 16:27:49 +0100173 if (bmp != null) {
174 site.setIcon(bmp);
175 }
176 }
177 }
178 } while (c.moveToNext());
179 }
180
181 // We can now simply populate our array with Site instances
Steve Blockee0d6392009-07-28 13:49:15 +0100182 sitesIter = sites.iterator();
183 while (sitesIter.hasNext()) {
184 Site site = sitesIter.next();
Nicolas Roarde46990e2009-06-19 16:27:49 +0100185 add(site);
186 }
187
188 if (getCount() == 0) {
189 finish(); // we close the screen
190 }
191 }
192
193 public int getCount() {
194 if (mCurrentSite == null) {
195 return super.getCount();
196 }
197 return 1; // db view
198 }
199
200 public String sizeValueToString(long value) {
201 float mb = (float) value / (1024.0F * 1024.0F);
202 int val = (int) (mb * 10);
203 float ret = (float) (val / 10.0F);
204 if (ret <= 0) {
205 return "0";
206 }
207 return String.valueOf(ret);
208 }
209
210 /*
211 * If we receive the back event and are displaying
212 * site's settings, we want to go back to the main
213 * list view. If not, we just do nothing (see
214 * dispatchKeyEvent() below).
215 */
216 public boolean backKeyPressed() {
217 if (mCurrentSite != null) {
218 mCurrentSite = null;
219 populateOrigins();
220 notifyDataSetChanged();
221 return true;
222 }
223 return false;
224 }
225
226 public View getView(int position, View convertView, ViewGroup parent) {
227 View view;
228 TextView title;
229 TextView subtitle;
230 ImageView icon;
231
232 if (convertView == null) {
233 view = mInflater.inflate(mResource, parent, false);
234 } else {
235 view = convertView;
236 }
237
238 title = (TextView) view.findViewById(R.id.title);
239 subtitle = (TextView) view.findViewById(R.id.subtitle);
240 icon = (ImageView) view.findViewById(R.id.icon);
241
242 if (mCurrentSite == null) {
243 Site site = getItem(position);
Steve Block1ad98cf2009-07-28 11:07:10 +0100244 title.setText(site.getPrettyTitle());
245 subtitle.setText(site.getPrettyOrigin());
Nicolas Roarde46990e2009-06-19 16:27:49 +0100246 icon.setVisibility(View.VISIBLE);
247 Bitmap bmp = site.getIcon();
248 if (bmp == null) {
249 bmp = mDefaultIcon;
250 }
251 icon.setImageBitmap(bmp);
252 // We set the site as the view's tag,
253 // so that we can get it in onItemClick()
254 view.setTag(site);
255 } else {
256 icon.setVisibility(View.GONE);
257 if (position == STORED_DATA) {
258 String origin = mCurrentSite.getOrigin();
259 long usageValue = WebStorage.getInstance().getUsageForOrigin(origin);
260 String usage = sizeValueToString(usageValue) + " " + sMBStored;
261
262 title.setText(R.string.webstorage_clear_data_title);
263 subtitle.setText(usage);
264 }
265 }
266
267 return view;
268 }
269
270 public void onItemClick(AdapterView<?> parent,
271 View view,
272 int position,
273 long id) {
274 if (mCurrentSite != null) {
275 if (position == STORED_DATA) {
276 new AlertDialog.Builder(getContext())
277 .setTitle(R.string.webstorage_clear_data_dialog_title)
278 .setMessage(R.string.webstorage_clear_data_dialog_message)
279 .setPositiveButton(R.string.webstorage_clear_data_dialog_ok_button,
280 new AlertDialog.OnClickListener() {
281 public void onClick(DialogInterface dlg, int which) {
282 WebStorage.getInstance().deleteOrigin(mCurrentSite.getOrigin());
283 mCurrentSite = null;
284 populateOrigins();
285 notifyDataSetChanged();
286 }})
287 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
288 .setIcon(android.R.drawable.ic_dialog_alert)
289 .show();
290 }
291 } else {
292 mCurrentSite = (Site) view.getTag();
293 notifyDataSetChanged();
294 }
295 }
296 }
297
298 /**
299 * Intercepts the back key to immediately notify
300 * NativeDialog that we are done.
301 */
302 public boolean dispatchKeyEvent(KeyEvent event) {
303 if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK)
304 && (event.getAction() == KeyEvent.ACTION_DOWN)) {
305 if ((mAdapter != null) && (mAdapter.backKeyPressed())){
306 return true; // event consumed
307 }
308 }
309 return super.dispatchKeyEvent(event);
310 }
311
312 @Override
313 protected void onCreate(Bundle icicle) {
314 super.onCreate(icicle);
315 if (sMBStored == null) {
316 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
317 }
318 mAdapter = new SiteAdapter(this, R.layout.application);
319 setListAdapter(mAdapter);
320 getListView().setOnItemClickListener(mAdapter);
321 }
322}