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