Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.server.pm; |
| 17 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 18 | import android.Manifest.permission; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.annotation.UserIdInt; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Makoto Onuki | 33525d2 | 2016-08-03 15:45:24 -0700 | [diff] [blame] | 24 | import android.app.ActivityManagerInternal; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 25 | import android.app.AppGlobals; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 26 | import android.app.IUidObserver; |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 27 | import android.app.usage.UsageStatsManagerInternal; |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 28 | import android.appwidget.AppWidgetProviderInfo; |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 33 | import android.content.IntentFilter; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 34 | import android.content.IntentSender; |
| 35 | import android.content.IntentSender.SendIntentException; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 36 | import android.content.pm.ActivityInfo; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
| 38 | import android.content.pm.IPackageManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 39 | import android.content.pm.IShortcutService; |
| 40 | import android.content.pm.LauncherApps; |
| 41 | import android.content.pm.LauncherApps.ShortcutQuery; |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 42 | import android.content.pm.PackageInfo; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 43 | import android.content.pm.PackageManager; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 44 | import android.content.pm.PackageManager.NameNotFoundException; |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManagerInternal; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 46 | import android.content.pm.ParceledListSlice; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 47 | import android.content.pm.ResolveInfo; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 48 | import android.content.pm.ShortcutInfo; |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 49 | import android.content.pm.ShortcutManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 50 | import android.content.pm.ShortcutServiceInternal; |
| 51 | import android.content.pm.ShortcutServiceInternal.ShortcutChangeListener; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 52 | import android.content.res.Resources; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 53 | import android.content.res.XmlResourceParser; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 54 | import android.graphics.Bitmap; |
| 55 | import android.graphics.Bitmap.CompressFormat; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 56 | import android.graphics.Canvas; |
| 57 | import android.graphics.RectF; |
Hyunyoung Song | 4703746 | 2017-05-08 16:51:43 -0700 | [diff] [blame] | 58 | import android.graphics.drawable.AdaptiveIconDrawable; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 59 | import android.graphics.drawable.Icon; |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 60 | import android.net.Uri; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 61 | import android.os.Binder; |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 62 | import android.os.Build; |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 63 | import android.os.Bundle; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 64 | import android.os.Environment; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 65 | import android.os.FileUtils; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 66 | import android.os.Handler; |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 67 | import android.os.LocaleList; |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 68 | import android.os.Looper; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 69 | import android.os.ParcelFileDescriptor; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 70 | import android.os.PersistableBundle; |
| 71 | import android.os.Process; |
| 72 | import android.os.RemoteException; |
| 73 | import android.os.ResultReceiver; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 74 | import android.os.SELinux; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 75 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 76 | import android.os.ShellCallback; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 77 | import android.os.ShellCommand; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 78 | import android.os.SystemClock; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 79 | import android.os.UserHandle; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 80 | import android.os.UserManagerInternal; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 81 | import android.text.TextUtils; |
Neil Fuller | 9aa7b74 | 2019-06-17 11:40:35 +0100 | [diff] [blame] | 82 | import android.text.format.TimeMigrationUtils; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 83 | import android.util.ArraySet; |
| 84 | import android.util.AtomicFile; |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 85 | import android.util.KeyValueListParser; |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 86 | import android.util.Log; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 87 | import android.util.Slog; |
| 88 | import android.util.SparseArray; |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 89 | import android.util.SparseBooleanArray; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 90 | import android.util.SparseIntArray; |
| 91 | import android.util.SparseLongArray; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 92 | import android.util.TypedValue; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 93 | import android.util.Xml; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 94 | import android.view.IWindowManager; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 95 | |
| 96 | import com.android.internal.annotations.GuardedBy; |
| 97 | import com.android.internal.annotations.VisibleForTesting; |
Mehdi Alizadeh | a20781c | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 98 | import com.android.internal.logging.MetricsLogger; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 99 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 100 | import com.android.internal.util.DumpUtils; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 101 | import com.android.internal.util.FastXmlSerializer; |
| 102 | import com.android.internal.util.Preconditions; |
Makoto Onuki | 49392d3 | 2018-04-11 13:51:02 -0700 | [diff] [blame] | 103 | import com.android.internal.util.StatLogger; |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 104 | import com.android.server.LocalServices; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 105 | import com.android.server.SystemService; |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 106 | import com.android.server.pm.ShortcutUser.PackageWithUser; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 107 | |
| 108 | import libcore.io.IoUtils; |
| 109 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 110 | import org.json.JSONArray; |
| 111 | import org.json.JSONException; |
| 112 | import org.json.JSONObject; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 113 | import org.xmlpull.v1.XmlPullParser; |
| 114 | import org.xmlpull.v1.XmlPullParserException; |
| 115 | import org.xmlpull.v1.XmlSerializer; |
| 116 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 117 | import java.io.BufferedInputStream; |
| 118 | import java.io.BufferedOutputStream; |
| 119 | import java.io.ByteArrayInputStream; |
| 120 | import java.io.ByteArrayOutputStream; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 121 | import java.io.File; |
| 122 | import java.io.FileDescriptor; |
| 123 | import java.io.FileInputStream; |
| 124 | import java.io.FileNotFoundException; |
| 125 | import java.io.FileOutputStream; |
| 126 | import java.io.IOException; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 127 | import java.io.InputStream; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 128 | import java.io.OutputStream; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 129 | import java.io.PrintWriter; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 130 | import java.lang.annotation.Retention; |
| 131 | import java.lang.annotation.RetentionPolicy; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 132 | import java.net.URISyntaxException; |
| 133 | import java.nio.charset.StandardCharsets; |
| 134 | import java.util.ArrayList; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 135 | import java.util.Collections; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 136 | import java.util.List; |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 137 | import java.util.Objects; |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 138 | import java.util.concurrent.atomic.AtomicBoolean; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 139 | import java.util.function.Consumer; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 140 | import java.util.function.Predicate; |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 141 | import java.util.regex.Pattern; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * TODO: |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 145 | * - getIconMaxWidth()/getIconMaxHeight() should use xdpi and ydpi. |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 146 | * -> But TypedValue.applyDimension() doesn't differentiate x and y..? |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 147 | * |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 148 | * - Detect when already registered instances are passed to APIs again, which might break |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 149 | * internal bitmap handling. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 150 | */ |
| 151 | public class ShortcutService extends IShortcutService.Stub { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 152 | static final String TAG = "ShortcutService"; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 153 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 154 | static final boolean DEBUG = false; // STOPSHIP if true |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 155 | static final boolean DEBUG_LOAD = false; // STOPSHIP if true |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 156 | static final boolean DEBUG_PROCSTATE = false; // STOPSHIP if true |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 157 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 158 | @VisibleForTesting |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 159 | static final long DEFAULT_RESET_INTERVAL_SEC = 24 * 60 * 60; // 1 day |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 160 | |
| 161 | @VisibleForTesting |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 162 | static final int DEFAULT_MAX_UPDATES_PER_INTERVAL = 10; |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 163 | |
| 164 | @VisibleForTesting |
Mehdi Alizadeh | bebfc0b3 | 2019-04-25 17:05:52 -0700 | [diff] [blame] | 165 | static final int DEFAULT_MAX_SHORTCUTS_PER_APP = 10; |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 166 | |
| 167 | @VisibleForTesting |
| 168 | static final int DEFAULT_MAX_ICON_DIMENSION_DP = 96; |
| 169 | |
| 170 | @VisibleForTesting |
| 171 | static final int DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP = 48; |
| 172 | |
| 173 | @VisibleForTesting |
| 174 | static final String DEFAULT_ICON_PERSIST_FORMAT = CompressFormat.PNG.name(); |
| 175 | |
| 176 | @VisibleForTesting |
| 177 | static final int DEFAULT_ICON_PERSIST_QUALITY = 100; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 178 | |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 179 | @VisibleForTesting |
| 180 | static final int DEFAULT_SAVE_DELAY_MS = 3000; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 181 | |
| 182 | @VisibleForTesting |
| 183 | static final String FILENAME_BASE_STATE = "shortcut_service.xml"; |
| 184 | |
| 185 | @VisibleForTesting |
| 186 | static final String DIRECTORY_PER_USER = "shortcut_service"; |
| 187 | |
| 188 | @VisibleForTesting |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 189 | static final String DIRECTORY_DUMP = "shortcut_dump"; |
| 190 | |
| 191 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 192 | static final String FILENAME_USER_PACKAGES = "shortcuts.xml"; |
| 193 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 194 | static final String DIRECTORY_BITMAPS = "bitmaps"; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 195 | |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 196 | private static final String TAG_ROOT = "root"; |
| 197 | private static final String TAG_LAST_RESET_TIME = "last_reset_time"; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 198 | |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 199 | private static final String ATTR_VALUE = "value"; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 200 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 201 | private static final String LAUNCHER_INTENT_CATEGORY = Intent.CATEGORY_LAUNCHER; |
| 202 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 203 | private static final String KEY_SHORTCUT = "shortcut"; |
| 204 | private static final String KEY_LOW_RAM = "lowRam"; |
| 205 | private static final String KEY_ICON_SIZE = "iconSize"; |
| 206 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 207 | private static final String DUMMY_MAIN_ACTIVITY = "android.__dummy__"; |
| 208 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 209 | @VisibleForTesting |
| 210 | interface ConfigConstants { |
| 211 | /** |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 212 | * Key name for the save delay, in milliseconds. (int) |
| 213 | */ |
| 214 | String KEY_SAVE_DELAY_MILLIS = "save_delay_ms"; |
| 215 | |
| 216 | /** |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 217 | * Key name for the throttling reset interval, in seconds. (long) |
| 218 | */ |
| 219 | String KEY_RESET_INTERVAL_SEC = "reset_interval_sec"; |
| 220 | |
| 221 | /** |
| 222 | * Key name for the max number of modifying API calls per app for every interval. (int) |
| 223 | */ |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 224 | String KEY_MAX_UPDATES_PER_INTERVAL = "max_updates_per_interval"; |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 225 | |
| 226 | /** |
| 227 | * Key name for the max icon dimensions in DP, for non-low-memory devices. |
| 228 | */ |
| 229 | String KEY_MAX_ICON_DIMENSION_DP = "max_icon_dimension_dp"; |
| 230 | |
| 231 | /** |
| 232 | * Key name for the max icon dimensions in DP, for low-memory devices. |
| 233 | */ |
| 234 | String KEY_MAX_ICON_DIMENSION_DP_LOWRAM = "max_icon_dimension_dp_lowram"; |
| 235 | |
| 236 | /** |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 237 | * Key name for the max dynamic shortcuts per activity. (int) |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 238 | */ |
| 239 | String KEY_MAX_SHORTCUTS = "max_shortcuts"; |
| 240 | |
| 241 | /** |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 242 | * Key name for icon compression quality, 0-100. |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 243 | */ |
| 244 | String KEY_ICON_QUALITY = "icon_quality"; |
| 245 | |
| 246 | /** |
| 247 | * Key name for icon compression format: "PNG", "JPEG" or "WEBP" |
| 248 | */ |
| 249 | String KEY_ICON_FORMAT = "icon_format"; |
| 250 | } |
| 251 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 252 | final Context mContext; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 253 | |
| 254 | private final Object mLock = new Object(); |
| 255 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 256 | private static List<ResolveInfo> EMPTY_RESOLVE_INFO = new ArrayList<>(0); |
| 257 | |
Mark Renouf | e065f7c | 2016-11-01 11:48:24 -0400 | [diff] [blame] | 258 | // Temporarily reverted to anonymous inner class form due to: b/32554459 |
| 259 | private static Predicate<ResolveInfo> ACTIVITY_NOT_EXPORTED = new Predicate<ResolveInfo>() { |
| 260 | public boolean test(ResolveInfo ri) { |
| 261 | return !ri.activityInfo.exported; |
| 262 | } |
| 263 | }; |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 264 | |
Mark Renouf | e065f7c | 2016-11-01 11:48:24 -0400 | [diff] [blame] | 265 | // Temporarily reverted to anonymous inner class form due to: b/32554459 |
| 266 | private static Predicate<PackageInfo> PACKAGE_NOT_INSTALLED = new Predicate<PackageInfo>() { |
| 267 | public boolean test(PackageInfo pi) { |
| 268 | return !isInstalled(pi); |
| 269 | } |
| 270 | }; |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 271 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 272 | private final Handler mHandler; |
| 273 | |
| 274 | @GuardedBy("mLock") |
| 275 | private final ArrayList<ShortcutChangeListener> mListeners = new ArrayList<>(1); |
| 276 | |
| 277 | @GuardedBy("mLock") |
| 278 | private long mRawLastResetTime; |
| 279 | |
| 280 | /** |
Makoto Onuki | 3f4b1ca | 2016-03-11 13:44:32 -0800 | [diff] [blame] | 281 | * User ID -> UserShortcuts |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 282 | */ |
| 283 | @GuardedBy("mLock") |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 284 | private final SparseArray<ShortcutUser> mUsers = new SparseArray<>(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 285 | |
| 286 | /** |
Makoto Onuki | 708703b | 2017-12-11 16:38:11 -0800 | [diff] [blame] | 287 | * User ID -> ShortcutNonPersistentUser |
| 288 | */ |
| 289 | @GuardedBy("mLock") |
| 290 | private final SparseArray<ShortcutNonPersistentUser> mShortcutNonPersistentUsers = |
| 291 | new SparseArray<>(); |
| 292 | |
| 293 | /** |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 294 | * Max number of dynamic + manifest shortcuts that each application can have at a time. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 295 | */ |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 296 | private int mMaxShortcuts; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 297 | |
| 298 | /** |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 299 | * Max number of updating API calls that each application can make during the interval. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 300 | */ |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 301 | int mMaxUpdatesPerInterval; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * Actual throttling-reset interval. By default it's a day. |
| 305 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 306 | private long mResetInterval; |
| 307 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 308 | /** |
| 309 | * Icon max width/height in pixels. |
| 310 | */ |
| 311 | private int mMaxIconDimension; |
| 312 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 313 | private CompressFormat mIconPersistFormat; |
| 314 | private int mIconPersistQuality; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 315 | |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 316 | private int mSaveDelayMillis; |
| 317 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 318 | private final IPackageManager mIPackageManager; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 319 | private final PackageManagerInternal mPackageManagerInternal; |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 320 | private final UserManagerInternal mUserManagerInternal; |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 321 | private final UsageStatsManagerInternal mUsageStatsManagerInternal; |
Makoto Onuki | 33525d2 | 2016-08-03 15:45:24 -0700 | [diff] [blame] | 322 | private final ActivityManagerInternal mActivityManagerInternal; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 323 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 324 | private final ShortcutRequestPinProcessor mShortcutRequestPinProcessor; |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 325 | private final ShortcutBitmapSaver mShortcutBitmapSaver; |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 326 | private final ShortcutDumpFiles mShortcutDumpFiles; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 327 | |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 328 | @GuardedBy("mLock") |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 329 | final SparseIntArray mUidState = new SparseIntArray(); |
| 330 | |
| 331 | @GuardedBy("mLock") |
| 332 | final SparseLongArray mUidLastForegroundElapsedTime = new SparseLongArray(); |
| 333 | |
| 334 | @GuardedBy("mLock") |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 335 | private List<Integer> mDirtyUserIds = new ArrayList<>(); |
| 336 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 337 | private final AtomicBoolean mBootCompleted = new AtomicBoolean(); |
| 338 | |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 339 | private static final int PACKAGE_MATCH_FLAGS = |
| 340 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 341 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE |
| 342 | | PackageManager.MATCH_UNINSTALLED_PACKAGES; |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 343 | |
Makoto Onuki | e63b04a | 2017-12-11 14:47:19 -0800 | [diff] [blame] | 344 | /** |
| 345 | * Note we use a fine-grained lock for {@link #mUnlockedUsers} due to b/64303666. |
| 346 | */ |
| 347 | @GuardedBy("mUnlockedUsers") |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 348 | final SparseBooleanArray mUnlockedUsers = new SparseBooleanArray(); |
| 349 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 350 | // Stats |
| 351 | @VisibleForTesting |
| 352 | interface Stats { |
| 353 | int GET_DEFAULT_HOME = 0; |
| 354 | int GET_PACKAGE_INFO = 1; |
| 355 | int GET_PACKAGE_INFO_WITH_SIG = 2; |
| 356 | int GET_APPLICATION_INFO = 3; |
| 357 | int LAUNCHER_PERMISSION_CHECK = 4; |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 358 | int CLEANUP_DANGLING_BITMAPS = 5; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 359 | int GET_ACTIVITY_WITH_METADATA = 6; |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 360 | int GET_INSTALLED_PACKAGES = 7; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 361 | int CHECK_PACKAGE_CHANGES = 8; |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 362 | int GET_APPLICATION_RESOURCES = 9; |
| 363 | int RESOURCE_NAME_LOOKUP = 10; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 364 | int GET_LAUNCHER_ACTIVITY = 11; |
| 365 | int CHECK_LAUNCHER_ACTIVITY = 12; |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 366 | int IS_ACTIVITY_ENABLED = 13; |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 367 | int PACKAGE_UPDATE_CHECK = 14; |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 368 | int ASYNC_PRELOAD_USER_DELAY = 15; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 369 | int GET_DEFAULT_LAUNCHER = 16; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 370 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 371 | int COUNT = GET_DEFAULT_LAUNCHER + 1; |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 374 | private final StatLogger mStatLogger = new StatLogger(new String[] { |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 375 | "getHomeActivities()", |
| 376 | "Launcher permission check", |
| 377 | "getPackageInfo()", |
| 378 | "getPackageInfo(SIG)", |
| 379 | "getApplicationInfo", |
| 380 | "cleanupDanglingBitmaps", |
| 381 | "getActivity+metadata", |
| 382 | "getInstalledPackages", |
| 383 | "checkPackageChanges", |
| 384 | "getApplicationResources", |
| 385 | "resourceNameLookup", |
| 386 | "getLauncherActivity", |
| 387 | "checkLauncherActivity", |
| 388 | "isActivityEnabled", |
| 389 | "packageUpdateCheck", |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 390 | "asyncPreloadUserDelay", |
| 391 | "getDefaultLauncher()" |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 392 | }); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 393 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 394 | private static final int PROCESS_STATE_FOREGROUND_THRESHOLD = |
Dianne Hackborn | 10fc4fd | 2017-12-19 17:23:13 -0800 | [diff] [blame] | 395 | ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 396 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 397 | static final int OPERATION_SET = 0; |
| 398 | static final int OPERATION_ADD = 1; |
| 399 | static final int OPERATION_UPDATE = 2; |
| 400 | |
| 401 | /** @hide */ |
| 402 | @IntDef(value = { |
| 403 | OPERATION_SET, |
| 404 | OPERATION_ADD, |
| 405 | OPERATION_UPDATE |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 406 | }) |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 407 | @Retention(RetentionPolicy.SOURCE) |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 408 | @interface ShortcutOperation { |
| 409 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 410 | |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 411 | @GuardedBy("mLock") |
| 412 | private int mWtfCount = 0; |
| 413 | |
| 414 | @GuardedBy("mLock") |
| 415 | private Exception mLastWtfStacktrace; |
| 416 | |
Mehdi Alizadeh | a20781c | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 417 | @GuardedBy("mLock") |
| 418 | private final MetricsLogger mMetricsLogger = new MetricsLogger(); |
| 419 | |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 420 | static class InvalidFileFormatException extends Exception { |
| 421 | public InvalidFileFormatException(String message, Throwable cause) { |
| 422 | super(message, cause); |
| 423 | } |
| 424 | } |
| 425 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 426 | public ShortcutService(Context context) { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 427 | this(context, BackgroundThread.get().getLooper(), /*onyForPackgeManagerApis*/ false); |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | @VisibleForTesting |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 431 | ShortcutService(Context context, Looper looper, boolean onlyForPackageManagerApis) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 432 | mContext = Objects.requireNonNull(context); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 433 | LocalServices.addService(ShortcutServiceInternal.class, new LocalService()); |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 434 | mHandler = new Handler(looper); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 435 | mIPackageManager = AppGlobals.getPackageManager(); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 436 | mPackageManagerInternal = Objects.requireNonNull( |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 437 | LocalServices.getService(PackageManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 438 | mUserManagerInternal = Objects.requireNonNull( |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 439 | LocalServices.getService(UserManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 440 | mUsageStatsManagerInternal = Objects.requireNonNull( |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 441 | LocalServices.getService(UsageStatsManagerInternal.class)); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 442 | mActivityManagerInternal = Objects.requireNonNull( |
Makoto Onuki | 33525d2 | 2016-08-03 15:45:24 -0700 | [diff] [blame] | 443 | LocalServices.getService(ActivityManagerInternal.class)); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 444 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 445 | mShortcutRequestPinProcessor = new ShortcutRequestPinProcessor(this, mLock); |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 446 | mShortcutBitmapSaver = new ShortcutBitmapSaver(this); |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 447 | mShortcutDumpFiles = new ShortcutDumpFiles(this); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 448 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 449 | if (onlyForPackageManagerApis) { |
| 450 | return; // Don't do anything further. For unit tests only. |
| 451 | } |
| 452 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 453 | // Register receivers. |
| 454 | |
| 455 | // We need to set a priority, so let's just not use PackageMonitor for now. |
| 456 | // TODO Refactor PackageMonitor to support priorities. |
| 457 | final IntentFilter packageFilter = new IntentFilter(); |
| 458 | packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
| 459 | packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 460 | packageFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 461 | packageFilter.addAction(Intent.ACTION_PACKAGE_DATA_CLEARED); |
| 462 | packageFilter.addDataScheme("package"); |
| 463 | packageFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 464 | mContext.registerReceiverAsUser(mPackageMonitor, UserHandle.ALL, |
| 465 | packageFilter, null, mHandler); |
| 466 | |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 467 | final IntentFilter preferedActivityFilter = new IntentFilter(); |
| 468 | preferedActivityFilter.addAction(Intent.ACTION_PREFERRED_ACTIVITY_CHANGED); |
| 469 | preferedActivityFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 470 | mContext.registerReceiverAsUser(mPackageMonitor, UserHandle.ALL, |
| 471 | preferedActivityFilter, null, mHandler); |
| 472 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 473 | final IntentFilter localeFilter = new IntentFilter(); |
| 474 | localeFilter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 475 | localeFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
| 476 | mContext.registerReceiverAsUser(mReceiver, UserHandle.ALL, |
| 477 | localeFilter, null, mHandler); |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 478 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 479 | injectRegisterUidObserver(mUidObserver, ActivityManager.UID_OBSERVER_PROCSTATE |
| 480 | | ActivityManager.UID_OBSERVER_GONE); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 483 | long getStatStartTime() { |
| 484 | return mStatLogger.getTime(); |
| 485 | } |
| 486 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 487 | void logDurationStat(int statId, long start) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 488 | mStatLogger.logDurationStat(statId, start); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 491 | public String injectGetLocaleTagsForUser(@UserIdInt int userId) { |
| 492 | // TODO This should get the per-user locale. b/30123329 b/30119489 |
| 493 | return LocaleList.getDefault().toLanguageTags(); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 497 | @Override |
Hui Yu | 2696932 | 2019-08-21 14:56:35 -0700 | [diff] [blame] | 498 | public void onUidStateChanged(int uid, int procState, long procStateSeq, int capability) { |
Makoto Onuki | dc3f6d1 | 2017-09-22 17:22:50 -0700 | [diff] [blame] | 499 | injectPostToHandler(() -> handleOnUidStateChanged(uid, procState)); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 502 | @Override |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 503 | public void onUidGone(int uid, boolean disabled) { |
Makoto Onuki | dc3f6d1 | 2017-09-22 17:22:50 -0700 | [diff] [blame] | 504 | injectPostToHandler(() -> |
| 505 | handleOnUidStateChanged(uid, ActivityManager.PROCESS_STATE_NONEXISTENT)); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 506 | } |
| 507 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 508 | @Override |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 509 | public void onUidActive(int uid) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 510 | } |
| 511 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 512 | @Override |
Dianne Hackborn | 3e99f65 | 2017-07-05 16:33:56 -0700 | [diff] [blame] | 513 | public void onUidIdle(int uid, boolean disabled) { |
| 514 | } |
| 515 | |
| 516 | @Override public void onUidCachedChanged(int uid, boolean cached) { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 517 | } |
| 518 | }; |
| 519 | |
| 520 | void handleOnUidStateChanged(int uid, int procState) { |
| 521 | if (DEBUG_PROCSTATE) { |
| 522 | Slog.d(TAG, "onUidStateChanged: uid=" + uid + " state=" + procState); |
| 523 | } |
| 524 | synchronized (mLock) { |
| 525 | mUidState.put(uid, procState); |
| 526 | |
| 527 | // We need to keep track of last time an app comes to foreground. |
| 528 | // See ShortcutPackage.getApiCallCount() for how it's used. |
| 529 | // It doesn't have to be persisted, but it needs to be the elapsed time. |
| 530 | if (isProcessStateForeground(procState)) { |
| 531 | mUidLastForegroundElapsedTime.put(uid, injectElapsedRealtime()); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | private boolean isProcessStateForeground(int processState) { |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 537 | return processState <= PROCESS_STATE_FOREGROUND_THRESHOLD; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 540 | @GuardedBy("mLock") |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 541 | boolean isUidForegroundLocked(int uid) { |
| 542 | if (uid == Process.SYSTEM_UID) { |
| 543 | // IUidObserver doesn't report the state of SYSTEM, but it always has bound services, |
| 544 | // so it's foreground anyway. |
| 545 | return true; |
| 546 | } |
Makoto Onuki | 33525d2 | 2016-08-03 15:45:24 -0700 | [diff] [blame] | 547 | // First, check with the local cache. |
| 548 | if (isProcessStateForeground(mUidState.get(uid, ActivityManager.MAX_PROCESS_STATE))) { |
| 549 | return true; |
| 550 | } |
| 551 | // If the cache says background, reach out to AM. Since it'll internally need to hold |
| 552 | // the AM lock, we use it as a last resort. |
| 553 | return isProcessStateForeground(mActivityManagerInternal.getUidProcessState(uid)); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 554 | } |
| 555 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 556 | @GuardedBy("mLock") |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 557 | long getUidLastForegroundElapsedTimeLocked(int uid) { |
| 558 | return mUidLastForegroundElapsedTime.get(uid); |
| 559 | } |
| 560 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 561 | /** |
| 562 | * System service lifecycle. |
| 563 | */ |
| 564 | public static final class Lifecycle extends SystemService { |
| 565 | final ShortcutService mService; |
| 566 | |
| 567 | public Lifecycle(Context context) { |
| 568 | super(context); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 569 | if (DEBUG) { |
| 570 | Binder.LOG_RUNTIME_EXCEPTION = true; |
| 571 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 572 | mService = new ShortcutService(context); |
| 573 | } |
| 574 | |
| 575 | @Override |
| 576 | public void onStart() { |
| 577 | publishBinderService(Context.SHORTCUT_SERVICE, mService); |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public void onBootPhase(int phase) { |
| 582 | mService.onBootPhase(phase); |
| 583 | } |
| 584 | |
| 585 | @Override |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 586 | public void onStopUser(int userHandle) { |
| 587 | mService.handleStopUser(userHandle); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | @Override |
Makoto Onuki | f3a572b | 2016-03-10 12:28:38 -0800 | [diff] [blame] | 591 | public void onUnlockUser(int userId) { |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 592 | mService.handleUnlockUser(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
| 596 | /** lifecycle event */ |
| 597 | void onBootPhase(int phase) { |
| 598 | if (DEBUG) { |
| 599 | Slog.d(TAG, "onBootPhase: " + phase); |
| 600 | } |
| 601 | switch (phase) { |
| 602 | case SystemService.PHASE_LOCK_SETTINGS_READY: |
| 603 | initialize(); |
| 604 | break; |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 605 | case SystemService.PHASE_BOOT_COMPLETED: |
| 606 | mBootCompleted.set(true); |
| 607 | break; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | /** lifecycle event */ |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 612 | void handleUnlockUser(int userId) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 613 | if (DEBUG) { |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 614 | Slog.d(TAG, "handleUnlockUser: user=" + userId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 615 | } |
Makoto Onuki | e63b04a | 2017-12-11 14:47:19 -0800 | [diff] [blame] | 616 | synchronized (mUnlockedUsers) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 617 | mUnlockedUsers.put(userId, true); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 618 | } |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 619 | |
| 620 | // Preload the user data. |
| 621 | // Note, we don't use mHandler here but instead just start a new thread. |
| 622 | // This is because mHandler (which uses com.android.internal.os.BackgroundThread) is very |
| 623 | // busy at this point and this could take hundreds of milliseconds, which would be too |
| 624 | // late since the launcher would already have started. |
| 625 | // So we just create a new thread. This code runs rarely, so we don't use a thread pool |
| 626 | // or anything. |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 627 | final long start = getStatStartTime(); |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 628 | injectRunOnNewThread(() -> { |
| 629 | synchronized (mLock) { |
| 630 | logDurationStat(Stats.ASYNC_PRELOAD_USER_DELAY, start); |
| 631 | getUserShortcutsLocked(userId); |
| 632 | } |
| 633 | }); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | /** lifecycle event */ |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 637 | void handleStopUser(int userId) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 638 | if (DEBUG) { |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 639 | Slog.d(TAG, "handleStopUser: user=" + userId); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 640 | } |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 641 | synchronized (mLock) { |
| 642 | unloadUserLocked(userId); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 643 | |
Makoto Onuki | e63b04a | 2017-12-11 14:47:19 -0800 | [diff] [blame] | 644 | synchronized (mUnlockedUsers) { |
| 645 | mUnlockedUsers.put(userId, false); |
| 646 | } |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 650 | @GuardedBy("mLock") |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 651 | private void unloadUserLocked(int userId) { |
| 652 | if (DEBUG) { |
| 653 | Slog.d(TAG, "unloadUserLocked: user=" + userId); |
| 654 | } |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 655 | // Save all dirty information. |
| 656 | saveDirtyInfo(); |
| 657 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 658 | // Unload |
Makoto Onuki | 3f4b1ca | 2016-03-11 13:44:32 -0800 | [diff] [blame] | 659 | mUsers.delete(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | /** Return the base state file name */ |
| 663 | private AtomicFile getBaseStateFile() { |
| 664 | final File path = new File(injectSystemDataPath(), FILENAME_BASE_STATE); |
| 665 | path.mkdirs(); |
| 666 | return new AtomicFile(path); |
| 667 | } |
| 668 | |
| 669 | /** |
| 670 | * Init the instance. (load the state file, etc) |
| 671 | */ |
| 672 | private void initialize() { |
| 673 | synchronized (mLock) { |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 674 | loadConfigurationLocked(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 675 | loadBaseStateLocked(); |
| 676 | } |
| 677 | } |
| 678 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 679 | /** |
| 680 | * Load the configuration from Settings. |
| 681 | */ |
| 682 | private void loadConfigurationLocked() { |
| 683 | updateConfigurationLocked(injectShortcutManagerConstants()); |
| 684 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 685 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 686 | /** |
| 687 | * Load the configuration from Settings. |
| 688 | */ |
| 689 | @VisibleForTesting |
| 690 | boolean updateConfigurationLocked(String config) { |
| 691 | boolean result = true; |
| 692 | |
| 693 | final KeyValueListParser parser = new KeyValueListParser(','); |
| 694 | try { |
| 695 | parser.setString(config); |
| 696 | } catch (IllegalArgumentException e) { |
| 697 | // Failed to parse the settings string, log this and move on |
| 698 | // with defaults. |
| 699 | Slog.e(TAG, "Bad shortcut manager settings", e); |
| 700 | result = false; |
| 701 | } |
| 702 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 703 | mSaveDelayMillis = Math.max(0, (int) parser.getLong(ConfigConstants.KEY_SAVE_DELAY_MILLIS, |
| 704 | DEFAULT_SAVE_DELAY_MS)); |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 705 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 706 | mResetInterval = Math.max(1, parser.getLong( |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 707 | ConfigConstants.KEY_RESET_INTERVAL_SEC, DEFAULT_RESET_INTERVAL_SEC) |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 708 | * 1000L); |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 709 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 710 | mMaxUpdatesPerInterval = Math.max(0, (int) parser.getLong( |
| 711 | ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL, DEFAULT_MAX_UPDATES_PER_INTERVAL)); |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 712 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 713 | mMaxShortcuts = Math.max(0, (int) parser.getLong( |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 714 | ConfigConstants.KEY_MAX_SHORTCUTS, DEFAULT_MAX_SHORTCUTS_PER_APP)); |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 715 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 716 | final int iconDimensionDp = Math.max(1, injectIsLowRamDevice() |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 717 | ? (int) parser.getLong( |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 718 | ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM, |
| 719 | DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP) |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 720 | : (int) parser.getLong( |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 721 | ConfigConstants.KEY_MAX_ICON_DIMENSION_DP, |
| 722 | DEFAULT_MAX_ICON_DIMENSION_DP)); |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 723 | |
| 724 | mMaxIconDimension = injectDipToPixel(iconDimensionDp); |
| 725 | |
| 726 | mIconPersistFormat = CompressFormat.valueOf( |
| 727 | parser.getString(ConfigConstants.KEY_ICON_FORMAT, DEFAULT_ICON_PERSIST_FORMAT)); |
| 728 | |
| 729 | mIconPersistQuality = (int) parser.getLong( |
| 730 | ConfigConstants.KEY_ICON_QUALITY, |
| 731 | DEFAULT_ICON_PERSIST_QUALITY); |
| 732 | |
| 733 | return result; |
| 734 | } |
| 735 | |
| 736 | @VisibleForTesting |
| 737 | String injectShortcutManagerConstants() { |
| 738 | return android.provider.Settings.Global.getString( |
| 739 | mContext.getContentResolver(), |
| 740 | android.provider.Settings.Global.SHORTCUT_MANAGER_CONSTANTS); |
| 741 | } |
| 742 | |
| 743 | @VisibleForTesting |
| 744 | int injectDipToPixel(int dip) { |
| 745 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, |
| 746 | mContext.getResources().getDisplayMetrics()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 749 | // === Persisting === |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 750 | |
| 751 | @Nullable |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 752 | static String parseStringAttribute(XmlPullParser parser, String attribute) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 753 | return parser.getAttributeValue(null, attribute); |
| 754 | } |
| 755 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 756 | static boolean parseBooleanAttribute(XmlPullParser parser, String attribute) { |
| 757 | return parseLongAttribute(parser, attribute) == 1; |
| 758 | } |
| 759 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 760 | static boolean parseBooleanAttribute(XmlPullParser parser, String attribute, boolean def) { |
| 761 | return parseLongAttribute(parser, attribute, (def ? 1 : 0)) == 1; |
| 762 | } |
| 763 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 764 | static int parseIntAttribute(XmlPullParser parser, String attribute) { |
| 765 | return (int) parseLongAttribute(parser, attribute); |
| 766 | } |
| 767 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 768 | static int parseIntAttribute(XmlPullParser parser, String attribute, int def) { |
| 769 | return (int) parseLongAttribute(parser, attribute, def); |
| 770 | } |
| 771 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 772 | static long parseLongAttribute(XmlPullParser parser, String attribute) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 773 | return parseLongAttribute(parser, attribute, 0); |
| 774 | } |
| 775 | |
| 776 | static long parseLongAttribute(XmlPullParser parser, String attribute, long def) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 777 | final String value = parseStringAttribute(parser, attribute); |
| 778 | if (TextUtils.isEmpty(value)) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 779 | return def; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 780 | } |
| 781 | try { |
| 782 | return Long.parseLong(value); |
| 783 | } catch (NumberFormatException e) { |
| 784 | Slog.e(TAG, "Error parsing long " + value); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 785 | return def; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | |
| 789 | @Nullable |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 790 | static ComponentName parseComponentNameAttribute(XmlPullParser parser, String attribute) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 791 | final String value = parseStringAttribute(parser, attribute); |
| 792 | if (TextUtils.isEmpty(value)) { |
| 793 | return null; |
| 794 | } |
| 795 | return ComponentName.unflattenFromString(value); |
| 796 | } |
| 797 | |
| 798 | @Nullable |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 799 | static Intent parseIntentAttributeNoDefault(XmlPullParser parser, String attribute) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 800 | final String value = parseStringAttribute(parser, attribute); |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 801 | Intent parsed = null; |
| 802 | if (!TextUtils.isEmpty(value)) { |
| 803 | try { |
| 804 | parsed = Intent.parseUri(value, /* flags =*/ 0); |
| 805 | } catch (URISyntaxException e) { |
| 806 | Slog.e(TAG, "Error parsing intent", e); |
| 807 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 808 | } |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 809 | return parsed; |
| 810 | } |
| 811 | |
| 812 | @Nullable |
| 813 | static Intent parseIntentAttribute(XmlPullParser parser, String attribute) { |
| 814 | Intent parsed = parseIntentAttributeNoDefault(parser, attribute); |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 815 | if (parsed == null) { |
| 816 | // Default intent. |
| 817 | parsed = new Intent(Intent.ACTION_VIEW); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 818 | } |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 819 | return parsed; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 820 | } |
| 821 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 822 | static void writeTagValue(XmlSerializer out, String tag, String value) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 823 | if (TextUtils.isEmpty(value)) return; |
| 824 | |
| 825 | out.startTag(null, tag); |
| 826 | out.attribute(null, ATTR_VALUE, value); |
| 827 | out.endTag(null, tag); |
| 828 | } |
| 829 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 830 | static void writeTagValue(XmlSerializer out, String tag, long value) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 831 | writeTagValue(out, tag, Long.toString(value)); |
| 832 | } |
| 833 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 834 | static void writeTagValue(XmlSerializer out, String tag, ComponentName name) throws IOException { |
| 835 | if (name == null) return; |
| 836 | writeTagValue(out, tag, name.flattenToString()); |
| 837 | } |
| 838 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 839 | static void writeTagExtra(XmlSerializer out, String tag, PersistableBundle bundle) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 840 | throws IOException, XmlPullParserException { |
| 841 | if (bundle == null) return; |
| 842 | |
| 843 | out.startTag(null, tag); |
| 844 | bundle.saveToXml(out); |
| 845 | out.endTag(null, tag); |
| 846 | } |
| 847 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 848 | static void writeAttr(XmlSerializer out, String name, CharSequence value) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 849 | if (TextUtils.isEmpty(value)) return; |
| 850 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 851 | out.attribute(null, name, value.toString()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 852 | } |
| 853 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 854 | static void writeAttr(XmlSerializer out, String name, long value) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 855 | writeAttr(out, name, String.valueOf(value)); |
| 856 | } |
| 857 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 858 | static void writeAttr(XmlSerializer out, String name, boolean value) throws IOException { |
| 859 | if (value) { |
| 860 | writeAttr(out, name, "1"); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 861 | } else { |
| 862 | writeAttr(out, name, "0"); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 866 | static void writeAttr(XmlSerializer out, String name, ComponentName comp) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 867 | if (comp == null) return; |
| 868 | writeAttr(out, name, comp.flattenToString()); |
| 869 | } |
| 870 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 871 | static void writeAttr(XmlSerializer out, String name, Intent intent) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 872 | if (intent == null) return; |
| 873 | |
| 874 | writeAttr(out, name, intent.toUri(/* flags =*/ 0)); |
| 875 | } |
| 876 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 877 | @GuardedBy("mLock") |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 878 | @VisibleForTesting |
| 879 | void saveBaseStateLocked() { |
| 880 | final AtomicFile file = getBaseStateFile(); |
| 881 | if (DEBUG) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 882 | Slog.d(TAG, "Saving to " + file.getBaseFile()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | FileOutputStream outs = null; |
| 886 | try { |
| 887 | outs = file.startWrite(); |
| 888 | |
| 889 | // Write to XML |
| 890 | XmlSerializer out = new FastXmlSerializer(); |
| 891 | out.setOutput(outs, StandardCharsets.UTF_8.name()); |
| 892 | out.startDocument(null, true); |
| 893 | out.startTag(null, TAG_ROOT); |
| 894 | |
| 895 | // Body. |
| 896 | writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime); |
| 897 | |
| 898 | // Epilogue. |
| 899 | out.endTag(null, TAG_ROOT); |
| 900 | out.endDocument(); |
| 901 | |
| 902 | // Close. |
| 903 | file.finishWrite(outs); |
| 904 | } catch (IOException e) { |
| 905 | Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); |
| 906 | file.failWrite(outs); |
| 907 | } |
| 908 | } |
| 909 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 910 | @GuardedBy("mLock") |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 911 | private void loadBaseStateLocked() { |
| 912 | mRawLastResetTime = 0; |
| 913 | |
| 914 | final AtomicFile file = getBaseStateFile(); |
| 915 | if (DEBUG) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 916 | Slog.d(TAG, "Loading from " + file.getBaseFile()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 917 | } |
| 918 | try (FileInputStream in = file.openRead()) { |
| 919 | XmlPullParser parser = Xml.newPullParser(); |
| 920 | parser.setInput(in, StandardCharsets.UTF_8.name()); |
| 921 | |
| 922 | int type; |
| 923 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 924 | if (type != XmlPullParser.START_TAG) { |
| 925 | continue; |
| 926 | } |
| 927 | final int depth = parser.getDepth(); |
| 928 | // Check the root tag |
| 929 | final String tag = parser.getName(); |
| 930 | if (depth == 1) { |
| 931 | if (!TAG_ROOT.equals(tag)) { |
| 932 | Slog.e(TAG, "Invalid root tag: " + tag); |
| 933 | return; |
| 934 | } |
| 935 | continue; |
| 936 | } |
| 937 | // Assume depth == 2 |
| 938 | switch (tag) { |
| 939 | case TAG_LAST_RESET_TIME: |
| 940 | mRawLastResetTime = parseLongAttribute(parser, ATTR_VALUE); |
| 941 | break; |
| 942 | default: |
| 943 | Slog.e(TAG, "Invalid tag: " + tag); |
| 944 | break; |
| 945 | } |
| 946 | } |
| 947 | } catch (FileNotFoundException e) { |
| 948 | // Use the default |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 949 | } catch (IOException | XmlPullParserException e) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 950 | Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); |
| 951 | |
| 952 | mRawLastResetTime = 0; |
| 953 | } |
| 954 | // Adjust the last reset time. |
| 955 | getLastResetTimeLocked(); |
| 956 | } |
| 957 | |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 958 | @VisibleForTesting |
| 959 | final File getUserFile(@UserIdInt int userId) { |
| 960 | return new File(injectUserDataPath(userId), FILENAME_USER_PACKAGES); |
| 961 | } |
| 962 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 963 | @GuardedBy("mLock") |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 964 | private void saveUserLocked(@UserIdInt int userId) { |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 965 | final File path = getUserFile(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 966 | if (DEBUG) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 967 | Slog.d(TAG, "Saving to " + path); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 968 | } |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 969 | |
| 970 | mShortcutBitmapSaver.waitForAllSavesLocked(); |
| 971 | |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 972 | path.getParentFile().mkdirs(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 973 | final AtomicFile file = new AtomicFile(path); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 974 | FileOutputStream os = null; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 975 | try { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 976 | os = file.startWrite(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 977 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 978 | saveUserInternalLocked(userId, os, /* forBackup= */ false); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 979 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 980 | file.finishWrite(os); |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 981 | |
| 982 | // Remove all dangling bitmap files. |
| 983 | cleanupDanglingBitmapDirectoriesLocked(userId); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 984 | } catch (XmlPullParserException | IOException e) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 985 | Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 986 | file.failWrite(os); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 987 | } |
Mehdi Alizadeh | a20781c | 2019-04-25 14:52:02 -0700 | [diff] [blame] | 988 | |
| 989 | getUserShortcutsLocked(userId).logSharingShortcutStats(mMetricsLogger); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 990 | } |
| 991 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 992 | @GuardedBy("mLock") |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 993 | private void saveUserInternalLocked(@UserIdInt int userId, OutputStream os, |
| 994 | boolean forBackup) throws IOException, XmlPullParserException { |
| 995 | |
| 996 | final BufferedOutputStream bos = new BufferedOutputStream(os); |
| 997 | |
| 998 | // Write to XML |
| 999 | XmlSerializer out = new FastXmlSerializer(); |
| 1000 | out.setOutput(bos, StandardCharsets.UTF_8.name()); |
| 1001 | out.startDocument(null, true); |
| 1002 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1003 | getUserShortcutsLocked(userId).saveToXml(out, forBackup); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1004 | |
| 1005 | out.endDocument(); |
| 1006 | |
| 1007 | bos.flush(); |
| 1008 | os.flush(); |
| 1009 | } |
| 1010 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 1011 | static IOException throwForInvalidTag(int depth, String tag) throws IOException { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1012 | throw new IOException(String.format("Invalid tag '%s' found at depth %d", tag, depth)); |
| 1013 | } |
| 1014 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1015 | static void warnForInvalidTag(int depth, String tag) throws IOException { |
| 1016 | Slog.w(TAG, String.format("Invalid tag '%s' found at depth %d", tag, depth)); |
| 1017 | } |
| 1018 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1019 | @Nullable |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1020 | private ShortcutUser loadUserLocked(@UserIdInt int userId) { |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 1021 | final File path = getUserFile(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1022 | if (DEBUG) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1023 | Slog.d(TAG, "Loading from " + path); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1024 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1025 | final AtomicFile file = new AtomicFile(path); |
| 1026 | |
| 1027 | final FileInputStream in; |
| 1028 | try { |
| 1029 | in = file.openRead(); |
| 1030 | } catch (FileNotFoundException e) { |
| 1031 | if (DEBUG) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1032 | Slog.d(TAG, "Not found " + path); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1033 | } |
| 1034 | return null; |
| 1035 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1036 | try { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1037 | final ShortcutUser ret = loadUserInternal(userId, in, /* forBackup= */ false); |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 1038 | return ret; |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 1039 | } catch (IOException | XmlPullParserException | InvalidFileFormatException e) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1040 | Slog.e(TAG, "Failed to read file " + file.getBaseFile(), e); |
| 1041 | return null; |
| 1042 | } finally { |
| 1043 | IoUtils.closeQuietly(in); |
| 1044 | } |
| 1045 | } |
| 1046 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1047 | private ShortcutUser loadUserInternal(@UserIdInt int userId, InputStream is, |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 1048 | boolean fromBackup) throws XmlPullParserException, IOException, |
| 1049 | InvalidFileFormatException { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1050 | |
| 1051 | final BufferedInputStream bis = new BufferedInputStream(is); |
| 1052 | |
| 1053 | ShortcutUser ret = null; |
| 1054 | XmlPullParser parser = Xml.newPullParser(); |
| 1055 | parser.setInput(bis, StandardCharsets.UTF_8.name()); |
| 1056 | |
| 1057 | int type; |
| 1058 | while ((type = parser.next()) != XmlPullParser.END_DOCUMENT) { |
| 1059 | if (type != XmlPullParser.START_TAG) { |
| 1060 | continue; |
| 1061 | } |
| 1062 | final int depth = parser.getDepth(); |
| 1063 | |
| 1064 | final String tag = parser.getName(); |
| 1065 | if (DEBUG_LOAD) { |
| 1066 | Slog.d(TAG, String.format("depth=%d type=%d name=%s", |
| 1067 | depth, type, tag)); |
| 1068 | } |
| 1069 | if ((depth == 1) && ShortcutUser.TAG_ROOT.equals(tag)) { |
| 1070 | ret = ShortcutUser.loadFromXml(this, parser, userId, fromBackup); |
| 1071 | continue; |
| 1072 | } |
| 1073 | throwForInvalidTag(depth, tag); |
| 1074 | } |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1078 | private void scheduleSaveBaseState() { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1079 | scheduleSaveInner(UserHandle.USER_NULL); // Special case -- use USER_NULL for base state. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1080 | } |
| 1081 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 1082 | void scheduleSaveUser(@UserIdInt int userId) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1083 | scheduleSaveInner(userId); |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | // In order to re-schedule, we need to reuse the same instance, so keep it in final. |
| 1087 | private final Runnable mSaveDirtyInfoRunner = this::saveDirtyInfo; |
| 1088 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 1089 | private void scheduleSaveInner(@UserIdInt int userId) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1090 | if (DEBUG) { |
| 1091 | Slog.d(TAG, "Scheduling to save for " + userId); |
| 1092 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1093 | synchronized (mLock) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1094 | if (!mDirtyUserIds.contains(userId)) { |
| 1095 | mDirtyUserIds.add(userId); |
| 1096 | } |
| 1097 | } |
| 1098 | // If already scheduled, remove that and re-schedule in N seconds. |
| 1099 | mHandler.removeCallbacks(mSaveDirtyInfoRunner); |
| 1100 | mHandler.postDelayed(mSaveDirtyInfoRunner, mSaveDelayMillis); |
| 1101 | } |
| 1102 | |
| 1103 | @VisibleForTesting |
| 1104 | void saveDirtyInfo() { |
| 1105 | if (DEBUG) { |
| 1106 | Slog.d(TAG, "saveDirtyInfo"); |
| 1107 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1108 | try { |
| 1109 | synchronized (mLock) { |
| 1110 | for (int i = mDirtyUserIds.size() - 1; i >= 0; i--) { |
| 1111 | final int userId = mDirtyUserIds.get(i); |
| 1112 | if (userId == UserHandle.USER_NULL) { // USER_NULL for base state. |
| 1113 | saveBaseStateLocked(); |
| 1114 | } else { |
| 1115 | saveUserLocked(userId); |
| 1116 | } |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1117 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1118 | mDirtyUserIds.clear(); |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1119 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1120 | } catch (Exception e) { |
| 1121 | wtf("Exception in saveDirtyInfo", e); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /** Return the last reset time. */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1126 | @GuardedBy("mLock") |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1127 | long getLastResetTimeLocked() { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1128 | updateTimesLocked(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1129 | return mRawLastResetTime; |
| 1130 | } |
| 1131 | |
| 1132 | /** Return the next reset time. */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1133 | @GuardedBy("mLock") |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1134 | long getNextResetTimeLocked() { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1135 | updateTimesLocked(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1136 | return mRawLastResetTime + mResetInterval; |
| 1137 | } |
| 1138 | |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 1139 | static boolean isClockValid(long time) { |
| 1140 | return time >= 1420070400; // Thu, 01 Jan 2015 00:00:00 GMT |
| 1141 | } |
| 1142 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1143 | /** |
| 1144 | * Update the last reset time. |
| 1145 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1146 | @GuardedBy("mLock") |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1147 | private void updateTimesLocked() { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1148 | |
| 1149 | final long now = injectCurrentTimeMillis(); |
| 1150 | |
| 1151 | final long prevLastResetTime = mRawLastResetTime; |
| 1152 | |
| 1153 | if (mRawLastResetTime == 0) { // first launch. |
| 1154 | // TODO Randomize?? |
| 1155 | mRawLastResetTime = now; |
| 1156 | } else if (now < mRawLastResetTime) { |
| 1157 | // Clock rewound. |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 1158 | if (isClockValid(now)) { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1159 | Slog.w(TAG, "Clock rewound"); |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 1160 | // TODO Randomize?? |
| 1161 | mRawLastResetTime = now; |
| 1162 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1163 | } else { |
Makoto Onuki | aa8b94a | 2016-03-17 13:14:05 -0700 | [diff] [blame] | 1164 | if ((mRawLastResetTime + mResetInterval) <= now) { |
| 1165 | final long offset = mRawLastResetTime % mResetInterval; |
| 1166 | mRawLastResetTime = ((now / mResetInterval) * mResetInterval) + offset; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | if (prevLastResetTime != mRawLastResetTime) { |
| 1170 | scheduleSaveBaseState(); |
| 1171 | } |
| 1172 | } |
| 1173 | |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 1174 | // Requires mLock held, but "Locked" prefix would look weired so we just say "L". |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1175 | protected boolean isUserUnlockedL(@UserIdInt int userId) { |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 1176 | // First, check the local copy. |
Makoto Onuki | e63b04a | 2017-12-11 14:47:19 -0800 | [diff] [blame] | 1177 | synchronized (mUnlockedUsers) { |
| 1178 | if (mUnlockedUsers.get(userId)) { |
| 1179 | return true; |
| 1180 | } |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 1181 | } |
Mehdi Alizadeh | 3277462 | 2018-11-05 17:32:01 -0800 | [diff] [blame] | 1182 | |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 1183 | // If the local copy says the user is locked, check with AM for the actual state, since |
| 1184 | // the user might just have been unlocked. |
| 1185 | // Note we just don't use isUserUnlockingOrUnlocked() here, because it'll return false |
| 1186 | // when the user is STOPPING, which we still want to consider as "unlocked". |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 1187 | return mUserManagerInternal.isUserUnlockingOrUnlocked(userId); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1190 | // Requires mLock held, but "Locked" prefix would look weired so we jsut say "L". |
| 1191 | void throwIfUserLockedL(@UserIdInt int userId) { |
| 1192 | if (!isUserUnlockedL(userId)) { |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 1193 | throw new IllegalStateException("User " + userId + " is locked or not running"); |
| 1194 | } |
| 1195 | } |
| 1196 | |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 1197 | @GuardedBy("mLock") |
| 1198 | @NonNull |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1199 | private boolean isUserLoadedLocked(@UserIdInt int userId) { |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 1200 | return mUsers.get(userId) != null; |
| 1201 | } |
| 1202 | |
Makoto Onuki | dc801c8 | 2019-10-08 13:56:10 -0700 | [diff] [blame] | 1203 | private int mLastLockedUser = -1; |
| 1204 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1205 | /** Return the per-user state. */ |
| 1206 | @GuardedBy("mLock") |
| 1207 | @NonNull |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1208 | ShortcutUser getUserShortcutsLocked(@UserIdInt int userId) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1209 | if (!isUserUnlockedL(userId)) { |
Makoto Onuki | dc801c8 | 2019-10-08 13:56:10 -0700 | [diff] [blame] | 1210 | // Only do wtf once for each user. (until the user is unlocked) |
| 1211 | if (userId != mLastLockedUser) { |
| 1212 | wtf("User still locked"); |
| 1213 | mLastLockedUser = userId; |
| 1214 | } |
| 1215 | } else { |
| 1216 | mLastLockedUser = -1; |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1219 | ShortcutUser userPackages = mUsers.get(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1220 | if (userPackages == null) { |
| 1221 | userPackages = loadUserLocked(userId); |
| 1222 | if (userPackages == null) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1223 | userPackages = new ShortcutUser(this, userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1224 | } |
Makoto Onuki | 3f4b1ca | 2016-03-11 13:44:32 -0800 | [diff] [blame] | 1225 | mUsers.put(userId, userPackages); |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 1226 | |
| 1227 | // Also when a user's data is first accessed, scan all packages. |
| 1228 | checkPackageChanges(userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1229 | } |
| 1230 | return userPackages; |
| 1231 | } |
| 1232 | |
Makoto Onuki | 708703b | 2017-12-11 16:38:11 -0800 | [diff] [blame] | 1233 | /** Return the non-persistent per-user state. */ |
| 1234 | @GuardedBy("mLock") |
| 1235 | @NonNull |
| 1236 | ShortcutNonPersistentUser getNonPersistentUserLocked(@UserIdInt int userId) { |
| 1237 | ShortcutNonPersistentUser ret = mShortcutNonPersistentUsers.get(userId); |
| 1238 | if (ret == null) { |
| 1239 | ret = new ShortcutNonPersistentUser(this, userId); |
| 1240 | mShortcutNonPersistentUsers.put(userId, ret); |
| 1241 | } |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1245 | @GuardedBy("mLock") |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1246 | void forEachLoadedUserLocked(@NonNull Consumer<ShortcutUser> c) { |
| 1247 | for (int i = mUsers.size() - 1; i >= 0; i--) { |
| 1248 | c.accept(mUsers.valueAt(i)); |
| 1249 | } |
| 1250 | } |
| 1251 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 1252 | /** |
| 1253 | * Return the per-user per-package state. If the caller is a publisher, use |
| 1254 | * {@link #getPackageShortcutsForPublisherLocked} instead. |
| 1255 | */ |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1256 | @GuardedBy("mLock") |
| 1257 | @NonNull |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 1258 | ShortcutPackage getPackageShortcutsLocked( |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1259 | @NonNull String packageName, @UserIdInt int userId) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1260 | return getUserShortcutsLocked(userId).getPackageShortcuts(packageName); |
Makoto Onuki | de66737 | 2016-03-15 14:29:20 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 1263 | /** Return the per-user per-package state. Use this when the caller is a publisher. */ |
| 1264 | @GuardedBy("mLock") |
| 1265 | @NonNull |
| 1266 | ShortcutPackage getPackageShortcutsForPublisherLocked( |
| 1267 | @NonNull String packageName, @UserIdInt int userId) { |
| 1268 | final ShortcutPackage ret = getUserShortcutsLocked(userId).getPackageShortcuts(packageName); |
| 1269 | ret.getUser().onCalledByPublisher(packageName); |
| 1270 | return ret; |
| 1271 | } |
| 1272 | |
Makoto Onuki | de66737 | 2016-03-15 14:29:20 -0700 | [diff] [blame] | 1273 | @GuardedBy("mLock") |
| 1274 | @NonNull |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 1275 | ShortcutLauncher getLauncherShortcutsLocked( |
| 1276 | @NonNull String packageName, @UserIdInt int ownerUserId, |
| 1277 | @UserIdInt int launcherUserId) { |
| 1278 | return getUserShortcutsLocked(ownerUserId) |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1279 | .getLauncherShortcuts(packageName, launcherUserId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | // === Caller validation === |
| 1283 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1284 | void removeIconLocked(ShortcutInfo shortcut) { |
| 1285 | mShortcutBitmapSaver.removeIcon(shortcut); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1286 | } |
| 1287 | |
Makoto Onuki | 0033b2a | 2016-04-14 17:19:16 -0700 | [diff] [blame] | 1288 | public void cleanupBitmapsForPackage(@UserIdInt int userId, String packageName) { |
| 1289 | final File packagePath = new File(getUserBitmapFilePath(userId), packageName); |
| 1290 | if (!packagePath.isDirectory()) { |
| 1291 | return; |
| 1292 | } |
| 1293 | if (!(FileUtils.deleteContents(packagePath) && packagePath.delete())) { |
| 1294 | Slog.w(TAG, "Unable to remove directory " + packagePath); |
| 1295 | } |
| 1296 | } |
| 1297 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1298 | /** |
| 1299 | * Remove dangling bitmap files for a user. |
| 1300 | * |
| 1301 | * Note this method must be called with the lock held after calling |
| 1302 | * {@link ShortcutBitmapSaver#waitForAllSavesLocked()} to make sure there's no pending bitmap |
| 1303 | * saves are going on. |
| 1304 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 1305 | @GuardedBy("mLock") |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 1306 | private void cleanupDanglingBitmapDirectoriesLocked(@UserIdInt int userId) { |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 1307 | if (DEBUG) { |
| 1308 | Slog.d(TAG, "cleanupDanglingBitmaps: userId=" + userId); |
| 1309 | } |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 1310 | final long start = getStatStartTime(); |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 1311 | |
Makoto Onuki | 1e17323 | 2016-08-10 10:47:13 -0700 | [diff] [blame] | 1312 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 1313 | |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 1314 | final File bitmapDir = getUserBitmapFilePath(userId); |
| 1315 | final File[] children = bitmapDir.listFiles(); |
| 1316 | if (children == null) { |
| 1317 | return; |
| 1318 | } |
| 1319 | for (File child : children) { |
| 1320 | if (!child.isDirectory()) { |
| 1321 | continue; |
| 1322 | } |
| 1323 | final String packageName = child.getName(); |
| 1324 | if (DEBUG) { |
| 1325 | Slog.d(TAG, "cleanupDanglingBitmaps: Found directory=" + packageName); |
| 1326 | } |
| 1327 | if (!user.hasPackage(packageName)) { |
| 1328 | if (DEBUG) { |
| 1329 | Slog.d(TAG, "Removing dangling bitmap directory: " + packageName); |
| 1330 | } |
| 1331 | cleanupBitmapsForPackage(userId, packageName); |
| 1332 | } else { |
| 1333 | cleanupDanglingBitmapFilesLocked(userId, user, packageName, child); |
| 1334 | } |
| 1335 | } |
| 1336 | logDurationStat(Stats.CLEANUP_DANGLING_BITMAPS, start); |
| 1337 | } |
| 1338 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1339 | /** |
| 1340 | * Remove dangling bitmap files for a package. |
| 1341 | * |
| 1342 | * Note this method must be called with the lock held after calling |
| 1343 | * {@link ShortcutBitmapSaver#waitForAllSavesLocked()} to make sure there's no pending bitmap |
| 1344 | * saves are going on. |
| 1345 | */ |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 1346 | private void cleanupDanglingBitmapFilesLocked(@UserIdInt int userId, @NonNull ShortcutUser user, |
| 1347 | @NonNull String packageName, @NonNull File path) { |
| 1348 | final ArraySet<String> usedFiles = |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 1349 | user.getPackageShortcuts(packageName).getUsedBitmapFiles(); |
Makoto Onuki | 6c1dbd5 | 2016-05-02 15:19:32 -0700 | [diff] [blame] | 1350 | |
| 1351 | for (File child : path.listFiles()) { |
| 1352 | if (!child.isFile()) { |
| 1353 | continue; |
| 1354 | } |
| 1355 | final String name = child.getName(); |
| 1356 | if (!usedFiles.contains(name)) { |
| 1357 | if (DEBUG) { |
| 1358 | Slog.d(TAG, "Removing dangling bitmap file: " + child.getAbsolutePath()); |
| 1359 | } |
| 1360 | child.delete(); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1365 | @VisibleForTesting |
| 1366 | static class FileOutputStreamWithPath extends FileOutputStream { |
| 1367 | private final File mFile; |
| 1368 | |
| 1369 | public FileOutputStreamWithPath(File file) throws FileNotFoundException { |
| 1370 | super(file); |
| 1371 | mFile = file; |
| 1372 | } |
| 1373 | |
| 1374 | public File getFile() { |
| 1375 | return mFile; |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * Build the cached bitmap filename for a shortcut icon. |
| 1381 | * |
| 1382 | * The filename will be based on the ID, except certain characters will be escaped. |
| 1383 | */ |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1384 | FileOutputStreamWithPath openIconFileForWrite(@UserIdInt int userId, ShortcutInfo shortcut) |
| 1385 | throws IOException { |
| 1386 | final File packagePath = new File(getUserBitmapFilePath(userId), |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1387 | shortcut.getPackage()); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1388 | if (!packagePath.isDirectory()) { |
| 1389 | packagePath.mkdirs(); |
| 1390 | if (!packagePath.isDirectory()) { |
| 1391 | throw new IOException("Unable to create directory " + packagePath); |
| 1392 | } |
| 1393 | SELinux.restorecon(packagePath); |
| 1394 | } |
| 1395 | |
| 1396 | final String baseName = String.valueOf(injectCurrentTimeMillis()); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1397 | for (int suffix = 0; ; suffix++) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1398 | final String filename = (suffix == 0 ? baseName : baseName + "_" + suffix) + ".png"; |
| 1399 | final File file = new File(packagePath, filename); |
| 1400 | if (!file.exists()) { |
| 1401 | if (DEBUG) { |
| 1402 | Slog.d(TAG, "Saving icon to " + file.getAbsolutePath()); |
| 1403 | } |
| 1404 | return new FileOutputStreamWithPath(file); |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1409 | void saveIconAndFixUpShortcutLocked(ShortcutInfo shortcut) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1410 | if (shortcut.hasIconFile() || shortcut.hasIconResource()) { |
| 1411 | return; |
| 1412 | } |
| 1413 | |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1414 | final long token = injectClearCallingIdentity(); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1415 | try { |
| 1416 | // Clear icon info on the shortcut. |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1417 | removeIconLocked(shortcut); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1418 | |
| 1419 | final Icon icon = shortcut.getIcon(); |
| 1420 | if (icon == null) { |
| 1421 | return; // has no icon |
| 1422 | } |
Hyunyoung Song | 4703746 | 2017-05-08 16:51:43 -0700 | [diff] [blame] | 1423 | int maxIconDimension = mMaxIconDimension; |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1424 | Bitmap bitmap; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1425 | try { |
| 1426 | switch (icon.getType()) { |
| 1427 | case Icon.TYPE_RESOURCE: { |
| 1428 | injectValidateIconResPackage(shortcut, icon); |
| 1429 | |
| 1430 | shortcut.setIconResourceId(icon.getResId()); |
| 1431 | shortcut.addFlags(ShortcutInfo.FLAG_HAS_ICON_RES); |
| 1432 | return; |
| 1433 | } |
Hyunyoung Song | f281e7a | 2017-02-13 10:57:42 -0800 | [diff] [blame] | 1434 | case Icon.TYPE_BITMAP: |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 1435 | bitmap = icon.getBitmap(); // Don't recycle in this case. |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1436 | break; |
Hyunyoung Song | 4703746 | 2017-05-08 16:51:43 -0700 | [diff] [blame] | 1437 | case Icon.TYPE_ADAPTIVE_BITMAP: { |
| 1438 | bitmap = icon.getBitmap(); // Don't recycle in this case. |
| 1439 | maxIconDimension *= (1 + 2 * AdaptiveIconDrawable.getExtraInsetFraction()); |
Hyunyoung Song | 8947f59 | 2017-05-15 10:12:11 -0700 | [diff] [blame] | 1440 | break; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1441 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1442 | default: |
| 1443 | // This shouldn't happen because we've already validated the icon, but |
| 1444 | // just in case. |
| 1445 | throw ShortcutInfo.getInvalidIconException(); |
| 1446 | } |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1447 | mShortcutBitmapSaver.saveBitmapLocked(shortcut, |
Hyunyoung Song | 4703746 | 2017-05-08 16:51:43 -0700 | [diff] [blame] | 1448 | maxIconDimension, mIconPersistFormat, mIconPersistQuality); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1449 | } finally { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1450 | // Once saved, we won't use the original icon information, so null it out. |
| 1451 | shortcut.clearIcon(); |
| 1452 | } |
| 1453 | } finally { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1454 | injectRestoreCallingIdentity(token); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | // Unfortunately we can't do this check in unit tests because we fake creator package names, |
| 1459 | // so override in unit tests. |
| 1460 | // TODO CTS this case. |
| 1461 | void injectValidateIconResPackage(ShortcutInfo shortcut, Icon icon) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1462 | if (!shortcut.getPackage().equals(icon.getResPackage())) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1463 | throw new IllegalArgumentException( |
| 1464 | "Icon resource must reside in shortcut owner package"); |
| 1465 | } |
| 1466 | } |
| 1467 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1468 | static Bitmap shrinkBitmap(Bitmap in, int maxSize) { |
| 1469 | // Original width/height. |
| 1470 | final int ow = in.getWidth(); |
| 1471 | final int oh = in.getHeight(); |
| 1472 | if ((ow <= maxSize) && (oh <= maxSize)) { |
| 1473 | if (DEBUG) { |
| 1474 | Slog.d(TAG, String.format("Icon size %dx%d, no need to shrink", ow, oh)); |
| 1475 | } |
| 1476 | return in; |
| 1477 | } |
| 1478 | final int longerDimension = Math.max(ow, oh); |
| 1479 | |
| 1480 | // New width and height. |
| 1481 | final int nw = ow * maxSize / longerDimension; |
| 1482 | final int nh = oh * maxSize / longerDimension; |
| 1483 | if (DEBUG) { |
| 1484 | Slog.d(TAG, String.format("Icon size %dx%d, shrinking to %dx%d", |
| 1485 | ow, oh, nw, nh)); |
| 1486 | } |
| 1487 | |
| 1488 | final Bitmap scaledBitmap = Bitmap.createBitmap(nw, nh, Bitmap.Config.ARGB_8888); |
| 1489 | final Canvas c = new Canvas(scaledBitmap); |
| 1490 | |
| 1491 | final RectF dst = new RectF(0, 0, nw, nh); |
| 1492 | |
| 1493 | c.drawBitmap(in, /*src=*/ null, dst, /* paint =*/ null); |
| 1494 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1495 | return scaledBitmap; |
| 1496 | } |
| 1497 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1498 | /** |
| 1499 | * For a shortcut, update all resource names from resource IDs, and also update all |
| 1500 | * resource-based strings. |
| 1501 | */ |
| 1502 | void fixUpShortcutResourceNamesAndValues(ShortcutInfo si) { |
| 1503 | final Resources publisherRes = injectGetResourcesForApplicationAsUser( |
| 1504 | si.getPackage(), si.getUserId()); |
| 1505 | if (publisherRes != null) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 1506 | final long start = getStatStartTime(); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1507 | try { |
| 1508 | si.lookupAndFillInResourceNames(publisherRes); |
| 1509 | } finally { |
| 1510 | logDurationStat(Stats.RESOURCE_NAME_LOOKUP, start); |
| 1511 | } |
| 1512 | si.resolveResourceStrings(publisherRes); |
| 1513 | } |
| 1514 | } |
| 1515 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1516 | // === Caller validation === |
| 1517 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1518 | private boolean isCallerSystem() { |
| 1519 | final int callingUid = injectBinderCallingUid(); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1520 | return UserHandle.isSameApp(callingUid, Process.SYSTEM_UID); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
| 1523 | private boolean isCallerShell() { |
| 1524 | final int callingUid = injectBinderCallingUid(); |
| 1525 | return callingUid == Process.SHELL_UID || callingUid == Process.ROOT_UID; |
| 1526 | } |
| 1527 | |
| 1528 | private void enforceSystemOrShell() { |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 1529 | if (!(isCallerSystem() || isCallerShell())) { |
| 1530 | throw new SecurityException("Caller must be system or shell"); |
| 1531 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | private void enforceShell() { |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 1535 | if (!isCallerShell()) { |
| 1536 | throw new SecurityException("Caller must be shell"); |
| 1537 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1538 | } |
| 1539 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1540 | private void enforceSystem() { |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 1541 | if (!isCallerSystem()) { |
| 1542 | throw new SecurityException("Caller must be system"); |
| 1543 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1546 | private void enforceResetThrottlingPermission() { |
| 1547 | if (isCallerSystem()) { |
| 1548 | return; |
| 1549 | } |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1550 | enforceCallingOrSelfPermission( |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1551 | android.Manifest.permission.RESET_SHORTCUT_MANAGER_THROTTLING, null); |
| 1552 | } |
| 1553 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 1554 | private void enforceCallingOrSelfPermission( |
| 1555 | @NonNull String permission, @Nullable String message) { |
| 1556 | if (isCallerSystem()) { |
| 1557 | return; |
| 1558 | } |
| 1559 | injectEnforceCallingPermission(permission, message); |
| 1560 | } |
| 1561 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 1562 | /** |
| 1563 | * Somehow overriding ServiceContext.enforceCallingPermission() in the unit tests would confuse |
| 1564 | * mockito. So instead we extracted it here and override it in the tests. |
| 1565 | */ |
| 1566 | @VisibleForTesting |
| 1567 | void injectEnforceCallingPermission( |
| 1568 | @NonNull String permission, @Nullable String message) { |
| 1569 | mContext.enforceCallingPermission(permission, message); |
| 1570 | } |
| 1571 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1572 | private void verifyCaller(@NonNull String packageName, @UserIdInt int userId) { |
| 1573 | Preconditions.checkStringNotEmpty(packageName, "packageName"); |
| 1574 | |
| 1575 | if (isCallerSystem()) { |
| 1576 | return; // no check |
| 1577 | } |
| 1578 | |
| 1579 | final int callingUid = injectBinderCallingUid(); |
| 1580 | |
| 1581 | // Otherwise, make sure the arguments are valid. |
| 1582 | if (UserHandle.getUserId(callingUid) != userId) { |
| 1583 | throw new SecurityException("Invalid user-ID"); |
| 1584 | } |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 1585 | if (injectGetPackageUid(packageName, userId) != callingUid) { |
| 1586 | throw new SecurityException("Calling package name mismatch"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1587 | } |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 1588 | Preconditions.checkState(!isEphemeralApp(packageName, userId), |
| 1589 | "Ephemeral apps can't use ShortcutManager"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1590 | } |
| 1591 | |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 1592 | private void verifyShortcutInfoPackage(String callerPackage, ShortcutInfo si) { |
| 1593 | if (si == null) { |
| 1594 | return; |
| 1595 | } |
| 1596 | if (!Objects.equals(callerPackage, si.getPackage())) { |
| 1597 | android.util.EventLog.writeEvent(0x534e4554, "109824443", -1, ""); |
| 1598 | throw new SecurityException("Shortcut package name mismatch"); |
| 1599 | } |
| 1600 | } |
| 1601 | |
| 1602 | private void verifyShortcutInfoPackages( |
| 1603 | String callerPackage, List<ShortcutInfo> list) { |
| 1604 | final int size = list.size(); |
| 1605 | for (int i = 0; i < size; i++) { |
| 1606 | verifyShortcutInfoPackage(callerPackage, list.get(i)); |
| 1607 | } |
| 1608 | } |
| 1609 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1610 | // Overridden in unit tests to execute r synchronously. |
| 1611 | void injectPostToHandler(Runnable r) { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1612 | mHandler.post(r); |
| 1613 | } |
| 1614 | |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 1615 | void injectRunOnNewThread(Runnable r) { |
| 1616 | new Thread(r).start(); |
| 1617 | } |
| 1618 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1619 | /** |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1620 | * @throws IllegalArgumentException if {@code numShortcuts} is bigger than |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1621 | * {@link #getMaxActivityShortcuts()}. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1622 | */ |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1623 | void enforceMaxActivityShortcuts(int numShortcuts) { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1624 | if (numShortcuts > mMaxShortcuts) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1625 | throw new IllegalArgumentException("Max number of dynamic shortcuts exceeded"); |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /** |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1630 | * Return the max number of dynamic + manifest shortcuts for each launcher icon. |
| 1631 | */ |
| 1632 | int getMaxActivityShortcuts() { |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 1633 | return mMaxShortcuts; |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | /** |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1637 | * - Sends a notification to LauncherApps |
| 1638 | * - Write to file |
| 1639 | */ |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1640 | void packageShortcutsChanged(@NonNull String packageName, @UserIdInt int userId) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1641 | notifyListeners(packageName, userId); |
| 1642 | scheduleSaveUser(userId); |
| 1643 | } |
| 1644 | |
| 1645 | private void notifyListeners(@NonNull String packageName, @UserIdInt int userId) { |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 1646 | if (DEBUG) { |
| 1647 | Slog.d(TAG, String.format( |
| 1648 | "Shortcut changes: package=%s, user=%d", packageName, userId)); |
| 1649 | } |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 1650 | injectPostToHandler(() -> { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1651 | try { |
| 1652 | final ArrayList<ShortcutChangeListener> copy; |
| 1653 | synchronized (mLock) { |
| 1654 | if (!isUserUnlockedL(userId)) { |
| 1655 | return; |
| 1656 | } |
| 1657 | |
| 1658 | copy = new ArrayList<>(mListeners); |
| 1659 | } |
| 1660 | // Note onShortcutChanged() needs to be called with the system service permissions. |
| 1661 | for (int i = copy.size() - 1; i >= 0; i--) { |
| 1662 | copy.get(i).onShortcutChanged(packageName, userId); |
| 1663 | } |
| 1664 | } catch (Exception ignore) { |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 1665 | } |
| 1666 | }); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /** |
| 1670 | * Clean up / validate an incoming shortcut. |
| 1671 | * - Make sure all mandatory fields are set. |
| 1672 | * - Make sure the intent's extras are persistable, and them to set |
Makoto Onuki | 0eed441 | 2016-07-21 11:21:59 -0700 | [diff] [blame] | 1673 | * {@link ShortcutInfo#mIntentPersistableExtrases}. Also clear its extras. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1674 | * - Clear flags. |
| 1675 | */ |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1676 | private void fixUpIncomingShortcutInfo(@NonNull ShortcutInfo shortcut, boolean forUpdate, |
| 1677 | boolean forPinRequest) { |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 1678 | if (shortcut.isReturnedByServer()) { |
| 1679 | Log.w(TAG, |
| 1680 | "Re-publishing ShortcutInfo returned by server is not supported." |
| 1681 | + " Some information such as icon may lost from shortcut."); |
| 1682 | } |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 1683 | Objects.requireNonNull(shortcut, "Null shortcut detected"); |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1684 | if (shortcut.getActivity() != null) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1685 | Preconditions.checkState( |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1686 | shortcut.getPackage().equals(shortcut.getActivity().getPackageName()), |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1687 | "Cannot publish shortcut: activity " + shortcut.getActivity() + " does not" |
| 1688 | + " belong to package " + shortcut.getPackage()); |
Makoto Onuki | 13260b6ff | 2016-07-13 18:03:13 -0700 | [diff] [blame] | 1689 | Preconditions.checkState( |
| 1690 | injectIsMainActivity(shortcut.getActivity(), shortcut.getUserId()), |
| 1691 | "Cannot publish shortcut: activity " + shortcut.getActivity() + " is not" |
| 1692 | + " main activity"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1693 | } |
| 1694 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1695 | if (!forUpdate) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1696 | shortcut.enforceMandatoryFields(/* forPinned= */ forPinRequest); |
| 1697 | if (!forPinRequest) { |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1698 | Preconditions.checkState(shortcut.getActivity() != null, |
| 1699 | "Cannot publish shortcut: target activity is not set"); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1700 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1701 | } |
| 1702 | if (shortcut.getIcon() != null) { |
| 1703 | ShortcutInfo.validateIcon(shortcut.getIcon()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1704 | } |
| 1705 | |
Makoto Onuki | de66737 | 2016-03-15 14:29:20 -0700 | [diff] [blame] | 1706 | shortcut.replaceFlags(0); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1707 | } |
| 1708 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1709 | private void fixUpIncomingShortcutInfo(@NonNull ShortcutInfo shortcut, boolean forUpdate) { |
| 1710 | fixUpIncomingShortcutInfo(shortcut, forUpdate, /*forPinRequest=*/ false); |
| 1711 | } |
| 1712 | |
| 1713 | public void validateShortcutForPinRequest(@NonNull ShortcutInfo shortcut) { |
| 1714 | fixUpIncomingShortcutInfo(shortcut, /* forUpdate= */ false, /*forPinRequest=*/ true); |
| 1715 | } |
| 1716 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1717 | /** |
| 1718 | * When a shortcut has no target activity, set the default one from the package. |
| 1719 | */ |
| 1720 | private void fillInDefaultActivity(List<ShortcutInfo> shortcuts) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1721 | ComponentName defaultActivity = null; |
| 1722 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
| 1723 | final ShortcutInfo si = shortcuts.get(i); |
| 1724 | if (si.getActivity() == null) { |
| 1725 | if (defaultActivity == null) { |
| 1726 | defaultActivity = injectGetDefaultMainActivity( |
| 1727 | si.getPackage(), si.getUserId()); |
| 1728 | Preconditions.checkState(defaultActivity != null, |
| 1729 | "Launcher activity not found for package " + si.getPackage()); |
| 1730 | } |
| 1731 | si.setActivity(defaultActivity); |
| 1732 | } |
| 1733 | } |
| 1734 | } |
| 1735 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1736 | private void assignImplicitRanks(List<ShortcutInfo> shortcuts) { |
| 1737 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
| 1738 | shortcuts.get(i).setImplicitRank(i); |
| 1739 | } |
| 1740 | } |
| 1741 | |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 1742 | private List<ShortcutInfo> setReturnedByServer(List<ShortcutInfo> shortcuts) { |
| 1743 | for (int i = shortcuts.size() - 1; i >= 0; i--) { |
| 1744 | shortcuts.get(i).setReturnedByServer(); |
| 1745 | } |
| 1746 | return shortcuts; |
| 1747 | } |
| 1748 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1749 | // === APIs === |
| 1750 | |
| 1751 | @Override |
| 1752 | public boolean setDynamicShortcuts(String packageName, ParceledListSlice shortcutInfoList, |
| 1753 | @UserIdInt int userId) { |
| 1754 | verifyCaller(packageName, userId); |
| 1755 | |
| 1756 | final List<ShortcutInfo> newShortcuts = (List<ShortcutInfo>) shortcutInfoList.getList(); |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 1757 | verifyShortcutInfoPackages(packageName, newShortcuts); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1758 | final int size = newShortcuts.size(); |
| 1759 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1760 | final boolean unlimited = injectHasUnlimitedShortcutsApiCallsPermission( |
| 1761 | injectBinderCallingPid(), injectBinderCallingUid()); |
| 1762 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1763 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1764 | throwIfUserLockedL(userId); |
| 1765 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 1766 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1767 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1768 | ps.ensureImmutableShortcutsNotIncluded(newShortcuts, /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1769 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1770 | fillInDefaultActivity(newShortcuts); |
| 1771 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1772 | ps.enforceShortcutCountsBeforeOperation(newShortcuts, OPERATION_SET); |
| 1773 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1774 | // Throttling. |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1775 | if (!ps.tryApiCall(unlimited)) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1776 | return false; |
| 1777 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1778 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1779 | // Initialize the implicit ranks for ShortcutPackage.adjustRanks(). |
| 1780 | ps.clearAllImplicitRanks(); |
| 1781 | assignImplicitRanks(newShortcuts); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1782 | |
Makoto Onuki | df6da04 | 2016-06-16 09:51:40 -0700 | [diff] [blame] | 1783 | for (int i = 0; i < size; i++) { |
| 1784 | fixUpIncomingShortcutInfo(newShortcuts.get(i), /* forUpdate= */ false); |
| 1785 | } |
| 1786 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1787 | // First, remove all un-pinned; dynamic shortcuts |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1788 | ps.deleteAllDynamicShortcuts(/*ignoreInvisible=*/ true); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1789 | |
| 1790 | // Then, add/update all. We need to make sure to take over "pinned" flag. |
| 1791 | for (int i = 0; i < size; i++) { |
| 1792 | final ShortcutInfo newShortcut = newShortcuts.get(i); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1793 | ps.addOrReplaceDynamicShortcut(newShortcut); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1794 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1795 | |
| 1796 | // Lastly, adjust the ranks. |
| 1797 | ps.adjustRanks(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1798 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1799 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1800 | |
| 1801 | verifyStates(); |
| 1802 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1803 | return true; |
| 1804 | } |
| 1805 | |
| 1806 | @Override |
| 1807 | public boolean updateShortcuts(String packageName, ParceledListSlice shortcutInfoList, |
| 1808 | @UserIdInt int userId) { |
| 1809 | verifyCaller(packageName, userId); |
| 1810 | |
| 1811 | final List<ShortcutInfo> newShortcuts = (List<ShortcutInfo>) shortcutInfoList.getList(); |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 1812 | verifyShortcutInfoPackages(packageName, newShortcuts); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1813 | final int size = newShortcuts.size(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1814 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1815 | final boolean unlimited = injectHasUnlimitedShortcutsApiCallsPermission( |
| 1816 | injectBinderCallingPid(), injectBinderCallingUid()); |
| 1817 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1818 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1819 | throwIfUserLockedL(userId); |
| 1820 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 1821 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1822 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1823 | ps.ensureImmutableShortcutsNotIncluded(newShortcuts, /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1824 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1825 | // For update, don't fill in the default activity. Having null activity means |
| 1826 | // "don't update the activity" here. |
| 1827 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1828 | ps.enforceShortcutCountsBeforeOperation(newShortcuts, OPERATION_UPDATE); |
| 1829 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1830 | // Throttling. |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1831 | if (!ps.tryApiCall(unlimited)) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1832 | return false; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1833 | } |
| 1834 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1835 | // Initialize the implicit ranks for ShortcutPackage.adjustRanks(). |
| 1836 | ps.clearAllImplicitRanks(); |
| 1837 | assignImplicitRanks(newShortcuts); |
| 1838 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1839 | for (int i = 0; i < size; i++) { |
| 1840 | final ShortcutInfo source = newShortcuts.get(i); |
| 1841 | fixUpIncomingShortcutInfo(source, /* forUpdate= */ true); |
| 1842 | |
| 1843 | final ShortcutInfo target = ps.findShortcutById(source.getId()); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1844 | |
| 1845 | // Invisible shortcuts can't be updated. |
| 1846 | if (target == null || !target.isVisibleToPublisher()) { |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1847 | continue; |
| 1848 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1849 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1850 | if (target.isEnabled() != source.isEnabled()) { |
| 1851 | Slog.w(TAG, |
| 1852 | "ShortcutInfo.enabled cannot be changed with updateShortcuts()"); |
| 1853 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1854 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1855 | // When updating the rank, we need to insert between existing ranks, so set |
| 1856 | // this setRankChanged, and also copy the implicit rank fo adjustRanks(). |
| 1857 | if (source.hasRank()) { |
| 1858 | target.setRankChanged(); |
| 1859 | target.setImplicitRank(source.getImplicitRank()); |
| 1860 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1861 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1862 | final boolean replacingIcon = (source.getIcon() != null); |
| 1863 | if (replacingIcon) { |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1864 | removeIconLocked(target); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1865 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1866 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1867 | // Note copyNonNullFieldsFrom() does the "updatable with?" check too. |
| 1868 | target.copyNonNullFieldsFrom(source); |
| 1869 | target.setTimestamp(injectCurrentTimeMillis()); |
| 1870 | |
| 1871 | if (replacingIcon) { |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 1872 | saveIconAndFixUpShortcutLocked(target); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
| 1875 | // When we're updating any resource related fields, re-extract the res names and |
| 1876 | // the values. |
| 1877 | if (replacingIcon || source.hasStringResources()) { |
| 1878 | fixUpShortcutResourceNamesAndValues(target); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 1879 | } |
| 1880 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1881 | |
| 1882 | // Lastly, adjust the ranks. |
| 1883 | ps.adjustRanks(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1884 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1885 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1886 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1887 | verifyStates(); |
| 1888 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1889 | return true; |
| 1890 | } |
| 1891 | |
| 1892 | @Override |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1893 | public boolean addDynamicShortcuts(String packageName, ParceledListSlice shortcutInfoList, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1894 | @UserIdInt int userId) { |
| 1895 | verifyCaller(packageName, userId); |
| 1896 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1897 | final List<ShortcutInfo> newShortcuts = (List<ShortcutInfo>) shortcutInfoList.getList(); |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 1898 | verifyShortcutInfoPackages(packageName, newShortcuts); |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1899 | final int size = newShortcuts.size(); |
| 1900 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1901 | final boolean unlimited = injectHasUnlimitedShortcutsApiCallsPermission( |
| 1902 | injectBinderCallingPid(), injectBinderCallingUid()); |
| 1903 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1904 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 1905 | throwIfUserLockedL(userId); |
| 1906 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 1907 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1908 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1909 | ps.ensureImmutableShortcutsNotIncluded(newShortcuts, /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 1910 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 1911 | fillInDefaultActivity(newShortcuts); |
| 1912 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1913 | ps.enforceShortcutCountsBeforeOperation(newShortcuts, OPERATION_ADD); |
| 1914 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1915 | // Initialize the implicit ranks for ShortcutPackage.adjustRanks(). |
| 1916 | ps.clearAllImplicitRanks(); |
| 1917 | assignImplicitRanks(newShortcuts); |
| 1918 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1919 | // Throttling. |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 1920 | if (!ps.tryApiCall(unlimited)) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1921 | return false; |
| 1922 | } |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1923 | for (int i = 0; i < size; i++) { |
| 1924 | final ShortcutInfo newShortcut = newShortcuts.get(i); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1925 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1926 | // Validate the shortcut. |
| 1927 | fixUpIncomingShortcutInfo(newShortcut, /* forUpdate= */ false); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1928 | |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1929 | // When ranks are changing, we need to insert between ranks, so set the |
| 1930 | // "rank changed" flag. |
| 1931 | newShortcut.setRankChanged(); |
| 1932 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1933 | // Add it. |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1934 | ps.addOrReplaceDynamicShortcut(newShortcut); |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 1935 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 1936 | |
| 1937 | // Lastly, adjust the ranks. |
| 1938 | ps.adjustRanks(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1939 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 1940 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1941 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 1942 | verifyStates(); |
| 1943 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 1944 | return true; |
| 1945 | } |
| 1946 | |
| 1947 | @Override |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1948 | public boolean requestPinShortcut(String packageName, ShortcutInfo shortcut, |
| 1949 | IntentSender resultIntent, int userId) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 1950 | Objects.requireNonNull(shortcut); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1951 | Preconditions.checkArgument(shortcut.isEnabled(), "Shortcut must be enabled"); |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 1952 | return requestPinItem(packageName, userId, shortcut, null, null, resultIntent); |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1953 | } |
| 1954 | |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 1955 | @Override |
| 1956 | public Intent createShortcutResultIntent(String packageName, ShortcutInfo shortcut, int userId) |
| 1957 | throws RemoteException { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 1958 | Objects.requireNonNull(shortcut); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 1959 | Preconditions.checkArgument(shortcut.isEnabled(), "Shortcut must be enabled"); |
| 1960 | verifyCaller(packageName, userId); |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 1961 | verifyShortcutInfoPackage(packageName, shortcut); |
Sunny Goyal | a6be88a | 2017-01-12 16:27:58 -0800 | [diff] [blame] | 1962 | |
| 1963 | final Intent ret; |
| 1964 | synchronized (mLock) { |
| 1965 | throwIfUserLockedL(userId); |
| 1966 | |
| 1967 | // Send request to the launcher, if supported. |
| 1968 | ret = mShortcutRequestPinProcessor.createShortcutResultIntent(shortcut, userId); |
| 1969 | } |
| 1970 | |
| 1971 | verifyStates(); |
| 1972 | return ret; |
| 1973 | } |
| 1974 | |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1975 | /** |
| 1976 | * Handles {@link #requestPinShortcut} and {@link ShortcutServiceInternal#requestPinAppWidget}. |
| 1977 | * After validating the caller, it passes the request to {@link #mShortcutRequestPinProcessor}. |
| 1978 | * Either {@param shortcut} or {@param appWidget} should be non-null. |
| 1979 | */ |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 1980 | private boolean requestPinItem(String packageName, int userId, ShortcutInfo shortcut, |
| 1981 | AppWidgetProviderInfo appWidget, Bundle extras, IntentSender resultIntent) { |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 1982 | verifyCaller(packageName, userId); |
Makoto Onuki | a19fb23 | 2018-06-12 13:01:42 -0700 | [diff] [blame] | 1983 | verifyShortcutInfoPackage(packageName, shortcut); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1984 | |
| 1985 | final boolean ret; |
| 1986 | synchronized (mLock) { |
| 1987 | throwIfUserLockedL(userId); |
| 1988 | |
Makoto Onuki | a01f4f0 | 2016-12-15 15:58:41 -0800 | [diff] [blame] | 1989 | Preconditions.checkState(isUidForegroundLocked(injectBinderCallingUid()), |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 1990 | "Calling application must have a foreground activity or a foreground service"); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 1991 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 1992 | // If it's a pin shortcut request, and there's already a shortcut with the same ID |
| 1993 | // that's not visible to the caller (i.e. restore-blocked; meaning it's pinned by |
| 1994 | // someone already), then we just replace the existing one with this new one, |
| 1995 | // and then proceed the rest of the process. |
| 1996 | if (shortcut != null) { |
| 1997 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked( |
| 1998 | packageName, userId); |
| 1999 | final String id = shortcut.getId(); |
| 2000 | if (ps.isShortcutExistsAndInvisibleToPublisher(id)) { |
| 2001 | |
| 2002 | ps.updateInvisibleShortcutForPinRequestWith(shortcut); |
| 2003 | |
| 2004 | packageShortcutsChanged(packageName, userId); |
| 2005 | } |
| 2006 | } |
| 2007 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2008 | // Send request to the launcher, if supported. |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 2009 | ret = mShortcutRequestPinProcessor.requestPinItemLocked(shortcut, appWidget, extras, |
| 2010 | userId, resultIntent); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2011 | } |
| 2012 | |
| 2013 | verifyStates(); |
| 2014 | |
| 2015 | return ret; |
| 2016 | } |
| 2017 | |
| 2018 | @Override |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2019 | public void disableShortcuts(String packageName, List shortcutIds, |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 2020 | CharSequence disabledMessage, int disabledMessageResId, @UserIdInt int userId) { |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2021 | verifyCaller(packageName, userId); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2022 | Objects.requireNonNull(shortcutIds, "shortcutIds must be provided"); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2023 | |
| 2024 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2025 | throwIfUserLockedL(userId); |
| 2026 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2027 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2028 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2029 | ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds, |
| 2030 | /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2031 | |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 2032 | final String disabledMessageString = |
| 2033 | (disabledMessage == null) ? null : disabledMessage.toString(); |
| 2034 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2035 | for (int i = shortcutIds.size() - 1; i >= 0; i--) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2036 | final String id = Preconditions.checkStringNotEmpty((String) shortcutIds.get(i)); |
| 2037 | if (!ps.isShortcutExistsAndVisibleToPublisher(id)) { |
| 2038 | continue; |
| 2039 | } |
| 2040 | ps.disableWithId(id, |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 2041 | disabledMessageString, disabledMessageResId, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2042 | /* overrideImmutable=*/ false, /*ignoreInvisible=*/ true, |
| 2043 | ShortcutInfo.DISABLED_REASON_BY_APP); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2044 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 2045 | |
| 2046 | // We may have removed dynamic shortcuts which may have left a gap, so adjust the ranks. |
| 2047 | ps.adjustRanks(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2048 | } |
| 2049 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 2050 | |
| 2051 | verifyStates(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | @Override |
| 2055 | public void enableShortcuts(String packageName, List shortcutIds, @UserIdInt int userId) { |
| 2056 | verifyCaller(packageName, userId); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2057 | Objects.requireNonNull(shortcutIds, "shortcutIds must be provided"); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2058 | |
| 2059 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2060 | throwIfUserLockedL(userId); |
| 2061 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2062 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2063 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2064 | ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds, |
| 2065 | /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2066 | |
| 2067 | for (int i = shortcutIds.size() - 1; i >= 0; i--) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2068 | final String id = Preconditions.checkStringNotEmpty((String) shortcutIds.get(i)); |
| 2069 | if (!ps.isShortcutExistsAndVisibleToPublisher(id)) { |
| 2070 | continue; |
| 2071 | } |
| 2072 | ps.enableWithId(id); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2073 | } |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2074 | } |
| 2075 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 2076 | |
| 2077 | verifyStates(); |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2078 | } |
| 2079 | |
| 2080 | @Override |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 2081 | public void removeDynamicShortcuts(String packageName, List shortcutIds, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2082 | @UserIdInt int userId) { |
| 2083 | verifyCaller(packageName, userId); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2084 | Objects.requireNonNull(shortcutIds, "shortcutIds must be provided"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2085 | |
| 2086 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2087 | throwIfUserLockedL(userId); |
| 2088 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2089 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2090 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2091 | ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds, |
| 2092 | /*ignoreInvisible=*/ true); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2093 | |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 2094 | for (int i = shortcutIds.size() - 1; i >= 0; i--) { |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2095 | final String id = Preconditions.checkStringNotEmpty((String) shortcutIds.get(i)); |
| 2096 | if (!ps.isShortcutExistsAndVisibleToPublisher(id)) { |
| 2097 | continue; |
| 2098 | } |
| 2099 | ps.deleteDynamicWithId(id, /*ignoreInvisible=*/ true); |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 2100 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 2101 | |
| 2102 | // We may have removed dynamic shortcuts which may have left a gap, so adjust the ranks. |
| 2103 | ps.adjustRanks(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2104 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 2105 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 2106 | |
| 2107 | verifyStates(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2108 | } |
| 2109 | |
| 2110 | @Override |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 2111 | public void removeAllDynamicShortcuts(String packageName, @UserIdInt int userId) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2112 | verifyCaller(packageName, userId); |
| 2113 | |
| 2114 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2115 | throwIfUserLockedL(userId); |
| 2116 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2117 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2118 | ps.deleteAllDynamicShortcuts(/*ignoreInvisible=*/ true); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2119 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 2120 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 2121 | |
| 2122 | verifyStates(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | @Override |
| 2126 | public ParceledListSlice<ShortcutInfo> getDynamicShortcuts(String packageName, |
| 2127 | @UserIdInt int userId) { |
| 2128 | verifyCaller(packageName, userId); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 2129 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2130 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2131 | throwIfUserLockedL(userId); |
| 2132 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2133 | return getShortcutsWithQueryLocked( |
| 2134 | packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2135 | ShortcutInfo::isDynamicVisible); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | @Override |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2140 | public ParceledListSlice<ShortcutInfo> getManifestShortcuts(String packageName, |
| 2141 | @UserIdInt int userId) { |
| 2142 | verifyCaller(packageName, userId); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 2143 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2144 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2145 | throwIfUserLockedL(userId); |
| 2146 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2147 | return getShortcutsWithQueryLocked( |
| 2148 | packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2149 | ShortcutInfo::isManifestVisible); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | |
| 2153 | @Override |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2154 | public ParceledListSlice<ShortcutInfo> getPinnedShortcuts(String packageName, |
| 2155 | @UserIdInt int userId) { |
| 2156 | verifyCaller(packageName, userId); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 2157 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2158 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2159 | throwIfUserLockedL(userId); |
| 2160 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2161 | return getShortcutsWithQueryLocked( |
| 2162 | packageName, userId, ShortcutInfo.CLONE_REMOVE_FOR_CREATOR, |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2163 | ShortcutInfo::isPinnedVisible); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2167 | @Override |
| 2168 | public ParceledListSlice<ShortcutManager.ShareShortcutInfo> getShareTargets(String packageName, |
| 2169 | IntentFilter filter, @UserIdInt int userId) { |
| 2170 | verifyCaller(packageName, userId); |
Mehdi Alizadeh | c86dd1f | 2019-01-25 16:59:54 -0800 | [diff] [blame] | 2171 | enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APP_PREDICTIONS, |
| 2172 | "getShareTargets"); |
Mehdi Alizadeh | 406e8b3 | 2018-12-11 18:21:49 -0800 | [diff] [blame] | 2173 | |
| 2174 | synchronized (mLock) { |
| 2175 | throwIfUserLockedL(userId); |
| 2176 | |
| 2177 | final List<ShortcutManager.ShareShortcutInfo> shortcutInfoList = new ArrayList<>(); |
| 2178 | |
| 2179 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 2180 | user.forAllPackages(p -> shortcutInfoList.addAll(p.getMatchingShareTargets(filter))); |
| 2181 | |
| 2182 | return new ParceledListSlice<>(shortcutInfoList); |
| 2183 | } |
| 2184 | } |
| 2185 | |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 2186 | @Override |
| 2187 | public boolean hasShareTargets(String packageName, String packageToCheck, |
| 2188 | @UserIdInt int userId) { |
| 2189 | verifyCaller(packageName, userId); |
Mehdi Alizadeh | 627d4db | 2019-02-04 13:52:03 -0800 | [diff] [blame] | 2190 | enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_APP_PREDICTIONS, |
| 2191 | "hasShareTargets"); |
Mehdi Alizadeh | 85fd3d5 | 2019-01-23 12:49:53 -0800 | [diff] [blame] | 2192 | |
| 2193 | synchronized (mLock) { |
| 2194 | throwIfUserLockedL(userId); |
| 2195 | |
| 2196 | return getPackageShortcutsLocked(packageToCheck, userId).hasShareTargets(); |
| 2197 | } |
| 2198 | } |
| 2199 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2200 | @GuardedBy("mLock") |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2201 | private ParceledListSlice<ShortcutInfo> getShortcutsWithQueryLocked(@NonNull String packageName, |
| 2202 | @UserIdInt int userId, int cloneFlags, @NonNull Predicate<ShortcutInfo> query) { |
| 2203 | |
| 2204 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(); |
| 2205 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2206 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2207 | ps.findAll(ret, query, cloneFlags); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2208 | |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 2209 | return new ParceledListSlice<>(setReturnedByServer(ret)); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2210 | } |
| 2211 | |
| 2212 | @Override |
Makoto Onuki | d688079 | 2016-06-29 13:37:43 -0700 | [diff] [blame] | 2213 | public int getMaxShortcutCountPerActivity(String packageName, @UserIdInt int userId) |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2214 | throws RemoteException { |
| 2215 | verifyCaller(packageName, userId); |
| 2216 | |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 2217 | return mMaxShortcuts; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2218 | } |
| 2219 | |
| 2220 | @Override |
| 2221 | public int getRemainingCallCount(String packageName, @UserIdInt int userId) { |
| 2222 | verifyCaller(packageName, userId); |
| 2223 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 2224 | final boolean unlimited = injectHasUnlimitedShortcutsApiCallsPermission( |
| 2225 | injectBinderCallingPid(), injectBinderCallingUid()); |
| 2226 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2227 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2228 | throwIfUserLockedL(userId); |
| 2229 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2230 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 2231 | return mMaxUpdatesPerInterval - ps.getApiCallCount(unlimited); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | @Override |
| 2236 | public long getRateLimitResetTime(String packageName, @UserIdInt int userId) { |
| 2237 | verifyCaller(packageName, userId); |
| 2238 | |
| 2239 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2240 | throwIfUserLockedL(userId); |
| 2241 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2242 | return getNextResetTimeLocked(); |
| 2243 | } |
| 2244 | } |
| 2245 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2246 | @Override |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2247 | public int getIconMaxDimensions(String packageName, int userId) { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 2248 | verifyCaller(packageName, userId); |
| 2249 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2250 | synchronized (mLock) { |
| 2251 | return mMaxIconDimension; |
| 2252 | } |
| 2253 | } |
| 2254 | |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2255 | @Override |
| 2256 | public void reportShortcutUsed(String packageName, String shortcutId, int userId) { |
| 2257 | verifyCaller(packageName, userId); |
| 2258 | |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2259 | Objects.requireNonNull(shortcutId); |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 2260 | |
| 2261 | if (DEBUG) { |
| 2262 | Slog.d(TAG, String.format("reportShortcutUsed: Shortcut %s package %s used on user %d", |
| 2263 | shortcutId, packageName, userId)); |
| 2264 | } |
| 2265 | |
| 2266 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2267 | throwIfUserLockedL(userId); |
| 2268 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 2269 | final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId); |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2270 | |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 2271 | if (ps.findShortcutById(shortcutId) == null) { |
| 2272 | Log.w(TAG, String.format("reportShortcutUsed: package %s doesn't have shortcut %s", |
| 2273 | packageName, shortcutId)); |
| 2274 | return; |
| 2275 | } |
| 2276 | } |
| 2277 | |
| 2278 | final long token = injectClearCallingIdentity(); |
| 2279 | try { |
| 2280 | mUsageStatsManagerInternal.reportShortcutUsage(packageName, shortcutId, userId); |
| 2281 | } finally { |
| 2282 | injectRestoreCallingIdentity(token); |
| 2283 | } |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2284 | } |
| 2285 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2286 | @Override |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 2287 | public boolean isRequestPinItemSupported(int callingUserId, int requestType) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2288 | final long token = injectClearCallingIdentity(); |
| 2289 | try { |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 2290 | return mShortcutRequestPinProcessor |
| 2291 | .isRequestPinItemSupported(callingUserId, requestType); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2292 | } finally { |
| 2293 | injectRestoreCallingIdentity(token); |
| 2294 | } |
| 2295 | } |
| 2296 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2297 | /** |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 2298 | * Reset all throttling, for developer options and command line. Only system/shell can call |
| 2299 | * it. |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2300 | */ |
| 2301 | @Override |
| 2302 | public void resetThrottling() { |
| 2303 | enforceSystemOrShell(); |
| 2304 | |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 2305 | resetThrottlingInner(getCallingUserId()); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2306 | } |
| 2307 | |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 2308 | void resetThrottlingInner(@UserIdInt int userId) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2309 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2310 | if (!isUserUnlockedL(userId)) { |
| 2311 | Log.w(TAG, "User " + userId + " is locked or not running"); |
| 2312 | return; |
| 2313 | } |
| 2314 | |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 2315 | getUserShortcutsLocked(userId).resetThrottling(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2316 | } |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 2317 | scheduleSaveUser(userId); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 2318 | Slog.i(TAG, "ShortcutManager: throttling counter reset for user " + userId); |
| 2319 | } |
| 2320 | |
| 2321 | void resetAllThrottlingInner() { |
| 2322 | synchronized (mLock) { |
| 2323 | mRawLastResetTime = injectCurrentTimeMillis(); |
| 2324 | } |
| 2325 | scheduleSaveBaseState(); |
| 2326 | Slog.i(TAG, "ShortcutManager: throttling counter reset for all users"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2327 | } |
| 2328 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2329 | @Override |
| 2330 | public void onApplicationActive(String packageName, int userId) { |
| 2331 | if (DEBUG) { |
| 2332 | Slog.d(TAG, "onApplicationActive: package=" + packageName + " userid=" + userId); |
| 2333 | } |
| 2334 | enforceResetThrottlingPermission(); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2335 | |
| 2336 | synchronized (mLock) { |
| 2337 | if (!isUserUnlockedL(userId)) { |
| 2338 | // This is called by system UI, so no need to throw. Just ignore. |
| 2339 | return; |
| 2340 | } |
| 2341 | |
| 2342 | getPackageShortcutsLocked(packageName, userId) |
| 2343 | .resetRateLimitingForCommandLineNoSaving(); |
| 2344 | saveUserLocked(userId); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 2345 | } |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2348 | // We override this method in unit tests to do a simpler check. |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2349 | boolean hasShortcutHostPermission(@NonNull String callingPackage, int userId, |
| 2350 | int callingPid, int callingUid) { |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2351 | if (canSeeAnyPinnedShortcut(callingPackage, userId, callingPid, callingUid)) { |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2352 | return true; |
| 2353 | } |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 2354 | final long start = getStatStartTime(); |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 2355 | try { |
| 2356 | return hasShortcutHostPermissionInner(callingPackage, userId); |
| 2357 | } finally { |
| 2358 | logDurationStat(Stats.LAUNCHER_PERMISSION_CHECK, start); |
| 2359 | } |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2360 | } |
| 2361 | |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2362 | boolean canSeeAnyPinnedShortcut(@NonNull String callingPackage, int userId, |
| 2363 | int callingPid, int callingUid) { |
| 2364 | if (injectHasAccessShortcutsPermission(callingPid, callingUid)) { |
| 2365 | return true; |
| 2366 | } |
| 2367 | synchronized (mLock) { |
Makoto Onuki | 708703b | 2017-12-11 16:38:11 -0800 | [diff] [blame] | 2368 | return getNonPersistentUserLocked(userId).hasHostPackage(callingPackage); |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2369 | } |
| 2370 | } |
| 2371 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2372 | /** |
| 2373 | * Returns true if the caller has the "ACCESS_SHORTCUTS" permission. |
| 2374 | */ |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2375 | @VisibleForTesting |
| 2376 | boolean injectHasAccessShortcutsPermission(int callingPid, int callingUid) { |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2377 | return mContext.checkPermission(android.Manifest.permission.ACCESS_SHORTCUTS, |
| 2378 | callingPid, callingUid) == PackageManager.PERMISSION_GRANTED; |
| 2379 | } |
| 2380 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 2381 | /** |
| 2382 | * Returns true if the caller has the "UNLIMITED_SHORTCUTS_API_CALLS" permission. |
| 2383 | */ |
| 2384 | @VisibleForTesting |
| 2385 | boolean injectHasUnlimitedShortcutsApiCallsPermission(int callingPid, int callingUid) { |
| 2386 | return mContext.checkPermission(permission.UNLIMITED_SHORTCUTS_API_CALLS, |
| 2387 | callingPid, callingUid) == PackageManager.PERMISSION_GRANTED; |
| 2388 | } |
| 2389 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2390 | // This method is extracted so we can directly call this method from unit tests, |
| 2391 | // even when hasShortcutPermission() is overridden. |
| 2392 | @VisibleForTesting |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2393 | boolean hasShortcutHostPermissionInner(@NonNull String packageName, int userId) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2394 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2395 | throwIfUserLockedL(userId); |
| 2396 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 2397 | final ShortcutUser user = getUserShortcutsLocked(userId); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2398 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2399 | // Always trust the cached component. |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 2400 | final ComponentName cached = user.getCachedLauncher(); |
| 2401 | if (cached != null) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2402 | if (cached.getPackageName().equals(packageName)) { |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 2403 | return true; |
| 2404 | } |
| 2405 | } |
| 2406 | // If the cached one doesn't match, then go ahead |
| 2407 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2408 | final ComponentName detected = getDefaultLauncher(userId); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2409 | |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 2410 | // Update the cache. |
| 2411 | user.setLauncher(detected); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2412 | if (detected != null) { |
| 2413 | if (DEBUG) { |
| 2414 | Slog.v(TAG, "Detected launcher: " + detected); |
| 2415 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2416 | return detected.getPackageName().equals(packageName); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2417 | } else { |
| 2418 | // Default launcher not found. |
| 2419 | return false; |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2424 | @Nullable |
| 2425 | ComponentName getDefaultLauncher(@UserIdInt int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 2426 | final long start = getStatStartTime(); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2427 | final long token = injectClearCallingIdentity(); |
| 2428 | try { |
| 2429 | synchronized (mLock) { |
| 2430 | throwIfUserLockedL(userId); |
| 2431 | |
| 2432 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 2433 | |
| 2434 | final List<ResolveInfo> allHomeCandidates = new ArrayList<>(); |
| 2435 | |
| 2436 | // Default launcher from package manager. |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 2437 | final long startGetHomeActivitiesAsUser = getStatStartTime(); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 2438 | final ComponentName defaultLauncher = mPackageManagerInternal |
| 2439 | .getHomeActivitiesAsUser(allHomeCandidates, userId); |
| 2440 | logDurationStat(Stats.GET_DEFAULT_HOME, startGetHomeActivitiesAsUser); |
| 2441 | |
| 2442 | ComponentName detected = null; |
| 2443 | if (defaultLauncher != null) { |
| 2444 | detected = defaultLauncher; |
| 2445 | if (DEBUG) { |
| 2446 | Slog.v(TAG, "Default launcher from PM: " + detected); |
| 2447 | } |
| 2448 | } else { |
| 2449 | detected = user.getLastKnownLauncher(); |
| 2450 | |
| 2451 | if (detected != null) { |
| 2452 | if (injectIsActivityEnabledAndExported(detected, userId)) { |
| 2453 | if (DEBUG) { |
| 2454 | Slog.v(TAG, "Cached launcher: " + detected); |
| 2455 | } |
| 2456 | } else { |
| 2457 | Slog.w(TAG, "Cached launcher " + detected + " no longer exists"); |
| 2458 | detected = null; |
| 2459 | user.clearLauncher(); |
| 2460 | } |
| 2461 | } |
| 2462 | } |
| 2463 | |
| 2464 | if (detected == null) { |
| 2465 | // If we reach here, that means it's the first check since the user was created, |
| 2466 | // and there's already multiple launchers and there's no default set. |
| 2467 | // Find the system one with the highest priority. |
| 2468 | // (We need to check the priority too because of FallbackHome in Settings.) |
| 2469 | // If there's no system launcher yet, then no one can access shortcuts, until |
| 2470 | // the user explicitly |
| 2471 | final int size = allHomeCandidates.size(); |
| 2472 | |
| 2473 | int lastPriority = Integer.MIN_VALUE; |
| 2474 | for (int i = 0; i < size; i++) { |
| 2475 | final ResolveInfo ri = allHomeCandidates.get(i); |
| 2476 | if (!ri.activityInfo.applicationInfo.isSystemApp()) { |
| 2477 | continue; |
| 2478 | } |
| 2479 | if (DEBUG) { |
| 2480 | Slog.d(TAG, String.format("hasShortcutPermissionInner: pkg=%s prio=%d", |
| 2481 | ri.activityInfo.getComponentName(), ri.priority)); |
| 2482 | } |
| 2483 | if (ri.priority < lastPriority) { |
| 2484 | continue; |
| 2485 | } |
| 2486 | detected = ri.activityInfo.getComponentName(); |
| 2487 | lastPriority = ri.priority; |
| 2488 | } |
| 2489 | } |
| 2490 | return detected; |
| 2491 | } |
| 2492 | } finally { |
| 2493 | injectRestoreCallingIdentity(token); |
| 2494 | logDurationStat(Stats.GET_DEFAULT_LAUNCHER, start); |
| 2495 | } |
| 2496 | } |
| 2497 | |
Dianne Hackborn | c160fa4 | 2017-11-01 16:14:26 -0700 | [diff] [blame] | 2498 | public void setShortcutHostPackage(@NonNull String type, @Nullable String packageName, |
| 2499 | int userId) { |
| 2500 | synchronized (mLock) { |
Makoto Onuki | 708703b | 2017-12-11 16:38:11 -0800 | [diff] [blame] | 2501 | getNonPersistentUserLocked(userId).setShortcutHostPackage(type, packageName); |
Dianne Hackborn | c160fa4 | 2017-11-01 16:14:26 -0700 | [diff] [blame] | 2502 | } |
| 2503 | } |
| 2504 | |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2505 | // === House keeping === |
| 2506 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 2507 | private void cleanUpPackageForAllLoadedUsers(String packageName, @UserIdInt int packageUserId, |
| 2508 | boolean appStillExists) { |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 2509 | synchronized (mLock) { |
| 2510 | forEachLoadedUserLocked(user -> |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 2511 | cleanUpPackageLocked(packageName, user.getUserId(), packageUserId, |
| 2512 | appStillExists)); |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 2513 | } |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 2514 | } |
| 2515 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2516 | /** |
| 2517 | * Remove all the information associated with a package. This will really remove all the |
| 2518 | * information, including the restore information (i.e. it'll remove packages even if they're |
| 2519 | * shadow). |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 2520 | * |
| 2521 | * This is called when an app is uninstalled, or an app gets "clear data"ed. |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2522 | */ |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2523 | @GuardedBy("mLock") |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 2524 | @VisibleForTesting |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 2525 | void cleanUpPackageLocked(String packageName, int owningUserId, int packageUserId, |
| 2526 | boolean appStillExists) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2527 | final boolean wasUserLoaded = isUserLoadedLocked(owningUserId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2528 | |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 2529 | final ShortcutUser user = getUserShortcutsLocked(owningUserId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2530 | boolean doNotify = false; |
| 2531 | |
| 2532 | // First, remove the package from the package list (if the package is a publisher). |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2533 | if (packageUserId == owningUserId) { |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2534 | if (user.removePackage(packageName) != null) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2535 | doNotify = true; |
| 2536 | } |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2537 | } |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2538 | |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2539 | // Also remove from the launcher list (if the package is a launcher). |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 2540 | user.removeLauncher(packageUserId, packageName); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2541 | |
| 2542 | // Then remove pinned shortcuts from all launchers. |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2543 | user.forAllLaunchers(l -> l.cleanUpPackage(packageName, packageUserId)); |
| 2544 | |
| 2545 | // Now there may be orphan shortcuts because we removed pinned shortcuts at the previous |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2546 | // step. Remove them too. |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2547 | user.forAllPackages(p -> p.refreshPinnedFlags()); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2548 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2549 | scheduleSaveUser(owningUserId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2550 | |
| 2551 | if (doNotify) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2552 | notifyListeners(packageName, owningUserId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2553 | } |
| 2554 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 2555 | // If the app still exists (i.e. data cleared), we need to re-publish manifest shortcuts. |
| 2556 | if (appStillExists && (packageUserId == owningUserId)) { |
| 2557 | // This will do the notification and save when needed, so do it after the above |
| 2558 | // notifyListeners. |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2559 | user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 2560 | } |
| 2561 | |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2562 | if (!wasUserLoaded) { |
| 2563 | // Note this will execute the scheduled save. |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2564 | unloadUserLocked(owningUserId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2565 | } |
| 2566 | } |
| 2567 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2568 | /** |
| 2569 | * Entry point from {@link LauncherApps}. |
| 2570 | */ |
| 2571 | private class LocalService extends ShortcutServiceInternal { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2572 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2573 | @Override |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2574 | public List<ShortcutInfo> getShortcuts(int launcherUserId, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2575 | @NonNull String callingPackage, long changedSince, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2576 | @Nullable String packageName, @Nullable List<String> shortcutIds, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 2577 | @Nullable ComponentName componentName, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2578 | int queryFlags, int userId, int callingPid, int callingUid) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2579 | final ArrayList<ShortcutInfo> ret = new ArrayList<>(); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 2580 | |
Makoto Onuki | 20c95f8 | 2016-05-11 16:51:01 -0700 | [diff] [blame] | 2581 | final boolean cloneKeyFieldOnly = |
| 2582 | ((queryFlags & ShortcutQuery.FLAG_GET_KEY_FIELDS_ONLY) != 0); |
| 2583 | final int cloneFlag = cloneKeyFieldOnly ? ShortcutInfo.CLONE_REMOVE_NON_KEY_INFO |
| 2584 | : ShortcutInfo.CLONE_REMOVE_FOR_LAUNCHER; |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2585 | if (packageName == null) { |
| 2586 | shortcutIds = null; // LauncherAppsService already threw for it though. |
| 2587 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2588 | |
| 2589 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2590 | throwIfUserLockedL(userId); |
| 2591 | throwIfUserLockedL(launcherUserId); |
| 2592 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2593 | getLauncherShortcutsLocked(callingPackage, userId, launcherUserId) |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2594 | .attemptToRestoreIfNeededAndSave(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2595 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2596 | if (packageName != null) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2597 | getShortcutsInnerLocked(launcherUserId, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2598 | callingPackage, packageName, shortcutIds, changedSince, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2599 | componentName, queryFlags, userId, ret, cloneFlag, |
| 2600 | callingPid, callingUid); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2601 | } else { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2602 | final List<String> shortcutIdsF = shortcutIds; |
| 2603 | getUserShortcutsLocked(userId).forAllPackages(p -> { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2604 | getShortcutsInnerLocked(launcherUserId, |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2605 | callingPackage, p.getPackageName(), shortcutIdsF, changedSince, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2606 | componentName, queryFlags, userId, ret, cloneFlag, |
| 2607 | callingPid, callingUid); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2608 | }); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2609 | } |
| 2610 | } |
Makoto Onuki | bf563b6 | 2017-05-04 10:25:30 -0700 | [diff] [blame] | 2611 | return setReturnedByServer(ret); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2612 | } |
| 2613 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2614 | @GuardedBy("ShortcutService.this.mLock") |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2615 | private void getShortcutsInnerLocked(int launcherUserId, @NonNull String callingPackage, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2616 | @Nullable String packageName, @Nullable List<String> shortcutIds, long changedSince, |
Makoto Onuki | 99302b5 | 2017-03-29 12:42:26 -0700 | [diff] [blame] | 2617 | @Nullable ComponentName componentName, int queryFlags, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2618 | int userId, ArrayList<ShortcutInfo> ret, int cloneFlag, |
| 2619 | int callingPid, int callingUid) { |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2620 | final ArraySet<String> ids = shortcutIds == null ? null |
| 2621 | : new ArraySet<>(shortcutIds); |
| 2622 | |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2623 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 2624 | final ShortcutPackage p = user.getPackageShortcutsIfExists(packageName); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2625 | if (p == null) { |
| 2626 | return; // No need to instantiate ShortcutPackage. |
| 2627 | } |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2628 | final boolean matchDynamic = (queryFlags & ShortcutQuery.FLAG_MATCH_DYNAMIC) != 0; |
| 2629 | final boolean matchPinned = (queryFlags & ShortcutQuery.FLAG_MATCH_PINNED) != 0; |
| 2630 | final boolean matchManifest = (queryFlags & ShortcutQuery.FLAG_MATCH_MANIFEST) != 0; |
| 2631 | |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2632 | final boolean canAccessAllShortcuts = |
| 2633 | canSeeAnyPinnedShortcut(callingPackage, launcherUserId, callingPid, callingUid); |
| 2634 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2635 | final boolean getPinnedByAnyLauncher = |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2636 | canAccessAllShortcuts && |
| 2637 | ((queryFlags & ShortcutQuery.FLAG_MATCH_PINNED_BY_ANY_LAUNCHER) != 0); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2638 | |
| 2639 | p.findAll(ret, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2640 | (ShortcutInfo si) -> { |
| 2641 | if (si.getLastChangedTimestamp() < changedSince) { |
| 2642 | return false; |
| 2643 | } |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2644 | if (ids != null && !ids.contains(si.getId())) { |
| 2645 | return false; |
| 2646 | } |
Makoto Onuki | 8569452 | 2016-05-04 12:53:37 -0700 | [diff] [blame] | 2647 | if (componentName != null) { |
Makoto Onuki | 9fd9019 | 2017-01-06 18:31:03 +0000 | [diff] [blame] | 2648 | if (si.getActivity() != null |
| 2649 | && !si.getActivity().equals(componentName)) { |
Makoto Onuki | 8569452 | 2016-05-04 12:53:37 -0700 | [diff] [blame] | 2650 | return false; |
| 2651 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2652 | } |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2653 | if (matchDynamic && si.isDynamic()) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2654 | return true; |
| 2655 | } |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2656 | if ((matchPinned || getPinnedByAnyLauncher) && si.isPinned()) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2657 | return true; |
| 2658 | } |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2659 | if (matchManifest && si.isDeclaredInManifest()) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 2660 | return true; |
| 2661 | } |
| 2662 | return false; |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2663 | }, cloneFlag, callingPackage, launcherUserId, getPinnedByAnyLauncher); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | @Override |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2667 | public boolean isPinnedByCaller(int launcherUserId, @NonNull String callingPackage, |
| 2668 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
| 2669 | Preconditions.checkStringNotEmpty(packageName, "packageName"); |
| 2670 | Preconditions.checkStringNotEmpty(shortcutId, "shortcutId"); |
| 2671 | |
| 2672 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2673 | throwIfUserLockedL(userId); |
| 2674 | throwIfUserLockedL(launcherUserId); |
| 2675 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2676 | getLauncherShortcutsLocked(callingPackage, userId, launcherUserId) |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2677 | .attemptToRestoreIfNeededAndSave(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2678 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2679 | final ShortcutInfo si = getShortcutInfoLocked( |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2680 | launcherUserId, callingPackage, packageName, shortcutId, userId, |
| 2681 | /*getPinnedByAnyLauncher=*/ false); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2682 | return si != null && si.isPinned(); |
| 2683 | } |
| 2684 | } |
| 2685 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 2686 | @GuardedBy("ShortcutService.this.mLock") |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2687 | private ShortcutInfo getShortcutInfoLocked( |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2688 | int launcherUserId, @NonNull String callingPackage, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2689 | @NonNull String packageName, @NonNull String shortcutId, int userId, |
| 2690 | boolean getPinnedByAnyLauncher) { |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2691 | Preconditions.checkStringNotEmpty(packageName, "packageName"); |
| 2692 | Preconditions.checkStringNotEmpty(shortcutId, "shortcutId"); |
| 2693 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2694 | throwIfUserLockedL(userId); |
| 2695 | throwIfUserLockedL(launcherUserId); |
Makoto Onuki | 9c85001 | 2016-07-26 15:50:50 -0700 | [diff] [blame] | 2696 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2697 | final ShortcutPackage p = getUserShortcutsLocked(userId) |
| 2698 | .getPackageShortcutsIfExists(packageName); |
| 2699 | if (p == null) { |
| 2700 | return null; |
| 2701 | } |
| 2702 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2703 | final ArrayList<ShortcutInfo> list = new ArrayList<>(1); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2704 | p.findAll(list, |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2705 | (ShortcutInfo si) -> shortcutId.equals(si.getId()), |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2706 | /* clone flags=*/ 0, callingPackage, launcherUserId, getPinnedByAnyLauncher); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2707 | return list.size() == 0 ? null : list.get(0); |
| 2708 | } |
| 2709 | |
| 2710 | @Override |
| 2711 | public void pinShortcuts(int launcherUserId, |
| 2712 | @NonNull String callingPackage, @NonNull String packageName, |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2713 | @NonNull List<String> shortcutIds, int userId) { |
| 2714 | // Calling permission must be checked by LauncherAppsImpl. |
| 2715 | Preconditions.checkStringNotEmpty(packageName, "packageName"); |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2716 | Objects.requireNonNull(shortcutIds, "shortcutIds"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2717 | |
| 2718 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2719 | throwIfUserLockedL(userId); |
| 2720 | throwIfUserLockedL(launcherUserId); |
| 2721 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 2722 | final ShortcutLauncher launcher = |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2723 | getLauncherShortcutsLocked(callingPackage, userId, launcherUserId); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2724 | launcher.attemptToRestoreIfNeededAndSave(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 2725 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 2726 | launcher.pinShortcuts(userId, packageName, shortcutIds, /*forPinRequest=*/ false); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2727 | } |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 2728 | packageShortcutsChanged(packageName, userId); |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 2729 | |
| 2730 | verifyStates(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2731 | } |
| 2732 | |
| 2733 | @Override |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 2734 | public Intent[] createShortcutIntents(int launcherUserId, |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2735 | @NonNull String callingPackage, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2736 | @NonNull String packageName, @NonNull String shortcutId, int userId, |
| 2737 | int callingPid, int callingUid) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2738 | // Calling permission must be checked by LauncherAppsImpl. |
Makoto Onuki | 43204b8 | 2016-03-08 16:16:44 -0800 | [diff] [blame] | 2739 | Preconditions.checkStringNotEmpty(packageName, "packageName can't be empty"); |
| 2740 | Preconditions.checkStringNotEmpty(shortcutId, "shortcutId can't be empty"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2741 | |
| 2742 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2743 | throwIfUserLockedL(userId); |
| 2744 | throwIfUserLockedL(launcherUserId); |
| 2745 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2746 | getLauncherShortcutsLocked(callingPackage, userId, launcherUserId) |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2747 | .attemptToRestoreIfNeededAndSave(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2748 | |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2749 | final boolean getPinnedByAnyLauncher = |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2750 | canSeeAnyPinnedShortcut(callingPackage, launcherUserId, |
| 2751 | callingPid, callingUid); |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2752 | |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2753 | // Make sure the shortcut is actually visible to the launcher. |
| 2754 | final ShortcutInfo si = getShortcutInfoLocked( |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2755 | launcherUserId, callingPackage, packageName, shortcutId, userId, |
| 2756 | getPinnedByAnyLauncher); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2757 | // "si == null" should suffice here, but check the flags too just to make sure. |
Makoto Onuki | 35559d6 | 2017-11-06 16:26:32 -0800 | [diff] [blame] | 2758 | if (si == null || !si.isEnabled() || !(si.isAlive() || getPinnedByAnyLauncher)) { |
Makoto Onuki | 83f6d2d | 2016-07-11 14:30:19 -0700 | [diff] [blame] | 2759 | Log.e(TAG, "Shortcut " + shortcutId + " does not exist or disabled"); |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2760 | return null; |
| 2761 | } |
Makoto Onuki | 440a1ea | 2016-07-20 14:21:18 -0700 | [diff] [blame] | 2762 | return si.getIntents(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2763 | } |
| 2764 | } |
| 2765 | |
| 2766 | @Override |
| 2767 | public void addListener(@NonNull ShortcutChangeListener listener) { |
| 2768 | synchronized (mLock) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2769 | mListeners.add(Objects.requireNonNull(listener)); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2770 | } |
| 2771 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2772 | |
| 2773 | @Override |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2774 | public int getShortcutIconResId(int launcherUserId, @NonNull String callingPackage, |
| 2775 | @NonNull String packageName, @NonNull String shortcutId, int userId) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2776 | Objects.requireNonNull(callingPackage, "callingPackage"); |
| 2777 | Objects.requireNonNull(packageName, "packageName"); |
| 2778 | Objects.requireNonNull(shortcutId, "shortcutId"); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2779 | |
| 2780 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2781 | throwIfUserLockedL(userId); |
| 2782 | throwIfUserLockedL(launcherUserId); |
| 2783 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2784 | getLauncherShortcutsLocked(callingPackage, userId, launcherUserId) |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2785 | .attemptToRestoreIfNeededAndSave(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2786 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2787 | final ShortcutPackage p = getUserShortcutsLocked(userId) |
| 2788 | .getPackageShortcutsIfExists(packageName); |
| 2789 | if (p == null) { |
| 2790 | return 0; |
| 2791 | } |
| 2792 | |
| 2793 | final ShortcutInfo shortcutInfo = p.findShortcutById(shortcutId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2794 | return (shortcutInfo != null && shortcutInfo.hasIconResource()) |
| 2795 | ? shortcutInfo.getIconResourceId() : 0; |
| 2796 | } |
| 2797 | } |
| 2798 | |
| 2799 | @Override |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2800 | public ParcelFileDescriptor getShortcutIconFd(int launcherUserId, |
Makoto Onuki | abe8442 | 2016-04-07 09:41:19 -0700 | [diff] [blame] | 2801 | @NonNull String callingPackage, @NonNull String packageName, |
| 2802 | @NonNull String shortcutId, int userId) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2803 | Objects.requireNonNull(callingPackage, "callingPackage"); |
| 2804 | Objects.requireNonNull(packageName, "packageName"); |
| 2805 | Objects.requireNonNull(shortcutId, "shortcutId"); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2806 | |
| 2807 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2808 | throwIfUserLockedL(userId); |
| 2809 | throwIfUserLockedL(launcherUserId); |
| 2810 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2811 | getLauncherShortcutsLocked(callingPackage, userId, launcherUserId) |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2812 | .attemptToRestoreIfNeededAndSave(); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 2813 | |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2814 | final ShortcutPackage p = getUserShortcutsLocked(userId) |
| 2815 | .getPackageShortcutsIfExists(packageName); |
| 2816 | if (p == null) { |
| 2817 | return null; |
| 2818 | } |
| 2819 | |
| 2820 | final ShortcutInfo shortcutInfo = p.findShortcutById(shortcutId); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2821 | if (shortcutInfo == null || !shortcutInfo.hasIconFile()) { |
| 2822 | return null; |
| 2823 | } |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 2824 | final String path = mShortcutBitmapSaver.getBitmapPathMayWaitLocked(shortcutInfo); |
| 2825 | if (path == null) { |
| 2826 | Slog.w(TAG, "null bitmap detected in getShortcutIconFd()"); |
| 2827 | return null; |
| 2828 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2829 | try { |
| 2830 | return ParcelFileDescriptor.open( |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 2831 | new File(path), |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2832 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 2833 | } catch (FileNotFoundException e) { |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 2834 | Slog.e(TAG, "Icon file not found: " + path); |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 2835 | return null; |
| 2836 | } |
| 2837 | } |
| 2838 | } |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2839 | |
| 2840 | @Override |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 2841 | public boolean hasShortcutHostPermission(int launcherUserId, |
Makoto Onuki | 634cecb | 2017-10-13 17:10:48 -0700 | [diff] [blame] | 2842 | @NonNull String callingPackage, int callingPid, int callingUid) { |
| 2843 | return ShortcutService.this.hasShortcutHostPermission(callingPackage, launcherUserId, |
| 2844 | callingPid, callingUid); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 2845 | } |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 2846 | |
| 2847 | @Override |
Dianne Hackborn | c160fa4 | 2017-11-01 16:14:26 -0700 | [diff] [blame] | 2848 | public void setShortcutHostPackage(@NonNull String type, @Nullable String packageName, |
| 2849 | int userId) { |
| 2850 | ShortcutService.this.setShortcutHostPackage(type, packageName, userId); |
| 2851 | } |
| 2852 | |
| 2853 | @Override |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 2854 | public boolean requestPinAppWidget(@NonNull String callingPackage, |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 2855 | @NonNull AppWidgetProviderInfo appWidget, @Nullable Bundle extras, |
| 2856 | @Nullable IntentSender resultIntent, int userId) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2857 | Objects.requireNonNull(appWidget); |
Sunny Goyal | 4ad6b57 | 2017-02-28 11:11:51 -0800 | [diff] [blame] | 2858 | return requestPinItem(callingPackage, userId, null, appWidget, extras, resultIntent); |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 2859 | } |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 2860 | |
| 2861 | @Override |
| 2862 | public boolean isRequestPinItemSupported(int callingUserId, int requestType) { |
| 2863 | return ShortcutService.this.isRequestPinItemSupported(callingUserId, requestType); |
| 2864 | } |
Tony Mak | ed6ef62 | 2017-12-07 16:36:16 +0000 | [diff] [blame] | 2865 | |
| 2866 | @Override |
| 2867 | public boolean isForegroundDefaultLauncher(@NonNull String callingPackage, int callingUid) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 2868 | Objects.requireNonNull(callingPackage); |
Tony Mak | ed6ef62 | 2017-12-07 16:36:16 +0000 | [diff] [blame] | 2869 | |
| 2870 | final int userId = UserHandle.getUserId(callingUid); |
| 2871 | final ComponentName defaultLauncher = getDefaultLauncher(userId); |
| 2872 | if (defaultLauncher == null) { |
| 2873 | return false; |
| 2874 | } |
| 2875 | if (!callingPackage.equals(defaultLauncher.getPackageName())) { |
| 2876 | return false; |
| 2877 | } |
| 2878 | synchronized (mLock) { |
| 2879 | if (!isUidForegroundLocked(callingUid)) { |
| 2880 | return false; |
| 2881 | } |
| 2882 | } |
| 2883 | return true; |
| 2884 | } |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2885 | } |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2886 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2887 | final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2888 | @Override |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2889 | public void onReceive(Context context, Intent intent) { |
| 2890 | if (!mBootCompleted.get()) { |
| 2891 | return; // Boot not completed, ignore the broadcast. |
| 2892 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2893 | try { |
| 2894 | if (Intent.ACTION_LOCALE_CHANGED.equals(intent.getAction())) { |
| 2895 | handleLocaleChanged(); |
| 2896 | } |
| 2897 | } catch (Exception e) { |
| 2898 | wtf("Exception in mReceiver.onReceive", e); |
Makoto Onuki | c51b287 | 2016-05-04 15:24:50 -0700 | [diff] [blame] | 2899 | } |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 2900 | } |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2901 | }; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 2902 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 2903 | void handleLocaleChanged() { |
| 2904 | if (DEBUG) { |
| 2905 | Slog.d(TAG, "handleLocaleChanged"); |
| 2906 | } |
| 2907 | scheduleSaveBaseState(); |
| 2908 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2909 | synchronized (mLock) { |
| 2910 | final long token = injectClearCallingIdentity(); |
| 2911 | try { |
| 2912 | forEachLoadedUserLocked(user -> user.detectLocaleChange()); |
| 2913 | } finally { |
| 2914 | injectRestoreCallingIdentity(token); |
| 2915 | } |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 2916 | } |
| 2917 | } |
| 2918 | |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 2919 | /** |
| 2920 | * Package event callbacks. |
| 2921 | */ |
| 2922 | @VisibleForTesting |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2923 | final BroadcastReceiver mPackageMonitor = new BroadcastReceiver() { |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 2924 | @Override |
| 2925 | public void onReceive(Context context, Intent intent) { |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2926 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 2927 | if (userId == UserHandle.USER_NULL) { |
| 2928 | Slog.w(TAG, "Intent broadcast does not contain user handle: " + intent); |
| 2929 | return; |
| 2930 | } |
| 2931 | |
| 2932 | final String action = intent.getAction(); |
| 2933 | |
| 2934 | // This is normally called on Handler, so clearCallingIdentity() isn't needed, |
| 2935 | // but we still check it in unit tests. |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 2936 | final long token = injectClearCallingIdentity(); |
| 2937 | try { |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 2938 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2939 | if (!isUserUnlockedL(userId)) { |
| 2940 | if (DEBUG) { |
| 2941 | Slog.d(TAG, "Ignoring package broadcast " + action |
| 2942 | + " for locked/stopped user " + userId); |
| 2943 | } |
| 2944 | return; |
| 2945 | } |
| 2946 | |
| 2947 | // Whenever we get one of those package broadcasts, or get |
| 2948 | // ACTION_PREFERRED_ACTIVITY_CHANGED, we purge the default launcher cache. |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 2949 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 2950 | user.clearLauncher(); |
| 2951 | } |
| 2952 | if (Intent.ACTION_PREFERRED_ACTIVITY_CHANGED.equals(action)) { |
| 2953 | // Nothing farther to do. |
| 2954 | return; |
| 2955 | } |
| 2956 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 2957 | final Uri intentUri = intent.getData(); |
| 2958 | final String packageName = (intentUri != null) ? intentUri.getSchemeSpecificPart() |
| 2959 | : null; |
| 2960 | if (packageName == null) { |
| 2961 | Slog.w(TAG, "Intent broadcast does not contain package name: " + intent); |
| 2962 | return; |
| 2963 | } |
| 2964 | |
| 2965 | final boolean replacing = intent.getBooleanExtra(Intent.EXTRA_REPLACING, false); |
| 2966 | |
| 2967 | switch (action) { |
| 2968 | case Intent.ACTION_PACKAGE_ADDED: |
| 2969 | if (replacing) { |
| 2970 | handlePackageUpdateFinished(packageName, userId); |
| 2971 | } else { |
| 2972 | handlePackageAdded(packageName, userId); |
| 2973 | } |
| 2974 | break; |
| 2975 | case Intent.ACTION_PACKAGE_REMOVED: |
| 2976 | if (!replacing) { |
| 2977 | handlePackageRemoved(packageName, userId); |
| 2978 | } |
| 2979 | break; |
| 2980 | case Intent.ACTION_PACKAGE_CHANGED: |
| 2981 | handlePackageChanged(packageName, userId); |
| 2982 | |
| 2983 | break; |
| 2984 | case Intent.ACTION_PACKAGE_DATA_CLEARED: |
| 2985 | handlePackageDataCleared(packageName, userId); |
| 2986 | break; |
| 2987 | } |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 2988 | } catch (Exception e) { |
| 2989 | wtf("Exception in mPackageMonitor.onReceive", e); |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 2990 | } finally { |
| 2991 | injectRestoreCallingIdentity(token); |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 2992 | } |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2993 | } |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 2994 | }; |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 2995 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 2996 | /** |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 2997 | * Called when a user is unlocked. |
| 2998 | * - Check all known packages still exist, and otherwise perform cleanup. |
| 2999 | * - If a package still exists, check the version code. If it's been updated, may need to |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3000 | * update timestamps of its shortcuts. |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3001 | */ |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 3002 | @VisibleForTesting |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 3003 | void checkPackageChanges(@UserIdInt int ownerUserId) { |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3004 | if (DEBUG) { |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 3005 | Slog.d(TAG, "checkPackageChanges() ownerUserId=" + ownerUserId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3006 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3007 | if (injectIsSafeModeEnabled()) { |
| 3008 | Slog.i(TAG, "Safe mode, skipping checkPackageChanges()"); |
| 3009 | return; |
| 3010 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3011 | |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3012 | final long start = getStatStartTime(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3013 | try { |
| 3014 | final ArrayList<PackageWithUser> gonePackages = new ArrayList<>(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3015 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3016 | synchronized (mLock) { |
| 3017 | final ShortcutUser user = getUserShortcutsLocked(ownerUserId); |
| 3018 | |
| 3019 | // Find packages that have been uninstalled. |
| 3020 | user.forAllPackageItems(spi -> { |
| 3021 | if (spi.getPackageInfo().isShadow()) { |
| 3022 | return; // Don't delete shadow information. |
| 3023 | } |
| 3024 | if (!isPackageInstalled(spi.getPackageName(), spi.getPackageUserId())) { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3025 | if (DEBUG) { |
| 3026 | Slog.d(TAG, "Uninstalled: " + spi.getPackageName() |
| 3027 | + " user " + spi.getPackageUserId()); |
| 3028 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3029 | gonePackages.add(PackageWithUser.of(spi)); |
| 3030 | } |
| 3031 | }); |
| 3032 | if (gonePackages.size() > 0) { |
| 3033 | for (int i = gonePackages.size() - 1; i >= 0; i--) { |
| 3034 | final PackageWithUser pu = gonePackages.get(i); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3035 | cleanUpPackageLocked(pu.packageName, ownerUserId, pu.userId, |
| 3036 | /* appStillExists = */ false); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3037 | } |
Makoto Onuki | d99c6f0 | 2016-03-28 11:02:54 -0700 | [diff] [blame] | 3038 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3039 | |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3040 | rescanUpdatedPackagesLocked(ownerUserId, user.getLastAppScanTime()); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3041 | } |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3042 | } finally { |
| 3043 | logDurationStat(Stats.CHECK_PACKAGE_CHANGES, start); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3044 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 3045 | verifyStates(); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3046 | } |
| 3047 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 3048 | @GuardedBy("mLock") |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3049 | private void rescanUpdatedPackagesLocked(@UserIdInt int userId, long lastScanTime) { |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3050 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 3051 | |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 3052 | // Note after each OTA, we'll need to rescan all system apps, as their lastUpdateTime |
| 3053 | // is not reliable. |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3054 | final long now = injectCurrentTimeMillis(); |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 3055 | final boolean afterOta = |
| 3056 | !injectBuildFingerprint().equals(user.getLastAppScanOsFingerprint()); |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3057 | |
| 3058 | // Then for each installed app, publish manifest shortcuts when needed. |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 3059 | forUpdatedPackages(userId, lastScanTime, afterOta, ai -> { |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3060 | user.attemptToRestoreIfNeededAndSave(this, ai.packageName, userId); |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3061 | |
| 3062 | user.rescanPackageIfNeeded(ai.packageName, /* forceRescan= */ true); |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3063 | }); |
| 3064 | |
| 3065 | // Write the time just before the scan, because there may be apps that have just |
| 3066 | // been updated, and we want to catch them in the next time. |
| 3067 | user.setLastAppScanTime(now); |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 3068 | user.setLastAppScanOsFingerprint(injectBuildFingerprint()); |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3069 | scheduleSaveUser(userId); |
| 3070 | } |
| 3071 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3072 | private void handlePackageAdded(String packageName, @UserIdInt int userId) { |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3073 | if (DEBUG) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3074 | Slog.d(TAG, String.format("handlePackageAdded: %s user=%d", packageName, userId)); |
| 3075 | } |
| 3076 | synchronized (mLock) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3077 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 3078 | user.attemptToRestoreIfNeededAndSave(this, packageName, userId); |
Makoto Onuki | 64183d5 | 2016-08-08 14:11:34 -0700 | [diff] [blame] | 3079 | user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3080 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 3081 | verifyStates(); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | private void handlePackageUpdateFinished(String packageName, @UserIdInt int userId) { |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3085 | if (DEBUG) { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3086 | Slog.d(TAG, String.format("handlePackageUpdateFinished: %s user=%d", |
| 3087 | packageName, userId)); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3088 | } |
| 3089 | synchronized (mLock) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3090 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 3091 | user.attemptToRestoreIfNeededAndSave(this, packageName, userId); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 3092 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3093 | if (isPackageInstalled(packageName, userId)) { |
Makoto Onuki | 64183d5 | 2016-08-08 14:11:34 -0700 | [diff] [blame] | 3094 | user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 3095 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3096 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 3097 | verifyStates(); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3098 | } |
| 3099 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3100 | private void handlePackageRemoved(String packageName, @UserIdInt int packageUserId) { |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3101 | if (DEBUG) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3102 | Slog.d(TAG, String.format("handlePackageRemoved: %s user=%d", packageName, |
| 3103 | packageUserId)); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3104 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3105 | cleanUpPackageForAllLoadedUsers(packageName, packageUserId, /* appStillExists = */ false); |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 3106 | |
| 3107 | verifyStates(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 3108 | } |
| 3109 | |
Makoto Onuki | 9ac59d0 | 2016-04-26 11:23:14 -0700 | [diff] [blame] | 3110 | private void handlePackageDataCleared(String packageName, int packageUserId) { |
| 3111 | if (DEBUG) { |
| 3112 | Slog.d(TAG, String.format("handlePackageDataCleared: %s user=%d", packageName, |
| 3113 | packageUserId)); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3114 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3115 | cleanUpPackageForAllLoadedUsers(packageName, packageUserId, /* appStillExists = */ true); |
| 3116 | |
| 3117 | verifyStates(); |
| 3118 | } |
| 3119 | |
| 3120 | private void handlePackageChanged(String packageName, int packageUserId) { |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 3121 | if (!isPackageInstalled(packageName, packageUserId)) { |
| 3122 | // Probably disabled, which is the same thing as uninstalled. |
| 3123 | handlePackageRemoved(packageName, packageUserId); |
| 3124 | return; |
| 3125 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3126 | if (DEBUG) { |
| 3127 | Slog.d(TAG, String.format("handlePackageChanged: %s user=%d", packageName, |
| 3128 | packageUserId)); |
| 3129 | } |
| 3130 | |
| 3131 | // Activities may be disabled or enabled. Just rescan the package. |
| 3132 | synchronized (mLock) { |
| 3133 | final ShortcutUser user = getUserShortcutsLocked(packageUserId); |
| 3134 | |
Makoto Onuki | 4e6cef4 | 2016-07-13 16:14:01 -0700 | [diff] [blame] | 3135 | user.rescanPackageIfNeeded(packageName, /* forceRescan=*/ true); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3136 | } |
Makoto Onuki | 9e1f559 | 2016-06-08 12:30:23 -0700 | [diff] [blame] | 3137 | |
| 3138 | verifyStates(); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3139 | } |
| 3140 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3141 | // === PackageManager interaction === |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3142 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3143 | /** |
| 3144 | * Returns {@link PackageInfo} unless it's uninstalled or disabled. |
| 3145 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3146 | @Nullable |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3147 | final PackageInfo getPackageInfoWithSignatures(String packageName, @UserIdInt int userId) { |
| 3148 | return getPackageInfo(packageName, userId, true); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3149 | } |
| 3150 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3151 | /** |
| 3152 | * Returns {@link PackageInfo} unless it's uninstalled or disabled. |
| 3153 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3154 | @Nullable |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3155 | final PackageInfo getPackageInfo(String packageName, @UserIdInt int userId) { |
| 3156 | return getPackageInfo(packageName, userId, false); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3157 | } |
| 3158 | |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3159 | int injectGetPackageUid(@NonNull String packageName, @UserIdInt int userId) { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3160 | final long token = injectClearCallingIdentity(); |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3161 | try { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3162 | return mIPackageManager.getPackageUid(packageName, PACKAGE_MATCH_FLAGS, userId); |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3163 | } catch (RemoteException e) { |
| 3164 | // Shouldn't happen. |
| 3165 | Slog.wtf(TAG, "RemoteException", e); |
| 3166 | return -1; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3167 | } finally { |
| 3168 | injectRestoreCallingIdentity(token); |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3169 | } |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3170 | } |
| 3171 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3172 | /** |
| 3173 | * Returns {@link PackageInfo} unless it's uninstalled or disabled. |
| 3174 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3175 | @Nullable |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3176 | @VisibleForTesting |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3177 | final PackageInfo getPackageInfo(String packageName, @UserIdInt int userId, |
| 3178 | boolean getSignatures) { |
| 3179 | return isInstalledOrNull(injectPackageInfoWithUninstalled( |
| 3180 | packageName, userId, getSignatures)); |
| 3181 | } |
| 3182 | |
| 3183 | /** |
| 3184 | * Do not use directly; this returns uninstalled packages too. |
| 3185 | */ |
| 3186 | @Nullable |
| 3187 | @VisibleForTesting |
| 3188 | PackageInfo injectPackageInfoWithUninstalled(String packageName, @UserIdInt int userId, |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3189 | boolean getSignatures) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3190 | final long start = getStatStartTime(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3191 | final long token = injectClearCallingIdentity(); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3192 | try { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3193 | return mIPackageManager.getPackageInfo( |
| 3194 | packageName, PACKAGE_MATCH_FLAGS |
Michal Karpinski | 528c3e5 | 2018-02-07 17:47:10 +0000 | [diff] [blame] | 3195 | | (getSignatures ? PackageManager.GET_SIGNING_CERTIFICATES : 0), |
| 3196 | userId); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3197 | } catch (RemoteException e) { |
| 3198 | // Shouldn't happen. |
| 3199 | Slog.wtf(TAG, "RemoteException", e); |
| 3200 | return null; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3201 | } finally { |
| 3202 | injectRestoreCallingIdentity(token); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3203 | |
| 3204 | logDurationStat( |
| 3205 | (getSignatures ? Stats.GET_PACKAGE_INFO_WITH_SIG : Stats.GET_PACKAGE_INFO), |
| 3206 | start); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3207 | } |
| 3208 | } |
| 3209 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3210 | /** |
| 3211 | * Returns {@link ApplicationInfo} unless it's uninstalled or disabled. |
| 3212 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3213 | @Nullable |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3214 | @VisibleForTesting |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3215 | final ApplicationInfo getApplicationInfo(String packageName, @UserIdInt int userId) { |
| 3216 | return isInstalledOrNull(injectApplicationInfoWithUninstalled(packageName, userId)); |
| 3217 | } |
| 3218 | |
| 3219 | /** |
| 3220 | * Do not use directly; this returns uninstalled packages too. |
| 3221 | */ |
| 3222 | @Nullable |
| 3223 | @VisibleForTesting |
| 3224 | ApplicationInfo injectApplicationInfoWithUninstalled( |
| 3225 | String packageName, @UserIdInt int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3226 | final long start = getStatStartTime(); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3227 | final long token = injectClearCallingIdentity(); |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3228 | try { |
| 3229 | return mIPackageManager.getApplicationInfo(packageName, PACKAGE_MATCH_FLAGS, userId); |
| 3230 | } catch (RemoteException e) { |
| 3231 | // Shouldn't happen. |
| 3232 | Slog.wtf(TAG, "RemoteException", e); |
| 3233 | return null; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3234 | } finally { |
| 3235 | injectRestoreCallingIdentity(token); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3236 | |
| 3237 | logDurationStat(Stats.GET_APPLICATION_INFO, start); |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3241 | /** |
| 3242 | * Returns {@link ActivityInfo} with its metadata unless it's uninstalled or disabled. |
| 3243 | */ |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3244 | @Nullable |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3245 | final ActivityInfo getActivityInfoWithMetadata(ComponentName activity, @UserIdInt int userId) { |
| 3246 | return isInstalledOrNull(injectGetActivityInfoWithMetadataWithUninstalled( |
| 3247 | activity, userId)); |
| 3248 | } |
| 3249 | |
| 3250 | /** |
| 3251 | * Do not use directly; this returns uninstalled packages too. |
| 3252 | */ |
| 3253 | @Nullable |
| 3254 | @VisibleForTesting |
| 3255 | ActivityInfo injectGetActivityInfoWithMetadataWithUninstalled( |
| 3256 | ComponentName activity, @UserIdInt int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3257 | final long start = getStatStartTime(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3258 | final long token = injectClearCallingIdentity(); |
| 3259 | try { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3260 | return mIPackageManager.getActivityInfo(activity, |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3261 | (PACKAGE_MATCH_FLAGS | PackageManager.GET_META_DATA), userId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3262 | } catch (RemoteException e) { |
| 3263 | // Shouldn't happen. |
| 3264 | Slog.wtf(TAG, "RemoteException", e); |
| 3265 | return null; |
| 3266 | } finally { |
| 3267 | injectRestoreCallingIdentity(token); |
| 3268 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3269 | logDurationStat(Stats.GET_ACTIVITY_WITH_METADATA, start); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3270 | } |
| 3271 | } |
| 3272 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3273 | /** |
| 3274 | * Return all installed and enabled packages. |
| 3275 | */ |
| 3276 | @NonNull |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3277 | @VisibleForTesting |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3278 | final List<PackageInfo> getInstalledPackages(@UserIdInt int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3279 | final long start = getStatStartTime(); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3280 | final long token = injectClearCallingIdentity(); |
| 3281 | try { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3282 | final List<PackageInfo> all = injectGetPackagesWithUninstalled(userId); |
| 3283 | |
| 3284 | all.removeIf(PACKAGE_NOT_INSTALLED); |
| 3285 | |
| 3286 | return all; |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3287 | } catch (RemoteException e) { |
| 3288 | // Shouldn't happen. |
| 3289 | Slog.wtf(TAG, "RemoteException", e); |
| 3290 | return null; |
| 3291 | } finally { |
| 3292 | injectRestoreCallingIdentity(token); |
| 3293 | |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 3294 | logDurationStat(Stats.GET_INSTALLED_PACKAGES, start); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3295 | } |
| 3296 | } |
| 3297 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3298 | /** |
| 3299 | * Do not use directly; this returns uninstalled packages too. |
| 3300 | */ |
| 3301 | @NonNull |
| 3302 | @VisibleForTesting |
| 3303 | List<PackageInfo> injectGetPackagesWithUninstalled(@UserIdInt int userId) |
| 3304 | throws RemoteException { |
| 3305 | final ParceledListSlice<PackageInfo> parceledList = |
| 3306 | mIPackageManager.getInstalledPackages(PACKAGE_MATCH_FLAGS, userId); |
| 3307 | if (parceledList == null) { |
| 3308 | return Collections.emptyList(); |
| 3309 | } |
| 3310 | return parceledList.getList(); |
| 3311 | } |
| 3312 | |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 3313 | private void forUpdatedPackages(@UserIdInt int userId, long lastScanTime, boolean afterOta, |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3314 | Consumer<ApplicationInfo> callback) { |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 3315 | if (DEBUG) { |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3316 | Slog.d(TAG, "forUpdatedPackages for user " + userId + ", lastScanTime=" + lastScanTime |
| 3317 | + " afterOta=" + afterOta); |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 3318 | } |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3319 | final List<PackageInfo> list = getInstalledPackages(userId); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3320 | for (int i = list.size() - 1; i >= 0; i--) { |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 3321 | final PackageInfo pi = list.get(i); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3322 | |
Makoto Onuki | 64183d5 | 2016-08-08 14:11:34 -0700 | [diff] [blame] | 3323 | // If the package has been updated since the last scan time, then scan it. |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3324 | // Also if it's right after an OTA, always re-scan all apps anyway, since the |
| 3325 | // shortcut parser might have changed. |
| 3326 | if (afterOta || (pi.lastUpdateTime >= lastScanTime)) { |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 3327 | if (DEBUG) { |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3328 | Slog.d(TAG, "Found updated package " + pi.packageName |
| 3329 | + " updateTime=" + pi.lastUpdateTime); |
Makoto Onuki | 6dd9fb7 | 2016-06-01 13:55:54 -0700 | [diff] [blame] | 3330 | } |
| 3331 | callback.accept(pi.applicationInfo); |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3332 | } |
| 3333 | } |
| 3334 | } |
| 3335 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3336 | private boolean isApplicationFlagSet(@NonNull String packageName, int userId, int flags) { |
| 3337 | final ApplicationInfo ai = injectApplicationInfoWithUninstalled(packageName, userId); |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3338 | return (ai != null) && ((ai.flags & flags) == flags); |
| 3339 | } |
| 3340 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3341 | private static boolean isInstalled(@Nullable ApplicationInfo ai) { |
Makoto Onuki | 82fb2eb | 2017-03-31 16:58:26 -0700 | [diff] [blame] | 3342 | return (ai != null) && ai.enabled && (ai.flags & ApplicationInfo.FLAG_INSTALLED) != 0; |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3343 | } |
| 3344 | |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 3345 | private static boolean isEphemeralApp(@Nullable ApplicationInfo ai) { |
Svetoslav Ganov | 096d304 | 2017-01-30 16:34:13 -0800 | [diff] [blame] | 3346 | return (ai != null) && ai.isInstantApp(); |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 3347 | } |
| 3348 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3349 | private static boolean isInstalled(@Nullable PackageInfo pi) { |
| 3350 | return (pi != null) && isInstalled(pi.applicationInfo); |
| 3351 | } |
| 3352 | |
| 3353 | private static boolean isInstalled(@Nullable ActivityInfo ai) { |
| 3354 | return (ai != null) && isInstalled(ai.applicationInfo); |
| 3355 | } |
| 3356 | |
| 3357 | private static ApplicationInfo isInstalledOrNull(ApplicationInfo ai) { |
| 3358 | return isInstalled(ai) ? ai : null; |
| 3359 | } |
| 3360 | |
| 3361 | private static PackageInfo isInstalledOrNull(PackageInfo pi) { |
| 3362 | return isInstalled(pi) ? pi : null; |
| 3363 | } |
| 3364 | |
| 3365 | private static ActivityInfo isInstalledOrNull(ActivityInfo ai) { |
| 3366 | return isInstalled(ai) ? ai : null; |
| 3367 | } |
| 3368 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3369 | boolean isPackageInstalled(String packageName, int userId) { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3370 | return getApplicationInfo(packageName, userId) != null; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
Makoto Onuki | 66e4a2b | 2017-01-23 11:37:45 -0800 | [diff] [blame] | 3373 | boolean isEphemeralApp(String packageName, int userId) { |
| 3374 | return isEphemeralApp(getApplicationInfo(packageName, userId)); |
| 3375 | } |
| 3376 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3377 | @Nullable |
| 3378 | XmlResourceParser injectXmlMetaData(ActivityInfo activityInfo, String key) { |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 3379 | return activityInfo.loadXmlMetaData(mContext.getPackageManager(), key); |
Makoto Onuki | 39686e8 | 2016-04-13 18:03:00 -0700 | [diff] [blame] | 3380 | } |
| 3381 | |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 3382 | @Nullable |
| 3383 | Resources injectGetResourcesForApplicationAsUser(String packageName, int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3384 | final long start = getStatStartTime(); |
Makoto Onuki | 157b162 | 2016-06-02 16:13:10 -0700 | [diff] [blame] | 3385 | final long token = injectClearCallingIdentity(); |
| 3386 | try { |
| 3387 | return mContext.getPackageManager().getResourcesForApplicationAsUser( |
| 3388 | packageName, userId); |
| 3389 | } catch (NameNotFoundException e) { |
| 3390 | Slog.e(TAG, "Resources for package " + packageName + " not found"); |
| 3391 | return null; |
| 3392 | } finally { |
| 3393 | injectRestoreCallingIdentity(token); |
| 3394 | |
| 3395 | logDurationStat(Stats.GET_APPLICATION_RESOURCES, start); |
| 3396 | } |
| 3397 | } |
| 3398 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3399 | private Intent getMainActivityIntent() { |
| 3400 | final Intent intent = new Intent(Intent.ACTION_MAIN); |
| 3401 | intent.addCategory(LAUNCHER_INTENT_CATEGORY); |
| 3402 | return intent; |
| 3403 | } |
| 3404 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3405 | /** |
| 3406 | * Same as queryIntentActivitiesAsUser, except it makes sure the package is installed, |
| 3407 | * and only returns exported activities. |
| 3408 | */ |
| 3409 | @NonNull |
| 3410 | @VisibleForTesting |
| 3411 | List<ResolveInfo> queryActivities(@NonNull Intent baseIntent, |
| 3412 | @NonNull String packageName, @Nullable ComponentName activity, int userId) { |
| 3413 | |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 3414 | baseIntent.setPackage(Objects.requireNonNull(packageName)); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3415 | if (activity != null) { |
| 3416 | baseIntent.setComponent(activity); |
| 3417 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3418 | return queryActivities(baseIntent, userId, /* exportedOnly =*/ true); |
| 3419 | } |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3420 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3421 | @NonNull |
| 3422 | List<ResolveInfo> queryActivities(@NonNull Intent intent, int userId, |
| 3423 | boolean exportedOnly) { |
| 3424 | final List<ResolveInfo> resolved; |
| 3425 | final long token = injectClearCallingIdentity(); |
| 3426 | try { |
| 3427 | resolved = |
| 3428 | mContext.getPackageManager().queryIntentActivitiesAsUser( |
| 3429 | intent, PACKAGE_MATCH_FLAGS, userId); |
| 3430 | } finally { |
| 3431 | injectRestoreCallingIdentity(token); |
| 3432 | } |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3433 | if (resolved == null || resolved.size() == 0) { |
| 3434 | return EMPTY_RESOLVE_INFO; |
| 3435 | } |
| 3436 | // Make sure the package is installed. |
| 3437 | if (!isInstalled(resolved.get(0).activityInfo)) { |
| 3438 | return EMPTY_RESOLVE_INFO; |
| 3439 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3440 | if (exportedOnly) { |
| 3441 | resolved.removeIf(ACTIVITY_NOT_EXPORTED); |
| 3442 | } |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3443 | return resolved; |
| 3444 | } |
| 3445 | |
| 3446 | /** |
| 3447 | * Return the main activity that is enabled and exported. If multiple activities are found, |
| 3448 | * return the first one. |
| 3449 | */ |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3450 | @Nullable |
| 3451 | ComponentName injectGetDefaultMainActivity(@NonNull String packageName, int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3452 | final long start = getStatStartTime(); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3453 | try { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3454 | final List<ResolveInfo> resolved = |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3455 | queryActivities(getMainActivityIntent(), packageName, null, userId); |
| 3456 | return resolved.size() == 0 ? null : resolved.get(0).activityInfo.getComponentName(); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3457 | } finally { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3458 | logDurationStat(Stats.GET_LAUNCHER_ACTIVITY, start); |
| 3459 | } |
| 3460 | } |
| 3461 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3462 | /** |
| 3463 | * Return whether an activity is enabled, exported and main. |
| 3464 | */ |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3465 | boolean injectIsMainActivity(@NonNull ComponentName activity, int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3466 | final long start = getStatStartTime(); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3467 | try { |
Makoto Onuki | 3414553 | 2017-03-14 17:58:36 -0700 | [diff] [blame] | 3468 | if (activity == null) { |
| 3469 | wtf("null activity detected"); |
| 3470 | return false; |
| 3471 | } |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3472 | if (DUMMY_MAIN_ACTIVITY.equals(activity.getClassName())) { |
| 3473 | return true; |
| 3474 | } |
| 3475 | final List<ResolveInfo> resolved = queryActivities( |
| 3476 | getMainActivityIntent(), activity.getPackageName(), activity, userId); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3477 | return resolved.size() > 0; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3478 | } finally { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3479 | logDurationStat(Stats.CHECK_LAUNCHER_ACTIVITY, start); |
| 3480 | } |
| 3481 | } |
| 3482 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3483 | /** |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3484 | * Create a dummy "main activity" component name which is used to create a dynamic shortcut |
| 3485 | * with no main activity temporarily. |
| 3486 | */ |
| 3487 | @NonNull |
| 3488 | ComponentName getDummyMainActivity(@NonNull String packageName) { |
| 3489 | return new ComponentName(packageName, DUMMY_MAIN_ACTIVITY); |
| 3490 | } |
| 3491 | |
Makoto Onuki | 255461f | 2017-01-10 11:47:25 -0800 | [diff] [blame] | 3492 | boolean isDummyMainActivity(@Nullable ComponentName name) { |
| 3493 | return name != null && DUMMY_MAIN_ACTIVITY.equals(name.getClassName()); |
| 3494 | } |
| 3495 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3496 | /** |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3497 | * Return all the enabled, exported and main activities from a package. |
| 3498 | */ |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3499 | @NonNull |
| 3500 | List<ResolveInfo> injectGetMainActivities(@NonNull String packageName, int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3501 | final long start = getStatStartTime(); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3502 | try { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3503 | return queryActivities(getMainActivityIntent(), packageName, null, userId); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3504 | } finally { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3505 | logDurationStat(Stats.CHECK_LAUNCHER_ACTIVITY, start); |
| 3506 | } |
| 3507 | } |
| 3508 | |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3509 | /** |
| 3510 | * Return whether an activity is enabled and exported. |
| 3511 | */ |
| 3512 | @VisibleForTesting |
| 3513 | boolean injectIsActivityEnabledAndExported( |
| 3514 | @NonNull ComponentName activity, @UserIdInt int userId) { |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3515 | final long start = getStatStartTime(); |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3516 | try { |
| 3517 | return queryActivities(new Intent(), activity.getPackageName(), activity, userId) |
| 3518 | .size() > 0; |
| 3519 | } finally { |
Makoto Onuki | ee6b6e4 | 2016-06-29 17:34:02 -0700 | [diff] [blame] | 3520 | logDurationStat(Stats.IS_ACTIVITY_ENABLED, start); |
| 3521 | } |
| 3522 | } |
| 3523 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3524 | /** |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 3525 | * Get the {@link LauncherApps#ACTION_CONFIRM_PIN_SHORTCUT} or |
| 3526 | * {@link LauncherApps#ACTION_CONFIRM_PIN_APPWIDGET} activity in a given package depending on |
| 3527 | * the requestType. |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3528 | */ |
| 3529 | @Nullable |
| 3530 | ComponentName injectGetPinConfirmationActivity(@NonNull String launcherPackageName, |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 3531 | int launcherUserId, int requestType) { |
Daulet Zhanguzin | 82adfcb | 2020-01-02 17:31:40 +0000 | [diff] [blame] | 3532 | Objects.requireNonNull(launcherPackageName); |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 3533 | String action = requestType == LauncherApps.PinItemRequest.REQUEST_TYPE_SHORTCUT ? |
| 3534 | LauncherApps.ACTION_CONFIRM_PIN_SHORTCUT : |
| 3535 | LauncherApps.ACTION_CONFIRM_PIN_APPWIDGET; |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3536 | |
Sunny Goyal | 7f7372a | 2017-01-24 11:53:54 -0800 | [diff] [blame] | 3537 | final Intent confirmIntent = new Intent(action).setPackage(launcherPackageName); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3538 | final List<ResolveInfo> candidates = queryActivities( |
| 3539 | confirmIntent, launcherUserId, /* exportedOnly =*/ false); |
| 3540 | for (ResolveInfo ri : candidates) { |
| 3541 | return ri.activityInfo.getComponentName(); |
| 3542 | } |
| 3543 | return null; |
| 3544 | } |
| 3545 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3546 | boolean injectIsSafeModeEnabled() { |
| 3547 | final long token = injectClearCallingIdentity(); |
| 3548 | try { |
| 3549 | return IWindowManager.Stub |
| 3550 | .asInterface(ServiceManager.getService(Context.WINDOW_SERVICE)) |
| 3551 | .isSafeModeEnabled(); |
| 3552 | } catch (RemoteException e) { |
| 3553 | return false; // Shouldn't happen though. |
| 3554 | } finally { |
| 3555 | injectRestoreCallingIdentity(token); |
| 3556 | } |
| 3557 | } |
| 3558 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3559 | /** |
| 3560 | * If {@code userId} is of a managed profile, return the parent user ID. Otherwise return |
| 3561 | * itself. |
| 3562 | */ |
| 3563 | int getParentOrSelfUserId(int userId) { |
Sunny Goyal | 145c8f8 | 2018-02-15 14:27:09 -0800 | [diff] [blame] | 3564 | return mUserManagerInternal.getProfileParentId(userId); |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3565 | } |
| 3566 | |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 3567 | void injectSendIntentSender(IntentSender intentSender, Intent extras) { |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3568 | if (intentSender == null) { |
| 3569 | return; |
| 3570 | } |
| 3571 | try { |
Sunny Goyal | 87a563e | 2017-01-01 19:42:45 -0800 | [diff] [blame] | 3572 | intentSender.sendIntent(mContext, /* code= */ 0, extras, |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 3573 | /* onFinished=*/ null, /* handler= */ null); |
| 3574 | } catch (SendIntentException e) { |
| 3575 | Slog.w(TAG, "sendIntent failed().", e); |
| 3576 | } |
| 3577 | } |
| 3578 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3579 | // === Backup & restore === |
| 3580 | |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3581 | boolean shouldBackupApp(String packageName, int userId) { |
Makoto Onuki | 905e885 | 2016-03-28 10:40:58 -0700 | [diff] [blame] | 3582 | return isApplicationFlagSet(packageName, userId, ApplicationInfo.FLAG_ALLOW_BACKUP); |
Makoto Onuki | 0acbb14 | 2016-03-22 17:02:57 -0700 | [diff] [blame] | 3583 | } |
| 3584 | |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 3585 | static boolean shouldBackupApp(PackageInfo pi) { |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3586 | return (pi.applicationInfo.flags & ApplicationInfo.FLAG_ALLOW_BACKUP) != 0; |
| 3587 | } |
| 3588 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3589 | @Override |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3590 | public byte[] getBackupPayload(@UserIdInt int userId) { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3591 | enforceSystem(); |
| 3592 | if (DEBUG) { |
| 3593 | Slog.d(TAG, "Backing up user " + userId); |
| 3594 | } |
| 3595 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 3596 | if (!isUserUnlockedL(userId)) { |
| 3597 | wtf("Can't backup: user " + userId + " is locked or not running"); |
| 3598 | return null; |
| 3599 | } |
| 3600 | |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3601 | final ShortcutUser user = getUserShortcutsLocked(userId); |
| 3602 | if (user == null) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 3603 | wtf("Can't backup: user not found: id=" + userId); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3604 | return null; |
| 3605 | } |
| 3606 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 3607 | // Update the signatures for all packages. |
| 3608 | user.forAllPackageItems(spi -> spi.refreshPackageSignatureAndSave()); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3609 | |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 3610 | // Rescan all apps; this will also update the version codes and "allow-backup". |
| 3611 | user.forAllPackages(pkg -> pkg.rescanPackageIfNeeded( |
| 3612 | /*isNewApp=*/ false, /*forceRescan=*/ true)); |
| 3613 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 3614 | // Set the version code for the launchers. |
Makoto Onuki | a4f89b1 | 2017-10-05 10:37:55 -0700 | [diff] [blame] | 3615 | user.forAllLaunchers(launcher -> launcher.ensurePackageInfo()); |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 3616 | |
| 3617 | // Save to the filesystem. |
| 3618 | scheduleSaveUser(userId); |
| 3619 | saveDirtyInfo(); |
| 3620 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 3621 | // Note, in case of backup, we don't have to wait on bitmap saving, because we don't |
| 3622 | // back up bitmaps anyway. |
| 3623 | |
Makoto Onuki | c8c3329 | 2016-09-12 16:36:59 -0700 | [diff] [blame] | 3624 | // Then create the backup payload. |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3625 | final ByteArrayOutputStream os = new ByteArrayOutputStream(32 * 1024); |
| 3626 | try { |
| 3627 | saveUserInternalLocked(userId, os, /* forBackup */ true); |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 3628 | } catch (XmlPullParserException | IOException e) { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3629 | // Shouldn't happen. |
| 3630 | Slog.w(TAG, "Backup failed.", e); |
| 3631 | return null; |
| 3632 | } |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 3633 | byte[] payload = os.toByteArray(); |
| 3634 | mShortcutDumpFiles.save("backup-1-payload.txt", payload); |
| 3635 | return payload; |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3636 | } |
| 3637 | } |
| 3638 | |
| 3639 | @Override |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3640 | public void applyRestore(byte[] payload, @UserIdInt int userId) { |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3641 | enforceSystem(); |
| 3642 | if (DEBUG) { |
| 3643 | Slog.d(TAG, "Restoring user " + userId); |
| 3644 | } |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3645 | synchronized (mLock) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 3646 | if (!isUserUnlockedL(userId)) { |
| 3647 | wtf("Can't restore: user " + userId + " is locked or not running"); |
| 3648 | return; |
| 3649 | } |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3650 | |
| 3651 | // Note we print the file timestamps in dumpsys too, but also printing the timestamp |
| 3652 | // in the files anyway. |
| 3653 | mShortcutDumpFiles.save("restore-0-start.txt", pw -> { |
| 3654 | pw.print("Start time: "); |
| 3655 | dumpCurrentTime(pw); |
| 3656 | pw.println(); |
| 3657 | }); |
| 3658 | mShortcutDumpFiles.save("restore-1-payload.xml", payload); |
| 3659 | |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 3660 | // Actually do restore. |
| 3661 | final ShortcutUser restored; |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 3662 | final ByteArrayInputStream is = new ByteArrayInputStream(payload); |
| 3663 | try { |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 3664 | restored = loadUserInternal(userId, is, /* fromBackup */ true); |
| 3665 | } catch (XmlPullParserException | IOException | InvalidFileFormatException e) { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 3666 | Slog.w(TAG, "Restoration failed.", e); |
| 3667 | return; |
| 3668 | } |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3669 | mShortcutDumpFiles.save("restore-2.txt", this::dumpInner); |
| 3670 | |
Makoto Onuki | fc4cf2d | 2016-08-24 11:10:26 -0700 | [diff] [blame] | 3671 | getUserShortcutsLocked(userId).mergeRestoredFile(restored); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3672 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3673 | mShortcutDumpFiles.save("restore-3.txt", this::dumpInner); |
| 3674 | |
Makoto Onuki | 377b797 | 2016-08-09 14:43:55 -0700 | [diff] [blame] | 3675 | // Rescan all packages to re-publish manifest shortcuts and do other checks. |
| 3676 | rescanUpdatedPackagesLocked(userId, |
Makoto Onuki | 248a0ef | 2016-11-03 15:59:01 -0700 | [diff] [blame] | 3677 | 0 // lastScanTime = 0; rescan all packages. |
| 3678 | ); |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3679 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3680 | mShortcutDumpFiles.save("restore-4.txt", this::dumpInner); |
| 3681 | |
| 3682 | mShortcutDumpFiles.save("restore-5-finish.txt", pw -> { |
| 3683 | pw.print("Finish time: "); |
| 3684 | dumpCurrentTime(pw); |
| 3685 | pw.println(); |
| 3686 | }); |
| 3687 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3688 | saveUserLocked(userId); |
Makoto Onuki | 9da23fc | 2016-03-29 11:14:42 -0700 | [diff] [blame] | 3689 | } |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 3690 | } |
| 3691 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3692 | // === Dump === |
| 3693 | |
| 3694 | @Override |
| 3695 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 3696 | if (!DumpUtils.checkDumpAndUsageStatsPermission(mContext, TAG, pw)) return; |
Makoto Onuki | c4361e3 | 2017-04-03 11:24:25 -0700 | [diff] [blame] | 3697 | dumpNoCheck(fd, pw, args); |
| 3698 | } |
| 3699 | |
| 3700 | @VisibleForTesting |
| 3701 | void dumpNoCheck(FileDescriptor fd, PrintWriter pw, String[] args) { |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3702 | final DumpFilter filter = parseDumpArgs(args); |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3703 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3704 | if (filter.shouldDumpCheckIn()) { |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3705 | // Other flags are not supported for checkin. |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3706 | dumpCheckin(pw, filter.shouldCheckInClear()); |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 3707 | } else { |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3708 | if (filter.shouldDumpMain()) { |
| 3709 | dumpInner(pw, filter); |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3710 | pw.println(); |
| 3711 | } |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3712 | if (filter.shouldDumpUid()) { |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3713 | dumpUid(pw); |
| 3714 | pw.println(); |
| 3715 | } |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3716 | if (filter.shouldDumpFiles()) { |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3717 | dumpDumpFiles(pw); |
| 3718 | pw.println(); |
| 3719 | } |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 3720 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3721 | } |
| 3722 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3723 | private static DumpFilter parseDumpArgs(String[] args) { |
| 3724 | final DumpFilter filter = new DumpFilter(); |
| 3725 | if (args == null) { |
| 3726 | return filter; |
| 3727 | } |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 3728 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3729 | int argIndex = 0; |
| 3730 | while (argIndex < args.length) { |
| 3731 | final String arg = args[argIndex++]; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3732 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3733 | if ("-c".equals(arg)) { |
| 3734 | filter.setDumpCheckIn(true); |
| 3735 | continue; |
| 3736 | } |
| 3737 | if ("--checkin".equals(arg)) { |
| 3738 | filter.setDumpCheckIn(true); |
| 3739 | filter.setCheckInClear(true); |
| 3740 | continue; |
| 3741 | } |
| 3742 | if ("-a".equals(arg) || "--all".equals(arg)) { |
| 3743 | filter.setDumpUid(true); |
| 3744 | filter.setDumpFiles(true); |
| 3745 | continue; |
| 3746 | } |
| 3747 | if ("-u".equals(arg) || "--uid".equals(arg)) { |
| 3748 | filter.setDumpUid(true); |
| 3749 | continue; |
| 3750 | } |
| 3751 | if ("-f".equals(arg) || "--files".equals(arg)) { |
| 3752 | filter.setDumpFiles(true); |
| 3753 | continue; |
| 3754 | } |
| 3755 | if ("-n".equals(arg) || "--no-main".equals(arg)) { |
| 3756 | filter.setDumpMain(false); |
| 3757 | continue; |
| 3758 | } |
| 3759 | if ("--user".equals(arg)) { |
| 3760 | if (argIndex >= args.length) { |
| 3761 | throw new IllegalArgumentException("Missing user ID for --user"); |
| 3762 | } |
| 3763 | try { |
| 3764 | filter.addUser(Integer.parseInt(args[argIndex++])); |
| 3765 | } catch (NumberFormatException e) { |
| 3766 | throw new IllegalArgumentException("Invalid user ID", e); |
| 3767 | } |
| 3768 | continue; |
| 3769 | } |
| 3770 | if ("-p".equals(arg) || "--package".equals(arg)) { |
| 3771 | if (argIndex >= args.length) { |
| 3772 | throw new IllegalArgumentException("Missing package name for --package"); |
| 3773 | } |
| 3774 | filter.addPackageRegex(args[argIndex++]); |
| 3775 | filter.setDumpDetails(false); |
| 3776 | continue; |
| 3777 | } |
| 3778 | if (arg.startsWith("-")) { |
| 3779 | throw new IllegalArgumentException("Unknown option " + arg); |
| 3780 | } |
| 3781 | break; |
| 3782 | } |
| 3783 | while (argIndex < args.length) { |
| 3784 | filter.addPackage(args[argIndex++]); |
| 3785 | } |
| 3786 | return filter; |
| 3787 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3788 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3789 | static class DumpFilter { |
| 3790 | private boolean mDumpCheckIn = false; |
| 3791 | private boolean mCheckInClear = false; |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 3792 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3793 | private boolean mDumpMain = true; |
| 3794 | private boolean mDumpUid = false; |
| 3795 | private boolean mDumpFiles = false; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 3796 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3797 | private boolean mDumpDetails = true; |
| 3798 | private List<Pattern> mPackagePatterns = new ArrayList<>(); |
| 3799 | private List<Integer> mUsers = new ArrayList<>(); |
| 3800 | |
| 3801 | void addPackageRegex(String regex) { |
| 3802 | mPackagePatterns.add(Pattern.compile(regex)); |
| 3803 | } |
| 3804 | |
| 3805 | public void addPackage(String packageName) { |
| 3806 | addPackageRegex(Pattern.quote(packageName)); |
| 3807 | } |
| 3808 | |
| 3809 | void addUser(int userId) { |
| 3810 | mUsers.add(userId); |
| 3811 | } |
| 3812 | |
| 3813 | boolean isPackageMatch(String packageName) { |
| 3814 | if (mPackagePatterns.size() == 0) { |
| 3815 | return true; |
| 3816 | } |
| 3817 | for (int i = 0; i < mPackagePatterns.size(); i++) { |
| 3818 | if (mPackagePatterns.get(i).matcher(packageName).find()) { |
| 3819 | return true; |
Makoto Onuki | 085a05c | 2016-08-19 11:39:29 -0700 | [diff] [blame] | 3820 | } |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 3821 | } |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3822 | return false; |
| 3823 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3824 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3825 | boolean isUserMatch(int userId) { |
| 3826 | if (mUsers.size() == 0) { |
| 3827 | return true; |
| 3828 | } |
| 3829 | for (int i = 0; i < mUsers.size(); i++) { |
| 3830 | if (mUsers.get(i) == userId) { |
| 3831 | return true; |
| 3832 | } |
| 3833 | } |
| 3834 | return false; |
| 3835 | } |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 3836 | |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3837 | public boolean shouldDumpCheckIn() { |
| 3838 | return mDumpCheckIn; |
| 3839 | } |
| 3840 | |
| 3841 | public void setDumpCheckIn(boolean dumpCheckIn) { |
| 3842 | mDumpCheckIn = dumpCheckIn; |
| 3843 | } |
| 3844 | |
| 3845 | public boolean shouldCheckInClear() { |
| 3846 | return mCheckInClear; |
| 3847 | } |
| 3848 | |
| 3849 | public void setCheckInClear(boolean checkInClear) { |
| 3850 | mCheckInClear = checkInClear; |
| 3851 | } |
| 3852 | |
| 3853 | public boolean shouldDumpMain() { |
| 3854 | return mDumpMain; |
| 3855 | } |
| 3856 | |
| 3857 | public void setDumpMain(boolean dumpMain) { |
| 3858 | mDumpMain = dumpMain; |
| 3859 | } |
| 3860 | |
| 3861 | public boolean shouldDumpUid() { |
| 3862 | return mDumpUid; |
| 3863 | } |
| 3864 | |
| 3865 | public void setDumpUid(boolean dumpUid) { |
| 3866 | mDumpUid = dumpUid; |
| 3867 | } |
| 3868 | |
| 3869 | public boolean shouldDumpFiles() { |
| 3870 | return mDumpFiles; |
| 3871 | } |
| 3872 | |
| 3873 | public void setDumpFiles(boolean dumpFiles) { |
| 3874 | mDumpFiles = dumpFiles; |
| 3875 | } |
| 3876 | |
| 3877 | public boolean shouldDumpDetails() { |
| 3878 | return mDumpDetails; |
| 3879 | } |
| 3880 | |
| 3881 | public void setDumpDetails(boolean dumpDetails) { |
| 3882 | mDumpDetails = dumpDetails; |
| 3883 | } |
| 3884 | } |
| 3885 | |
| 3886 | private void dumpInner(PrintWriter pw) { |
| 3887 | dumpInner(pw, new DumpFilter()); |
| 3888 | } |
| 3889 | |
| 3890 | private void dumpInner(PrintWriter pw, DumpFilter filter) { |
| 3891 | synchronized (mLock) { |
| 3892 | if (filter.shouldDumpDetails()) { |
| 3893 | final long now = injectCurrentTimeMillis(); |
| 3894 | pw.print("Now: ["); |
| 3895 | pw.print(now); |
| 3896 | pw.print("] "); |
| 3897 | pw.print(formatTime(now)); |
| 3898 | |
| 3899 | pw.print(" Raw last reset: ["); |
| 3900 | pw.print(mRawLastResetTime); |
| 3901 | pw.print("] "); |
| 3902 | pw.print(formatTime(mRawLastResetTime)); |
| 3903 | |
| 3904 | final long last = getLastResetTimeLocked(); |
| 3905 | pw.print(" Last reset: ["); |
| 3906 | pw.print(last); |
| 3907 | pw.print("] "); |
| 3908 | pw.print(formatTime(last)); |
| 3909 | |
| 3910 | final long next = getNextResetTimeLocked(); |
| 3911 | pw.print(" Next reset: ["); |
| 3912 | pw.print(next); |
| 3913 | pw.print("] "); |
| 3914 | pw.print(formatTime(next)); |
Makoto Onuki | e3fffa9 | 2018-02-28 16:25:40 -0800 | [diff] [blame] | 3915 | pw.println(); |
| 3916 | pw.println(); |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3917 | |
| 3918 | pw.print(" Config:"); |
| 3919 | pw.print(" Max icon dim: "); |
| 3920 | pw.println(mMaxIconDimension); |
| 3921 | pw.print(" Icon format: "); |
| 3922 | pw.println(mIconPersistFormat); |
| 3923 | pw.print(" Icon quality: "); |
| 3924 | pw.println(mIconPersistQuality); |
| 3925 | pw.print(" saveDelayMillis: "); |
| 3926 | pw.println(mSaveDelayMillis); |
| 3927 | pw.print(" resetInterval: "); |
| 3928 | pw.println(mResetInterval); |
| 3929 | pw.print(" maxUpdatesPerInterval: "); |
| 3930 | pw.println(mMaxUpdatesPerInterval); |
| 3931 | pw.print(" maxShortcutsPerActivity: "); |
| 3932 | pw.println(mMaxShortcuts); |
| 3933 | pw.println(); |
| 3934 | |
Makoto Onuki | 84d5934 | 2018-02-02 09:22:38 -0800 | [diff] [blame] | 3935 | mStatLogger.dump(pw, " "); |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3936 | |
| 3937 | pw.println(); |
| 3938 | pw.print(" #Failures: "); |
| 3939 | pw.println(mWtfCount); |
| 3940 | |
| 3941 | if (mLastWtfStacktrace != null) { |
| 3942 | pw.print(" Last failure stack trace: "); |
| 3943 | pw.println(Log.getStackTraceString(mLastWtfStacktrace)); |
| 3944 | } |
| 3945 | |
| 3946 | pw.println(); |
| 3947 | mShortcutBitmapSaver.dumpLocked(pw, " "); |
| 3948 | |
| 3949 | pw.println(); |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 3950 | } |
| 3951 | |
Makoto Onuki | 3f4b1ca | 2016-03-11 13:44:32 -0800 | [diff] [blame] | 3952 | for (int i = 0; i < mUsers.size(); i++) { |
Makoto Onuki | 20b8221 | 2017-10-04 15:03:50 -0700 | [diff] [blame] | 3953 | final ShortcutUser user = mUsers.valueAt(i); |
| 3954 | if (filter.isUserMatch(user.getUserId())) { |
| 3955 | user.dump(pw, " ", filter); |
| 3956 | pw.println(); |
| 3957 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3958 | } |
Makoto Onuki | 708703b | 2017-12-11 16:38:11 -0800 | [diff] [blame] | 3959 | |
| 3960 | for (int i = 0; i < mShortcutNonPersistentUsers.size(); i++) { |
| 3961 | final ShortcutNonPersistentUser user = mShortcutNonPersistentUsers.valueAt(i); |
| 3962 | if (filter.isUserMatch(user.getUserId())) { |
| 3963 | user.dump(pw, " ", filter); |
| 3964 | pw.println(); |
| 3965 | } |
| 3966 | } |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3967 | } |
| 3968 | } |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 3969 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3970 | private void dumpUid(PrintWriter pw) { |
| 3971 | synchronized (mLock) { |
| 3972 | pw.println("** SHORTCUT MANAGER UID STATES (dumpsys shortcut -n -u)"); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 3973 | |
| 3974 | for (int i = 0; i < mUidState.size(); i++) { |
| 3975 | final int uid = mUidState.keyAt(i); |
| 3976 | final int state = mUidState.valueAt(i); |
| 3977 | pw.print(" UID="); |
| 3978 | pw.print(uid); |
| 3979 | pw.print(" state="); |
| 3980 | pw.print(state); |
| 3981 | if (isProcessStateForeground(state)) { |
| 3982 | pw.print(" [FG]"); |
| 3983 | } |
| 3984 | pw.print(" last FG="); |
| 3985 | pw.print(mUidLastForegroundElapsedTime.get(uid)); |
| 3986 | pw.println(); |
| 3987 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3988 | } |
| 3989 | } |
| 3990 | |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 3991 | static String formatTime(long time) { |
Neil Fuller | 9aa7b74 | 2019-06-17 11:40:35 +0100 | [diff] [blame] | 3992 | return TimeMigrationUtils.formatMillisWithFixedFormat(time); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 3993 | } |
| 3994 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 3995 | private void dumpCurrentTime(PrintWriter pw) { |
| 3996 | pw.print(formatTime(injectCurrentTimeMillis())); |
| 3997 | } |
| 3998 | |
Makoto Onuki | 7626992 | 2016-07-15 14:58:54 -0700 | [diff] [blame] | 3999 | /** |
| 4000 | * Dumpsys for checkin. |
| 4001 | * |
| 4002 | * @param clear if true, clear the history information. Some other system services have this |
| 4003 | * behavior but shortcut service doesn't for now. |
| 4004 | */ |
| 4005 | private void dumpCheckin(PrintWriter pw, boolean clear) { |
| 4006 | synchronized (mLock) { |
| 4007 | try { |
| 4008 | final JSONArray users = new JSONArray(); |
| 4009 | |
| 4010 | for (int i = 0; i < mUsers.size(); i++) { |
| 4011 | users.put(mUsers.valueAt(i).dumpCheckin(clear)); |
| 4012 | } |
| 4013 | |
| 4014 | final JSONObject result = new JSONObject(); |
| 4015 | |
| 4016 | result.put(KEY_SHORTCUT, users); |
| 4017 | result.put(KEY_LOW_RAM, injectIsLowRamDevice()); |
| 4018 | result.put(KEY_ICON_SIZE, mMaxIconDimension); |
| 4019 | |
| 4020 | pw.println(result.toString(1)); |
| 4021 | } catch (JSONException e) { |
| 4022 | Slog.e(TAG, "Unable to write in json", e); |
| 4023 | } |
| 4024 | } |
| 4025 | } |
| 4026 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 4027 | private void dumpDumpFiles(PrintWriter pw) { |
| 4028 | synchronized (mLock) { |
| 4029 | pw.println("** SHORTCUT MANAGER FILES (dumpsys shortcut -n -f)"); |
| 4030 | mShortcutDumpFiles.dumpAll(pw); |
| 4031 | } |
| 4032 | } |
| 4033 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4034 | // === Shell support === |
| 4035 | |
| 4036 | @Override |
| 4037 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 4038 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4039 | |
| 4040 | enforceShell(); |
| 4041 | |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 4042 | final long token = injectClearCallingIdentity(); |
| 4043 | try { |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 4044 | final int status = (new MyShellCommand()).exec(this, in, out, err, args, callback, |
| 4045 | resultReceiver); |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 4046 | resultReceiver.send(status, null); |
| 4047 | } finally { |
| 4048 | injectRestoreCallingIdentity(token); |
| 4049 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4050 | } |
| 4051 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4052 | static class CommandException extends Exception { |
| 4053 | public CommandException(String message) { |
| 4054 | super(message); |
| 4055 | } |
| 4056 | } |
| 4057 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4058 | /** |
| 4059 | * Handle "adb shell cmd". |
| 4060 | */ |
| 4061 | private class MyShellCommand extends ShellCommand { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4062 | |
| 4063 | private int mUserId = UserHandle.USER_SYSTEM; |
| 4064 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4065 | private void parseOptionsLocked(boolean takeUser) |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4066 | throws CommandException { |
| 4067 | String opt; |
| 4068 | while ((opt = getNextOption()) != null) { |
| 4069 | switch (opt) { |
| 4070 | case "--user": |
| 4071 | if (takeUser) { |
| 4072 | mUserId = UserHandle.parseUserArg(getNextArgRequired()); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4073 | if (!isUserUnlockedL(mUserId)) { |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 4074 | throw new CommandException( |
| 4075 | "User " + mUserId + " is not running or locked"); |
| 4076 | } |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4077 | break; |
| 4078 | } |
| 4079 | // fallthrough |
| 4080 | default: |
| 4081 | throw new CommandException("Unknown option: " + opt); |
| 4082 | } |
| 4083 | } |
| 4084 | } |
| 4085 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4086 | @Override |
| 4087 | public int onCommand(String cmd) { |
| 4088 | if (cmd == null) { |
| 4089 | return handleDefaultCommands(cmd); |
| 4090 | } |
| 4091 | final PrintWriter pw = getOutPrintWriter(); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4092 | try { |
| 4093 | switch (cmd) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4094 | case "reset-throttling": |
| 4095 | handleResetThrottling(); |
| 4096 | break; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4097 | case "reset-all-throttling": |
| 4098 | handleResetAllThrottling(); |
| 4099 | break; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4100 | case "override-config": |
| 4101 | handleOverrideConfig(); |
| 4102 | break; |
| 4103 | case "reset-config": |
| 4104 | handleResetConfig(); |
| 4105 | break; |
| 4106 | case "clear-default-launcher": |
| 4107 | handleClearDefaultLauncher(); |
| 4108 | break; |
| 4109 | case "get-default-launcher": |
| 4110 | handleGetDefaultLauncher(); |
| 4111 | break; |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 4112 | case "unload-user": |
| 4113 | handleUnloadUser(); |
| 4114 | break; |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4115 | case "clear-shortcuts": |
| 4116 | handleClearShortcuts(); |
| 4117 | break; |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 4118 | case "verify-states": // hidden command to verify various internal states. |
| 4119 | handleVerifyStates(); |
| 4120 | break; |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4121 | default: |
| 4122 | return handleDefaultCommands(cmd); |
| 4123 | } |
| 4124 | } catch (CommandException e) { |
| 4125 | pw.println("Error: " + e.getMessage()); |
| 4126 | return 1; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4127 | } |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4128 | pw.println("Success"); |
| 4129 | return 0; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4130 | } |
| 4131 | |
| 4132 | @Override |
| 4133 | public void onHelp() { |
| 4134 | final PrintWriter pw = getOutPrintWriter(); |
| 4135 | pw.println("Usage: cmd shortcut COMMAND [options ...]"); |
| 4136 | pw.println(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4137 | pw.println("cmd shortcut reset-throttling [--user USER_ID]"); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4138 | pw.println(" Reset throttling for all packages and users"); |
| 4139 | pw.println(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4140 | pw.println("cmd shortcut reset-all-throttling"); |
| 4141 | pw.println(" Reset the throttling state for all users"); |
| 4142 | pw.println(); |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4143 | pw.println("cmd shortcut override-config CONFIG"); |
| 4144 | pw.println(" Override the configuration for testing (will last until reboot)"); |
| 4145 | pw.println(); |
| 4146 | pw.println("cmd shortcut reset-config"); |
| 4147 | pw.println(" Reset the configuration set with \"update-config\""); |
| 4148 | pw.println(); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4149 | pw.println("cmd shortcut clear-default-launcher [--user USER_ID]"); |
| 4150 | pw.println(" Clear the cached default launcher"); |
| 4151 | pw.println(); |
| 4152 | pw.println("cmd shortcut get-default-launcher [--user USER_ID]"); |
Makoto Onuki | 0b9d1db | 2016-07-18 14:16:41 -0700 | [diff] [blame] | 4153 | pw.println(" Show the default launcher"); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4154 | pw.println(); |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 4155 | pw.println("cmd shortcut unload-user [--user USER_ID]"); |
| 4156 | pw.println(" Unload a user from the memory"); |
| 4157 | pw.println(" (This should not affect any observable behavior)"); |
| 4158 | pw.println(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4159 | pw.println("cmd shortcut clear-shortcuts [--user USER_ID] PACKAGE"); |
| 4160 | pw.println(" Remove all shortcuts from a package, including pinned shortcuts"); |
| 4161 | pw.println(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4162 | } |
| 4163 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4164 | private void handleResetThrottling() throws CommandException { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4165 | synchronized (mLock) { |
| 4166 | parseOptionsLocked(/* takeUser =*/ true); |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 4167 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4168 | Slog.i(TAG, "cmd: handleResetThrottling: user=" + mUserId); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4169 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4170 | resetThrottlingInner(mUserId); |
| 4171 | } |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | private void handleResetAllThrottling() { |
| 4175 | Slog.i(TAG, "cmd: handleResetAllThrottling"); |
| 4176 | |
| 4177 | resetAllThrottlingInner(); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4178 | } |
| 4179 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4180 | private void handleOverrideConfig() throws CommandException { |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4181 | final String config = getNextArgRequired(); |
| 4182 | |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4183 | Slog.i(TAG, "cmd: handleOverrideConfig: " + config); |
| 4184 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4185 | synchronized (mLock) { |
| 4186 | if (!updateConfigurationLocked(config)) { |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4187 | throw new CommandException("override-config failed. See logcat for details."); |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4188 | } |
| 4189 | } |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4190 | } |
| 4191 | |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4192 | private void handleResetConfig() { |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4193 | Slog.i(TAG, "cmd: handleResetConfig"); |
| 4194 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4195 | synchronized (mLock) { |
| 4196 | loadConfigurationLocked(); |
| 4197 | } |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4198 | } |
| 4199 | |
| 4200 | private void clearLauncher() { |
| 4201 | synchronized (mLock) { |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 4202 | getUserShortcutsLocked(mUserId).forceClearLauncher(); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4203 | } |
| 4204 | } |
| 4205 | |
| 4206 | private void showLauncher() { |
| 4207 | synchronized (mLock) { |
| 4208 | // This ensures to set the cached launcher. Package name doesn't matter. |
| 4209 | hasShortcutHostPermissionInner("-", mUserId); |
| 4210 | |
| 4211 | getOutPrintWriter().println("Launcher: " |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 4212 | + getUserShortcutsLocked(mUserId).getLastKnownLauncher()); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | private void handleClearDefaultLauncher() throws CommandException { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4217 | synchronized (mLock) { |
| 4218 | parseOptionsLocked(/* takeUser =*/ true); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4219 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4220 | clearLauncher(); |
| 4221 | } |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | private void handleGetDefaultLauncher() throws CommandException { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4225 | synchronized (mLock) { |
| 4226 | parseOptionsLocked(/* takeUser =*/ true); |
Makoto Onuki | 2d5b465 | 2016-03-11 16:09:54 -0800 | [diff] [blame] | 4227 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4228 | clearLauncher(); |
| 4229 | showLauncher(); |
| 4230 | } |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4231 | } |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 4232 | |
| 4233 | private void handleUnloadUser() throws CommandException { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4234 | synchronized (mLock) { |
| 4235 | parseOptionsLocked(/* takeUser =*/ true); |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 4236 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4237 | Slog.i(TAG, "cmd: handleUnloadUser: user=" + mUserId); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4238 | |
Makoto Onuki | 01ce92b | 2017-04-28 12:24:16 -0700 | [diff] [blame] | 4239 | ShortcutService.this.handleStopUser(mUserId); |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4240 | } |
Makoto Onuki | ac21497 | 2016-04-04 10:19:45 -0700 | [diff] [blame] | 4241 | } |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4242 | |
| 4243 | private void handleClearShortcuts() throws CommandException { |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4244 | synchronized (mLock) { |
| 4245 | parseOptionsLocked(/* takeUser =*/ true); |
| 4246 | final String packageName = getNextArgRequired(); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4247 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4248 | Slog.i(TAG, "cmd: handleClearShortcuts: user" + mUserId + ", " + packageName); |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4249 | |
Makoto Onuki | 02f338e | 2016-07-29 09:40:40 -0700 | [diff] [blame] | 4250 | ShortcutService.this.cleanUpPackageForAllLoadedUsers(packageName, mUserId, |
| 4251 | /* appStillExists = */ true); |
| 4252 | } |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | private void handleVerifyStates() throws CommandException { |
| 4256 | try { |
| 4257 | verifyStatesForce(); // This will throw when there's an issue. |
| 4258 | } catch (Throwable th) { |
| 4259 | throw new CommandException(th.getMessage() + "\n" + Log.getStackTraceString(th)); |
| 4260 | } |
Makoto Onuki | 5ba0d3e | 2016-04-11 14:03:46 -0700 | [diff] [blame] | 4261 | } |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | // === Unit test support === |
| 4265 | |
| 4266 | // Injection point. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4267 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4268 | long injectCurrentTimeMillis() { |
| 4269 | return System.currentTimeMillis(); |
| 4270 | } |
| 4271 | |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 4272 | @VisibleForTesting |
| 4273 | long injectElapsedRealtime() { |
| 4274 | return SystemClock.elapsedRealtime(); |
| 4275 | } |
| 4276 | |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 4277 | @VisibleForTesting |
| 4278 | long injectUptimeMillis() { |
| 4279 | return SystemClock.uptimeMillis(); |
| 4280 | } |
| 4281 | |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4282 | // Injection point. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4283 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4284 | int injectBinderCallingUid() { |
| 4285 | return getCallingUid(); |
| 4286 | } |
| 4287 | |
Makoto Onuki | 7d0fa81 | 2018-02-21 11:24:43 -0800 | [diff] [blame] | 4288 | @VisibleForTesting |
| 4289 | int injectBinderCallingPid() { |
| 4290 | return getCallingPid(); |
| 4291 | } |
| 4292 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4293 | private int getCallingUserId() { |
Makoto Onuki | 4554d0e | 2016-03-14 15:51:41 -0700 | [diff] [blame] | 4294 | return UserHandle.getUserId(injectBinderCallingUid()); |
| 4295 | } |
| 4296 | |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 4297 | // Injection point. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4298 | @VisibleForTesting |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 4299 | long injectClearCallingIdentity() { |
| 4300 | return Binder.clearCallingIdentity(); |
| 4301 | } |
| 4302 | |
| 4303 | // Injection point. |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4304 | @VisibleForTesting |
Makoto Onuki | 4dbe0de | 2016-03-14 17:31:49 -0700 | [diff] [blame] | 4305 | void injectRestoreCallingIdentity(long token) { |
| 4306 | Binder.restoreCallingIdentity(token); |
| 4307 | } |
| 4308 | |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 4309 | // Injection point. |
Makoto Onuki | 3366328 | 2016-08-22 16:19:04 -0700 | [diff] [blame] | 4310 | String injectBuildFingerprint() { |
| 4311 | return Build.FINGERPRINT; |
| 4312 | } |
| 4313 | |
Makoto Onuki | de66737 | 2016-03-15 14:29:20 -0700 | [diff] [blame] | 4314 | final void wtf(String message) { |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 4315 | wtf(message, /* exception= */ null); |
Makoto Onuki | de66737 | 2016-03-15 14:29:20 -0700 | [diff] [blame] | 4316 | } |
| 4317 | |
Makoto Onuki | 2e210c4 | 2016-03-30 08:30:36 -0700 | [diff] [blame] | 4318 | // Injection point. |
Makoto Onuki | a224183 | 2016-07-06 13:28:37 -0700 | [diff] [blame] | 4319 | void wtf(String message, Throwable e) { |
| 4320 | if (e == null) { |
| 4321 | e = new RuntimeException("Stacktrace"); |
| 4322 | } |
| 4323 | synchronized (mLock) { |
| 4324 | mWtfCount++; |
| 4325 | mLastWtfStacktrace = new Exception("Last failure was logged here:"); |
| 4326 | } |
Makoto Onuki | de66737 | 2016-03-15 14:29:20 -0700 | [diff] [blame] | 4327 | Slog.wtf(TAG, message, e); |
| 4328 | } |
| 4329 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4330 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4331 | File injectSystemDataPath() { |
| 4332 | return Environment.getDataSystemDirectory(); |
| 4333 | } |
| 4334 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4335 | @VisibleForTesting |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4336 | File injectUserDataPath(@UserIdInt int userId) { |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 4337 | return new File(Environment.getDataSystemCeDirectory(userId), DIRECTORY_PER_USER); |
| 4338 | } |
| 4339 | |
Makoto Onuki | 50a320e | 2017-05-31 14:38:42 -0700 | [diff] [blame] | 4340 | public File getDumpPath() { |
| 4341 | return new File(injectUserDataPath(UserHandle.USER_SYSTEM), DIRECTORY_DUMP); |
| 4342 | } |
| 4343 | |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4344 | @VisibleForTesting |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 4345 | boolean injectIsLowRamDevice() { |
| 4346 | return ActivityManager.isLowRamDeviceStatic(); |
| 4347 | } |
| 4348 | |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4349 | @VisibleForTesting |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 4350 | void injectRegisterUidObserver(IUidObserver observer, int which) { |
| 4351 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 4352 | ActivityManager.getService().registerUidObserver(observer, which, |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 4353 | ActivityManager.PROCESS_STATE_UNKNOWN, null); |
Makoto Onuki | 4d36b3a | 2016-04-27 12:00:17 -0700 | [diff] [blame] | 4354 | } catch (RemoteException shouldntHappen) { |
| 4355 | } |
| 4356 | } |
| 4357 | |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 4358 | File getUserBitmapFilePath(@UserIdInt int userId) { |
| 4359 | return new File(injectUserDataPath(userId), DIRECTORY_BITMAPS); |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4360 | } |
| 4361 | |
| 4362 | @VisibleForTesting |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4363 | SparseArray<ShortcutUser> getShortcutsForTest() { |
Makoto Onuki | 3f4b1ca | 2016-03-11 13:44:32 -0800 | [diff] [blame] | 4364 | return mUsers; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4365 | } |
| 4366 | |
| 4367 | @VisibleForTesting |
Makoto Onuki | b5a012f | 2016-06-21 11:13:53 -0700 | [diff] [blame] | 4368 | int getMaxShortcutsForTest() { |
| 4369 | return mMaxShortcuts; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | @VisibleForTesting |
Makoto Onuki | b6d3523 | 2016-04-04 15:57:17 -0700 | [diff] [blame] | 4373 | int getMaxUpdatesPerIntervalForTest() { |
| 4374 | return mMaxUpdatesPerInterval; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | @VisibleForTesting |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4378 | long getResetIntervalForTest() { |
| 4379 | return mResetInterval; |
Makoto Onuki | 5504622 | 2016-03-08 10:49:47 -0800 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | @VisibleForTesting |
Makoto Onuki | 4362a66 | 2016-03-08 18:59:09 -0800 | [diff] [blame] | 4383 | int getMaxIconDimensionForTest() { |
| 4384 | return mMaxIconDimension; |
| 4385 | } |
| 4386 | |
| 4387 | @VisibleForTesting |
| 4388 | CompressFormat getIconPersistFormatForTest() { |
| 4389 | return mIconPersistFormat; |
| 4390 | } |
| 4391 | |
| 4392 | @VisibleForTesting |
| 4393 | int getIconPersistQualityForTest() { |
| 4394 | return mIconPersistQuality; |
Makoto Onuki | 6f7362d9 | 2016-03-04 13:39:41 -0800 | [diff] [blame] | 4395 | } |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 4396 | |
| 4397 | @VisibleForTesting |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 4398 | ShortcutPackage getPackageShortcutForTest(String packageName, int userId) { |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 4399 | synchronized (mLock) { |
Makoto Onuki | 3145924 | 2016-03-22 11:12:18 -0700 | [diff] [blame] | 4400 | final ShortcutUser user = mUsers.get(userId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 4401 | if (user == null) return null; |
| 4402 | |
Makoto Onuki | 22fcc68 | 2016-05-17 14:52:19 -0700 | [diff] [blame] | 4403 | return user.getAllPackagesForTest().get(packageName); |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | @VisibleForTesting |
| 4408 | ShortcutInfo getPackageShortcutForTest(String packageName, String shortcutId, int userId) { |
| 4409 | synchronized (mLock) { |
Makoto Onuki | f34c308 | 2016-07-13 10:25:25 -0700 | [diff] [blame] | 4410 | final ShortcutPackage pkg = getPackageShortcutForTest(packageName, userId); |
Makoto Onuki | cdc78f7 | 2016-03-21 15:47:52 -0700 | [diff] [blame] | 4411 | if (pkg == null) return null; |
| 4412 | |
| 4413 | return pkg.findShortcutById(shortcutId); |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 4414 | } |
| 4415 | } |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 4416 | |
Makoto Onuki | fac592f | 2016-11-21 13:41:32 -0800 | [diff] [blame] | 4417 | @VisibleForTesting |
| 4418 | ShortcutLauncher getLauncherShortcutForTest(String packageName, int userId) { |
| 4419 | synchronized (mLock) { |
| 4420 | final ShortcutUser user = mUsers.get(userId); |
| 4421 | if (user == null) return null; |
| 4422 | |
| 4423 | return user.getAllLaunchersForTest().get(PackageWithUser.of(userId, packageName)); |
| 4424 | } |
| 4425 | } |
| 4426 | |
Makoto Onuki | 2d895c3 | 2016-12-02 15:48:40 -0800 | [diff] [blame] | 4427 | @VisibleForTesting |
| 4428 | ShortcutRequestPinProcessor getShortcutRequestPinProcessorForTest() { |
| 4429 | return mShortcutRequestPinProcessor; |
| 4430 | } |
| 4431 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 4432 | /** |
| 4433 | * Control whether {@link #verifyStates} should be performed. We always perform it during unit |
| 4434 | * tests. |
| 4435 | */ |
| 4436 | @VisibleForTesting |
| 4437 | boolean injectShouldPerformVerification() { |
| 4438 | return DEBUG; |
| 4439 | } |
| 4440 | |
| 4441 | /** |
| 4442 | * Check various internal states and throws if there's any inconsistency. |
| 4443 | * This is normally only enabled during unit tests. |
| 4444 | */ |
| 4445 | final void verifyStates() { |
| 4446 | if (injectShouldPerformVerification()) { |
| 4447 | verifyStatesInner(); |
| 4448 | } |
| 4449 | } |
| 4450 | |
Makoto Onuki | b08790c | 2016-06-23 14:05:46 -0700 | [diff] [blame] | 4451 | private final void verifyStatesForce() { |
| 4452 | verifyStatesInner(); |
| 4453 | } |
| 4454 | |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 4455 | private void verifyStatesInner() { |
Makoto Onuki | 1030520 | 2016-07-14 18:14:08 -0700 | [diff] [blame] | 4456 | synchronized (mLock) { |
Makoto Onuki | 7001a61 | 2016-05-27 13:24:28 -0700 | [diff] [blame] | 4457 | forEachLoadedUserLocked(u -> u.forAllPackageItems(ShortcutPackageItem::verifyStates)); |
| 4458 | } |
| 4459 | } |
Makoto Onuki | 475c365 | 2017-05-08 14:29:03 -0700 | [diff] [blame] | 4460 | |
| 4461 | @VisibleForTesting |
| 4462 | void waitForBitmapSavesForTest() { |
| 4463 | synchronized (mLock) { |
| 4464 | mShortcutBitmapSaver.waitForAllSavesLocked(); |
| 4465 | } |
| 4466 | } |
Makoto Onuki | 41066a6 | 2016-03-09 16:18:44 -0800 | [diff] [blame] | 4467 | } |