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