• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ApplicationInfo;
22 import android.content.pm.InstallSourceInfo;
23 import android.content.pm.PackageManager.NameNotFoundException;
24 import android.content.pm.ResolveInfo;
25 import android.util.Log;
26 
27 // This class provides methods that help dealing with app stores.
28 public class AppStoreUtil {
29     private static final String LOG_TAG = "AppStoreUtil";
30 
resolveIntent(Context context, Intent i)31     private static Intent resolveIntent(Context context, Intent i) {
32         ResolveInfo result = context.getPackageManager().resolveActivity(i, 0);
33         return result != null ? new Intent(i.getAction())
34                 .setClassName(result.activityInfo.packageName, result.activityInfo.name) : null;
35     }
36 
37     // Returns the package name of the app that we consider to be the user-visible 'installer'
38     // of given packageName, if one is available.
getInstallerPackageName(Context context, String packageName)39     public static String getInstallerPackageName(Context context, String packageName) {
40         String installerPackageName;
41         try {
42             InstallSourceInfo source =
43                     context.getPackageManager().getInstallSourceInfo(packageName);
44             // By default, use the installing package name.
45             installerPackageName = source.getInstallingPackageName();
46             // Use the recorded originating package name only if the initiating package is a system
47             // app (eg. Package Installer). The originating package is not verified by the platform,
48             // so we choose to ignore this when supplied by a non-system app.
49             String originatingPackageName = source.getOriginatingPackageName();
50             String initiatingPackageName = source.getInitiatingPackageName();
51             if (originatingPackageName != null && initiatingPackageName != null) {
52                 ApplicationInfo ai = context.getPackageManager().getApplicationInfo(
53                         initiatingPackageName, 0);
54                 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
55                     installerPackageName = originatingPackageName;
56                 }
57             }
58         } catch (NameNotFoundException e) {
59             Log.e(LOG_TAG, "Exception while retrieving the package installer of " + packageName, e);
60             installerPackageName = null;
61         }
62         return installerPackageName;
63     }
64 
65     // Returns a link to the installer app store for a given package name.
getAppStoreLink(Context context, String installerPackageName, String packageName)66     public static Intent getAppStoreLink(Context context, String installerPackageName,
67             String packageName) {
68         Intent intent = new Intent(Intent.ACTION_SHOW_APP_INFO)
69                 .setPackage(installerPackageName);
70         final Intent result = resolveIntent(context, intent);
71         if (result != null) {
72             result.putExtra(Intent.EXTRA_PACKAGE_NAME, packageName);
73             return result;
74         }
75         return null;
76     }
77 
78     // Convenience method that looks up the installerPackageName for you.
getAppStoreLink(Context context, String packageName)79     public static Intent getAppStoreLink(Context context, String packageName) {
80       String installerPackageName = getInstallerPackageName(context, packageName);
81       return getAppStoreLink(context, installerPackageName, packageName);
82     }
83 }
84