1 /*
2  * Copyright (C) 2021 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.content.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.res.Resources;
24 import android.util.Log;
25 
26 import java.net.URISyntaxException;
27 
28 /**
29  * Utils for CarLauncher package.
30  */
31 public class CarLauncherUtils {
32 
33     private static final String TAG = "CarLauncherUtils";
34 
CarLauncherUtils()35     private CarLauncherUtils() {
36     }
37 
38     /** Intent used to find/launch the maps activity to run in the relevant DisplayArea. */
getMapsIntent(Context context)39     public static Intent getMapsIntent(Context context) {
40         Intent defaultIntent =
41                 Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MAPS);
42         defaultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
43         PackageManager pm = context.getPackageManager();
44         ComponentName defaultActivity = defaultIntent.resolveActivity(pm);
45         defaultIntent.setComponent(defaultActivity);
46 
47         for (String intentUri : context.getResources().getStringArray(
48                 R.array.config_homeCardPreferredMapActivities)) {
49             Intent preferredIntent;
50             try {
51                 preferredIntent = Intent.parseUri(intentUri, Intent.URI_ANDROID_APP_SCHEME);
52             } catch (URISyntaxException se) {
53                 Log.w(TAG, "Invalid intent URI in config_homeCardPreferredMapActivities", se);
54                 continue;
55             }
56 
57             if (defaultActivity != null && !defaultActivity.getPackageName().equals(
58                     preferredIntent.getPackage())) {
59                 continue;
60             }
61 
62             if (preferredIntent.resolveActivityInfo(pm, /* flags= */ 0) != null) {
63                 return preferredIntent;
64             }
65         }
66         return defaultIntent;
67     }
68 
isCustomDisplayPolicyDefined(Context context)69     static boolean isCustomDisplayPolicyDefined(Context context) {
70         Resources resources = context.getResources();
71         String customPolicyName = null;
72         try {
73             customPolicyName = resources
74                     .getString(
75                             com.android.internal
76                                     .R.string.config_deviceSpecificDisplayAreaPolicyProvider);
77         } catch (Resources.NotFoundException ex) {
78             Log.w(TAG, "custom policy provider not defined");
79         }
80         return customPolicyName != null && !customPolicyName.isEmpty();
81     }
82 }
83