• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.systemui.controls.management
18 
19 import android.app.ActivityManager
20 import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
21 import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE
22 import android.content.BroadcastReceiver
23 import android.content.ComponentName
24 import android.content.Context
25 import android.content.Intent
26 import android.content.pm.PackageManager
27 import android.os.UserHandle
28 import android.service.controls.Control
29 import android.service.controls.ControlsProviderService
30 import android.util.Log
31 
32 /**
33  * Proxy to launch in user 0
34  */
35 class ControlsRequestReceiver : BroadcastReceiver() {
36 
37     companion object {
38         private const val TAG = "ControlsRequestReceiver"
39 
isPackageInForegroundnull40         fun isPackageInForeground(context: Context, packageName: String): Boolean {
41             val uid = try {
42                 context.packageManager.getPackageUid(packageName, 0)
43             } catch (_: PackageManager.NameNotFoundException) {
44                 Log.w(TAG, "Package $packageName not found")
45                 return false
46             }
47 
48             val am = context.getSystemService(ActivityManager::class.java)
49             if ((am?.getUidImportance(uid) ?: IMPORTANCE_GONE) != IMPORTANCE_FOREGROUND) {
50                 Log.w(TAG, "Uid $uid not in foreground")
51                 return false
52             }
53             return true
54         }
55     }
56 
onReceivenull57     override fun onReceive(context: Context, intent: Intent) {
58         if (!context.packageManager.hasSystemFeature(PackageManager.FEATURE_CONTROLS)) {
59             return
60         }
61 
62         val packageName = intent.getParcelableExtra<ComponentName>(Intent.EXTRA_COMPONENT_NAME)
63                 ?.packageName
64 
65         if (packageName == null || !isPackageInForeground(context, packageName)) {
66             return
67         }
68 
69         val activityIntent = Intent(context, ControlsRequestDialog::class.java).apply {
70             Intent.EXTRA_COMPONENT_NAME.let {
71                 putExtra(it, intent.getParcelableExtra<ComponentName>(it))
72             }
73             ControlsProviderService.EXTRA_CONTROL.let {
74                 putExtra(it, intent.getParcelableExtra<Control>(it))
75             }
76             addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
77         }
78         activityIntent.putExtra(Intent.EXTRA_USER_ID, context.userId)
79 
80         context.startActivityAsUser(activityIntent, UserHandle.SYSTEM)
81     }
82 }