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.settingslib.spa.framework.util 18 19 import android.content.ComponentName 20 import android.content.Intent 21 import com.android.settingslib.spa.framework.common.SettingsEntry 22 import com.android.settingslib.spa.framework.common.SettingsPage 23 import com.android.settingslib.spa.framework.common.SpaEnvironmentFactory 24 25 const val SESSION_UNKNOWN = "unknown" 26 const val SESSION_BROWSE = "browse" 27 const val SESSION_SEARCH = "search" 28 const val SESSION_SLICE = "slice" 29 const val SESSION_EXTERNAL = "external" 30 31 const val KEY_DESTINATION = "spaActivityDestination" 32 const val KEY_HIGHLIGHT_ENTRY = "highlightEntry" 33 const val KEY_SESSION_SOURCE_NAME = "sessionSource" 34 35 val SPA_INTENT_RESERVED_KEYS = listOf( 36 KEY_DESTINATION, 37 KEY_HIGHLIGHT_ENTRY, 38 KEY_SESSION_SOURCE_NAME 39 ) 40 41 private fun createBaseIntent(): Intent? { 42 val context = SpaEnvironmentFactory.instance.appContext 43 val browseActivityClass = SpaEnvironmentFactory.instance.browseActivityClass ?: return null 44 return Intent().setComponent(ComponentName(context, browseActivityClass)) 45 } 46 47 fun SettingsPage.createIntent(sessionName: String? = null): Intent? { 48 if (!isBrowsable()) return null 49 return createBaseIntent()?.appendSpaParams( 50 destination = buildRoute(), 51 sessionName = sessionName 52 ) 53 } 54 55 fun SettingsEntry.createIntent(sessionName: String? = null): Intent? { 56 val sp = containerPage() 57 if (!sp.isBrowsable()) return null 58 return createBaseIntent()?.appendSpaParams( 59 destination = sp.buildRoute(), 60 entryId = id, 61 sessionName = sessionName 62 ) 63 } 64 65 fun Intent.appendSpaParams( 66 destination: String? = null, 67 entryId: String? = null, 68 sessionName: String? = null 69 ): Intent { 70 return apply { 71 if (destination != null) putExtra(KEY_DESTINATION, destination) 72 if (entryId != null) putExtra(KEY_HIGHLIGHT_ENTRY, entryId) 73 if (sessionName != null) putExtra(KEY_SESSION_SOURCE_NAME, sessionName) 74 } 75 } 76 77 fun Intent.getDestination(): String? { 78 return getStringExtra(KEY_DESTINATION) 79 } 80 81 fun Intent.getEntryId(): String? { 82 return getStringExtra(KEY_HIGHLIGHT_ENTRY) 83 } 84 85 fun Intent.getSessionName(): String? { 86 return getStringExtra(KEY_SESSION_SOURCE_NAME) 87 } 88