OSDN Git Service

am 28539897: am 8815f032: Merge "Always set right auth_type value in apn."
[android-x86/packages-apps-Settings.git] / src / com / android / settings / AccountPreference.java
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.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;
29
30 /**
31  * AccountPreference is used to display a username, status and provider icon for an account on
32  * the device.
33  */
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
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         setLayoutResource(R.layout.account_preference);
53         setTitle(mAccount.name);
54         setSummary("");
55         // Add account info to the intent for AccountSyncSettings
56         Intent intent = new Intent("android.settings.ACCOUNT_SYNC_SETTINGS");
57         intent.putExtra("account", mAccount);
58         setIntent(intent);
59         setPersistent(false);
60         setSyncStatus(SYNC_DISABLED);
61     }
62
63     public Account getAccount() {
64         return mAccount;
65     }
66
67     public ArrayList<String> getAuthorities() {
68         return mAuthorities;
69     }
70
71     @Override
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));
79     }
80
81     public void setProviderIcon(Drawable icon) {
82         mProviderIcon = icon;
83         if (mProviderIconView != null) {
84             mProviderIconView.setImageDrawable(icon);
85         }
86     }
87
88     public void setSyncStatus(int status) {
89         mStatus = status;
90         if (mSyncStatusIcon != null) {
91             mSyncStatusIcon.setImageResource(getSyncStatusIcon(status));
92         }
93         setSummary(getSyncStatusMessage(status));
94     }
95
96     private int getSyncStatusMessage(int status) {
97         int res;
98         switch (status) {
99             case SYNC_ENABLED:
100                 res = R.string.sync_enabled;
101                 break;
102             case SYNC_DISABLED:
103                 res = R.string.sync_disabled;
104                 break;
105             case SYNC_ERROR:
106                 res = R.string.sync_error;
107                 break;
108             default:
109                 res = R.string.sync_error;
110                 Log.e(TAG, "Unknown sync status: " + status);
111         }
112         return res;
113     }
114
115     private int getSyncStatusIcon(int status) {
116         int res;
117         switch (status) {
118             case SYNC_ENABLED:
119                 res = R.drawable.ic_sync_green;
120                 break;
121             case SYNC_DISABLED:
122                 res = R.drawable.ic_sync_grey;
123                 break;
124             case SYNC_ERROR:
125                 res = R.drawable.ic_sync_red;
126                 break;
127             default:
128                 res = R.drawable.ic_sync_red;
129                 Log.e(TAG, "Unknown sync status: " + status);
130         }
131         return res;
132     }
133
134     @Override
135     public int compareTo(Preference other) {
136         if (!(other instanceof AccountPreference)) {
137             // Put other preference types above us
138             return 1;
139         }
140         return mAccount.name.compareTo(((AccountPreference) other).mAccount.name);
141     }
142 }