1 /*
2  * Copyright 2019 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.apps.common.util;
18 
19 import android.app.PendingIntent;
20 import android.car.content.pm.CarPackageManager;
21 import android.content.Intent;
22 import android.content.pm.ActivityInfo;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 
26 import androidx.annotation.NonNull;
27 
28 /**
29  * Utility class to access CarPackageManager
30  */
31 public class CarPackageManagerUtils {
32     private static final String TAG = "CarPackageManagerUtils";
33 
34     /**
35      * Returns whether the given {@link PendingIntent} represents an activity that is distraction
36      * optimized.
37      */
isDistractionOptimized(CarPackageManager carPackageManager, @NonNull PendingIntent pendingIntent)38     public static boolean isDistractionOptimized(CarPackageManager carPackageManager,
39             @NonNull PendingIntent pendingIntent) {
40         if (carPackageManager != null) {
41             return carPackageManager.isPendingIntentDistractionOptimized(pendingIntent);
42         }
43         return false;
44     }
45 
46     /**
47      * Returns true if the provided Activity is distraction optimized
48      */
isDistractionOptimized(CarPackageManager carPackageManager, @NonNull ActivityInfo activityInfo)49     public static boolean isDistractionOptimized(CarPackageManager carPackageManager,
50             @NonNull ActivityInfo activityInfo) {
51         if (carPackageManager != null) {
52             return carPackageManager.isActivityDistractionOptimized(
53                     activityInfo.packageName, activityInfo.name);
54         }
55         return false;
56     }
57 
58     /**
59      * Attempts to resolve the provided intent into an activity, and returns true if the
60      * resolved activity is distraction optimized
61      */
isDistractionOptimized(CarPackageManager carPackageManager, PackageManager packageManager, Intent intent)62     public static boolean isDistractionOptimized(CarPackageManager carPackageManager,
63             PackageManager packageManager, Intent intent) {
64         ResolveInfo info = packageManager.resolveActivity(
65                 intent, PackageManager.MATCH_DEFAULT_ONLY);
66         return (info != null) ? isDistractionOptimized(carPackageManager, info.activityInfo)
67                 : false;
68     }
69 }
70