1 /* //device/apps/Notes/NotesList.java 2 ** 3 ** Copyright 2006, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 package com.android.development; 18 19 import android.app.ListActivity; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.database.Cursor; 24 import android.database.sqlite.SQLiteDatabase; 25 import android.net.Uri; 26 import android.os.Bundle; 27 import android.text.Editable; 28 import android.text.Selection; 29 import android.text.format.DateUtils; 30 import android.util.AttributeSet; 31 import android.view.Menu; 32 import android.view.MenuItem; 33 import android.view.View; 34 import android.widget.EditText; 35 import android.widget.ListView; 36 import android.widget.SimpleAdapter; 37 import android.graphics.Rect; 38 39 import java.io.FileNotFoundException; 40 import java.util.ArrayList; 41 import java.util.GregorianCalendar; 42 import java.util.HashMap; 43 44 public class EnterURL extends ListActivity 45 { 46 private static final int DATABASE_VERSION = 1; 47 public static class UrlEditText extends EditText 48 { 49 public UrlEditText(Context context, AttributeSet attrs) 50 { 51 super(context, attrs); 52 } 53 54 @Override 55 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) 56 { 57 super.onFocusChanged(focused, direction, previouslyFocusedRect); 58 if (focused) { 59 Editable text = getText(); 60 String str = text.toString(); 61 int highlightStart = 0; 62 if (str.startsWith("content://")) { 63 highlightStart = "content://".length(); 64 } 65 Selection.setSelection(text, highlightStart, text.length()); 66 } 67 } 68 } 69 70 public static class DisplayEditText extends EditText 71 { 72 public DisplayEditText(Context context, AttributeSet attrs) 73 { 74 super(context, attrs); 75 } 76 77 @Override 78 protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) 79 { 80 super.onFocusChanged(focused, direction, previouslyFocusedRect); 81 if (focused) { 82 Editable text = getText(); 83 Selection.setSelection(text, 0, text.length()); 84 } 85 } 86 } 87 88 @Override 89 public View onCreateView(String name, Context context, AttributeSet attrs) 90 { 91 if (name.equals("com.android.development.UrlEditText")) { 92 return new UrlEditText(this, attrs); 93 } 94 if (name.equals("com.android.development.DisplayEditText")) { 95 return new DisplayEditText(this, attrs); 96 } 97 return null; 98 } 99 100 View.OnClickListener mViewItemAction = new View.OnClickListener () { 101 public void onClick(View v) 102 { 103 String url = mUrlField.getText().toString(); 104 String display = mDisplayField.getText().toString(); 105 viewItem(url, display); 106 } 107 }; 108 109 public void onCreate(Bundle icicle) 110 { 111 super.onCreate(icicle); 112 setContentView(R.layout.enter_url); 113 114 // display 115 mDisplayField = (DisplayEditText)findViewById(R.id.display_edit_text); 116 mDisplayField.setOnClickListener(mViewItemAction); 117 // url 118 mUrlField = (UrlEditText)findViewById(R.id.url_edit_text); 119 mUrlField.setOnClickListener(mViewItemAction); 120 } 121 122 public void onStop() 123 { 124 super.onStop(); 125 126 if (mCursor != null) { 127 mCursor.deactivate(); 128 } 129 } 130 131 public void onResume() 132 { 133 super.onResume(); 134 135 // show the history 136 loadPrefs(); 137 fillListView(); 138 if (mHistory.size() > 0) { 139 ListView lv = this.getListView(); 140 lv.setSelection(0); 141 lv.requestFocus(); 142 } 143 } 144 145 public boolean onCreateOptionsMenu(Menu menu) 146 { 147 super.onCreateOptionsMenu(menu); 148 menu.add(0, 0, 0, "Clear Bookmarks").setOnMenuItemClickListener(mClearBookmarks); 149 return true; 150 } 151 152 protected void onListItemClick(ListView l, View v, int position, long id) 153 { 154 HistoryEntry he = mHistory.get(position); 155 viewItem(he.url, he.display); 156 } 157 158 private final void viewItem(String url, String display) 159 { 160 // -------------- save this in the history ---------------- 161 // look in the history 162 int count = mHistory.size(); 163 int i; 164 for (i=0; i<count; i++) { 165 HistoryEntry he = mHistory.get(i); 166 if (he.url.equals(url) && he.display.equals(display)) { 167 he.updateAccessTime(); 168 mHistory.remove(i); 169 mHistory.add(0, he); 170 break; 171 } 172 } 173 if (i >= count) { 174 // didn't find it, add it first 175 HistoryEntry he = new HistoryEntry(); 176 he.url = url; 177 he.display = display; 178 he.updateAccessTime(); 179 mHistory.add(0, he); 180 } 181 182 savePrefs(); 183 184 // -------------- view it --------------------------------- 185 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); 186 intent.setClass(this, DataList.class); 187 intent.putExtra("display", display); 188 startActivity(intent); 189 } 190 191 MenuItem.OnMenuItemClickListener mClearBookmarks = new MenuItem.OnMenuItemClickListener() { 192 public boolean onMenuItemClick(MenuItem item) { 193 mHistory.clear(); 194 savePrefs(); 195 fillListView(); 196 return true; 197 } 198 }; 199 200 private void fillListView() 201 { 202 loadPrefs(); 203 ArrayList<HashMap<String, String>> d = new ArrayList<HashMap<String, String>>(); 204 int count = mHistory.size(); 205 for (int i=0; i<count; i++) { 206 HashMap<String, String> m = new HashMap<String, String>(); 207 HistoryEntry he = mHistory.get(i); 208 m.put("title", he.url + " (" + he.display + ")"); 209 d.add(m); 210 } 211 setListAdapter(new SimpleAdapter(this, d, R.layout.url_list, 212 new String[] {"title"}, 213 new int[] {android.R.id.text1})); 214 } 215 216 SQLiteDatabase openDB() 217 { 218 SQLiteDatabase db = null; 219 db = openOrCreateDatabase("inspector.db", 0, null); 220 int version = db.getVersion(); 221 if (version != DATABASE_VERSION) { 222 db.execSQL("CREATE TABLE History (" 223 + " url TEXT," 224 + " display TEXT," 225 + " lastAccessTime TEXT" 226 + ");"); 227 db.execSQL("CREATE TABLE FieldState (" 228 + " url TEXT," 229 + " display TEXT" 230 + ");"); 231 db.setVersion(DATABASE_VERSION); 232 } 233 return db; 234 } 235 236 private void loadPrefs() 237 { 238 SQLiteDatabase db = openDB(); 239 Cursor c = db.query("History", 240 new String[] { "url", "display", "lastAccessTime" }, 241 null, null, null, null, "lastAccessTime DESC"); 242 int urlCol = c.getColumnIndex("url"); 243 int accessCol = c.getColumnIndex("lastAccessTime"); 244 int displayCol = c.getColumnIndex("display"); 245 mHistory.clear(); 246 while (c.moveToNext()) { 247 HistoryEntry he = new HistoryEntry(); 248 he.url = c.getString(urlCol); 249 he.display = c.getString(displayCol); 250 he.lastAccessTime = c.getString(accessCol); 251 mHistory.add(he); 252 } 253 254 c = db.query("FieldState", null, null, null, null, null, null); 255 if (c.moveToNext()) { 256 urlCol = c.getColumnIndex("url"); 257 displayCol = c.getColumnIndex("display"); 258 mUrlField.setText(c.getString(urlCol)); 259 mDisplayField.setText(c.getString(displayCol)); 260 } else { 261 mDisplayField.setText("_id"); 262 mUrlField.setText("content://"); 263 } 264 265 db.close(); 266 } 267 268 private void savePrefs() 269 { 270 ContentValues m; 271 HistoryEntry he; 272 273 SQLiteDatabase db = openDB(); 274 db.execSQL("DELETE FROM History;"); 275 int count = mHistory.size(); 276 for (int i=0; i<count; i++) { 277 m = new ContentValues(); 278 he = mHistory.get(i); 279 m.put("url", he.url); 280 m.put("display", he.display); 281 m.put("lastAccessTime", he.lastAccessTime); 282 db.insert("History", null, m); 283 } 284 285 db.execSQL("DELETE FROM FieldState"); 286 m = new ContentValues(); 287 m.put("url", mUrlField.getText().toString()); 288 m.put("display", mDisplayField.getText().toString()); 289 db.insert("FieldState", null, m); 290 291 db.close(); 292 } 293 294 private class HistoryEntry 295 { 296 public String url; 297 public String display; 298 public String lastAccessTime; 299 public void updateAccessTime() 300 { 301 this.lastAccessTime = DateUtils.writeDateTime( 302 new GregorianCalendar()); 303 } 304 } 305 306 private ArrayList<HistoryEntry> mHistory = new ArrayList<HistoryEntry>(); 307 private UrlEditText mUrlField; 308 private DisplayEditText mDisplayField; 309 private Cursor mCursor; 310 } 311