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