Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.server.pm; |
| 18 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 19 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 20 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 21 | |
Svetoslav | 805b63e | 2015-04-09 17:28:54 -0700 | [diff] [blame] | 22 | import android.Manifest; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 24 | import android.app.AppGlobals; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 25 | import android.app.AppOpsManager; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 26 | import android.app.Notification; |
| 27 | import android.app.NotificationManager; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 28 | import android.app.PackageDeleteObserver; |
| 29 | import android.app.PackageInstallObserver; |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 30 | import android.app.admin.DevicePolicyManagerInternal; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 31 | import android.content.Context; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 32 | import android.content.Intent; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 33 | import android.content.IntentSender; |
| 34 | import android.content.IntentSender.SendIntentException; |
Philip P. Moltmann | d9bb39a | 2017-09-05 12:41:15 -0700 | [diff] [blame] | 35 | import android.content.pm.ApplicationInfo; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 36 | import android.content.pm.IPackageInstaller; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 37 | import android.content.pm.IPackageInstallerCallback; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 38 | import android.content.pm.IPackageInstallerSession; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 39 | import android.content.pm.PackageInfo; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 40 | import android.content.pm.PackageInstaller; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 41 | import android.content.pm.PackageInstaller.SessionInfo; |
| 42 | import android.content.pm.PackageInstaller.SessionParams; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 44 | import android.content.pm.ParceledListSlice; |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 45 | import android.content.pm.VersionedPackage; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 46 | import android.graphics.Bitmap; |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 47 | import android.net.Uri; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 48 | import android.os.Binder; |
Philip P. Moltmann | d9bb39a | 2017-09-05 12:41:15 -0700 | [diff] [blame] | 49 | import android.os.Build; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 50 | import android.os.Bundle; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 51 | import android.os.Environment; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 52 | import android.os.Handler; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 53 | import android.os.HandlerThread; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 54 | import android.os.Looper; |
| 55 | import android.os.Message; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 56 | import android.os.Process; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 57 | import android.os.RemoteCallbackList; |
| 58 | import android.os.RemoteException; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 59 | import android.os.SELinux; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 60 | import android.os.UserManager; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 61 | import android.os.storage.StorageManager; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 62 | import android.system.ErrnoException; |
| 63 | import android.system.Os; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 64 | import android.text.TextUtils; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 65 | import android.text.format.DateUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 66 | import android.util.ArraySet; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 67 | import android.util.AtomicFile; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 68 | import android.util.ExceptionUtils; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 69 | import android.util.Slog; |
| 70 | import android.util.SparseArray; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 71 | import android.util.SparseBooleanArray; |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 72 | import android.util.SparseIntArray; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 73 | import android.util.Xml; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 74 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 75 | import com.android.internal.R; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 76 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 77 | import com.android.internal.content.PackageHelper; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 78 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 79 | import com.android.internal.notification.SystemNotificationChannels; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 80 | import com.android.internal.util.FastXmlSerializer; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 81 | import com.android.internal.util.ImageUtils; |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 82 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 83 | import com.android.server.IoThread; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 84 | import com.android.server.LocalServices; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 85 | import com.android.server.pm.permission.PermissionManagerServiceInternal; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 86 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 87 | import libcore.io.IoUtils; |
| 88 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 89 | import org.xmlpull.v1.XmlPullParser; |
| 90 | import org.xmlpull.v1.XmlPullParserException; |
| 91 | import org.xmlpull.v1.XmlSerializer; |
| 92 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 93 | import java.io.CharArrayWriter; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 94 | import java.io.File; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 95 | import java.io.FileInputStream; |
| 96 | import java.io.FileNotFoundException; |
| 97 | import java.io.FileOutputStream; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 98 | import java.io.FilenameFilter; |
| 99 | import java.io.IOException; |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 100 | import java.nio.charset.StandardCharsets; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 101 | import java.security.SecureRandom; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 102 | import java.util.ArrayList; |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 103 | import java.util.Collections; |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 104 | import java.util.List; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 105 | import java.util.Objects; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 106 | import java.util.Random; |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 107 | import java.util.function.IntPredicate; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 108 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 109 | /** The service responsible for installing packages. */ |
| 110 | public class PackageInstallerService extends IPackageInstaller.Stub implements |
| 111 | PackageSessionProvider { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 112 | private static final String TAG = "PackageInstaller"; |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 113 | private static final boolean LOGD = false; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 114 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 115 | // TODO: remove outstanding sessions when installer package goes away |
Jeff Sharkey | 6c833e0 | 2014-07-14 22:44:30 -0700 | [diff] [blame] | 116 | // TODO: notify listeners in other users when package has been installed there |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 117 | // TODO: purge expired sessions periodically in addition to at reboot |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 118 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 119 | /** XML constants used in {@link #mSessionsFile} */ |
| 120 | private static final String TAG_SESSIONS = "sessions"; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 121 | |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 122 | /** Automatically destroy sessions older than this */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 123 | private static final long MAX_AGE_MILLIS = 3 * DateUtils.DAY_IN_MILLIS; |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 124 | /** Upper bound on number of active sessions for a UID */ |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 125 | private static final long MAX_ACTIVE_SESSIONS = 1024; |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 126 | /** Upper bound on number of historical sessions for a UID */ |
| 127 | private static final long MAX_HISTORICAL_SESSIONS = 1048576; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 128 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 129 | private final Context mContext; |
| 130 | private final PackageManagerService mPm; |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 131 | private final ApexManager mApexManager; |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 132 | private final StagingManager mStagingManager; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 133 | private final PermissionManagerServiceInternal mPermissionManager; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 135 | private AppOpsManager mAppOps; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 136 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 137 | private final HandlerThread mInstallThread; |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 138 | private final Handler mInstallHandler; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 139 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 140 | private final Callbacks mCallbacks; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 141 | |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 142 | private volatile boolean mOkToSendBroadcasts = false; |
Dario Freni | 9694b80 | 2019-01-27 23:26:06 +0000 | [diff] [blame] | 143 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 144 | /** |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 145 | * File storing persisted {@link #mSessions} metadata. |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 146 | */ |
| 147 | private final AtomicFile mSessionsFile; |
| 148 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Directory storing persisted {@link #mSessions} metadata which is too |
| 151 | * heavy to store directly in {@link #mSessionsFile}. |
| 152 | */ |
| 153 | private final File mSessionsDir; |
| 154 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 155 | private final InternalCallback mInternalCallback = new InternalCallback(); |
| 156 | |
| 157 | /** |
| 158 | * Used for generating session IDs. Since this is created at boot time, |
| 159 | * normal random might be predictable. |
| 160 | */ |
| 161 | private final Random mRandom = new SecureRandom(); |
| 162 | |
Todd Kennedy | 28c4e80 | 2016-07-13 13:20:30 -0700 | [diff] [blame] | 163 | /** All sessions allocated */ |
| 164 | @GuardedBy("mSessions") |
| 165 | private final SparseBooleanArray mAllocatedSessions = new SparseBooleanArray(); |
| 166 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 167 | @GuardedBy("mSessions") |
| 168 | private final SparseArray<PackageInstallerSession> mSessions = new SparseArray<>(); |
| 169 | |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 170 | /** Historical sessions kept around for debugging purposes */ |
| 171 | @GuardedBy("mSessions") |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 172 | private final List<String> mHistoricalSessions = new ArrayList<>(); |
| 173 | |
| 174 | @GuardedBy("mSessions") |
| 175 | private final SparseIntArray mHistoricalSessionsByInstaller = new SparseIntArray(); |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 177 | /** Sessions allocated to legacy users */ |
| 178 | @GuardedBy("mSessions") |
| 179 | private final SparseBooleanArray mLegacySessions = new SparseBooleanArray(); |
| 180 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 181 | private static final FilenameFilter sStageFilter = new FilenameFilter() { |
| 182 | @Override |
| 183 | public boolean accept(File dir, String name) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 184 | return isStageName(name); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 185 | } |
| 186 | }; |
| 187 | |
Dario Freni | 2e8dffc | 2019-02-06 14:55:16 +0000 | [diff] [blame] | 188 | public PackageInstallerService(Context context, PackageManagerService pm, ApexManager am) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 189 | mContext = context; |
| 190 | mPm = pm; |
Philip P. Moltmann | 4845667 | 2019-01-20 13:14:03 -0800 | [diff] [blame] | 191 | mPermissionManager = LocalServices.getService(PermissionManagerServiceInternal.class); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 192 | |
| 193 | mInstallThread = new HandlerThread(TAG); |
| 194 | mInstallThread.start(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 195 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 196 | mInstallHandler = new Handler(mInstallThread.getLooper()); |
| 197 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 198 | mCallbacks = new Callbacks(mInstallThread.getLooper()); |
| 199 | |
| 200 | mSessionsFile = new AtomicFile( |
Dianne Hackborn | e17b445 | 2018-01-10 13:15:40 -0800 | [diff] [blame] | 201 | new File(Environment.getDataSystemDirectory(), "install_sessions.xml"), |
| 202 | "package-session"); |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 203 | mSessionsDir = new File(Environment.getDataSystemDirectory(), "install_sessions"); |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 204 | mSessionsDir.mkdirs(); |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 205 | |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 206 | mApexManager = am; |
Nikita Ioffe | 39a6a5b | 2019-02-26 15:36:39 +0000 | [diff] [blame] | 207 | |
| 208 | mStagingManager = new StagingManager(pm, this, am, context); |
Tony Mak | 606f8e7 | 2017-02-15 18:40:03 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 211 | boolean okToSendBroadcasts() { |
| 212 | return mOkToSendBroadcasts; |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Tony Mak | 606f8e7 | 2017-02-15 18:40:03 +0000 | [diff] [blame] | 215 | public void systemReady() { |
| 216 | mAppOps = mContext.getSystemService(AppOpsManager.class); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 217 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 218 | synchronized (mSessions) { |
| 219 | readSessionsLocked(); |
| 220 | |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 221 | reconcileStagesLocked(StorageManager.UUID_PRIVATE_INTERNAL); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 222 | |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 223 | final ArraySet<File> unclaimedIcons = newArraySet( |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 224 | mSessionsDir.listFiles()); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 225 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 226 | // Ignore stages and icons claimed by active sessions |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 227 | for (int i = 0; i < mSessions.size(); i++) { |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 228 | final PackageInstallerSession session = mSessions.valueAt(i); |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 229 | unclaimedIcons.remove(buildAppIconFile(session.sessionId)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 230 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 231 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 232 | // Clean up orphaned icons |
| 233 | for (File icon : unclaimedIcons) { |
| 234 | Slog.w(TAG, "Deleting orphan icon " + icon); |
| 235 | icon.delete(); |
| 236 | } |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 237 | |
| 238 | // Invalid sessions might have been marked while parsing. Re-write the database with |
| 239 | // the updated information. |
| 240 | writeSessionsLocked(); |
| 241 | |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | void restoreAndApplyStagedSessionIfNeeded() { |
| 246 | List<PackageInstallerSession> stagedSessionsToRestore = new ArrayList<>(); |
| 247 | synchronized (mSessions) { |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 248 | for (int i = 0; i < mSessions.size(); i++) { |
| 249 | final PackageInstallerSession session = mSessions.valueAt(i); |
| 250 | if (session.isStaged()) { |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 251 | stagedSessionsToRestore.add(session); |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 252 | } |
| 253 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 254 | } |
Dario Freni | 4b572c0 | 2019-01-29 09:40:31 +0000 | [diff] [blame] | 255 | // Don't hold mSessions lock when calling restoreSession, since it might trigger an APK |
| 256 | // atomic install which needs to query sessions, which requires lock on mSessions. |
| 257 | for (PackageInstallerSession session : stagedSessionsToRestore) { |
| 258 | mStagingManager.restoreSession(session); |
| 259 | } |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 260 | // Broadcasts are not sent while we restore sessions on boot, since no processes would be |
| 261 | // ready to listen to them. From now on, we greedily assume that broadcasts requests are |
| 262 | // safe to send out. The worst that can happen is that a broadcast is attempted before |
| 263 | // ActivityManagerService completes its own systemReady(), in which case it will be rejected |
| 264 | // with an otherwise harmless exception. |
| 265 | // A more appropriate way to do this would be to wait until the correct boot phase is |
| 266 | // reached, but since we are not a SystemService we can't override onBootPhase. |
| 267 | // Waiting on the BOOT_COMPLETED broadcast can take several minutes, so that's not a viable |
| 268 | // way either. |
| 269 | mOkToSendBroadcasts = true; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 272 | @GuardedBy("mSessions") |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 273 | private void reconcileStagesLocked(String volumeUuid) { |
| 274 | final File stagingDir = getTmpSessionDir(volumeUuid); |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 275 | final ArraySet<File> unclaimedStages = newArraySet( |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 276 | stagingDir.listFiles(sStageFilter)); |
| 277 | |
| 278 | // Ignore stages claimed by active sessions |
| 279 | for (int i = 0; i < mSessions.size(); i++) { |
| 280 | final PackageInstallerSession session = mSessions.valueAt(i); |
| 281 | unclaimedStages.remove(session.stageDir); |
| 282 | } |
| 283 | |
| 284 | // Clean up orphaned staging directories |
| 285 | for (File stage : unclaimedStages) { |
| 286 | Slog.w(TAG, "Deleting orphan stage " + stage); |
| 287 | synchronized (mPm.mInstallLock) { |
Jeff Sharkey | fdeeeea | 2016-01-11 17:34:24 -0700 | [diff] [blame] | 288 | mPm.removeCodePathLI(stage); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | public void onPrivateVolumeMounted(String volumeUuid) { |
| 294 | synchronized (mSessions) { |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 295 | reconcileStagesLocked(volumeUuid); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 299 | public static boolean isStageName(String name) { |
| 300 | final boolean isFile = name.startsWith("vmdl") && name.endsWith(".tmp"); |
| 301 | final boolean isContainer = name.startsWith("smdl") && name.endsWith(".tmp"); |
| 302 | final boolean isLegacyContainer = name.startsWith("smdl2tmp"); |
| 303 | return isFile || isContainer || isLegacyContainer; |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 306 | @Deprecated |
Todd Kennedy | 2699f06 | 2015-11-20 13:07:17 -0800 | [diff] [blame] | 307 | public File allocateStageDirLegacy(String volumeUuid, boolean isEphemeral) throws IOException { |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 308 | synchronized (mSessions) { |
| 309 | try { |
| 310 | final int sessionId = allocateSessionIdLocked(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 311 | mLegacySessions.put(sessionId, true); |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 312 | final File sessionStageDir = buildTmpSessionDir(sessionId, volumeUuid); |
| 313 | prepareStageDir(sessionStageDir); |
| 314 | return sessionStageDir; |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 315 | } catch (IllegalStateException e) { |
| 316 | throw new IOException(e); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 321 | @Deprecated |
| 322 | public String allocateExternalStageCidLegacy() { |
| 323 | synchronized (mSessions) { |
| 324 | final int sessionId = allocateSessionIdLocked(); |
| 325 | mLegacySessions.put(sessionId, true); |
| 326 | return "smdl" + sessionId + ".tmp"; |
| 327 | } |
| 328 | } |
| 329 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 330 | @GuardedBy("mSessions") |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 331 | private void readSessionsLocked() { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 332 | if (LOGD) Slog.v(TAG, "readSessionsLocked()"); |
| 333 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 334 | mSessions.clear(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 335 | |
| 336 | FileInputStream fis = null; |
| 337 | try { |
| 338 | fis = mSessionsFile.openRead(); |
| 339 | final XmlPullParser in = Xml.newPullParser(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 340 | in.setInput(fis, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 341 | |
| 342 | int type; |
| 343 | while ((type = in.next()) != END_DOCUMENT) { |
| 344 | if (type == START_TAG) { |
| 345 | final String tag = in.getName(); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 346 | if (PackageInstallerSession.TAG_SESSION.equals(tag)) { |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 347 | final PackageInstallerSession session; |
| 348 | try { |
| 349 | session = PackageInstallerSession.readFromXml(in, mInternalCallback, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 350 | mContext, mPm, mInstallThread.getLooper(), mStagingManager, |
| 351 | mSessionsDir, this); |
Philip P. Moltmann | 37dd1ba | 2017-09-08 09:46:22 -0700 | [diff] [blame] | 352 | } catch (Exception e) { |
| 353 | Slog.e(TAG, "Could not read session", e); |
| 354 | continue; |
| 355 | } |
| 356 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 357 | final long age = System.currentTimeMillis() - session.createdMillis; |
| 358 | |
| 359 | final boolean valid; |
| 360 | if (age >= MAX_AGE_MILLIS) { |
| 361 | Slog.w(TAG, "Abandoning old session first created at " |
| 362 | + session.createdMillis); |
| 363 | valid = false; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 364 | } else { |
| 365 | valid = true; |
| 366 | } |
| 367 | |
| 368 | if (valid) { |
| 369 | mSessions.put(session.sessionId, session); |
| 370 | } else { |
| 371 | // Since this is early during boot we don't send |
| 372 | // any observer events about the session, but we |
| 373 | // keep details around for dumpsys. |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 374 | addHistoricalSessionLocked(session); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 375 | } |
Todd Kennedy | 28c4e80 | 2016-07-13 13:20:30 -0700 | [diff] [blame] | 376 | mAllocatedSessions.put(session.sessionId, true); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | } |
| 380 | } catch (FileNotFoundException e) { |
| 381 | // Missing sessions are okay, probably first boot |
Svet Ganov | 7121e18 | 2015-07-13 22:38:12 -0700 | [diff] [blame] | 382 | } catch (IOException | XmlPullParserException e) { |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 383 | Slog.wtf(TAG, "Failed reading install sessions", e); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 384 | } finally { |
| 385 | IoUtils.closeQuietly(fis); |
| 386 | } |
| 387 | } |
| 388 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 389 | @GuardedBy("mSessions") |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 390 | private void addHistoricalSessionLocked(PackageInstallerSession session) { |
| 391 | CharArrayWriter writer = new CharArrayWriter(); |
| 392 | IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
| 393 | session.dump(pw); |
| 394 | mHistoricalSessions.add(writer.toString()); |
| 395 | |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 396 | int installerUid = session.getInstallerUid(); |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 397 | // Increment the number of sessions by this installerUid. |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 398 | mHistoricalSessionsByInstaller.put(installerUid, |
| 399 | mHistoricalSessionsByInstaller.get(installerUid) + 1); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 402 | @GuardedBy("mSessions") |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 403 | private void writeSessionsLocked() { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 404 | if (LOGD) Slog.v(TAG, "writeSessionsLocked()"); |
| 405 | |
| 406 | FileOutputStream fos = null; |
| 407 | try { |
| 408 | fos = mSessionsFile.startWrite(); |
| 409 | |
| 410 | XmlSerializer out = new FastXmlSerializer(); |
Wojciech Staszkiewicz | 9e9e2e7 | 2015-05-08 14:58:46 +0100 | [diff] [blame] | 411 | out.setOutput(fos, StandardCharsets.UTF_8.name()); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 412 | out.startDocument(null, true); |
| 413 | out.startTag(null, TAG_SESSIONS); |
| 414 | final int size = mSessions.size(); |
| 415 | for (int i = 0; i < size; i++) { |
| 416 | final PackageInstallerSession session = mSessions.valueAt(i); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 417 | session.write(out, mSessionsDir); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 418 | } |
| 419 | out.endTag(null, TAG_SESSIONS); |
| 420 | out.endDocument(); |
| 421 | |
| 422 | mSessionsFile.finishWrite(fos); |
| 423 | } catch (IOException e) { |
| 424 | if (fos != null) { |
| 425 | mSessionsFile.failWrite(fos); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 430 | private File buildAppIconFile(int sessionId) { |
| 431 | return new File(mSessionsDir, "app_icon." + sessionId + ".png"); |
| 432 | } |
| 433 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 434 | private void writeSessionsAsync() { |
| 435 | IoThread.getHandler().post(new Runnable() { |
| 436 | @Override |
| 437 | public void run() { |
| 438 | synchronized (mSessions) { |
| 439 | writeSessionsLocked(); |
| 440 | } |
| 441 | } |
| 442 | }); |
| 443 | } |
| 444 | |
| 445 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 446 | public int createSession(SessionParams params, String installerPackageName, int userId) { |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 447 | try { |
| 448 | return createSessionInternal(params, installerPackageName, userId); |
| 449 | } catch (IOException e) { |
| 450 | throw ExceptionUtils.wrap(e); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | private int createSessionInternal(SessionParams params, String installerPackageName, int userId) |
| 455 | throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 456 | final int callingUid = Binder.getCallingUid(); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 457 | mPermissionManager.enforceCrossUserPermission( |
| 458 | callingUid, userId, true, true, "createSession"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 459 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 460 | if (mPm.isUserRestricted(userId, UserManager.DISALLOW_INSTALL_APPS)) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 461 | throw new SecurityException("User restriction prevents installing"); |
| 462 | } |
| 463 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 464 | if ((callingUid == Process.SHELL_UID) || (callingUid == Process.ROOT_UID)) { |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 465 | params.installFlags |= PackageManager.INSTALL_FROM_ADB; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 466 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 467 | } else { |
Philip P. Moltmann | 79c238a | 2017-12-13 15:59:07 -0800 | [diff] [blame] | 468 | // Only apps with INSTALL_PACKAGES are allowed to set an installer that is not the |
| 469 | // caller. |
| 470 | if (mContext.checkCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES) != |
| 471 | PackageManager.PERMISSION_GRANTED) { |
| 472 | mAppOps.checkPackage(callingUid, installerPackageName); |
| 473 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 474 | |
Jeff Sharkey | e980804 | 2014-09-11 21:15:37 -0700 | [diff] [blame] | 475 | params.installFlags &= ~PackageManager.INSTALL_FROM_ADB; |
| 476 | params.installFlags &= ~PackageManager.INSTALL_ALL_USERS; |
| 477 | params.installFlags |= PackageManager.INSTALL_REPLACE_EXISTING; |
Todd Kennedy | 78a7250 | 2017-07-19 12:49:30 -0700 | [diff] [blame] | 478 | if ((params.installFlags & PackageManager.INSTALL_VIRTUAL_PRELOAD) != 0 |
| 479 | && !mPm.isCallerVerifier(callingUid)) { |
| 480 | params.installFlags &= ~PackageManager.INSTALL_VIRTUAL_PRELOAD; |
| 481 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Nikita Ioffe | 4501c11 | 2019-02-22 11:51:09 +0000 | [diff] [blame] | 484 | boolean isApex = (params.installFlags & PackageManager.INSTALL_APEX) != 0; |
| 485 | if (params.isStaged || isApex) { |
Dario Freni | 77786d9 | 2019-02-08 17:26:05 +0000 | [diff] [blame] | 486 | mContext.enforceCallingOrSelfPermission(Manifest.permission.INSTALL_PACKAGES, TAG); |
| 487 | } |
| 488 | |
Nikita Ioffe | 4501c11 | 2019-02-22 11:51:09 +0000 | [diff] [blame] | 489 | if (isApex) { |
Dario Freni | 8362060 | 2019-02-18 14:30:57 +0000 | [diff] [blame] | 490 | if (!mApexManager.isApexSupported()) { |
| 491 | throw new IllegalArgumentException( |
| 492 | "This device doesn't support the installation of APEX files"); |
| 493 | } |
| 494 | if (!params.isStaged) { |
| 495 | throw new IllegalArgumentException( |
| 496 | "APEX files can only be installed as part of a staged session."); |
| 497 | } |
| 498 | } |
| 499 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 500 | if (!params.isMultiPackage) { |
| 501 | // Only system components can circumvent runtime permissions when installing. |
| 502 | if ((params.installFlags & PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS) != 0 |
| 503 | && mContext.checkCallingOrSelfPermission(Manifest.permission |
| 504 | .INSTALL_GRANT_RUNTIME_PERMISSIONS) == PackageManager.PERMISSION_DENIED) { |
| 505 | throw new SecurityException("You need the " |
| 506 | + "android.permission.INSTALL_GRANT_RUNTIME_PERMISSIONS permission " |
| 507 | + "to use the PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS flag"); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 510 | // Defensively resize giant app icons |
| 511 | if (params.appIcon != null) { |
| 512 | final ActivityManager am = (ActivityManager) mContext.getSystemService( |
| 513 | Context.ACTIVITY_SERVICE); |
| 514 | final int iconSize = am.getLauncherLargeIconSize(); |
| 515 | if ((params.appIcon.getWidth() > iconSize * 2) |
| 516 | || (params.appIcon.getHeight() > iconSize * 2)) { |
| 517 | params.appIcon = Bitmap.createScaledBitmap(params.appIcon, iconSize, iconSize, |
| 518 | true); |
| 519 | } |
| 520 | } |
Jeff Sharkey | ab23409 | 2015-06-09 21:42:22 -0700 | [diff] [blame] | 521 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 522 | switch (params.mode) { |
| 523 | case SessionParams.MODE_FULL_INSTALL: |
| 524 | case SessionParams.MODE_INHERIT_EXISTING: |
| 525 | break; |
| 526 | default: |
| 527 | throw new IllegalArgumentException("Invalid install mode: " + params.mode); |
| 528 | } |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 529 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 530 | // If caller requested explicit location, sanity check it, otherwise |
| 531 | // resolve the best internal or adopted location. |
| 532 | if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) { |
| 533 | if (!PackageHelper.fitsOnInternal(mContext, params)) { |
| 534 | throw new IOException("No suitable internal storage available"); |
| 535 | } |
Patrick Baumann | ff10c9c | 2018-12-11 15:17:38 -0800 | [diff] [blame] | 536 | } else if ((params.installFlags & PackageManager.INSTALL_FORCE_VOLUME_UUID) != 0) { |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 537 | // For now, installs to adopted media are treated as internal from |
| 538 | // an install flag point-of-view. |
Patrick Baumann | fc2851e | 2018-11-13 15:23:22 -0800 | [diff] [blame] | 539 | params.installFlags |= PackageManager.INSTALL_INTERNAL; |
Patrick Baumann | ff10c9c | 2018-12-11 15:17:38 -0800 | [diff] [blame] | 540 | } else { |
| 541 | params.installFlags |= PackageManager.INSTALL_INTERNAL; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 542 | |
| 543 | // Resolve best location for install, based on combination of |
| 544 | // requested install flags, delta size, and manifest settings. |
| 545 | final long ident = Binder.clearCallingIdentity(); |
| 546 | try { |
| 547 | params.volumeUuid = PackageHelper.resolveInstallVolume(mContext, params); |
| 548 | } finally { |
| 549 | Binder.restoreCallingIdentity(ident); |
| 550 | } |
Robin Lee | e812d90 | 2014-08-21 16:51:18 +0100 | [diff] [blame] | 551 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | final int sessionId; |
| 555 | final PackageInstallerSession session; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 556 | synchronized (mSessions) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 557 | // Sanity check that installer isn't going crazy |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 558 | final int activeCount = getSessionCount(mSessions, callingUid); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 559 | if (activeCount >= MAX_ACTIVE_SESSIONS) { |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 560 | throw new IllegalStateException( |
| 561 | "Too many active sessions for UID " + callingUid); |
| 562 | } |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 563 | final int historicalCount = mHistoricalSessionsByInstaller.get(callingUid); |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 564 | if (historicalCount >= MAX_HISTORICAL_SESSIONS) { |
| 565 | throw new IllegalStateException( |
| 566 | "Too many historical sessions for UID " + callingUid); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 569 | sessionId = allocateSessionIdLocked(); |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 570 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 571 | |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 572 | final long createdMillis = System.currentTimeMillis(); |
| 573 | // We're staging to exactly one location |
| 574 | File stageDir = null; |
| 575 | String stageCid = null; |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 576 | if (!params.isMultiPackage) { |
| 577 | if ((params.installFlags & PackageManager.INSTALL_INTERNAL) != 0) { |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 578 | stageDir = buildSessionDir(sessionId, params); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 579 | } else { |
| 580 | stageCid = buildExternalStageCid(sessionId); |
| 581 | } |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 582 | } |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 583 | session = new PackageInstallerSession(mInternalCallback, mContext, mPm, this, |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 584 | mInstallThread.getLooper(), mStagingManager, sessionId, userId, |
| 585 | installerPackageName, callingUid, params, createdMillis, stageDir, stageCid, false, |
Dario Freni | b6d2896 | 2019-01-31 15:52:24 +0000 | [diff] [blame] | 586 | false, null, SessionInfo.INVALID_ID, false, false, false, |
| 587 | SessionInfo.STAGED_SESSION_NO_ERROR, ""); |
Todd Kennedy | 04918fe | 2016-07-12 14:07:40 -0700 | [diff] [blame] | 588 | |
| 589 | synchronized (mSessions) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 590 | mSessions.put(sessionId, session); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 591 | } |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 592 | if (params.isStaged) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 593 | mStagingManager.createSession(session); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 594 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 595 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 596 | mCallbacks.notifySessionCreated(session.sessionId, session.userId); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 597 | writeSessionsAsync(); |
| 598 | return sessionId; |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 599 | } |
| 600 | |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 601 | @Override |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 602 | public void updateSessionAppIcon(int sessionId, Bitmap appIcon) { |
| 603 | synchronized (mSessions) { |
| 604 | final PackageInstallerSession session = mSessions.get(sessionId); |
| 605 | if (session == null || !isCallingUidOwner(session)) { |
| 606 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 607 | } |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 608 | |
| 609 | // Defensively resize giant app icons |
| 610 | if (appIcon != null) { |
| 611 | final ActivityManager am = (ActivityManager) mContext.getSystemService( |
| 612 | Context.ACTIVITY_SERVICE); |
| 613 | final int iconSize = am.getLauncherLargeIconSize(); |
| 614 | if ((appIcon.getWidth() > iconSize * 2) |
| 615 | || (appIcon.getHeight() > iconSize * 2)) { |
| 616 | appIcon = Bitmap.createScaledBitmap(appIcon, iconSize, iconSize, true); |
| 617 | } |
| 618 | } |
| 619 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 620 | session.params.appIcon = appIcon; |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 621 | session.params.appIconLastModified = -1; |
| 622 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 623 | mInternalCallback.onSessionBadgingChanged(session); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | @Override |
| 628 | public void updateSessionAppLabel(int sessionId, String appLabel) { |
| 629 | synchronized (mSessions) { |
| 630 | final PackageInstallerSession session = mSessions.get(sessionId); |
| 631 | if (session == null || !isCallingUidOwner(session)) { |
| 632 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 633 | } |
| 634 | session.params.appLabel = appLabel; |
| 635 | mInternalCallback.onSessionBadgingChanged(session); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | @Override |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 640 | public void abandonSession(int sessionId) { |
| 641 | synchronized (mSessions) { |
| 642 | final PackageInstallerSession session = mSessions.get(sessionId); |
| 643 | if (session == null || !isCallingUidOwner(session)) { |
| 644 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 645 | } |
| 646 | session.abandon(); |
| 647 | } |
| 648 | } |
| 649 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 650 | @Override |
| 651 | public IPackageInstallerSession openSession(int sessionId) { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 652 | try { |
| 653 | return openSessionInternal(sessionId); |
| 654 | } catch (IOException e) { |
| 655 | throw ExceptionUtils.wrap(e); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | private IPackageInstallerSession openSessionInternal(int sessionId) throws IOException { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 660 | synchronized (mSessions) { |
| 661 | final PackageInstallerSession session = mSessions.get(sessionId); |
Jeff Sharkey | 381d94b | 2014-08-24 14:45:56 -0700 | [diff] [blame] | 662 | if (session == null || !isCallingUidOwner(session)) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 663 | throw new SecurityException("Caller has no access to session " + sessionId); |
| 664 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 665 | session.open(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 666 | return session; |
| 667 | } |
| 668 | } |
| 669 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 670 | @GuardedBy("mSessions") |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 671 | private int allocateSessionIdLocked() { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 672 | int n = 0; |
| 673 | int sessionId; |
| 674 | do { |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 675 | sessionId = mRandom.nextInt(Integer.MAX_VALUE - 1) + 1; |
Todd Kennedy | 28c4e80 | 2016-07-13 13:20:30 -0700 | [diff] [blame] | 676 | if (!mAllocatedSessions.get(sessionId, false)) { |
| 677 | mAllocatedSessions.put(sessionId, true); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 678 | return sessionId; |
| 679 | } |
| 680 | } while (n++ < 32); |
| 681 | |
| 682 | throw new IllegalStateException("Failed to allocate session ID"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 685 | private File getTmpSessionDir(String volumeUuid) { |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 686 | return Environment.getDataAppDirectory(volumeUuid); |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 687 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 688 | |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 689 | private File buildTmpSessionDir(int sessionId, String volumeUuid) { |
| 690 | final File sessionStagingDir = getTmpSessionDir(volumeUuid); |
| 691 | return new File(sessionStagingDir, "vmdl" + sessionId + ".tmp"); |
| 692 | } |
| 693 | |
| 694 | private File buildSessionDir(int sessionId, SessionParams params) { |
| 695 | if (params.isStaged) { |
| 696 | final File sessionStagingDir = Environment.getDataStagingDirectory(params.volumeUuid); |
| 697 | return new File(sessionStagingDir, "session_" + sessionId); |
| 698 | } |
| 699 | return buildTmpSessionDir(sessionId, params.volumeUuid); |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | static void prepareStageDir(File stageDir) throws IOException { |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 703 | if (stageDir.exists()) { |
| 704 | throw new IOException("Session dir already exists: " + stageDir); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | try { |
Dario Freni | d8bf22e | 2018-08-31 14:18:04 +0100 | [diff] [blame] | 708 | Os.mkdir(stageDir.getAbsolutePath(), 0775); |
| 709 | Os.chmod(stageDir.getAbsolutePath(), 0775); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 710 | } catch (ErrnoException e) { |
| 711 | // This purposefully throws if directory already exists |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 712 | throw new IOException("Failed to prepare session dir: " + stageDir, e); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 715 | if (!SELinux.restorecon(stageDir)) { |
| 716 | throw new IOException("Failed to restorecon session dir: " + stageDir); |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 717 | } |
Jeff Sharkey | ec55ef0 | 2014-07-08 11:28:00 -0700 | [diff] [blame] | 718 | } |
| 719 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 720 | private String buildExternalStageCid(int sessionId) { |
| 721 | return "smdl" + sessionId + ".tmp"; |
| 722 | } |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 723 | |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 724 | @Override |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 725 | public SessionInfo getSessionInfo(int sessionId) { |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 726 | synchronized (mSessions) { |
| 727 | final PackageInstallerSession session = mSessions.get(sessionId); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 728 | return session != null ? session.generateInfo() : null; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | @Override |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 733 | public ParceledListSlice<SessionInfo> getStagedSessions() { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 734 | return mStagingManager.getSessions(); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | @Override |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 738 | public ParceledListSlice<SessionInfo> getAllSessions(int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 739 | mPermissionManager.enforceCrossUserPermission( |
| 740 | Binder.getCallingUid(), userId, true, false, "getAllSessions"); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 741 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 742 | final List<SessionInfo> result = new ArrayList<>(); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 743 | synchronized (mSessions) { |
| 744 | for (int i = 0; i < mSessions.size(); i++) { |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 745 | final PackageInstallerSession session = mSessions.valueAt(i); |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 746 | if (session.userId == userId && !session.hasParentSessionId()) { |
Jeff Sharkey | da1247a | 2017-06-08 14:13:29 -0600 | [diff] [blame] | 747 | result.add(session.generateInfo(false)); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | } |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 751 | return new ParceledListSlice<>(result); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | @Override |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 755 | public ParceledListSlice<SessionInfo> getMySessions(String installerPackageName, int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 756 | mPermissionManager.enforceCrossUserPermission( |
| 757 | Binder.getCallingUid(), userId, true, false, "getMySessions"); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 758 | mAppOps.checkPackage(Binder.getCallingUid(), installerPackageName); |
| 759 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 760 | final List<SessionInfo> result = new ArrayList<>(); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 761 | synchronized (mSessions) { |
| 762 | for (int i = 0; i < mSessions.size(); i++) { |
| 763 | final PackageInstallerSession session = mSessions.valueAt(i); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 764 | |
| 765 | SessionInfo info = session.generateInfo(false); |
| 766 | if (Objects.equals(info.getInstallerPackageName(), installerPackageName) |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 767 | && session.userId == userId && !session.hasParentSessionId()) { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 768 | result.add(info); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | } |
Jeff Sharkey | 97d47ed | 2014-10-15 09:19:47 -0700 | [diff] [blame] | 772 | return new ParceledListSlice<>(result); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | @Override |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 776 | public void uninstall(VersionedPackage versionedPackage, String callerPackageName, int flags, |
| 777 | IntentSender statusReceiver, int userId) throws RemoteException { |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 778 | final int callingUid = Binder.getCallingUid(); |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 779 | mPermissionManager.enforceCrossUserPermission(callingUid, userId, true, true, "uninstall"); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 780 | if ((callingUid != Process.SHELL_UID) && (callingUid != Process.ROOT_UID)) { |
| 781 | mAppOps.checkPackage(callingUid, callerPackageName); |
| 782 | } |
| 783 | |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 784 | // Check whether the caller is device owner or affiliated profile owner, in which case we do |
| 785 | // it silently. |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 786 | DevicePolicyManagerInternal dpmi = |
| 787 | LocalServices.getService(DevicePolicyManagerInternal.class); |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 788 | final boolean canSilentlyInstallPackage = |
| 789 | dpmi != null && dpmi.canSilentlyInstallPackage(callerPackageName, callingUid); |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 790 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 791 | final PackageDeleteObserverAdapter adapter = new PackageDeleteObserverAdapter(mContext, |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 792 | statusReceiver, versionedPackage.getPackageName(), |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 793 | canSilentlyInstallPackage, userId); |
Sudheer Shanka | 72de4dd | 2016-07-22 15:46:37 -0700 | [diff] [blame] | 794 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DELETE_PACKAGES) |
| 795 | == PackageManager.PERMISSION_GRANTED) { |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 796 | // Sweet, call straight through! |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 797 | mPm.deletePackageVersioned(versionedPackage, adapter.getBinder(), userId, flags); |
Rubin Xu | fd4a3b4 | 2018-12-05 16:03:27 +0000 | [diff] [blame] | 798 | } else if (canSilentlyInstallPackage) { |
Benjamin Franz | dabae88 | 2017-08-08 12:33:19 +0100 | [diff] [blame] | 799 | // Allow the device owner and affiliated profile owner to silently delete packages |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 800 | // Need to clear the calling identity to get DELETE_PACKAGES permission |
| 801 | long ident = Binder.clearCallingIdentity(); |
| 802 | try { |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 803 | mPm.deletePackageVersioned(versionedPackage, adapter.getBinder(), userId, flags); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 804 | } finally { |
| 805 | Binder.restoreCallingIdentity(ident); |
| 806 | } |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 807 | } else { |
Philip P. Moltmann | d9bb39a | 2017-09-05 12:41:15 -0700 | [diff] [blame] | 808 | ApplicationInfo appInfo = mPm.getApplicationInfo(callerPackageName, 0, userId); |
| 809 | if (appInfo.targetSdkVersion >= Build.VERSION_CODES.P) { |
| 810 | mContext.enforceCallingOrSelfPermission(Manifest.permission.REQUEST_DELETE_PACKAGES, |
| 811 | null); |
| 812 | } |
| 813 | |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 814 | // Take a short detour to confirm with user |
| 815 | final Intent intent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE); |
Svet Ganov | 6788212 | 2016-12-11 16:36:34 -0800 | [diff] [blame] | 816 | intent.setData(Uri.fromParts("package", versionedPackage.getPackageName(), null)); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 817 | intent.putExtra(PackageInstaller.EXTRA_CALLBACK, adapter.getBinder().asBinder()); |
| 818 | adapter.onUserActionRequired(intent); |
Jeff Sharkey | f060095 | 2014-08-07 17:31:53 -0700 | [diff] [blame] | 819 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | @Override |
Chandan Nath | e8e463b | 2019-01-28 15:23:38 +0000 | [diff] [blame] | 823 | public void installExistingPackage(String packageName, int installFlags, int installReason, |
| 824 | IntentSender statusReceiver, int userId) { |
| 825 | mPm.installExistingPackageAsUser(packageName, userId, installFlags, installReason, |
| 826 | statusReceiver); |
| 827 | } |
| 828 | |
| 829 | @Override |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 830 | public void setPermissionsResult(int sessionId, boolean accepted) { |
| 831 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, TAG); |
| 832 | |
| 833 | synchronized (mSessions) { |
Svet Ganov | 3baa876 | 2016-04-08 09:22:54 -0700 | [diff] [blame] | 834 | PackageInstallerSession session = mSessions.get(sessionId); |
| 835 | if (session != null) { |
| 836 | session.setPermissionsResult(accepted); |
| 837 | } |
Jeff Sharkey | 7328a1b | 2014-08-07 14:01:43 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
| 840 | |
| 841 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 842 | public void registerCallback(IPackageInstallerCallback callback, int userId) { |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 843 | mPermissionManager.enforceCrossUserPermission( |
| 844 | Binder.getCallingUid(), userId, true, false, "registerCallback"); |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 845 | registerCallback(callback, eventUserId -> userId == eventUserId); |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Assume permissions already checked and caller's identity cleared |
| 850 | */ |
| 851 | public void registerCallback(IPackageInstallerCallback callback, IntPredicate userCheck) { |
| 852 | mCallbacks.register(callback, userCheck); |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | @Override |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 856 | public void unregisterCallback(IPackageInstallerCallback callback) { |
| 857 | mCallbacks.unregister(callback); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Patrick Baumann | 0aff9b1 | 2018-11-08 14:05:08 +0000 | [diff] [blame] | 860 | @Override |
| 861 | public PackageInstallerSession getSession(int sessionId) { |
| 862 | synchronized (mSessions) { |
| 863 | return mSessions.get(sessionId); |
| 864 | } |
| 865 | } |
| 866 | |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 867 | private static int getSessionCount(SparseArray<PackageInstallerSession> sessions, |
| 868 | int installerUid) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 869 | int count = 0; |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 870 | final int size = sessions.size(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 871 | for (int i = 0; i < size; i++) { |
Jeff Sharkey | f174c6e | 2014-08-05 10:42:27 -0700 | [diff] [blame] | 872 | final PackageInstallerSession session = sessions.valueAt(i); |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 873 | if (session.getInstallerUid() == installerUid) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 874 | count++; |
| 875 | } |
| 876 | } |
| 877 | return count; |
| 878 | } |
| 879 | |
| 880 | private boolean isCallingUidOwner(PackageInstallerSession session) { |
| 881 | final int callingUid = Binder.getCallingUid(); |
| 882 | if (callingUid == Process.ROOT_UID) { |
| 883 | return true; |
| 884 | } else { |
Philip P. Moltmann | 7460c59 | 2017-08-08 20:07:11 +0000 | [diff] [blame] | 885 | return (session != null) && (callingUid == session.getInstallerUid()); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 889 | static class PackageDeleteObserverAdapter extends PackageDeleteObserver { |
| 890 | private final Context mContext; |
| 891 | private final IntentSender mTarget; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 892 | private final String mPackageName; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 893 | private final Notification mNotification; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 894 | |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 895 | public PackageDeleteObserverAdapter(Context context, IntentSender target, |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 896 | String packageName, boolean showNotification, int userId) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 897 | mContext = context; |
| 898 | mTarget = target; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 899 | mPackageName = packageName; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 900 | if (showNotification) { |
| 901 | mNotification = buildSuccessNotification(mContext, |
| 902 | mContext.getResources().getString(R.string.package_deleted_device_owner), |
| 903 | packageName, |
| 904 | userId); |
| 905 | } else { |
| 906 | mNotification = null; |
| 907 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | @Override |
| 911 | public void onUserActionRequired(Intent intent) { |
| 912 | final Intent fillIn = new Intent(); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 913 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, mPackageName); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 914 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 915 | PackageInstaller.STATUS_PENDING_USER_ACTION); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 916 | fillIn.putExtra(Intent.EXTRA_INTENT, intent); |
| 917 | try { |
| 918 | mTarget.sendIntent(mContext, 0, fillIn, null, null); |
| 919 | } catch (SendIntentException ignored) { |
| 920 | } |
| 921 | } |
| 922 | |
| 923 | @Override |
| 924 | public void onPackageDeleted(String basePackageName, int returnCode, String msg) { |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 925 | if (PackageManager.DELETE_SUCCEEDED == returnCode && mNotification != null) { |
| 926 | NotificationManager notificationManager = (NotificationManager) |
| 927 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 928 | notificationManager.notify(basePackageName, |
| 929 | SystemMessage.NOTE_PACKAGE_STATE, |
| 930 | mNotification); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 931 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 932 | final Intent fillIn = new Intent(); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 933 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, mPackageName); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 934 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
| 935 | PackageManager.deleteStatusToPublicStatus(returnCode)); |
| 936 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, |
| 937 | PackageManager.deleteStatusToString(returnCode, msg)); |
| 938 | fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode); |
| 939 | try { |
| 940 | mTarget.sendIntent(mContext, 0, fillIn, null, null); |
| 941 | } catch (SendIntentException ignored) { |
| 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | static class PackageInstallObserverAdapter extends PackageInstallObserver { |
| 947 | private final Context mContext; |
| 948 | private final IntentSender mTarget; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 949 | private final int mSessionId; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 950 | private final boolean mShowNotification; |
| 951 | private final int mUserId; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 952 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 953 | public PackageInstallObserverAdapter(Context context, IntentSender target, int sessionId, |
| 954 | boolean showNotification, int userId) { |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 955 | mContext = context; |
| 956 | mTarget = target; |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 957 | mSessionId = sessionId; |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 958 | mShowNotification = showNotification; |
| 959 | mUserId = userId; |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | @Override |
| 963 | public void onUserActionRequired(Intent intent) { |
| 964 | final Intent fillIn = new Intent(); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 965 | fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, mSessionId); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 966 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 967 | PackageInstaller.STATUS_PENDING_USER_ACTION); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 968 | fillIn.putExtra(Intent.EXTRA_INTENT, intent); |
| 969 | try { |
| 970 | mTarget.sendIntent(mContext, 0, fillIn, null, null); |
| 971 | } catch (SendIntentException ignored) { |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public void onPackageInstalled(String basePackageName, int returnCode, String msg, |
| 977 | Bundle extras) { |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 978 | if (PackageManager.INSTALL_SUCCEEDED == returnCode && mShowNotification) { |
Benjamin Franz | 2e3e943 | 2015-04-17 15:28:17 +0100 | [diff] [blame] | 979 | boolean update = (extras != null) && extras.getBoolean(Intent.EXTRA_REPLACING); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 980 | Notification notification = buildSuccessNotification(mContext, |
Benjamin Franz | 2e3e943 | 2015-04-17 15:28:17 +0100 | [diff] [blame] | 981 | mContext.getResources() |
| 982 | .getString(update ? R.string.package_updated_device_owner : |
| 983 | R.string.package_installed_device_owner), |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 984 | basePackageName, |
| 985 | mUserId); |
| 986 | if (notification != null) { |
| 987 | NotificationManager notificationManager = (NotificationManager) |
| 988 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 989 | notificationManager.notify(basePackageName, |
| 990 | SystemMessage.NOTE_PACKAGE_STATE, |
| 991 | notification); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 992 | } |
| 993 | } |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 994 | final Intent fillIn = new Intent(); |
Benjamin Franz | 2e3e943 | 2015-04-17 15:28:17 +0100 | [diff] [blame] | 995 | fillIn.putExtra(PackageInstaller.EXTRA_PACKAGE_NAME, basePackageName); |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 996 | fillIn.putExtra(PackageInstaller.EXTRA_SESSION_ID, mSessionId); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 997 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS, |
| 998 | PackageManager.installStatusToPublicStatus(returnCode)); |
| 999 | fillIn.putExtra(PackageInstaller.EXTRA_STATUS_MESSAGE, |
| 1000 | PackageManager.installStatusToString(returnCode, msg)); |
| 1001 | fillIn.putExtra(PackageInstaller.EXTRA_LEGACY_STATUS, returnCode); |
| 1002 | if (extras != null) { |
| 1003 | final String existing = extras.getString( |
| 1004 | PackageManager.EXTRA_FAILURE_EXISTING_PACKAGE); |
| 1005 | if (!TextUtils.isEmpty(existing)) { |
Jeff Sharkey | 941a8ba | 2014-08-20 16:26:32 -0700 | [diff] [blame] | 1006 | fillIn.putExtra(PackageInstaller.EXTRA_OTHER_PACKAGE_NAME, existing); |
Jeff Sharkey | a090743 | 2014-08-15 10:23:11 -0700 | [diff] [blame] | 1007 | } |
| 1008 | } |
| 1009 | try { |
| 1010 | mTarget.sendIntent(mContext, 0, fillIn, null, null); |
| 1011 | } catch (SendIntentException ignored) { |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1016 | /** |
| 1017 | * Build a notification for package installation / deletion by device owners that is shown if |
| 1018 | * the operation succeeds. |
| 1019 | */ |
| 1020 | private static Notification buildSuccessNotification(Context context, String contentText, |
| 1021 | String basePackageName, int userId) { |
| 1022 | PackageInfo packageInfo = null; |
| 1023 | try { |
| 1024 | packageInfo = AppGlobals.getPackageManager().getPackageInfo( |
Svet Ganov | a5c867c | 2017-05-15 01:17:05 -0700 | [diff] [blame] | 1025 | basePackageName, PackageManager.MATCH_STATIC_SHARED_LIBRARIES, userId); |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1026 | } catch (RemoteException ignored) { |
| 1027 | } |
| 1028 | if (packageInfo == null || packageInfo.applicationInfo == null) { |
| 1029 | Slog.w(TAG, "Notification not built for package: " + basePackageName); |
| 1030 | return null; |
| 1031 | } |
| 1032 | PackageManager pm = context.getPackageManager(); |
| 1033 | Bitmap packageIcon = ImageUtils.buildScaledBitmap( |
| 1034 | packageInfo.applicationInfo.loadIcon(pm), |
| 1035 | context.getResources().getDimensionPixelSize( |
| 1036 | android.R.dimen.notification_large_icon_width), |
| 1037 | context.getResources().getDimensionPixelSize( |
| 1038 | android.R.dimen.notification_large_icon_height)); |
| 1039 | CharSequence packageLabel = packageInfo.applicationInfo.loadLabel(pm); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 1040 | return new Notification.Builder(context, SystemNotificationChannels.DEVICE_ADMIN) |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1041 | .setSmallIcon(R.drawable.ic_check_circle_24px) |
| 1042 | .setColor(context.getResources().getColor( |
| 1043 | R.color.system_notification_accent_color)) |
| 1044 | .setContentTitle(packageLabel) |
| 1045 | .setContentText(contentText) |
Benjamin Franz | 2e3e943 | 2015-04-17 15:28:17 +0100 | [diff] [blame] | 1046 | .setStyle(new Notification.BigTextStyle().bigText(contentText)) |
Benjamin Franz | 39fb7fd | 2015-02-18 16:11:18 +0000 | [diff] [blame] | 1047 | .setLargeIcon(packageIcon) |
| 1048 | .build(); |
| 1049 | } |
| 1050 | |
Jeff Sharkey | 54d42be | 2015-07-20 16:36:55 -0700 | [diff] [blame] | 1051 | public static <E> ArraySet<E> newArraySet(E... elements) { |
| 1052 | final ArraySet<E> set = new ArraySet<E>(); |
| 1053 | if (elements != null) { |
| 1054 | set.ensureCapacity(elements.length); |
| 1055 | Collections.addAll(set, elements); |
| 1056 | } |
| 1057 | return set; |
| 1058 | } |
| 1059 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1060 | private static class Callbacks extends Handler { |
| 1061 | private static final int MSG_SESSION_CREATED = 1; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1062 | private static final int MSG_SESSION_BADGING_CHANGED = 2; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1063 | private static final int MSG_SESSION_ACTIVE_CHANGED = 3; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1064 | private static final int MSG_SESSION_PROGRESS_CHANGED = 4; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1065 | private static final int MSG_SESSION_FINISHED = 5; |
Jeff Sharkey | 16c8e3f | 2014-07-24 17:08:17 -0700 | [diff] [blame] | 1066 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1067 | private final RemoteCallbackList<IPackageInstallerCallback> |
| 1068 | mCallbacks = new RemoteCallbackList<>(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1069 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1070 | public Callbacks(Looper looper) { |
| 1071 | super(looper); |
| 1072 | } |
| 1073 | |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1074 | public void register(IPackageInstallerCallback callback, IntPredicate userCheck) { |
| 1075 | mCallbacks.register(callback, userCheck); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | public void unregister(IPackageInstallerCallback callback) { |
| 1079 | mCallbacks.unregister(callback); |
| 1080 | } |
| 1081 | |
| 1082 | @Override |
| 1083 | public void handleMessage(Message msg) { |
| 1084 | final int userId = msg.arg2; |
| 1085 | final int n = mCallbacks.beginBroadcast(); |
| 1086 | for (int i = 0; i < n; i++) { |
| 1087 | final IPackageInstallerCallback callback = mCallbacks.getBroadcastItem(i); |
Jon Miranda | 2b340a2 | 2019-01-25 14:03:49 -0800 | [diff] [blame] | 1088 | final IntPredicate userCheck = (IntPredicate) mCallbacks.getBroadcastCookie(i); |
| 1089 | if (userCheck.test(userId)) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1090 | try { |
| 1091 | invokeCallback(callback, msg); |
| 1092 | } catch (RemoteException ignored) { |
| 1093 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1094 | } |
| 1095 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1096 | mCallbacks.finishBroadcast(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1097 | } |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1098 | |
| 1099 | private void invokeCallback(IPackageInstallerCallback callback, Message msg) |
| 1100 | throws RemoteException { |
| 1101 | final int sessionId = msg.arg1; |
| 1102 | switch (msg.what) { |
| 1103 | case MSG_SESSION_CREATED: |
| 1104 | callback.onSessionCreated(sessionId); |
| 1105 | break; |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1106 | case MSG_SESSION_BADGING_CHANGED: |
| 1107 | callback.onSessionBadgingChanged(sessionId); |
| 1108 | break; |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1109 | case MSG_SESSION_ACTIVE_CHANGED: |
| 1110 | callback.onSessionActiveChanged(sessionId, (boolean) msg.obj); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1111 | break; |
| 1112 | case MSG_SESSION_PROGRESS_CHANGED: |
| 1113 | callback.onSessionProgressChanged(sessionId, (float) msg.obj); |
| 1114 | break; |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1115 | case MSG_SESSION_FINISHED: |
| 1116 | callback.onSessionFinished(sessionId, (boolean) msg.obj); |
| 1117 | break; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | private void notifySessionCreated(int sessionId, int userId) { |
| 1122 | obtainMessage(MSG_SESSION_CREATED, sessionId, userId).sendToTarget(); |
| 1123 | } |
| 1124 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1125 | private void notifySessionBadgingChanged(int sessionId, int userId) { |
| 1126 | obtainMessage(MSG_SESSION_BADGING_CHANGED, sessionId, userId).sendToTarget(); |
| 1127 | } |
| 1128 | |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1129 | private void notifySessionActiveChanged(int sessionId, int userId, boolean active) { |
| 1130 | obtainMessage(MSG_SESSION_ACTIVE_CHANGED, sessionId, userId, active).sendToTarget(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | private void notifySessionProgressChanged(int sessionId, int userId, float progress) { |
| 1134 | obtainMessage(MSG_SESSION_PROGRESS_CHANGED, sessionId, userId, progress).sendToTarget(); |
| 1135 | } |
| 1136 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1137 | public void notifySessionFinished(int sessionId, int userId, boolean success) { |
| 1138 | obtainMessage(MSG_SESSION_FINISHED, sessionId, userId, success).sendToTarget(); |
| 1139 | } |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1143 | synchronized (mSessions) { |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1144 | pw.println("Active install sessions:"); |
| 1145 | pw.increaseIndent(); |
| 1146 | int N = mSessions.size(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1147 | for (int i = 0; i < N; i++) { |
| 1148 | final PackageInstallerSession session = mSessions.valueAt(i); |
| 1149 | session.dump(pw); |
| 1150 | pw.println(); |
| 1151 | } |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1152 | pw.println(); |
| 1153 | pw.decreaseIndent(); |
| 1154 | |
| 1155 | pw.println("Historical install sessions:"); |
| 1156 | pw.increaseIndent(); |
| 1157 | N = mHistoricalSessions.size(); |
| 1158 | for (int i = 0; i < N; i++) { |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 1159 | pw.print(mHistoricalSessions.get(i)); |
Jeff Sharkey | 9a44577 | 2014-07-16 11:32:08 -0700 | [diff] [blame] | 1160 | pw.println(); |
| 1161 | } |
| 1162 | pw.println(); |
| 1163 | pw.decreaseIndent(); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1164 | |
| 1165 | pw.println("Legacy install sessions:"); |
| 1166 | pw.increaseIndent(); |
| 1167 | pw.println(mLegacySessions.toString()); |
| 1168 | pw.decreaseIndent(); |
Jeff Sharkey | a103114 | 2014-07-12 18:09:46 -0700 | [diff] [blame] | 1169 | } |
Jeff Sharkey | bb58067 | 2014-07-10 12:10:25 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1172 | class InternalCallback { |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1173 | public void onSessionBadgingChanged(PackageInstallerSession session) { |
| 1174 | mCallbacks.notifySessionBadgingChanged(session.sessionId, session.userId); |
| 1175 | writeSessionsAsync(); |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Jeff Sharkey | bc7bce3 | 2014-09-05 15:53:05 -0700 | [diff] [blame] | 1178 | public void onSessionActiveChanged(PackageInstallerSession session, boolean active) { |
| 1179 | mCallbacks.notifySessionActiveChanged(session.sessionId, session.userId, active); |
Jeff Sharkey | 742e790 | 2014-08-16 19:09:13 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Jeff Sharkey | ec9bad2 | 2014-09-05 09:45:20 -0700 | [diff] [blame] | 1182 | public void onSessionProgressChanged(PackageInstallerSession session, float progress) { |
| 1183 | mCallbacks.notifySessionProgressChanged(session.sessionId, session.userId, progress); |
| 1184 | } |
| 1185 | |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 1186 | public void onStagedSessionChanged(PackageInstallerSession session) { |
| 1187 | writeSessionsAsync(); |
Dario Freni | 14f885b | 2019-02-25 12:48:47 +0000 | [diff] [blame] | 1188 | if (mOkToSendBroadcasts) { |
Dario Freni | 9694b80 | 2019-01-27 23:26:06 +0000 | [diff] [blame] | 1189 | mPm.sendSessionUpdatedBroadcast(session.generateInfo(false), |
| 1190 | session.userId); |
| 1191 | } |
Dario Freni | 0180d0b | 2019-01-11 21:08:13 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1194 | public void onSessionFinished(final PackageInstallerSession session, boolean success) { |
Jeff Sharkey | 1cb2d0d | 2014-07-30 16:45:01 -0700 | [diff] [blame] | 1195 | mCallbacks.notifySessionFinished(session.sessionId, session.userId, success); |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1196 | |
| 1197 | mInstallHandler.post(new Runnable() { |
| 1198 | @Override |
| 1199 | public void run() { |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 1200 | if (session.isStaged()) { |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 1201 | if (!success) { |
Dario Freni | be98c3f | 2018-12-22 15:25:27 +0000 | [diff] [blame] | 1202 | mStagingManager.abortSession(session); |
Dario Freni | aac4ba4 | 2018-12-06 15:47:16 +0000 | [diff] [blame] | 1203 | } |
| 1204 | } |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1205 | synchronized (mSessions) { |
Dario Freni | 8e7d0ec | 2019-01-10 15:21:40 +0000 | [diff] [blame] | 1206 | if (!session.isStaged() || !success) { |
| 1207 | mSessions.remove(session.sessionId); |
| 1208 | } |
Narayan Kamath | a22a766 | 2017-06-12 13:34:29 +0100 | [diff] [blame] | 1209 | addHistoricalSessionLocked(session); |
Jeff Sharkey | 02bd784 | 2014-10-06 15:14:27 -0700 | [diff] [blame] | 1210 | |
| 1211 | final File appIconFile = buildAppIconFile(session.sessionId); |
| 1212 | if (appIconFile.exists()) { |
| 1213 | appIconFile.delete(); |
| 1214 | } |
| 1215 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1216 | writeSessionsLocked(); |
| 1217 | } |
| 1218 | } |
| 1219 | }); |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1220 | } |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1221 | |
Jeff Sharkey | 77d218e | 2014-09-06 12:20:37 -0700 | [diff] [blame] | 1222 | public void onSessionPrepared(PackageInstallerSession session) { |
| 1223 | // We prepared the destination to write into; we want to persist |
| 1224 | // this, but it's not critical enough to block for. |
| 1225 | writeSessionsAsync(); |
| 1226 | } |
| 1227 | |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1228 | public void onSessionSealedBlocking(PackageInstallerSession session) { |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1229 | // It's very important that we block until we've recorded the |
| 1230 | // session as being sealed, since we never want to allow mutation |
| 1231 | // after sealing. |
Jeff Sharkey | cbf4791 | 2014-09-12 09:55:32 -0700 | [diff] [blame] | 1232 | synchronized (mSessions) { |
| 1233 | writeSessionsLocked(); |
| 1234 | } |
Jeff Sharkey | bb7b7be | 2014-08-19 16:18:28 -0700 | [diff] [blame] | 1235 | } |
Jeff Sharkey | 3a44f3f | 2014-04-28 17:36:31 -0700 | [diff] [blame] | 1236 | } |
| 1237 | } |