From bff4a440ccca9b6a72a53f3ce2ee0cf2ec147c19 Mon Sep 17 00:00:00 2001 From: Edgar Wang Date: Thu, 17 Jun 2021 17:14:02 +0800 Subject: [PATCH] Relace BuildCompat.isAtLeastS to check Build.VERSION.SDK_INT Fixes: 191227729 Test: robotest & manual Change-Id: I4b865b7de33a6801783dd645213fef0cda8d4dd2 --- .../ActionButtonsPreference/Android.bp | 3 +- .../widget/ActionButtonsPreference.java | 5 +- .../BannerMessagePreference/Android.bp | 1 + .../widget/BannerMessagePreference.java | 5 +- .../MainSwitchPreference/Android.bp | 3 +- .../MainSwitchPreference/AndroidManifest.xml | 2 +- .../settingslib/widget/MainSwitchBar.java | 9 +-- .../SettingsLib/Utils/AndroidManifest.xml | 2 +- .../settingslib/utils/BuildCompatUtils.java | 61 +++++++++++++++++++ .../utils/applications/AppUtils.java | 2 +- 10 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 packages/SettingsLib/Utils/src/com/android/settingslib/utils/BuildCompatUtils.java diff --git a/packages/SettingsLib/ActionButtonsPreference/Android.bp b/packages/SettingsLib/ActionButtonsPreference/Android.bp index 51c9c39cdf034..b6e167717023b 100644 --- a/packages/SettingsLib/ActionButtonsPreference/Android.bp +++ b/packages/SettingsLib/ActionButtonsPreference/Android.bp @@ -14,7 +14,8 @@ android_library { resource_dirs: ["res"], static_libs: [ - "androidx.preference_preference", + "androidx.preference_preference", + "SettingsLibUtils", ], sdk_version: "system_current", diff --git a/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java b/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java index 9aa511d610f2a..029c9196fa5e2 100644 --- a/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java +++ b/packages/SettingsLib/ActionButtonsPreference/src/com/android/settingslib/widget/ActionButtonsPreference.java @@ -27,10 +27,11 @@ import android.widget.Button; import androidx.annotation.DrawableRes; import androidx.annotation.StringRes; -import androidx.core.os.BuildCompat; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; +import com.android.settingslib.utils.BuildCompatUtils; + import java.util.ArrayList; import java.util.List; @@ -94,7 +95,7 @@ public class ActionButtonsPreference extends Preference { public void onBindViewHolder(PreferenceViewHolder holder) { super.onBindViewHolder(holder); - final boolean allowedDivider = !BuildCompat.isAtLeastS(); + final boolean allowedDivider = !BuildCompatUtils.isAtLeastS(); holder.setDividerAllowedAbove(allowedDivider); holder.setDividerAllowedBelow(allowedDivider); diff --git a/packages/SettingsLib/BannerMessagePreference/Android.bp b/packages/SettingsLib/BannerMessagePreference/Android.bp index c6a95625a4148..0f7a451e7c8b3 100644 --- a/packages/SettingsLib/BannerMessagePreference/Android.bp +++ b/packages/SettingsLib/BannerMessagePreference/Android.bp @@ -16,6 +16,7 @@ android_library { static_libs: [ "androidx.preference_preference", "SettingsLibSettingsTheme", + "SettingsLibUtils", ], sdk_version: "system_current", diff --git a/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java b/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java index 9e39355e452dc..3a70d657f62ff 100644 --- a/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java +++ b/packages/SettingsLib/BannerMessagePreference/src/com/android/settingslib/widget/BannerMessagePreference.java @@ -35,10 +35,11 @@ import androidx.annotation.ColorInt; import androidx.annotation.ColorRes; import androidx.annotation.RequiresApi; import androidx.annotation.StringRes; -import androidx.core.os.BuildCompat; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; +import com.android.settingslib.utils.BuildCompatUtils; + /** * Banner message is a banner displaying important information (permission request, page error etc), * and provide actions for user to address. It requires a user action to be dismissed. @@ -83,7 +84,7 @@ public class BannerMessagePreference extends Preference { } private static final String TAG = "BannerPreference"; - private static final boolean IS_AT_LEAST_S = BuildCompat.isAtLeastS(); + private static final boolean IS_AT_LEAST_S = BuildCompatUtils.isAtLeastS(); private final BannerMessagePreference.ButtonInfo mPositiveButtonInfo = new BannerMessagePreference.ButtonInfo(); diff --git a/packages/SettingsLib/MainSwitchPreference/Android.bp b/packages/SettingsLib/MainSwitchPreference/Android.bp index 23ee49e70595a..76d1ea78258d6 100644 --- a/packages/SettingsLib/MainSwitchPreference/Android.bp +++ b/packages/SettingsLib/MainSwitchPreference/Android.bp @@ -16,8 +16,9 @@ android_library { static_libs: [ "androidx.preference_preference", "SettingsLibSettingsTheme", + "SettingsLibUtils", ], sdk_version: "system_current", - min_sdk_version: "21", + min_sdk_version: "28", } diff --git a/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml b/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml index 26075295c1555..5817f77eb6d96 100644 --- a/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml +++ b/packages/SettingsLib/MainSwitchPreference/AndroidManifest.xml @@ -19,7 +19,7 @@ package="com.android.settingslib.widget"> diff --git a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java index 91e1b9319f28e..58fc892c53eb3 100644 --- a/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java +++ b/packages/SettingsLib/MainSwitchPreference/src/com/android/settingslib/widget/MainSwitchBar.java @@ -30,7 +30,8 @@ import android.widget.Switch; import android.widget.TextView; import androidx.annotation.ColorInt; -import androidx.core.os.BuildCompat; + +import com.android.settingslib.utils.BuildCompatUtils; import java.util.ArrayList; import java.util.List; @@ -74,7 +75,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec LayoutInflater.from(context).inflate(R.layout.settingslib_main_switch_bar, this); - if (!BuildCompat.isAtLeastS()) { + if (!BuildCompatUtils.isAtLeastS()) { final TypedArray a = context.obtainStyledAttributes( new int[]{android.R.attr.colorAccent}); mBackgroundActivatedColor = a.getColor(0, 0); @@ -207,7 +208,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec mTextView.setEnabled(enabled); mSwitch.setEnabled(enabled); - if (BuildCompat.isAtLeastS()) { + if (BuildCompatUtils.isAtLeastS()) { if (enabled) { mFrameView.setBackground(isChecked() ? mBackgroundOn : mBackgroundOff); } else { @@ -226,7 +227,7 @@ public class MainSwitchBar extends LinearLayout implements CompoundButton.OnChec } private void setBackground(boolean isChecked) { - if (!BuildCompat.isAtLeastS()) { + if (!BuildCompatUtils.isAtLeastS()) { setBackgroundColor(isChecked ? mBackgroundActivatedColor : mBackgroundColor); } else { mFrameView.setBackground(isChecked ? mBackgroundOn : mBackgroundOff); diff --git a/packages/SettingsLib/Utils/AndroidManifest.xml b/packages/SettingsLib/Utils/AndroidManifest.xml index 96d9e518663f9..fd89676ab6d7f 100644 --- a/packages/SettingsLib/Utils/AndroidManifest.xml +++ b/packages/SettingsLib/Utils/AndroidManifest.xml @@ -16,7 +16,7 @@ --> + package="com.android.settingslib.utils"> diff --git a/packages/SettingsLib/Utils/src/com/android/settingslib/utils/BuildCompatUtils.java b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/BuildCompatUtils.java new file mode 100644 index 0000000000000..44f6f5439af3e --- /dev/null +++ b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/BuildCompatUtils.java @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2021 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.settingslib.utils; + +import android.os.Build.VERSION; + +/** + * An util class to check whether the current OS version is higher or equal to sdk version of + * device. + */ +public final class BuildCompatUtils { + + /** + * Implementation of BuildCompat.isAtLeast*() suitable for use in Settings + * + *

This still should try using BuildCompat.isAtLeastR() as source of truth, but also checking + * for VERSION_SDK_INT and VERSION.CODENAME in case when BuildCompat implementation returned + * false. Note that both checks should be >= and not = to make sure that when Android version + * increases (i.e., from R to S), this does not stop working. + * + *

Supported configurations: + * + *

    + *
  • For current Android release: when new API is not finalized yet (CODENAME = "S", SDK_INT + * = 30|31) + *
  • For current Android release: when new API is finalized (CODENAME = "REL", SDK_INT = 31) + *
  • For next Android release (CODENAME = "T", SDK_INT = 30+) + *
+ * + *

Note that Build.VERSION_CODES.S cannot be used here until final SDK is available, because + * it is equal to Build.VERSION_CODES.CUR_DEVELOPMENT before API finalization. + * + * @return Whether the current OS version is higher or equal to S. + */ + public static boolean isAtLeastS() { + if (VERSION.SDK_INT < 30) { + return false; + } + + return (VERSION.CODENAME.equals("REL") && VERSION.SDK_INT >= 31) + || (VERSION.CODENAME.length() == 1 + && VERSION.CODENAME.compareTo("S") >= 0 + && VERSION.CODENAME.compareTo("Z") <= 0); + } + + private BuildCompatUtils() {} +} diff --git a/packages/SettingsLib/Utils/src/com/android/settingslib/utils/applications/AppUtils.java b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/applications/AppUtils.java index 4505dad8ea12b..5dc0b7274408c 100644 --- a/packages/SettingsLib/Utils/src/com/android/settingslib/utils/applications/AppUtils.java +++ b/packages/SettingsLib/Utils/src/com/android/settingslib/utils/applications/AppUtils.java @@ -22,7 +22,7 @@ import android.content.pm.PackageManager; import android.os.UserManager; import android.util.Log; -import com.android.settingslib.widget.R; +import com.android.settingslib.utils.R; public class AppUtils {