1 /*
2  * Copyright (C) 2019 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.packageinstaller.auto;
18 
19 import android.os.Bundle;
20 import android.text.TextUtils;
21 import android.view.LayoutInflater;
22 import android.view.View;
23 import android.view.ViewGroup;
24 import android.widget.Button;
25 import android.widget.ProgressBar;
26 import android.widget.TextView;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.preference.PreferenceFragmentCompat;
31 
32 import com.android.permissioncontroller.R;
33 
34 /** Common settings frame for car related settings in permission controller. */
35 public abstract class AutoSettingsFrameFragment extends PreferenceFragmentCompat {
36 
37     private TextView mLabelView;
38     private ProgressBar mProgressBar;
39     private Button mAction;
40 
41     private CharSequence mLabel;
42     private boolean mIsLoading;
43     private CharSequence mActionLabel;
44     private View.OnClickListener mActionOnClickListener;
45 
46     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)47     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
48             @Nullable Bundle savedInstanceState) {
49         View rootView = super.onCreateView(inflater, container, savedInstanceState);
50 
51         View backButton = rootView.findViewById(R.id.back_button);
52         backButton.setOnClickListener(v -> getActivity().onBackPressed());
53 
54         mLabelView = rootView.findViewById(R.id.label);
55         updateHeaderLabel();
56 
57         mProgressBar = rootView.findViewById(R.id.progress_bar);
58         updateLoading();
59 
60         mAction = rootView.findViewById(R.id.action);
61         updateAction();
62 
63         return rootView;
64     }
65 
66     /** Sets the header text of this fragment. */
setHeaderLabel(CharSequence label)67     public void setHeaderLabel(CharSequence label) {
68         mLabel = label;
69         updateHeaderLabel();
70     }
71 
72     /** Gets the header text of this fragment. */
getHeaderLabel()73     public CharSequence getHeaderLabel() {
74         return mLabel;
75     }
76 
updateHeaderLabel()77     private void updateHeaderLabel() {
78         if (mLabelView != null) {
79             mLabelView.setText(mLabel);
80         }
81     }
82 
83     /**
84      * Shows a progress view while content is loading.
85      *
86      * @param isLoading {@code true} if the progress view should be visible.
87      */
setLoading(boolean isLoading)88     public void setLoading(boolean isLoading) {
89         mIsLoading = isLoading;
90         updateLoading();
91     }
92 
updateLoading()93     private void updateLoading() {
94         if (mProgressBar != null) {
95             mProgressBar.setVisibility(mIsLoading ? View.VISIBLE : View.GONE);
96         }
97     }
98 
99     /**
100      * Shows a button with the given {@code label} that when clicked will call the given {@code
101      * onClickListener}.
102      */
setAction(CharSequence label, View.OnClickListener onClickListener)103     public void setAction(CharSequence label, View.OnClickListener onClickListener) {
104         mActionLabel = label;
105         mActionOnClickListener = onClickListener;
106         updateAction();
107     }
108 
updateAction()109     private void updateAction() {
110         if (mAction == null) {
111             return;
112         }
113         if (!TextUtils.isEmpty(mActionLabel) && mActionOnClickListener != null) {
114             mAction.setText(mActionLabel);
115             mAction.setOnClickListener(mActionOnClickListener);
116             mAction.setVisibility(View.VISIBLE);
117         } else {
118             mAction.setVisibility(View.GONE);
119         }
120     }
121 }
122