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