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