1 /*
2  * Copyright (C) 2023 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.healthconnect.controller.utils.activity
18 
19 import android.app.Activity
20 import android.content.Intent
21 import android.provider.Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY
22 import android.provider.Settings.EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI
23 import android.util.Log
24 import com.android.settingslib.activityembedding.ActivityEmbeddingUtils.*
25 
26 object EmbeddingUtils {
27 
28     private const val TAG = "EmbeddingUtils"
29     private const val MENU_KEY_HEALTH_CONNECT = "top_level_privacy"
30 
maybeRedirectIntoTwoPaneSettingsnull31     fun maybeRedirectIntoTwoPaneSettings(activity: Activity): Boolean {
32         return shouldUseTwoPaneSettings(activity) && tryRedirectTwoPaneSettings(activity)
33     }
34 
shouldUseTwoPaneSettingsnull35     private fun shouldUseTwoPaneSettings(activity: Activity): Boolean {
36         if (!isEmbeddingActivityEnabled(activity)) {
37             return false
38         }
39         return activity.isTaskRoot &&
40             !isActivityEmbedded(activity) &&
41             !isEmbeddedIntent(activity.intent)
42     }
43 
isEmbeddedIntentnull44     private fun isEmbeddedIntent(intent: Intent): Boolean {
45         return intent.hasExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI)
46     }
47 
tryRedirectTwoPaneSettingsnull48     private fun tryRedirectTwoPaneSettings(activity: Activity): Boolean {
49         try {
50             val twoPaneIntent = getTwoPaneIntent(activity) ?: return false
51             Log.i(TAG, "Health Connect restarting in Settings two-pane layout.")
52             activity.startActivity(twoPaneIntent)
53             activity.finishAndRemoveTask()
54             return true
55         } catch (ex: Exception) {
56             Log.i(TAG, "Failed to restart Health Connect in Settings two-pane layout.")
57             return false
58         }
59     }
60 
getTwoPaneIntentnull61     private fun getTwoPaneIntent(activity: Activity): Intent? {
62         val twoPaneIntent = buildEmbeddingActivityBaseIntent(activity)
63         return twoPaneIntent?.apply {
64             putExtras(activity.intent)
65             setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
66             putExtra(EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_HIGHLIGHT_MENU_KEY, MENU_KEY_HEALTH_CONNECT)
67             putExtra(
68                 EXTRA_SETTINGS_EMBEDDED_DEEP_LINK_INTENT_URI,
69                 activity.intent.toUri(Intent.URI_INTENT_SCHEME))
70         }
71     }
72 }
73