• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.carlauncher;
18 
19 import android.annotation.Nullable;
20 import android.car.CarNotConnectedException;
21 import android.car.content.pm.CarPackageManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.LauncherActivityInfo;
25 import android.content.pm.LauncherApps;
26 import android.content.pm.PackageManager;
27 import android.graphics.ColorMatrix;
28 import android.graphics.ColorMatrixColorFilter;
29 import android.graphics.drawable.Drawable;
30 import android.os.Process;
31 import android.util.Log;
32 
33 import java.util.ArrayList;
34 import java.util.Comparator;
35 import java.util.List;
36 
37 /**
38  * Util class that contains helper method used by app launcher classes.
39  */
40 class AppLauncherUtils {
41 
42     private static final String TAG = "AppLauncherUtils";
43 
AppLauncherUtils()44     private AppLauncherUtils() {
45     }
46 
47     /**
48      * Comparator for {@link AppMetaData} that sorts the list
49      * by the "displayName" property in ascending order.
50      */
51     static final Comparator<AppMetaData> ALPHABETICAL_COMPARATOR = Comparator
52             .comparing(AppMetaData::getDisplayName, String::compareToIgnoreCase);
53 
54     /**
55      * Helper method that launches the app given the app's AppMetaData.
56      *
57      * @param app the requesting app's AppMetaData
58      */
launchApp(Context context, AppMetaData app)59     static void launchApp(Context context, AppMetaData app) {
60         Intent intent =
61                 context.getPackageManager().getLaunchIntentForPackage(app.getPackageName());
62         context.startActivity(intent);
63     }
64 
65     /**
66      * Converts a {@link Drawable} to grey scale.
67      *
68      * @param drawable the original drawable
69      * @return the grey scale drawable
70      */
toGrayscale(Drawable drawable)71     static Drawable toGrayscale(Drawable drawable) {
72         ColorMatrix matrix = new ColorMatrix();
73         matrix.setSaturation(0);
74         ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
75         // deep copy the original drawable
76         Drawable newDrawable = drawable.getConstantState().newDrawable().mutate();
77         newDrawable.setColorFilter(filter);
78         return newDrawable;
79     }
80 
81     /**
82      * Gets all apps that support launching from launcher in unsorted order.
83      *
84      * @param launcherApps      The {@link LauncherApps} system service
85      * @param carPackageManager The {@link CarPackageManager} system service
86      * @param packageManager    The {@link PackageManager} system service
87      * @return a list of all apps' metadata
88      */
89     @Nullable
getAllLaunchableApps( LauncherApps launcherApps, CarPackageManager carPackageManager, PackageManager packageManager)90     static List<AppMetaData> getAllLaunchableApps(
91             LauncherApps launcherApps,
92             CarPackageManager carPackageManager,
93             PackageManager packageManager) {
94 
95         if (launcherApps == null || carPackageManager == null || packageManager == null) {
96             return null;
97         }
98 
99         List<AppMetaData> apps = new ArrayList<>();
100 
101         Intent intent = new Intent(Intent.ACTION_MAIN, null);
102         intent.addCategory(Intent.CATEGORY_LAUNCHER);
103 
104         List<LauncherActivityInfo> availableActivities =
105                 launcherApps.getActivityList(null, Process.myUserHandle());
106         for (LauncherActivityInfo info : availableActivities) {
107             String packageName = info.getComponentName().getPackageName();
108             boolean isDistractionOptimized =
109                     isActivityDistractionOptimized(carPackageManager, packageName, info.getName());
110 
111             AppMetaData app = new AppMetaData(
112                     info.getLabel(),
113                     packageName,
114                     info.getApplicationInfo().loadIcon(packageManager),
115                     isDistractionOptimized);
116             apps.add(app);
117         }
118         return apps;
119     }
120 
121     /**
122      * Gets if an activity is distraction optimized.
123      *
124      * @param carPackageManager The {@link CarPackageManager} system service
125      * @param packageName       The package name of the app
126      * @param activityName      The requested activity name
127      * @return true if the supplied activity is distraction optimized
128      */
isActivityDistractionOptimized( CarPackageManager carPackageManager, String packageName, String activityName)129     static boolean isActivityDistractionOptimized(
130             CarPackageManager carPackageManager, String packageName, String activityName) {
131         boolean isDistractionOptimized = false;
132         // try getting distraction optimization info
133         try {
134             if (carPackageManager != null) {
135                 isDistractionOptimized =
136                         carPackageManager.isActivityDistractionOptimized(packageName, activityName);
137             }
138         } catch (CarNotConnectedException e) {
139             Log.e(TAG, "Car not connected when getting DO info", e);
140         }
141         return isDistractionOptimized;
142     }
143 }
144