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