blob: fc480eefd752d336a0978a1a6f1ec20f900ff7b0 [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;
71
Leon Scroggins74dbe012010-10-15 10:54:27 -040072 private static final int MAX_CRUMBS_SHOWN = 2;
73
The Android Open Source Project0c908882009-03-03 19:32:16 -080074 private final String LOGTAG = "Bookmarks";
Leon Scroggins74dbe012010-10-15 10:54:27 -040075 // Set to true to see the crash on the code I would like to run.
76 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080077
Leon Scroggins III052ce662010-09-13 14:44:16 -040078 // IDs for the CursorLoaders that are used.
79 private final int LOADER_ID_FOLDER_CONTENTS = 0;
80 private final int LOADER_ID_ALL_FOLDERS = 1;
81
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 private EditText mTitle;
83 private EditText mAddress;
84 private TextView mButton;
85 private View mCancelButton;
86 private boolean mEditingExisting;
87 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040088 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010089 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040090 private TextView mFolder;
91 private View mDefaultView;
92 private View mFolderSelector;
93 private EditText mFolderNamer;
94 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040095 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -040096 private long mCurrentFolder = 0;
97 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -040098 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040099 private TextView mFakeTitle;
100 private View mCrumbHolder;
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400101 private ListView mListView;
Leon Scroggins88d08032010-10-21 15:17:10 -0400102 private boolean mSaveToHomeScreen;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400103
104 private static class Folder {
105 String Name;
106 long Id;
107 Folder(String name, long id) {
108 Name = name;
109 Id = id;
110 }
111 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800112
Ben Murdoch1794fe22009-09-29 18:14:30 +0100113 // Message IDs
114 private static final int SAVE_BOOKMARK = 100;
Leon Scroggins88d08032010-10-21 15:17:10 -0400115 private static final int TOUCH_ICON_DOWNLOADED = 101;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100116
117 private Handler mHandler;
118
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100119 private InputMethodManager getInputMethodManager() {
120 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
121 }
122
Leon Scroggins III052ce662010-09-13 14:44:16 -0400123 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400124 public void onTop(int level, Object data) {
125 if (null == data) return;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400126 Folder folderData = (Folder) data;
127 long folder = folderData.Id;
128 Uri uri = BrowserContract.Bookmarks.buildFolderUri(folder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400129 LoaderManager manager = getLoaderManager();
130 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
131 LOADER_ID_FOLDER_CONTENTS));
132 loader.setUri(uri);
133 loader.forceLoad();
134 updateVisible();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400135 if (mFolderNamer.getVisibility() == View.VISIBLE) {
136 completeOrCancelFolderNaming(true);
137 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400138 }
139
140 /**
141 * Update the views shown to only show the two deepest levels of crumbs.
142 * Note that this method depends on internal knowledge of BreadCrumbView.
143 */
144 private void updateVisible() {
145 if (MAX_CRUMBS_SHOWN > 0) {
146 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
147 // This class always uses a back button, which is the first child.
148 int childIndex = 1;
149 if (invisibleCrumbs > 0) {
150 int crumbIndex = 0;
151 while (crumbIndex < invisibleCrumbs) {
152 // Set the crumb to GONE.
153 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
154 childIndex++;
155 // Each crumb is followed by a separator (except the last
156 // one). Also make it GONE
157 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
158 childIndex++;
159 // Move to the next crumb.
160 crumbIndex++;
161 }
162 }
163 // Make sure the last two are visible.
164 int childCount = mCrumbs.getChildCount();
165 while (childIndex < childCount) {
166 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
167 childIndex++;
168 }
169 }
170 }
171
172 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400173 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
174 if (v == mFolderNamer) {
175 if (v.getText().length() > 0) {
176 if (actionId == EditorInfo.IME_NULL) {
177 // Only want to do this once.
178 if (event.getAction() == KeyEvent.ACTION_UP) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400179 completeOrCancelFolderNaming(false);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400180 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400181 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800182 }
Michael Kolb31829b92010-10-01 11:50:21 -0700183 // Steal the key press; otherwise a newline will be added
184 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800185 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400186 return false;
187 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800188
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400189 private void switchToDefaultView(boolean changedFolder) {
190 mFolderSelector.setVisibility(View.GONE);
191 mDefaultView.setVisibility(View.VISIBLE);
192 mCrumbHolder.setVisibility(View.GONE);
193 mFakeTitle.setVisibility(View.VISIBLE);
194 if (changedFolder) {
195 Object data = mCrumbs.getTopData();
196 if (data != null) {
197 Folder folder = (Folder) data;
198 mCurrentFolder = folder.Id;
199 mFolder.setText(folder.Name);
200 }
201 }
202 }
203
Leon Scroggins III052ce662010-09-13 14:44:16 -0400204 @Override
205 public void onClick(View v) {
206 if (v == mButton) {
207 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400208 // We are showing the folder selector.
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700209 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400210 completeOrCancelFolderNaming(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700211 } else {
212 // User has selected a folder. Go back to the opening page
Leon Scroggins88d08032010-10-21 15:17:10 -0400213 mSaveToHomeScreen = false;
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400214 switchToDefaultView(true);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700215 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400216 } else if (save()) {
217 finish();
218 }
219 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700220 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400221 completeOrCancelFolderNaming(true);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400222 } else if (mFolderSelector.getVisibility() == View.VISIBLE) {
223 switchToDefaultView(false);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700224 } else {
225 finish();
226 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400227 } else if (v == mFolder) {
Leon Scroggins88d08032010-10-21 15:17:10 -0400228 // FIXME: We want to use mFolder as an anchor, but cannot until we
229 // fix the issue that the PopupMenu will not extend past the edge of
230 // the dialog.
231 PopupMenu popup = new PopupMenu(this, mFakeTitle);
232 popup.getMenuInflater().inflate(R.menu.folder_choice,
233 popup.getMenu());
234 popup.setOnMenuItemClickListener(this);
235 popup.show();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400236 } else if (v == mAddNewFolder) {
237 mFolderNamer.setVisibility(View.VISIBLE);
238 mFolderNamer.setText(R.string.new_folder);
239 mFolderNamer.requestFocus();
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400240 updateList();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700241 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400242 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100243 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400244 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800245 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400246 }
247
Leon Scroggins88d08032010-10-21 15:17:10 -0400248 @Override
249 public boolean onMenuItemClick(MenuItem item) {
250 switch(item.getItemId()) {
251 case R.id.bookmarks:
252 mCurrentFolder = getBookmarksBarId(this);
253 mFolder.setText(item.getTitle());
254 mSaveToHomeScreen = false;
255 break;
256 case R.id.home_screen:
257 // Create a short cut to the home screen
258 mSaveToHomeScreen = true;
259 mFolder.setText(item.getTitle());
260 break;
261 case R.id.other:
262 switchToFolderSelector();
263 break;
264 default:
265 return false;
266 }
267 return true;
268 }
269
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400270 // Refresh the ListView to hide or show the empty view, as necessary.
271 // Should be called after mFolderNamer is shown or hidden.
272 private void updateList() {
273 if (mAdapter.getCount() == 0) {
274 // XXX: Is there a better way to refresh the ListView?
275 mListView.setAdapter(mAdapter);
276 }
277 }
278
279 private void completeOrCancelFolderNaming(boolean cancel) {
280 if (!cancel && !TextUtils.isEmpty(mFolderNamer.getText())) {
Michael Kolb31829b92010-10-01 11:50:21 -0700281 String name = mFolderNamer.getText().toString();
282 long id = addFolderToCurrent(mFolderNamer.getText().toString());
283 descendInto(name, id);
Michael Kolb31829b92010-10-01 11:50:21 -0700284 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400285 mFolderNamer.setVisibility(View.GONE);
286 mAddNewFolder.setVisibility(View.VISIBLE);
287 mAddSeparator.setVisibility(View.VISIBLE);
288 getInputMethodManager().hideSoftInputFromWindow(
289 mFolderNamer.getWindowToken(), 0);
290 updateList();
Michael Kolb31829b92010-10-01 11:50:21 -0700291 }
292
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700293 private long addFolderToCurrent(String name) {
294 // Add the folder to the database
295 ContentValues values = new ContentValues();
296 values.put(BrowserContract.Bookmarks.TITLE,
297 name);
298 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400299 long currentFolder;
300 Object data = mCrumbs.getTopData();
301 if (data != null) {
302 currentFolder = ((Folder) data).Id;
303 } else {
304 currentFolder = getBookmarksBarId(this);
305 }
306 values.put(BrowserContract.Bookmarks.PARENT, currentFolder);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700307 Uri uri = getContentResolver().insert(
308 BrowserContract.Bookmarks.CONTENT_URI, values);
309 if (uri != null) {
310 return ContentUris.parseId(uri);
311 } else {
312 return -1;
313 }
314 }
315
Leon Scroggins III052ce662010-09-13 14:44:16 -0400316 private void switchToFolderSelector() {
317 mDefaultView.setVisibility(View.GONE);
318 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400319 mCrumbHolder.setVisibility(View.VISIBLE);
320 mFakeTitle.setVisibility(View.GONE);
321 mAddNewFolder.setVisibility(View.VISIBLE);
322 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400323 }
324
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700325 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700326 if (id != DEFAULT_FOLDER_ID) {
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400327 mCrumbs.pushView(foldername, new Folder(foldername, id));
Leon Scroggins74dbe012010-10-15 10:54:27 -0400328 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700329 }
330 }
331
Leon Scroggins III052ce662010-09-13 14:44:16 -0400332 @Override
333 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
334 String[] projection;
335 switch (id) {
336 case LOADER_ID_ALL_FOLDERS:
337 projection = new String[] {
338 BrowserContract.Bookmarks._ID,
339 BrowserContract.Bookmarks.PARENT,
340 BrowserContract.Bookmarks.TITLE,
341 BrowserContract.Bookmarks.IS_FOLDER
342 };
343 return new CursorLoader(this,
344 BrowserContract.Bookmarks.CONTENT_URI,
345 projection,
346 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
347 null,
348 null);
349 case LOADER_ID_FOLDER_CONTENTS:
350 projection = new String[] {
351 BrowserContract.Bookmarks._ID,
352 BrowserContract.Bookmarks.TITLE,
353 BrowserContract.Bookmarks.IS_FOLDER
354 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400355 return new CursorLoader(this,
356 BrowserContract.Bookmarks.buildFolderUri(
357 mCurrentFolder),
358 projection,
359 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
360 null,
361 null);
362 default:
363 throw new AssertionError("Asking for nonexistant loader!");
364 }
365 }
366
367 @Override
368 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
369 switch (loader.getId()) {
370 case LOADER_ID_FOLDER_CONTENTS:
371 mAdapter.changeCursor(cursor);
372 break;
373 case LOADER_ID_ALL_FOLDERS:
374 long parent = mCurrentFolder;
375 int idIndex = cursor.getColumnIndexOrThrow(
376 BrowserContract.Bookmarks._ID);
377 int titleIndex = cursor.getColumnIndexOrThrow(
378 BrowserContract.Bookmarks.TITLE);
379 int parentIndex = cursor.getColumnIndexOrThrow(
380 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400381 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700382 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
383 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400384 // First, find the folder corresponding to the current
385 // folder
386 if (!cursor.moveToFirst()) {
387 throw new AssertionError("No folders in the database!");
388 }
389 long folder;
390 do {
391 folder = cursor.getLong(idIndex);
392 } while (folder != parent && cursor.moveToNext());
393 if (cursor.isAfterLast()) {
394 throw new AssertionError("Folder(id=" + parent
395 + ") holding this bookmark does not exist!");
396 }
397 String name = cursor.getString(titleIndex);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400398 if (parent == mCurrentFolder) {
399 mFolder.setText(name);
400 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400401 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400402 parent = cursor.getLong(parentIndex);
403 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400404 while (!folderStack.isEmpty()) {
405 Folder thisFolder = (Folder) folderStack.pop();
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400406 mCrumbs.pushView(thisFolder.Name, thisFolder);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400407 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400408 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400409 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400410 break;
411 default:
412 break;
413 }
414 }
415
Leon Scroggins III052ce662010-09-13 14:44:16 -0400416 @Override
417 public void onItemClick(AdapterView<?> parent, View view, int position,
418 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400419 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400420 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400421 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400422 }
423
424 /**
425 * Shows a list of names of folders.
426 */
427 private class FolderAdapter extends CursorAdapter {
428 public FolderAdapter(Context context) {
429 super(context, null);
430 }
431
432 @Override
433 public void bindView(View view, Context context, Cursor cursor) {
434 ((TextView) view.findViewById(android.R.id.text1)).setText(
435 cursor.getString(cursor.getColumnIndexOrThrow(
436 BrowserContract.Bookmarks.TITLE)));
437 }
438
439 @Override
440 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700441 View view = LayoutInflater.from(context).inflate(
442 R.layout.folder_list_item, null);
443 view.setBackgroundDrawable(context.getResources().
444 getDrawable(android.R.drawable.list_selector_background));
445 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400446 }
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400447
448 @Override
449 public boolean isEmpty() {
450 // Do not show the empty view if the user is creating a new folder.
451 return super.isEmpty() && mFolderNamer.getVisibility() == View.GONE;
452 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400453 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800454
455 protected void onCreate(Bundle icicle) {
456 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400457 if (DEBUG_CRASH) {
458 requestWindowFeature(Window.FEATURE_NO_TITLE);
459 } else {
460 requestWindowFeature(Window.FEATURE_LEFT_ICON);
461 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100462
463 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100464
Leon Scroggins III052ce662010-09-13 14:44:16 -0400465 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100466
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400467 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400468 if (!DEBUG_CRASH) {
469 setTitle(R.string.bookmark_this_page);
470 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
471 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700472
The Android Open Source Project0c908882009-03-03 19:32:16 -0800473 String title = null;
474 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100475
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400476 mFakeTitle = (TextView) findViewById(R.id.fake_title);
477
The Android Open Source Project0c908882009-03-03 19:32:16 -0800478 if (mMap != null) {
479 Bundle b = mMap.getBundle("bookmark");
480 if (b != null) {
481 mMap = b;
482 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400483 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400484 if (!DEBUG_CRASH) {
485 setTitle(R.string.bookmark_this_page);
486 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400487 } else {
488 int gravity = mMap.getInt("gravity", -1);
489 if (gravity != -1) {
490 WindowManager.LayoutParams l = window.getAttributes();
491 l.gravity = gravity;
492 window.setAttributes(l);
493 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800494 }
495 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100496 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400497 mTouchIconUrl = mMap.getString("touch_icon_url");
Michael Kolb370a4f32010-10-06 10:45:32 -0700498 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400499 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700500 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400501 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800502 }
503
504 mTitle = (EditText) findViewById(R.id.title);
505 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100506
Leon Scroggins III052ce662010-09-13 14:44:16 -0400507 mAddress = (EditText) findViewById(R.id.address);
508 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800509
The Android Open Source Project0c908882009-03-03 19:32:16 -0800510 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400511 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800512
513 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400514 mCancelButton.setOnClickListener(this);
515
516 mFolder = (TextView) findViewById(R.id.folder);
517 mFolder.setOnClickListener(this);
518
519 mDefaultView = findViewById(R.id.default_view);
520 mFolderSelector = findViewById(R.id.folder_selector);
521
522 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
523 mFolderNamer.setOnEditorActionListener(this);
524
525 mAddNewFolder = findViewById(R.id.add_new_folder);
526 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400527 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400528
Leon Scroggins74dbe012010-10-15 10:54:27 -0400529 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
530 mCrumbs.setUseBackButton(true);
531 mCrumbs.setController(this);
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400532 String name = getString(R.string.bookmarks);
533 mCrumbs.pushView(name, false,
534 new Folder(name, BrowserProvider2.FIXED_ID_ROOT));
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400535 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400536
Leon Scroggins III052ce662010-09-13 14:44:16 -0400537 mAdapter = new FolderAdapter(this);
Leon Scroggins6573f9e2010-10-18 17:22:35 -0400538 mListView = (ListView) findViewById(R.id.list);
539 View empty = findViewById(R.id.empty);
540 mListView.setEmptyView(empty);
541 mListView.setAdapter(mAdapter);
542 mListView.setOnItemClickListener(this);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400543 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500544 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400545 // Find all the folders
546 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
547 }
Leon Scroggins52f7daa2010-10-18 16:57:46 -0400548 // Find the contents of the current folder
Leon Scroggins III052ce662010-09-13 14:44:16 -0400549 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
550
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700551
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400552 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800553 mButton.requestFocus();
554 }
555 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100556
Leon Scroggins25230d72010-09-28 20:09:25 -0400557 // FIXME: Use a CursorLoader
558 private long getBookmarksBarId(Context context) {
559 SharedPreferences prefs
560 = PreferenceManager.getDefaultSharedPreferences(context);
561 String accountName =
562 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
563 String accountType =
564 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
565 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500566 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400567 }
568 Cursor cursor = null;
569 try {
570 cursor = context.getContentResolver().query(
571 BrowserContract.Bookmarks.CONTENT_URI,
572 new String[] { BrowserContract.Bookmarks._ID },
573 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
574 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
575 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
576 new String[] {
577 BrowserContract.ChromeSyncColumns
578 .FOLDER_NAME_BOOKMARKS_BAR,
579 accountName,
580 accountType },
581 null);
582 if (cursor != null && cursor.moveToFirst()) {
583 return cursor.getLong(0);
584 }
585 } finally {
586 if (cursor != null) cursor.close();
587 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500588 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400589 }
590
Leon Scroggins02065b02010-01-04 14:30:13 -0500591 /**
592 * Runnable to save a bookmark, so it can be performed in its own thread.
593 */
594 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400595 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500596 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200597 private Context mContext;
598 public SaveBookmarkRunnable(Context ctx, Message msg) {
599 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500600 mMessage = msg;
601 }
602 public void run() {
603 // Unbundle bookmark data.
604 Bundle bundle = mMessage.getData();
605 String title = bundle.getString("title");
606 String url = bundle.getString("url");
607 boolean invalidateThumbnail = bundle.getBoolean(
608 "invalidateThumbnail");
609 Bitmap thumbnail = invalidateThumbnail ? null
610 : (Bitmap) bundle.getParcelable("thumbnail");
611 String touchIconUrl = bundle.getString("touchIconUrl");
612
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(
647 AddBookmarkPage.this, b.getString("url"),
648 b.getString("title"),
649 (Bitmap) b.getParcelable("touchIcon"),
650 (Bitmap) b.getParcelable("favicon")));
651 break;
Ben Murdoch1794fe22009-09-29 18:14:30 +0100652 }
653 }
654 };
655 }
656 }
657
The Android Open Source Project0c908882009-03-03 19:32:16 -0800658 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100659 * 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 -0800660 */
661 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100662 createHandler();
663
The Android Open Source Project0c908882009-03-03 19:32:16 -0800664 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100665 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400666 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100667
The Android Open Source Project0c908882009-03-03 19:32:16 -0800668 boolean emptyTitle = title.length() == 0;
669 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
670 Resources r = getResources();
671 if (emptyTitle || emptyUrl) {
672 if (emptyTitle) {
673 mTitle.setError(r.getText(R.string.bookmark_needs_title));
674 }
675 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400676 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800677 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400678 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100679
The Android Open Source Project0c908882009-03-03 19:32:16 -0800680 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000681 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400682 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000683 // We allow bookmarks with a javascript: scheme, but these will in most cases
684 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
685
686 if (!url.toLowerCase().startsWith("javascript:")) {
687 URI uriObj = new URI(url);
688 String scheme = uriObj.getScheme();
689 if (!Bookmarks.urlHasAcceptableScheme(url)) {
690 // If the scheme was non-null, let the user know that we
691 // can't save their bookmark. If it was null, we'll assume
692 // they meant http when we parse it in the WebAddress class.
693 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400694 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000695 return false;
696 }
697 WebAddress address;
698 try {
699 address = new WebAddress(unfilteredUrl);
700 } catch (ParseException e) {
701 throw new URISyntaxException("", "");
702 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100703 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000704 throw new URISyntaxException("", "");
705 }
706 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100707 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800708 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400709 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400710 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400711 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800712 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100713
Leon Scroggins88d08032010-10-21 15:17:10 -0400714 if (mSaveToHomeScreen) {
715 mEditingExisting = false;
716 }
717
718 boolean urlUnmodified = url.equals(mOriginalUrl);
719
Ben Murdoch1794fe22009-09-29 18:14:30 +0100720 if (mEditingExisting) {
721 mMap.putString("title", title);
722 mMap.putString("url", url);
Leon Scroggins88d08032010-10-21 15:17:10 -0400723 mMap.putBoolean("invalidateThumbnail", !urlUnmodified);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400724 // FIXME: This does not work yet
725 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100726 setResult(RESULT_OK, (new Intent()).setAction(
727 getIntent().toString()).putExtras(mMap));
728 } else {
Leon Scroggins88d08032010-10-21 15:17:10 -0400729 Bitmap thumbnail;
730 Bitmap favicon;
731 if (urlUnmodified) {
732 thumbnail = (Bitmap) mMap.getParcelable("thumbnail");
733 favicon = (Bitmap) mMap.getParcelable("favicon");
734 } else {
735 thumbnail = null;
736 favicon = null;
737 }
738
Ben Murdoch1794fe22009-09-29 18:14:30 +0100739 Bundle bundle = new Bundle();
740 bundle.putString("title", title);
741 bundle.putString("url", url);
Leon Scroggins88d08032010-10-21 15:17:10 -0400742 bundle.putParcelable("favicon", favicon);
743
744 if (mSaveToHomeScreen) {
745 if (mTouchIconUrl != null && urlUnmodified) {
746 Message msg = Message.obtain(mHandler,
747 TOUCH_ICON_DOWNLOADED);
748 msg.setData(bundle);
749 DownloadTouchIcon icon = new DownloadTouchIcon(this, msg,
750 mMap.getString("user_agent"));
751 icon.execute(mTouchIconUrl);
752 } else {
753 sendBroadcast(BookmarkUtils.createAddToHomeIntent(this, url,
754 title, null /*touchIcon*/, favicon));
755 }
756 } else {
757 bundle.putParcelable("thumbnail", thumbnail);
758 bundle.putBoolean("invalidateThumbnail", !urlUnmodified);
759 bundle.putString("touchIconUrl", mTouchIconUrl);
760 // Post a message to write to the DB.
761 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
762 msg.setData(bundle);
763 // Start a new thread so as to not slow down the UI
764 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
765 t.start();
766 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100767 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000768 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800769 }
770 return true;
771 }
Michael Kolb31829b92010-10-01 11:50:21 -0700772
The Android Open Source Project0c908882009-03-03 19:32:16 -0800773}