• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 android.platform.helpers.notesrole
18 
19 import android.content.BroadcastReceiver
20 import android.content.ComponentName
21 import android.content.Context
22 import android.content.Intent
23 import android.content.Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT
24 import android.content.Intent.FLAG_ACTIVITY_MULTIPLE_TASK
25 import android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT
26 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
27 import android.content.IntentFilter
28 import android.os.Bundle
29 import androidx.activity.ComponentActivity
30 
31 /** Empty activity for app clips screenshots. */
32 class AppClipsScreenshotActivity : ComponentActivity() {
33 
34     private val launchDifferentAppInSplitScreenReceiver: BroadcastReceiver =
35         object : BroadcastReceiver() {
onReceivenull36             override fun onReceive(context: Context?, intent: Intent?) {
37                 launchDifferentAppInSplitScreen()
38             }
39         }
40 
41     private val launchSameAppInSplitScreenReceiver: BroadcastReceiver =
42         object : BroadcastReceiver() {
onReceivenull43             override fun onReceive(context: Context?, intent: Intent?) {
44                 launchSameAppInSplitScreen()
45             }
46         }
47 
onCreatenull48     override fun onCreate(savedInstanceState: Bundle?) {
49         super.onCreate(savedInstanceState)
50 
51         registerReceiver(
52             launchDifferentAppInSplitScreenReceiver,
53             IntentFilter(LAUNCH_DIFFERENT_APP_IN_SPLIT_SCREEN_ACTION),
54             RECEIVER_EXPORTED
55         )
56 
57         registerReceiver(
58             launchSameAppInSplitScreenReceiver,
59             IntentFilter(LAUNCH_SAME_APP_IN_SPLIT_SCREEN_ACTION),
60             RECEIVER_EXPORTED
61         )
62     }
63 
onDestroynull64     override fun onDestroy() {
65         super.onDestroy()
66         unregisterReceiver(launchDifferentAppInSplitScreenReceiver)
67         unregisterReceiver(launchSameAppInSplitScreenReceiver)
68     }
69 
launchDifferentAppInSplitScreennull70     private fun launchDifferentAppInSplitScreen() =
71         startActivity(
72             Intent().apply {
73                 setComponent(ComponentName(packageName, SPLIT_DIFFERENT_ACTIVITY_NAME))
74                 addFlags(
75                     FLAG_ACTIVITY_NEW_TASK or
76                         FLAG_ACTIVITY_LAUNCH_ADJACENT or
77                         FLAG_ACTIVITY_MULTIPLE_TASK or
78                         FLAG_ACTIVITY_NEW_DOCUMENT
79                 )
80             }
81         )
82 
launchSameAppInSplitScreennull83     private fun launchSameAppInSplitScreen() =
84         startActivity(
85             Intent().apply {
86                 setComponent(this@AppClipsScreenshotActivity.componentName)
87                 addFlags(
88                     FLAG_ACTIVITY_NEW_TASK or
89                         FLAG_ACTIVITY_LAUNCH_ADJACENT or
90                         FLAG_ACTIVITY_MULTIPLE_TASK or
91                         FLAG_ACTIVITY_NEW_DOCUMENT
92                 )
93             }
94         )
95 
96     companion object {
97         const val LAUNCH_DIFFERENT_APP_IN_SPLIT_SCREEN_ACTION: String =
98             "LAUNCH_DIFFERENT_APP_IN_SPLIT_SCREEN_ACTION"
99         const val LAUNCH_SAME_APP_IN_SPLIT_SCREEN_ACTION: String =
100             "LAUNCH_SAME_APP_IN_SPLIT_SCREEN_ACTION"
101         private const val SPLIT_DIFFERENT_ACTIVITY_NAME: String =
102             "android.platform.helpers.notesrole.AppClipsScreenshotActivity.Split"
103 
getIntentnull104         fun getIntent(context: Context): Intent =
105             Intent(context, AppClipsScreenshotActivity::class.java).addFlags(FLAG_ACTIVITY_NEW_TASK)
106     }
107 }
108