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