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.app.ActivityTaskManager;
21 import android.content.Context;
22 import android.graphics.Rect;
23 import android.util.DisplayMetrics;
24 import android.util.FeatureFlagUtils;
25 import android.util.Log;
26 import android.util.TypedValue;
27 
28 import androidx.window.embedding.SplitController;
29 
30 /** An util class collecting all common methods for the embedding activity features. */
31 public class ActivityEmbeddingUtils {
32     public static final float SPLIT_RATIO = 0.5f;
33     // The smallest value of current width of the window when the split should be used.
34     private static final float MIN_CURRENT_SCREEN_SPLIT_WIDTH_DP = 720f;
35     // The smallest value of the smallest-width (sw) of the window in any rotation when
36     // the split should be used.
37     private static final float MIN_SMALLEST_SCREEN_SPLIT_WIDTH_DP = 600f;
38     private static final String TAG = "ActivityEmbeddingUtils";
39 
40     /** Get the smallest pixel value of width of the window when the split should be used. */
getMinCurrentScreenSplitWidthPx(Context context)41     public static int getMinCurrentScreenSplitWidthPx(Context context) {
42         final DisplayMetrics dm = context.getResources().getDisplayMetrics();
43         return (int) TypedValue.applyDimension(
44                 TypedValue.COMPLEX_UNIT_DIP, MIN_CURRENT_SCREEN_SPLIT_WIDTH_DP, dm);
45     }
46 
47     /**
48      * Get the smallest pixel value of the smallest-width (sw) of the window in any rotation when
49      * the split should be used.
50      */
getMinSmallestScreenSplitWidthPx(Context context)51     public static int getMinSmallestScreenSplitWidthPx(Context context) {
52         final DisplayMetrics dm = context.getResources().getDisplayMetrics();
53         return (int) TypedValue.applyDimension(
54                 TypedValue.COMPLEX_UNIT_DIP, MIN_SMALLEST_SCREEN_SPLIT_WIDTH_DP, dm);
55     }
56 
57     /** Whether to support embedding activity feature. */
isEmbeddingActivityEnabled(Context context)58     public static boolean isEmbeddingActivityEnabled(Context context) {
59         final boolean isFlagEnabled = FeatureFlagUtils.isEnabled(context,
60                 FeatureFlagUtils.SETTINGS_SUPPORT_LARGE_SCREEN);
61         final boolean isSplitSupported = SplitController.getInstance().isSplitSupported();
62 
63         Log.d(TAG, "isFlagEnabled = " + isFlagEnabled);
64         Log.d(TAG, "isSplitSupported = " + isSplitSupported);
65 
66         return isFlagEnabled && isSplitSupported;
67     }
68 
69     /** Whether the screen meets two-pane resolution. */
isTwoPaneResolution(Activity activity)70     public static boolean isTwoPaneResolution(Activity activity) {
71         final Rect currentTaskBounds =
72                 ActivityTaskManager.getInstance().getTaskBounds(activity.getTaskId());
73 
74         return currentTaskBounds.width() >= getMinCurrentScreenSplitWidthPx(activity)
75                 && currentTaskBounds.height() >= getMinSmallestScreenSplitWidthPx(activity);
76     }
77 }
78