Show last folder saved to as an option.

Bug:3424716

Change-Id: Id0087fc07dc2cab58177711c63d0a2e27e99e662
diff --git a/src/com/android/browser/AddBookmarkPage.java b/src/com/android/browser/AddBookmarkPage.java
index fc3b0a9..9a7227c 100644
--- a/src/com/android/browser/AddBookmarkPage.java
+++ b/src/com/android/browser/AddBookmarkPage.java
@@ -87,6 +87,8 @@
     private final int LOADER_ID_ALL_FOLDERS = 1;
     private final int LOADER_ID_FIND_ROOT = 2;
     private final int LOADER_ID_CHECK_FOR_DUPE = 3;
+    private final int LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK = 4;
+    private final int LOADER_ID_FIND_FOLDER_BY_ID = 5;
 
     private EditText    mTitle;
     private EditText    mAddress;
@@ -118,6 +120,7 @@
     private Drawable mHeaderIcon;
     private View mRemoveLink;
     private View mFakeTitleHolder;
+    private FolderSpinnerAdapter mFolderAdapter;
     private static class Folder {
         String Name;
         long Id;
@@ -309,6 +312,15 @@
             case FolderSpinnerAdapter.OTHER_FOLDER:
                 switchToFolderSelector();
                 break;
+            case FolderSpinnerAdapter.RECENT_FOLDER:
+                mCurrentFolder = mFolderAdapter.recentFolderId();
+                mSaveToHomeScreen = false;
+                // In case the user decides to select OTHER_FOLDER
+                // and choose a different one, so that we will start from
+                // the correct place.
+                LoaderManager manager = getLoaderManager();
+                manager.initLoader(LOADER_ID_ALL_FOLDERS, null, this);
+                manager.restartLoader(LOADER_ID_FOLDER_CONTENTS, null, this);
             default:
                 break;
         }
@@ -413,6 +425,18 @@
                         selection,
                         selArgs,
                         null);
+            case LOADER_ID_FIND_FOLDER_BY_ID:
+                projection = new String[] {
+                        BrowserContract.Bookmarks._ID,
+                        BrowserContract.Bookmarks.TITLE
+                };
+                return new CursorLoader(this,
+                        BrowserContract.Bookmarks.CONTENT_URI,
+                        projection,
+                        BrowserContract.Bookmarks._ID + " = "
+                                + args.getLong(BrowserContract.Bookmarks._ID),
+                        null,
+                        null);
             case LOADER_ID_ALL_FOLDERS:
                 projection = new String[] {
                         BrowserContract.Bookmarks._ID,
@@ -443,6 +467,16 @@
                         where,
                         null,
                         BrowserContract.Bookmarks._ID + " ASC");
+            case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK:
+                projection = new String[] {
+                        BrowserContract.Bookmarks.PARENT
+                };
+                return new CursorLoader(this,
+                        BrowserContract.Bookmarks.CONTENT_URI,
+                        projection,
+                        BrowserContract.Bookmarks.IS_FOLDER + " = 0",
+                        null,
+                        BrowserContract.Bookmarks.DATE_CREATED + " DESC");
             default:
                 throw new AssertionError("Asking for nonexistant loader!");
         }
@@ -485,6 +519,32 @@
             case LOADER_ID_FOLDER_CONTENTS:
                 mAdapter.changeCursor(cursor);
                 break;
+            case LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK:
+                LoaderManager manager = getLoaderManager();
+                if (cursor != null && cursor.moveToFirst()) {
+                    // Find the parent
+                    long lastUsedFolder = cursor.getLong(0);
+                    if (lastUsedFolder != mRootFolder
+                            && lastUsedFolder != mCurrentFolder
+                            && lastUsedFolder != 0) {
+                        // Find out the parent's name
+                        Bundle b = new Bundle();
+                        b.putLong(BrowserContract.Bookmarks._ID, lastUsedFolder);
+                        manager.initLoader(LOADER_ID_FIND_FOLDER_BY_ID, b, this);
+                    }
+                }
+                manager.destroyLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK);
+                break;
+            case LOADER_ID_FIND_FOLDER_BY_ID:
+                if (cursor != null && cursor.moveToFirst()) {
+                    long id = cursor.getLong(cursor.getColumnIndexOrThrow(
+                            BrowserContract.Bookmarks._ID));
+                    String title = cursor.getString(cursor.getColumnIndexOrThrow(
+                            BrowserContract.Bookmarks.TITLE));
+                    mFolderAdapter.addRecentFolder(id, title);
+                }
+                getLoaderManager().destroyLoader(LOADER_ID_FIND_FOLDER_BY_ID);
+                break;
             case LOADER_ID_ALL_FOLDERS:
                 long parent = mCurrentFolder;
                 int idIndex = cursor.getColumnIndexOrThrow(
@@ -666,7 +726,8 @@
         mCancelButton.setOnClickListener(this);
 
         mFolder = (FolderSpinner) findViewById(R.id.folder);
-        mFolder.setAdapter(new FolderSpinnerAdapter(!mEditingFolder));
+        mFolderAdapter = new FolderSpinnerAdapter(!mEditingFolder);
+        mFolder.setAdapter(mFolderAdapter);
         mFolder.setOnSetSelectionListener(this);
 
         mDefaultView = findViewById(R.id.default_view);
@@ -759,6 +820,11 @@
             mFolder.setSelectionIgnoringSelectionChange(mEditingFolder ? 1 : 2);
         } else {
             setShowBookmarkIcon(true);
+            if (!mEditingExisting) {
+                // Find the most recently saved bookmark, so that we can include it in
+                // the list of options to save the current bookmark.
+                manager.initLoader(LOADER_ID_MOST_RECENTLY_SAVED_BOOKMARK, null, this);
+            }
             if (!mEditingFolder) {
                 // Initially the "Bookmarks" folder should be showing, rather than
                 // the home screen.  In the editing folder case, home screen is not
diff --git a/src/com/android/browser/addbookmark/FolderSpinnerAdapter.java b/src/com/android/browser/addbookmark/FolderSpinnerAdapter.java
index 0712469..261aa62 100644
--- a/src/com/android/browser/addbookmark/FolderSpinnerAdapter.java
+++ b/src/com/android/browser/addbookmark/FolderSpinnerAdapter.java
@@ -35,15 +35,27 @@
  */
 public class FolderSpinnerAdapter implements SpinnerAdapter {
     private boolean mIncludeHomeScreen;
+    private boolean mIncludesRecentFolder;
+    private long mRecentFolderId;
+    private String mRecentFolderName;
 
     public static final int HOME_SCREEN = 0;
     public static final int ROOT_FOLDER = 1;
     public static final int OTHER_FOLDER = 2;
+    public static final int RECENT_FOLDER = 3;
 
     public FolderSpinnerAdapter(boolean includeHomeScreen) {
         mIncludeHomeScreen = includeHomeScreen;
     }
 
+    public void addRecentFolder(long folderId, String folderName) {
+        mIncludesRecentFolder = true;
+        mRecentFolderId = folderId;
+        mRecentFolderName = folderName;
+    }
+
+    public long recentFolderId() { return mRecentFolderId; }
+
     @Override
     public View getDropDownView(int position, View convertView, ViewGroup parent) {
         int labelResource;
@@ -60,6 +72,8 @@
                 labelResource = R.string.add_to_bookmarks_menu_option;
                 drawableResource = R.drawable.ic_bookmarks_holo_dark;
                 break;
+            case RECENT_FOLDER:
+                // Fall through and use the same icon resource
             case OTHER_FOLDER:
                 labelResource = R.string.add_to_other_folder_menu_option;
                 drawableResource = R.drawable.ic_folder_holo_dark;
@@ -73,7 +87,11 @@
         Context context = parent.getContext();
         LayoutInflater factory = LayoutInflater.from(context);
         TextView textView = (TextView) factory.inflate(R.layout.add_to_option, null);
-        textView.setText(labelResource);
+        if (position == RECENT_FOLDER) {
+            textView.setText(mRecentFolderName);
+        } else {
+            textView.setText(labelResource);
+        }
         Drawable drawable = context.getResources().getDrawable(drawableResource);
         textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null,
                 null, null);
@@ -90,7 +108,10 @@
 
     @Override
     public int getCount() {
-        return mIncludeHomeScreen ? 3 : 2;
+        int count = 2;
+        if (mIncludeHomeScreen) count++;
+        if (mIncludesRecentFolder) count++;
+        return count;
     }
 
     @Override