blob: 7fea766f02bb70ac9623fb4a503e643960f2cf76 [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;
43import java.util.Iterator;
44import java.util.Set;
45import java.util.Vector;
46
47/**
48 * Manage the settings for an origin.
49 * We use it to keep track of the HTML5 settings, i.e. database (webstorage).
50 */
51public class WebsiteSettingsActivity extends ListActivity {
52
53 private String LOGTAG = "WebsiteSettingsActivity";
54 private static String sMBStored = null;
55 private SiteAdapter mAdapter = null;
56
57 class Site {
58 private String mOrigin;
59 private String mTitle;
60 private Bitmap mIcon;
61
62 public Site(String origin, String title, Bitmap icon) {
63 mOrigin = origin;
64 mTitle = title;
65 mIcon = icon;
66 }
67
68 public String getOrigin() {
69 return mOrigin;
70 }
71
72 public void setTitle(String title) {
73 mTitle = title;
74 }
75
76 public String getTitle() {
77 return mTitle;
78 }
79
80 public void setIcon(Bitmap icon) {
81 mIcon = icon;
82 }
83
84 public Bitmap getIcon() {
85 return mIcon;
86 }
87 }
88
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;
96
97 public SiteAdapter(Context context, int rsc) {
98 super(context, rsc);
99 mResource = rsc;
100 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
101 mDefaultIcon = BitmapFactory.decodeResource(getResources(),
102 R.drawable.ic_launcher_shortcut_browser_bookmark);
103 populateOrigins();
104 }
105
106 public void populateOrigins() {
107 clear();
108
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);
117 }
118 }
119
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);
125
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);
130 do {
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);
138 if (data != null) {
139 Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
140 if (bmp != null) {
141 site.setIcon(bmp);
142 }
143 }
144 }
145 } while (c.moveToNext());
146 }
147
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);
154 add(site);
155 }
156
157 if (getCount() == 0) {
158 finish(); // we close the screen
159 }
160 }
161
162 public int getCount() {
163 if (mCurrentSite == null) {
164 return super.getCount();
165 }
166 return 1; // db view
167 }
168
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);
173 if (ret <= 0) {
174 return "0";
175 }
176 return String.valueOf(ret);
177 }
178
179 /*
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).
184 */
185 public boolean backKeyPressed() {
186 if (mCurrentSite != null) {
187 mCurrentSite = null;
188 populateOrigins();
189 notifyDataSetChanged();
190 return true;
191 }
192 return false;
193 }
194
195 public View getView(int position, View convertView, ViewGroup parent) {
196 View view;
197 TextView title;
198 TextView subtitle;
199 ImageView icon;
200
201 if (convertView == null) {
202 view = mInflater.inflate(mResource, parent, false);
203 } else {
204 view = convertView;
205 }
206
207 title = (TextView) view.findViewById(R.id.title);
208 subtitle = (TextView) view.findViewById(R.id.subtitle);
209 icon = (ImageView) view.findViewById(R.id.icon);
210
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();
217 if (bmp == null) {
218 bmp = mDefaultIcon;
219 }
220 icon.setImageBitmap(bmp);
221 // We set the site as the view's tag,
222 // so that we can get it in onItemClick()
223 view.setTag(site);
224 } else {
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;
230
231 title.setText(R.string.webstorage_clear_data_title);
232 subtitle.setText(usage);
233 }
234 }
235
236 return view;
237 }
238
239 public void onItemClick(AdapterView<?> parent,
240 View view,
241 int position,
242 long id) {
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());
252 mCurrentSite = null;
253 populateOrigins();
254 notifyDataSetChanged();
255 }})
256 .setNegativeButton(R.string.webstorage_clear_data_dialog_cancel_button, null)
257 .setIcon(android.R.drawable.ic_dialog_alert)
258 .show();
259 }
260 } else {
261 mCurrentSite = (Site) view.getTag();
262 notifyDataSetChanged();
263 }
264 }
265 }
266
267 /**
268 * Intercepts the back key to immediately notify
269 * NativeDialog that we are done.
270 */
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
276 }
277 }
278 return super.dispatchKeyEvent(event);
279 }
280
281 @Override
282 protected void onCreate(Bundle icicle) {
283 super.onCreate(icicle);
284 if (sMBStored == null) {
285 sMBStored = getString(R.string.webstorage_origin_summary_mb_stored);
286 }
287 mAdapter = new SiteAdapter(this, R.layout.application);
288 setListAdapter(mAdapter);
289 getListView().setOnItemClickListener(mAdapter);
290 }
291}