1 /* 2 * Copyright 2019 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 android.content.pm; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.os.Parcel; 22 import android.os.Parcelable; 23 24 /** 25 * Information about how an app was installed. 26 * @see PackageManager#getInstallSourceInfo(String) 27 */ 28 public final class InstallSourceInfo implements Parcelable { 29 30 @Nullable private final String mInitiatingPackageName; 31 32 @Nullable private final SigningInfo mInitiatingPackageSigningInfo; 33 34 @Nullable private final String mOriginatingPackageName; 35 36 @Nullable private final String mInstallingPackageName; 37 38 @Nullable private final String mUpdateOwnerPackageName; 39 40 @Nullable private final int mPackageSource; 41 42 /** @hide */ InstallSourceInfo(@ullable String initiatingPackageName, @Nullable SigningInfo initiatingPackageSigningInfo, @Nullable String originatingPackageName, @Nullable String installingPackageName)43 public InstallSourceInfo(@Nullable String initiatingPackageName, 44 @Nullable SigningInfo initiatingPackageSigningInfo, 45 @Nullable String originatingPackageName, @Nullable String installingPackageName) { 46 this(initiatingPackageName, initiatingPackageSigningInfo, originatingPackageName, 47 installingPackageName, null /* updateOwnerPackageName */, 48 PackageInstaller.PACKAGE_SOURCE_UNSPECIFIED); 49 } 50 51 /** @hide */ InstallSourceInfo(@ullable String initiatingPackageName, @Nullable SigningInfo initiatingPackageSigningInfo, @Nullable String originatingPackageName, @Nullable String installingPackageName, @Nullable String updateOwnerPackageName, int packageSource)52 public InstallSourceInfo(@Nullable String initiatingPackageName, 53 @Nullable SigningInfo initiatingPackageSigningInfo, 54 @Nullable String originatingPackageName, @Nullable String installingPackageName, 55 @Nullable String updateOwnerPackageName, int packageSource) { 56 mInitiatingPackageName = initiatingPackageName; 57 mInitiatingPackageSigningInfo = initiatingPackageSigningInfo; 58 mOriginatingPackageName = originatingPackageName; 59 mInstallingPackageName = installingPackageName; 60 mUpdateOwnerPackageName = updateOwnerPackageName; 61 mPackageSource = packageSource; 62 } 63 64 @Override describeContents()65 public int describeContents() { 66 return mInitiatingPackageSigningInfo == null 67 ? 0 : mInitiatingPackageSigningInfo.describeContents(); 68 } 69 70 @Override writeToParcel(@onNull Parcel dest, int flags)71 public void writeToParcel(@NonNull Parcel dest, int flags) { 72 dest.writeString(mInitiatingPackageName); 73 dest.writeParcelable(mInitiatingPackageSigningInfo, flags); 74 dest.writeString(mOriginatingPackageName); 75 dest.writeString(mInstallingPackageName); 76 dest.writeString8(mUpdateOwnerPackageName); 77 dest.writeInt(mPackageSource); 78 } 79 InstallSourceInfo(Parcel source)80 private InstallSourceInfo(Parcel source) { 81 mInitiatingPackageName = source.readString(); 82 mInitiatingPackageSigningInfo = source.readParcelable(SigningInfo.class.getClassLoader(), android.content.pm.SigningInfo.class); 83 mOriginatingPackageName = source.readString(); 84 mInstallingPackageName = source.readString(); 85 mUpdateOwnerPackageName = source.readString8(); 86 mPackageSource = source.readInt(); 87 } 88 89 /** 90 * The name of the package that requested the installation, or null if not available. 91 * 92 * This is normally the same as the installing package name. If the installing package name 93 * is changed, for example by calling 94 * {@link PackageManager#setInstallerPackageName(String, String)}, the initiating package name 95 * remains unchanged. It continues to identify the actual package that performed the install 96 * or update. 97 * <p> 98 * Null may be returned if the app was not installed by a package (e.g. a system app) or if the 99 * initiating package has itself been uninstalled. 100 */ 101 @Nullable getInitiatingPackageName()102 public String getInitiatingPackageName() { 103 return mInitiatingPackageName; 104 } 105 106 /** 107 * Information about the signing certificates used to sign the initiating package, if available. 108 */ 109 @Nullable getInitiatingPackageSigningInfo()110 public SigningInfo getInitiatingPackageSigningInfo() { 111 return mInitiatingPackageSigningInfo; 112 } 113 114 /** 115 * The name of the package on behalf of which the initiating package requested the installation, 116 * or null if not available. 117 * <p> 118 * For example if a downloaded APK is installed via the Package Installer this could be the 119 * app that performed the download. This value is provided by the initiating package and not 120 * verified by the framework. 121 * <p> 122 * Note that the {@code InstallSourceInfo} returned by 123 * {@link PackageManager#getInstallSourceInfo(String)} will not have this information 124 * available unless the calling application holds the INSTALL_PACKAGES permission. 125 */ 126 @Nullable getOriginatingPackageName()127 public String getOriginatingPackageName() { 128 return mOriginatingPackageName; 129 } 130 131 /** 132 * The name of the package responsible for the installation (the installer of record), or null 133 * if not available. 134 * Note that this may differ from the initiating package name and can be modified via 135 * {@link PackageManager#setInstallerPackageName(String, String)}. 136 * <p> 137 * Null may be returned if the app was not installed by a package (e.g. a system app or an app 138 * installed via adb) or if the installing package has itself been uninstalled. 139 */ 140 @Nullable getInstallingPackageName()141 public String getInstallingPackageName() { 142 return mInstallingPackageName; 143 } 144 145 /** 146 * The name of the package that is the update owner, or null if not available. 147 * 148 * This indicates the update ownership enforcement is enabled for this app, 149 * and which package is the update owner. 150 * 151 * Returns null if the update ownership enforcement is disabled for the app. 152 * 153 * @see PackageInstaller.SessionParams#setRequestUpdateOwnership 154 */ 155 @Nullable getUpdateOwnerPackageName()156 public String getUpdateOwnerPackageName() { 157 return mUpdateOwnerPackageName; 158 } 159 160 /** 161 * Information about the package source when installer installed this app. 162 */ getPackageSource()163 public @PackageInstaller.PackageSourceType int getPackageSource() { 164 return mPackageSource; 165 } 166 167 @NonNull 168 public static final Parcelable.Creator<InstallSourceInfo> CREATOR = 169 new Creator<InstallSourceInfo>() { 170 @Override 171 public InstallSourceInfo createFromParcel(Parcel source) { 172 return new InstallSourceInfo(source); 173 } 174 175 @Override 176 public InstallSourceInfo[] newArray(int size) { 177 return new InstallSourceInfo[size]; 178 } 179 }; 180 } 181