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.settingslib.activityembedding;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.util.Log;
24 
25 import androidx.core.os.BuildCompat;
26 import androidx.window.embedding.ActivityEmbeddingController;
27 
28 import com.android.settingslib.utils.BuildCompatUtils;
29 
30 /**
31  * An util class collecting all common methods for the embedding activity features.
32  */
33 public final class ActivityEmbeddingUtils {
34     private static final String ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY =
35             "android.settings.SETTINGS_EMBED_DEEP_LINK_ACTIVITY";
36     private static final String PACKAGE_NAME_SETTINGS = "com.android.settings";
37     private static final String TAG = "ActivityEmbeddingUtils";
38 
39     /**
40      * Whether the embedding activity feature is enabled.
41      *
42      * <p>This returns false if the Android version is below S or if the embedding activity is not
43      * enabled (unsupported devices).
44      */
isEmbeddingActivityEnabled(Context context)45     public static boolean isEmbeddingActivityEnabled(Context context) {
46         final boolean isEmbeddingActivityEnabled = getEmbeddingActivityComponent(context) != null;
47         Log.d(TAG, "isEmbeddingActivityEnabled : " + isEmbeddingActivityEnabled);
48         return isEmbeddingActivityEnabled;
49     }
50 
51     /**
52      * Returns a base Intent to the embedding activity (without the extras).
53      *
54      * <p>This returns null if the Android version is below S or if the embedding activity is not
55      * enabled (unsupported devices).
56      */
buildEmbeddingActivityBaseIntent(Context context)57     public static Intent buildEmbeddingActivityBaseIntent(Context context) {
58         ComponentName embeddingActivityComponentName = getEmbeddingActivityComponent(context);
59         if (embeddingActivityComponentName == null) {
60             return null;
61         }
62         return new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY)
63                 .setComponent(embeddingActivityComponentName);
64     }
65 
66     /**
67      * Returns the ComponentName associated with the embedding activity.
68      *
69      * <p>This returns null if the Android version is below S or if the embedding activity is not
70      * enabled (unsupported devices).
71      */
getEmbeddingActivityComponent(Context context)72     private static ComponentName getEmbeddingActivityComponent(Context context) {
73         if (!BuildCompatUtils.isAtLeastSV2()) {
74             return null;
75         }
76         final Intent intent = new Intent(ACTION_SETTINGS_EMBED_DEEP_LINK_ACTIVITY);
77         intent.setPackage(PACKAGE_NAME_SETTINGS);
78         return intent.resolveActivity(context.getPackageManager());
79     }
80 
81     /**
82      * Whether the current activity is embedded in the Settings app or not.
83      *
84      * @param activity Activity that needs the check
85      */
isActivityEmbedded(Activity activity)86     public static boolean isActivityEmbedded(Activity activity) {
87         return ActivityEmbeddingController.getInstance(activity).isActivityEmbedded(activity);
88     }
89 
90     /**
91      * Whether the current activity should hide the navigate up button.
92      *
93      * @param activity          Activity that needs the check
94      * @param isSecondLayerPage indicates if the activity(page) is shown in the 2nd layer of
95      *                          Settings app
96      */
shouldHideNavigateUpButton(Activity activity, boolean isSecondLayerPage)97     public static boolean shouldHideNavigateUpButton(Activity activity, boolean isSecondLayerPage) {
98         if (!BuildCompat.isAtLeastT()) {
99             return false;
100         }
101 
102         if (!isSecondLayerPage) {
103             return false;
104         }
105 
106         return isActivityEmbedded(activity);
107     }
108 
ActivityEmbeddingUtils()109     private ActivityEmbeddingUtils() {
110     }
111 }
112