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 * Returns {@code true} if a proper limited map intent is configured via 74 * {@code config_smallCanvasOptimizedMapIntent} string resource. 75 */ isSmallCanvasOptimizedMapIntentConfigured(Context context)76 public static boolean isSmallCanvasOptimizedMapIntentConfigured(Context context) { 77 String intentString = context.getString(R.string.config_smallCanvasOptimizedMapIntent); 78 if (intentString.isEmpty()) { 79 return false; 80 } 81 82 try { 83 Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME); 84 return true; 85 } catch (URISyntaxException e) { 86 return false; 87 } 88 } 89 90 /** 91 * Returns an intent to trigger a map with a limited functionality (e.g., one to be used when 92 * there's not much screen real estate). 93 */ getSmallCanvasOptimizedMapIntent(Context context)94 public static Intent getSmallCanvasOptimizedMapIntent(Context context) { 95 String intentString = context.getString(R.string.config_smallCanvasOptimizedMapIntent); 96 try { 97 Intent intent = Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME); 98 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 99 return intent; 100 } catch (URISyntaxException e) { 101 Log.w(TAG, "Invalid intent URI in config_smallCanvasOptimizedMapIntent: \"" 102 + intentString + "\". Falling back to fullscreen map."); 103 return getMapsIntent(context); 104 } 105 } 106 } 107