OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.bookmark; |
| 6 |
| 7 import static org.chromium.chrome.browser.ChromeBrowserProviderClient.INVALID_BO
OKMARK_ID; |
| 8 |
| 9 import android.app.AlertDialog; |
| 10 import android.content.ContentUris; |
| 11 import android.content.ContentValues; |
| 12 import android.content.Context; |
| 13 import android.content.DialogInterface; |
| 14 import android.content.res.Resources; |
| 15 import android.net.Uri; |
| 16 import android.os.Bundle; |
| 17 import android.provider.Browser; |
| 18 import android.text.Editable; |
| 19 import android.text.TextUtils; |
| 20 import android.text.TextWatcher; |
| 21 import android.util.Log; |
| 22 import android.view.LayoutInflater; |
| 23 import android.view.View; |
| 24 import android.view.View.OnClickListener; |
| 25 import android.view.ViewGroup; |
| 26 import android.view.inputmethod.InputMethodManager; |
| 27 import android.widget.Button; |
| 28 import android.widget.EditText; |
| 29 import android.widget.TextView; |
| 30 |
| 31 import com.google.android.apps.chrome.R; |
| 32 |
| 33 import org.chromium.base.VisibleForTesting; |
| 34 import org.chromium.chrome.browser.ChromeBrowserProvider; |
| 35 import org.chromium.chrome.browser.ChromeBrowserProvider.BookmarkNode; |
| 36 import org.chromium.chrome.browser.ChromeBrowserProviderClient; |
| 37 |
| 38 import javax.annotation.Nullable; |
| 39 |
| 40 /** |
| 41 * UI container that manages adding or editing bookmark nodes. |
| 42 */ |
| 43 public class AddEditBookmarkFragment extends AsyncTaskFragment implements OnClic
kListener { |
| 44 private static final String TAG = "AddEditBookmarkFragment"; |
| 45 |
| 46 /** |
| 47 * Defines the constants used as arguments to this fragment. |
| 48 * @see android.app.Fragment#setArguments(Bundle) |
| 49 */ |
| 50 private static class Arguments { |
| 51 private Arguments() {} |
| 52 |
| 53 public static final String MODE = "mode"; |
| 54 public static final String ID = "id"; |
| 55 public static final String NAME = "name"; |
| 56 public static final String URL = "url"; |
| 57 public static final String PARENT_FOLDER_ID = "parentId"; |
| 58 public static final String PARENT_FOLDER_NAME = "parentName"; |
| 59 } |
| 60 |
| 61 /** |
| 62 * Determines the operating mode/UI of this fragment. |
| 63 */ |
| 64 private enum Mode { |
| 65 ADD_BOOKMARK, |
| 66 EDIT_BOOKMARK, |
| 67 ADD_FOLDER(true), |
| 68 EDIT_FOLDER(true); |
| 69 |
| 70 private final boolean mIsFolder; |
| 71 |
| 72 private Mode() { |
| 73 mIsFolder = false; |
| 74 } |
| 75 |
| 76 private Mode(boolean isFolder) { |
| 77 mIsFolder = isFolder; |
| 78 } |
| 79 |
| 80 /** @return Whether this mode is operating on a folder (creation or edit
ing). */ |
| 81 protected boolean isFolder() { |
| 82 return mIsFolder; |
| 83 } |
| 84 } |
| 85 |
| 86 private Button mFolderInput; |
| 87 private Button mRemoveButton; |
| 88 private Button mOkButton; |
| 89 private Button mCancelButton; |
| 90 |
| 91 private EditText mTitleInput; |
| 92 private EditText mUrlInput; |
| 93 |
| 94 /** The initial name of a bookmark (will only be used if adding a new node).
*/ |
| 95 private String mInitialName; |
| 96 |
| 97 /** The initial URL of a bookmark (will only be used if adding a new non-fol
der node). */ |
| 98 private String mInitialUrl; |
| 99 |
| 100 private long mParentFolderId = INVALID_BOOKMARK_ID; |
| 101 |
| 102 /** The name of the parent folder to this bookmark. */ |
| 103 private String mParentFolderName; |
| 104 |
| 105 /** The mode of the current activity to distinguish between adding vs editin
g. */ |
| 106 private Mode mActivityMode = Mode.ADD_BOOKMARK; |
| 107 |
| 108 /** |
| 109 * The ID of the current bookmark being edited (if adding a new bookmark the
n this will |
| 110 * be null). |
| 111 */ |
| 112 private Long mBookmarkId; |
| 113 |
| 114 /** The listener that will be notified when the actions of this fragment are
completed. */ |
| 115 private OnActionListener mActionListener; |
| 116 |
| 117 /** Lock for synchronizing calls to the loaded status checks. */ |
| 118 private final Object mLoadedLock = new Object(); |
| 119 private boolean mBookmarkNodeLoaded = false; |
| 120 private boolean mDefaultFolderLoaded = false; |
| 121 |
| 122 /** |
| 123 * Creates a new add bookmark folder fragment. |
| 124 * |
| 125 * @param parentFolderId The ID of the default parent folder for the new fol
der. |
| 126 * @param parentFolderName The name of the default parent folder for the new
folder. |
| 127 * @return An initialized add new bookmark folder fragment. |
| 128 */ |
| 129 public static AddEditBookmarkFragment newAddNewFolderInstance( |
| 130 long parentFolderId, String parentFolderName) { |
| 131 AddEditBookmarkFragment fragment = new AddEditBookmarkFragment(); |
| 132 Bundle arguments = new Bundle(); |
| 133 arguments.putSerializable(Arguments.MODE, Mode.ADD_FOLDER); |
| 134 arguments.putLong(Arguments.PARENT_FOLDER_ID, parentFolderId); |
| 135 arguments.putString(Arguments.PARENT_FOLDER_NAME, parentFolderName); |
| 136 fragment.setArguments(arguments); |
| 137 return fragment; |
| 138 } |
| 139 |
| 140 /** |
| 141 * Creates a new editing bookmark fragment where all the details of the book
mark node are to |
| 142 * be loaded from the model. |
| 143 * |
| 144 * @param isFolder Whether the node being edited is a folder. |
| 145 * @param bookmarkId The ID of the node being edited. |
| 146 * @return An initialized edit bookmark fragment. |
| 147 */ |
| 148 public static AddEditBookmarkFragment newEditInstance(boolean isFolder, long
bookmarkId) { |
| 149 AddEditBookmarkFragment fragment = new AddEditBookmarkFragment(); |
| 150 Bundle arguments = new Bundle(); |
| 151 arguments.putSerializable(Arguments.MODE, isFolder ? Mode.EDIT_FOLDER :
Mode.EDIT_BOOKMARK); |
| 152 arguments.putLong(Arguments.ID, bookmarkId); |
| 153 fragment.setArguments(arguments); |
| 154 return fragment; |
| 155 } |
| 156 |
| 157 /** |
| 158 * Creates a new add/edit bookmark fragment with the given details. |
| 159 * |
| 160 * @param isFolder Whether the node being edited is a folder. |
| 161 * @param bookmarkId The ID of the node being edited (or null if adding a ne
w bookmark node). |
| 162 * @param name The name of the bookmark being added/edited. |
| 163 * @param url The url of the bookmark being added/edited (not applicable if
isFolder is set) |
| 164 * @return An initialized add or edit bookmark fragment. |
| 165 */ |
| 166 public static AddEditBookmarkFragment newInstance( |
| 167 boolean isFolder, |
| 168 @Nullable Long bookmarkId, |
| 169 @Nullable String name, |
| 170 @Nullable String url) { |
| 171 AddEditBookmarkFragment fragment = new AddEditBookmarkFragment(); |
| 172 Bundle arguments = new Bundle(); |
| 173 if (bookmarkId == null || bookmarkId == INVALID_BOOKMARK_ID) { |
| 174 arguments.putSerializable( |
| 175 Arguments.MODE, isFolder ? Mode.ADD_FOLDER : Mode.ADD_BOOKMA
RK); |
| 176 } else { |
| 177 arguments.putSerializable( |
| 178 Arguments.MODE, isFolder ? Mode.EDIT_FOLDER : Mode.EDIT_BOOK
MARK); |
| 179 arguments.putLong(Arguments.ID, bookmarkId); |
| 180 } |
| 181 if (name != null) arguments.putString(Arguments.NAME, name); |
| 182 if (url != null) arguments.putString(Arguments.URL, url); |
| 183 fragment.setArguments(arguments); |
| 184 return fragment; |
| 185 } |
| 186 |
| 187 /** |
| 188 * Sets the action listener for this fragment. |
| 189 * @param listener The listener to be set. |
| 190 */ |
| 191 public void setOnActionListener(OnActionListener listener) { |
| 192 mActionListener = listener; |
| 193 } |
| 194 |
| 195 /** |
| 196 * Retrieves the current action listener for this fragment. |
| 197 * Used by testing to intercept calls as a proxy. |
| 198 */ |
| 199 @VisibleForTesting |
| 200 public OnActionListener getOnActionListenerForTest() { |
| 201 return mActionListener; |
| 202 } |
| 203 |
| 204 /** |
| 205 * @return Whether the default bookmark folder has been loaded. |
| 206 */ |
| 207 protected boolean isDefaultFolderLoaded() { |
| 208 synchronized (mLoadedLock) { |
| 209 return mDefaultFolderLoaded; |
| 210 } |
| 211 } |
| 212 |
| 213 /** |
| 214 * @return Whether the bookmark node has been loaded from the backend model. |
| 215 */ |
| 216 protected boolean isBookmarkNodeLoaded() { |
| 217 synchronized (mLoadedLock) { |
| 218 return mBookmarkNodeLoaded; |
| 219 } |
| 220 } |
| 221 |
| 222 @Override |
| 223 public void onCreate(Bundle savedInstanceState) { |
| 224 super.onCreate(savedInstanceState); |
| 225 |
| 226 Bundle args = getArguments(); |
| 227 mActivityMode = (Mode) args.get(Arguments.MODE); |
| 228 if (mActivityMode == null) { |
| 229 Log.e(getClass().getName(), "Created new AddEditBookmarkFragment wit
hout a mode " |
| 230 + "defined. Make sure arguments are correctly populated."); |
| 231 mActivityMode = Mode.ADD_BOOKMARK; |
| 232 return; |
| 233 } |
| 234 if (args.containsKey(Arguments.ID)) mBookmarkId = args.getLong(Arguments
.ID); |
| 235 mParentFolderId = args.getLong(Arguments.PARENT_FOLDER_ID, INVALID_BOOKM
ARK_ID); |
| 236 mParentFolderName = args.getString(Arguments.PARENT_FOLDER_NAME, null); |
| 237 mInitialName = args.getString(Arguments.NAME, null); |
| 238 mInitialUrl = args.getString(Arguments.URL, null); |
| 239 } |
| 240 |
| 241 @Override |
| 242 public View onCreateView( |
| 243 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceSt
ate) { |
| 244 return inflater.inflate(R.layout.add_bookmark, container, false); |
| 245 } |
| 246 |
| 247 @Override |
| 248 public void onViewCreated(View view, Bundle savedInstanceState) { |
| 249 super.onViewCreated(view, savedInstanceState); |
| 250 |
| 251 mRemoveButton = (Button) getView().findViewById(R.id.remove); |
| 252 mRemoveButton.setOnClickListener(this); |
| 253 mOkButton = (Button) getView().findViewById(R.id.ok); |
| 254 mOkButton.setOnClickListener(this); |
| 255 mCancelButton = (Button) getView().findViewById(R.id.cancel); |
| 256 mCancelButton.setOnClickListener(this); |
| 257 mTitleInput = (EditText) getView().findViewById(R.id.bookmark_title_inpu
t); |
| 258 clearErrorWhenNonEmpty(mTitleInput); |
| 259 mUrlInput = (EditText) getView().findViewById(R.id.bookmark_url_input); |
| 260 clearErrorWhenNonEmpty(mUrlInput); |
| 261 mFolderInput = (Button) getView().findViewById(R.id.bookmark_folder_sele
ct); |
| 262 mFolderInput.setOnClickListener(this); |
| 263 } |
| 264 |
| 265 @Override |
| 266 public void onActivityCreated(Bundle savedInstanceState) { |
| 267 super.onActivityCreated(savedInstanceState); |
| 268 |
| 269 int actionTitleTextResId = R.string.edit_bookmark; |
| 270 int bookmarkInputDescResId = R.string.accessibility_bookmark_title_textb
ox; |
| 271 |
| 272 switch (mActivityMode) { |
| 273 case ADD_BOOKMARK: |
| 274 actionTitleTextResId = R.string.add_bookmark; |
| 275 bookmarkInputDescResId = R.string.accessibility_bookmark_title_t
extbox; |
| 276 break; |
| 277 case EDIT_BOOKMARK: |
| 278 actionTitleTextResId = R.string.edit_bookmark; |
| 279 bookmarkInputDescResId = R.string.accessibility_bookmark_title_t
extbox; |
| 280 break; |
| 281 case ADD_FOLDER: |
| 282 actionTitleTextResId = R.string.add_folder; |
| 283 bookmarkInputDescResId = R.string.accessibility_bookmark_folder_
name_textbox; |
| 284 break; |
| 285 case EDIT_FOLDER: |
| 286 actionTitleTextResId = R.string.edit_folder; |
| 287 bookmarkInputDescResId = R.string.accessibility_bookmark_folder_
name_textbox; |
| 288 break; |
| 289 default: |
| 290 assert false; |
| 291 } |
| 292 |
| 293 TextView actionTitle = (TextView) getView().findViewById(R.id.bookmark_a
ction_title); |
| 294 EditText titleInput = (EditText) getView().findViewById(R.id.bookmark_ti
tle_input); |
| 295 actionTitle.setText(actionTitleTextResId); |
| 296 titleInput.setContentDescription(getResources().getString(bookmarkInputD
escResId)); |
| 297 |
| 298 if (mActivityMode == Mode.ADD_BOOKMARK || mActivityMode == Mode.ADD_FOLD
ER) { |
| 299 mRemoveButton.setVisibility(View.GONE); |
| 300 } |
| 301 if (mActivityMode.isFolder()) { |
| 302 hideUrlInputRow(); |
| 303 } |
| 304 |
| 305 initializeFolderInputContent(savedInstanceState); |
| 306 initializeStateFromIntent(savedInstanceState); |
| 307 |
| 308 if (mActivityMode == Mode.ADD_FOLDER) { |
| 309 mTitleInput.post(new Runnable() { |
| 310 @Override |
| 311 public void run() { |
| 312 if (getActivity() == null) return; |
| 313 |
| 314 mTitleInput.requestFocus(); |
| 315 InputMethodManager imm = (InputMethodManager) getActivity().
getSystemService( |
| 316 Context.INPUT_METHOD_SERVICE); |
| 317 imm.showSoftInput(mTitleInput, InputMethodManager.SHOW_IMPLI
CIT); |
| 318 } |
| 319 }); |
| 320 } |
| 321 } |
| 322 |
| 323 private void hideUrlInputRow() { |
| 324 getView().findViewById(R.id.bookmark_url_label).setVisibility(View.GONE)
; |
| 325 getView().findViewById(R.id.bookmark_url_input).setVisibility(View.GONE)
; |
| 326 } |
| 327 |
| 328 private void initializeStateFromIntent(Bundle savedInstanceState) { |
| 329 // If restoring from a previous state, do not set the fields to their in
itial values |
| 330 // (allow the individual views to reset their values correctly). |
| 331 if (savedInstanceState != null) { |
| 332 return; |
| 333 } |
| 334 |
| 335 if (!TextUtils.isEmpty(mInitialName)) mTitleInput.setText(mInitialName); |
| 336 if (!TextUtils.isEmpty(mTitleInput.getText())) { |
| 337 mTitleInput.setSelection(mTitleInput.getText().length()); |
| 338 } |
| 339 if (!TextUtils.isEmpty(mInitialUrl)) mUrlInput.setText(mInitialUrl); |
| 340 } |
| 341 |
| 342 private void initializeFolderInputContent(Bundle savedInstanceState) { |
| 343 if (savedInstanceState != null) { |
| 344 long savedParentId = savedInstanceState.getLong( |
| 345 Arguments.PARENT_FOLDER_ID, INVALID_BOOKMARK_ID); |
| 346 if (savedParentId != INVALID_BOOKMARK_ID) { |
| 347 mParentFolderId = savedParentId; |
| 348 mFolderInput.setText(savedInstanceState.getString(Arguments.PARE
NT_FOLDER_NAME)); |
| 349 return; |
| 350 } |
| 351 } else if (mParentFolderId != INVALID_BOOKMARK_ID && mParentFolderName !
= null) { |
| 352 mFolderInput.setText(mParentFolderName); |
| 353 return; |
| 354 } |
| 355 |
| 356 // Start a new asynchronous fragment task to read the bookmark folder da
ta. |
| 357 if (!isFragmentAsyncTaskRunning() && !isDefaultFolderLoaded() && !isBook
markNodeLoaded()) { |
| 358 runFragmentAsyncTask(new LoadBookmarkNodeTask( |
| 359 (mActivityMode == Mode.ADD_BOOKMARK || mActivityMode == Mode
.ADD_FOLDER) |
| 360 ? null : mBookmarkId), |
| 361 getActivity().getString(R.string.loading_bookmark)); |
| 362 } |
| 363 } |
| 364 |
| 365 @Override |
| 366 public void onSaveInstanceState(Bundle outState) { |
| 367 super.onSaveInstanceState(outState); |
| 368 |
| 369 if (!isVisible()) return; |
| 370 |
| 371 outState.putString(Arguments.PARENT_FOLDER_NAME, mFolderInput.getText().
toString()); |
| 372 outState.putLong(Arguments.PARENT_FOLDER_ID, mParentFolderId); |
| 373 } |
| 374 |
| 375 @Override |
| 376 public void onClick(View v) { |
| 377 if (mActionListener == null) { |
| 378 Log.d(getClass().getName(), "No OnResultListener specified -- onClic
k == NoOp"); |
| 379 return; |
| 380 } |
| 381 String title = mTitleInput.getText().toString().trim(); |
| 382 String url = mUrlInput.getText().toString().trim(); |
| 383 if (v == mOkButton) { |
| 384 if (!checkValidInputs(title, url)) return; |
| 385 if (isFragmentAsyncTaskRunning()) { |
| 386 Log.e(TAG, "Pending asynchronous task when trying to add/update
bookmarks."); |
| 387 return; |
| 388 } |
| 389 |
| 390 ContentValues bookmarkValues = new ContentValues(); |
| 391 if (!mActivityMode.isFolder()) { |
| 392 bookmarkValues.put(Browser.BookmarkColumns.URL, url); |
| 393 } |
| 394 bookmarkValues.put(Browser.BookmarkColumns.TITLE, title); |
| 395 bookmarkValues.put( |
| 396 ChromeBrowserProvider.BOOKMARK_PARENT_ID_PARAM, mParentFolde
rId); |
| 397 bookmarkValues.put( |
| 398 ChromeBrowserProvider.BOOKMARK_IS_FOLDER_PARAM, mActivityMod
e.isFolder()); |
| 399 |
| 400 runFragmentAsyncTask(new InsertUpdateBookmarkTask( |
| 401 bookmarkValues, |
| 402 (mActivityMode == Mode.ADD_BOOKMARK || mActivityMode == Mode
.ADD_FOLDER) |
| 403 ? null : mBookmarkId), |
| 404 getActivity().getString(R.string.saving_bookmark)); |
| 405 |
| 406 } else if (v == mRemoveButton) { |
| 407 if (mActivityMode == Mode.ADD_BOOKMARK) { |
| 408 throw new AssertionError( |
| 409 "The remove functionality should be disabled for new boo
kmarks."); |
| 410 } |
| 411 |
| 412 if (isFragmentAsyncTaskRunning()) { |
| 413 Log.e(TAG, "Pending asynchronous task when trying to delete book
marks."); |
| 414 return; |
| 415 } |
| 416 |
| 417 runFragmentAsyncTask(new DeleteBookmarkTask(mBookmarkId), |
| 418 getActivity().getString(R.string.removing_bookmark)); |
| 419 |
| 420 } else if (v == mCancelButton) { |
| 421 mActionListener.onCancel(); |
| 422 } else if (v == mFolderInput) { |
| 423 mActionListener.triggerFolderSelection(); |
| 424 } |
| 425 } |
| 426 |
| 427 /** |
| 428 * Adds a listener to |textView| that will clear the TextView's error once t
he user types |
| 429 * something. |
| 430 * Note that we needed this function in EnhancedBookmarkAddEditFolderDialog.
java and just |
| 431 * duplicated there instead of sharing, because the old bookmark UI is suppo
sed go away anyways. |
| 432 * If you have found this message in the latest repository in 2016, you can
come by Kibeom's |
| 433 * desk and punch him. |
| 434 */ |
| 435 private void clearErrorWhenNonEmpty(final TextView textView) { |
| 436 textView.addTextChangedListener(new TextWatcher() { |
| 437 @Override |
| 438 public void afterTextChanged(Editable s) { |
| 439 } |
| 440 |
| 441 @Override |
| 442 public void beforeTextChanged(CharSequence s, int start, int count,
int after) { |
| 443 } |
| 444 |
| 445 @Override |
| 446 public void onTextChanged(CharSequence s, int start, int before, int
count) { |
| 447 if (s.length() != 0) { |
| 448 textView.setError(null); |
| 449 } |
| 450 } |
| 451 }); |
| 452 } |
| 453 |
| 454 private boolean checkValidInputs(String title, String url) { |
| 455 Resources resources = getResources(); |
| 456 boolean validInput = true; |
| 457 if (!mActivityMode.isFolder() && url.isEmpty()) { |
| 458 mUrlInput.setError(resources.getText(R.string.bookmark_missing_url))
; |
| 459 mUrlInput.requestFocus(); |
| 460 validInput = false; |
| 461 } |
| 462 if (title.isEmpty()) { |
| 463 mTitleInput.setError(resources.getText(R.string.bookmark_missing_tit
le)); |
| 464 mTitleInput.requestFocus(); |
| 465 validInput = false; |
| 466 } |
| 467 return validInput; |
| 468 } |
| 469 |
| 470 @VisibleForTesting |
| 471 public boolean isLoadingBookmarks() { |
| 472 return mCurrentTask instanceof LoadBookmarkNodeTask; |
| 473 } |
| 474 |
| 475 /** |
| 476 * Sets the parent folder information for this bookmark node (updating the v
iew if present). |
| 477 * @param id The ID of the parent folder. |
| 478 * @param name The name of the parent folder. |
| 479 */ |
| 480 protected void setParentFolderInfo(long id, String name) { |
| 481 mParentFolderName = name; |
| 482 mParentFolderId = id; |
| 483 if (isVisible()) { |
| 484 mFolderInput.setText(name); |
| 485 } |
| 486 } |
| 487 |
| 488 /** |
| 489 * @return The ID of the parent node of this bookmark. |
| 490 */ |
| 491 protected long getParentFolderId() { |
| 492 return mParentFolderId; |
| 493 } |
| 494 |
| 495 /** |
| 496 * @return boolean Whether a folder or bookmark is being created/edited. |
| 497 */ |
| 498 protected boolean isFolder() { |
| 499 return mActivityMode.isFolder(); |
| 500 } |
| 501 |
| 502 private void handleGetBookmarkNode(BookmarkNode result) { |
| 503 if (getActivity() == null || getActivity().isFinishing()) return; |
| 504 |
| 505 if (result == null) { |
| 506 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()) |
| 507 .setMessage(getActivity().getResources().getText(R.string.in
valid_bookmark)) |
| 508 .setPositiveButton(getActivity().getResources().getText(R.st
ring.ok), |
| 509 new DialogInterface.OnClickListener() { |
| 510 @Override |
| 511 public void onClick(DialogInterface dialog, int
id) { |
| 512 getActivity().finish(); |
| 513 } |
| 514 }).setOnCancelListener(new DialogInterface.OnCancelL
istener() { |
| 515 @Override |
| 516 public void onCancel(DialogInterface dialog) { |
| 517 getActivity().finish(); |
| 518 } |
| 519 }); |
| 520 builder.create().show(); |
| 521 return; |
| 522 } |
| 523 |
| 524 mTitleInput.setText(result.name()); |
| 525 if (result.isUrl()) mUrlInput.setText(result.url()); |
| 526 if (result.parent() != null) { |
| 527 setParentFolderInfo(result.parent().id(), result.parent().name()); |
| 528 } |
| 529 |
| 530 synchronized (mLoadedLock) { |
| 531 mBookmarkNodeLoaded = true; |
| 532 } |
| 533 } |
| 534 |
| 535 private void handleDefaultBookmarkNode(BookmarkNode result) { |
| 536 if (getActivity() == null || getActivity().isFinishing()) return; |
| 537 |
| 538 if (result != null) { |
| 539 setParentFolderInfo(result.id(), result.name()); |
| 540 } else { |
| 541 mFolderInput.setError(getResources().getText(R.string.default_folder
_error)); |
| 542 } |
| 543 |
| 544 synchronized (mLoadedLock) { |
| 545 mDefaultFolderLoaded = true; |
| 546 } |
| 547 } |
| 548 |
| 549 /** |
| 550 * Asynchronously retrieves the requested bookmark node showing a progress d
ialog if the task |
| 551 * takes too long. |
| 552 */ |
| 553 private class LoadBookmarkNodeTask extends FragmentAsyncTask { |
| 554 private final Context mContext; |
| 555 private final Long mBookmarkId; |
| 556 private BookmarkNode mResult; |
| 557 |
| 558 /** |
| 559 * @param bookmarkId Id of the bookmark node to retrieve. Will retrieve
the default bookmark |
| 560 * folder if null. |
| 561 */ |
| 562 LoadBookmarkNodeTask(Long bookmarkId) { |
| 563 // The activity might be temporarily detached during the background
operation if the |
| 564 // screen is rotated. Make sure to use a valid context even in that
case. |
| 565 mContext = getActivity().getApplicationContext(); |
| 566 mBookmarkId = bookmarkId; |
| 567 } |
| 568 |
| 569 @Override |
| 570 protected void runBackgroundTask() { |
| 571 mResult = (mBookmarkId == null) |
| 572 ? ChromeBrowserProviderClient.getDefaultBookmarkFolder(mCont
ext) |
| 573 : ChromeBrowserProviderClient.getBookmarkNode(mContext, mBoo
kmarkId, |
| 574 ChromeBrowserProviderClient.GET_PARENT); |
| 575 } |
| 576 |
| 577 @Override |
| 578 protected void onTaskFinished() { |
| 579 if (mBookmarkId == null) { |
| 580 handleDefaultBookmarkNode(mResult); |
| 581 } else { |
| 582 handleGetBookmarkNode(mResult); |
| 583 } |
| 584 } |
| 585 |
| 586 @Override |
| 587 protected void setDependentUIEnabled(boolean enabled) { |
| 588 mOkButton.setEnabled(enabled); |
| 589 mFolderInput.setEnabled(enabled); |
| 590 |
| 591 switch (mActivityMode) { |
| 592 case ADD_BOOKMARK: |
| 593 case ADD_FOLDER: |
| 594 break; |
| 595 case EDIT_BOOKMARK: |
| 596 case EDIT_FOLDER: |
| 597 mTitleInput.setEnabled(enabled); |
| 598 mUrlInput.setEnabled(enabled); |
| 599 mRemoveButton.setEnabled(enabled); |
| 600 |
| 601 if (!enabled) { |
| 602 mUrlInput.setText(R.string.loading_bookmark); |
| 603 mTitleInput.setText(R.string.loading_bookmark); |
| 604 } |
| 605 break; |
| 606 default: |
| 607 assert false; |
| 608 } |
| 609 } |
| 610 } |
| 611 |
| 612 /** |
| 613 * Asynchronously inserts or updates a bookmark showing a progress dialog if
the task takes |
| 614 * too long. |
| 615 */ |
| 616 private class InsertUpdateBookmarkTask extends FragmentAsyncTask { |
| 617 private final Context mContext; |
| 618 private final ContentValues mBookmarkValues; |
| 619 private Long mBookmarkId; |
| 620 |
| 621 /** |
| 622 * @param bookmarkValues Contents of the bookmark to be inserted or upda
ted. |
| 623 * @param bookmarkId Id of the bookmark node to update. If null the book
mark will be |
| 624 * inserted. |
| 625 */ |
| 626 InsertUpdateBookmarkTask(ContentValues bookmarkValues, Long bookmarkId)
{ |
| 627 mContext = getActivity().getApplicationContext(); |
| 628 mBookmarkValues = bookmarkValues; |
| 629 mBookmarkId = bookmarkId; |
| 630 } |
| 631 |
| 632 @Override |
| 633 protected void runBackgroundTask() { |
| 634 Uri bookmarksUri = ChromeBrowserProvider.getBookmarksUri(mContext); |
| 635 if (mBookmarkId == null) { |
| 636 Uri response = mContext.getContentResolver().insert( |
| 637 bookmarksUri, mBookmarkValues); |
| 638 mBookmarkId = response != null ? ContentUris.parseId(response) : |
| 639 INVALID_BOOKMARK_ID; |
| 640 } else { |
| 641 mContext.getContentResolver().update( |
| 642 ContentUris.withAppendedId(bookmarksUri, mBookmarkId), |
| 643 mBookmarkValues, null, null); |
| 644 } |
| 645 } |
| 646 |
| 647 @Override |
| 648 protected void onTaskFinished() { |
| 649 if (isFolder()) { |
| 650 mActionListener.onFolderCreated(mBookmarkId, mTitleInput.getText
().toString()); |
| 651 } else { |
| 652 mActionListener.onNodeEdited(mBookmarkId); |
| 653 } |
| 654 } |
| 655 |
| 656 @Override |
| 657 protected void setDependentUIEnabled(boolean enabled) { |
| 658 mOkButton.setEnabled(enabled); |
| 659 mRemoveButton.setEnabled(enabled); |
| 660 mCancelButton.setEnabled(enabled); |
| 661 mActionListener.setBackEnabled(enabled); |
| 662 } |
| 663 } |
| 664 |
| 665 /** |
| 666 * Asynchronously deletes a bookmark showing a progress dialog if the task t
akes too long. |
| 667 */ |
| 668 private class DeleteBookmarkTask extends FragmentAsyncTask { |
| 669 private final Context mContext; |
| 670 private final long mBookmarkId; |
| 671 |
| 672 DeleteBookmarkTask(long bookmarkId) { |
| 673 mContext = getActivity().getApplicationContext(); |
| 674 mBookmarkId = bookmarkId; |
| 675 } |
| 676 |
| 677 @Override |
| 678 protected void runBackgroundTask() { |
| 679 Uri bookmarksUri = ChromeBrowserProvider.getBookmarksUri(mContext); |
| 680 mContext.getContentResolver().delete( |
| 681 ContentUris.withAppendedId(bookmarksUri, mBookmarkId), |
| 682 null, null); |
| 683 } |
| 684 |
| 685 @Override |
| 686 protected void onTaskFinished() { |
| 687 mActionListener.onRemove(); |
| 688 } |
| 689 |
| 690 @Override |
| 691 protected void setDependentUIEnabled(boolean enabled) { |
| 692 mOkButton.setEnabled(enabled); |
| 693 mRemoveButton.setEnabled(enabled); |
| 694 mCancelButton.setEnabled(enabled); |
| 695 mActionListener.setBackEnabled(enabled); |
| 696 } |
| 697 } |
| 698 |
| 699 /** |
| 700 * Listener to handle actions triggered by this fragment. |
| 701 */ |
| 702 public static interface OnActionListener { |
| 703 /** |
| 704 * Called when the addition/editing of this bookmark node has been cance
led. |
| 705 */ |
| 706 public void onCancel(); |
| 707 |
| 708 /** |
| 709 * The add/edit of this bookmark node was successful. |
| 710 * @param id The ID of the newly added bookmark (or the ID of the existi
ng |
| 711 * bookmark being edited). |
| 712 */ |
| 713 public void onNodeEdited(long id); |
| 714 |
| 715 /** |
| 716 * The folder was succesfully created. |
| 717 * @param id The ID of the newly added folder. |
| 718 * @param name Name of the folder as well. |
| 719 */ |
| 720 public void onFolderCreated(long id, String name); |
| 721 |
| 722 /** |
| 723 * Triggered when the user asks select a different bookmark folder for t
his node. |
| 724 */ |
| 725 public void triggerFolderSelection(); |
| 726 |
| 727 /** |
| 728 * Called when the bookmark node has been removed. |
| 729 */ |
| 730 public void onRemove(); |
| 731 |
| 732 /** |
| 733 * Called when the fragment wants to temporarily disable the back button
. |
| 734 * Done only while performing an asynchronous task that changes the book
mark model. |
| 735 */ |
| 736 public void setBackEnabled(boolean enabled); |
| 737 } |
| 738 } |
OLD | NEW |