blob: c230858668285454e569037a44c0cf0aa6281f3e [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045import android.view.View;
Leon Scroggins III052ce662010-09-13 14:44:16 -040046import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080047import android.view.Window;
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -040048import android.view.WindowManager;
Leon Scroggins III052ce662010-09-13 14:44:16 -040049import android.view.inputmethod.EditorInfo;
50import android.view.inputmethod.InputMethodManager;
51import android.widget.AdapterView;
52import android.widget.CursorAdapter;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053import android.widget.EditText;
Leon Scroggins III052ce662010-09-13 14:44:16 -040054import android.widget.ListView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080055import android.widget.TextView;
56import android.widget.Toast;
57
Cary Clarkf2407c62009-09-04 12:25:10 -040058import java.net.URI;
59import java.net.URISyntaxException;
Leon Scroggins III052ce662010-09-13 14:44:16 -040060import java.util.ArrayList;
Leon Scroggins74dbe012010-10-15 10:54:27 -040061import java.util.Stack;
Leon Scroggins III052ce662010-09-13 14:44:16 -040062
63public class AddBookmarkPage extends Activity
64 implements View.OnClickListener, TextView.OnEditorActionListener,
Leon Scroggins74dbe012010-10-15 10:54:27 -040065 AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor>,
66 BreadCrumbView.Controller {
The Android Open Source Project0c908882009-03-03 19:32:16 -080067
Michael Kolb370a4f32010-10-06 10:45:32 -070068 public static final long DEFAULT_FOLDER_ID = -1;
69
Leon Scroggins74dbe012010-10-15 10:54:27 -040070 private static final int MAX_CRUMBS_SHOWN = 2;
71
The Android Open Source Project0c908882009-03-03 19:32:16 -080072 private final String LOGTAG = "Bookmarks";
Leon Scroggins74dbe012010-10-15 10:54:27 -040073 // Set to true to see the crash on the code I would like to run.
74 private final boolean DEBUG_CRASH = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080075
Leon Scroggins III052ce662010-09-13 14:44:16 -040076 // IDs for the CursorLoaders that are used.
77 private final int LOADER_ID_FOLDER_CONTENTS = 0;
78 private final int LOADER_ID_ALL_FOLDERS = 1;
79
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 private EditText mTitle;
81 private EditText mAddress;
82 private TextView mButton;
83 private View mCancelButton;
84 private boolean mEditingExisting;
85 private Bundle mMap;
Patrick Scott3918d442009-08-04 13:22:29 -040086 private String mTouchIconUrl;
Ben Murdochaac7aa62009-09-17 16:57:40 +010087 private Bitmap mThumbnail;
88 private String mOriginalUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -040089 private TextView mFolder;
90 private View mDefaultView;
91 private View mFolderSelector;
92 private EditText mFolderNamer;
93 private View mAddNewFolder;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040094 private View mAddSeparator;
Leon Scroggins III052ce662010-09-13 14:44:16 -040095 private long mCurrentFolder = 0;
96 private FolderAdapter mAdapter;
Leon Scroggins74dbe012010-10-15 10:54:27 -040097 private BreadCrumbView mCrumbs;
Leon Scroggins7e5f7352010-10-18 13:25:31 -040098 private TextView mFakeTitle;
99 private View mCrumbHolder;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400100
101 private static class Folder {
102 String Name;
103 long Id;
104 Folder(String name, long id) {
105 Name = name;
106 Id = id;
107 }
108 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109
Ben Murdoch1794fe22009-09-29 18:14:30 +0100110 // Message IDs
111 private static final int SAVE_BOOKMARK = 100;
112
113 private Handler mHandler;
114
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100115 private InputMethodManager getInputMethodManager() {
116 return (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
117 }
118
Leon Scroggins III052ce662010-09-13 14:44:16 -0400119 @Override
Leon Scroggins74dbe012010-10-15 10:54:27 -0400120 public void onTop(int level, Object data) {
121 if (null == data) return;
122 mCurrentFolder = (Long) data;
123 Uri uri = BrowserContract.Bookmarks.buildFolderUri(mCurrentFolder);
124 LoaderManager manager = getLoaderManager();
125 CursorLoader loader = (CursorLoader) ((Loader) manager.getLoader(
126 LOADER_ID_FOLDER_CONTENTS));
127 loader.setUri(uri);
128 loader.forceLoad();
129 updateVisible();
130 }
131
132 /**
133 * Update the views shown to only show the two deepest levels of crumbs.
134 * Note that this method depends on internal knowledge of BreadCrumbView.
135 */
136 private void updateVisible() {
137 if (MAX_CRUMBS_SHOWN > 0) {
138 int invisibleCrumbs = mCrumbs.size() - MAX_CRUMBS_SHOWN;
139 // This class always uses a back button, which is the first child.
140 int childIndex = 1;
141 if (invisibleCrumbs > 0) {
142 int crumbIndex = 0;
143 while (crumbIndex < invisibleCrumbs) {
144 // Set the crumb to GONE.
145 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
146 childIndex++;
147 // Each crumb is followed by a separator (except the last
148 // one). Also make it GONE
149 mCrumbs.getChildAt(childIndex).setVisibility(View.GONE);
150 childIndex++;
151 // Move to the next crumb.
152 crumbIndex++;
153 }
154 }
155 // Make sure the last two are visible.
156 int childCount = mCrumbs.getChildCount();
157 while (childIndex < childCount) {
158 mCrumbs.getChildAt(childIndex).setVisibility(View.VISIBLE);
159 childIndex++;
160 }
161 }
162 }
163
164 @Override
Leon Scroggins III052ce662010-09-13 14:44:16 -0400165 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
166 if (v == mFolderNamer) {
167 if (v.getText().length() > 0) {
168 if (actionId == EditorInfo.IME_NULL) {
169 // Only want to do this once.
170 if (event.getAction() == KeyEvent.ACTION_UP) {
Michael Kolb31829b92010-10-01 11:50:21 -0700171 completeFolderNaming();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400172 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400173 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800174 }
Michael Kolb31829b92010-10-01 11:50:21 -0700175 // Steal the key press; otherwise a newline will be added
176 return true;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800177 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400178 return false;
179 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800180
Leon Scroggins III052ce662010-09-13 14:44:16 -0400181 @Override
182 public void onClick(View v) {
183 if (v == mButton) {
184 if (mFolderSelector.getVisibility() == View.VISIBLE) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700185 // We are showing the folder selector.
186 if (mFolderNamer.getVisibility() == View.VISIBLE) {
Michael Kolb31829b92010-10-01 11:50:21 -0700187 completeFolderNaming();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700188 } else {
189 // User has selected a folder. Go back to the opening page
190 mFolderSelector.setVisibility(View.GONE);
191 mDefaultView.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400192 mCrumbHolder.setVisibility(View.GONE);
193 mFakeTitle.setVisibility(View.VISIBLE);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700194 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400195 } else if (save()) {
196 finish();
197 }
198 } else if (v == mCancelButton) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700199 if (mFolderNamer.getVisibility() == View.VISIBLE) {
200 mFolderNamer.setVisibility(View.GONE);
201 mAddNewFolder.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400202 mAddSeparator.setVisibility(View.VISIBLE);
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700203 } else {
204 finish();
205 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400206 } else if (v == mFolder) {
207 switchToFolderSelector();
208 } else if (v == mAddNewFolder) {
209 mFolderNamer.setVisibility(View.VISIBLE);
210 mFolderNamer.setText(R.string.new_folder);
211 mFolderNamer.requestFocus();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700212 mAddNewFolder.setVisibility(View.GONE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400213 mAddSeparator.setVisibility(View.GONE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100214 getInputMethodManager().showSoftInput(mFolderNamer,
Leon Scroggins III052ce662010-09-13 14:44:16 -0400215 InputMethodManager.SHOW_IMPLICIT);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800216 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400217 }
218
Michael Kolb31829b92010-10-01 11:50:21 -0700219 private void completeFolderNaming() {
220 if (!TextUtils.isEmpty(mFolderNamer.getText())) {
221 String name = mFolderNamer.getText().toString();
222 long id = addFolderToCurrent(mFolderNamer.getText().toString());
223 descendInto(name, id);
224 mFolderNamer.setVisibility(View.GONE);
225 mAddNewFolder.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400226 mAddSeparator.setVisibility(View.VISIBLE);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100227 getInputMethodManager().hideSoftInputFromWindow(
Michael Kolb31829b92010-10-01 11:50:21 -0700228 mFolderNamer.getWindowToken(), 0);
229 }
230 }
231
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700232 private long addFolderToCurrent(String name) {
233 // Add the folder to the database
234 ContentValues values = new ContentValues();
235 values.put(BrowserContract.Bookmarks.TITLE,
236 name);
237 values.put(BrowserContract.Bookmarks.IS_FOLDER, 1);
238 values.put(BrowserContract.Bookmarks.PARENT,
239 mCurrentFolder);
240 Uri uri = getContentResolver().insert(
241 BrowserContract.Bookmarks.CONTENT_URI, values);
242 if (uri != null) {
243 return ContentUris.parseId(uri);
244 } else {
245 return -1;
246 }
247 }
248
Leon Scroggins III052ce662010-09-13 14:44:16 -0400249 private void switchToFolderSelector() {
250 mDefaultView.setVisibility(View.GONE);
251 mFolderSelector.setVisibility(View.VISIBLE);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400252 mCrumbHolder.setVisibility(View.VISIBLE);
253 mFakeTitle.setVisibility(View.GONE);
254 mAddNewFolder.setVisibility(View.VISIBLE);
255 mAddSeparator.setVisibility(View.VISIBLE);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400256 }
257
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700258 private void descendInto(String foldername, long id) {
Michael Kolb370a4f32010-10-06 10:45:32 -0700259 if (id != DEFAULT_FOLDER_ID) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700260 mCurrentFolder = id;
Leon Scroggins74dbe012010-10-15 10:54:27 -0400261 mCrumbs.pushView(foldername, id);
262 mCrumbs.notifyController();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700263 }
264 }
265
Leon Scroggins III052ce662010-09-13 14:44:16 -0400266 @Override
267 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
268 String[] projection;
269 switch (id) {
270 case LOADER_ID_ALL_FOLDERS:
271 projection = new String[] {
272 BrowserContract.Bookmarks._ID,
273 BrowserContract.Bookmarks.PARENT,
274 BrowserContract.Bookmarks.TITLE,
275 BrowserContract.Bookmarks.IS_FOLDER
276 };
277 return new CursorLoader(this,
278 BrowserContract.Bookmarks.CONTENT_URI,
279 projection,
280 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
281 null,
282 null);
283 case LOADER_ID_FOLDER_CONTENTS:
284 projection = new String[] {
285 BrowserContract.Bookmarks._ID,
286 BrowserContract.Bookmarks.TITLE,
287 BrowserContract.Bookmarks.IS_FOLDER
288 };
Leon Scroggins III052ce662010-09-13 14:44:16 -0400289 return new CursorLoader(this,
290 BrowserContract.Bookmarks.buildFolderUri(
291 mCurrentFolder),
292 projection,
293 BrowserContract.Bookmarks.IS_FOLDER + " != 0",
294 null,
295 null);
296 default:
297 throw new AssertionError("Asking for nonexistant loader!");
298 }
299 }
300
301 @Override
302 public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
303 switch (loader.getId()) {
304 case LOADER_ID_FOLDER_CONTENTS:
305 mAdapter.changeCursor(cursor);
306 break;
307 case LOADER_ID_ALL_FOLDERS:
308 long parent = mCurrentFolder;
309 int idIndex = cursor.getColumnIndexOrThrow(
310 BrowserContract.Bookmarks._ID);
311 int titleIndex = cursor.getColumnIndexOrThrow(
312 BrowserContract.Bookmarks.TITLE);
313 int parentIndex = cursor.getColumnIndexOrThrow(
314 BrowserContract.Bookmarks.PARENT);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400315 Stack folderStack = new Stack();
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700316 while ((parent != BrowserProvider2.FIXED_ID_ROOT) &&
317 (parent != 0)) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400318 // First, find the folder corresponding to the current
319 // folder
320 if (!cursor.moveToFirst()) {
321 throw new AssertionError("No folders in the database!");
322 }
323 long folder;
324 do {
325 folder = cursor.getLong(idIndex);
326 } while (folder != parent && cursor.moveToNext());
327 if (cursor.isAfterLast()) {
328 throw new AssertionError("Folder(id=" + parent
329 + ") holding this bookmark does not exist!");
330 }
331 String name = cursor.getString(titleIndex);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400332 folderStack.push(new Folder(name, parent));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400333 parent = cursor.getLong(parentIndex);
334 }
Leon Scroggins74dbe012010-10-15 10:54:27 -0400335 while (!folderStack.isEmpty()) {
336 Folder thisFolder = (Folder) folderStack.pop();
337 mCrumbs.pushView(thisFolder.Name, thisFolder.Id);
338 }
Leon Scroggins III052ce662010-09-13 14:44:16 -0400339 getLoaderManager().stopLoader(LOADER_ID_ALL_FOLDERS);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400340 updateVisible();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400341 break;
342 default:
343 break;
344 }
345 }
346
Leon Scroggins III052ce662010-09-13 14:44:16 -0400347 @Override
348 public void onItemClick(AdapterView<?> parent, View view, int position,
349 long id) {
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400350 TextView tv = (TextView) view.findViewById(android.R.id.text1);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400351 // Switch to the folder that was clicked on.
Leon Scrogginsd14cb122010-09-29 16:01:48 -0400352 descendInto(tv.getText().toString(), id);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400353 }
354
355 /**
356 * Shows a list of names of folders.
357 */
358 private class FolderAdapter extends CursorAdapter {
359 public FolderAdapter(Context context) {
360 super(context, null);
361 }
362
363 @Override
364 public void bindView(View view, Context context, Cursor cursor) {
365 ((TextView) view.findViewById(android.R.id.text1)).setText(
366 cursor.getString(cursor.getColumnIndexOrThrow(
367 BrowserContract.Bookmarks.TITLE)));
368 }
369
370 @Override
371 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700372 View view = LayoutInflater.from(context).inflate(
373 R.layout.folder_list_item, null);
374 view.setBackgroundDrawable(context.getResources().
375 getDrawable(android.R.drawable.list_selector_background));
376 return view;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400377 }
378 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800379
380 protected void onCreate(Bundle icicle) {
381 super.onCreate(icicle);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400382 if (DEBUG_CRASH) {
383 requestWindowFeature(Window.FEATURE_NO_TITLE);
384 } else {
385 requestWindowFeature(Window.FEATURE_LEFT_ICON);
386 }
Ben Murdocheecb4e62010-07-06 16:30:38 +0100387
388 mMap = getIntent().getExtras();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100389
Leon Scroggins III052ce662010-09-13 14:44:16 -0400390 setContentView(R.layout.browser_add_bookmark);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100391
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400392 Window window = getWindow();
Leon Scroggins74dbe012010-10-15 10:54:27 -0400393 if (!DEBUG_CRASH) {
394 setTitle(R.string.bookmark_this_page);
395 window.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_list_bookmark);
396 }
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700397
The Android Open Source Project0c908882009-03-03 19:32:16 -0800398 String title = null;
399 String url = null;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100400
The Android Open Source Project0c908882009-03-03 19:32:16 -0800401 if (mMap != null) {
402 Bundle b = mMap.getBundle("bookmark");
403 if (b != null) {
404 mMap = b;
405 mEditingExisting = true;
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400406 mFakeTitle.setText(R.string.edit_bookmark);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400407 if (!DEBUG_CRASH) {
408 setTitle(R.string.bookmark_this_page);
409 }
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400410 } else {
411 int gravity = mMap.getInt("gravity", -1);
412 if (gravity != -1) {
413 WindowManager.LayoutParams l = window.getAttributes();
414 l.gravity = gravity;
415 window.setAttributes(l);
416 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800417 }
418 title = mMap.getString("title");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100419 url = mOriginalUrl = mMap.getString("url");
Patrick Scott3918d442009-08-04 13:22:29 -0400420 mTouchIconUrl = mMap.getString("touch_icon_url");
Ben Murdochaac7aa62009-09-17 16:57:40 +0100421 mThumbnail = (Bitmap) mMap.getParcelable("thumbnail");
Michael Kolb370a4f32010-10-06 10:45:32 -0700422 mCurrentFolder = mMap.getLong(BrowserContract.Bookmarks.PARENT, DEFAULT_FOLDER_ID);
Leon Scroggins25230d72010-09-28 20:09:25 -0400423 }
Michael Kolb370a4f32010-10-06 10:45:32 -0700424 if (mCurrentFolder == DEFAULT_FOLDER_ID) {
Leon Scroggins25230d72010-09-28 20:09:25 -0400425 mCurrentFolder = getBookmarksBarId(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800426 }
427
428 mTitle = (EditText) findViewById(R.id.title);
429 mTitle.setText(title);
Ben Murdocheecb4e62010-07-06 16:30:38 +0100430
Leon Scroggins III052ce662010-09-13 14:44:16 -0400431 mAddress = (EditText) findViewById(R.id.address);
432 mAddress.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800433
The Android Open Source Project0c908882009-03-03 19:32:16 -0800434 mButton = (TextView) findViewById(R.id.OK);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400435 mButton.setOnClickListener(this);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800436
437 mCancelButton = findViewById(R.id.cancel);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400438 mCancelButton.setOnClickListener(this);
439
440 mFolder = (TextView) findViewById(R.id.folder);
441 mFolder.setOnClickListener(this);
442
443 mDefaultView = findViewById(R.id.default_view);
444 mFolderSelector = findViewById(R.id.folder_selector);
445
446 mFolderNamer = (EditText) findViewById(R.id.folder_namer);
447 mFolderNamer.setOnEditorActionListener(this);
448
449 mAddNewFolder = findViewById(R.id.add_new_folder);
450 mAddNewFolder.setOnClickListener(this);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400451 mAddSeparator = findViewById(R.id.add_divider);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400452
Leon Scroggins74dbe012010-10-15 10:54:27 -0400453 mCrumbs = (BreadCrumbView) findViewById(R.id.crumbs);
454 mCrumbs.setUseBackButton(true);
455 mCrumbs.setController(this);
456 mCrumbs.pushView(getString(R.string.bookmarks), false,
457 BrowserProvider2.FIXED_ID_ROOT);
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400458 mCrumbHolder = findViewById(R.id.crumb_holder);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400459
Leon Scroggins7e5f7352010-10-18 13:25:31 -0400460 mFakeTitle = (TextView) findViewById(R.id.fake_title);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400461
Leon Scroggins III052ce662010-09-13 14:44:16 -0400462 mAdapter = new FolderAdapter(this);
Leon Scroggins74dbe012010-10-15 10:54:27 -0400463 ListView list = (ListView) findViewById(R.id.list);
Leon Scroggins III052ce662010-09-13 14:44:16 -0400464 list.setAdapter(mAdapter);
465 list.setOnItemClickListener(this);
466 LoaderManager manager = getLoaderManager();
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500467 if (mCurrentFolder != BrowserProvider2.FIXED_ID_ROOT) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400468 // Find all the folders
469 manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
470 }
471 manager.initLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
472
Michael Kolbd40ac1a2010-09-29 00:23:46 -0700473
Leon Scroggins III76a0e9c2010-10-05 16:10:01 -0400474 if (!window.getDecorView().isInTouchMode()) {
The Android Open Source Project0c908882009-03-03 19:32:16 -0800475 mButton.requestFocus();
476 }
477 }
Ben Murdoch1794fe22009-09-29 18:14:30 +0100478
Leon Scroggins25230d72010-09-28 20:09:25 -0400479 // FIXME: Use a CursorLoader
480 private long getBookmarksBarId(Context context) {
481 SharedPreferences prefs
482 = PreferenceManager.getDefaultSharedPreferences(context);
483 String accountName =
484 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_NAME, null);
485 String accountType =
486 prefs.getString(BrowserBookmarksPage.PREF_ACCOUNT_TYPE, null);
487 if (TextUtils.isEmpty(accountName) || TextUtils.isEmpty(accountType)) {
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500488 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400489 }
490 Cursor cursor = null;
491 try {
492 cursor = context.getContentResolver().query(
493 BrowserContract.Bookmarks.CONTENT_URI,
494 new String[] { BrowserContract.Bookmarks._ID },
495 BrowserContract.ChromeSyncColumns.SERVER_UNIQUE + "=? AND "
496 + BrowserContract.Bookmarks.ACCOUNT_NAME + "=? AND "
497 + BrowserContract.Bookmarks.ACCOUNT_TYPE + "=?",
498 new String[] {
499 BrowserContract.ChromeSyncColumns
500 .FOLDER_NAME_BOOKMARKS_BAR,
501 accountName,
502 accountType },
503 null);
504 if (cursor != null && cursor.moveToFirst()) {
505 return cursor.getLong(0);
506 }
507 } finally {
508 if (cursor != null) cursor.close();
509 }
Jeff Hamiltona9bad832010-09-24 11:05:30 -0500510 return BrowserProvider2.FIXED_ID_ROOT;
Leon Scroggins25230d72010-09-28 20:09:25 -0400511 }
512
Leon Scroggins02065b02010-01-04 14:30:13 -0500513 /**
514 * Runnable to save a bookmark, so it can be performed in its own thread.
515 */
516 private class SaveBookmarkRunnable implements Runnable {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400517 // FIXME: This should be an async task.
Leon Scroggins02065b02010-01-04 14:30:13 -0500518 private Message mMessage;
Henrik Baard980e9952010-09-06 18:13:23 +0200519 private Context mContext;
520 public SaveBookmarkRunnable(Context ctx, Message msg) {
521 mContext = ctx;
Leon Scroggins02065b02010-01-04 14:30:13 -0500522 mMessage = msg;
523 }
524 public void run() {
525 // Unbundle bookmark data.
526 Bundle bundle = mMessage.getData();
527 String title = bundle.getString("title");
528 String url = bundle.getString("url");
529 boolean invalidateThumbnail = bundle.getBoolean(
530 "invalidateThumbnail");
531 Bitmap thumbnail = invalidateThumbnail ? null
532 : (Bitmap) bundle.getParcelable("thumbnail");
533 String touchIconUrl = bundle.getString("touchIconUrl");
534
535 // Save to the bookmarks DB.
536 try {
537 final ContentResolver cr = getContentResolver();
Leon Scroggins III052ce662010-09-13 14:44:16 -0400538 Bookmarks.addBookmark(AddBookmarkPage.this, false, url,
539 title, thumbnail, true, mCurrentFolder);
Leon Scroggins02065b02010-01-04 14:30:13 -0500540 if (touchIconUrl != null) {
Henrik Baard980e9952010-09-06 18:13:23 +0200541 new DownloadTouchIcon(mContext, cr, url).execute(mTouchIconUrl);
Leon Scroggins02065b02010-01-04 14:30:13 -0500542 }
543 mMessage.arg1 = 1;
544 } catch (IllegalStateException e) {
545 mMessage.arg1 = 0;
546 }
547 mMessage.sendToTarget();
548 }
549 }
550
Ben Murdoch1794fe22009-09-29 18:14:30 +0100551 private void createHandler() {
552 if (mHandler == null) {
553 mHandler = new Handler() {
554 @Override
555 public void handleMessage(Message msg) {
556 switch (msg.what) {
557 case SAVE_BOOKMARK:
Leon Scroggins02065b02010-01-04 14:30:13 -0500558 if (1 == msg.arg1) {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100559 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_saved,
560 Toast.LENGTH_LONG).show();
561 } else {
562 Toast.makeText(AddBookmarkPage.this, R.string.bookmark_not_saved,
563 Toast.LENGTH_LONG).show();
564 }
565 break;
566 }
567 }
568 };
569 }
570 }
571
The Android Open Source Project0c908882009-03-03 19:32:16 -0800572 /**
Ben Murdoch1794fe22009-09-29 18:14:30 +0100573 * 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 -0800574 */
575 boolean save() {
Ben Murdoch1794fe22009-09-29 18:14:30 +0100576 createHandler();
577
The Android Open Source Project0c908882009-03-03 19:32:16 -0800578 String title = mTitle.getText().toString().trim();
Ben Murdocheecb4e62010-07-06 16:30:38 +0100579 String unfilteredUrl;
Leon Scroggins III052ce662010-09-13 14:44:16 -0400580 unfilteredUrl = BrowserActivity.fixUrl(mAddress.getText().toString());
Ben Murdocheecb4e62010-07-06 16:30:38 +0100581
The Android Open Source Project0c908882009-03-03 19:32:16 -0800582 boolean emptyTitle = title.length() == 0;
583 boolean emptyUrl = unfilteredUrl.trim().length() == 0;
584 Resources r = getResources();
585 if (emptyTitle || emptyUrl) {
586 if (emptyTitle) {
587 mTitle.setError(r.getText(R.string.bookmark_needs_title));
588 }
589 if (emptyUrl) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400590 mAddress.setError(r.getText(R.string.bookmark_needs_url));
The Android Open Source Project0c908882009-03-03 19:32:16 -0800591 }
Leon Scroggins III6e3faea2010-09-07 15:22:14 -0400592 return false;
Ben Murdocheecb4e62010-07-06 16:30:38 +0100593
The Android Open Source Project0c908882009-03-03 19:32:16 -0800594 }
Ben Murdochca12cfa2009-11-17 13:57:44 +0000595 String url = unfilteredUrl.trim();
Cary Clarkf2407c62009-09-04 12:25:10 -0400596 try {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000597 // We allow bookmarks with a javascript: scheme, but these will in most cases
598 // fail URI parsing, so don't try it if that's the kind of bookmark we have.
599
600 if (!url.toLowerCase().startsWith("javascript:")) {
601 URI uriObj = new URI(url);
602 String scheme = uriObj.getScheme();
603 if (!Bookmarks.urlHasAcceptableScheme(url)) {
604 // If the scheme was non-null, let the user know that we
605 // can't save their bookmark. If it was null, we'll assume
606 // they meant http when we parse it in the WebAddress class.
607 if (scheme != null) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400608 mAddress.setError(r.getText(R.string.bookmark_cannot_save_url));
Ben Murdochca12cfa2009-11-17 13:57:44 +0000609 return false;
610 }
611 WebAddress address;
612 try {
613 address = new WebAddress(unfilteredUrl);
614 } catch (ParseException e) {
615 throw new URISyntaxException("", "");
616 }
Bjorn Bringert131ab512010-10-12 16:25:47 +0100617 if (address.getHost().length() == 0) {
Ben Murdochca12cfa2009-11-17 13:57:44 +0000618 throw new URISyntaxException("", "");
619 }
620 url = address.toString();
Ben Murdochde353622009-10-12 10:29:00 +0100621 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800622 }
Cary Clarkf2407c62009-09-04 12:25:10 -0400623 } catch (URISyntaxException e) {
Leon Scroggins III052ce662010-09-13 14:44:16 -0400624 mAddress.setError(r.getText(R.string.bookmark_url_not_valid));
Cary Clarkf2407c62009-09-04 12:25:10 -0400625 return false;
The Android Open Source Project0c908882009-03-03 19:32:16 -0800626 }
Ben Murdochaac7aa62009-09-17 16:57:40 +0100627
Ben Murdoch1794fe22009-09-29 18:14:30 +0100628 if (mEditingExisting) {
629 mMap.putString("title", title);
630 mMap.putString("url", url);
631 mMap.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
Leon Scroggins III052ce662010-09-13 14:44:16 -0400632 // FIXME: This does not work yet
633 mMap.putLong(BrowserContract.Bookmarks.PARENT, mCurrentFolder);
Ben Murdoch1794fe22009-09-29 18:14:30 +0100634 setResult(RESULT_OK, (new Intent()).setAction(
635 getIntent().toString()).putExtras(mMap));
636 } else {
637 // Post a message to write to the DB.
638 Bundle bundle = new Bundle();
639 bundle.putString("title", title);
640 bundle.putString("url", url);
641 bundle.putParcelable("thumbnail", mThumbnail);
642 bundle.putBoolean("invalidateThumbnail", !url.equals(mOriginalUrl));
643 bundle.putString("touchIconUrl", mTouchIconUrl);
644 Message msg = Message.obtain(mHandler, SAVE_BOOKMARK);
645 msg.setData(bundle);
Leon Scroggins02065b02010-01-04 14:30:13 -0500646 // Start a new thread so as to not slow down the UI
Henrik Baard980e9952010-09-06 18:13:23 +0200647 Thread t = new Thread(new SaveBookmarkRunnable(getApplicationContext(), msg));
Leon Scroggins02065b02010-01-04 14:30:13 -0500648 t.start();
Ben Murdoch1794fe22009-09-29 18:14:30 +0100649 setResult(RESULT_OK);
Kristian Monsen0a1d8382010-02-01 18:41:07 +0000650 LogTag.logBookmarkAdded(url, "bookmarkview");
The Android Open Source Project0c908882009-03-03 19:32:16 -0800651 }
652 return true;
653 }
Michael Kolb31829b92010-10-01 11:50:21 -0700654
The Android Open Source Project0c908882009-03-03 19:32:16 -0800655}