1 /* 2 * Copyright (C) 2020 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 package com.android.wallpaper.picker; 17 18 import android.app.Activity; 19 import android.os.Bundle; 20 import android.view.View; 21 22 import androidx.annotation.CallSuper; 23 import androidx.annotation.NonNull; 24 import androidx.annotation.Nullable; 25 import androidx.fragment.app.Fragment; 26 27 import com.android.wallpaper.R; 28 import com.android.wallpaper.widget.BottomActionBar; 29 import com.android.wallpaper.widget.BottomActionBar.BottomActionBarHost; 30 31 /** 32 * Base class for Fragments that own a {@link BottomActionBar} widget. 33 * 34 * <p>A Fragment extending this class is expected to have a {@link BottomActionBar} in its activity 35 * which is a {@link BottomActionBarHost}, which can handle lifecycle management of 36 * {@link BottomActionBar} for extending fragment. 37 * 38 * <p>Or helping some fragments that own a {@link BottomActionBar} in fragment view to setup the 39 * common behavior for {@link BottomActionBar}. 40 */ 41 public class BottomActionBarFragment extends Fragment { 42 43 @Nullable private BottomActionBar mBottomActionBar; 44 45 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)46 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 47 super.onViewCreated(view, savedInstanceState); 48 mBottomActionBar = findBottomActionBar(); 49 if (mBottomActionBar != null) { 50 mBottomActionBar.reset(); 51 onBottomActionBarReady(mBottomActionBar); 52 } 53 } 54 55 /** Returns {@code true} if the fragment would handle the event. */ onBackPressed()56 public boolean onBackPressed() { 57 return false; 58 } 59 60 /** 61 * Gets called when {@link #onViewCreated} finished. For extending fragment, this is the only 62 * one interface to get {@link BottomActionBar}. 63 */ 64 @CallSuper onBottomActionBarReady(BottomActionBar bottomActionBar)65 protected void onBottomActionBarReady(BottomActionBar bottomActionBar) { 66 // Needed for some cases that need to recreate the BottomActionBar. 67 mBottomActionBar = bottomActionBar; 68 bottomActionBar.bindBackButtonToSystemBackKey(getActivity()); 69 } 70 71 @Nullable findBottomActionBar()72 private BottomActionBar findBottomActionBar() { 73 Activity activity = getActivity(); 74 if (activity instanceof BottomActionBarHost) { 75 return ((BottomActionBarHost) activity).getBottomActionBar(); 76 } 77 78 View root = getView(); 79 if (root != null) { 80 return root.findViewById(R.id.bottom_actionbar); 81 } 82 return null; 83 } 84 } 85