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.settings.spa
18 
19 import android.app.Activity
20 import android.content.pm.PackageManager
21 import androidx.annotation.VisibleForTesting
22 import com.android.settings.SettingsActivity.META_DATA_KEY_HIGHLIGHT_MENU_KEY
23 
24 data class SpaDestination(
25     val destination: String,
26     val highlightMenuKey: String?,
27 ) {
28     companion object {
getDestinationnull29         fun Activity.getDestination(
30             destinationFactory: (String) -> String? = { it },
31         ): SpaDestination? {
32             val metaData = packageManager.getActivityInfo(
33                 componentName,
34                 PackageManager.ComponentInfoFlags.of(PackageManager.GET_META_DATA.toLong())
35             ).metaData
36             val destination = metaData.getString(META_DATA_KEY_DESTINATION)
37             if (destination.isNullOrBlank()) return null
38             val finalDestination = destinationFactory(destination)
39             if (finalDestination.isNullOrBlank()) return null
40             return SpaDestination(
41                 destination = finalDestination,
42                 highlightMenuKey = metaData.getString(META_DATA_KEY_HIGHLIGHT_MENU_KEY),
43             )
44         }
45 
46         @VisibleForTesting
47         const val META_DATA_KEY_DESTINATION = "com.android.settings.spa.DESTINATION"
48     }
49 }
50