[1/3] Create BottomActionBarFragment for BottomActionBar

- BottomActionBar will be avaible for extending fragments.
- Extending fragemnts won't handle reset BottomActionBar anymore.
- All the back key event for BottomActionBar will be handled by BottomActionBarFragment, won't distribute in fragments.

Idea graph: https://screenshot.googleplex.com/aNQVz6Di7B7.png

Test: Manually
Bug: 151287328
Change-Id: I36ed1725165bafd8624a24e9e4fe9cd4f15a846e
diff --git a/src/com/android/wallpaper/picker/ToolbarFragment.java b/src/com/android/wallpaper/picker/AppbarFragment.java
similarity index 93%
rename from src/com/android/wallpaper/picker/ToolbarFragment.java
rename to src/com/android/wallpaper/picker/AppbarFragment.java
index 488d49e..0729d16 100644
--- a/src/com/android/wallpaper/picker/ToolbarFragment.java
+++ b/src/com/android/wallpaper/picker/AppbarFragment.java
@@ -24,12 +24,13 @@
 import androidx.annotation.MenuRes;
 import androidx.appcompat.widget.Toolbar;
 import androidx.appcompat.widget.Toolbar.OnMenuItemClickListener;
-import androidx.fragment.app.Fragment;
 
 import com.android.wallpaper.R;
+import com.android.wallpaper.widget.BottomActionBar;
 
 /**
- * Base class for Fragments that own a {@link Toolbar} widget.
+ * Base class for Fragments that own a {@link Toolbar} widget and a {@link BottomActionBar}.
+ *
  * A Fragment extending this class is expected to have a {@link Toolbar} in its root view, with id
  * {@link R.id#toolbar}, which can optionally have a TextView with id custom_toolbar_title for
  * the title.
@@ -39,8 +40,10 @@
  * used as title.
  *
  * @see #setArguments(Bundle)
+ * @see BottomActionBarFragment
  */
-public abstract class ToolbarFragment extends Fragment implements OnMenuItemClickListener {
+public abstract class AppbarFragment extends BottomActionBarFragment
+        implements OnMenuItemClickListener {
 
     private static final String ARG_TITLE = "ToolbarFragment.title";
 
diff --git a/src/com/android/wallpaper/picker/BottomActionBarFragment.java b/src/com/android/wallpaper/picker/BottomActionBarFragment.java
new file mode 100644
index 0000000..9c4fbe0
--- /dev/null
+++ b/src/com/android/wallpaper/picker/BottomActionBarFragment.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.wallpaper.picker;
+
+import android.os.Bundle;
+import android.view.View;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.fragment.app.Fragment;
+
+import com.android.wallpaper.widget.BottomActionBar;
+import com.android.wallpaper.widget.BottomActionBar.BottomActionBarHost;
+
+/**
+ * Base class for Fragments that own a {@link BottomActionBar} widget.
+ *
+ * A Fragment extending this class is expected to have a {@link BottomActionBar} in its activity
+ * which is a {@link BottomActionBarHost}, which can handle lifecycle management of
+ * {@link BottomActionBar} for extending fragment.
+ */
+public class BottomActionBarFragment extends Fragment {
+
+    private BottomActionBar mBottomActionBar;
+
+    @Override
+    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
+        super.onViewCreated(view, savedInstanceState);
+        mBottomActionBar = ((BottomActionBarHost) getActivity()).getBottomActionBar();
+
+        // Do something common for BottomActionBar when the fragment is attached.
+
+        onBottomActionBarReady(mBottomActionBar);
+    }
+
+    @Override
+    public void onDestroyView() {
+        if (mBottomActionBar != null) {
+            mBottomActionBar.reset();
+            mBottomActionBar = null;
+        }
+        super.onDestroyView();
+    }
+
+    /** Returns {@code true} if the fragment would handle the event. */
+    public boolean onBackPressed() {
+        if (mBottomActionBar != null && mBottomActionBar.isVisible()) {
+            mBottomActionBar.hide();
+            return true;
+        }
+        return false;
+    }
+
+    /**
+     * Gets called when {@link #onViewCreated} finished. For extending fragment, this is the only
+     * one interface to get {@link BottomActionBar}.
+     */
+    protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {}
+}
diff --git a/src/com/android/wallpaper/picker/CategoryFragment.java b/src/com/android/wallpaper/picker/CategoryFragment.java
index dc0c42a..cdf47c1 100755
--- a/src/com/android/wallpaper/picker/CategoryFragment.java
+++ b/src/com/android/wallpaper/picker/CategoryFragment.java
@@ -110,7 +110,7 @@
 /**
  * Displays the Main UI for picking a category of wallpapers to choose from.
  */
-public class CategoryFragment extends ToolbarFragment
+public class CategoryFragment extends AppbarFragment
         implements CategorySelectorFragmentHost, ThumbnailUpdater, WallpaperDestinationCallback {
 
     private final Rect mPreviewLocalRect = new Rect();
@@ -131,7 +131,7 @@
 
     public static CategoryFragment newInstance(CharSequence title) {
         CategoryFragment fragment = new CategoryFragment();
-        fragment.setArguments(ToolbarFragment.createArguments(title));
+        fragment.setArguments(AppbarFragment.createArguments(title));
         return fragment;
     }
 
diff --git a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
index 1d0d8f0..b513dc2 100755
--- a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
+++ b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
@@ -79,6 +79,8 @@
 import com.android.wallpaper.picker.individual.IndividualPickerFragment;
 import com.android.wallpaper.util.ScreenSizeCalculator;
 import com.android.wallpaper.util.ThrowableAnalyzer;
+import com.android.wallpaper.widget.BottomActionBar;
+import com.android.wallpaper.widget.BottomActionBar.BottomActionBarHost;
 
 import com.google.android.material.bottomsheet.BottomSheetBehavior;
 import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback;
@@ -93,7 +95,7 @@
  */
 public class TopLevelPickerActivity extends BaseActivity implements WallpapersUiContainer,
         CurrentWallpaperBottomSheetPresenter, SetWallpaperErrorDialogFragment.Listener,
-        MyPhotosStarter, CategoryFragmentHost {
+        MyPhotosStarter, CategoryFragmentHost, BottomActionBarHost {
 
     private static final String TAG_SET_WALLPAPER_ERROR_DIALOG_FRAGMENT =
             "toplevel_set_wallpaper_error_dialog";
@@ -1095,6 +1097,11 @@
         }
     }
 
+    @Override
+    public BottomActionBar getBottomActionBar() {
+        return findViewById(R.id.bottom_actionbar);
+    }
+
     private interface AssetReceiver {
         void onAssetReceived(Asset asset);
     }
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java b/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java
index beb8a75..57d16de 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerActivity.java
@@ -50,12 +50,14 @@
 import com.android.wallpaper.picker.BaseActivity;
 import com.android.wallpaper.picker.PreviewActivity.PreviewActivityIntentFactory;
 import com.android.wallpaper.util.DiskBasedLogger;
+import com.android.wallpaper.widget.BottomActionBar;
+import com.android.wallpaper.widget.BottomActionBar.BottomActionBarHost;
 
 /**
  * Activity that can be launched from the Android wallpaper picker and allows users to pick from
  * various wallpapers and enter a preview mode for specific ones.
  */
-public class IndividualPickerActivity extends BaseActivity {
+public class IndividualPickerActivity extends BaseActivity implements BottomActionBarHost {
     private static final String TAG = "IndividualPickerAct";
     private static final String EXTRA_CATEGORY_COLLECTION_ID =
             "com.android.wallpaper.category_collection_id";
@@ -258,6 +260,11 @@
         bundle.putString(KEY_CATEGORY_COLLECTION_ID, mCategoryCollectionId);
     }
 
+    @Override
+    public BottomActionBar getBottomActionBar() {
+        return findViewById(R.id.bottom_actionbar);
+    }
+
     /**
      * Default implementation of intent factory that provides an intent to start an
      * IndividualPickerActivity.
diff --git a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
index b48c9f7..668c820 100755
--- a/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
+++ b/src/com/android/wallpaper/picker/individual/IndividualPickerFragment.java
@@ -46,7 +46,6 @@
 import androidx.annotation.Nullable;
 import androidx.cardview.widget.CardView;
 import androidx.fragment.app.DialogFragment;
-import androidx.fragment.app.Fragment;
 import androidx.recyclerview.widget.GridLayoutManager;
 import androidx.recyclerview.widget.RecyclerView;
 import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
@@ -78,6 +77,7 @@
 import com.android.wallpaper.module.WallpaperPreferences;
 import com.android.wallpaper.module.WallpaperSetter;
 import com.android.wallpaper.picker.BaseActivity;
+import com.android.wallpaper.picker.BottomActionBarFragment;
 import com.android.wallpaper.picker.CurrentWallpaperBottomSheetPresenter;
 import com.android.wallpaper.picker.MyPhotosStarter.MyPhotosStarterProvider;
 import com.android.wallpaper.picker.RotationStarter;
@@ -104,7 +104,7 @@
 /**
  * Displays the Main UI for picking an individual wallpaper image.
  */
-public class IndividualPickerFragment extends Fragment
+public class IndividualPickerFragment extends BottomActionBarFragment
         implements RotationStarter, StartRotationErrorDialogFragment.Listener,
         CurrentWallpaperBottomSheetPresenter.RefreshListener,
         SetWallpaperErrorDialogFragment.Listener, SetWallpaperDialogFragment.Listener {
@@ -449,18 +449,13 @@
                 getResources().getDimensionPixelSize(R.dimen.grid_padding)));
 
         maybeSetUpImageGrid();
-
         setUpBottomSheet();
-        setupBottomActionBar();
-
         return view;
     }
 
     @Override
     public void onDestroyView() {
         if (TEMP_BOTTOM_ACTION_BAR_FEATURE) {
-            mBottomActionBar.hide();
-            mBottomActionBar.clearActionClickListeners();
             updateThumbnail(null);
         }
         super.onDestroyView();
@@ -538,10 +533,12 @@
         });
     }
 
-    void setupBottomActionBar() {
+    @Override
+    protected void onBottomActionBarReady(BottomActionBar bottomActionBar) {
         if (TEMP_BOTTOM_ACTION_BAR_FEATURE) {
-            mBottomActionBar = getActivity().findViewById(R.id.bottom_actionbar);
-
+            mBottomActionBar = bottomActionBar;
+            mBottomActionBar.showActionsOnly(
+                    isRotationEnabled() ? EnumSet.of(CANCEL, ROTATION) : EnumSet.of(CANCEL));
             mBottomActionBar.setActionClickListener(CANCEL, unused -> {
                 if (mSelectedWallpaperInfo != null) {
                     onWallpaperSelected(null, 0);
@@ -560,10 +557,7 @@
                 mWallpaperSetter.requestDestination(getActivity(), getFragmentManager(), this,
                         mSelectedWallpaperInfo instanceof LiveWallpaperInfo);
             });
-
             mBottomActionBar.show();
-            mBottomActionBar.showActionsOnly(
-                    isRotationEnabled() ? EnumSet.of(CANCEL, ROTATION) : EnumSet.of(CANCEL));
         }
     }
 
@@ -900,10 +894,10 @@
     }
 
     void updateBottomActions(boolean hasWallpaperSelected) {
-        mBottomActionBar.showActions(
-                hasWallpaperSelected ? EnumSet.of(APPLY, INFORMATION) : EnumSet.of(ROTATION));
-        mBottomActionBar.hideActions(
-                hasWallpaperSelected ? EnumSet.of(ROTATION) : EnumSet.of(APPLY, INFORMATION));
+        mBottomActionBar.showActionsOnly(
+                hasWallpaperSelected
+                    ? EnumSet.of(CANCEL, INFORMATION, APPLY)
+                    : EnumSet.of(CANCEL, ROTATION));
     }
 
     private void updateThumbnail(WallpaperInfo selectedWallpaperInfo) {
diff --git a/src/com/android/wallpaper/widget/BottomActionBar.java b/src/com/android/wallpaper/widget/BottomActionBar.java
index 626182c..a0eccee 100644
--- a/src/com/android/wallpaper/widget/BottomActionBar.java
+++ b/src/com/android/wallpaper/widget/BottomActionBar.java
@@ -44,6 +44,14 @@
 /** A {@code ViewGroup} which provides the specific actions for the user to interact with. */
 public class BottomActionBar extends FrameLayout {
 
+    /**
+     * Interface to be implemented by an Activity hosting a {@link BottomActionBar}
+     */
+    public interface BottomActionBarHost {
+        /** Gets {@link BottomActionBar}. */
+        BottomActionBar getBottomActionBar();
+    }
+
     // TODO(b/154299462): Separate downloadable related actions from WallpaperPicker.
     /** The action items in the bottom action bar. */
     public enum BottomAction {
@@ -219,6 +227,15 @@
         enableActions(false);
     }
 
+    /** Resets {@link BottomActionBar}. */
+    public void reset() {
+        hide();
+        showActionsOnly(EnumSet.noneOf(BottomAction.class));
+        clearActionClickListeners();
+        enableActions();
+        resetInfoPage();
+    }
+
     private void enableActions(boolean enable) {
         mActionList.forEach((bottomAction, view) -> view.setEnabled(enable));
     }