1 /*
2  * Copyright (C) 2008 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.settings;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.View;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceViewHolder;
25 
26 /**
27  * A category with a progress spinner
28  */
29 public class ProgressCategory extends ProgressCategoryBase {
30 
31     private int mEmptyTextRes;
32     private boolean mProgress = false;
33     private Preference mNoDeviceFoundPreference;
34     private boolean mNoDeviceFoundAdded;
35 
ProgressCategory(Context context)36     public ProgressCategory(Context context) {
37         super(context);
38         setLayoutResource(R.layout.preference_progress_category);
39     }
40 
ProgressCategory(Context context, AttributeSet attrs)41     public ProgressCategory(Context context, AttributeSet attrs) {
42         super(context, attrs);
43         setLayoutResource(R.layout.preference_progress_category);
44     }
45 
ProgressCategory(Context context, AttributeSet attrs, int defStyleAttr)46     public ProgressCategory(Context context, AttributeSet attrs,
47             int defStyleAttr) {
48         super(context, attrs, defStyleAttr);
49         setLayoutResource(R.layout.preference_progress_category);
50     }
51 
ProgressCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)52     public ProgressCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
53         super(context, attrs, defStyleAttr, defStyleRes);
54         setLayoutResource(R.layout.preference_progress_category);
55     }
56 
setEmptyTextRes(int emptyTextRes)57     public void setEmptyTextRes(int emptyTextRes) {
58         mEmptyTextRes = emptyTextRes;
59     }
60 
61     @Override
onBindViewHolder(PreferenceViewHolder view)62     public void onBindViewHolder(PreferenceViewHolder view) {
63         super.onBindViewHolder(view);
64         final View progressBar = view.findViewById(R.id.scanning_progress);
65 
66         boolean noDeviceFound = (getPreferenceCount() == 0 ||
67                 (getPreferenceCount() == 1 && getPreference(0) == mNoDeviceFoundPreference));
68         progressBar.setVisibility(mProgress ? View.VISIBLE : View.GONE);
69 
70         if (mProgress || !noDeviceFound) {
71             if (mNoDeviceFoundAdded) {
72                 removePreference(mNoDeviceFoundPreference);
73                 mNoDeviceFoundAdded = false;
74             }
75         } else {
76             if (!mNoDeviceFoundAdded) {
77                 if (mNoDeviceFoundPreference == null) {
78                     mNoDeviceFoundPreference = new Preference(getContext());
79                     mNoDeviceFoundPreference.setLayoutResource(R.layout.preference_empty_list);
80                     mNoDeviceFoundPreference.setTitle(mEmptyTextRes);
81                     mNoDeviceFoundPreference.setSelectable(false);
82                 }
83                 addPreference(mNoDeviceFoundPreference);
84                 mNoDeviceFoundAdded = true;
85             }
86         }
87     }
88 
89     @Override
setProgress(boolean progressOn)90     public void setProgress(boolean progressOn) {
91         mProgress = progressOn;
92         notifyChanged();
93     }
94 }
95