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