2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings;
19 import java.util.ArrayList;
21 import android.accounts.Account;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.graphics.drawable.Drawable;
25 import android.preference.Preference;
26 import android.util.Log;
27 import android.view.View;
28 import android.widget.ImageView;
31 * AccountPreference is used to display a username, status and provider icon for an account on
34 public class AccountPreference extends Preference {
35 private static final String TAG = "AccountPreference";
36 public static final int SYNC_ENABLED = 0; // all know sync adapters are enabled and OK
37 public static final int SYNC_DISABLED = 1; // no sync adapters are enabled
38 public static final int SYNC_ERROR = 2; // one or more sync adapters have a problem
40 private Account mAccount;
41 private ArrayList<String> mAuthorities;
42 private Drawable mProviderIcon;
43 private ImageView mSyncStatusIcon;
44 private ImageView mProviderIconView;
46 public AccountPreference(Context context, Account account, Drawable icon,
47 ArrayList<String> authorities) {
50 mAuthorities = authorities;
52 setLayoutResource(R.layout.account_preference);
53 setTitle(mAccount.name);
55 // Add account info to the intent for AccountSyncSettings
56 Intent intent = new Intent("android.settings.ACCOUNT_SYNC_SETTINGS");
57 intent.putExtra("account", mAccount);
60 setSyncStatus(SYNC_DISABLED);
63 public Account getAccount() {
67 public ArrayList<String> getAuthorities() {
72 protected void onBindView(View view) {
73 super.onBindView(view);
74 setSummary(getSyncStatusMessage(mStatus));
75 mProviderIconView = (ImageView) view.findViewById(R.id.providerIcon);
76 mProviderIconView.setImageDrawable(mProviderIcon);
77 mSyncStatusIcon = (ImageView) view.findViewById(R.id.syncStatusIcon);
78 mSyncStatusIcon.setImageResource(getSyncStatusIcon(mStatus));
81 public void setProviderIcon(Drawable icon) {
83 if (mProviderIconView != null) {
84 mProviderIconView.setImageDrawable(icon);
88 public void setSyncStatus(int status) {
90 if (mSyncStatusIcon != null) {
91 mSyncStatusIcon.setImageResource(getSyncStatusIcon(status));
93 setSummary(getSyncStatusMessage(status));
96 private int getSyncStatusMessage(int status) {
100 res = R.string.sync_enabled;
103 res = R.string.sync_disabled;
106 res = R.string.sync_error;
109 res = R.string.sync_error;
110 Log.e(TAG, "Unknown sync status: " + status);
115 private int getSyncStatusIcon(int status) {
119 res = R.drawable.ic_sync_green;
122 res = R.drawable.ic_sync_grey;
125 res = R.drawable.ic_sync_red;
128 res = R.drawable.ic_sync_red;
129 Log.e(TAG, "Unknown sync status: " + status);
135 public int compareTo(Preference other) {
136 if (!(other instanceof AccountPreference)) {
137 // Put other preference types above us
140 return mAccount.name.compareTo(((AccountPreference) other).mAccount.name);