1 /* 2 * Copyright (C) 2016 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 package com.android.support.car.lenspicker; 17 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.SharedPreferences; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.service.media.MediaBrowserService; 24 25 import androidx.annotation.Nullable; 26 27 /** 28 * Utility methods for the lenspicker 29 */ 30 public class LensPickerUtils { 31 private static final String FACET_KEY_PREFIX = "facet_key_"; 32 private static final String PACKAGE_KEY_PREFIX = "package_key_"; 33 34 private static final String SHARED_PREF_FILE_KEY 35 = "com.android.support.car.lenspicker.LENSPICKER_PREFERENCE_KEY"; 36 private static final String MEDIA_TEMPLATE_COMPONENT = "com.android.car.media"; 37 38 private static final String LAST_LAUNCHED_FACET_ID = "last_launched_facet_id"; 39 private static final String LAST_LAUNCHED_PACKAGE_NAME = "last_launched_package_name"; 40 private static final String LAST_LAUNCHED_INTENT_KEY = "last_launched_intent_key"; 41 42 // TODO: These two come from MediaManager.java in CarMediaApp and should probably be pushed 43 // into a common place so that these two don't go out of sync. Duplicated for now. 44 public static final String KEY_MEDIA_PACKAGE = "media_package"; 45 public static final String KEY_MEDIA_CLASS = "media_class"; 46 getFacetKey(String facetId)47 public static String getFacetKey(String facetId) { 48 return FACET_KEY_PREFIX + facetId; 49 } 50 getPackageKey(String packageName)51 public static String getPackageKey(String packageName) { 52 return PACKAGE_KEY_PREFIX + packageName; 53 } 54 getFacetSharedPrefs(Context context)55 public static SharedPreferences getFacetSharedPrefs(Context context) { 56 return context.getSharedPreferences(SHARED_PREF_FILE_KEY, Context.MODE_PRIVATE); 57 } 58 59 /** 60 * Launches the application that is specified by the given launch launch. The other parameters 61 * are used to store information about what application was last launched so that subsequent 62 * launches of that application are faster. 63 */ launch(Context context, SharedPreferences sharedPrefs, String facetId, String packageName, Intent launchIntent)64 public static void launch(Context context, SharedPreferences sharedPrefs, String facetId, 65 String packageName, Intent launchIntent) { 66 SharedPreferences.Editor editor = sharedPrefs.edit(); 67 editor.putString(LensPickerUtils.getFacetKey(facetId), packageName); 68 69 // Store information about the last launched application. 70 editor.putString(LAST_LAUNCHED_FACET_ID, facetId); 71 editor.putString(LAST_LAUNCHED_PACKAGE_NAME, packageName); 72 73 String uriString = launchIntent.toUri(Intent.URI_INTENT_SCHEME); 74 editor.putString(LAST_LAUNCHED_INTENT_KEY, uriString); 75 76 editor.commit(); 77 78 context.startActivity(launchIntent); 79 } 80 81 /** 82 * Saves the given app launch information as the last launched application. 83 */ saveLastLaunchedAppInfo(SharedPreferences sharedPrefs, String facetId, String packageName, Intent launchIntent)84 public static void saveLastLaunchedAppInfo(SharedPreferences sharedPrefs, String facetId, 85 String packageName, Intent launchIntent) { 86 SharedPreferences.Editor editor = sharedPrefs.edit(); 87 88 // Store information about the last launched application. 89 editor.putString(LAST_LAUNCHED_FACET_ID, facetId); 90 editor.putString(LAST_LAUNCHED_PACKAGE_NAME, packageName); 91 92 String uriString = launchIntent.toUri(Intent.URI_INTENT_SCHEME); 93 editor.putString(LAST_LAUNCHED_INTENT_KEY, uriString); 94 95 editor.commit(); 96 } 97 98 /** 99 * Returns the {@link AppLaunchInformation} for the last launched application from the 100 * LensPicker. 101 */ 102 @Nullable getLastLaunchedAppInfo(SharedPreferences sharedPrefs)103 public static AppLaunchInformation getLastLaunchedAppInfo(SharedPreferences sharedPrefs) { 104 String facetId = sharedPrefs.getString(LAST_LAUNCHED_FACET_ID, null); 105 String packageName = sharedPrefs.getString(LAST_LAUNCHED_PACKAGE_NAME, null); 106 String intentString = sharedPrefs.getString(LAST_LAUNCHED_INTENT_KEY, null); 107 108 if (facetId == null || packageName == null || intentString == null) { 109 return null; 110 } 111 112 return new AppLaunchInformation(facetId, packageName, intentString); 113 } 114 getMediaLaunchIntent(PackageManager pm, String packageName, String className)115 public static Intent getMediaLaunchIntent(PackageManager pm, String packageName, 116 String className) { 117 Intent intent = pm.getLaunchIntentForPackage(MEDIA_TEMPLATE_COMPONENT); 118 intent.putExtra(KEY_MEDIA_PACKAGE, packageName); 119 intent.putExtra(KEY_MEDIA_CLASS, className); 120 return intent; 121 } 122 getPackageName(ResolveInfo info)123 public static String getPackageName(ResolveInfo info) { 124 if (info.activityInfo != null) { 125 return info.activityInfo.packageName; 126 } else if (info.serviceInfo != null) { 127 return info.serviceInfo.packageName; 128 } 129 130 throw new RuntimeException("No activityInfo or serviceInfo. This should not happen!"); 131 } 132 isMediaService(ResolveInfo rInfo)133 public static boolean isMediaService(ResolveInfo rInfo) { 134 return rInfo.serviceInfo != null && rInfo.filter != null 135 && rInfo.filter.hasAction(MediaBrowserService.SERVICE_INTERFACE); 136 } 137 138 @Nullable getLaunchIntent(String packageName, ResolveInfo rInfo, PackageManager pm)139 public static Intent getLaunchIntent(String packageName, ResolveInfo rInfo, PackageManager pm) { 140 if (LensPickerUtils.isMediaService(rInfo)) { 141 return LensPickerUtils.getMediaLaunchIntent(pm, packageName, 142 rInfo.serviceInfo.name); 143 } 144 145 return pm.getLaunchIntentForPackage(packageName); 146 } 147 148 /** 149 * A class that wraps all the information needed to launch a particular application from the 150 * LensPicker. 151 */ 152 public static class AppLaunchInformation { 153 private final String mFacetId; 154 private final String mPackageName; 155 private final String mIntentString; 156 AppLaunchInformation(String facetId, String packageName, String intentString)157 public AppLaunchInformation(String facetId, String packageName, String intentString) { 158 mFacetId = facetId; 159 mPackageName = packageName; 160 mIntentString = intentString; 161 } 162 getFacetId()163 public String getFacetId() { 164 return mFacetId; 165 } 166 getPackageName()167 public String getPackageName() { 168 return mPackageName; 169 } 170 getIntentString()171 public String getIntentString() { 172 return mIntentString; 173 } 174 } 175 } 176