1 /*
2  * Copyright (C) 2022 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.spa
18 
19 import android.app.Activity
20 import android.content.pm.PackageManager.ComponentInfoFlags
21 import android.content.pm.PackageManager.GET_META_DATA
22 import android.os.Bundle
23 import androidx.annotation.VisibleForTesting
24 import com.android.settings.SettingsActivity.META_DATA_KEY_HIGHLIGHT_MENU_KEY
25 
26 /**
27  * Activity used as a bridge to [SpaActivity].
28  *
29  * Since [SpaActivity] is not exported, [SpaActivity] could not be the target activity of
30  * <activity-alias>, otherwise all its pages will be exported. So need this bridge activity to sit
31  * in the middle of <activity-alias> and [SpaActivity].
32  */
33 class SpaBridgeActivity : Activity() {
onCreatenull34     override fun onCreate(savedInstanceState: Bundle?) {
35         super.onCreate(savedInstanceState)
36         startSpaActivityFromBridge()
37         finish()
38     }
39 
40     companion object {
<lambda>null41         fun Activity.startSpaActivityFromBridge(destinationFactory: (String) -> String? = { it }) {
42             getDestination(destinationFactory)?.startFromExportedActivity(this)
43         }
44 
45         @VisibleForTesting
getDestinationnull46         fun Activity.getDestination(
47             destinationFactory: (String) -> String? = { it },
48         ): SpaDestination? {
49             val metaData =
50                 packageManager
51                     .getActivityInfo(componentName, ComponentInfoFlags.of(GET_META_DATA.toLong()))
52                     .metaData
53             val destination = metaData.getString(META_DATA_KEY_DESTINATION)
54             if (destination.isNullOrBlank()) return null
55             val finalDestination = destinationFactory(destination)
56             if (finalDestination.isNullOrBlank()) return null
57             return SpaDestination(
58                 destination = finalDestination,
59                 highlightMenuKey = metaData.getString(META_DATA_KEY_HIGHLIGHT_MENU_KEY),
60             )
61         }
62 
63         @VisibleForTesting
64         const val META_DATA_KEY_DESTINATION = "com.android.settings.spa.DESTINATION"
65     }
66 }
67