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