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 com.android.providers.subscribedfeeds.R;
     20 
     21 import android.content.Context;
     22 import android.graphics.drawable.AnimationDrawable;
     23 import android.preference.CheckBoxPreference;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import android.widget.ImageView;
     27 import android.widget.TextView;
     28 import android.accounts.Account;
     29 
     30 public class SyncStateCheckBoxPreference extends CheckBoxPreference {
     31 
     32     private boolean mIsActive = false;
     33     private boolean mIsPending = false;
     34     private boolean mFailed = false;
     35     private Account mAccount;
     36     private String mAuthority;
     37 
     38     /**
     39      * A mode for this preference where clicking does a one-time sync instead of
     40      * toggling whether the provider will do autosync.
     41      */
     42     private boolean mOneTimeSyncMode = false;
     43 
     44     public SyncStateCheckBoxPreference(Context context, AttributeSet attrs) {
     45         super(context, attrs);
     46         setWidgetLayoutResource(R.layout.preference_widget_sync_toggle);
     47         mAccount = null;
     48         mAuthority = null;
     49     }
     50 
     51     public SyncStateCheckBoxPreference(Context context, Account account, String authority) {
     52         super(context, null);
     53         mAccount = account;
     54         mAuthority = authority;
     55         setWidgetLayoutResource(R.layout.preference_widget_sync_toggle);
     56     }
     57 
     58     @Override
     59     public void onBindView(View view) {
     60         super.onBindView(view);
     61         ImageView syncActiveView = (ImageView) view.findViewById(R.id.sync_active);
     62         View syncPendingView = view.findViewById(R.id.sync_pending);
     63         View syncFailedView = view.findViewById(R.id.sync_failed);
     64 
     65         syncActiveView.setVisibility(mIsActive ? View.VISIBLE : View.GONE);
     66         final AnimationDrawable anim = (AnimationDrawable) syncActiveView.getDrawable();
     67         boolean showError;
     68         boolean showPending;
     69         if (mIsActive) {
     70             syncActiveView.post(new Runnable() {
     71                 public void run() {
     72                     anim.start();
     73                 }
     74             });
     75             showPending = false;
     76             showError = false;
     77         } else {
     78             anim.stop();
     79             if (mIsPending) {
     80                 showPending = true;
     81                 showError = false;
     82             } else {
     83                 showPending = false;
     84                 showError = mFailed;
     85             }
     86         }
     87 
     88         syncFailedView.setVisibility(showError ? View.VISIBLE : View.GONE);
     89         syncPendingView.setVisibility((showPending && !mIsActive) ? View.VISIBLE : View.GONE);
     90 
     91         View checkBox = view.findViewById(android.R.id.checkbox);
     92         if (mOneTimeSyncMode) {
     93             checkBox.setVisibility(View.GONE);
     94 
     95             /*
     96              * Override the summary. Fill in the %1$s with the existing summary
     97              * (what ends up happening is the old summary is shown on the next
     98              * line).
     99              */
    100             TextView summary = (TextView) view.findViewById(android.R.id.summary);
    101             summary.setText(getContext().getString(R.string.sync_one_time_sync, getSummary()));
    102         } else {
    103             checkBox.setVisibility(View.VISIBLE);
    104         }
    105     }
    106 
    107     /**
    108      * Set whether the sync is active.
    109      * @param isActive whether or not the sync is active
    110      */
    111     public void setActive(boolean isActive) {
    112         mIsActive = isActive;
    113         notifyChanged();
    114     }
    115 
    116     /**
    117      * Set whether a sync is pending.
    118      * @param isPending whether or not the sync is pending
    119      */
    120     public void setPending(boolean isPending) {
    121         mIsPending = isPending;
    122         notifyChanged();
    123     }
    124 
    125     /**
    126      * Set whether the corresponding sync failed.
    127      * @param failed whether or not the sync failed
    128      */
    129     public void setFailed(boolean failed) {
    130         mFailed = failed;
    131         notifyChanged();
    132     }
    133 
    134     /**
    135      * Sets whether the preference is in one-time sync mode.
    136      */
    137     public void setOneTimeSyncMode(boolean oneTimeSyncMode) {
    138         mOneTimeSyncMode = oneTimeSyncMode;
    139         notifyChanged();
    140     }
    141 
    142     /**
    143      * Gets whether the preference is in one-time sync mode.
    144      */
    145     public boolean isOneTimeSyncMode() {
    146         return mOneTimeSyncMode;
    147     }
    148 
    149     @Override
    150     protected void onClick() {
    151         // When we're in one-time sync mode, we don't want a click to change the
    152         // checkbox state
    153         if (!mOneTimeSyncMode) {
    154             super.onClick();
    155         }
    156     }
    157 
    158     public Account getAccount() {
    159         return mAccount;
    160     }
    161 
    162     public String getAuthority() {
    163         return mAuthority;
    164     }
    165 }
    166