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