blob: f8957c9d9f9b9dd49836ee7b9fbd0df7dbc458c8 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 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
Leon Scroggins25230d72010-09-28 20:09:25 -040019import com.android.browser.provider.BrowserProvider2;
20
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.app.Activity;
Leon Scroggins III052ce662010-09-13 14:44:16 -040022import android.app.LoaderManager;
The Android Open Source Project0c908882009-03-03 19:32:16 -080023import android.content.ContentResolver;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070024import android.content.ContentUris;
Leon Scroggins III052ce662010-09-13 14:44:16 -040025import android.content.ContentValues;
26import android.content.Context;
27import android.content.CursorLoader;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.content.Intent;
Leon Scroggins III052ce662010-09-13 14:44:16 -040029import android.content.Loader;
Leon Scroggins25230d72010-09-28 20:09:25 -040030import android.content.SharedPreferences;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.content.res.Resources;
Patrick Scott3918d442009-08-04 13:22:29 -040032import android.database.Cursor;
Ben Murdochaac7aa62009-09-17 16:57:40 +010033import android.graphics.Bitmap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080034import android.net.ParseException;
Michael Kolbd40ac1a2010-09-29 00:23:46 -070035import android.net.Uri;
The Android Open Source Project0c908882009-03-03 19:32:16 -080036import android.net.WebAddress;
37import android.os.Bundle;
Ben Murdoch1794fe22009-09-29 18:14:30 +010038import android.os.Handler;
39import android.os.Message;
Leon Scroggins25230d72010-09-28 20:09:25 -040040import android.preference.PreferenceManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040041import android.provider.BrowserContract;
Leon Scroggins25230d72010-09-28 20:09:25 -040042import android.text.TextUtils;
Leon Scroggins III052ce662010-09-13 14:44:16 -040043import android.view.KeyEvent;
44import android.view.LayoutInflater;
Leon Scroggins88d08032010-10-21 15:17:10 -040045import android.view.MenuItem;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040047import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080048import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040049import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040050import android.view.inputmethod.EditorInfo;
51import android.view.inputmethod.InputMethodManager;
52import android.widget.AdapterView;
53import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080054import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040055import android.widget.ListView;
Leon Scroggins88d08032010-10-21 15:17:10 -040056import android.widget.PopupMenu;
The Android Open Source Project0c908882009-03-03 19:32:16 -080057import android.widget.TextView;
58import android.widget.Toast;
59
Cary Clarkf2407c62009-09-04 12:25:10 -040060import java.net.URI;
61import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040062import java.util.ArrayList;
Leon Scroggins74dbe012010-10-15 10:54:27 -040063import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040064
65public class AddBookmarkPage extends Activity
66 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040067 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
Leon Scroggins88d08032010-10-21 15:17:10 -040068 BreadCrumbView.Controller, PopupMenu.OnMenuItemClickListener {
The Android Open Source Project0c908882009-03-03 19:32:16 -080069
Michael Kolb370a4f32010-10-06 10:45:32 -070070 public static final long DEFAULT_FOLDER_ID = -1;
Leon Scrogginsbc922852010-10-22 12:15:27 -040071 public static final String TOUCH_ICON_URL = "touch_icon_url";
72 // Place on an edited bookmark to remove the saved thumbnail
73 public static final String REMOVE_THUMBNAIL = "remove_thumbnail";
74 public static final String USER_AGENT = "user_agent";
Michael Kolb370a4f32010-10-06 10:45:32 -070075
Leon Scroggins74dbe012010-10-15 10:54:27 -040076 private static final int MAX_CRUMBS_SHOWN = 2;
77
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 private final String LOGTAG = "Bookmarks";
Leon Scroggins74dbe012010-10-15 10:54:27 -040079 // Set to true to see the crash on the code I would like to run.
80 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081
Leon Scroggins III052ce662010-09-13 14:44:16 -040082 // IDs for the CursorLoaders that are used.
83 private final int LOADER_ID_FOLDER_CONTENTS = 0;
84 private final int LOADER_ID_ALL_FOLDERS = 1;
85
The Android Open Source Project0c908882009-03-03 19:32:16 -080086 private EditText mTitle;
87 private EditText mAddress;
88 private TextView mButton;
89 private View mCancelButton;
90 private boolean mEditingExisting;
91 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040092 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010093 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040094 private TextView mFolder;
95 private View mDefaultView;
96 private View mFolderSelector;
97 private EditText mFolderNamer;
98 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040099 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400100 private long mCurrentFolder = 0;
101 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400102 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400103 private TextView mFakeTitle;
104 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400105 private ListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400106 private boolean mSaveToHomeScreen;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400107
108 private static class Folder {
109 String Name;
110 long Id;
111 Folder(String name, long id) {
112 Name = name;
113 Id = id;
114 }
115 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800116
Ben Murdoch1794fe22009-09-29 18:14:30 +0100117 // Message IDs
118 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400119 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100120
121 private Handler mHandler;
122
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100123 private InputMethodManager getInputMethodManager() {
124 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
125 }
126
Leon Scroggins III052ce662010-09-13 14:44:16 -0400127 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400128 public void onTop(int level, Object data) {
129 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400130 Folder folderData = (Folder) data;
131 long folder = folderData.Id;
132 Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400133 LoaderManager manager = getLoaderManager();
134 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
135 LOADER_ID_FOLDER_CONTENTS));
136 loader.setUri(uri);
137 loader.forceLoad();
138 updateVisible();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400139 if (mFolderNamer.getVisibility() == View.VISIBLE) {
140 completeOrCancelFolderNaming(true);
141 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400142 }
143
144 /**
145 * Update the views shown to only show the two deepest levels of crumbs.
146 * Note that this method depends on internal knowledge of BreadCrumbView.
147 */
148 private void updateVisible() {
149 if (MAX_CRUMBS_SHOWN > 0) {
150 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
151 // This class always uses a back button, which is the first child.
152 int childIndex = 1;
153 if (invisibleCrumbs > 0) {
154 int crumbIndex = 0;
155 while (crumbIndex < invisibleCrumbs) {
156 // Set the crumb to GONE.
157 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
158 childIndex++;
159 // Each crumb is followed by a separator (except the last
160 // one). Also make it GONE
161 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
162 childIndex++;
163 // Move to the next crumb.
164 crumbIndex++;
165 }
166 }
167 // Make sure the last two are visible.
168 int childCount = mCrumbs.getChildCount();
169 while (childIndex < childCount) {
170 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
171 childIndex++;
172 }
173 }
174 }
175
176 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400177 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
178 if (v == mFolderNamer) {
179 if (v.getText().length() > 0) {
180 if (actionId == EditorInfo.IME_NULL) {
181 // Only want to do this once.
182 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400183 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400184 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400185 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800186 }
Michael Kolb31829b92010-10-01 11:50:21 -0700187 // Steal the key press; otherwise a newline will be added
188 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800189 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400190 return false;
191 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800192
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400193 private void switchToDefaultView(boolean changedFolder) {
194 mFolderSelector.setVisibility(View.GONE);
195 mDefaultView.setVisibility(View.VISIBLE);
196 mCrumbHolder.setVisibility(View.GONE);
197 mFakeTitle.setVisibility(View.VISIBLE);
198 if (changedFolder) {
199 Object data = mCrumbs.getTopData();
200 if (data != null) {
201 Folder folder = (Folder) data;
202 mCurrentFolder = folder.Id;
203 mFolder.setText(folder.Name);
204 }
205 }
206 }
207
Leon Scroggins III052ce662010-09-13 14:44:16 -0400208 @Override
209 public void onClick(View v) {
210 if (v == mButton) {
211 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400212 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700213 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400214 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700215 } else {
216 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400217 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400218 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700219 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400220 } else if (save()) {
221 finish();
222 }
223 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700224 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400225 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400226 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
227 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700228 } else {
229 finish();
230 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400231 } else if (v == mFolder) {
Leon Scroggins26318a12010-11-08 10:08:40 -0500232 PopupMenu popup = new PopupMenu(this, mFolder);
Leon Scroggins88d08032010-10-21 15:17:10 -0400233 popup.getMenuInflater().inflate(R.menu.folder_choice,
234 popup.getMenu());
235 popup.setOnMenuItemClickListener(this);
236 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400237 } else if (v == mAddNewFolder) {
238 mFolderNamer.setVisibility(View.VISIBLE);
239 mFolderNamer.setText(R.string.new_folder);
240 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400241 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700242 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400243 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100244 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400245 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800246 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400247 }
248
Leon Scroggins88d08032010-10-21 15:17:10 -0400249 @Override
250 public boolean onMenuItemClick(MenuItem item) {
251 switch(item.getItemId()) {
252 case R.id.bookmarks:
253 mCurrentFolder = getBookmarksBarId(this);
254 mFolder.setText(item.getTitle());
255 mSaveToHomeScreen = false;
256 break;
257 case R.id.home_screen:
258 // Create a short cut to the home screen
259 mSaveToHomeScreen = true;
260 mFolder.setText(item.getTitle());
261 break;
262 case R.id.other:
263 switchToFolderSelector();
264 break;
265 default:
266 return false;
267 }
268 return true;
269 }
270
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400271 // Refresh the ListView to hide or show the empty view, as necessary.
272 // Should be called after mFolderNamer is shown or hidden.
273 private void updateList() {
274 if (mAdapter.getCount() == 0) {
275 // XXX: Is there a better way to refresh the ListView?
276 mListView.setAdapter(mAdapter);
277 }
278 }
279
280 private void completeOrCancelFolderNaming(boolean cancel) {
281 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700282 String name = mFolderNamer.getText().toString();
283 long id = addFolderToCurrent(mFolderNamer.getText().toString());
284 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700285 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400286 mFolderNamer.setVisibility(View.GONE);
287 mAddNewFolder.setVisibility(View.VISIBLE);
288 mAddSeparator.setVisibility(View.VISIBLE);
289 getInputMethodManager().hideSoftInputFromWindow(
290 mFolderNamer.getWindowToken(), 0);
291 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700292 }
293
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700294 private long addFolderToCurrent(String name) {
295 // Add the folder to the database
296 ContentValues values = new ContentValues();
297 values.put(BrowserContract.Bookmarks.TITLE,
298 name);
299 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400300 long currentFolder;
301 Object data = mCrumbs.getTopData();
302 if (data != null) {
303 currentFolder = ((Folder) data).Id;
304 } else {
305 currentFolder = getBookmarksBarId(this);
306 }
307 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700308 Uri uri = getContentResolver().insert(
309 BrowserContract.Bookmarks.CONTENT_URI, values);
310 if (uri != null) {
311 return ContentUris.parseId(uri);
312 } else {
313 return -1;
314 }
315 }
316
Leon Scroggins III052ce662010-09-13 14:44:16 -0400317 private void switchToFolderSelector() {
318 mDefaultView.setVisibility(View.GONE);
319 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400320 mCrumbHolder.setVisibility(View.VISIBLE);
321 mFakeTitle.setVisibility(View.GONE);
322 mAddNewFolder.setVisibility(View.VISIBLE);
323 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400324 }
325
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700326 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700327 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400328 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400329 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700330 }
331 }
332
Leon Scroggins III052ce662010-09-13 14:44:16 -0400333 @Override
334 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
335 String[] projection;
336 switch (id) {
337 case LOADER_ID_ALL_FOLDERS:
338 projection = new String[] {
339 BrowserContract.Bookmarks._ID,
340 BrowserContract.Bookmarks.PARENT,
341 BrowserContract.Bookmarks.TITLE,
342 BrowserContract.Bookmarks.IS_FOLDER
343 };
344 return new CursorLoader(this,
345 BrowserContract.Bookmarks.CONTENT_URI,
346 projection,
347 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
348 null,
349 null);
350 case LOADER_ID_FOLDER_CONTENTS:
351 projection = new String[] {
352 BrowserContract.Bookmarks._ID,
353 BrowserContract.Bookmarks.TITLE,
354 BrowserContract.Bookmarks.IS_FOLDER
355 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400356 return new CursorLoader(this,
357 BrowserContract.Bookmarks.buildFolderUri(
358 mCurrentFolder),
359 projection,
360 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
361 null,
362 null);
363 default:
364 throw new AssertionError("Asking for nonexistant loader!");
365 }
366 }
367
368 @Override
369 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
370 switch (loader.getId()) {
371 case LOADER_ID_FOLDER_CONTENTS:
372 mAdapter.changeCursor(cursor);
373 break;
374 case LOADER_ID_ALL_FOLDERS:
375 long parent = mCurrentFolder;
376 int idIndex = cursor.getColumnIndexOrThrow(
377 BrowserContract.Bookmarks._ID);
378 int titleIndex = cursor.getColumnIndexOrThrow(
379 BrowserContract.Bookmarks.TITLE);
380 int parentIndex = cursor.getColumnIndexOrThrow(
381 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400382 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700383 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
384 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400385 // First, find the folder corresponding to the current
386 // folder
387 if (!cursor.moveToFirst()) {
388 throw new AssertionError("No folders in the database!");
389 }
390 long folder;
391 do {
392 folder = cursor.getLong(idIndex);
393 } while (folder != parent && cursor.moveToNext());
394 if (cursor.isAfterLast()) {
395 throw new AssertionError("Folder(id=" + parent
396 + ") holding this bookmark does not exist!");
397 }
398 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400399 if (parent == mCurrentFolder) {
400 mFolder.setText(name);
401 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400402 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400403 parent = cursor.getLong(parentIndex);
404 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400405 while (!folderStack.isEmpty()) {
406 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400407 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400408 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400409 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400410 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400411 break;
412 default:
413 break;
414 }
415 }
416
Leon Scroggins III052ce662010-09-13 14:44:16 -0400417 @Override
418 public void onItemClick(AdapterView<?> parent, View view, int position,
419 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400420 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400421 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400422 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400423 }
424
425 /**
426 * Shows a list of names of folders.
427 */
428 private class FolderAdapter extends CursorAdapter {
429 public FolderAdapter(Context context) {
430 super(context, null);
431 }
432
433 @Override
434 public void bindView(View view, Context context, Cursor cursor) {
435 ((TextView) view.findViewById(android.R.id.text1)).setText(
436 cursor.getString(cursor.getColumnIndexOrThrow(
437 BrowserContract.Bookmarks.TITLE)));
438 }
439
440 @Override
441 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700442 View view = LayoutInflater.from(context).inflate(
443 R.layout.folder_list_item, null);
444 view.setBackgroundDrawable(context.getResources().
445 getDrawable(android.R.drawable.list_selector_background));
446 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400447 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400448
449 @Override
450 public boolean isEmpty() {
451 // Do not show the empty view if the user is creating a new folder.
452 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
453 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400454 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800455
456 protected void onCreate(Bundle icicle) {
457 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400458 if (DEBUG_CRASH) {
459 requestWindowFeature(Window.FEATURE_NO_TITLE);
460 } else {
461 requestWindowFeature(Window.FEATURE_LEFT_ICON);
462 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100463
464 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100465
Leon Scroggins III052ce662010-09-13 14:44:16 -0400466 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100467
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400468 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400469 if (!DEBUG_CRASH) {
470 setTitle(R.string.bookmark_this_page);
471 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
472 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700473
The Android Open Source Project0c908882009-03-03 19:32:16 -0800474 String title = null;
475 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100476
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400477 mFakeTitle = (TextView) findViewById(R.id.fake_title);
478
The Android Open Source Project0c908882009-03-03 19:32:16 -0800479 if (mMap != null) {
480 Bundle b = mMap.getBundle("bookmark");
481 if (b != null) {
482 mMap = b;
483 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400484 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400485 if (!DEBUG_CRASH) {
486 setTitle(R.string.bookmark_this_page);
487 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400488 } else {
489 int gravity = mMap.getInt("gravity", -1);
490 if (gravity != -1) {
491 WindowManager.LayoutParams l = window.getAttributes();
492 l.gravity = gravity;
493 window.setAttributes(l);
494 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800495 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400496 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
497 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
498 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700499 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400500 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700501 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400502 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800503 }
504
505 mTitle = (EditText) findViewById(R.id.title);
506 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100507
Leon Scroggins III052ce662010-09-13 14:44:16 -0400508 mAddress = (EditText) findViewById(R.id.address);
509 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510
The Android Open Source Project0c908882009-03-03 19:32:16 -0800511 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400512 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800513
514 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400515 mCancelButton.setOnClickListener(this);
516
517 mFolder = (TextView) findViewById(R.id.folder);
518 mFolder.setOnClickListener(this);
519
520 mDefaultView = findViewById(R.id.default_view);
521 mFolderSelector = findViewById(R.id.folder_selector);
522
523 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
524 mFolderNamer.setOnEditorActionListener(this);
525
526 mAddNewFolder = findViewById(R.id.add_new_folder);
527 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400528 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400529
Leon Scroggins74dbe012010-10-15 10:54:27 -0400530 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
531 mCrumbs.setUseBackButton(true);
532 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400533 String name = getString(R.string.bookmarks);
534 mCrumbs.pushView(name, false,
535 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400536 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400537
Leon Scroggins III052ce662010-09-13 14:44:16 -0400538 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400539 mListView = (ListView) findViewById(R.id.list);
540 View empty = findViewById(R.id.empty);
541 mListView.setEmptyView(empty);
542 mListView.setAdapter(mAdapter);
543 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400544 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500545 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400546 // Find all the folders
547 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
548 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400549 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400550 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
551
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700552
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400553 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800554 mButton.requestFocus();
555 }
556 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100557
Leon Scroggins25230d72010-09-28 20:09:25 -0400558 // FIXME: Use a CursorLoader
559 private long getBookmarksBarId(Context context) {
560 SharedPreferences prefs
561 = PreferenceManager.getDefaultSharedPreferences(context);
562 String accountName =
563 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
564 String accountType =
565 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
566 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500567 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400568 }
569 Cursor cursor = null;
570 try {
571 cursor = context.getContentResolver().query(
572 BrowserContract.Bookmarks.CONTENT_URI,
573 new String[] { BrowserContract.Bookmarks._ID },
574 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
575 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
576 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
577 new String[] {
578 BrowserContract.ChromeSyncColumns
579 .FOLDER_NAME_BOOKMARKS_BAR,
580 accountName,
581 accountType },
582 null);
583 if (cursor != null && cursor.moveToFirst()) {
584 return cursor.getLong(0);
585 }
586 } finally {
587 if (cursor != null) cursor.close();
588 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500589 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400590 }
591
Leon Scroggins02065b02010-01-04 14:30:13 -0500592 /**
593 * Runnable to save a bookmark, so it can be performed in its own thread.
594 */
595 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400596 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500597 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200598 private Context mContext;
599 public SaveBookmarkRunnable(Context ctx, Message msg) {
600 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500601 mMessage = msg;
602 }
603 public void run() {
604 // Unbundle bookmark data.
605 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400606 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
607 String url = bundle.getString(BrowserContract.Bookmarks.URL);
608 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500609 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400610 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
611 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500612
613 // Save to the bookmarks DB.
614 try {
615 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400616 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
617 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500618 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200619 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500620 }
621 mMessage.arg1 = 1;
622 } catch (IllegalStateException e) {
623 mMessage.arg1 = 0;
624 }
625 mMessage.sendToTarget();
626 }
627 }
628
Ben Murdoch1794fe22009-09-29 18:14:30 +0100629 private void createHandler() {
630 if (mHandler == null) {
631 mHandler = new Handler() {
632 @Override
633 public void handleMessage(Message msg) {
634 switch (msg.what) {
635 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500636 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100637 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
638 Toast.LENGTH_LONG).show();
639 } else {
640 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
641 Toast.LENGTH_LONG).show();
642 }
643 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400644 case TOUCH_ICON_DOWNLOADED:
645 Bundle b = msg.getData();
646 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400647 AddBookmarkPage.this,
648 b.getString(BrowserContract.Bookmarks.URL),
649 b.getString(BrowserContract.Bookmarks.TITLE),
650 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
651 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400652 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100653 }
654 }
655 };
656 }
657 }
658
The Android Open Source Project0c908882009-03-03 19:32:16 -0800659 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100660 * Parse the data entered in the dialog and post a message to update the bookmarks database.
The Android Open Source Project0c908882009-03-03 19:32:16 -0800661 */
662 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100663 createHandler();
664
The Android Open Source Project0c908882009-03-03 19:32:16 -0800665 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100666 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400667 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100668
The Android Open Source Project0c908882009-03-03 19:32:16 -0800669 boolean emptyTitle = title.length() == 0;
670 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
671 Resources r = getResources();
672 if (emptyTitle || emptyUrl) {
673 if (emptyTitle) {
674 mTitle.setError(r.getText(R.string.bookmark_needs_title));
675 }
676 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400677 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800678 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400679 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100680
The Android Open Source Project0c908882009-03-03 19:32:16 -0800681 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000682 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400683 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000684 // We allow bookmarks with a javascript: scheme, but these will in most cases
685 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
686
687 if (!url.toLowerCase().startsWith("javascript:")) {
688 URI uriObj = new URI(url);
689 String scheme = uriObj.getScheme();
690 if (!Bookmarks.urlHasAcceptableScheme(url)) {
691 // If the scheme was non-null, let the user know that we
692 // can't save their bookmark. If it was null, we'll assume
693 // they meant http when we parse it in the WebAddress class.
694 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400695 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000696 return false;
697 }
698 WebAddress address;
699 try {
700 address = new WebAddress(unfilteredUrl);
701 } catch (ParseException e) {
702 throw new URISyntaxException("", "");
703 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100704 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000705 throw new URISyntaxException("", "");
706 }
707 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100708 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800709 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400710 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400711 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400712 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800713 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100714
Leon Scroggins88d08032010-10-21 15:17:10 -0400715 if (mSaveToHomeScreen) {
716 mEditingExisting = false;
717 }
718
719 boolean urlUnmodified = url.equals(mOriginalUrl);
720
Ben Murdoch1794fe22009-09-29 18:14:30 +0100721 if (mEditingExisting) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400722 mMap.putString(BrowserContract.Bookmarks.TITLE, title);
723 mMap.putString(BrowserContract.Bookmarks.URL, url);
724 mMap.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400725 // FIXME: This does not work yet
726 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100727 setResult(RESULT_OK, (new Intent()).setAction(
728 getIntent().toString()).putExtras(mMap));
729 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400730 Bitmap thumbnail;
731 Bitmap favicon;
732 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400733 thumbnail = (Bitmap) mMap.getParcelable(
734 BrowserContract.Bookmarks.THUMBNAIL);
735 favicon = (Bitmap) mMap.getParcelable(
736 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400737 } else {
738 thumbnail = null;
739 favicon = null;
740 }
741
Ben Murdoch1794fe22009-09-29 18:14:30 +0100742 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400743 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
744 bundle.putString(BrowserContract.Bookmarks.URL, url);
745 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400746
747 if (mSaveToHomeScreen) {
748 if (mTouchIconUrl != null && urlUnmodified) {
749 Message msg = Message.obtain(mHandler,
750 TOUCH_ICON_DOWNLOADED);
751 msg.setData(bundle);
752 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400753 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400754 icon.execute(mTouchIconUrl);
755 } else {
756 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
757 title, null /*touchIcon*/, favicon));
758 }
759 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400760 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
761 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
762 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400763 // Post a message to write to the DB.
764 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
765 msg.setData(bundle);
766 // Start a new thread so as to not slow down the UI
767 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
768 t.start();
769 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100770 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000771 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800772 }
773 return true;
774 }
Michael Kolb31829b92010-10-01 11:50:21 -0700775
The Android Open Source Project0c908882009-03-03 19:32:16 -0800776}