1 /* 2 * Copyright (C) 2021 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.activityembedding; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.os.SystemProperties; 22 import android.util.DisplayMetrics; 23 import android.util.FeatureFlagUtils; 24 import android.util.Log; 25 import android.util.TypedValue; 26 27 import androidx.window.embedding.ActivityEmbeddingController; 28 import androidx.window.embedding.SplitController; 29 30 import com.android.settings.R; 31 import com.android.window.flags.Flags; 32 33 import com.google.android.setupcompat.util.WizardManagerHelper; 34 35 /** An util class collecting all common methods for the embedding activity features. */ 36 public class ActivityEmbeddingUtils { 37 // The minimum width of the activity to show the regular homepage layout. 38 private static final float MIN_REGULAR_HOMEPAGE_LAYOUT_WIDTH_DP = 380f; 39 40 /** 41 * Indicates whether to enable large screen optimization if the device supports 42 * the Activity Embedding split feature. 43 * <p> 44 * Note that the large screen optimization won't be enabled if the device doesn't support the 45 * Activity Embedding feature regardless of this property value. 46 * 47 * @see androidx.window.embedding.SplitController#getSplitSupportStatus 48 * @see androidx.window.embedding.SplitController.SplitSupportStatus#SPLIT_AVAILABLE 49 * @see androidx.window.embedding.SplitController.SplitSupportStatus#SPLIT_UNAVAILABLE 50 */ 51 private static final boolean SHOULD_ENABLE_LARGE_SCREEN_OPTIMIZATION = 52 SystemProperties.getBoolean("persist.settings.large_screen_opt.enabled", false) 53 || (Flags.activityEmbeddingSupportForConnectedDisplays() 54 && SystemProperties.getBoolean( 55 "persist.settings.large_screen_opt_for_dp.enabled", false)); 56 57 private static final String TAG = "ActivityEmbeddingUtils"; 58 59 /** Get the smallest width dp of the window when the split should be used. */ getMinCurrentScreenSplitWidthDp(Context context)60 public static int getMinCurrentScreenSplitWidthDp(Context context) { 61 return context.getResources().getInteger(R.integer.config_activity_embed_split_min_cur_dp); 62 } 63 64 /** 65 * Get the smallest dp value of the smallest-width (sw) of the window in any rotation when 66 * the split should be used. 67 */ getMinSmallestScreenSplitWidthDp(Context context)68 public static int getMinSmallestScreenSplitWidthDp(Context context) { 69 return context.getResources().getInteger(R.integer.config_activity_embed_split_min_sw_dp); 70 } 71 72 /** 73 * Get the ratio to use when splitting windows. This should be a float which describes 74 * the percentage of the screen which the first window should occupy. 75 */ getSplitRatio(Context context)76 public static float getSplitRatio(Context context) { 77 return context.getResources().getFloat(R.dimen.config_activity_embed_split_ratio); 78 } 79 80 /** 81 * Returns {@code true} to indicate that Settings app support the Activity Embedding feature on 82 * this device. Returns {@code false}, otherwise. 83 */ isSettingsSplitEnabled(Context context)84 public static boolean isSettingsSplitEnabled(Context context) { 85 return SHOULD_ENABLE_LARGE_SCREEN_OPTIMIZATION 86 && SplitController.getInstance(context).getSplitSupportStatus() 87 == SplitController.SplitSupportStatus.SPLIT_AVAILABLE; 88 } 89 90 /** 91 * Checks whether to support embedding activity feature with following conditions: 92 * <ul> 93 * <li>Whether {@link #isSettingsSplitEnabled(Context)}</li> 94 * <li>Whether {@link FeatureFlagUtils#SETTINGS_SUPPORT_LARGE_SCREEN} is enabled</li> 95 * <li>Whether User setup is completed</li> 96 * </ul> 97 */ isEmbeddingActivityEnabled(Context context)98 public static boolean isEmbeddingActivityEnabled(Context context) { 99 // Activity Embedding feature is not enabled if Settings doesn't enable large screen 100 // optimization or the device is not supported. 101 if (!isSettingsSplitEnabled(context)) { 102 Log.d(TAG, "isSettingsSplitSupported = false"); 103 return false; 104 } 105 // Activity Embedding feature is not enabled if a user chooses to disable the feature. 106 if (!FeatureFlagUtils.isEnabled(context, FeatureFlagUtils.SETTINGS_SUPPORT_LARGE_SCREEN)) { 107 Log.d(TAG, "isFlagEnabled = false"); 108 return false; 109 } 110 // Don't enable Activity embedding for setup wizard. 111 if (!WizardManagerHelper.isUserSetupComplete(context)) { 112 Log.d(TAG, "isUserSetupComplete = false"); 113 return false; 114 } 115 Log.d(TAG, "isEmbeddingActivityEnabled = true"); 116 return true; 117 } 118 119 /** Whether to show the regular or simplified homepage layout. */ isRegularHomepageLayout(Activity activity)120 public static boolean isRegularHomepageLayout(Activity activity) { 121 DisplayMetrics dm = activity.getResources().getDisplayMetrics(); 122 return dm.widthPixels >= (int) TypedValue.applyDimension( 123 TypedValue.COMPLEX_UNIT_DIP, MIN_REGULAR_HOMEPAGE_LAYOUT_WIDTH_DP, dm); 124 } 125 126 /** 127 * Check if activity is already embedded 128 */ isAlreadyEmbedded(Activity activity)129 public static boolean isAlreadyEmbedded(Activity activity) { 130 return isEmbeddingActivityEnabled(activity) && ActivityEmbeddingController.getInstance( 131 activity).isActivityEmbedded(activity); 132 } 133 } 134