1 /* 2 * Copyright (C) 2024 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.notification.syncacrossdevices; 18 19 import android.content.Context; 20 import android.util.Log; 21 22 import androidx.annotation.NonNull; 23 import androidx.annotation.Nullable; 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceGroup; 27 import androidx.preference.PreferenceScreen; 28 29 import com.android.settings.core.BasePreferenceController; 30 import com.android.settings.core.PreferenceControllerMixin; 31 import com.android.settings.overlay.FeatureFactory; 32 33 public class SyncAcrossDevicesPreferenceController extends BasePreferenceController 34 implements PreferenceControllerMixin, SyncAcrossDevicesFeatureCallback { 35 36 private static final String TAG = "SyncXDevicesPrefCtr"; 37 38 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 39 40 private PreferenceGroup mPreferenceGroup; 41 private SyncAcrossDevicesFeatureUpdater mSyncAcrossDevicesFeatureUpdater; 42 SyncAcrossDevicesPreferenceController(@onNull Context context, @NonNull String key)43 public SyncAcrossDevicesPreferenceController(@NonNull Context context, @NonNull String key) { 44 super(context, key); 45 SyncAcrossDevicesFeatureProvider syncAcrossDevicesFeatureProvider = 46 FeatureFactory.getFeatureFactory().getSyncAcrossDevicesFeatureProvider(); 47 mSyncAcrossDevicesFeatureUpdater = 48 syncAcrossDevicesFeatureProvider.getSyncAcrossDevicesFeatureUpdater(context, this); 49 } 50 51 @Override displayPreference(@onNull PreferenceScreen screen)52 public void displayPreference(@NonNull PreferenceScreen screen) { 53 super.displayPreference(screen); 54 mPreferenceGroup = screen.findPreference(getPreferenceKey()); 55 mPreferenceGroup.setVisible(false); 56 if (isAvailable()) { 57 final Context context = screen.getContext(); 58 mSyncAcrossDevicesFeatureUpdater.setPreferenceContext(context); 59 mSyncAcrossDevicesFeatureUpdater.forceUpdate(); 60 } 61 } 62 63 @Override getAvailabilityStatus()64 public int getAvailabilityStatus() { 65 return mSyncAcrossDevicesFeatureUpdater != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 66 } 67 68 @Override onFeatureAdded(@ullable Preference preference)69 public void onFeatureAdded(@Nullable Preference preference) { 70 if (preference == null) { 71 if (DEBUG) { 72 Log.d(TAG, "onFeatureAdded receives null preference. Ignore."); 73 } 74 return; 75 } 76 mPreferenceGroup.addPreference(preference); 77 updatePreferenceVisibility(); 78 } 79 80 @Override onFeatureRemoved(@ullable Preference preference)81 public void onFeatureRemoved(@Nullable Preference preference) { 82 if (preference == null) { 83 if (DEBUG) { 84 Log.d(TAG, "onFeatureRemoved receives null preference. Ignore."); 85 } 86 return; 87 } 88 mPreferenceGroup.removePreference(preference); 89 updatePreferenceVisibility(); 90 } 91 updatePreferenceVisibility()92 private void updatePreferenceVisibility() { 93 mPreferenceGroup.setVisible(mPreferenceGroup.getPreferenceCount() > 0); 94 } 95 96 @VisibleForTesting setPreferenceGroup(@onNull PreferenceGroup preferenceGroup)97 public void setPreferenceGroup(@NonNull PreferenceGroup preferenceGroup) { 98 mPreferenceGroup = preferenceGroup; 99 } 100 } 101