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.util.Log;
24 
25 import java.net.URISyntaxException;
26 
27 /**
28  * Utils for CarLauncher package.
29  */
30 public class CarLauncherUtils {
31 
32     private static final String TAG = "CarLauncherUtils";
33     private static final String ACTION_APP_GRID = "com.android.car.carlauncher.ACTION_APP_GRID";
34 
CarLauncherUtils()35     private CarLauncherUtils() {
36     }
37 
getAppsGridIntent()38     public static Intent getAppsGridIntent() {
39         return new Intent(ACTION_APP_GRID);
40     }
41 
42     /** Intent used to find/launch the maps activity to run in the relevant DisplayArea. */
getMapsIntent(Context context)43     public static Intent getMapsIntent(Context context) {
44         Intent defaultIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MAPS);
45         defaultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
46         PackageManager pm = context.getPackageManager();
47         ComponentName defaultActivity = defaultIntent.resolveActivity(pm);
48         defaultIntent.setComponent(defaultActivity);
49 
50         for (String intentUri : context.getResources().getStringArray(
51                 R.array.config_homeCardPreferredMapActivities)) {
52             Intent preferredIntent;
53             try {
54                 preferredIntent = Intent.parseUri(intentUri, Intent.URI_ANDROID_APP_SCHEME);
55             } catch (URISyntaxException se) {
56                 Log.w(TAG, "Invalid intent URI in config_homeCardPreferredMapActivities", se);
57                 continue;
58             }
59 
60             if (defaultActivity != null && !defaultActivity.getPackageName().equals(
61                     preferredIntent.getPackage())) {
62                 continue;
63             }
64 
65             if (preferredIntent.resolveActivityInfo(pm, /* flags= */ 0) != null) {
66                 return preferredIntent;
67             }
68         }
69         return defaultIntent;
70     }
71 
72     /**
73      * Return an intent used to launch the tos map activity
74      * @param context The application context
75      * @return Tos Intent, null if the config is incorrect
76      */
getTosMapIntent(Context context)77     public static Intent getTosMapIntent(Context context) {
78         String intentString = context.getString(R.string.config_tosMapIntent);
79         try {
80             return Intent.parseUri(intentString, Intent.URI_ANDROID_APP_SCHEME);
81         } catch (URISyntaxException se) {
82             Log.w(TAG, "Invalid intent URI in config_tosMapIntent", se);
83             return null;
84         }
85     }
86 
87     /**
88      * Returns {@code true} if a proper limited map intent is configured via
89      * {@code config_smallCanvasOptimizedMapIntent} string resource.
90      */
isSmallCanvasOptimizedMapIntentConfigured(Context context)91     public static boolean isSmallCanvasOptimizedMapIntentConfigured(Context context) {
92         String intentString = context.getString(R.string.config_smallCanvasOptimizedMapIntent);
93         if (intentString.isEmpty()) {
94             Log.d(TAG, "Empty intent URI in config_smallCanvasOptimizedMapIntent");
95             return false;
96         }
97 
98         try {
99             Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME);
100             return true;
101         } catch (URISyntaxException e) {
102             Log.w(TAG, "Invalid intent URI in config_smallCanvasOptimizedMapIntent: \""
103                     + intentString);
104             return false;
105         }
106     }
107 
108     /**
109      * Returns an intent to trigger a map with a limited functionality (e.g., one to be used when
110      * there's not much screen real estate).
111      */
getSmallCanvasOptimizedMapIntent(Context context)112     public static Intent getSmallCanvasOptimizedMapIntent(Context context) {
113         String intentString = context.getString(R.string.config_smallCanvasOptimizedMapIntent);
114         try {
115             Intent intent = Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME);
116             intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
117             return intent;
118         } catch (URISyntaxException e) {
119             Log.w(TAG, "Invalid intent URI in config_smallCanvasOptimizedMapIntent: \""
120                     + intentString + "\". Falling back to fullscreen map.");
121             return getMapsIntent(context);
122         }
123     }
124 }
125