1 /*
2  * Copyright (C) 2017 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.settings.applications;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.pm.ResolveInfo;
22 import android.util.Log;
23 
24 // This class provides methods that help dealing with app stores.
25 public class AppStoreUtil {
26     private static final String LOG_TAG = "AppStoreUtil";
27 
resolveIntent(Context context, Intent i)28     private static Intent resolveIntent(Context context, Intent i) {
29         ResolveInfo result = context.getPackageManager().resolveActivity(i, 0);
30         return result != null ? new Intent(i.getAction())
31                 .setClassName(result.activityInfo.packageName, result.activityInfo.name) : null;
32     }
33 
34     // Returns the package name of the app which installed a given packageName, if one is
35     // available.
getInstallerPackageName(Context context, String packageName)36     public static String getInstallerPackageName(Context context, String packageName) {
37         String installerPackageName = null;
38         try {
39             installerPackageName =
40                     context.getPackageManager().getInstallerPackageName(packageName);
41         } catch (IllegalArgumentException e) {
42             Log.e(LOG_TAG, "Exception while retrieving the package installer of " + packageName, e);
43         }
44         return installerPackageName;
45     }
46 
47     // Returns a link to the installer app store for a given package name.
getAppStoreLink(Context context, String installerPackageName, String packageName)48     public static Intent getAppStoreLink(Context context, String installerPackageName,
49             String packageName) {
50         Intent intent = new Intent(Intent.ACTION_SHOW_APP_INFO)
51                 .setPackage(installerPackageName);
52         final Intent result = resolveIntent(context, intent);
53         if (result != null) {
54             result.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
55             return result;
56         }
57         return null;
58     }
59 
60     // Convenience method that looks up the installerPackageName for you.
getAppStoreLink(Context context, String packageName)61     public static Intent getAppStoreLink(Context context, String packageName) {
62       String installerPackageName = getInstallerPackageName(context, packageName);
63       return getAppStoreLink(context, installerPackageName, packageName);
64     }
65 }
66