blob: 6802b76388963129676fbe00343e8559f44273c4 [file] [log] [blame]
chihhangchuang3efb6832020-04-17 02:06:25 +08001/*
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 */
16package com.android.wallpaper.picker;
17
Chuck Liao59505802020-04-28 12:32:35 +080018import android.app.Activity;
chihhangchuang3efb6832020-04-17 02:06:25 +080019import android.os.Bundle;
20import android.view.View;
21
Chihhang Chuanged63d9a2021-06-01 15:42:46 +080022import androidx.annotation.CallSuper;
chihhangchuang3efb6832020-04-17 02:06:25 +080023import androidx.annotation.NonNull;
24import androidx.annotation.Nullable;
25import androidx.fragment.app.Fragment;
26
chihhangchuang13cb1fe2020-06-30 16:46:59 +080027import com.android.wallpaper.R;
chihhangchuang3efb6832020-04-17 02:06:25 +080028import com.android.wallpaper.widget.BottomActionBar;
29import com.android.wallpaper.widget.BottomActionBar.BottomActionBarHost;
30
31/**
32 * Base class for Fragments that own a {@link BottomActionBar} widget.
33 *
chihhangchuang13cb1fe2020-06-30 16:46:59 +080034 * <p>A Fragment extending this class is expected to have a {@link BottomActionBar} in its activity
chihhangchuang3efb6832020-04-17 02:06:25 +080035 * which is a {@link BottomActionBarHost}, which can handle lifecycle management of
36 * {@link BottomActionBar} for extending fragment.
chihhangchuang13cb1fe2020-06-30 16:46:59 +080037 *
38 * <p>Or helping some fragments that own a {@link BottomActionBar} in fragment view to setup the
39 * common behavior for {@link BottomActionBar}.
chihhangchuang3efb6832020-04-17 02:06:25 +080040 */
41public class BottomActionBarFragment extends Fragment {
42
chihhangchuang13cb1fe2020-06-30 16:46:59 +080043 @Nullable private BottomActionBar mBottomActionBar;
chihhangchuang3efb6832020-04-17 02:06:25 +080044
45 @Override
46 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
47 super.onViewCreated(view, savedInstanceState);
chihhangchuang13cb1fe2020-06-30 16:46:59 +080048 mBottomActionBar = findBottomActionBar();
49 if (mBottomActionBar != null) {
Chuck Liao81145b22020-09-03 09:52:25 +080050 mBottomActionBar.reset();
Chuck Liao59505802020-04-28 12:32:35 +080051 onBottomActionBarReady(mBottomActionBar);
52 }
chihhangchuang3efb6832020-04-17 02:06:25 +080053 }
54
chihhangchuang3efb6832020-04-17 02:06:25 +080055 /** Returns {@code true} if the fragment would handle the event. */
56 public boolean onBackPressed() {
chihhangchuang3efb6832020-04-17 02:06:25 +080057 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 */
Chihhang Chuanged63d9a2021-06-01 15:42:46 +080064 @CallSuper
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 }
chihhangchuang13cb1fe2020-06-30 16:46:59 +080070
71 @Nullable
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 }
chihhangchuang3efb6832020-04-17 02:06:25 +080084}