1 /*
2  * Copyright (C) 2017 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 
17 package com.android.car.settings.common;
18 
19 import static com.android.car.settings.common.BaseCarSettingsActivity.META_DATA_KEY_SINGLE_PANE;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.car.drivingstate.CarUxRestrictionsManager;
23 import android.content.Context;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 
29 import androidx.annotation.LayoutRes;
30 import androidx.annotation.NonNull;
31 import androidx.annotation.StringRes;
32 import androidx.fragment.app.Fragment;
33 
34 import com.android.car.settings.R;
35 import com.android.car.ui.toolbar.MenuItem;
36 import com.android.car.ui.toolbar.NavButtonMode;
37 import com.android.car.ui.toolbar.ToolbarController;
38 
39 import java.util.List;
40 
41 /**
42  * Base fragment for setting activity.
43  */
44 public abstract class BaseFragment extends Fragment implements
45         CarUxRestrictionsManager.OnUxRestrictionsChangedListener {
46 
47     /**
48      * Return the {@link FragmentHost}.
49      */
getFragmentHost()50     public final FragmentHost getFragmentHost() {
51         return (FragmentHost) requireActivity();
52     }
53 
54     /**
55      * Assume The activity holds this fragment also implements the UxRestrictionsProvider.
56      * This function should be called after onAttach()
57      */
getCurrentRestrictions()58     protected final CarUxRestrictions getCurrentRestrictions() {
59         return ((UxRestrictionsProvider) getActivity()).getCarUxRestrictions();
60     }
61 
62     /**
63      * Checks if this fragment can be shown or not given the CarUxRestrictions. Default to
64      * {@code false} if UX_RESTRICTIONS_NO_SETUP is set.
65      */
canBeShown(@onNull CarUxRestrictions carUxRestrictions)66     protected boolean canBeShown(@NonNull CarUxRestrictions carUxRestrictions) {
67         return !CarUxRestrictionsHelper.isNoSetup(carUxRestrictions);
68     }
69 
70     @Override
onUxRestrictionsChanged(CarUxRestrictions restrictionInfo)71     public void onUxRestrictionsChanged(CarUxRestrictions restrictionInfo) {
72     }
73 
74     /**
75      * Returns the layout id of the current Fragment.
76      */
77     @LayoutRes
getLayoutId()78     protected abstract int getLayoutId();
79 
80     /**
81      * Returns the string id for the current Fragment title. Subclasses should override this
82      * method to set the title to display. Use {@link #getToolbar().setTitle(CharSequence)} to
83      * update the displayed title while resumed. The default title is the Settings Activity label.
84      */
85     @StringRes
getTitleId()86     protected int getTitleId() {
87         return R.string.settings_label;
88     }
89 
90     /**
91      * Returns the MenuItems to display in the toolbar. Subclasses should override this to
92      * add additional buttons, switches, ect. to the toolbar.
93      */
getToolbarMenuItems()94     protected List<MenuItem> getToolbarMenuItems() {
95         return null;
96     }
97 
getToolbarNavButtonStyle()98     protected NavButtonMode getToolbarNavButtonStyle() {
99         return NavButtonMode.BACK;
100     }
101 
getToolbar()102     protected final ToolbarController getToolbar() {
103         return getFragmentHost().getToolbar();
104     }
105 
106     @Override
onAttach(Context context)107     public void onAttach(Context context) {
108         super.onAttach(context);
109         if (!(getActivity() instanceof FragmentHost)) {
110             throw new IllegalStateException("Must attach to a FragmentHost");
111         }
112         if (!(getActivity() instanceof UxRestrictionsProvider)) {
113             throw new IllegalStateException("Must attach to a UxRestrictionsProvider");
114         }
115     }
116 
117     @Override
onCreateView(@onNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)118     public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
119             Bundle savedInstanceState) {
120         @LayoutRes int layoutId = getLayoutId();
121         return inflater.inflate(layoutId, container, false);
122     }
123 
124     @Override
onActivityCreated(Bundle savedInstanceState)125     public void onActivityCreated(Bundle savedInstanceState) {
126         super.onActivityCreated(savedInstanceState);
127         ToolbarController toolbar = getToolbar();
128         if (toolbar != null) {
129             List<MenuItem> items = getToolbarMenuItems();
130             if (items != null) {
131                 if (items.size() == 1) {
132                     items.get(0).setId(R.id.toolbar_menu_item_0);
133                 } else if (items.size() == 2) {
134                     items.get(0).setId(R.id.toolbar_menu_item_0);
135                     items.get(1).setId(R.id.toolbar_menu_item_1);
136                 }
137             }
138             toolbar.setTitle(getTitleId());
139             toolbar.setMenuItems(items);
140             if (getActivity().getIntent().getBooleanExtra(META_DATA_KEY_SINGLE_PANE, false)) {
141                 toolbar.setNavButtonMode(getToolbarNavButtonStyle());
142             }
143         }
144     }
145 
146     @Override
onStart()147     public void onStart() {
148         super.onStart();
149         onUxRestrictionsChanged(getCurrentRestrictions());
150     }
151 
152     /**
153      * Allow fragment to intercept back press and customize behavior.
154      */
onBackPressed()155     protected void onBackPressed() {
156         getFragmentHost().goBack();
157     }
158 }
159