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