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