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