chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 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 | |
Chuck Liao | 5950580 | 2020-04-28 12:32:35 +0800 | [diff] [blame] | 18 | import android.app.Activity; |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 19 | import android.os.Bundle; |
| 20 | import android.view.View; |
| 21 | |
Chihhang Chuang | ed63d9a | 2021-06-01 15:42:46 +0800 | [diff] [blame] | 22 | import androidx.annotation.CallSuper; |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 23 | import androidx.annotation.NonNull; |
| 24 | import androidx.annotation.Nullable; |
| 25 | import androidx.fragment.app.Fragment; |
| 26 | |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 27 | import com.android.wallpaper.R; |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 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 | * |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 34 | * <p>A Fragment extending this class is expected to have a {@link BottomActionBar} in its activity |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 35 | * which is a {@link BottomActionBarHost}, which can handle lifecycle management of |
| 36 | * {@link BottomActionBar} for extending fragment. |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 37 | * |
| 38 | * <p>Or helping some fragments that own a {@link BottomActionBar} in fragment view to setup the |
| 39 | * common behavior for {@link BottomActionBar}. |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 40 | */ |
| 41 | public class BottomActionBarFragment extends Fragment { |
| 42 | |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 43 | @Nullable private BottomActionBar mBottomActionBar; |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 44 | |
| 45 | @Override |
| 46 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 47 | super.onViewCreated(view, savedInstanceState); |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 48 | mBottomActionBar = findBottomActionBar(); |
| 49 | if (mBottomActionBar != null) { |
Chuck Liao | 81145b2 | 2020-09-03 09:52:25 +0800 | [diff] [blame] | 50 | mBottomActionBar.reset(); |
Chuck Liao | 5950580 | 2020-04-28 12:32:35 +0800 | [diff] [blame] | 51 | onBottomActionBarReady(mBottomActionBar); |
| 52 | } |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 53 | } |
| 54 | |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 55 | /** Returns {@code true} if the fragment would handle the event. */ |
| 56 | public boolean onBackPressed() { |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 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 | */ |
Chihhang Chuang | ed63d9a | 2021-06-01 15:42:46 +0800 | [diff] [blame] | 64 | @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 | } |
chihhangchuang | 13cb1fe | 2020-06-30 16:46:59 +0800 | [diff] [blame] | 70 | |
| 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 | } |
chihhangchuang | 3efb683 | 2020-04-17 02:06:25 +0800 | [diff] [blame] | 84 | } |