Update ActivityEmbeddingUtils

- Provide base Intent in the two-pane Settings API.
  By returning an Intent with ComponentName, this makes it easier
  to embed the Intent into a SaferPendingIntent.
- Only support API for scv2 platform.
- Update BuildCompatUtils to support isAtLeastSV2 and isAtLeastT.

Bug: 227563999
Test: rebuild
Change-Id: Ib054c5c37e29d43662bcf9b0d86ecbee7c2b921d
This commit is contained in:
Edgar Wang 2022-03-31 17:19:34 +08:00
parent 0c36369480
commit f531535036
2 changed files with 77 additions and 21 deletions

View File

@ -17,10 +17,12 @@
package com.android.settingslib.activityembedding;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import androidx.core.os.BuildCompat;
import androidx.window.embedding.SplitController;
@ -30,24 +32,52 @@ import com.android.settingslib.utils.BuildCompatUtils;
/**
* An util class collecting all common methods for the embedding activity features.
*/
public class ActivityEmbeddingUtils {
public final class ActivityEmbeddingUtils {
private static final String ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY =
"android.settings.SETTINGS_EMBED_DEEP_LINK_ACTIVITY";
private static final String PACKAGE_NAME_SETTINGS = "com.android.settings";
private static final String TAG = "ActivityEmbeddingUtils";
/**
* Whether to support embedding activity feature.
* Whether the embedding activity feature is enabled.
*
* <p>This returns false if the Android version is below S or if the embedding activity is not
* enabled (unsupported devices).
*/
public static boolean isEmbeddingActivityEnabled(Context context) {
if (BuildCompatUtils.isAtLeastS()) {
final Intent intent = new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY);
intent.setPackage(PACKAGE_NAME_SETTINGS);
final boolean isEmbeddingActivityEnabled =
intent.resolveActivity(context.getPackageManager()) != null;
final boolean isEmbeddingActivityEnabled = getEmbeddingActivityComponent(context) != null;
Log.d(TAG, "isEmbeddingActivityEnabled : " + isEmbeddingActivityEnabled);
return isEmbeddingActivityEnabled;
}
return isEmbeddingActivityEnabled;
/**
* Returns a base Intent to the embedding activity (without the extras).
*
* <p>This returns null if the Android version is below S or if the embedding activity is not
* enabled (unsupported devices).
*/
public static Intent buildEmbeddingActivityBaseIntent(Context context) {
ComponentName embeddingActivityComponentName = getEmbeddingActivityComponent(context);
if (embeddingActivityComponentName == null) {
return null;
}
return false;
return new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY)
.setComponent(embeddingActivityComponentName);
}
/**
* Returns the ComponentName associated with the embedding activity.
*
* <p>This returns null if the Android version is below S or if the embedding activity is not
* enabled (unsupported devices).
*/
private static ComponentName getEmbeddingActivityComponent(Context context) {
if (!BuildCompatUtils.isAtLeastSV2()) {
return null;
}
final Intent intent = new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY);
intent.setPackage(PACKAGE_NAME_SETTINGS);
return intent.resolveActivity(context.getPackageManager());
}
/**
@ -62,17 +92,19 @@ public class ActivityEmbeddingUtils {
/**
* Whether the current activity should hide the navigate up button.
*
* @param activity Activity that needs the check
* @param activity Activity that needs the check
* @param isSecondLayerPage indicates if the activity(page) is shown in the 2nd layer of
* Settings app
* Settings app
*/
public static boolean shouldHideNavigateUpButton(Activity activity, boolean isSecondLayerPage) {
if (!BuildCompat.isAtLeastT()) {
return false;
}
if (!isSecondLayerPage) {
return false;
}
final String shouldHideNavigateUpButton =
Settings.Global.getString(activity.getContentResolver(),
"settings_hide_second_layer_page_navigate_up_button_in_two_pane");

View File

@ -16,14 +16,37 @@
package com.android.settingslib.utils;
import android.os.Build;
import android.os.Build.VERSION;
import androidx.annotation.ChecksSdkIntAtLeast;
/**
* 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.isAtLeastS() suitable for use in Settings
*
* @return Whether the current OS version is higher or equal to S.
*/
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S)
public static boolean isAtLeastS() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S;
}
/**
* Implementation of BuildCompat.isAtLeastS() suitable for use in Settings
*
* @return Whether the current OS version is higher or equal to Sv2.
*/
@ChecksSdkIntAtLeast(api = Build.VERSION_CODES.S_V2)
public static boolean isAtLeastSV2() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2;
}
/**
* Implementation of BuildCompat.isAtLeast*() suitable for use in Settings
*
@ -35,26 +58,27 @@ public final class BuildCompatUtils {
* <p>Supported configurations:
*
* <ul>
* <li>For current Android release: when new API is not finalized yet (CODENAME = "S", SDK_INT
* = 30|31)
* <li>For current Android release: when new API is finalized (CODENAME = "REL", SDK_INT = 31)
* <li>For next Android release (CODENAME = "T", SDK_INT = 30+)
* <li>For current Android release: when new API is not finalized yet (CODENAME = "Tiramisu",
* SDK_INT = 32)
* <li>For current Android release: when new API is finalized (CODENAME = "REL", SDK_INT = 33)
* <li>For next Android release (CODENAME = "U", SDK_INT = 34+)
* </ul>
*
* <p>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.
* @return Whether the current OS version is higher or equal to T.
*/
public static boolean isAtLeastS() {
if (VERSION.SDK_INT < 30) {
public static boolean isAtLeastT() {
if (!isAtLeastS()) {
return false;
}
return (VERSION.CODENAME.equals("REL") && VERSION.SDK_INT >= 31)
return (VERSION.CODENAME.equals("REL") && VERSION.SDK_INT >= 33)
|| (VERSION.CODENAME.length() >= 1
&& VERSION.CODENAME.toUpperCase().charAt(0) >= 'S'
&& VERSION.CODENAME.toUpperCase().charAt(0) <= 'Z');
&& VERSION.CODENAME.toUpperCase().charAt(0) >= 'T'
&& VERSION.CODENAME.toUpperCase().charAt(0) <= 'Z')
|| (Build.VERSION.CODENAME.equals("Tiramisu") && Build.VERSION.SDK_INT >= 32);
}
private BuildCompatUtils() {}