• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.os.Bundle;
19 import android.text.TextUtils;
20 import android.view.MenuItem;
21 import android.view.View;
22 import android.widget.TextView;
23 
24 import androidx.annotation.MenuRes;
25 import androidx.appcompat.widget.Toolbar;
26 import androidx.appcompat.widget.Toolbar.OnMenuItemClickListener;
27 import androidx.fragment.app.Fragment;
28 
29 import com.android.wallpaper.R;
30 
31 /**
32  * Base class for Fragments that own a {@link Toolbar} widget.
33  * A Fragment extending this class is expected to have a {@link Toolbar} in its root view, with id
34  * {@link R.id#toolbar}, which can optionally have a TextView with id custom_toolbar_title for
35  * the title.
36  * If the Bundle returned by {@link #createArguments(CharSequence)} is used as Arguments for this
37  * Fragment, the title provided to that method will be used as the Fragment's toolbar title,
38  * otherwise, the value returned by {@link #getDefaultTitle()} (default being {@code null}) will be
39  * used as title.
40  *
41  * @see #setArguments(Bundle)
42  */
43 public abstract class ToolbarFragment extends Fragment implements OnMenuItemClickListener {
44 
45     private static final String ARG_TITLE = "ToolbarFragment.title";
46 
47     /**
48      * Returns a newly created {@link Bundle} containing the given title as an argument.
49      * If set as a ToolbarFragment's arguments bundle, this will be used to set up the title of
50      * the Toolbar in {@link #setUpToolbar(View)}
51      */
createArguments(CharSequence title)52     protected static Bundle createArguments(CharSequence title) {
53         Bundle args = new Bundle();
54         args.putCharSequence(ARG_TITLE, title);
55         return args;
56     }
57 
58     protected Toolbar mToolbar;
59     private TextView mTitleView;
60 
61     /**
62      * Configures a toolbar in the given rootView, with id {@code toolbar} and sets its title to
63      * the value in Arguments or {@link #getDefaultTitle()}
64      */
setUpToolbar(View rootView)65     public void setUpToolbar(View rootView) {
66         mToolbar = rootView.findViewById(R.id.toolbar);
67 
68         mTitleView = mToolbar.findViewById(R.id.custom_toolbar_title);
69         CharSequence title;
70         if (getArguments() != null) {
71             title = getArguments().getCharSequence(ARG_TITLE, getDefaultTitle());
72         } else {
73             title = getDefaultTitle();
74         }
75         if (!TextUtils.isEmpty(title)) {
76             setTitle(title);
77         }
78     }
79 
80     /**
81      * Configures a toolbar in the given rootView, inflating the menu corresponding to the given id
82      * for the toolbar menu.
83      * Override {@link #onMenuItemClick(MenuItem)} to listen to item click events.
84      * @see #setUpToolbar(View)
85      */
setUpToolbar(View rootView, @MenuRes int menuResId)86     public void setUpToolbar(View rootView, @MenuRes int menuResId) {
87         setUpToolbar(rootView);
88         mToolbar.inflateMenu(menuResId);
89         mToolbar.setOnMenuItemClickListener(this);
90     }
91 
92     /**
93      * Provides a title for this Fragment's toolbar to be used if none is found in
94      * {@link #getArguments()}.
95      * Default implementation returns {@code null}.
96      */
getDefaultTitle()97     public CharSequence getDefaultTitle() {
98         return null;
99     }
100 
setTitle(CharSequence title)101     private void setTitle(CharSequence title) {
102         if (mToolbar == null) {
103             return;
104         }
105         if (mTitleView != null) {
106             mToolbar.setTitle(null);
107             mTitleView.setText(title);
108         } else {
109             mToolbar.setTitle(title);
110         }
111     }
112 
113     @Override
onMenuItemClick(MenuItem item)114     public boolean onMenuItemClick(MenuItem item) {
115         return false;
116     }
117 }
118