AccountPreference.java revision d1ab82807aae63926b35f66080a1f7a75c00b95b
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.graphics.drawable.Drawable;
24import android.preference.Preference;
25import android.util.Log;
26import android.view.View;
27import 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 */
33public 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
43    public AccountPreference(Context context, Account account, Drawable icon,
44            ArrayList<String> authorities) {
45        super(context);
46        mAccount = account;
47        mAuthorities = authorities;
48        setTitle(mAccount.name);
49        setSummary("");
50        setPersistent(false);
51        setSyncStatus(SYNC_DISABLED);
52    }
53
54    public Account getAccount() {
55        return mAccount;
56    }
57
58    public ArrayList<String> getAuthorities() {
59        return mAuthorities;
60    }
61
62    @Override
63    protected void onBindView(View view) {
64        super.onBindView(view);
65        setSummary(getSyncStatusMessage(mStatus));
66        ImageView iconView = (ImageView) view.findViewById(android.R.id.icon);
67        iconView.setImageResource(getSyncStatusIcon(mStatus));
68        iconView.setContentDescription(getSyncContentDescription(mStatus));
69    }
70
71    public void setSyncStatus(int status) {
72        mStatus = status;
73        setIcon(getSyncStatusIcon(status));
74        setSummary(getSyncStatusMessage(status));
75    }
76
77    private int getSyncStatusMessage(int status) {
78        int res;
79        switch (status) {
80            case SYNC_ENABLED:
81                res = R.string.sync_enabled;
82                break;
83            case SYNC_DISABLED:
84                res = R.string.sync_disabled;
85                break;
86            case SYNC_ERROR:
87                res = R.string.sync_error;
88                break;
89            case SYNC_IN_PROGRESS:
90                res = R.string.sync_in_progress;
91                break;
92            default:
93                res = R.string.sync_error;
94                Log.e(TAG, "Unknown sync status: " + status);
95        }
96        return res;
97    }
98
99    private int getSyncStatusIcon(int status) {
100        int res;
101        switch (status) {
102            case SYNC_ENABLED:
103                res = R.drawable.ic_sync_green_holo;
104                break;
105            case SYNC_DISABLED:
106                res = R.drawable.ic_sync_grey_holo;
107                break;
108            case SYNC_ERROR:
109                res = R.drawable.ic_sync_red_holo;
110                break;
111            case SYNC_IN_PROGRESS:
112                res = R.drawable.ic_sync_grey_holo;
113                break;
114            default:
115                res = R.drawable.ic_sync_red_holo;
116                Log.e(TAG, "Unknown sync status: " + status);
117        }
118        return res;
119    }
120
121    private String getSyncContentDescription(int status) {
122        switch (status) {
123            case SYNC_ENABLED:
124                return getContext().getString(R.string.accessibility_sync_enabled);
125            case SYNC_DISABLED:
126                return getContext().getString(R.string.accessibility_sync_disabled);
127            case SYNC_ERROR:
128                return getContext().getString(R.string.accessibility_sync_error);
129            default:
130                Log.e(TAG, "Unknown sync status: " + status);
131                return getContext().getString(R.string.accessibility_sync_error);
132        }
133    }
134}
135