blob: 2b745b1480ddb0b5747bf195e3fbc7ca57f4d88a [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);
John Recke89daa92010-11-05 14:39:39 -0700300 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
301 String accountType = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
302 String accountName = prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
303 if (!TextUtils.isEmpty(accountName) && !TextUtils.isEmpty(accountType)) {
304 values.put(BrowserContract.Bookmarks.ACCOUNT_TYPE, accountType);
305 values.put(BrowserContract.Bookmarks.ACCOUNT_NAME, accountName);
306 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400307 long currentFolder;
308 Object data = mCrumbs.getTopData();
309 if (data != null) {
310 currentFolder = ((Folder) data).Id;
311 } else {
312 currentFolder = getBookmarksBarId(this);
313 }
314 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700315 Uri uri = getContentResolver().insert(
316 BrowserContract.Bookmarks.CONTENT_URI, values);
317 if (uri != null) {
318 return ContentUris.parseId(uri);
319 } else {
320 return -1;
321 }
322 }
323
Leon Scroggins III052ce662010-09-13 14:44:16 -0400324 private void switchToFolderSelector() {
325 mDefaultView.setVisibility(View.GONE);
326 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400327 mCrumbHolder.setVisibility(View.VISIBLE);
328 mFakeTitle.setVisibility(View.GONE);
329 mAddNewFolder.setVisibility(View.VISIBLE);
330 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400331 }
332
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700333 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700334 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400335 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400336 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700337 }
338 }
339
Leon Scroggins III052ce662010-09-13 14:44:16 -0400340 @Override
341 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
342 String[] projection;
343 switch (id) {
344 case LOADER_ID_ALL_FOLDERS:
345 projection = new String[] {
346 BrowserContract.Bookmarks._ID,
347 BrowserContract.Bookmarks.PARENT,
348 BrowserContract.Bookmarks.TITLE,
349 BrowserContract.Bookmarks.IS_FOLDER
350 };
351 return new CursorLoader(this,
352 BrowserContract.Bookmarks.CONTENT_URI,
353 projection,
354 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
355 null,
356 null);
357 case LOADER_ID_FOLDER_CONTENTS:
358 projection = new String[] {
359 BrowserContract.Bookmarks._ID,
360 BrowserContract.Bookmarks.TITLE,
361 BrowserContract.Bookmarks.IS_FOLDER
362 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400363 return new CursorLoader(this,
364 BrowserContract.Bookmarks.buildFolderUri(
365 mCurrentFolder),
366 projection,
367 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
368 null,
369 null);
370 default:
371 throw new AssertionError("Asking for nonexistant loader!");
372 }
373 }
374
375 @Override
376 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
377 switch (loader.getId()) {
378 case LOADER_ID_FOLDER_CONTENTS:
379 mAdapter.changeCursor(cursor);
380 break;
381 case LOADER_ID_ALL_FOLDERS:
382 long parent = mCurrentFolder;
383 int idIndex = cursor.getColumnIndexOrThrow(
384 BrowserContract.Bookmarks._ID);
385 int titleIndex = cursor.getColumnIndexOrThrow(
386 BrowserContract.Bookmarks.TITLE);
387 int parentIndex = cursor.getColumnIndexOrThrow(
388 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400389 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700390 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
391 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400392 // First, find the folder corresponding to the current
393 // folder
394 if (!cursor.moveToFirst()) {
395 throw new AssertionError("No folders in the database!");
396 }
397 long folder;
398 do {
399 folder = cursor.getLong(idIndex);
400 } while (folder != parent && cursor.moveToNext());
401 if (cursor.isAfterLast()) {
402 throw new AssertionError("Folder(id=" + parent
403 + ") holding this bookmark does not exist!");
404 }
405 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400406 if (parent == mCurrentFolder) {
407 mFolder.setText(name);
408 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400409 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400410 parent = cursor.getLong(parentIndex);
411 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400412 while (!folderStack.isEmpty()) {
413 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400414 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400415 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400416 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400417 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400418 break;
419 default:
420 break;
421 }
422 }
423
Leon Scroggins III052ce662010-09-13 14:44:16 -0400424 @Override
425 public void onItemClick(AdapterView<?> parent, View view, int position,
426 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400427 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400428 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400429 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400430 }
431
432 /**
433 * Shows a list of names of folders.
434 */
435 private class FolderAdapter extends CursorAdapter {
436 public FolderAdapter(Context context) {
437 super(context, null);
438 }
439
440 @Override
441 public void bindView(View view, Context context, Cursor cursor) {
442 ((TextView) view.findViewById(android.R.id.text1)).setText(
443 cursor.getString(cursor.getColumnIndexOrThrow(
444 BrowserContract.Bookmarks.TITLE)));
445 }
446
447 @Override
448 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700449 View view = LayoutInflater.from(context).inflate(
450 R.layout.folder_list_item, null);
451 view.setBackgroundDrawable(context.getResources().
452 getDrawable(android.R.drawable.list_selector_background));
453 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400454 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400455
456 @Override
457 public boolean isEmpty() {
458 // Do not show the empty view if the user is creating a new folder.
459 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
460 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400461 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800462
463 protected void onCreate(Bundle icicle) {
464 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400465 if (DEBUG_CRASH) {
466 requestWindowFeature(Window.FEATURE_NO_TITLE);
467 } else {
468 requestWindowFeature(Window.FEATURE_LEFT_ICON);
469 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100470
471 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100472
Leon Scroggins III052ce662010-09-13 14:44:16 -0400473 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100474
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400475 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400476 if (!DEBUG_CRASH) {
477 setTitle(R.string.bookmark_this_page);
478 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
479 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700480
The Android Open Source Project0c908882009-03-03 19:32:16 -0800481 String title = null;
482 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100483
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400484 mFakeTitle = (TextView) findViewById(R.id.fake_title);
485
The Android Open Source Project0c908882009-03-03 19:32:16 -0800486 if (mMap != null) {
487 Bundle b = mMap.getBundle("bookmark");
488 if (b != null) {
489 mMap = b;
490 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400491 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400492 if (!DEBUG_CRASH) {
493 setTitle(R.string.bookmark_this_page);
494 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400495 } else {
496 int gravity = mMap.getInt("gravity", -1);
497 if (gravity != -1) {
498 WindowManager.LayoutParams l = window.getAttributes();
499 l.gravity = gravity;
500 window.setAttributes(l);
501 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
Leon Scrogginsbc922852010-10-22 12:15:27 -0400503 title = mMap.getString(BrowserContract.Bookmarks.TITLE);
504 url = mOriginalUrl = mMap.getString(BrowserContract.Bookmarks.URL);
505 mTouchIconUrl = mMap.getString(TOUCH_ICON_URL);
Michael Kolb370a4f32010-10-06 10:45:32 -0700506 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400507 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700508 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400509 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 }
511
512 mTitle = (EditText) findViewById(R.id.title);
513 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100514
Leon Scroggins III052ce662010-09-13 14:44:16 -0400515 mAddress = (EditText) findViewById(R.id.address);
516 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800517
The Android Open Source Project0c908882009-03-03 19:32:16 -0800518 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400519 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800520
521 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400522 mCancelButton.setOnClickListener(this);
523
524 mFolder = (TextView) findViewById(R.id.folder);
525 mFolder.setOnClickListener(this);
526
527 mDefaultView = findViewById(R.id.default_view);
528 mFolderSelector = findViewById(R.id.folder_selector);
529
530 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
531 mFolderNamer.setOnEditorActionListener(this);
532
533 mAddNewFolder = findViewById(R.id.add_new_folder);
534 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400535 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400536
Leon Scroggins74dbe012010-10-15 10:54:27 -0400537 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
538 mCrumbs.setUseBackButton(true);
539 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400540 String name = getString(R.string.bookmarks);
541 mCrumbs.pushView(name, false,
542 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400543 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400544
Leon Scroggins III052ce662010-09-13 14:44:16 -0400545 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400546 mListView = (ListView) findViewById(R.id.list);
547 View empty = findViewById(R.id.empty);
548 mListView.setEmptyView(empty);
549 mListView.setAdapter(mAdapter);
550 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400551 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500552 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400553 // Find all the folders
554 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
555 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400556 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400557 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
558
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700559
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400560 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800561 mButton.requestFocus();
562 }
563 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100564
Leon Scroggins25230d72010-09-28 20:09:25 -0400565 // FIXME: Use a CursorLoader
566 private long getBookmarksBarId(Context context) {
567 SharedPreferences prefs
568 = PreferenceManager.getDefaultSharedPreferences(context);
569 String accountName =
570 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
571 String accountType =
572 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
573 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500574 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400575 }
576 Cursor cursor = null;
577 try {
578 cursor = context.getContentResolver().query(
579 BrowserContract.Bookmarks.CONTENT_URI,
580 new String[] { BrowserContract.Bookmarks._ID },
581 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
582 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
583 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
584 new String[] {
585 BrowserContract.ChromeSyncColumns
586 .FOLDER_NAME_BOOKMARKS_BAR,
587 accountName,
588 accountType },
589 null);
590 if (cursor != null && cursor.moveToFirst()) {
591 return cursor.getLong(0);
592 }
593 } finally {
594 if (cursor != null) cursor.close();
595 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500596 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400597 }
598
Leon Scroggins02065b02010-01-04 14:30:13 -0500599 /**
600 * Runnable to save a bookmark, so it can be performed in its own thread.
601 */
602 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400603 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500604 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200605 private Context mContext;
606 public SaveBookmarkRunnable(Context ctx, Message msg) {
607 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500608 mMessage = msg;
609 }
610 public void run() {
611 // Unbundle bookmark data.
612 Bundle bundle = mMessage.getData();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400613 String title = bundle.getString(BrowserContract.Bookmarks.TITLE);
614 String url = bundle.getString(BrowserContract.Bookmarks.URL);
615 boolean invalidateThumbnail = bundle.getBoolean(REMOVE_THUMBNAIL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500616 Bitmap thumbnail = invalidateThumbnail ? null
Leon Scrogginsbc922852010-10-22 12:15:27 -0400617 : (Bitmap) bundle.getParcelable(BrowserContract.Bookmarks.THUMBNAIL);
618 String touchIconUrl = bundle.getString(TOUCH_ICON_URL);
Leon Scroggins02065b02010-01-04 14:30:13 -0500619
620 // Save to the bookmarks DB.
621 try {
622 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400623 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
624 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500625 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200626 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500627 }
628 mMessage.arg1 = 1;
629 } catch (IllegalStateException e) {
630 mMessage.arg1 = 0;
631 }
632 mMessage.sendToTarget();
633 }
634 }
635
Ben Murdoch1794fe22009-09-29 18:14:30 +0100636 private void createHandler() {
637 if (mHandler == null) {
638 mHandler = new Handler() {
639 @Override
640 public void handleMessage(Message msg) {
641 switch (msg.what) {
642 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500643 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100644 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
645 Toast.LENGTH_LONG).show();
646 } else {
647 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
648 Toast.LENGTH_LONG).show();
649 }
650 break;
Leon Scroggins88d08032010-10-21 15:17:10 -0400651 case TOUCH_ICON_DOWNLOADED:
652 Bundle b = msg.getData();
653 sendBroadcast(BookmarkUtils.createAddToHomeIntent(
Leon Scrogginsbc922852010-10-22 12:15:27 -0400654 AddBookmarkPage.this,
655 b.getString(BrowserContract.Bookmarks.URL),
656 b.getString(BrowserContract.Bookmarks.TITLE),
657 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.TOUCH_ICON),
658 (Bitmap) b.getParcelable(BrowserContract.Bookmarks.FAVICON)));
Leon Scroggins88d08032010-10-21 15:17:10 -0400659 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100660 }
661 }
662 };
663 }
664 }
665
The Android Open Source Project0c908882009-03-03 19:32:16 -0800666 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100667 * 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 -0800668 */
669 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100670 createHandler();
671
The Android Open Source Project0c908882009-03-03 19:32:16 -0800672 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100673 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400674 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100675
The Android Open Source Project0c908882009-03-03 19:32:16 -0800676 boolean emptyTitle = title.length() == 0;
677 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
678 Resources r = getResources();
679 if (emptyTitle || emptyUrl) {
680 if (emptyTitle) {
681 mTitle.setError(r.getText(R.string.bookmark_needs_title));
682 }
683 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400684 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800685 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400686 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100687
The Android Open Source Project0c908882009-03-03 19:32:16 -0800688 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000689 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400690 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000691 // We allow bookmarks with a javascript: scheme, but these will in most cases
692 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
693
694 if (!url.toLowerCase().startsWith("javascript:")) {
695 URI uriObj = new URI(url);
696 String scheme = uriObj.getScheme();
697 if (!Bookmarks.urlHasAcceptableScheme(url)) {
698 // If the scheme was non-null, let the user know that we
699 // can't save their bookmark. If it was null, we'll assume
700 // they meant http when we parse it in the WebAddress class.
701 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400702 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000703 return false;
704 }
705 WebAddress address;
706 try {
707 address = new WebAddress(unfilteredUrl);
708 } catch (ParseException e) {
709 throw new URISyntaxException("", "");
710 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100711 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000712 throw new URISyntaxException("", "");
713 }
714 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100715 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800716 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400717 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400718 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400719 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800720 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100721
Leon Scroggins88d08032010-10-21 15:17:10 -0400722 if (mSaveToHomeScreen) {
723 mEditingExisting = false;
724 }
725
726 boolean urlUnmodified = url.equals(mOriginalUrl);
727
Ben Murdoch1794fe22009-09-29 18:14:30 +0100728 if (mEditingExisting) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400729 mMap.putString(BrowserContract.Bookmarks.TITLE, title);
730 mMap.putString(BrowserContract.Bookmarks.URL, url);
731 mMap.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400732 // FIXME: This does not work yet
733 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100734 setResult(RESULT_OK, (new Intent()).setAction(
735 getIntent().toString()).putExtras(mMap));
736 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400737 Bitmap thumbnail;
738 Bitmap favicon;
739 if (urlUnmodified) {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400740 thumbnail = (Bitmap) mMap.getParcelable(
741 BrowserContract.Bookmarks.THUMBNAIL);
742 favicon = (Bitmap) mMap.getParcelable(
743 BrowserContract.Bookmarks.FAVICON);
Leon Scroggins88d08032010-10-21 15:17:10 -0400744 } else {
745 thumbnail = null;
746 favicon = null;
747 }
748
Ben Murdoch1794fe22009-09-29 18:14:30 +0100749 Bundle bundle = new Bundle();
Leon Scrogginsbc922852010-10-22 12:15:27 -0400750 bundle.putString(BrowserContract.Bookmarks.TITLE, title);
751 bundle.putString(BrowserContract.Bookmarks.URL, url);
752 bundle.putParcelable(BrowserContract.Bookmarks.FAVICON, favicon);
Leon Scroggins88d08032010-10-21 15:17:10 -0400753
754 if (mSaveToHomeScreen) {
755 if (mTouchIconUrl != null && urlUnmodified) {
756 Message msg = Message.obtain(mHandler,
757 TOUCH_ICON_DOWNLOADED);
758 msg.setData(bundle);
759 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
Leon Scrogginsbc922852010-10-22 12:15:27 -0400760 mMap.getString(USER_AGENT));
Leon Scroggins88d08032010-10-21 15:17:10 -0400761 icon.execute(mTouchIconUrl);
762 } else {
763 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
764 title, null /*touchIcon*/, favicon));
765 }
766 } else {
Leon Scrogginsbc922852010-10-22 12:15:27 -0400767 bundle.putParcelable(BrowserContract.Bookmarks.THUMBNAIL, thumbnail);
768 bundle.putBoolean(REMOVE_THUMBNAIL, !urlUnmodified);
769 bundle.putString(TOUCH_ICON_URL, mTouchIconUrl);
Leon Scroggins88d08032010-10-21 15:17:10 -0400770 // Post a message to write to the DB.
771 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
772 msg.setData(bundle);
773 // Start a new thread so as to not slow down the UI
774 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
775 t.start();
776 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100777 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000778 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800779 }
780 return true;
781 }
Michael Kolb31829b92010-10-01 11:50:21 -0700782
The Android Open Source Project0c908882009-03-03 19:32:16 -0800783}