diff --git a/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java b/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java index 2f04b62a821..4fc0e1600f7 100644 --- a/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java +++ b/src/com/android/settings/applications/credentials/CredentialManagerPreferenceController.java @@ -46,6 +46,7 @@ import android.provider.Settings; import android.service.autofill.AutofillServiceInfo; import android.text.TextUtils; import android.util.Log; +import android.util.Pair; import android.view.View; import android.widget.CompoundButton; @@ -77,6 +78,7 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.Executor; @@ -114,7 +116,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl private @Nullable String mFlagOverrideForTest = null; private @Nullable PreferenceScreen mPreferenceScreen = null; - private boolean mVisibility = false; + private Optional mSimulateHiddenForTests = Optional.empty(); private boolean mIsWorkProfile = false; private boolean mSimulateConnectedForTests = false; @@ -159,7 +161,9 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl return UNSUPPORTED_ON_DEVICE; } - if (!mVisibility) { + // If there is no top provider or any providers in the list then + // we should hide this pref. + if (isHiddenDueToNoProviderSet()) { return CONDITIONALLY_UNAVAILABLE; } @@ -378,20 +382,29 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl } @VisibleForTesting - public void setVisibility(boolean newVisibility) { - if (newVisibility == mVisibility) { - return; - } - - mVisibility = newVisibility; + public void forceDelegateRefresh() { if (mDelegate != null) { mDelegate.forceDelegateRefresh(); } } @VisibleForTesting - public boolean getVisibility() { - return mVisibility; + public void setSimulateHiddenForTests(Optional simulateHiddenForTests) { + mSimulateHiddenForTests = simulateHiddenForTests; + } + + @VisibleForTesting + public boolean isHiddenDueToNoProviderSet() { + return isHiddenDueToNoProviderSet(getProviders()); + } + + private boolean isHiddenDueToNoProviderSet( + Pair, CombinedProviderInfo> providerPair) { + if (mSimulateHiddenForTests.isPresent()) { + return mSimulateHiddenForTests.get(); + } + + return (providerPair.first.size() == 0 || providerPair.second == null); } @VisibleForTesting @@ -459,10 +472,11 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl return preference; } - /** Aggregates the list of services and builds a list of UI prefs to show. */ - @VisibleForTesting - public Map buildPreferenceList( - Context context, PreferenceGroup group) { + /** + * Returns a pair that contains a list of the providers in the first position and the top + * provider in the second position. + */ + private Pair, CombinedProviderInfo> getProviders() { // Get the selected autofill provider. If it is the placeholder then replace it with an // empty string. String selectedAutofillProvider = @@ -475,15 +489,25 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl // Get the list of combined providers. List providers = CombinedProviderInfo.buildMergedList( - AutofillServiceInfo.getAvailableServices(context, getUser()), + AutofillServiceInfo.getAvailableServices(mContext, getUser()), mServices, selectedAutofillProvider); + return new Pair<>(providers, CombinedProviderInfo.getTopProvider(providers)); + } - // Get the provider that is displayed at the top. If there is none then hide - // everything. - CombinedProviderInfo topProvider = CombinedProviderInfo.getTopProvider(providers); - if (topProvider == null) { - setVisibility(false); + /** Aggregates the list of services and builds a list of UI prefs to show. */ + @VisibleForTesting + public @NonNull Map buildPreferenceList( + @NonNull Context context, @NonNull PreferenceGroup group) { + // Get the providers and extract the values. + Pair, CombinedProviderInfo> providerPair = getProviders(); + CombinedProviderInfo topProvider = providerPair.second; + List providers = providerPair.first; + + // If the provider is set to "none" or there are no providers then we should not + // return any providers. + if (isHiddenDueToNoProviderSet(providerPair)) { + forceDelegateRefresh(); return new HashMap<>(); } @@ -520,7 +544,7 @@ public class CredentialManagerPreferenceController extends BasePreferenceControl } // Set the visibility if we have services. - setVisibility(!output.isEmpty()); + forceDelegateRefresh(); return output; } diff --git a/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java b/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java index 18ad2109405..3d85ca2b0bc 100644 --- a/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java +++ b/src/com/android/settings/bluetooth/BluetoothDetailsHearingDeviceControlsController.java @@ -38,7 +38,8 @@ import com.google.common.annotations.VisibleForTesting; public class BluetoothDetailsHearingDeviceControlsController extends BluetoothDetailsController implements Preference.OnPreferenceClickListener { - private static final String KEY_DEVICE_CONTROLS_GENERAL_GROUP = "device_controls_general"; + @VisibleForTesting + static final String KEY_DEVICE_CONTROLS_GENERAL_GROUP = "device_controls_general"; @VisibleForTesting static final String KEY_HEARING_DEVICE_CONTROLS = "hearing_device_controls"; diff --git a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java index 5e41a202812..9c68c9cc870 100644 --- a/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java +++ b/src/com/android/settings/bluetooth/BluetoothDeviceDetailsFragment.java @@ -22,6 +22,7 @@ import static android.os.UserManager.DISALLOW_CONFIG_BLUETOOTH; import android.app.settings.SettingsEnums; import android.bluetooth.BluetoothDevice; import android.content.Context; +import android.content.Intent; import android.content.res.TypedArray; import android.hardware.input.InputManager; import android.net.Uri; @@ -53,6 +54,7 @@ import com.android.settings.slices.SlicePreferenceController; import com.android.settingslib.bluetooth.CachedBluetoothDevice; import com.android.settingslib.bluetooth.LocalBluetoothManager; import com.android.settingslib.core.AbstractPreferenceController; +import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; import com.android.settingslib.core.lifecycle.Lifecycle; import java.util.ArrayList; @@ -324,8 +326,11 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment lifecycle)); controllers.add(new BluetoothDetailsPairOtherController(context, this, mCachedDevice, lifecycle)); - controllers.add(new BluetoothDetailsHearingDeviceControlsController(context, this, - mCachedDevice, lifecycle)); + // Don't need to show hearing device again when launched from the same page. + if (!isLaunchFromHearingDevicePage()) { + controllers.add(new BluetoothDetailsHearingDeviceControlsController(context, this, + mCachedDevice, lifecycle)); + } controllers.add(new BluetoothDetailsDataSyncController(context, this, mCachedDevice, lifecycle)); controllers.add( @@ -348,6 +353,16 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment return width; } + private boolean isLaunchFromHearingDevicePage() { + final Intent intent = getIntent(); + if (intent == null) { + return false; + } + + return intent.getIntExtra(MetricsFeatureProvider.EXTRA_SOURCE_METRICS_CATEGORY, + SettingsEnums.PAGE_UNKNOWN) == SettingsEnums.ACCESSIBILITY_HEARING_AID_SETTINGS; + } + @VisibleForTesting void setTitleForInputDevice() { if (StylusDevicesController.isDeviceStylus(mInputDevice, mCachedDevice)) { diff --git a/src/com/android/settings/fuelgauge/BatterySettingsFeatureProvider.java b/src/com/android/settings/fuelgauge/BatterySettingsFeatureProvider.java index cc333a53879..62be5dfc365 100644 --- a/src/com/android/settings/fuelgauge/BatterySettingsFeatureProvider.java +++ b/src/com/android/settings/fuelgauge/BatterySettingsFeatureProvider.java @@ -18,6 +18,7 @@ package com.android.settings.fuelgauge; import android.content.Context; +import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy; import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; import java.util.List; @@ -35,5 +36,9 @@ public interface BatterySettingsFeatureProvider { boolean isBatteryInfoEnabled(Context context); /** A way to add more battery tip detectors. */ - void addBatteryTipDetector(Context context, List tips, BatteryInfo batteryInfo); + void addBatteryTipDetector( + Context context, + List batteryTips, + BatteryInfo batteryInfo, + BatteryTipPolicy batteryTipPolicy); } diff --git a/src/com/android/settings/fuelgauge/BatterySettingsFeatureProviderImpl.java b/src/com/android/settings/fuelgauge/BatterySettingsFeatureProviderImpl.java index f974b9d7df6..f39837370d1 100644 --- a/src/com/android/settings/fuelgauge/BatterySettingsFeatureProviderImpl.java +++ b/src/com/android/settings/fuelgauge/BatterySettingsFeatureProviderImpl.java @@ -18,6 +18,8 @@ package com.android.settings.fuelgauge; import android.content.Context; +import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy; +import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector; import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; import java.util.List; @@ -42,5 +44,10 @@ public class BatterySettingsFeatureProviderImpl implements BatterySettingsFeatur @Override public void addBatteryTipDetector( - Context context, List tips, BatteryInfo batteryInfo) {} + Context context, + List batteryTips, + BatteryInfo batteryInfo, + BatteryTipPolicy batteryTipPolicy) { + batteryTips.add(new LowBatteryDetector(context, batteryTipPolicy, batteryInfo).detect()); + } } diff --git a/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java b/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java index 53521050c8e..d68bf39295c 100644 --- a/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java +++ b/src/com/android/settings/fuelgauge/batterytip/BatteryTipLoader.java @@ -18,7 +18,6 @@ package com.android.settings.fuelgauge.batterytip; import android.content.Context; import android.os.BatteryUsageStats; -import android.os.PowerManager; import androidx.annotation.VisibleForTesting; @@ -27,7 +26,6 @@ import com.android.settings.fuelgauge.BatteryUtils; import com.android.settings.fuelgauge.batterytip.detectors.BatteryDefenderDetector; import com.android.settings.fuelgauge.batterytip.detectors.HighUsageDetector; import com.android.settings.fuelgauge.batterytip.detectors.IncompatibleChargerDetector; -import com.android.settings.fuelgauge.batterytip.detectors.LowBatteryDetector; import com.android.settings.fuelgauge.batterytip.tips.BatteryTip; import com.android.settings.overlay.FeatureFactory; import com.android.settingslib.utils.AsyncLoaderCompat; @@ -56,19 +54,18 @@ public class BatteryTipLoader extends AsyncLoaderCompat> { @Override public List loadInBackground() { final List tips = new ArrayList<>(); - final BatteryTipPolicy policy = new BatteryTipPolicy(getContext()); + final BatteryTipPolicy batteryTipPolicy = new BatteryTipPolicy(getContext()); final BatteryInfo batteryInfo = mBatteryUtils.getBatteryInfo(TAG); final Context context = getContext().getApplicationContext(); - final boolean isPowerSaveMode = - context.getSystemService(PowerManager.class).isPowerSaveMode(); - tips.add(new LowBatteryDetector(context, policy, batteryInfo, isPowerSaveMode).detect()); - tips.add(new HighUsageDetector(context, policy, mBatteryUsageStats, batteryInfo).detect()); + tips.add( + new HighUsageDetector(context, batteryTipPolicy, mBatteryUsageStats, batteryInfo) + .detect()); tips.add(new BatteryDefenderDetector(batteryInfo, context).detect()); tips.add(new IncompatibleChargerDetector(context).detect()); FeatureFactory.getFeatureFactory() .getBatterySettingsFeatureProvider() - .addBatteryTipDetector(context, tips, batteryInfo); + .addBatteryTipDetector(context, tips, batteryInfo, batteryTipPolicy); Collections.sort(tips); return tips; } diff --git a/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java b/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java index 1ce5a8ec891..b1a15627407 100644 --- a/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java +++ b/src/com/android/settings/fuelgauge/batterytip/detectors/LowBatteryDetector.java @@ -17,6 +17,7 @@ package com.android.settings.fuelgauge.batterytip.detectors; import android.content.Context; +import android.os.PowerManager; import com.android.settings.fuelgauge.BatteryInfo; import com.android.settings.fuelgauge.batterytip.BatteryTipPolicy; @@ -26,37 +27,33 @@ import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip; /** Detect whether the battery is too low */ public class LowBatteryDetector implements BatteryTipDetector { private final BatteryInfo mBatteryInfo; - private final BatteryTipPolicy mPolicy; + private final BatteryTipPolicy mBatteryTipPolicy; private final boolean mIsPowerSaveMode; private final int mWarningLevel; public LowBatteryDetector( - Context context, - BatteryTipPolicy policy, - BatteryInfo batteryInfo, - boolean isPowerSaveMode) { - mPolicy = policy; + Context context, BatteryTipPolicy batteryTipPolicy, BatteryInfo batteryInfo) { + mBatteryTipPolicy = batteryTipPolicy; mBatteryInfo = batteryInfo; mWarningLevel = context.getResources() .getInteger(com.android.internal.R.integer.config_lowBatteryWarningLevel); - mIsPowerSaveMode = isPowerSaveMode; + mIsPowerSaveMode = context.getSystemService(PowerManager.class).isPowerSaveMode(); } @Override public BatteryTip detect() { final boolean lowBattery = mBatteryInfo.batteryLevel <= mWarningLevel; - final boolean lowBatteryEnabled = mPolicy.lowBatteryEnabled && !mIsPowerSaveMode; + final boolean lowBatteryEnabled = mBatteryTipPolicy.lowBatteryEnabled && !mIsPowerSaveMode; final boolean dischargingLowBatteryState = - mPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery); - - int state = BatteryTip.StateType.INVISIBLE; + mBatteryTipPolicy.testLowBatteryTip || (mBatteryInfo.discharging && lowBattery); // Show it as new if in test or in discharging low battery state, // dismiss it if battery saver is on or disabled by config. - if (lowBatteryEnabled && dischargingLowBatteryState) { - state = BatteryTip.StateType.NEW; - } + final int state = + lowBatteryEnabled && dischargingLowBatteryState + ? BatteryTip.StateType.NEW + : BatteryTip.StateType.INVISIBLE; return new LowBatteryTip(state, mIsPowerSaveMode); } diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java index 047bf135741..319ba7a16c8 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/BatteryDefenderTip.java @@ -16,12 +16,14 @@ package com.android.settings.fuelgauge.batterytip.tips; +import android.app.Activity; import android.app.settings.SettingsEnums; import android.content.Context; import android.content.Intent; import android.os.Parcel; import android.util.Log; +import androidx.core.app.ActivityCompat; import androidx.preference.Preference; import com.android.settings.R; @@ -30,6 +32,8 @@ import com.android.settings.widget.CardPreference; import com.android.settingslib.HelpUtils; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; +import kotlin.Unit; + /** Tip to show current battery is overheated */ public class BatteryDefenderTip extends BatteryTip { @@ -83,28 +87,39 @@ public class BatteryDefenderTip extends BatteryTip { } cardPreference.setSelectable(false); + cardPreference.setIconResId(getIconId()); cardPreference.setPrimaryButtonText(context.getString(R.string.learn_more)); - cardPreference.setPrimaryButtonClickListener( - button -> - button.startActivityForResult( - HelpUtils.getHelpIntent( - context, - context.getString(R.string.help_url_battery_defender), - /* backupContext */ ""), /* requestCode */ - 0)); - cardPreference.setPrimaryButtonVisible(true); + cardPreference.setPrimaryButtonAction( + () -> { + var helpIntent = + HelpUtils.getHelpIntent( + context, + context.getString(R.string.help_url_battery_defender), + /* backupContext= */ ""); + ActivityCompat.startActivityForResult( + (Activity) preference.getContext(), + helpIntent, + /* requestCode= */ 0, + /* options= */ null); + + return Unit.INSTANCE; + }); + cardPreference.setPrimaryButtonVisibility(true); cardPreference.setPrimaryButtonContentDescription( context.getString( R.string.battery_tip_limited_temporarily_sec_button_content_description)); cardPreference.setSecondaryButtonText( context.getString(R.string.battery_tip_charge_to_full_button)); - cardPreference.setSecondaryButtonClickListener( - unused -> { + cardPreference.setSecondaryButtonAction( + () -> { resumeCharging(context); preference.setVisible(false); + + return Unit.INSTANCE; }); - cardPreference.setSecondaryButtonVisible(mIsPluggedIn); + cardPreference.setSecondaryButtonVisibility(mIsPluggedIn); + cardPreference.buildContent(); } private void resumeCharging(Context context) { diff --git a/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java b/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java index 882b755a28f..c9ff8641ceb 100644 --- a/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java +++ b/src/com/android/settings/fuelgauge/batterytip/tips/IncompatibleChargerTip.java @@ -16,11 +16,13 @@ package com.android.settings.fuelgauge.batterytip.tips; +import android.app.Activity; import android.app.settings.SettingsEnums; import android.content.Context; import android.os.Parcel; import android.util.Log; +import androidx.core.app.ActivityCompat; import androidx.preference.Preference; import com.android.settings.R; @@ -28,6 +30,8 @@ import com.android.settings.widget.CardPreference; import com.android.settingslib.HelpUtils; import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; +import kotlin.Unit; + /** Tip to show incompatible charger state */ public final class IncompatibleChargerTip extends BatteryTip { private static final String TAG = "IncompatibleChargerTip"; @@ -77,18 +81,27 @@ public final class IncompatibleChargerTip extends BatteryTip { } cardPreference.setSelectable(false); + cardPreference.setIconResId(getIconId()); cardPreference.setPrimaryButtonText(context.getString(R.string.learn_more)); - cardPreference.setPrimaryButtonClickListener( - button -> - button.startActivityForResult( - HelpUtils.getHelpIntent( - context, - context.getString(R.string.help_url_incompatible_charging), - /* backupContext */ ""), /* requestCode */ - 0)); - cardPreference.setPrimaryButtonVisible(true); + cardPreference.setPrimaryButtonAction( + () -> { + var helpIntent = + HelpUtils.getHelpIntent( + context, + context.getString(R.string.help_url_incompatible_charging), + /* backupContext */ ""); + ActivityCompat.startActivityForResult( + (Activity) context, + helpIntent, + /* requestCode= */ 0, + /* options= */ null); + + return Unit.INSTANCE; + }); + cardPreference.setPrimaryButtonVisibility(true); cardPreference.setPrimaryButtonContentDescription( context.getString(R.string.battery_tip_incompatible_charging_content_description)); + cardPreference.buildContent(); } public static final Creator CREATOR = diff --git a/src/com/android/settings/widget/CardPreference.java b/src/com/android/settings/widget/CardPreference.java deleted file mode 100644 index 61114d959f2..00000000000 --- a/src/com/android/settings/widget/CardPreference.java +++ /dev/null @@ -1,170 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.settings.widget; - -import android.content.Context; -import android.util.AttributeSet; -import android.view.View; -import android.widget.Button; - -import androidx.preference.Preference; -import androidx.preference.PreferenceViewHolder; - -import com.android.settings.R; - -import com.google.android.material.card.MaterialCardView; - -import java.util.Optional; - -/** Preference that wrapped by {@link MaterialCardView} */ -public class CardPreference extends Preference { - - private View.OnClickListener mPrimaryBtnClickListener = null; - private View.OnClickListener mSecondaryBtnClickListener = null; - - private String mPrimaryButtonText = null; - private String mSecondaryButtonText = null; - - private Optional