• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         this(context, null);
37     }
38 
ProgressCategory(Context context, AttributeSet attrs)39     public ProgressCategory(Context context, AttributeSet attrs) {
40         super(context, attrs, 0);
41     }
42 
ProgressCategory(Context context, AttributeSet attrs, int defStyleAttr)43     public ProgressCategory(Context context, AttributeSet attrs,
44             int defStyleAttr) {
45         this(context, attrs, defStyleAttr, 0);
46     }
47 
ProgressCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)48     public ProgressCategory(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
49         super(context, attrs, defStyleAttr, defStyleRes);
50         setLayoutResource(R.layout.preference_progress_category);
51     }
52 
setEmptyTextRes(int emptyTextRes)53     public void setEmptyTextRes(int emptyTextRes) {
54         mEmptyTextRes = emptyTextRes;
55     }
56 
57     @Override
onBindViewHolder(PreferenceViewHolder view)58     public void onBindViewHolder(PreferenceViewHolder view) {
59         super.onBindViewHolder(view);
60         final View progressBar = view.findViewById(R.id.scanning_progress);
61 
62         boolean noDeviceFound = (getPreferenceCount() == 0 ||
63                 (getPreferenceCount() == 1 && getPreference(0) == mNoDeviceFoundPreference));
64         progressBar.setVisibility(mProgress ? View.VISIBLE : View.GONE);
65 
66         if (mProgress || !noDeviceFound) {
67             if (mNoDeviceFoundAdded) {
68                 removePreference(mNoDeviceFoundPreference);
69                 mNoDeviceFoundAdded = false;
70             }
71         } else {
72             if (!mNoDeviceFoundAdded) {
73                 if (mNoDeviceFoundPreference == null) {
74                     mNoDeviceFoundPreference = new Preference(getPreferenceManager().getContext());
75                     mNoDeviceFoundPreference.setLayoutResource(R.layout.preference_empty_list);
76                     mNoDeviceFoundPreference.setTitle(mEmptyTextRes);
77                     mNoDeviceFoundPreference.setSelectable(false);
78                 }
79                 addPreference(mNoDeviceFoundPreference);
80                 mNoDeviceFoundAdded = true;
81             }
82         }
83     }
84 
85     @Override
setProgress(boolean progressOn)86     public void setProgress(boolean progressOn) {
87         mProgress = progressOn;
88         notifyChanged();
89     }
90 }
91