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 java.util.ArrayList;
     20 
     21 import android.accounts.Account;
     22 import android.content.Context;
     23 import android.graphics.drawable.Drawable;
     24 import android.preference.Preference;
     25 import android.util.Log;
     26 import android.view.View;
     27 import android.widget.ImageView;
     28 
     29 /**
     30  * AccountPreference is used to display a username, status and provider icon for an account on
     31  * the device.
     32  */
     33 public class AccountPreference extends Preference {
     34     private static final String TAG = "AccountPreference";
     35     public static final int SYNC_ENABLED = 0; // all know sync adapters are enabled and OK
     36     public static final int SYNC_DISABLED = 1; // no sync adapters are enabled
     37     public static final int SYNC_ERROR = 2; // one or more sync adapters have a problem
     38     public static final int SYNC_IN_PROGRESS = 3; // currently syncing
     39     private int mStatus;
     40     private Account mAccount;
     41     private ArrayList<String> mAuthorities;
     42     private ImageView mSyncStatusIcon;
     43     private boolean mShowTypeIcon;
     44 
     45     public AccountPreference(Context context, Account account, Drawable icon,
     46             ArrayList<String> authorities, boolean showTypeIcon) {
     47         super(context);
     48         mAccount = account;
     49         mAuthorities = authorities;
     50         mShowTypeIcon = showTypeIcon;
     51         if (showTypeIcon) {
     52             setIcon(icon);
     53         } else {
     54             setIcon(getSyncStatusIcon(SYNC_DISABLED));
     55         }
     56         setTitle(mAccount.name);
     57         setSummary("");
     58         setPersistent(false);
     59         setSyncStatus(SYNC_DISABLED, false);
     60     }
     61 
     62     public Account getAccount() {
     63         return mAccount;
     64     }
     65 
     66     public ArrayList<String> getAuthorities() {
     67         return mAuthorities;
     68     }
     69 
     70     @Override
     71     protected void onBindView(View view) {
     72         super.onBindView(view);
     73         if (!mShowTypeIcon) {
     74             mSyncStatusIcon = (ImageView) view.findViewById(android.R.id.icon);
     75             mSyncStatusIcon.setImageResource(getSyncStatusIcon(mStatus));
     76             mSyncStatusIcon.setContentDescription(getSyncContentDescription(mStatus));
     77         }
     78     }
     79 
     80     public void setSyncStatus(int status, boolean updateSummary) {
     81         mStatus = status;
     82         if (!mShowTypeIcon && mSyncStatusIcon != null) {
     83             mSyncStatusIcon.setImageResource(getSyncStatusIcon(status));
     84             mSyncStatusIcon.setContentDescription(getSyncContentDescription(mStatus));
     85         }
     86         if (updateSummary) {
     87             setSummary(getSyncStatusMessage(status));
     88         }
     89     }
     90 
     91     private int getSyncStatusMessage(int status) {
     92         int res;
     93         switch (status) {
     94             case SYNC_ENABLED:
     95                 res = R.string.sync_enabled;
     96                 break;
     97             case SYNC_DISABLED:
     98                 res = R.string.sync_disabled;
     99                 break;
    100             case SYNC_ERROR:
    101                 res = R.string.sync_error;
    102                 break;
    103             case SYNC_IN_PROGRESS:
    104                 res = R.string.sync_in_progress;
    105                 break;
    106             default:
    107                 res = R.string.sync_error;
    108                 Log.e(TAG, "Unknown sync status: " + status);
    109         }
    110         return res;
    111     }
    112 
    113     private int getSyncStatusIcon(int status) {
    114         int res;
    115         switch (status) {
    116             case SYNC_ENABLED:
    117                 res = R.drawable.ic_sync_green_holo;
    118                 break;
    119             case SYNC_DISABLED:
    120                 res = R.drawable.ic_sync_grey_holo;
    121                 break;
    122             case SYNC_ERROR:
    123                 res = R.drawable.ic_sync_red_holo;
    124                 break;
    125             case SYNC_IN_PROGRESS:
    126                 res = R.drawable.ic_sync_green_holo;
    127                 break;
    128             default:
    129                 res = R.drawable.ic_sync_red_holo;
    130                 Log.e(TAG, "Unknown sync status: " + status);
    131         }
    132         return res;
    133     }
    134 
    135     private String getSyncContentDescription(int status) {
    136         switch (status) {
    137             case SYNC_ENABLED:
    138                 return getContext().getString(R.string.accessibility_sync_enabled);
    139             case SYNC_DISABLED:
    140                 return getContext().getString(R.string.accessibility_sync_disabled);
    141             case SYNC_ERROR:
    142                 return getContext().getString(R.string.accessibility_sync_error);
    143             default:
    144                 Log.e(TAG, "Unknown sync status: " + status);
    145                 return getContext().getString(R.string.accessibility_sync_error);
    146         }
    147     }
    148 }
    149