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