1 /* <lambda>null2 * 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.car.docklib.events 18 19 import android.content.BroadcastReceiver 20 import android.content.Context 21 import android.content.Intent 22 import android.content.IntentFilter 23 import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED 24 import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER 25 import android.os.Build 26 import android.util.Log 27 import com.android.car.docklib.DockInterface 28 29 class DockPackageChangeReceiver( 30 private val dockController: DockInterface 31 ) : BroadcastReceiver() { 32 companion object { 33 private val DEBUG = Build.isDebuggable() 34 private const val TAG = "DockPackageChangeReceiver" 35 36 /** 37 * Helper method to register [DockPackageChangeReceiver] through context and listen to 38 * changes to packages in the system. 39 * 40 * @param context the context through which the [DockPackageChangeReceiver] is registered 41 * @return successfully registered [DockPackageChangeReceiver]. 42 */ 43 fun registerReceiver( 44 context: Context, 45 dockController: DockInterface 46 ): DockPackageChangeReceiver { 47 val receiver = DockPackageChangeReceiver(dockController) 48 val filter = IntentFilter() 49 filter.addAction(Intent.ACTION_PACKAGE_ADDED) 50 filter.addAction(Intent.ACTION_PACKAGE_REMOVED) 51 filter.addAction(Intent.ACTION_PACKAGE_CHANGED) 52 filter.addDataScheme("package") 53 context.registerReceiver(receiver, filter, Context.RECEIVER_EXPORTED) 54 if (DEBUG) { 55 Log.d( 56 TAG, 57 "DockPackageChangeReceiver registered from package: " + 58 "${context.packageName}, for user ${context.userId}" 59 ) 60 } 61 return receiver 62 } 63 } 64 65 override fun onReceive(context: Context?, intent: Intent?) { 66 intent?.data?.schemeSpecificPart?.let { packageName -> 67 if (DEBUG) Log.d(TAG, "package name: $packageName") 68 when (intent.action) { 69 Intent.ACTION_PACKAGE_ADDED -> { 70 if (intent.getBooleanExtra( 71 Intent.EXTRA_REPLACING, 72 false // defaultValue 73 ) 74 ) { 75 return 76 } 77 if (DEBUG) Log.d(TAG, "ACTION_PACKAGE_ADDED") 78 dockController.packageAdded(packageName) 79 } 80 81 Intent.ACTION_PACKAGE_REMOVED -> { 82 if (intent.getBooleanExtra( 83 Intent.EXTRA_REPLACING, 84 false // defaultValue 85 ) 86 ) { 87 return 88 } 89 if (DEBUG) Log.d(TAG, "ACTION_PACKAGE_REMOVED") 90 dockController.packageRemoved(packageName) 91 } 92 93 Intent.ACTION_PACKAGE_CHANGED -> { 94 if (DEBUG) Log.d(TAG, "ACTION_PACKAGE_CHANGED") 95 when (context?.packageManager?.getApplicationEnabledSetting(packageName)) { 96 COMPONENT_ENABLED_STATE_DISABLED, COMPONENT_ENABLED_STATE_DISABLED_USER -> { 97 if (DEBUG) Log.d(TAG, "package disabled") 98 dockController.packageRemoved(packageName) 99 } 100 } 101 } 102 } 103 } 104 } 105 } 106