1 /*
2  * Copyright (C) 2016 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.accounts;
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 com.android.settings.R;
28 
29 import java.util.ArrayList;
30 
31 /**
32  * AccountPreference is used to display a username, status and provider icon for an account on
33  * the device.
34  */
35 public class AccountPreference extends Preference {
36     private static final String TAG = "AccountPreference";
37     public static final int SYNC_ENABLED = 0; // all know sync adapters are enabled and OK
38     public static final int SYNC_DISABLED = 1; // no sync adapters are enabled
39     public static final int SYNC_ERROR = 2; // one or more sync adapters have a problem
40     public static final int SYNC_IN_PROGRESS = 3; // currently syncing
41     private int mStatus;
42     private Account mAccount;
43     private ArrayList<String> mAuthorities;
44     private ImageView mSyncStatusIcon;
45     private boolean mShowTypeIcon;
46 
AccountPreference(Context context, Account account, Drawable icon, ArrayList<String> authorities, boolean showTypeIcon)47     public AccountPreference(Context context, Account account, Drawable icon,
48             ArrayList<String> authorities, boolean showTypeIcon) {
49         super(context);
50         mAccount = account;
51         mAuthorities = authorities;
52         mShowTypeIcon = showTypeIcon;
53         if (showTypeIcon) {
54             setIcon(icon);
55         } else {
56             setIcon(getSyncStatusIcon(SYNC_DISABLED));
57         }
58         setTitle(mAccount.name);
59         setSummary("");
60         setPersistent(false);
61         setSyncStatus(SYNC_DISABLED, false);
62     }
63 
getAccount()64     public Account getAccount() {
65         return mAccount;
66     }
67 
getAuthorities()68     public ArrayList<String> getAuthorities() {
69         return mAuthorities;
70     }
71 
72     @Override
onBindViewHolder(PreferenceViewHolder view)73     public void onBindViewHolder(PreferenceViewHolder view) {
74         super.onBindViewHolder(view);
75         if (!mShowTypeIcon) {
76             mSyncStatusIcon = (ImageView) view.findViewById(android.R.id.icon);
77             mSyncStatusIcon.setImageResource(getSyncStatusIcon(mStatus));
78             mSyncStatusIcon.setContentDescription(getSyncContentDescription(mStatus));
79         }
80     }
81 
setSyncStatus(int status, boolean updateSummary)82     public void setSyncStatus(int status, boolean updateSummary) {
83         if (mStatus == status) {
84             Log.d(TAG, "Status is the same, not changing anything");
85             return;
86         }
87         mStatus = status;
88         if (!mShowTypeIcon && mSyncStatusIcon != null) {
89             mSyncStatusIcon.setImageResource(getSyncStatusIcon(status));
90             mSyncStatusIcon.setContentDescription(getSyncContentDescription(mStatus));
91         }
92         if (updateSummary) {
93             setSummary(getSyncStatusMessage(status));
94         }
95     }
96 
getSyncStatusMessage(int status)97     private int getSyncStatusMessage(int status) {
98         int res;
99         switch (status) {
100             case SYNC_ENABLED:
101                 res = R.string.sync_enabled;
102                 break;
103             case SYNC_DISABLED:
104                 res = R.string.sync_disabled;
105                 break;
106             case SYNC_ERROR:
107                 res = R.string.sync_error;
108                 break;
109             case SYNC_IN_PROGRESS:
110                 res = R.string.sync_in_progress;
111                 break;
112             default:
113                 res = R.string.sync_error;
114                 Log.e(TAG, "Unknown sync status: " + status);
115         }
116         return res;
117     }
118 
getSyncStatusIcon(int status)119     private int getSyncStatusIcon(int status) {
120         int res;
121         switch (status) {
122             case SYNC_ENABLED:
123             case SYNC_IN_PROGRESS:
124                 res = R.drawable.ic_settings_sync;
125                 break;
126             case SYNC_DISABLED:
127                 res = R.drawable.ic_sync_grey_holo;
128                 break;
129             case SYNC_ERROR:
130                 res = R.drawable.ic_sync_red_holo;
131                 break;
132             default:
133                 res = R.drawable.ic_sync_red_holo;
134                 Log.e(TAG, "Unknown sync status: " + status);
135         }
136         return res;
137     }
138 
getSyncContentDescription(int status)139     private String getSyncContentDescription(int status) {
140         switch (status) {
141             case SYNC_ENABLED:
142                 return getContext().getString(R.string.accessibility_sync_enabled);
143             case SYNC_DISABLED:
144                 return getContext().getString(R.string.accessibility_sync_disabled);
145             case SYNC_ERROR:
146                 return getContext().getString(R.string.accessibility_sync_error);
147             case SYNC_IN_PROGRESS:
148                 return getContext().getString(R.string.accessibility_sync_in_progress);
149             default:
150                 Log.e(TAG, "Unknown sync status: " + status);
151                 return getContext().getString(R.string.accessibility_sync_error);
152         }
153     }
154 }
155