1 /* 2 * Copyright (C) 2018 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.car.settings.accounts; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.widget.TextView; 22 23 import androidx.preference.PreferenceViewHolder; 24 25 import com.android.car.apps.common.util.Themes; 26 import com.android.car.settings.R; 27 import com.android.car.ui.preference.CarUiSwitchPreference; 28 29 /** 30 * A preference that represents the state of a sync adapter. 31 * 32 * <p>Largely derived from {@link com.android.settings.accounts.SyncStateSwitchPreference}. 33 */ 34 public class SyncPreference extends CarUiSwitchPreference { 35 private int mUid; 36 private String mPackageName; 37 private AccountSyncHelper.SyncState mSyncState = AccountSyncHelper.SyncState.NONE; 38 private boolean mOneTimeSyncMode = false; 39 SyncPreference(Context context, String authority)40 public SyncPreference(Context context, String authority) { 41 super(context); 42 setKey(authority); 43 setPersistent(false); 44 updateIcon(); 45 } 46 47 @Override onBindViewHolder(PreferenceViewHolder view)48 public void onBindViewHolder(PreferenceViewHolder view) { 49 super.onBindViewHolder(view); 50 51 View switchView = view.findViewById(com.android.internal.R.id.switch_widget); 52 if (mOneTimeSyncMode) { 53 switchView.setVisibility(View.GONE); 54 55 /* 56 * Override the summary. Fill in the %1$s with the existing summary 57 * (what ends up happening is the old summary is shown on the next 58 * line). 59 */ 60 TextView summary = (TextView) view.findViewById(android.R.id.summary); 61 summary.setText(getContext().getString(R.string.sync_one_time_sync, getSummary())); 62 } else { 63 switchView.setVisibility(View.VISIBLE); 64 } 65 } 66 67 /** Updates the preference icon based on the current syncing state. */ updateIcon()68 private void updateIcon() { 69 switch (mSyncState) { 70 case ACTIVE: 71 setIcon(R.drawable.ic_sync_anim); 72 getIcon().setTintList(Themes.getAttrColorStateList(getContext(), R.attr.iconColor)); 73 break; 74 case PENDING: 75 setIcon(R.drawable.ic_sync); 76 getIcon().setTintList(Themes.getAttrColorStateList(getContext(), R.attr.iconColor)); 77 break; 78 case FAILED: 79 setIcon(R.drawable.ic_sync_problem); 80 getIcon().setTintList(Themes.getAttrColorStateList(getContext(), R.attr.iconColor)); 81 break; 82 default: 83 setIcon(null); 84 setIconSpaceReserved(true); 85 break; 86 } 87 } 88 89 /** Sets the sync state for this preference. */ setSyncState(AccountSyncHelper.SyncState state)90 public void setSyncState(AccountSyncHelper.SyncState state) { 91 mSyncState = state; 92 // Force a manual update of the icon since the sync state affects what is shown. 93 updateIcon(); 94 } 95 96 /** 97 * Returns whether or not the sync adapter is in one-time sync mode. 98 * 99 * <p>One-time sync mode means that the sync adapter is not being automatically synced but 100 * can be manually synced (i.e. a one time sync). 101 */ isOneTimeSyncMode()102 public boolean isOneTimeSyncMode() { 103 return mOneTimeSyncMode; 104 } 105 106 /** Sets whether one-time sync mode is on for this preference. */ setOneTimeSyncMode(boolean oneTimeSyncMode)107 public void setOneTimeSyncMode(boolean oneTimeSyncMode) { 108 mOneTimeSyncMode = oneTimeSyncMode; 109 // Force a refresh so that onBindViewHolder is called 110 notifyChanged(); 111 } 112 113 /** 114 * Returns the uid corresponding to the sync adapter's package. 115 * 116 * <p>This can be used to create an intent to request account access via 117 * {@link android.accounts.AccountManager#createRequestAccountAccessIntentSenderAsUser}. 118 */ getUid()119 public int getUid() { 120 return mUid; 121 } 122 123 /** Sets the uid for this preference. */ setUid(int uid)124 public void setUid(int uid) { 125 mUid = uid; 126 } 127 getPackageName()128 public String getPackageName() { 129 return mPackageName; 130 } 131 setPackageName(String packageName)132 public void setPackageName(String packageName) { 133 mPackageName = packageName; 134 } 135 } 136