The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.content; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 20 | import android.accounts.AccountAndUser; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 21 | import android.accounts.AccountManager; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 22 | import android.accounts.AccountManagerInternal; |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 23 | import android.annotation.NonNull; |
| 24 | import android.annotation.UserIdInt; |
Fred Quintana | 33e4469 | 2011-12-05 15:04:16 -0800 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 26 | import android.app.AppGlobals; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 27 | import android.app.Notification; |
| 28 | import android.app.NotificationManager; |
| 29 | import android.app.PendingIntent; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 30 | import android.app.job.JobInfo; |
| 31 | import android.app.job.JobScheduler; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 32 | import android.app.usage.UsageStatsManagerInternal; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 33 | import android.content.BroadcastReceiver; |
| 34 | import android.content.ComponentName; |
| 35 | import android.content.ContentResolver; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 36 | import android.content.ContentResolver.SyncExemption; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 37 | import android.content.Context; |
| 38 | import android.content.ISyncAdapter; |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 39 | import android.content.ISyncAdapterUnsyncableAccountCallback; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 40 | import android.content.ISyncContext; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 41 | import android.content.Intent; |
| 42 | import android.content.IntentFilter; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 43 | import android.content.PeriodicSync; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 44 | import android.content.ServiceConnection; |
| 45 | import android.content.SyncActivityTooManyDeletes; |
| 46 | import android.content.SyncAdapterType; |
| 47 | import android.content.SyncAdaptersCache; |
| 48 | import android.content.SyncInfo; |
| 49 | import android.content.SyncResult; |
| 50 | import android.content.SyncStatusInfo; |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 51 | import android.content.SyncStatusInfo.Stats; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 52 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 53 | import android.content.pm.PackageInfo; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 54 | import android.content.pm.PackageManager; |
Todd Kennedy | 0eb9738 | 2017-10-03 16:57:22 -0700 | [diff] [blame] | 55 | import android.content.pm.PackageManager.NameNotFoundException; |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 56 | import android.content.pm.PackageManagerInternal; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 57 | import android.content.pm.ProviderInfo; |
| 58 | import android.content.pm.RegisteredServicesCache; |
| 59 | import android.content.pm.RegisteredServicesCacheListener; |
| 60 | import android.content.pm.ResolveInfo; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 61 | import android.content.pm.UserInfo; |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 62 | import android.database.ContentObserver; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 63 | import android.net.ConnectivityManager; |
| 64 | import android.net.NetworkInfo; |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 65 | import android.net.TrafficStats; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 66 | import android.os.BatteryStats; |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 67 | import android.os.Binder; |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 68 | import android.os.Build; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 69 | import android.os.Bundle; |
| 70 | import android.os.Handler; |
Makoto Onuki | e08a5c2 | 2018-04-27 13:28:46 -0700 | [diff] [blame] | 71 | import android.os.HandlerThread; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 72 | import android.os.IBinder; |
| 73 | import android.os.Looper; |
| 74 | import android.os.Message; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 75 | import android.os.Messenger; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 76 | import android.os.PowerManager; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 77 | import android.os.Process; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 78 | import android.os.RemoteCallback; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 79 | import android.os.RemoteException; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 80 | import android.os.ServiceManager; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 81 | import android.os.SystemClock; |
| 82 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 83 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 84 | import android.os.UserManager; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 85 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.util.EventLog; |
| 89 | import android.util.Log; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 90 | import android.util.Pair; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 91 | import android.util.Slog; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 92 | |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 93 | import com.android.internal.R; |
Makoto Onuki | 056a975 | 2018-05-08 15:21:56 -0700 | [diff] [blame] | 94 | import com.android.internal.annotations.GuardedBy; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 95 | import com.android.internal.app.IBatteryStats; |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 96 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
| 97 | import com.android.internal.notification.SystemNotificationChannels; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 98 | import com.android.internal.os.BackgroundThread; |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 99 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 100 | import com.android.internal.util.IndentingPrintWriter; |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 101 | import com.android.internal.util.function.QuadConsumer; |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 102 | import com.android.server.DeviceIdleController; |
| 103 | import com.android.server.LocalServices; |
| 104 | import com.android.server.SystemService; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 105 | import com.android.server.accounts.AccountManagerService; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 106 | import com.android.server.backup.AccountSyncSettingsBackupHelper; |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 107 | import com.android.server.content.SyncStorageEngine.AuthorityInfo; |
Amith Yamasani | 96a0fd65 | 2015-04-10 16:16:30 -0700 | [diff] [blame] | 108 | import com.android.server.content.SyncStorageEngine.EndPoint; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 109 | import com.android.server.content.SyncStorageEngine.OnSyncRequestListener; |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 110 | import com.android.server.job.JobSchedulerInternal; |
| 111 | |
| 112 | import com.google.android.collect.Lists; |
| 113 | import com.google.android.collect.Maps; |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import java.io.FileDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | import java.io.PrintWriter; |
| 117 | import java.util.ArrayList; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 118 | import java.util.Arrays; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 119 | import java.util.Collection; |
| 120 | import java.util.Collections; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 121 | import java.util.Comparator; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 122 | import java.util.HashMap; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 123 | import java.util.HashSet; |
| 124 | import java.util.List; |
| 125 | import java.util.Map; |
Matthew Williams | 9ad2c84 | 2015-10-16 12:01:31 -0700 | [diff] [blame] | 126 | import java.util.Objects; |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 127 | import java.util.Random; |
| 128 | import java.util.Set; |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 129 | import java.util.function.Function; |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 130 | import java.util.function.Predicate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
| 132 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 133 | * Implementation details: |
| 134 | * All scheduled syncs will be passed on to JobScheduler as jobs |
| 135 | * (See {@link #scheduleSyncOperationH(SyncOperation, long)}. This function schedules a job |
| 136 | * with JobScheduler with appropriate delay and constraints (according to backoffs and extras). |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 137 | * The scheduleSyncOperationH function also assigns a unique jobId to each |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 138 | * SyncOperation. |
| 139 | * |
| 140 | * Periodic Syncs: |
| 141 | * Each periodic sync is scheduled as a periodic job. If a periodic sync fails, we create a new |
| 142 | * one off SyncOperation and set its {@link SyncOperation#sourcePeriodicId} field to the jobId of the |
| 143 | * periodic sync. We don't allow the periodic job to run while any job initiated by it is pending. |
| 144 | * |
| 145 | * Backoffs: |
| 146 | * Each {@link EndPoint} has a backoff associated with it. When a SyncOperation fails, we increase |
| 147 | * the backoff on the authority. Then we reschedule all syncs associated with that authority to |
| 148 | * run at a later time. Similarly, when a sync succeeds, backoff is cleared and all associated syncs |
| 149 | * are rescheduled. A rescheduled sync will get a new jobId. |
| 150 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | * @hide |
| 152 | */ |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 153 | public class SyncManager { |
Amith Yamasani | 96a0fd65 | 2015-04-10 16:16:30 -0700 | [diff] [blame] | 154 | static final String TAG = "SyncManager"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 156 | private static final boolean DEBUG_ACCOUNT_ACCESS = false; |
| 157 | |
Makoto Onuki | b47e894 | 2017-09-18 14:03:03 -0700 | [diff] [blame] | 158 | // Only do the check on a debuggable build. |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 159 | private static final boolean ENABLE_SUSPICIOUS_CHECK = Build.IS_DEBUGGABLE; |
| 160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | /** Delay a sync due to local changes this long. In milliseconds */ |
Debajit Ghosh | 44ee0f0 | 2009-09-14 14:58:31 -0700 | [diff] [blame] | 162 | private static final long LOCAL_SYNC_DELAY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
Debajit Ghosh | 44ee0f0 | 2009-09-14 14:58:31 -0700 | [diff] [blame] | 164 | static { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 165 | LOCAL_SYNC_DELAY = |
| 166 | SystemProperties.getLong("sync.local_sync_delay", 30 * 1000 /* 30 seconds */); |
Debajit Ghosh | 44ee0f0 | 2009-09-14 14:58:31 -0700 | [diff] [blame] | 167 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | /** |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 170 | * How long to wait before retrying a sync that failed due to one already being in progress. |
| 171 | */ |
| 172 | private static final int DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS = 10; |
| 173 | |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 174 | /** |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 175 | * How often to periodically poll network traffic for an adapter performing a sync to determine |
| 176 | * whether progress is being made. |
| 177 | */ |
| 178 | private static final long SYNC_MONITOR_WINDOW_LENGTH_MILLIS = 60 * 1000; // 60 seconds |
| 179 | |
| 180 | /** |
| 181 | * How many bytes must be transferred (Tx + Rx) over the period of time defined by |
| 182 | * {@link #SYNC_MONITOR_WINDOW_LENGTH_MILLIS} for the sync to be considered to be making |
| 183 | * progress. |
| 184 | */ |
| 185 | private static final int SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES = 10; // 10 bytes |
| 186 | |
| 187 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 188 | * If a previously scheduled sync becomes ready and we are low on storage, it gets |
| 189 | * pushed back for this amount of time. |
| 190 | */ |
| 191 | private static final long SYNC_DELAY_ON_LOW_STORAGE = 60*60*1000; // 1 hour |
| 192 | |
| 193 | /** |
| 194 | * If a sync becomes ready and it conflicts with an already running sync, it gets |
| 195 | * pushed back for this amount of time. |
| 196 | */ |
| 197 | private static final long SYNC_DELAY_ON_CONFLICT = 10*1000; // 10 seconds |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 198 | |
Shreyas Basarge | fa27253 | 2016-03-09 16:52:48 +0000 | [diff] [blame] | 199 | /** |
| 200 | * Generate job ids in the range [MIN_SYNC_JOB_ID, MAX_SYNC_JOB_ID) to avoid conflicts with |
| 201 | * other jobs scheduled by the system process. |
| 202 | */ |
| 203 | private static final int MIN_SYNC_JOB_ID = 100000; |
| 204 | private static final int MAX_SYNC_JOB_ID = 110000; |
| 205 | |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 206 | private static final String SYNC_WAKE_LOCK_PREFIX = "*sync*/"; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 207 | private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm"; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 208 | private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock"; |
| 209 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 210 | |
| 211 | private static final int SYNC_OP_STATE_VALID = 0; |
| 212 | private static final int SYNC_OP_STATE_INVALID = 1; |
| 213 | private static final int SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS = 2; |
| 214 | |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 215 | /** Flags used when connecting to a sync adapter service */ |
| 216 | private static final int SYNC_ADAPTER_CONNECTION_FLAGS = Context.BIND_AUTO_CREATE |
| 217 | | Context.BIND_NOT_FOREGROUND | Context.BIND_ALLOW_OOM_MANAGEMENT; |
| 218 | |
Makoto Onuki | 056a975 | 2018-05-08 15:21:56 -0700 | [diff] [blame] | 219 | /** Singleton instance. */ |
| 220 | @GuardedBy("SyncManager.class") |
| 221 | private static SyncManager sInstance; |
| 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | private Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 225 | private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0]; |
| 226 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 227 | // TODO: add better locking around mRunningAccounts |
| 228 | private volatile AccountAndUser[] mRunningAccounts = INITIAL_ACCOUNTS_ARRAY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | volatile private PowerManager.WakeLock mHandleAlarmWakeLock; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 231 | volatile private PowerManager.WakeLock mSyncManagerWakeLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | volatile private boolean mDataConnectionIsConnected = false; |
| 233 | volatile private boolean mStorageIsLow = false; |
Dianne Hackborn | 4870e9d | 2015-04-08 16:55:47 -0700 | [diff] [blame] | 234 | volatile private boolean mDeviceIsIdle = false; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 235 | volatile private boolean mReportedSyncActive = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | |
| 237 | private final NotificationManager mNotificationMgr; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 238 | private final IBatteryStats mBatteryStats; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 239 | private JobScheduler mJobScheduler; |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 240 | private JobSchedulerInternal mJobSchedulerInternal; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 241 | private SyncJobService mSyncJobService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 243 | private SyncStorageEngine mSyncStorageEngine; |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 244 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 245 | protected final ArrayList<ActiveSyncContext> mActiveSyncContexts = Lists.newArrayList(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 247 | // Synchronized on "this". Instead of using this directly one should instead call |
| 248 | // its accessor, getConnManager(). |
| 249 | private ConnectivityManager mConnManagerDoNotUseDirectly; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 251 | /** Track whether the device has already been provisioned. */ |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 252 | private volatile boolean mProvisioned; |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 253 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 254 | protected final SyncAdaptersCache mSyncAdapters; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 255 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 256 | private final Random mRand; |
| 257 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 258 | private final SyncLogger mLogger; |
| 259 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 260 | private boolean isJobIdInUseLockedH(int jobId, List<JobInfo> pendingJobs) { |
| 261 | for (JobInfo job: pendingJobs) { |
| 262 | if (job.getId() == jobId) { |
| 263 | return true; |
| 264 | } |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 265 | } |
| 266 | for (ActiveSyncContext asc: mActiveSyncContexts) { |
| 267 | if (asc.mSyncOperation.jobId == jobId) { |
| 268 | return true; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 269 | } |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | private int getUnusedJobIdH() { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 275 | int newJobId; |
| 276 | do { |
| 277 | newJobId = MIN_SYNC_JOB_ID + mRand.nextInt(MAX_SYNC_JOB_ID - MIN_SYNC_JOB_ID); |
| 278 | } while (isJobIdInUseLockedH(newJobId, |
| 279 | mJobSchedulerInternal.getSystemScheduledPendingJobs())); |
| 280 | return newJobId; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 283 | private List<SyncOperation> getAllPendingSyncs() { |
| 284 | verifyJobScheduler(); |
| 285 | List<JobInfo> pendingJobs = mJobSchedulerInternal.getSystemScheduledPendingJobs(); |
| 286 | List<SyncOperation> pendingSyncs = new ArrayList<SyncOperation>(pendingJobs.size()); |
| 287 | for (JobInfo job: pendingJobs) { |
| 288 | SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras()); |
| 289 | if (op != null) { |
| 290 | pendingSyncs.add(op); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 291 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 292 | } |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 293 | return pendingSyncs; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 294 | } |
Amith Yamasani | 96a0fd65 | 2015-04-10 16:16:30 -0700 | [diff] [blame] | 295 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 296 | private final BroadcastReceiver mStorageIntentReceiver = |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | new BroadcastReceiver() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 298 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | public void onReceive(Context context, Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | String action = intent.getAction(); |
| 301 | if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) { |
| 302 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 303 | Slog.v(TAG, "Internal storage is low."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | } |
| 305 | mStorageIsLow = true; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 306 | cancelActiveSync( |
| 307 | SyncStorageEngine.EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL, |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 308 | null /* any sync */, |
| 309 | "storage low"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) { |
| 311 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 312 | Slog.v(TAG, "Internal storage is ok."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | } |
| 314 | mStorageIsLow = false; |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 315 | rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL, |
| 316 | "storage ok"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | }; |
| 320 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 321 | private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 322 | @Override |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 323 | public void onReceive(Context context, Intent intent) { |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 324 | mBootCompleted = true; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 325 | // Called because it gets all pending jobs and stores them in mScheduledSyncs cache. |
| 326 | verifyJobScheduler(); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 327 | mSyncHandler.onBootCompleted(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 328 | } |
| 329 | }; |
| 330 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 331 | private final BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 332 | @Override |
Amith Yamasani | d648a60 | 2012-09-26 15:06:10 -0700 | [diff] [blame] | 333 | public void onReceive(Context context, Intent intent) { |
Makoto Onuki | fb636cc | 2017-12-07 15:46:26 -0800 | [diff] [blame] | 334 | EndPoint target = new EndPoint(null, null, getSendingUserId()); |
Shreyas Basarge | dcb88c8 | 2016-02-03 00:09:18 +0000 | [diff] [blame] | 335 | updateRunningAccounts(target /* sync targets for user */); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 336 | } |
| 337 | }; |
| 338 | |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 339 | private final PowerManager mPowerManager; |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 340 | |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 341 | private final UserManager mUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 342 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 343 | private final AccountManager mAccountManager; |
| 344 | |
| 345 | private final AccountManagerInternal mAccountManagerInternal; |
| 346 | |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 347 | private final PackageManagerInternal mPackageManagerInternal; |
| 348 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 349 | private List<UserInfo> getAllUsers() { |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 350 | return mUserManager.getUsers(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | private boolean containsAccountAndUser(AccountAndUser[] accounts, Account account, int userId) { |
| 354 | boolean found = false; |
| 355 | for (int i = 0; i < accounts.length; i++) { |
| 356 | if (accounts[i].userId == userId |
| 357 | && accounts[i].account.equals(account)) { |
| 358 | found = true; |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | return found; |
| 363 | } |
| 364 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 365 | /** target indicates endpoints that should be synced after account info is updated. */ |
| 366 | private void updateRunningAccounts(EndPoint target) { |
| 367 | if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_ACCOUNTS_UPDATED"); |
振淦王 | 60a7431 | 2015-12-01 16:37:31 +0800 | [diff] [blame] | 368 | // Update accounts in handler thread. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 369 | Message m = mSyncHandler.obtainMessage(SyncHandler.MESSAGE_ACCOUNTS_UPDATED); |
| 370 | m.obj = target; |
| 371 | m.sendToTarget(); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 374 | private void doDatabaseCleanup() { |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 375 | for (UserInfo user : mUserManager.getUsers(true)) { |
| 376 | // Skip any partially created/removed users |
| 377 | if (user.partial) continue; |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 378 | Account[] accountsForUser = AccountManagerService.getSingleton().getAccounts( |
| 379 | user.id, mContext.getOpPackageName()); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 380 | |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 381 | mSyncStorageEngine.doDatabaseCleanup(accountsForUser, user.id); |
| 382 | } |
| 383 | } |
| 384 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | private BroadcastReceiver mConnectivityIntentReceiver = |
| 386 | new BroadcastReceiver() { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 387 | @Override |
| 388 | public void onReceive(Context context, Intent intent) { |
| 389 | final boolean wasConnected = mDataConnectionIsConnected; |
Alon Albert | ed1d253 | 2011-02-15 14:02:14 -0800 | [diff] [blame] | 390 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 391 | // Don't use the intent to figure out if network is connected, just check |
| 392 | // ConnectivityManager directly. |
| 393 | mDataConnectionIsConnected = readDataConnectionState(); |
| 394 | if (mDataConnectionIsConnected) { |
| 395 | if (!wasConnected) { |
| 396 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 397 | Slog.v(TAG, "Reconnection detected: clearing all backoffs"); |
| 398 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 399 | // Note the location of this code was wrong from nyc to oc; fixed in DR. |
| 400 | clearAllBackoffs("network reconnect"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 401 | } |
Matthew Williams | 119aac9 | 2014-09-28 20:42:23 -0700 | [diff] [blame] | 402 | } |
Alon Albert | ed1d253 | 2011-02-15 14:02:14 -0800 | [diff] [blame] | 403 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 404 | }; |
| 405 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 406 | private void clearAllBackoffs(String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 407 | mSyncStorageEngine.clearAllBackoffsLocked(); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 408 | rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL, why); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 409 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | |
Alon Albert | 1bad83a | 2011-02-16 10:29:56 -0800 | [diff] [blame] | 411 | private boolean readDataConnectionState() { |
Alon Albert | ed1d253 | 2011-02-15 14:02:14 -0800 | [diff] [blame] | 412 | NetworkInfo networkInfo = getConnectivityManager().getActiveNetworkInfo(); |
| 413 | return (networkInfo != null) && networkInfo.isConnected(); |
| 414 | } |
| 415 | |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 416 | private String getJobStats() { |
| 417 | JobSchedulerInternal js = LocalServices.getService(JobSchedulerInternal.class); |
| 418 | return "JobStats: " |
| 419 | + ((js == null) ? "(JobSchedulerInternal==null)" |
| 420 | : js.getPersistStats().toString()); |
| 421 | } |
| 422 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 423 | private BroadcastReceiver mShutdownIntentReceiver = |
| 424 | new BroadcastReceiver() { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 425 | @Override |
| 426 | public void onReceive(Context context, Intent intent) { |
| 427 | Log.w(TAG, "Writing sync state before shutdown..."); |
| 428 | getSyncStorageEngine().writeAllState(); |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 429 | |
| 430 | mLogger.log(getJobStats()); |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 431 | mLogger.log("Shutting down."); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 432 | } |
| 433 | }; |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 434 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 435 | private final BroadcastReceiver mOtherIntentsReceiver = |
| 436 | new BroadcastReceiver() { |
| 437 | @Override |
| 438 | public void onReceive(Context context, Intent intent) { |
| 439 | if (Intent.ACTION_TIME_CHANGED.equals(intent.getAction())) { |
| 440 | mSyncStorageEngine.setClockValid(); |
| 441 | return; |
| 442 | } |
| 443 | } |
| 444 | }; |
| 445 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 446 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 447 | @Override |
| 448 | public void onReceive(Context context, Intent intent) { |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 449 | String action = intent.getAction(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 450 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 451 | if (userId == UserHandle.USER_NULL) return; |
| 452 | |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 453 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 454 | onUserRemoved(userId); |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 455 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 456 | onUserUnlocked(userId); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 457 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
| 458 | onUserStopped(userId); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 459 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 460 | } |
| 461 | }; |
| 462 | |
Makoto Onuki | e08a5c2 | 2018-04-27 13:28:46 -0700 | [diff] [blame] | 463 | private final HandlerThread mThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | private final SyncHandler mSyncHandler; |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 465 | private final SyncManagerConstants mConstants; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | |
Fred Quintana | 4f9cfc5 | 2009-09-02 15:20:23 -0700 | [diff] [blame] | 467 | private volatile boolean mBootCompleted = false; |
Shreyas Basarge | a4ac5ab | 2016-04-21 20:31:44 +0100 | [diff] [blame] | 468 | private volatile boolean mJobServiceReady = false; |
Fred Quintana | 4f9cfc5 | 2009-09-02 15:20:23 -0700 | [diff] [blame] | 469 | |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 470 | private ConnectivityManager getConnectivityManager() { |
| 471 | synchronized (this) { |
| 472 | if (mConnManagerDoNotUseDirectly == null) { |
| 473 | mConnManagerDoNotUseDirectly = (ConnectivityManager)mContext.getSystemService( |
| 474 | Context.CONNECTIVITY_SERVICE); |
| 475 | } |
| 476 | return mConnManagerDoNotUseDirectly; |
| 477 | } |
| 478 | } |
| 479 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 480 | /** |
| 481 | * Cancel all unnecessary jobs. This function will be run once after every boot. |
| 482 | */ |
| 483 | private void cleanupJobs() { |
| 484 | // O(n^2) in number of jobs, so we run this on the background thread. |
| 485 | mSyncHandler.postAtFrontOfQueue(new Runnable() { |
| 486 | @Override |
| 487 | public void run() { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 488 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 489 | Set<String> cleanedKeys = new HashSet<String>(); |
| 490 | for (SyncOperation opx: ops) { |
| 491 | if (cleanedKeys.contains(opx.key)) { |
| 492 | continue; |
| 493 | } |
| 494 | cleanedKeys.add(opx.key); |
| 495 | for (SyncOperation opy: ops) { |
| 496 | if (opx == opy) { |
| 497 | continue; |
| 498 | } |
| 499 | if (opx.key.equals(opy.key)) { |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 500 | mLogger.log("Removing duplicate sync: ", opy); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 501 | cancelJob(opy, "cleanupJobs() x=" + opx + " y=" + opy); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | }); |
| 507 | } |
| 508 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 509 | private synchronized void verifyJobScheduler() { |
| 510 | if (mJobScheduler != null) { |
| 511 | return; |
| 512 | } |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 513 | final long token = Binder.clearCallingIdentity(); |
| 514 | try { |
| 515 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 516 | Log.d(TAG, "initializing JobScheduler object."); |
| 517 | } |
| 518 | mJobScheduler = (JobScheduler) mContext.getSystemService( |
| 519 | Context.JOB_SCHEDULER_SERVICE); |
| 520 | mJobSchedulerInternal = LocalServices.getService(JobSchedulerInternal.class); |
| 521 | // Get all persisted syncs from JobScheduler |
| 522 | List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs(); |
| 523 | |
| 524 | int numPersistedPeriodicSyncs = 0; |
| 525 | int numPersistedOneshotSyncs = 0; |
| 526 | for (JobInfo job : pendingJobs) { |
| 527 | SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras()); |
| 528 | if (op != null) { |
| 529 | if (op.isPeriodic) { |
| 530 | numPersistedPeriodicSyncs++; |
| 531 | } else { |
| 532 | numPersistedOneshotSyncs++; |
| 533 | // Set the pending status of this EndPoint to true. Pending icon is |
| 534 | // shown on the settings activity. |
| 535 | mSyncStorageEngine.markPending(op.target, true); |
| 536 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 539 | final String summary = "Loaded persisted syncs: " |
| 540 | + numPersistedPeriodicSyncs + " periodic syncs, " |
| 541 | + numPersistedOneshotSyncs + " oneshot syncs, " |
| 542 | + (pendingJobs.size()) + " total system server jobs, " |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 543 | + getJobStats(); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 544 | Slog.i(TAG, summary); |
| 545 | mLogger.log(summary); |
| 546 | |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 547 | cleanupJobs(); |
| 548 | |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 549 | if (ENABLE_SUSPICIOUS_CHECK && |
| 550 | (numPersistedPeriodicSyncs == 0) && likelyHasPeriodicSyncs()) { |
| 551 | Slog.wtf(TAG, "Device booted with no persisted periodic syncs: " + summary); |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 552 | } |
| 553 | } finally { |
| 554 | Binder.restoreCallingIdentity(token); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 555 | } |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | /** |
| 559 | * @return whether the device most likely has some periodic syncs. |
| 560 | */ |
| 561 | private boolean likelyHasPeriodicSyncs() { |
| 562 | try { |
Makoto Onuki | b47e894 | 2017-09-18 14:03:03 -0700 | [diff] [blame] | 563 | // Each sync adapter has a daily periodic sync by default, but sync adapters can remove |
| 564 | // them by themselves. So here, we use an arbitrary threshold. If there are more than |
| 565 | // this many sync endpoints, surely one of them should have a periodic sync... |
| 566 | return mSyncStorageEngine.getAuthorityCount() >= 6; |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 567 | } catch (Throwable th) { |
| 568 | // Just in case. |
| 569 | } |
| 570 | return false; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | private JobScheduler getJobScheduler() { |
| 574 | verifyJobScheduler(); |
| 575 | return mJobScheduler; |
| 576 | } |
| 577 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 578 | public SyncManager(Context context, boolean factoryTest) { |
Makoto Onuki | 056a975 | 2018-05-08 15:21:56 -0700 | [diff] [blame] | 579 | synchronized (SyncManager.class) { |
| 580 | if (sInstance == null) { |
| 581 | sInstance = this; |
| 582 | } else { |
| 583 | Slog.wtf(TAG, "SyncManager instantiated multiple times"); |
| 584 | } |
| 585 | } |
| 586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | // Initialize the SyncStorageEngine first, before registering observers |
| 588 | // and creating threads and so on; it may fail if the disk is full. |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 589 | mContext = context; |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 590 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 591 | mLogger = SyncLogger.getInstance(); |
| 592 | |
Makoto Onuki | 6963bea7 | 2017-12-12 10:42:39 -0800 | [diff] [blame] | 593 | SyncStorageEngine.init(context, BackgroundThread.get().getLooper()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | mSyncStorageEngine = SyncStorageEngine.getSingleton(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 595 | mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 596 | @Override |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 597 | public void onSyncRequest(SyncStorageEngine.EndPoint info, int reason, Bundle extras, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 598 | @SyncExemption int syncExemptionFlag) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 599 | scheduleSync(info.account, info.userId, reason, info.provider, extras, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 600 | AuthorityInfo.UNDEFINED, syncExemptionFlag); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 601 | } |
| 602 | }); |
| 603 | |
| 604 | mSyncStorageEngine.setPeriodicSyncAddedListener( |
| 605 | new SyncStorageEngine.PeriodicSyncAddedListener() { |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 606 | @Override |
| 607 | public void onPeriodicSyncAdded(EndPoint target, Bundle extras, long pollFrequency, |
| 608 | long flex) { |
| 609 | updateOrAddPeriodicSync(target, pollFrequency, flex, extras); |
| 610 | } |
| 611 | }); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 612 | |
| 613 | mSyncStorageEngine.setOnAuthorityRemovedListener(new SyncStorageEngine.OnAuthorityRemovedListener() { |
| 614 | @Override |
| 615 | public void onAuthorityRemoved(EndPoint removedAuthority) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 616 | removeSyncsForAuthority(removedAuthority, "onAuthorityRemoved"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 617 | } |
| 618 | }); |
| 619 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 620 | mSyncAdapters = new SyncAdaptersCache(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | |
Makoto Onuki | e08a5c2 | 2018-04-27 13:28:46 -0700 | [diff] [blame] | 622 | mThread = new HandlerThread("SyncManager", android.os.Process.THREAD_PRIORITY_BACKGROUND); |
| 623 | mThread.start(); |
| 624 | mSyncHandler = new SyncHandler(mThread.getLooper()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | |
Fred Quintana | 44037e6 | 2010-01-21 13:14:49 -0800 | [diff] [blame] | 626 | mSyncAdapters.setListener(new RegisteredServicesCacheListener<SyncAdapterType>() { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 627 | @Override |
| 628 | public void onServiceChanged(SyncAdapterType type, int userId, boolean removed) { |
Fred Quintana | 44037e6 | 2010-01-21 13:14:49 -0800 | [diff] [blame] | 629 | if (!removed) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 630 | scheduleSync(null, UserHandle.USER_ALL, |
| 631 | SyncOperation.REASON_SERVICE_CHANGED, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 632 | type.authority, null, AuthorityInfo.UNDEFINED, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 633 | ContentResolver.SYNC_EXEMPTION_NONE); |
Fred Quintana | 44037e6 | 2010-01-21 13:14:49 -0800 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | }, mSyncHandler); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 637 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 638 | mRand = new Random(System.currentTimeMillis()); |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 639 | mConstants = new SyncManagerConstants(context); |
Amith Yamasani | 96a0fd65 | 2015-04-10 16:16:30 -0700 | [diff] [blame] | 640 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 641 | IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); |
| 642 | context.registerReceiver(mConnectivityIntentReceiver, intentFilter); |
| 643 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 644 | if (!factoryTest) { |
| 645 | intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
Dianne Hackborn | d83a096 | 2014-05-02 16:28:33 -0700 | [diff] [blame] | 646 | intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 647 | context.registerReceiver(mBootCompletedReceiver, intentFilter); |
| 648 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 649 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 650 | intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); |
| 651 | intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK); |
| 652 | context.registerReceiver(mStorageIntentReceiver, intentFilter); |
| 653 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 654 | intentFilter = new IntentFilter(Intent.ACTION_SHUTDOWN); |
| 655 | intentFilter.setPriority(100); |
| 656 | context.registerReceiver(mShutdownIntentReceiver, intentFilter); |
| 657 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 658 | intentFilter = new IntentFilter(); |
| 659 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 660 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 661 | intentFilter.addAction(Intent.ACTION_USER_STOPPED); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 662 | mContext.registerReceiverAsUser( |
| 663 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 664 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 665 | intentFilter = new IntentFilter(Intent.ACTION_TIME_CHANGED); |
| 666 | context.registerReceiver(mOtherIntentsReceiver, intentFilter); |
| 667 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | if (!factoryTest) { |
| 669 | mNotificationMgr = (NotificationManager) |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 670 | context.getSystemService(Context.NOTIFICATION_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | } else { |
| 672 | mNotificationMgr = null; |
| 673 | } |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 674 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 675 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 676 | mAccountManager = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE); |
| 677 | mAccountManagerInternal = LocalServices.getService(AccountManagerInternal.class); |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 678 | mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 679 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 680 | mAccountManagerInternal.addOnAppPermissionChangeListener((Account account, int uid) -> { |
| 681 | // If the UID gained access to the account kick-off syncs lacking account access |
| 682 | if (mAccountManagerInternal.hasAccountAccess(account, uid)) { |
| 683 | scheduleSync(account, UserHandle.getUserId(uid), |
| 684 | SyncOperation.REASON_ACCOUNTS_UPDATED, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 685 | null, null, AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 686 | ContentResolver.SYNC_EXEMPTION_NONE); |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 687 | } |
| 688 | }); |
| 689 | |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 690 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 691 | BatteryStats.SERVICE_NAME)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 692 | |
| 693 | // This WakeLock is used to ensure that we stay awake between the time that we receive |
| 694 | // a sync alarm notification and when we finish processing it. We need to do this |
| 695 | // because we don't do the work in the alarm handler, rather we do it in a message |
| 696 | // handler. |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 697 | mHandleAlarmWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | HANDLE_SYNC_ALARM_WAKE_LOCK); |
| 699 | mHandleAlarmWakeLock.setReferenceCounted(false); |
| 700 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 701 | // This WakeLock is used to ensure that we stay awake while running the sync loop |
| 702 | // message handler. Normally we will hold a sync adapter wake lock while it is being |
| 703 | // synced but during the execution of the sync loop it might finish a sync for |
| 704 | // one sync adapter before starting the sync for the other sync adapter and we |
| 705 | // don't want the device to go to sleep during that window. |
| 706 | mSyncManagerWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 707 | SYNC_LOOP_WAKE_LOCK); |
| 708 | mSyncManagerWakeLock.setReferenceCounted(false); |
| 709 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 710 | mProvisioned = isDeviceProvisioned(); |
| 711 | if (!mProvisioned) { |
| 712 | final ContentResolver resolver = context.getContentResolver(); |
| 713 | ContentObserver provisionedObserver = |
| 714 | new ContentObserver(null /* current thread */) { |
| 715 | public void onChange(boolean selfChange) { |
| 716 | mProvisioned |= isDeviceProvisioned(); |
| 717 | if (mProvisioned) { |
| 718 | mSyncHandler.onDeviceProvisioned(); |
| 719 | resolver.unregisterContentObserver(this); |
| 720 | } |
| 721 | } |
| 722 | }; |
| 723 | |
| 724 | synchronized (mSyncHandler) { |
| 725 | resolver.registerContentObserver( |
| 726 | Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED), |
| 727 | false /* notifyForDescendents */, |
| 728 | provisionedObserver); |
| 729 | |
| 730 | // The device *may* have been provisioned while we were registering above observer. |
| 731 | // Check again to make sure. |
| 732 | mProvisioned |= isDeviceProvisioned(); |
| 733 | if (mProvisioned) { |
| 734 | resolver.unregisterContentObserver(provisionedObserver); |
| 735 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 736 | } |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 737 | } |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 738 | |
| 739 | if (!factoryTest) { |
Amith Yamasani | d648a60 | 2012-09-26 15:06:10 -0700 | [diff] [blame] | 740 | // Register for account list updates for all users |
| 741 | mContext.registerReceiverAsUser(mAccountsUpdatedReceiver, |
| 742 | UserHandle.ALL, |
| 743 | new IntentFilter(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION), |
Matthew Williams | 5c6756f | 2014-10-02 04:12:28 +0000 | [diff] [blame] | 744 | null, null); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 745 | } |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 746 | |
Christopher Tate | 734c91d | 2016-02-02 12:46:18 -0800 | [diff] [blame] | 747 | // Set up the communication channel between the scheduled job and the sync manager. |
| 748 | // This is posted to the *main* looper intentionally, to defer calling startService() |
| 749 | // until after the lengthy primary boot sequence completes on that thread, to avoid |
| 750 | // spurious ANR triggering. |
| 751 | final Intent startServiceIntent = new Intent(mContext, SyncJobService.class); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 752 | startServiceIntent.putExtra(SyncJobService.EXTRA_MESSENGER, new Messenger(mSyncHandler)); |
Christopher Tate | 734c91d | 2016-02-02 12:46:18 -0800 | [diff] [blame] | 753 | new Handler(mContext.getMainLooper()).post(new Runnable() { |
| 754 | @Override |
| 755 | public void run() { |
| 756 | mContext.startService(startServiceIntent); |
| 757 | } |
| 758 | }); |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 759 | |
| 760 | // Sync adapters were able to access the synced account without the accounts |
| 761 | // permission which circumvents our permission model. Therefore, we require |
| 762 | // sync adapters that don't have access to the account to get user consent. |
| 763 | // This can be noisy, therefore we will white-list sync adapters installed |
| 764 | // before we started checking for account access because they already know |
| 765 | // the account (they run before) which is the genie is out of the bottle. |
| 766 | whiteListExistingSyncAdaptersIfNeeded(); |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 767 | |
Makoto Onuki | e7b0298 | 2017-08-24 14:23:36 -0700 | [diff] [blame] | 768 | mLogger.log("Sync manager initialized: " + Build.FINGERPRINT); |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 771 | public void onStartUser(int userHandle) { |
Makoto Onuki | f15a942 | 2017-12-11 15:50:58 -0800 | [diff] [blame] | 772 | mSyncHandler.post(() -> mLogger.log("onStartUser: user=", userHandle)); |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | public void onUnlockUser(int userHandle) { |
Makoto Onuki | f15a942 | 2017-12-11 15:50:58 -0800 | [diff] [blame] | 776 | mSyncHandler.post(() -> mLogger.log("onUnlockUser: user=", userHandle)); |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | public void onStopUser(int userHandle) { |
Makoto Onuki | f15a942 | 2017-12-11 15:50:58 -0800 | [diff] [blame] | 780 | mSyncHandler.post(() -> mLogger.log("onStopUser: user=", userHandle)); |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 783 | public void onBootPhase(int phase) { |
Makoto Onuki | d0c50dd | 2018-02-16 09:52:50 -0800 | [diff] [blame] | 784 | // Note SyncManager only receives PHASE_ACTIVITY_MANAGER_READY and after. |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 785 | switch (phase) { |
Makoto Onuki | d0c50dd | 2018-02-16 09:52:50 -0800 | [diff] [blame] | 786 | case SystemService.PHASE_ACTIVITY_MANAGER_READY: |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 787 | mConstants.start(); |
| 788 | break; |
| 789 | } |
| 790 | } |
Makoto Onuki | fe224e0 | 2017-06-29 14:11:14 -0700 | [diff] [blame] | 791 | |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 792 | private void whiteListExistingSyncAdaptersIfNeeded() { |
| 793 | if (!mSyncStorageEngine.shouldGrantSyncAdaptersAccountAccess()) { |
| 794 | return; |
| 795 | } |
| 796 | List<UserInfo> users = mUserManager.getUsers(true); |
| 797 | final int userCount = users.size(); |
| 798 | for (int i = 0; i < userCount; i++) { |
| 799 | UserHandle userHandle = users.get(i).getUserHandle(); |
| 800 | final int userId = userHandle.getIdentifier(); |
| 801 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> service |
| 802 | : mSyncAdapters.getAllServices(userId)) { |
| 803 | String packageName = service.componentName.getPackageName(); |
| 804 | for (Account account : mAccountManager.getAccountsByTypeAsUser( |
| 805 | service.type.accountType, userHandle)) { |
| 806 | if (!canAccessAccount(account, packageName, userId)) { |
| 807 | mAccountManager.updateAppPermission(account, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 808 | AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, service.uid, true); |
Svet Ganov | 65712b0 | 2016-09-01 10:24:11 -0700 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
| 812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 815 | private boolean isDeviceProvisioned() { |
| 816 | final ContentResolver resolver = mContext.getContentResolver(); |
| 817 | return (Settings.Global.getInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0) != 0); |
| 818 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | /** |
| 820 | * Return a random value v that satisfies minValue <= v < maxValue. The difference between |
| 821 | * maxValue and minValue must be less than Integer.MAX_VALUE. |
| 822 | */ |
| 823 | private long jitterize(long minValue, long maxValue) { |
| 824 | Random random = new Random(SystemClock.elapsedRealtime()); |
| 825 | long spread = maxValue - minValue; |
| 826 | if (spread > Integer.MAX_VALUE) { |
| 827 | throw new IllegalArgumentException("the difference between the maxValue and the " |
| 828 | + "minValue must be less than " + Integer.MAX_VALUE); |
| 829 | } |
| 830 | return minValue + random.nextInt((int)spread); |
| 831 | } |
| 832 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 833 | public SyncStorageEngine getSyncStorageEngine() { |
| 834 | return mSyncStorageEngine; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 835 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 836 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 837 | private int getIsSyncable(Account account, int userId, String providerName) { |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 838 | int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName); |
| 839 | UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId); |
| 840 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 841 | // If it's not a restricted user, return isSyncable. |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 842 | if (userInfo == null || !userInfo.isRestricted()) return isSyncable; |
| 843 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 844 | // Else check if the sync adapter has opted-in or not. |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 845 | RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = |
| 846 | mSyncAdapters.getServiceInfo( |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 847 | SyncAdapterType.newKey(providerName, account.type), userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 848 | if (syncAdapterInfo == null) return AuthorityInfo.NOT_SYNCABLE; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 849 | |
| 850 | PackageInfo pInfo = null; |
| 851 | try { |
| 852 | pInfo = AppGlobals.getPackageManager().getPackageInfo( |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 853 | syncAdapterInfo.componentName.getPackageName(), 0, userId); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 854 | if (pInfo == null) return AuthorityInfo.NOT_SYNCABLE; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 855 | } catch (RemoteException re) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 856 | // Shouldn't happen. |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 857 | return AuthorityInfo.NOT_SYNCABLE; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 858 | } |
| 859 | if (pInfo.restrictedAccountType != null |
| 860 | && pInfo.restrictedAccountType.equals(account.type)) { |
| 861 | return isSyncable; |
| 862 | } else { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 863 | return AuthorityInfo.NOT_SYNCABLE; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 867 | private void setAuthorityPendingState(EndPoint info) { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 868 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 869 | for (SyncOperation op: ops) { |
| 870 | if (!op.isPeriodic && op.target.matchesSpec(info)) { |
| 871 | getSyncStorageEngine().markPending(info, true); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 872 | return; |
| 873 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 874 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 875 | getSyncStorageEngine().markPending(info, false); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Initiate a sync. This can start a sync for all providers |
| 880 | * (pass null to url, set onlyTicklable to false), only those |
| 881 | * providers that are marked as ticklable (pass null to url, |
| 882 | * set onlyTicklable to true), or a specific provider (set url |
| 883 | * to the content url of the provider). |
| 884 | * |
| 885 | * <p>If the ContentResolver.SYNC_EXTRAS_UPLOAD boolean in extras is |
| 886 | * true then initiate a sync that just checks for local changes to send |
| 887 | * to the server, otherwise initiate a sync that first gets any |
| 888 | * changes from the server before sending local changes back to |
| 889 | * the server. |
| 890 | * |
| 891 | * <p>If a specific provider is being synced (the url is non-null) |
| 892 | * then the extras can contain SyncAdapter-specific information |
| 893 | * to control what gets synced (e.g. which specific feed to sync). |
| 894 | * |
| 895 | * <p>You'll start getting callbacks after this. |
| 896 | * |
| 897 | * @param requestedAccount the account to sync, may be null to signify all accounts |
| 898 | * @param userId the id of the user whose accounts are to be synced. If userId is USER_ALL, |
| 899 | * then all users' accounts are considered. |
| 900 | * @param reason for sync request. If this is a positive integer, it is the Linux uid |
| 901 | * assigned to the process that requested the sync. If it's negative, the sync was requested by |
| 902 | * the SyncManager itself and could be one of the following: |
| 903 | * {@link SyncOperation#REASON_BACKGROUND_DATA_SETTINGS_CHANGED} |
| 904 | * {@link SyncOperation#REASON_ACCOUNTS_UPDATED} |
| 905 | * {@link SyncOperation#REASON_SERVICE_CHANGED} |
| 906 | * {@link SyncOperation#REASON_PERIODIC} |
| 907 | * {@link SyncOperation#REASON_IS_SYNCABLE} |
| 908 | * {@link SyncOperation#REASON_SYNC_AUTO} |
| 909 | * {@link SyncOperation#REASON_MASTER_SYNC_AUTO} |
| 910 | * {@link SyncOperation#REASON_USER_START} |
| 911 | * @param requestedAuthority the authority to sync, may be null to indicate all authorities |
| 912 | * @param extras a Map of SyncAdapter-specific information to control |
| 913 | * syncs of a specific provider. Can be null. Is ignored |
| 914 | * if the url is null. |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 915 | * @param targetSyncState Only sync authorities that have the specified sync state. |
| 916 | * Use {@link AuthorityInfo#UNDEFINED} to sync all authorities. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 917 | */ |
| 918 | public void scheduleSync(Account requestedAccount, int userId, int reason, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 919 | String requestedAuthority, Bundle extras, int targetSyncState, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 920 | @SyncExemption int syncExemptionFlag) { |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 921 | scheduleSync(requestedAccount, userId, reason, requestedAuthority, extras, targetSyncState, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 922 | 0 /* min delay */, true /* checkIfAccountReady */, syncExemptionFlag); |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /** |
| 926 | * @param minDelayMillis The sync can't land before this delay expires. |
| 927 | */ |
| 928 | private void scheduleSync(Account requestedAccount, int userId, int reason, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 929 | String requestedAuthority, Bundle extras, int targetSyncState, |
| 930 | final long minDelayMillis, boolean checkIfAccountReady, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 931 | @SyncExemption int syncExemptionFlag) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 932 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 933 | if (extras == null) { |
| 934 | extras = new Bundle(); |
| 935 | } |
| 936 | if (isLoggable) { |
| 937 | Log.d(TAG, "one-time sync for: " + requestedAccount + " " + extras.toString() + " " |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 938 | + requestedAuthority |
| 939 | + " reason=" + reason |
| 940 | + " checkIfAccountReady=" + checkIfAccountReady |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 941 | + " syncExemptionFlag=" + syncExemptionFlag); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 942 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 943 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 944 | AccountAndUser[] accounts = null; |
| 945 | if (requestedAccount != null) { |
| 946 | if (userId != UserHandle.USER_ALL) { |
| 947 | accounts = new AccountAndUser[]{new AccountAndUser(requestedAccount, userId)}; |
| 948 | } else { |
| 949 | for (AccountAndUser runningAccount : mRunningAccounts) { |
| 950 | if (requestedAccount.equals(runningAccount.account)) { |
| 951 | accounts = ArrayUtils.appendElement(AccountAndUser.class, |
| 952 | accounts, runningAccount); |
| 953 | } |
| 954 | } |
| 955 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 956 | } else { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 957 | accounts = mRunningAccounts; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | if (ArrayUtils.isEmpty(accounts)) { |
| 961 | if (isLoggable) { |
| 962 | Slog.v(TAG, "scheduleSync: no accounts configured, dropping"); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 963 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 964 | return; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false); |
| 968 | final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); |
| 969 | if (manualSync) { |
| 970 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true); |
| 971 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true); |
| 972 | } |
| 973 | final boolean ignoreSettings = |
| 974 | extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false); |
| 975 | |
| 976 | int source; |
| 977 | if (uploadOnly) { |
| 978 | source = SyncStorageEngine.SOURCE_LOCAL; |
| 979 | } else if (manualSync) { |
| 980 | source = SyncStorageEngine.SOURCE_USER; |
| 981 | } else if (requestedAuthority == null) { |
| 982 | source = SyncStorageEngine.SOURCE_POLL; |
| 983 | } else { |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 984 | if (extras.containsKey("feed")) { |
| 985 | source = SyncStorageEngine.SOURCE_FEED; |
| 986 | } else{ |
| 987 | // This isn't strictly server, since arbitrary callers can (and do) request |
| 988 | // a non-forced two-way sync on a specific url. |
| 989 | source = SyncStorageEngine.SOURCE_OTHER; |
| 990 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | for (AccountAndUser account : accounts) { |
Fyodor Kupolov | 6fde298 | 2015-01-06 17:46:37 -0800 | [diff] [blame] | 994 | // If userId is specified, do not sync accounts of other users |
Xiaohui Chen | 98404fd | 2015-08-17 16:09:02 -0700 | [diff] [blame] | 995 | if (userId >= UserHandle.USER_SYSTEM && account.userId >= UserHandle.USER_SYSTEM |
Fyodor Kupolov | 6fde298 | 2015-01-06 17:46:37 -0800 | [diff] [blame] | 996 | && userId != account.userId) { |
| 997 | continue; |
| 998 | } |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 999 | // Compile a list of authorities that have sync adapters. |
| 1000 | // For each authority sync each account that matches a sync adapter. |
| 1001 | final HashSet<String> syncableAuthorities = new HashSet<String>(); |
| 1002 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapter : |
| 1003 | mSyncAdapters.getAllServices(account.userId)) { |
| 1004 | syncableAuthorities.add(syncAdapter.type.authority); |
| 1005 | } |
| 1006 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1007 | // If the url was specified then replace the list of authorities |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1008 | // with just this authority or clear it if this authority isn't |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1009 | // syncable. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1010 | if (requestedAuthority != null) { |
| 1011 | final boolean hasSyncAdapter = syncableAuthorities.contains(requestedAuthority); |
| 1012 | syncableAuthorities.clear(); |
| 1013 | if (hasSyncAdapter) syncableAuthorities.add(requestedAuthority); |
| 1014 | } |
| 1015 | |
| 1016 | for (String authority : syncableAuthorities) { |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1017 | int isSyncable = computeSyncable(account.account, account.userId, authority, |
| 1018 | !checkIfAccountReady); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1019 | |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 1020 | if (isSyncable == AuthorityInfo.NOT_SYNCABLE) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1021 | continue; |
| 1022 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1023 | |
| 1024 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = |
| 1025 | mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(authority, |
| 1026 | account.account.type), account.userId); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1027 | if (syncAdapterInfo == null) { |
| 1028 | continue; |
| 1029 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1030 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1031 | final int owningUid = syncAdapterInfo.uid; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1032 | |
| 1033 | if (isSyncable == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) { |
| 1034 | if (isLoggable) { |
| 1035 | Slog.v(TAG, " Not scheduling sync operation: " |
| 1036 | + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS"); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1037 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1038 | Bundle finalExtras = new Bundle(extras); |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 1039 | String packageName = syncAdapterInfo.componentName.getPackageName(); |
| 1040 | // If the app did not run and has no account access, done |
Amith Yamasani | 2cbfa1e | 2017-03-28 10:34:01 -0700 | [diff] [blame] | 1041 | try { |
| 1042 | if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) { |
| 1043 | continue; |
| 1044 | } |
| 1045 | } catch (IllegalArgumentException e) { |
| 1046 | // Package not found, race with an uninstall |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 1047 | continue; |
| 1048 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1049 | mAccountManagerInternal.requestAccountAccess(account.account, |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 1050 | packageName, userId, |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1051 | new RemoteCallback((Bundle result) -> { |
| 1052 | if (result != null |
| 1053 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 1054 | scheduleSync(account.account, userId, reason, authority, |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1055 | finalExtras, targetSyncState, minDelayMillis, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1056 | true /* checkIfAccountReady */, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1057 | syncExemptionFlag); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | )); |
| 1061 | continue; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1062 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1063 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1064 | final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs(); |
| 1065 | final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable(); |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1066 | if (!checkIfAccountReady && isSyncable < 0 && isAlwaysSyncable) { |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 1067 | mSyncStorageEngine.setIsSyncable( |
Makoto Onuki | d476430 | 2018-03-30 17:32:57 -0700 | [diff] [blame] | 1068 | account.account, account.userId, authority, AuthorityInfo.SYNCABLE, |
| 1069 | SyncLogger.CALLING_UID_SELF); |
Matthew Williams | 53abfdb | 2015-06-10 20:06:37 -0700 | [diff] [blame] | 1070 | isSyncable = AuthorityInfo.SYNCABLE; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1071 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1072 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1073 | if (targetSyncState != AuthorityInfo.UNDEFINED && targetSyncState != isSyncable) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1074 | continue; |
| 1075 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1076 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1077 | if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) { |
| 1078 | continue; |
| 1079 | } |
| 1080 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1081 | boolean syncAllowed = |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1082 | (isSyncable < 0) // Always allow if the isSyncable state is unknown. |
| 1083 | || ignoreSettings |
| 1084 | || (mSyncStorageEngine.getMasterSyncAutomatically(account.userId) |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1085 | && mSyncStorageEngine.getSyncAutomatically(account.account, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1086 | account.userId, authority)); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1087 | if (!syncAllowed) { |
| 1088 | if (isLoggable) { |
| 1089 | Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority |
| 1090 | + " is not allowed, dropping request"); |
| 1091 | } |
| 1092 | continue; |
| 1093 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1094 | SyncStorageEngine.EndPoint info = |
| 1095 | new SyncStorageEngine.EndPoint( |
| 1096 | account.account, authority, account.userId); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1097 | long delayUntil = |
| 1098 | mSyncStorageEngine.getDelayUntilTime(info); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1099 | |
| 1100 | final String owningPackage = syncAdapterInfo.componentName.getPackageName(); |
| 1101 | |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1102 | if (isSyncable == AuthorityInfo.NOT_INITIALIZED) { |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1103 | if (checkIfAccountReady) { |
| 1104 | Bundle finalExtras = new Bundle(extras); |
| 1105 | |
| 1106 | sendOnUnsyncableAccount(mContext, syncAdapterInfo, account.userId, |
| 1107 | () -> scheduleSync(account.account, account.userId, reason, |
| 1108 | authority, finalExtras, targetSyncState, minDelayMillis, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1109 | false, syncExemptionFlag)); |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1110 | } else { |
| 1111 | // Initialisation sync. |
| 1112 | Bundle newExtras = new Bundle(); |
| 1113 | newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true); |
| 1114 | if (isLoggable) { |
| 1115 | Slog.v(TAG, "schedule initialisation Sync:" |
| 1116 | + ", delay until " + delayUntil |
| 1117 | + ", run by " + 0 |
| 1118 | + ", flexMillis " + 0 |
| 1119 | + ", source " + source |
| 1120 | + ", account " + account |
| 1121 | + ", authority " + authority |
| 1122 | + ", extras " + newExtras); |
| 1123 | } |
| 1124 | postScheduleSyncMessage( |
| 1125 | new SyncOperation(account.account, account.userId, |
| 1126 | owningUid, owningPackage, reason, source, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1127 | authority, newExtras, allowParallelSyncs, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1128 | syncExemptionFlag), |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1129 | minDelayMillis |
| 1130 | ); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1131 | } |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 1132 | } else if (targetSyncState == AuthorityInfo.UNDEFINED |
| 1133 | || targetSyncState == isSyncable) { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1134 | if (isLoggable) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1135 | Slog.v(TAG, "scheduleSync:" |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1136 | + " delay until " + delayUntil |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1137 | + ", source " + source |
| 1138 | + ", account " + account |
| 1139 | + ", authority " + authority |
| 1140 | + ", extras " + extras); |
| 1141 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1142 | postScheduleSyncMessage( |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1143 | new SyncOperation(account.account, account.userId, |
| 1144 | owningUid, owningPackage, reason, source, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1145 | authority, extras, allowParallelSyncs, syncExemptionFlag), |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 1146 | minDelayMillis |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1147 | ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
Svet Ganov | 96b9c75 | 2016-10-17 19:29:58 -0700 | [diff] [blame] | 1153 | public int computeSyncable(Account account, int userId, String authority, |
| 1154 | boolean checkAccountAccess) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1155 | final int status = getIsSyncable(account, userId, authority); |
| 1156 | if (status == AuthorityInfo.NOT_SYNCABLE) { |
| 1157 | return AuthorityInfo.NOT_SYNCABLE; |
| 1158 | } |
| 1159 | final SyncAdapterType type = SyncAdapterType.newKey(authority, account.type); |
| 1160 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = |
| 1161 | mSyncAdapters.getServiceInfo(type, userId); |
| 1162 | if (syncAdapterInfo == null) { |
| 1163 | return AuthorityInfo.NOT_SYNCABLE; |
| 1164 | } |
| 1165 | final int owningUid = syncAdapterInfo.uid; |
| 1166 | final String owningPackage = syncAdapterInfo.componentName.getPackageName(); |
| 1167 | try { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 1168 | if (ActivityManager.getService().isAppStartModeDisabled(owningUid, owningPackage)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1169 | Slog.w(TAG, "Not scheduling job " + syncAdapterInfo.uid + ":" |
| 1170 | + syncAdapterInfo.componentName |
| 1171 | + " -- package not allowed to start"); |
| 1172 | return AuthorityInfo.NOT_SYNCABLE; |
| 1173 | } |
| 1174 | } catch (RemoteException e) { |
| 1175 | /* ignore - local call */ |
| 1176 | } |
Svet Ganov | 96b9c75 | 2016-10-17 19:29:58 -0700 | [diff] [blame] | 1177 | if (checkAccountAccess && !canAccessAccount(account, owningPackage, owningUid)) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 1178 | Log.w(TAG, "Access to " + account + " denied for package " |
| 1179 | + owningPackage + " in UID " + syncAdapterInfo.uid); |
| 1180 | return AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS; |
| 1181 | } |
| 1182 | |
| 1183 | return status; |
| 1184 | } |
| 1185 | |
| 1186 | private boolean canAccessAccount(Account account, String packageName, int uid) { |
| 1187 | if (mAccountManager.hasAccountAccess(account, packageName, |
| 1188 | UserHandle.getUserHandleForUid(uid))) { |
| 1189 | return true; |
| 1190 | } |
| 1191 | // We relax the account access rule to also include the system apps as |
| 1192 | // they are trusted and we want to minimize the cases where the user |
| 1193 | // involvement is required to grant access to the synced account. |
| 1194 | try { |
| 1195 | mContext.getPackageManager().getApplicationInfoAsUser(packageName, |
| 1196 | PackageManager.MATCH_SYSTEM_ONLY, UserHandle.getUserId(uid)); |
| 1197 | return true; |
| 1198 | } catch (NameNotFoundException e) { |
| 1199 | return false; |
| 1200 | } |
| 1201 | } |
| 1202 | |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1203 | private void removeSyncsForAuthority(EndPoint info, String why) { |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 1204 | mLogger.log("removeSyncsForAuthority: ", info); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1205 | verifyJobScheduler(); |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1206 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1207 | for (SyncOperation op: ops) { |
| 1208 | if (op.target.matchesSpec(info)) { |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 1209 | mLogger.log("canceling: ", op); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1210 | cancelJob(op, why); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | |
| 1215 | /** |
| 1216 | * Remove a specific periodic sync identified by its target and extras. |
| 1217 | */ |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1218 | public void removePeriodicSync(EndPoint target, Bundle extras, String why) { |
| 1219 | Message m = mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_REMOVE_PERIODIC_SYNC, |
| 1220 | Pair.create(target, why)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1221 | m.setData(extras); |
| 1222 | m.sendToTarget(); |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * Add a periodic sync. If a sync with same target and extras exists, its period and |
| 1227 | * flexMillis will be updated. |
| 1228 | */ |
| 1229 | public void updateOrAddPeriodicSync(EndPoint target, long pollFrequency, long flex, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 1230 | Bundle extras) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1231 | UpdatePeriodicSyncMessagePayload payload = new UpdatePeriodicSyncMessagePayload(target, |
| 1232 | pollFrequency, flex, extras); |
| 1233 | mSyncHandler.obtainMessage(SyncHandler.MESSAGE_UPDATE_PERIODIC_SYNC, payload) |
| 1234 | .sendToTarget(); |
| 1235 | } |
| 1236 | |
| 1237 | /** |
| 1238 | * Get a list of periodic syncs corresponding to the given target. |
| 1239 | */ |
| 1240 | public List<PeriodicSync> getPeriodicSyncs(EndPoint target) { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1241 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1242 | List<PeriodicSync> periodicSyncs = new ArrayList<PeriodicSync>(); |
| 1243 | |
| 1244 | for (SyncOperation op: ops) { |
| 1245 | if (op.isPeriodic && op.target.matchesSpec(target)) { |
| 1246 | periodicSyncs.add(new PeriodicSync(op.target.account, op.target.provider, |
| 1247 | op.extras, op.periodMillis / 1000, op.flexMillis / 1000)); |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | return periodicSyncs; |
| 1252 | } |
| 1253 | |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1254 | /** |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 1255 | * Schedule sync based on local changes to a provider. We wait for at least LOCAL_SYNC_DELAY |
| 1256 | * ms to batch syncs. |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1257 | */ |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1258 | public void scheduleLocalSync(Account account, int userId, int reason, String authority, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1259 | @SyncExemption int syncExemptionFlag) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1260 | final Bundle extras = new Bundle(); |
| 1261 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true); |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 1262 | scheduleSync(account, userId, reason, authority, extras, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1263 | AuthorityInfo.UNDEFINED, LOCAL_SYNC_DELAY, true /* checkIfAccountReady */, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1264 | syncExemptionFlag); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1267 | public SyncAdapterType[] getSyncAdapterTypes(int userId) { |
| 1268 | final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos; |
| 1269 | serviceInfos = mSyncAdapters.getAllServices(userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1270 | SyncAdapterType[] types = new SyncAdapterType[serviceInfos.size()]; |
| 1271 | int i = 0; |
| 1272 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> serviceInfo : serviceInfos) { |
| 1273 | types[i] = serviceInfo.type; |
| 1274 | ++i; |
| 1275 | } |
| 1276 | return types; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1277 | } |
| 1278 | |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 1279 | public String[] getSyncAdapterPackagesForAuthorityAsUser(String authority, int userId) { |
| 1280 | return mSyncAdapters.getSyncAdapterPackagesForAuthority(authority, userId); |
| 1281 | } |
| 1282 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1283 | private void sendSyncFinishedOrCanceledMessage(ActiveSyncContext syncContext, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 1284 | SyncResult syncResult) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1285 | if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_SYNC_FINISHED"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | Message msg = mSyncHandler.obtainMessage(); |
| 1287 | msg.what = SyncHandler.MESSAGE_SYNC_FINISHED; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1288 | msg.obj = new SyncFinishedOrCancelledMessagePayload(syncContext, syncResult); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1289 | mSyncHandler.sendMessage(msg); |
| 1290 | } |
| 1291 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1292 | private void sendCancelSyncsMessage(final SyncStorageEngine.EndPoint info, Bundle extras, |
| 1293 | String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1294 | if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_CANCEL"); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1295 | |
| 1296 | mLogger.log("sendCancelSyncsMessage() ep=", info, " why=", why); |
| 1297 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1298 | Message msg = mSyncHandler.obtainMessage(); |
| 1299 | msg.what = SyncHandler.MESSAGE_CANCEL; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1300 | msg.setData(extras); |
| 1301 | msg.obj = info; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1302 | mSyncHandler.sendMessage(msg); |
| 1303 | } |
| 1304 | |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 1305 | /** |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 1306 | * Post a delayed message that will monitor the given sync context by periodically checking how |
| 1307 | * much network has been used by the uid. |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 1308 | */ |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 1309 | private void postMonitorSyncProgressMessage(ActiveSyncContext activeSyncContext) { |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 1310 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1311 | Slog.v(TAG, "posting MESSAGE_SYNC_MONITOR in " + |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 1312 | (SYNC_MONITOR_WINDOW_LENGTH_MILLIS/1000) + "s"); |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 1313 | } |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 1314 | |
| 1315 | activeSyncContext.mBytesTransferredAtLastPoll = |
| 1316 | getTotalBytesTransferredByUid(activeSyncContext.mSyncAdapterUid); |
| 1317 | activeSyncContext.mLastPolledTimeElapsed = SystemClock.elapsedRealtime(); |
| 1318 | Message monitorMessage = |
| 1319 | mSyncHandler.obtainMessage( |
| 1320 | SyncHandler.MESSAGE_MONITOR_SYNC, |
| 1321 | activeSyncContext); |
| 1322 | mSyncHandler.sendMessageDelayed(monitorMessage, SYNC_MONITOR_WINDOW_LENGTH_MILLIS); |
Matthew Williams | 92a1c09 | 2014-08-25 19:18:32 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 1325 | private void postScheduleSyncMessage(SyncOperation syncOperation, long minDelayMillis) { |
| 1326 | ScheduleSyncMessagePayload payload = |
| 1327 | new ScheduleSyncMessagePayload(syncOperation, minDelayMillis); |
| 1328 | mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_SCHEDULE_SYNC, payload).sendToTarget(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 1331 | /** |
| 1332 | * Monitor sync progress by calculating how many bytes it is managing to send to and fro. |
| 1333 | */ |
| 1334 | private long getTotalBytesTransferredByUid(int uid) { |
| 1335 | return (TrafficStats.getUidRxBytes(uid) + TrafficStats.getUidTxBytes(uid)); |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * Convenience class for passing parameters for a finished or cancelled sync to the handler |
| 1340 | * to be processed. |
| 1341 | */ |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1342 | private class SyncFinishedOrCancelledMessagePayload { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | public final ActiveSyncContext activeSyncContext; |
| 1344 | public final SyncResult syncResult; |
| 1345 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1346 | SyncFinishedOrCancelledMessagePayload(ActiveSyncContext syncContext, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 1347 | SyncResult syncResult) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | this.activeSyncContext = syncContext; |
| 1349 | this.syncResult = syncResult; |
| 1350 | } |
| 1351 | } |
| 1352 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1353 | private class UpdatePeriodicSyncMessagePayload { |
| 1354 | public final EndPoint target; |
| 1355 | public final long pollFrequency; |
| 1356 | public final long flex; |
| 1357 | public final Bundle extras; |
| 1358 | |
| 1359 | UpdatePeriodicSyncMessagePayload(EndPoint target, long pollFrequency, long flex, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 1360 | Bundle extras) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1361 | this.target = target; |
| 1362 | this.pollFrequency = pollFrequency; |
| 1363 | this.flex = flex; |
| 1364 | this.extras = extras; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 1368 | private static class ScheduleSyncMessagePayload { |
| 1369 | final SyncOperation syncOperation; |
| 1370 | final long minDelayMillis; |
| 1371 | |
| 1372 | ScheduleSyncMessagePayload(SyncOperation syncOperation, long minDelayMillis) { |
| 1373 | this.syncOperation = syncOperation; |
| 1374 | this.minDelayMillis = minDelayMillis; |
| 1375 | } |
| 1376 | } |
| 1377 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1378 | private void clearBackoffSetting(EndPoint target, String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1379 | Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target); |
| 1380 | if (backoff != null && backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE && |
| 1381 | backoff.second == SyncStorageEngine.NOT_IN_BACKOFF_MODE) { |
| 1382 | return; |
| 1383 | } |
| 1384 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1385 | Slog.v(TAG, "Clearing backoffs for " + target); |
| 1386 | } |
| 1387 | mSyncStorageEngine.setBackoff(target, |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1388 | SyncStorageEngine.NOT_IN_BACKOFF_MODE, |
| 1389 | SyncStorageEngine.NOT_IN_BACKOFF_MODE); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1390 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1391 | rescheduleSyncs(target, why); |
Alon Albert | 6e079a3 | 2010-11-12 12:41:09 -0800 | [diff] [blame] | 1392 | } |
| 1393 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1394 | private void increaseBackoffSetting(EndPoint target) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1395 | final long now = SystemClock.elapsedRealtime(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1397 | final Pair<Long, Long> previousSettings = |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1398 | mSyncStorageEngine.getBackoff(target); |
Alon Albert | aeeb620 | 2010-12-09 16:14:02 -0800 | [diff] [blame] | 1399 | long newDelayInMs = -1; |
| 1400 | if (previousSettings != null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1401 | // Don't increase backoff before current backoff is expired. This will happen for op's |
Alon Albert | aeeb620 | 2010-12-09 16:14:02 -0800 | [diff] [blame] | 1402 | // with ignoreBackoff set. |
| 1403 | if (now < previousSettings.first) { |
| 1404 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1405 | Slog.v(TAG, "Still in backoff, do not increase it. " |
| 1406 | + "Remaining: " + ((previousSettings.first - now) / 1000) + " seconds."); |
Alon Albert | aeeb620 | 2010-12-09 16:14:02 -0800 | [diff] [blame] | 1407 | } |
| 1408 | return; |
| 1409 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1410 | // Subsequent delays are the double of the previous delay. |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 1411 | newDelayInMs = |
| 1412 | (long) (previousSettings.second * mConstants.getRetryTimeIncreaseFactor()); |
Alon Albert | aeeb620 | 2010-12-09 16:14:02 -0800 | [diff] [blame] | 1413 | } |
| 1414 | if (newDelayInMs <= 0) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1415 | // The initial delay is the jitterized INITIAL_SYNC_RETRY_TIME_IN_MS. |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 1416 | final long initialRetryMs = mConstants.getInitialSyncRetryTimeInSeconds() * 1000; |
| 1417 | newDelayInMs = jitterize(initialRetryMs, (long)(initialRetryMs * 1.1)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | } |
| 1419 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1420 | // Cap the delay. |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 1421 | final long maxSyncRetryTimeInSeconds = mConstants.getMaxSyncRetryTimeInSeconds(); |
| 1422 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1423 | if (newDelayInMs > maxSyncRetryTimeInSeconds * 1000) { |
| 1424 | newDelayInMs = maxSyncRetryTimeInSeconds * 1000; |
| 1425 | } |
| 1426 | |
Alon Albert | c1ac776 | 2010-10-28 13:35:55 -0700 | [diff] [blame] | 1427 | final long backoff = now + newDelayInMs; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1428 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1429 | Slog.v(TAG, "Backoff until: " + backoff + ", delayTime: " + newDelayInMs); |
| 1430 | } |
| 1431 | mSyncStorageEngine.setBackoff(target, backoff, newDelayInMs); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1432 | rescheduleSyncs(target, "increaseBackoffSetting"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1433 | } |
Alon Albert | c1ac776 | 2010-10-28 13:35:55 -0700 | [diff] [blame] | 1434 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1435 | /** |
| 1436 | * Reschedule all scheduled syncs for this EndPoint. The syncs will be scheduled according |
| 1437 | * to current backoff and delayUntil values of this EndPoint. |
| 1438 | */ |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1439 | private void rescheduleSyncs(EndPoint target, String why) { |
| 1440 | mLogger.log("rescheduleSyncs() ep=", target, " why=", why); |
| 1441 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1442 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1443 | int count = 0; |
| 1444 | for (SyncOperation op: ops) { |
| 1445 | if (!op.isPeriodic && op.target.matchesSpec(target)) { |
| 1446 | count++; |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1447 | cancelJob(op, why); |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 1448 | postScheduleSyncMessage(op, 0 /* min delay */); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1452 | Slog.v(TAG, "Rescheduled " + count + " syncs for " + target); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1453 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1454 | } |
| 1455 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1456 | private void setDelayUntilTime(EndPoint target, long delayUntilSeconds) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1457 | final long delayUntil = delayUntilSeconds * 1000; |
| 1458 | final long absoluteNow = System.currentTimeMillis(); |
| 1459 | long newDelayUntilTime; |
| 1460 | if (delayUntil > absoluteNow) { |
| 1461 | newDelayUntilTime = SystemClock.elapsedRealtime() + (delayUntil - absoluteNow); |
| 1462 | } else { |
| 1463 | newDelayUntilTime = 0; |
| 1464 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1465 | mSyncStorageEngine.setDelayUntilTime(target, newDelayUntilTime); |
| 1466 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1467 | Slog.v(TAG, "Delay Until time set to " + newDelayUntilTime + " for " + target); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1468 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1469 | rescheduleSyncs(target, "delayUntil newDelayUntilTime: " + newDelayUntilTime); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | private boolean isAdapterDelayed(EndPoint target) { |
| 1473 | long now = SystemClock.elapsedRealtime(); |
| 1474 | Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target); |
| 1475 | if (backoff != null && backoff.first != SyncStorageEngine.NOT_IN_BACKOFF_MODE |
| 1476 | && backoff.first > now) { |
| 1477 | return true; |
| 1478 | } |
| 1479 | if (mSyncStorageEngine.getDelayUntilTime(target) > now) { |
| 1480 | return true; |
| 1481 | } |
| 1482 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1486 | * Cancel the active sync if it matches the target. |
| 1487 | * @param info object containing info about which syncs to cancel. The target can |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1488 | * have null account/provider info to specify all accounts/providers. |
| 1489 | * @param extras if non-null, specifies the exact sync to remove. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1490 | */ |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1491 | public void cancelActiveSync(SyncStorageEngine.EndPoint info, Bundle extras, String why) { |
| 1492 | sendCancelSyncsMessage(info, extras, why); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1496 | * Schedule a sync operation with JobScheduler. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1497 | */ |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1498 | private void scheduleSyncOperationH(SyncOperation syncOperation) { |
| 1499 | scheduleSyncOperationH(syncOperation, 0L); |
| 1500 | } |
| 1501 | |
| 1502 | private void scheduleSyncOperationH(SyncOperation syncOperation, long minDelay) { |
| 1503 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
| 1504 | if (syncOperation == null) { |
| 1505 | Slog.e(TAG, "Can't schedule null sync operation."); |
| 1506 | return; |
| 1507 | } |
| 1508 | if (!syncOperation.ignoreBackoff()) { |
| 1509 | Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(syncOperation.target); |
| 1510 | if (backoff == null) { |
| 1511 | Slog.e(TAG, "Couldn't find backoff values for " + syncOperation.target); |
| 1512 | backoff = new Pair<Long, Long>(SyncStorageEngine.NOT_IN_BACKOFF_MODE, |
| 1513 | SyncStorageEngine.NOT_IN_BACKOFF_MODE); |
| 1514 | } |
| 1515 | long now = SystemClock.elapsedRealtime(); |
| 1516 | long backoffDelay = backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE ? 0 |
| 1517 | : backoff.first - now; |
| 1518 | long delayUntil = mSyncStorageEngine.getDelayUntilTime(syncOperation.target); |
| 1519 | long delayUntilDelay = delayUntil > now ? delayUntil - now : 0; |
| 1520 | if (isLoggable) { |
| 1521 | Slog.v(TAG, "backoff delay:" + backoffDelay |
| 1522 | + " delayUntil delay:" + delayUntilDelay); |
| 1523 | } |
| 1524 | minDelay = Math.max(minDelay, Math.max(backoffDelay, delayUntilDelay)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1525 | } |
| 1526 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1527 | if (minDelay < 0) { |
| 1528 | minDelay = 0; |
| 1529 | } |
| 1530 | |
| 1531 | // Check if duplicate syncs are pending. If found, keep one with least expected run time. |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1532 | |
| 1533 | // If any of the duplicate ones has exemption, then we inherit it. |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 1534 | if (!syncOperation.isPeriodic) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1535 | int inheritedSyncExemptionFlag = ContentResolver.SYNC_EXEMPTION_NONE; |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1536 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 1537 | // Check currently running syncs |
| 1538 | for (ActiveSyncContext asc: mActiveSyncContexts) { |
| 1539 | if (asc.mSyncOperation.key.equals(syncOperation.key)) { |
| 1540 | if (isLoggable) { |
| 1541 | Log.v(TAG, "Duplicate sync is already running. Not scheduling " |
| 1542 | + syncOperation); |
| 1543 | } |
| 1544 | return; |
| 1545 | } |
| 1546 | } |
| 1547 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1548 | int duplicatesCount = 0; |
| 1549 | long now = SystemClock.elapsedRealtime(); |
| 1550 | syncOperation.expectedRuntime = now + minDelay; |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1551 | List<SyncOperation> pending = getAllPendingSyncs(); |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1552 | SyncOperation syncToRun = syncOperation; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1553 | for (SyncOperation op : pending) { |
| 1554 | if (op.isPeriodic) { |
| 1555 | continue; |
| 1556 | } |
| 1557 | if (op.key.equals(syncOperation.key)) { |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1558 | if (syncToRun.expectedRuntime > op.expectedRuntime) { |
| 1559 | syncToRun = op; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1560 | } |
| 1561 | duplicatesCount++; |
| 1562 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1563 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1564 | if (duplicatesCount > 1) { |
| 1565 | Slog.e(TAG, "FATAL ERROR! File a bug if you see this."); |
| 1566 | } |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1567 | |
| 1568 | if (syncOperation != syncToRun) { |
| 1569 | // If there's a duplicate with an earlier run time that's not exempted, |
| 1570 | // and if the current operation is exempted with no minDelay, |
| 1571 | // cancel the duplicate one and keep the current one. |
| 1572 | // |
| 1573 | // This means the duplicate one has a negative expected run time, but it hasn't |
| 1574 | // been executed possibly because of app-standby. |
| 1575 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1576 | if ((minDelay == 0) |
| 1577 | && (syncToRun.syncExemptionFlag < syncOperation.syncExemptionFlag)) { |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1578 | syncToRun = syncOperation; |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1579 | inheritedSyncExemptionFlag = |
| 1580 | Math.max(inheritedSyncExemptionFlag, syncToRun.syncExemptionFlag); |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1581 | } |
| 1582 | } |
| 1583 | |
| 1584 | // Cancel all other duplicate syncs. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1585 | for (SyncOperation op : pending) { |
| 1586 | if (op.isPeriodic) { |
| 1587 | continue; |
| 1588 | } |
| 1589 | if (op.key.equals(syncOperation.key)) { |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1590 | if (op != syncToRun) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1591 | if (isLoggable) { |
| 1592 | Slog.v(TAG, "Cancelling duplicate sync " + op); |
| 1593 | } |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1594 | inheritedSyncExemptionFlag = |
| 1595 | Math.max(inheritedSyncExemptionFlag, op.syncExemptionFlag); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1596 | cancelJob(op, "scheduleSyncOperationH-duplicate"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | } |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1600 | if (syncToRun != syncOperation) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1601 | // Don't schedule because a duplicate sync with earlier expected runtime exists. |
| 1602 | if (isLoggable) { |
| 1603 | Slog.v(TAG, "Not scheduling because a duplicate exists."); |
| 1604 | } |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1605 | |
| 1606 | // TODO Should we give the winning one SYNC_EXTRAS_APP_STANDBY_EXEMPTED |
| 1607 | // if the current one has it? |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1608 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | } |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1610 | |
| 1611 | // If any of the duplicates had exemption, we exempt the current one. |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1612 | // |
| 1613 | if (inheritedSyncExemptionFlag > ContentResolver.SYNC_EXEMPTION_NONE) { |
| 1614 | syncOperation.syncExemptionFlag = inheritedSyncExemptionFlag; |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1615 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1616 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1617 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 1618 | // Syncs that are re-scheduled shouldn't get a new job id. |
| 1619 | if (syncOperation.jobId == SyncOperation.NO_JOB_ID) { |
| 1620 | syncOperation.jobId = getUnusedJobIdH(); |
| 1621 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1622 | |
| 1623 | if (isLoggable) { |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 1624 | Slog.v(TAG, "scheduling sync operation " + syncOperation.toString()); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1625 | } |
| 1626 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1627 | int priority = syncOperation.findPriority(); |
| 1628 | |
| 1629 | final int networkType = syncOperation.isNotAllowedOnMetered() ? |
| 1630 | JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY; |
| 1631 | |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1632 | // Note this logic means when an exempted sync fails, |
| 1633 | // the back-off one will inherit it too, and will be exempted from app-standby. |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1634 | final int jobFlags = syncOperation.isAppStandbyExempted() |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1635 | ? JobInfo.FLAG_EXEMPT_FROM_APP_STANDBY : 0; |
| 1636 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1637 | JobInfo.Builder b = new JobInfo.Builder(syncOperation.jobId, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 1638 | new ComponentName(mContext, SyncJobService.class)) |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1639 | .setExtras(syncOperation.toJobInfoExtras()) |
| 1640 | .setRequiredNetworkType(networkType) |
| 1641 | .setPersisted(true) |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1642 | .setPriority(priority) |
| 1643 | .setFlags(jobFlags); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1644 | |
| 1645 | if (syncOperation.isPeriodic) { |
| 1646 | b.setPeriodic(syncOperation.periodMillis, syncOperation.flexMillis); |
| 1647 | } else { |
| 1648 | if (minDelay > 0) { |
| 1649 | b.setMinimumLatency(minDelay); |
| 1650 | } |
| 1651 | getSyncStorageEngine().markPending(syncOperation.target, true); |
| 1652 | } |
| 1653 | |
| 1654 | if (syncOperation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_REQUIRE_CHARGING)) { |
| 1655 | b.setRequiresCharging(true); |
| 1656 | } |
| 1657 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1658 | if (syncOperation.syncExemptionFlag |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1659 | == ContentResolver.SYNC_EXEMPTION_PROMOTE_BUCKET_WITH_TEMP) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1660 | DeviceIdleController.LocalService dic = |
| 1661 | LocalServices.getService(DeviceIdleController.LocalService.class); |
| 1662 | if (dic != null) { |
| 1663 | dic.addPowerSaveTempWhitelistApp(Process.SYSTEM_UID, |
| 1664 | syncOperation.owningPackage, |
| 1665 | mConstants.getKeyExemptionTempWhitelistDurationInSeconds() * 1000, |
| 1666 | UserHandle.getUserId(syncOperation.owningUid), |
| 1667 | /* sync=*/ false, "sync by top app"); |
| 1668 | } |
| 1669 | } |
| 1670 | |
Makoto Onuki | d5f25d2 | 2018-05-22 16:02:17 -0700 | [diff] [blame] | 1671 | if (syncOperation.isAppStandbyExempted()) { |
| 1672 | final UsageStatsManagerInternal usmi = LocalServices.getService( |
| 1673 | UsageStatsManagerInternal.class); |
| 1674 | if (usmi != null) { |
| 1675 | usmi.reportExemptedSyncScheduled(syncOperation.owningPackage, |
| 1676 | UserHandle.getUserId(syncOperation.owningUid)); |
| 1677 | } |
| 1678 | } |
| 1679 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1680 | getJobScheduler().scheduleAsPackage(b.build(), syncOperation.owningPackage, |
Shreyas Basarge | eda34e4 | 2016-04-26 00:14:02 +0100 | [diff] [blame] | 1681 | syncOperation.target.userId, syncOperation.wakeLockName()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | } |
| 1683 | |
| 1684 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 1685 | * Remove scheduled sync operations. |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1686 | * @param info limit the removals to operations that match this target. The target can |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1687 | * have null account/provider info to specify all accounts/providers. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1688 | */ |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1689 | public void clearScheduledSyncOperations(SyncStorageEngine.EndPoint info) { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1690 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1691 | for (SyncOperation op: ops) { |
| 1692 | if (!op.isPeriodic && op.target.matchesSpec(info)) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1693 | cancelJob(op, "clearScheduledSyncOperations"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1694 | getSyncStorageEngine().markPending(op.target, false); |
| 1695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1696 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1697 | mSyncStorageEngine.setBackoff(info, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1698 | SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | } |
| 1700 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1701 | /** |
| 1702 | * Remove a specified sync, if it exists. |
| 1703 | * @param info Authority for which the sync is to be removed. |
| 1704 | * @param extras extras bundle to uniquely identify sync. |
| 1705 | */ |
| 1706 | public void cancelScheduledSyncOperation(SyncStorageEngine.EndPoint info, Bundle extras) { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1707 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1708 | for (SyncOperation op: ops) { |
| 1709 | if (!op.isPeriodic && op.target.matchesSpec(info) |
| 1710 | && syncExtrasEquals(extras, op.extras, false)) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1711 | cancelJob(op, "cancelScheduledSyncOperation"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1712 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1713 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1714 | setAuthorityPendingState(info); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1715 | // Reset the back-off if there are no more syncs pending. |
| 1716 | if (!mSyncStorageEngine.isSyncPending(info)) { |
| 1717 | mSyncStorageEngine.setBackoff(info, |
| 1718 | SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE); |
| 1719 | } |
| 1720 | } |
| 1721 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1722 | private void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) { |
| 1723 | final boolean isLoggable = Log.isLoggable(TAG, Log.DEBUG); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1724 | if (isLoggable) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1725 | Log.d(TAG, "encountered error(s) during the sync: " + syncResult + ", " + operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1726 | } |
| 1727 | |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1728 | // The SYNC_EXTRAS_IGNORE_BACKOFF only applies to the first attempt to sync a given |
| 1729 | // request. Retries of the request will always honor the backoff, so clear the |
| 1730 | // flag in case we retry this request. |
| 1731 | if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)) { |
| 1732 | operation.extras.remove(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF); |
| 1733 | } |
| 1734 | |
Shreyas Basarge | bd4c3ea | 2016-06-16 11:54:35 +0100 | [diff] [blame] | 1735 | if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false) |
| 1736 | && !syncResult.syncAlreadyInProgress) { |
| 1737 | // syncAlreadyInProgress flag is set by AbstractThreadedSyncAdapter. The sync adapter |
| 1738 | // has no way of knowing that a sync error occured. So we DO retry if the error is |
| 1739 | // syncAlreadyInProgress. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1740 | if (isLoggable) { |
| 1741 | Log.d(TAG, "not retrying sync operation because SYNC_EXTRAS_DO_NOT_RETRY was specified " |
| 1742 | + operation); |
| 1743 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1744 | } else if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false) |
| 1745 | && !syncResult.syncAlreadyInProgress) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1746 | // If this was an upward sync then schedule a two-way sync immediately. |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1747 | operation.extras.remove(ContentResolver.SYNC_EXTRAS_UPLOAD); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1748 | if (isLoggable) { |
| 1749 | Log.d(TAG, "retrying sync operation as a two-way sync because an upload-only sync " |
| 1750 | + "encountered an error: " + operation); |
| 1751 | } |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 1752 | scheduleSyncOperationH(operation); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1753 | } else if (syncResult.tooManyRetries) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1754 | // If this sync aborted because the internal sync loop retried too many times then |
| 1755 | // don't reschedule. Otherwise we risk getting into a retry loop. |
| 1756 | if (isLoggable) { |
| 1757 | Log.d(TAG, "not retrying sync operation because it retried too many times: " |
| 1758 | + operation); |
| 1759 | } |
Fred Quintana | aa7edda | 2009-12-03 14:18:58 -0800 | [diff] [blame] | 1760 | } else if (syncResult.madeSomeProgress()) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1761 | // If the operation succeeded to some extent then retry immediately. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1762 | if (isLoggable) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1763 | Log.d(TAG, "retrying sync operation because even though it had an error " |
| 1764 | + "it achieved some success"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1765 | } |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 1766 | scheduleSyncOperationH(operation); |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 1767 | } else if (syncResult.syncAlreadyInProgress) { |
| 1768 | if (isLoggable) { |
| 1769 | Log.d(TAG, "retrying sync operation that failed because there was already a " |
| 1770 | + "sync in progress: " + operation); |
| 1771 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1772 | scheduleSyncOperationH(operation, DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS * 1000); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1773 | } else if (syncResult.hasSoftError()) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1774 | // If this was a two-way sync then retry soft errors with an exponential backoff. |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1775 | if (isLoggable) { |
| 1776 | Log.d(TAG, "retrying sync operation because it encountered a soft error: " |
| 1777 | + operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1778 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1779 | scheduleSyncOperationH(operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1780 | } else { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1781 | // Otherwise do not reschedule. |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1782 | Log.d(TAG, "not retrying sync operation because the error is a hard error: " |
| 1783 | + operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1784 | } |
| 1785 | } |
| 1786 | |
Jeff Sharkey | 9d8a104 | 2015-12-03 17:56:20 -0700 | [diff] [blame] | 1787 | private void onUserUnlocked(int userId) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1788 | // Make sure that accounts we're about to use are valid. |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1789 | AccountManagerService.getSingleton().validateAccounts(userId); |
| 1790 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1791 | mSyncAdapters.invalidateCache(userId); |
| 1792 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1793 | EndPoint target = new EndPoint(null, null, userId); |
| 1794 | updateRunningAccounts(target); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1795 | |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 1796 | // Schedule sync for any accounts under started user, but only the NOT_INITIALIZED adapters. |
Svetoslav | f3f02ac | 2015-09-08 14:36:35 -0700 | [diff] [blame] | 1797 | final Account[] accounts = AccountManagerService.getSingleton().getAccounts(userId, |
| 1798 | mContext.getOpPackageName()); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1799 | for (Account account : accounts) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1800 | scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1801 | AuthorityInfo.NOT_INITIALIZED, ContentResolver.SYNC_EXEMPTION_NONE); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1802 | } |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 1803 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1804 | |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 1805 | private void onUserStopped(int userId) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1806 | updateRunningAccounts(null /* Don't sync any target */); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1807 | |
| 1808 | cancelActiveSync( |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1809 | new SyncStorageEngine.EndPoint( |
| 1810 | null /* any account */, |
| 1811 | null /* any authority */, |
| 1812 | userId), |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1813 | null /* any sync. */, |
| 1814 | "onUserStopped" |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1815 | ); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | private void onUserRemoved(int userId) { |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 1819 | mLogger.log("onUserRemoved: u", userId); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1820 | updateRunningAccounts(null /* Don't sync any target */); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1821 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1822 | // Clean up the storage engine database |
| 1823 | mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId); |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1824 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1825 | for (SyncOperation op: ops) { |
| 1826 | if (op.target.userId == userId) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 1827 | cancelJob(op, "user removed u" + userId); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | } |
| 1831 | |
Amith Yamasani | 96a0fd65 | 2015-04-10 16:16:30 -0700 | [diff] [blame] | 1832 | /** |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1833 | * Construct intent used to bind to an adapter. |
| 1834 | * |
| 1835 | * @param context Context to create intent for |
| 1836 | * @param syncAdapterComponent The adapter description |
| 1837 | * @param userId The user the adapter belongs to |
| 1838 | * |
| 1839 | * @return The intent required to bind to the adapter |
| 1840 | */ |
| 1841 | static @NonNull Intent getAdapterBindIntent(@NonNull Context context, |
| 1842 | @NonNull ComponentName syncAdapterComponent, @UserIdInt int userId) { |
| 1843 | final Intent intent = new Intent(); |
| 1844 | intent.setAction("android.content.SyncAdapter"); |
| 1845 | intent.setComponent(syncAdapterComponent); |
| 1846 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 1847 | com.android.internal.R.string.sync_binding_label); |
| 1848 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(context, 0, |
| 1849 | new Intent(Settings.ACTION_SYNC_SETTINGS), 0, null, UserHandle.of(userId))); |
| 1850 | |
| 1851 | return intent; |
| 1852 | } |
| 1853 | |
| 1854 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1855 | * @hide |
| 1856 | */ |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 1857 | class ActiveSyncContext extends ISyncContext.Stub |
| 1858 | implements ServiceConnection, IBinder.DeathRecipient { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1859 | final SyncOperation mSyncOperation; |
| 1860 | final long mHistoryRowId; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1861 | ISyncAdapter mSyncAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1862 | final long mStartTime; |
| 1863 | long mTimeoutStartTime; |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1864 | boolean mBound; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1865 | final PowerManager.WakeLock mSyncWakeLock; |
| 1866 | final int mSyncAdapterUid; |
| 1867 | SyncInfo mSyncInfo; |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 1868 | boolean mIsLinkedToDeath = false; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1869 | String mEventName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1870 | |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 1871 | /** Total bytes transferred, counted at {@link #mLastPolledTimeElapsed} */ |
| 1872 | long mBytesTransferredAtLastPoll; |
| 1873 | /** |
| 1874 | * Last point in {@link SystemClock#elapsedRealtime()} at which we checked the # of bytes |
| 1875 | * transferred to/fro by this adapter. |
| 1876 | */ |
| 1877 | long mLastPolledTimeElapsed; |
| 1878 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1879 | /** |
| 1880 | * Create an ActiveSyncContext for an impending sync and grab the wakelock for that |
| 1881 | * sync adapter. Since this grabs the wakelock you need to be sure to call |
| 1882 | * close() when you are done with this ActiveSyncContext, whether the sync succeeded |
| 1883 | * or not. |
| 1884 | * @param syncOperation the SyncOperation we are about to sync |
| 1885 | * @param historyRowId the row in which to record the history info for this sync |
| 1886 | * @param syncAdapterUid the UID of the application that contains the sync adapter |
| 1887 | * for this sync. This is used to attribute the wakelock hold to that application. |
| 1888 | */ |
| 1889 | public ActiveSyncContext(SyncOperation syncOperation, long historyRowId, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 1890 | int syncAdapterUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | super(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1892 | mSyncAdapterUid = syncAdapterUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1893 | mSyncOperation = syncOperation; |
| 1894 | mHistoryRowId = historyRowId; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1895 | mSyncAdapter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1896 | mStartTime = SystemClock.elapsedRealtime(); |
| 1897 | mTimeoutStartTime = mStartTime; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1898 | mSyncWakeLock = mSyncHandler.getSyncWakeLock(mSyncOperation); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1899 | mSyncWakeLock.setWorkSource(new WorkSource(syncAdapterUid)); |
| 1900 | mSyncWakeLock.acquire(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1901 | } |
| 1902 | |
| 1903 | public void sendHeartbeat() { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1904 | // Heartbeats are no longer used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1905 | } |
| 1906 | |
| 1907 | public void onFinished(SyncResult result) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1908 | if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "onFinished: " + this); |
| 1909 | // Include "this" in the message so that the handler can ignore it if this |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1910 | // ActiveSyncContext is no longer the mActiveSyncContext at message handling |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 1911 | // time. |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 1912 | mLogger.log("onFinished result=", result, " endpoint=", |
| 1913 | (mSyncOperation == null ? "null" : mSyncOperation.target)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1914 | sendSyncFinishedOrCanceledMessage(this, result); |
| 1915 | } |
| 1916 | |
| 1917 | public void toString(StringBuilder sb) { |
| 1918 | sb.append("startTime ").append(mStartTime) |
| 1919 | .append(", mTimeoutStartTime ").append(mTimeoutStartTime) |
| 1920 | .append(", mHistoryRowId ").append(mHistoryRowId) |
| 1921 | .append(", syncOperation ").append(mSyncOperation); |
| 1922 | } |
| 1923 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1924 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 1925 | Message msg = mSyncHandler.obtainMessage(); |
| 1926 | msg.what = SyncHandler.MESSAGE_SERVICE_CONNECTED; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1927 | msg.obj = new ServiceConnectionData(this, service); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1928 | mSyncHandler.sendMessage(msg); |
| 1929 | } |
| 1930 | |
| 1931 | public void onServiceDisconnected(ComponentName name) { |
| 1932 | Message msg = mSyncHandler.obtainMessage(); |
| 1933 | msg.what = SyncHandler.MESSAGE_SERVICE_DISCONNECTED; |
| 1934 | msg.obj = new ServiceConnectionData(this, null); |
| 1935 | mSyncHandler.sendMessage(msg); |
| 1936 | } |
| 1937 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1938 | boolean bindToSyncAdapter(ComponentName serviceComponent, int userId) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1939 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 1940 | Log.d(TAG, "bindToSyncAdapter: " + serviceComponent + ", connection " + this); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1941 | } |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1942 | Intent intent = getAdapterBindIntent(mContext, serviceComponent, userId); |
| 1943 | |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1944 | mBound = true; |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1945 | final boolean bindResult = mContext.bindServiceAsUser(intent, this, |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 1946 | SYNC_ADAPTER_CONNECTION_FLAGS, new UserHandle(mSyncOperation.target.userId)); |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 1947 | mLogger.log("bindService() returned=", mBound, " for ", this); |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1948 | if (!bindResult) { |
| 1949 | mBound = false; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1950 | } else { |
| 1951 | try { |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 1952 | mEventName = mSyncOperation.wakeLockName(); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1953 | mBatteryStats.noteSyncStart(mEventName, mSyncAdapterUid); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1954 | } catch (RemoteException e) { |
| 1955 | } |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1956 | } |
| 1957 | return bindResult; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1960 | /** |
| 1961 | * Performs the required cleanup, which is the releasing of the wakelock and |
| 1962 | * unbinding from the sync adapter (if actually bound). |
| 1963 | */ |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1964 | protected void close() { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1965 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1966 | Log.d(TAG, "unBindFromSyncAdapter: connection " + this); |
| 1967 | } |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1968 | if (mBound) { |
| 1969 | mBound = false; |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 1970 | mLogger.log("unbindService for ", this); |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1971 | mContext.unbindService(this); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1972 | try { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1973 | mBatteryStats.noteSyncFinish(mEventName, mSyncAdapterUid); |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 1974 | } catch (RemoteException e) { |
| 1975 | } |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1976 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1977 | mSyncWakeLock.release(); |
Dianne Hackborn | c24ab86 | 2011-10-18 15:55:03 -0700 | [diff] [blame] | 1978 | mSyncWakeLock.setWorkSource(null); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1979 | } |
| 1980 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1981 | public String toString() { |
| 1982 | StringBuilder sb = new StringBuilder(); |
| 1983 | toString(sb); |
| 1984 | return sb.toString(); |
| 1985 | } |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 1986 | |
| 1987 | @Override |
| 1988 | public void binderDied() { |
| 1989 | sendSyncFinishedOrCanceledMessage(this, null); |
| 1990 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1991 | } |
| 1992 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 1993 | protected void dump(FileDescriptor fd, PrintWriter pw, boolean dumpAll) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1994 | final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 1995 | |
| 1996 | final SyncAdapterStateFetcher buckets = new SyncAdapterStateFetcher(); |
| 1997 | |
| 1998 | dumpSyncState(ipw, buckets); |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 1999 | mConstants.dump(pw, ""); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2000 | dumpSyncAdapters(ipw); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 2001 | |
| 2002 | if (dumpAll) { |
| 2003 | ipw.println("Detailed Sync History"); |
| 2004 | mLogger.dumpAll(pw); |
| 2005 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | } |
| 2007 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2008 | static String formatTime(long time) { |
Makoto Onuki | f74cf94 | 2018-04-16 17:04:58 -0700 | [diff] [blame] | 2009 | if (time == 0) { |
| 2010 | return "N/A"; |
| 2011 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2012 | Time tobj = new Time(); |
| 2013 | tobj.set(time); |
| 2014 | return tobj.format("%Y-%m-%d %H:%M:%S"); |
| 2015 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2016 | |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2017 | private final static Comparator<SyncOperation> sOpDumpComparator = (op1, op2) -> { |
| 2018 | int res = Integer.compare(op1.target.userId, op2.target.userId); |
| 2019 | if (res != 0) return res; |
| 2020 | |
| 2021 | final Comparator<String> stringComparator = String.CASE_INSENSITIVE_ORDER; |
| 2022 | |
| 2023 | res = stringComparator.compare(op1.target.account.type, op2.target.account.type); |
| 2024 | if (res != 0) return res; |
| 2025 | |
| 2026 | res = stringComparator.compare(op1.target.account.name, op2.target.account.name); |
| 2027 | if (res != 0) return res; |
| 2028 | |
| 2029 | res = stringComparator.compare(op1.target.provider, op2.target.provider); |
| 2030 | if (res != 0) return res; |
| 2031 | |
| 2032 | res = Integer.compare(op1.reason, op2.reason); |
| 2033 | if (res != 0) return res; |
| 2034 | |
| 2035 | res = Long.compare(op1.periodMillis, op2.periodMillis); |
| 2036 | if (res != 0) return res; |
| 2037 | |
| 2038 | res = Long.compare(op1.expectedRuntime, op2.expectedRuntime); |
| 2039 | if (res != 0) return res; |
| 2040 | |
| 2041 | res = Long.compare(op1.jobId, op2.jobId); |
| 2042 | if (res != 0) return res; |
| 2043 | |
| 2044 | return 0; |
| 2045 | }; |
| 2046 | |
| 2047 | private final static Comparator<SyncOperation> sOpRuntimeComparator = (op1, op2) -> { |
| 2048 | int res = Long.compare(op1.expectedRuntime, op2.expectedRuntime); |
| 2049 | if (res != 0) return res; |
| 2050 | |
| 2051 | return sOpDumpComparator.compare(op1, op2); |
| 2052 | }; |
| 2053 | |
| 2054 | private static <T> int countIf(Collection<T> col, Predicate<T> p) { |
| 2055 | int ret = 0; |
| 2056 | for (T item : col) { |
| 2057 | if (p.test(item)) ret++; |
| 2058 | } |
| 2059 | return ret; |
| 2060 | } |
| 2061 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2062 | protected void dumpPendingSyncs(PrintWriter pw, SyncAdapterStateFetcher buckets) { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2063 | List<SyncOperation> pendingSyncs = getAllPendingSyncs(); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2064 | |
| 2065 | pw.print("Pending Syncs: "); |
| 2066 | pw.println(countIf(pendingSyncs, op -> !op.isPeriodic)); |
| 2067 | |
| 2068 | Collections.sort(pendingSyncs, sOpRuntimeComparator); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2069 | int count = 0; |
| 2070 | for (SyncOperation op: pendingSyncs) { |
| 2071 | if (!op.isPeriodic) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2072 | pw.println(op.dump(null, false, buckets)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2073 | count++; |
| 2074 | } |
| 2075 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2076 | pw.println(); |
| 2077 | } |
| 2078 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2079 | protected void dumpPeriodicSyncs(PrintWriter pw, SyncAdapterStateFetcher buckets) { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 2080 | List<SyncOperation> pendingSyncs = getAllPendingSyncs(); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2081 | |
| 2082 | pw.print("Periodic Syncs: "); |
| 2083 | pw.println(countIf(pendingSyncs, op -> op.isPeriodic)); |
| 2084 | |
| 2085 | Collections.sort(pendingSyncs, sOpDumpComparator); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2086 | int count = 0; |
| 2087 | for (SyncOperation op: pendingSyncs) { |
| 2088 | if (op.isPeriodic) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2089 | pw.println(op.dump(null, false, buckets)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2090 | count++; |
| 2091 | } |
| 2092 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2093 | pw.println(); |
| 2094 | } |
| 2095 | |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2096 | /** |
| 2097 | * Similar to {@link android.util.TimeUtils#formatDuration}, but it's more suitable and concise |
| 2098 | * for the sync manager dumpsys. (Don't add the leading + sign, don't show milliseconds.) |
| 2099 | */ |
| 2100 | public static StringBuilder formatDurationHMS(StringBuilder sb, long duration) { |
| 2101 | duration /= 1000; |
| 2102 | if (duration < 0) { |
| 2103 | sb.append('-'); |
| 2104 | duration = -duration; |
| 2105 | } |
| 2106 | final long seconds = duration % 60; |
| 2107 | duration /= 60; |
| 2108 | |
| 2109 | final long minutes = duration % 60; |
| 2110 | duration /= 60; |
| 2111 | |
| 2112 | final long hours = duration % 24; |
| 2113 | duration /= 24; |
| 2114 | |
| 2115 | final long days = duration; |
| 2116 | |
| 2117 | boolean print = false; |
| 2118 | if (days > 0) { |
| 2119 | sb.append(days); |
| 2120 | sb.append('d'); |
| 2121 | print = true; |
| 2122 | } |
| 2123 | print = printTwoDigitNumber(sb, hours, 'h', print); |
| 2124 | print = printTwoDigitNumber(sb, minutes, 'm', print); |
| 2125 | print = printTwoDigitNumber(sb, seconds, 's', print); |
| 2126 | if (!print) { |
| 2127 | sb.append("0s"); |
| 2128 | } |
| 2129 | |
| 2130 | return sb; |
| 2131 | } |
| 2132 | |
| 2133 | private static boolean printTwoDigitNumber(StringBuilder sb, long value, char unit, |
| 2134 | boolean always) { |
| 2135 | if (!always && (value == 0)) { |
| 2136 | return false; |
| 2137 | } |
| 2138 | if (always && (value < 10)) { |
| 2139 | sb.append('0'); |
| 2140 | } |
| 2141 | sb.append(value); |
| 2142 | sb.append(unit); |
| 2143 | return true; |
| 2144 | } |
| 2145 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2146 | protected void dumpSyncState(PrintWriter pw, SyncAdapterStateFetcher buckets) { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2147 | final StringBuilder sb = new StringBuilder(); |
| 2148 | |
Makoto Onuki | 1ba9ebc | 2018-02-15 10:39:26 -0800 | [diff] [blame] | 2149 | pw.print("Data connected: "); pw.println(mDataConnectionIsConnected); |
| 2150 | pw.print("Battery saver: "); |
| 2151 | pw.println((mPowerManager != null) && mPowerManager.isPowerSaveMode()); |
| 2152 | |
| 2153 | pw.print("Background network restriction: "); |
| 2154 | { |
| 2155 | final ConnectivityManager cm = getConnectivityManager(); |
| 2156 | final int status = (cm == null) ? -1 : cm.getRestrictBackgroundStatus(); |
| 2157 | switch (status) { |
| 2158 | case ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED: |
| 2159 | pw.println(" disabled"); |
| 2160 | break; |
| 2161 | case ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELISTED: |
| 2162 | pw.println(" whitelisted"); |
| 2163 | break; |
| 2164 | case ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED: |
| 2165 | pw.println(" enabled"); |
| 2166 | break; |
| 2167 | default: |
| 2168 | pw.print("Unknown("); |
| 2169 | pw.print(status); |
| 2170 | pw.println(")"); |
| 2171 | break; |
| 2172 | } |
| 2173 | } |
| 2174 | |
| 2175 | pw.print("Auto sync: "); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2176 | List<UserInfo> users = getAllUsers(); |
| 2177 | if (users != null) { |
| 2178 | for (UserInfo user : users) { |
| 2179 | pw.print("u" + user.id + "=" |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2180 | + mSyncStorageEngine.getMasterSyncAutomatically(user.id) + " "); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2181 | } |
| 2182 | pw.println(); |
| 2183 | } |
Makoto Onuki | 1ba9ebc | 2018-02-15 10:39:26 -0800 | [diff] [blame] | 2184 | pw.print("Memory low: "); pw.println(mStorageIsLow); |
| 2185 | pw.print("Device idle: "); pw.println(mDeviceIsIdle); |
| 2186 | pw.print("Reported active: "); pw.println(mReportedSyncActive); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2187 | pw.print("Clock valid: "); pw.println(mSyncStorageEngine.isClockValid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2188 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 2189 | final AccountAndUser[] accounts = AccountManagerService.getSingleton().getAllAccounts(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2190 | |
Makoto Onuki | 1ba9ebc | 2018-02-15 10:39:26 -0800 | [diff] [blame] | 2191 | pw.print("Accounts: "); |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2192 | if (accounts != INITIAL_ACCOUNTS_ARRAY) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2193 | pw.println(accounts.length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2194 | } else { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2195 | pw.println("not known yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2196 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2197 | final long now = SystemClock.elapsedRealtime(); |
Makoto Onuki | 1ba9ebc | 2018-02-15 10:39:26 -0800 | [diff] [blame] | 2198 | pw.print("Now: "); pw.print(now); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2199 | pw.println(" (" + formatTime(System.currentTimeMillis()) + ")"); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2200 | |
| 2201 | sb.setLength(0); |
Makoto Onuki | 1ba9ebc | 2018-02-15 10:39:26 -0800 | [diff] [blame] | 2202 | pw.print("Uptime: "); pw.print(formatDurationHMS(sb, now)); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2203 | pw.println(); |
Makoto Onuki | 1ba9ebc | 2018-02-15 10:39:26 -0800 | [diff] [blame] | 2204 | pw.print("Time spent syncing: "); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2205 | |
| 2206 | sb.setLength(0); |
| 2207 | pw.print(formatDurationHMS(sb, |
| 2208 | mSyncHandler.mSyncTimeTracker.timeSpentSyncing())); |
| 2209 | pw.print(", sync "); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2210 | pw.print(mSyncHandler.mSyncTimeTracker.mLastWasSyncing ? "" : "not "); |
| 2211 | pw.println("in progress"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2212 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2213 | pw.println(); |
| 2214 | pw.println("Active Syncs: " + mActiveSyncContexts.size()); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2215 | final PackageManager pm = mContext.getPackageManager(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2216 | for (SyncManager.ActiveSyncContext activeSyncContext : mActiveSyncContexts) { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2217 | final long durationInSeconds = (now - activeSyncContext.mStartTime); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2218 | pw.print(" "); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2219 | sb.setLength(0); |
| 2220 | pw.print(formatDurationHMS(sb, durationInSeconds)); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2221 | pw.print(" - "); |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2222 | pw.print(activeSyncContext.mSyncOperation.dump(pm, false, buckets)); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2223 | pw.println(); |
| 2224 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2225 | pw.println(); |
| 2226 | |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 2227 | dumpPendingSyncs(pw, buckets); |
| 2228 | dumpPeriodicSyncs(pw, buckets); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2229 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2230 | // Join the installed sync adapter with the accounts list and emit for everything. |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2231 | pw.println("Sync Status"); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2232 | |
| 2233 | final ArrayList<Pair<EndPoint, SyncStatusInfo>> statuses = new ArrayList<>(); |
| 2234 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2235 | mSyncStorageEngine.resetTodayStats(/* force=*/ false); |
| 2236 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2237 | for (AccountAndUser account : accounts) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2238 | pw.printf("Account %s u%d %s\n", |
| 2239 | account.account.name, account.userId, account.account.type); |
| 2240 | |
| 2241 | pw.println("======================================================================="); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2242 | final PrintTable table = new PrintTable(16); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2243 | table.set(0, 0, |
| 2244 | "Authority", // 0 |
| 2245 | "Syncable", // 1 |
| 2246 | "Enabled", // 2 |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2247 | |
| 2248 | "Stats", // 3 "Total", "Today" or "Yesterday". |
| 2249 | |
| 2250 | "Loc", // 4 # of syncs with local sources. (including failures/cancels. ) |
| 2251 | "Poll", // 5 "poll" syncs. |
| 2252 | "Per", // 6 Periodic syncs. |
| 2253 | "Feed", // 7 Syncs with a "feed" extra. (subscribedfeeds?) |
| 2254 | "User", // 8 User-initiated |
| 2255 | "Othr", // 9 Other sources. |
| 2256 | |
| 2257 | "Tot", // 10 Total syncs (including failures / cancels) |
| 2258 | "Fail", // 11 (Failure) |
| 2259 | "Can", // 12 (Cancel) |
| 2260 | |
| 2261 | "Time", // 13 Total time |
| 2262 | "Last Sync", // 14 |
| 2263 | "Backoff" // 15 |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2264 | ); |
| 2265 | |
| 2266 | final List<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> sorted = |
| 2267 | Lists.newArrayList(); |
| 2268 | sorted.addAll(mSyncAdapters.getAllServices(account.userId)); |
| 2269 | Collections.sort(sorted, |
| 2270 | new Comparator<RegisteredServicesCache.ServiceInfo<SyncAdapterType>>() { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2271 | @Override |
| 2272 | public int compare(RegisteredServicesCache.ServiceInfo<SyncAdapterType> lhs, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 2273 | RegisteredServicesCache.ServiceInfo<SyncAdapterType> rhs) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2274 | return lhs.type.authority.compareTo(rhs.type.authority); |
| 2275 | } |
| 2276 | }); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2277 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterType : sorted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2278 | if (!syncAdapterType.type.accountType.equals(account.account.type)) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2279 | continue; |
| 2280 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2281 | int row = table.getNumRows(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2282 | Pair<AuthorityInfo, SyncStatusInfo> syncAuthoritySyncStatus = |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 2283 | mSyncStorageEngine.getCopyOfAuthorityWithSyncStatus( |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2284 | new SyncStorageEngine.EndPoint( |
| 2285 | account.account, |
| 2286 | syncAdapterType.type.authority, |
| 2287 | account.userId)); |
Georgi Nikolov | dbe846b | 2013-06-25 14:09:56 -0700 | [diff] [blame] | 2288 | SyncStorageEngine.AuthorityInfo settings = syncAuthoritySyncStatus.first; |
| 2289 | SyncStatusInfo status = syncAuthoritySyncStatus.second; |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2290 | statuses.add(Pair.create(settings.target, status)); |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2291 | String authority = settings.target.provider; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2292 | if (authority.length() > 50) { |
| 2293 | authority = authority.substring(authority.length() - 50); |
| 2294 | } |
| 2295 | table.set(row, 0, authority, settings.syncable, settings.enabled); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2296 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2297 | QuadConsumer<String, Stats, Function<Integer, String>, Integer> c = |
| 2298 | (label, stats, filter, r) -> { |
| 2299 | sb.setLength(0); |
| 2300 | table.set(r, 3, |
| 2301 | label, |
| 2302 | filter.apply(stats.numSourceLocal), |
| 2303 | filter.apply(stats.numSourcePoll), |
| 2304 | filter.apply(stats.numSourcePeriodic), |
| 2305 | filter.apply(stats.numSourceFeed), |
| 2306 | filter.apply(stats.numSourceUser), |
| 2307 | filter.apply(stats.numSourceOther), |
| 2308 | filter.apply(stats.numSyncs), |
| 2309 | filter.apply(stats.numFailures), |
| 2310 | filter.apply(stats.numCancels), |
| 2311 | formatDurationHMS(sb, stats.totalElapsedTime)); |
| 2312 | }; |
| 2313 | c.accept("Total", status.totalStats, (i) -> Integer.toString(i), row); |
| 2314 | c.accept("Today", status.todayStats, this::zeroToEmpty, row + 1); |
| 2315 | c.accept("Yestr", status.yesterdayStats, this::zeroToEmpty, row + 2); |
| 2316 | |
| 2317 | final int LAST_SYNC = 14; |
| 2318 | final int BACKOFF = LAST_SYNC + 1; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2319 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2320 | int row1 = row; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2321 | if (settings.delayUntil > now) { |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2322 | table.set(row1++, BACKOFF, "D: " + (settings.delayUntil - now) / 1000); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2323 | if (settings.backoffTime > now) { |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2324 | table.set(row1++, BACKOFF, "B: " + (settings.backoffTime - now) / 1000); |
| 2325 | table.set(row1++, BACKOFF, settings.backoffDelay / 1000); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2326 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2327 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2328 | |
Makoto Onuki | 010291d | 2017-06-06 16:32:47 -0700 | [diff] [blame] | 2329 | row1 = row; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2330 | if (status.lastSuccessTime != 0) { |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2331 | table.set(row1++, LAST_SYNC, SyncStorageEngine.SOURCES[status.lastSuccessSource] |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2332 | + " " + "SUCCESS"); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2333 | table.set(row1++, LAST_SYNC, formatTime(status.lastSuccessTime)); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2334 | } |
| 2335 | if (status.lastFailureTime != 0) { |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2336 | table.set(row1++, LAST_SYNC, SyncStorageEngine.SOURCES[status.lastFailureSource] |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2337 | + " " + "FAILURE"); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2338 | table.set(row1++, LAST_SYNC, formatTime(status.lastFailureTime)); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2339 | //noinspection UnusedAssignment |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2340 | table.set(row1++, LAST_SYNC, status.lastFailureMesg); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2343 | table.writeTo(pw); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2344 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2345 | |
| 2346 | dumpSyncHistory(pw); |
| 2347 | |
| 2348 | pw.println(); |
| 2349 | pw.println("Per Adapter History"); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2350 | pw.println("(SERVER is now split up to FEED and OTHER)"); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2351 | |
| 2352 | for (int i = 0; i < statuses.size(); i++) { |
| 2353 | final Pair<EndPoint, SyncStatusInfo> event = statuses.get(i); |
| 2354 | |
| 2355 | pw.print(" "); |
| 2356 | pw.print(event.first.account.name); |
| 2357 | pw.print('/'); |
| 2358 | pw.print(event.first.account.type); |
| 2359 | pw.print(" u"); |
| 2360 | pw.print(event.first.userId); |
| 2361 | pw.print(" ["); |
| 2362 | pw.print(event.first.provider); |
| 2363 | pw.print("]"); |
| 2364 | pw.println(); |
| 2365 | |
Makoto Onuki | f74cf94 | 2018-04-16 17:04:58 -0700 | [diff] [blame] | 2366 | pw.println(" Per source last syncs:"); |
| 2367 | for (int j = 0; j < SyncStorageEngine.SOURCES.length; j++) { |
| 2368 | pw.print(" "); |
| 2369 | pw.print(String.format("%8s", SyncStorageEngine.SOURCES[j])); |
| 2370 | pw.print(" Success: "); |
| 2371 | pw.print(formatTime(event.second.perSourceLastSuccessTimes[j])); |
| 2372 | |
| 2373 | pw.print(" Failure: "); |
| 2374 | pw.println(formatTime(event.second.perSourceLastFailureTimes[j])); |
| 2375 | } |
| 2376 | |
| 2377 | pw.println(" Last syncs:"); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2378 | for (int j = 0; j < event.second.getEventCount(); j++) { |
Makoto Onuki | f74cf94 | 2018-04-16 17:04:58 -0700 | [diff] [blame] | 2379 | pw.print(" "); |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2380 | pw.print(formatTime(event.second.getEventTime(j))); |
| 2381 | pw.print(' '); |
| 2382 | pw.print(event.second.getEvent(j)); |
| 2383 | pw.println(); |
| 2384 | } |
Makoto Onuki | f74cf94 | 2018-04-16 17:04:58 -0700 | [diff] [blame] | 2385 | if (event.second.getEventCount() == 0) { |
| 2386 | pw.println(" N/A"); |
| 2387 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 2388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2391 | private String zeroToEmpty(int value) { |
| 2392 | return (value != 0) ? Integer.toString(value) : ""; |
| 2393 | } |
| 2394 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2395 | private void dumpTimeSec(PrintWriter pw, long time) { |
| 2396 | pw.print(time/1000); pw.print('.'); pw.print((time/100)%10); |
| 2397 | pw.print('s'); |
| 2398 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2399 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2400 | private void dumpDayStatistic(PrintWriter pw, SyncStorageEngine.DayStats ds) { |
| 2401 | pw.print("Success ("); pw.print(ds.successCount); |
| 2402 | if (ds.successCount > 0) { |
| 2403 | pw.print(" for "); dumpTimeSec(pw, ds.successTime); |
| 2404 | pw.print(" avg="); dumpTimeSec(pw, ds.successTime/ds.successCount); |
| 2405 | } |
| 2406 | pw.print(") Failure ("); pw.print(ds.failureCount); |
| 2407 | if (ds.failureCount > 0) { |
| 2408 | pw.print(" for "); dumpTimeSec(pw, ds.failureTime); |
| 2409 | pw.print(" avg="); dumpTimeSec(pw, ds.failureTime/ds.failureCount); |
| 2410 | } |
| 2411 | pw.println(")"); |
| 2412 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2413 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2414 | protected void dumpSyncHistory(PrintWriter pw) { |
| 2415 | dumpRecentHistory(pw); |
| 2416 | dumpDayStatistics(pw); |
| 2417 | } |
| 2418 | |
| 2419 | private void dumpRecentHistory(PrintWriter pw) { |
| 2420 | final ArrayList<SyncStorageEngine.SyncHistoryItem> items |
| 2421 | = mSyncStorageEngine.getSyncHistory(); |
| 2422 | if (items != null && items.size() > 0) { |
| 2423 | final Map<String, AuthoritySyncStats> authorityMap = Maps.newHashMap(); |
| 2424 | long totalElapsedTime = 0; |
| 2425 | long totalTimes = 0; |
| 2426 | final int N = items.size(); |
| 2427 | |
| 2428 | int maxAuthority = 0; |
| 2429 | int maxAccount = 0; |
| 2430 | for (SyncStorageEngine.SyncHistoryItem item : items) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2431 | SyncStorageEngine.AuthorityInfo authorityInfo |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2432 | = mSyncStorageEngine.getAuthority(item.authorityId); |
| 2433 | final String authorityName; |
| 2434 | final String accountKey; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2435 | if (authorityInfo != null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2436 | authorityName = authorityInfo.target.provider; |
| 2437 | accountKey = authorityInfo.target.account.name + "/" |
| 2438 | + authorityInfo.target.account.type |
| 2439 | + " u" + authorityInfo.target.userId; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2440 | } else { |
| 2441 | authorityName = "Unknown"; |
| 2442 | accountKey = "Unknown"; |
| 2443 | } |
| 2444 | |
| 2445 | int length = authorityName.length(); |
| 2446 | if (length > maxAuthority) { |
| 2447 | maxAuthority = length; |
| 2448 | } |
| 2449 | length = accountKey.length(); |
| 2450 | if (length > maxAccount) { |
| 2451 | maxAccount = length; |
| 2452 | } |
| 2453 | |
| 2454 | final long elapsedTime = item.elapsedTime; |
| 2455 | totalElapsedTime += elapsedTime; |
| 2456 | totalTimes++; |
| 2457 | AuthoritySyncStats authoritySyncStats = authorityMap.get(authorityName); |
| 2458 | if (authoritySyncStats == null) { |
| 2459 | authoritySyncStats = new AuthoritySyncStats(authorityName); |
| 2460 | authorityMap.put(authorityName, authoritySyncStats); |
| 2461 | } |
| 2462 | authoritySyncStats.elapsedTime += elapsedTime; |
| 2463 | authoritySyncStats.times++; |
| 2464 | final Map<String, AccountSyncStats> accountMap = authoritySyncStats.accountMap; |
| 2465 | AccountSyncStats accountSyncStats = accountMap.get(accountKey); |
| 2466 | if (accountSyncStats == null) { |
| 2467 | accountSyncStats = new AccountSyncStats(accountKey); |
| 2468 | accountMap.put(accountKey, accountSyncStats); |
| 2469 | } |
| 2470 | accountSyncStats.elapsedTime += elapsedTime; |
| 2471 | accountSyncStats.times++; |
| 2472 | |
| 2473 | } |
| 2474 | |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 2475 | if (totalElapsedTime > 0) { |
| 2476 | pw.println(); |
| 2477 | pw.printf("Detailed Statistics (Recent history): " |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2478 | + "%d (# of times) %ds (sync time)\n", |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 2479 | totalTimes, totalElapsedTime / 1000); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2480 | |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 2481 | final List<AuthoritySyncStats> sortedAuthorities = |
| 2482 | new ArrayList<AuthoritySyncStats>(authorityMap.values()); |
| 2483 | Collections.sort(sortedAuthorities, new Comparator<AuthoritySyncStats>() { |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2484 | @Override |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 2485 | public int compare(AuthoritySyncStats lhs, AuthoritySyncStats rhs) { |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2486 | // reverse order |
| 2487 | int compare = Integer.compare(rhs.times, lhs.times); |
| 2488 | if (compare == 0) { |
| 2489 | compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2490 | } |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2491 | return compare; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2492 | } |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2493 | }); |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 2494 | |
| 2495 | final int maxLength = Math.max(maxAuthority, maxAccount + 3); |
| 2496 | final int padLength = 2 + 2 + maxLength + 2 + 10 + 11; |
| 2497 | final char chars[] = new char[padLength]; |
| 2498 | Arrays.fill(chars, '-'); |
| 2499 | final String separator = new String(chars); |
| 2500 | |
| 2501 | final String authorityFormat = |
| 2502 | String.format(" %%-%ds: %%-9s %%-11s\n", maxLength + 2); |
| 2503 | final String accountFormat = |
| 2504 | String.format(" %%-%ds: %%-9s %%-11s\n", maxLength); |
| 2505 | |
| 2506 | pw.println(separator); |
| 2507 | for (AuthoritySyncStats authoritySyncStats : sortedAuthorities) { |
| 2508 | String name = authoritySyncStats.name; |
| 2509 | long elapsedTime; |
| 2510 | int times; |
| 2511 | String timeStr; |
| 2512 | String timesStr; |
| 2513 | |
| 2514 | elapsedTime = authoritySyncStats.elapsedTime; |
| 2515 | times = authoritySyncStats.times; |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2516 | timeStr = String.format("%ds/%d%%", |
| 2517 | elapsedTime / 1000, |
| 2518 | elapsedTime * 100 / totalElapsedTime); |
| 2519 | timesStr = String.format("%d/%d%%", |
| 2520 | times, |
| 2521 | times * 100 / totalTimes); |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 2522 | pw.printf(authorityFormat, name, timesStr, timeStr); |
| 2523 | |
| 2524 | final List<AccountSyncStats> sortedAccounts = |
| 2525 | new ArrayList<AccountSyncStats>( |
| 2526 | authoritySyncStats.accountMap.values()); |
| 2527 | Collections.sort(sortedAccounts, new Comparator<AccountSyncStats>() { |
| 2528 | @Override |
| 2529 | public int compare(AccountSyncStats lhs, AccountSyncStats rhs) { |
| 2530 | // reverse order |
| 2531 | int compare = Integer.compare(rhs.times, lhs.times); |
| 2532 | if (compare == 0) { |
| 2533 | compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime); |
| 2534 | } |
| 2535 | return compare; |
| 2536 | } |
| 2537 | }); |
| 2538 | for (AccountSyncStats stats: sortedAccounts) { |
| 2539 | elapsedTime = stats.elapsedTime; |
| 2540 | times = stats.times; |
| 2541 | timeStr = String.format("%ds/%d%%", |
| 2542 | elapsedTime / 1000, |
| 2543 | elapsedTime * 100 / totalElapsedTime); |
| 2544 | timesStr = String.format("%d/%d%%", |
| 2545 | times, |
| 2546 | times * 100 / totalTimes); |
| 2547 | pw.printf(accountFormat, stats.name, timesStr, timeStr); |
| 2548 | } |
| 2549 | pw.println(separator); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2550 | } |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | pw.println(); |
| 2554 | pw.println("Recent Sync History"); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2555 | pw.println("(SERVER is now split up to FEED and OTHER)"); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2556 | final String format = " %-" + maxAccount + "s %-" + maxAuthority + "s %s\n"; |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2557 | final Map<String, Long> lastTimeMap = Maps.newHashMap(); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2558 | final PackageManager pm = mContext.getPackageManager(); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2559 | for (int i = 0; i < N; i++) { |
| 2560 | SyncStorageEngine.SyncHistoryItem item = items.get(i); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2561 | SyncStorageEngine.AuthorityInfo authorityInfo |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2562 | = mSyncStorageEngine.getAuthority(item.authorityId); |
| 2563 | final String authorityName; |
| 2564 | final String accountKey; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2565 | if (authorityInfo != null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2566 | authorityName = authorityInfo.target.provider; |
| 2567 | accountKey = authorityInfo.target.account.name + "/" |
| 2568 | + authorityInfo.target.account.type |
| 2569 | + " u" + authorityInfo.target.userId; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2570 | } else { |
| 2571 | authorityName = "Unknown"; |
| 2572 | accountKey = "Unknown"; |
| 2573 | } |
| 2574 | final long elapsedTime = item.elapsedTime; |
| 2575 | final Time time = new Time(); |
| 2576 | final long eventTime = item.eventTime; |
| 2577 | time.set(eventTime); |
| 2578 | |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2579 | final String key = authorityName + "/" + accountKey; |
| 2580 | final Long lastEventTime = lastTimeMap.get(key); |
| 2581 | final String diffString; |
| 2582 | if (lastEventTime == null) { |
| 2583 | diffString = ""; |
| 2584 | } else { |
| 2585 | final long diff = (lastEventTime - eventTime) / 1000; |
| 2586 | if (diff < 60) { |
| 2587 | diffString = String.valueOf(diff); |
| 2588 | } else if (diff < 3600) { |
| 2589 | diffString = String.format("%02d:%02d", diff / 60, diff % 60); |
| 2590 | } else { |
| 2591 | final long sec = diff % 3600; |
| 2592 | diffString = String.format("%02d:%02d:%02d", |
| 2593 | diff / 3600, sec / 60, sec % 60); |
| 2594 | } |
| 2595 | } |
| 2596 | lastTimeMap.put(key, eventTime); |
| 2597 | |
| 2598 | pw.printf(" #%-3d: %s %8s %5.1fs %8s", |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2599 | i + 1, |
| 2600 | formatTime(eventTime), |
| 2601 | SyncStorageEngine.SOURCES[item.source], |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 2602 | ((float) elapsedTime) / 1000, |
| 2603 | diffString); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2604 | pw.printf(format, accountKey, authorityName, |
| 2605 | SyncOperation.reasonToString(pm, item.reason)); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2606 | |
| 2607 | if (item.event != SyncStorageEngine.EVENT_STOP |
| 2608 | || item.upstreamActivity != 0 |
| 2609 | || item.downstreamActivity != 0) { |
| 2610 | pw.printf(" event=%d upstreamActivity=%d downstreamActivity=%d\n", |
| 2611 | item.event, |
| 2612 | item.upstreamActivity, |
| 2613 | item.downstreamActivity); |
| 2614 | } |
| 2615 | if (item.mesg != null |
| 2616 | && !SyncStorageEngine.MESG_SUCCESS.equals(item.mesg)) { |
| 2617 | pw.printf(" mesg=%s\n", item.mesg); |
| 2618 | } |
| 2619 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2620 | pw.println(); |
| 2621 | pw.println("Recent Sync History Extras"); |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 2622 | pw.println("(SERVER is now split up to FEED and OTHER)"); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2623 | for (int i = 0; i < N; i++) { |
| 2624 | final SyncStorageEngine.SyncHistoryItem item = items.get(i); |
| 2625 | final Bundle extras = item.extras; |
| 2626 | if (extras == null || extras.size() == 0) { |
| 2627 | continue; |
| 2628 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2629 | final SyncStorageEngine.AuthorityInfo authorityInfo |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2630 | = mSyncStorageEngine.getAuthority(item.authorityId); |
| 2631 | final String authorityName; |
| 2632 | final String accountKey; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2633 | if (authorityInfo != null) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2634 | authorityName = authorityInfo.target.provider; |
| 2635 | accountKey = authorityInfo.target.account.name + "/" |
| 2636 | + authorityInfo.target.account.type |
| 2637 | + " u" + authorityInfo.target.userId; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2638 | } else { |
| 2639 | authorityName = "Unknown"; |
| 2640 | accountKey = "Unknown"; |
| 2641 | } |
| 2642 | final Time time = new Time(); |
| 2643 | final long eventTime = item.eventTime; |
| 2644 | time.set(eventTime); |
| 2645 | |
| 2646 | pw.printf(" #%-3d: %s %8s ", |
| 2647 | i + 1, |
| 2648 | formatTime(eventTime), |
| 2649 | SyncStorageEngine.SOURCES[item.source]); |
| 2650 | |
| 2651 | pw.printf(format, accountKey, authorityName, extras); |
| 2652 | } |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2653 | } |
| 2654 | } |
| 2655 | |
| 2656 | private void dumpDayStatistics(PrintWriter pw) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2657 | SyncStorageEngine.DayStats dses[] = mSyncStorageEngine.getDayStatistics(); |
| 2658 | if (dses != null && dses[0] != null) { |
| 2659 | pw.println(); |
| 2660 | pw.println("Sync Statistics"); |
| 2661 | pw.print(" Today: "); dumpDayStatistic(pw, dses[0]); |
| 2662 | int today = dses[0].day; |
| 2663 | int i; |
| 2664 | SyncStorageEngine.DayStats ds; |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2665 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2666 | // Print each day in the current week. |
| 2667 | for (i=1; i<=6 && i < dses.length; i++) { |
| 2668 | ds = dses[i]; |
| 2669 | if (ds == null) break; |
| 2670 | int delta = today-ds.day; |
| 2671 | if (delta > 6) break; |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2672 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2673 | pw.print(" Day-"); pw.print(delta); pw.print(": "); |
| 2674 | dumpDayStatistic(pw, ds); |
| 2675 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2676 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2677 | // Aggregate all following days into weeks and print totals. |
| 2678 | int weekDay = today; |
| 2679 | while (i < dses.length) { |
| 2680 | SyncStorageEngine.DayStats aggr = null; |
| 2681 | weekDay -= 7; |
| 2682 | while (i < dses.length) { |
| 2683 | ds = dses[i]; |
| 2684 | if (ds == null) { |
| 2685 | i = dses.length; |
| 2686 | break; |
| 2687 | } |
| 2688 | int delta = weekDay-ds.day; |
| 2689 | if (delta > 6) break; |
| 2690 | i++; |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2691 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2692 | if (aggr == null) { |
| 2693 | aggr = new SyncStorageEngine.DayStats(weekDay); |
| 2694 | } |
| 2695 | aggr.successCount += ds.successCount; |
| 2696 | aggr.successTime += ds.successTime; |
| 2697 | aggr.failureCount += ds.failureCount; |
| 2698 | aggr.failureTime += ds.failureTime; |
| 2699 | } |
| 2700 | if (aggr != null) { |
| 2701 | pw.print(" Week-"); pw.print((today-weekDay)/7); pw.print(": "); |
| 2702 | dumpDayStatistic(pw, aggr); |
| 2703 | } |
| 2704 | } |
| 2705 | } |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2706 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2707 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2708 | private void dumpSyncAdapters(IndentingPrintWriter pw) { |
| 2709 | pw.println(); |
| 2710 | final List<UserInfo> users = getAllUsers(); |
| 2711 | if (users != null) { |
| 2712 | for (UserInfo user : users) { |
| 2713 | pw.println("Sync adapters for " + user + ":"); |
| 2714 | pw.increaseIndent(); |
| 2715 | for (RegisteredServicesCache.ServiceInfo<?> info : |
| 2716 | mSyncAdapters.getAllServices(user.id)) { |
| 2717 | pw.println(info); |
| 2718 | } |
| 2719 | pw.decreaseIndent(); |
| 2720 | pw.println(); |
| 2721 | } |
| 2722 | } |
| 2723 | } |
| 2724 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 2725 | private static class AuthoritySyncStats { |
| 2726 | String name; |
| 2727 | long elapsedTime; |
| 2728 | int times; |
| 2729 | Map<String, AccountSyncStats> accountMap = Maps.newHashMap(); |
| 2730 | |
| 2731 | private AuthoritySyncStats(String name) { |
| 2732 | this.name = name; |
| 2733 | } |
| 2734 | } |
| 2735 | |
| 2736 | private static class AccountSyncStats { |
| 2737 | String name; |
| 2738 | long elapsedTime; |
| 2739 | int times; |
| 2740 | |
| 2741 | private AccountSyncStats(String name) { |
| 2742 | this.name = name; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2743 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 2746 | interface OnReadyCallback { |
| 2747 | void onReady(); |
| 2748 | } |
| 2749 | |
| 2750 | static void sendOnUnsyncableAccount(@NonNull Context context, |
| 2751 | @NonNull RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo, |
| 2752 | @UserIdInt int userId, @NonNull OnReadyCallback onReadyCallback) { |
| 2753 | OnUnsyncableAccountCheck connection = new OnUnsyncableAccountCheck(syncAdapterInfo, |
| 2754 | onReadyCallback); |
| 2755 | |
| 2756 | boolean isBound = context.bindServiceAsUser( |
| 2757 | getAdapterBindIntent(context, syncAdapterInfo.componentName, userId), |
| 2758 | connection, SYNC_ADAPTER_CONNECTION_FLAGS, UserHandle.of(userId)); |
| 2759 | |
| 2760 | if (isBound) { |
| 2761 | // Unbind after SERVICE_BOUND_TIME_MILLIS to not leak the connection. |
| 2762 | (new Handler(Looper.getMainLooper())).postDelayed( |
| 2763 | () -> context.unbindService(connection), |
| 2764 | OnUnsyncableAccountCheck.SERVICE_BOUND_TIME_MILLIS); |
| 2765 | } else { |
| 2766 | /* |
| 2767 | * The default implementation of adapter.onUnsyncableAccount returns true. Hence if |
| 2768 | * there the service cannot be bound, assume the default behavior. |
| 2769 | */ |
| 2770 | connection.onReady(); |
| 2771 | } |
| 2772 | } |
| 2773 | |
| 2774 | |
| 2775 | /** |
| 2776 | * Helper class for calling ISyncAdapter.onUnsyncableAccountDone. |
| 2777 | * |
| 2778 | * If this returns {@code true} the onReadyCallback is called. Otherwise nothing happens. |
| 2779 | */ |
| 2780 | private static class OnUnsyncableAccountCheck implements ServiceConnection { |
| 2781 | static final long SERVICE_BOUND_TIME_MILLIS = 5000; |
| 2782 | |
| 2783 | private final @NonNull OnReadyCallback mOnReadyCallback; |
| 2784 | private final @NonNull RegisteredServicesCache.ServiceInfo<SyncAdapterType> |
| 2785 | mSyncAdapterInfo; |
| 2786 | |
| 2787 | OnUnsyncableAccountCheck( |
| 2788 | @NonNull RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo, |
| 2789 | @NonNull OnReadyCallback onReadyCallback) { |
| 2790 | mSyncAdapterInfo = syncAdapterInfo; |
| 2791 | mOnReadyCallback = onReadyCallback; |
| 2792 | } |
| 2793 | |
| 2794 | private void onReady() { |
| 2795 | long identity = Binder.clearCallingIdentity(); |
| 2796 | try { |
| 2797 | mOnReadyCallback.onReady(); |
| 2798 | } finally { |
| 2799 | Binder.restoreCallingIdentity(identity); |
| 2800 | } |
| 2801 | } |
| 2802 | |
| 2803 | @Override |
| 2804 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 2805 | final ISyncAdapter adapter = ISyncAdapter.Stub.asInterface(service); |
| 2806 | |
| 2807 | try { |
| 2808 | adapter.onUnsyncableAccount(new ISyncAdapterUnsyncableAccountCallback.Stub() { |
| 2809 | @Override |
| 2810 | public void onUnsyncableAccountDone(boolean isReady) { |
| 2811 | if (isReady) { |
| 2812 | onReady(); |
| 2813 | } |
| 2814 | } |
| 2815 | }); |
| 2816 | } catch (RemoteException e) { |
| 2817 | Slog.e(TAG, "Could not call onUnsyncableAccountDone " + mSyncAdapterInfo, e); |
| 2818 | /* |
| 2819 | * The default implementation of adapter.onUnsyncableAccount returns true. Hence if |
| 2820 | * there is a crash in the implementation, assume the default behavior. |
| 2821 | */ |
| 2822 | onReady(); |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | @Override |
| 2827 | public void onServiceDisconnected(ComponentName name) { |
| 2828 | // Wait until the service connects again |
| 2829 | } |
| 2830 | } |
| 2831 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2832 | /** |
| 2833 | * A helper object to keep track of the time we have spent syncing since the last boot |
| 2834 | */ |
| 2835 | private class SyncTimeTracker { |
| 2836 | /** True if a sync was in progress on the most recent call to update() */ |
| 2837 | boolean mLastWasSyncing = false; |
| 2838 | /** Used to track when lastWasSyncing was last set */ |
| 2839 | long mWhenSyncStarted = 0; |
| 2840 | /** The cumulative time we have spent syncing */ |
| 2841 | private long mTimeSpentSyncing; |
| 2842 | |
| 2843 | /** Call to let the tracker know that the sync state may have changed */ |
| 2844 | public synchronized void update() { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2845 | final boolean isSyncInProgress = !mActiveSyncContexts.isEmpty(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2846 | if (isSyncInProgress == mLastWasSyncing) return; |
| 2847 | final long now = SystemClock.elapsedRealtime(); |
| 2848 | if (isSyncInProgress) { |
| 2849 | mWhenSyncStarted = now; |
| 2850 | } else { |
| 2851 | mTimeSpentSyncing += now - mWhenSyncStarted; |
| 2852 | } |
| 2853 | mLastWasSyncing = isSyncInProgress; |
| 2854 | } |
| 2855 | |
| 2856 | /** Get how long we have been syncing, in ms */ |
| 2857 | public synchronized long timeSpentSyncing() { |
| 2858 | if (!mLastWasSyncing) return mTimeSpentSyncing; |
| 2859 | |
| 2860 | final long now = SystemClock.elapsedRealtime(); |
| 2861 | return mTimeSpentSyncing + (now - mWhenSyncStarted); |
| 2862 | } |
| 2863 | } |
| 2864 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2865 | class ServiceConnectionData { |
| 2866 | public final ActiveSyncContext activeSyncContext; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2867 | public final IBinder adapter; |
| 2868 | |
| 2869 | ServiceConnectionData(ActiveSyncContext activeSyncContext, IBinder adapter) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2870 | this.activeSyncContext = activeSyncContext; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2871 | this.adapter = adapter; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2872 | } |
| 2873 | } |
| 2874 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2875 | /** |
Makoto Onuki | 056a975 | 2018-05-08 15:21:56 -0700 | [diff] [blame] | 2876 | * @return whether the device is ready to run sync jobs. |
| 2877 | */ |
| 2878 | public static boolean readyToSync() { |
| 2879 | synchronized (SyncManager.class) { |
| 2880 | return sInstance != null && sInstance.mProvisioned && sInstance.mBootCompleted |
| 2881 | && sInstance.mJobServiceReady; |
| 2882 | } |
| 2883 | } |
| 2884 | |
| 2885 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2886 | * Handles SyncOperation Messages that are posted to the associated |
| 2887 | * HandlerThread. |
| 2888 | */ |
| 2889 | class SyncHandler extends Handler { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2890 | // Messages that can be sent on mHandler. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2891 | private static final int MESSAGE_SYNC_FINISHED = 1; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2892 | private static final int MESSAGE_RELEASE_MESSAGES_FROM_QUEUE = 2; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2893 | private static final int MESSAGE_SERVICE_CONNECTED = 4; |
| 2894 | private static final int MESSAGE_SERVICE_DISCONNECTED = 5; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2895 | private static final int MESSAGE_CANCEL = 6; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2896 | static final int MESSAGE_JOBSERVICE_OBJECT = 7; |
| 2897 | static final int MESSAGE_START_SYNC = 10; |
| 2898 | static final int MESSAGE_STOP_SYNC = 11; |
| 2899 | static final int MESSAGE_SCHEDULE_SYNC = 12; |
| 2900 | static final int MESSAGE_UPDATE_PERIODIC_SYNC = 13; |
| 2901 | static final int MESSAGE_REMOVE_PERIODIC_SYNC = 14; |
Shreyas Basarge | a4ac5ab | 2016-04-21 20:31:44 +0100 | [diff] [blame] | 2902 | |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 2903 | /** |
| 2904 | * Posted periodically to monitor network process for long-running syncs. |
| 2905 | * obj: {@link com.android.server.content.SyncManager.ActiveSyncContext} |
| 2906 | */ |
| 2907 | private static final int MESSAGE_MONITOR_SYNC = 8; |
振淦王 | 60a7431 | 2015-12-01 16:37:31 +0800 | [diff] [blame] | 2908 | private static final int MESSAGE_ACCOUNTS_UPDATED = 9; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2909 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2910 | public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 2911 | private final HashMap<String, PowerManager.WakeLock> mWakeLocks = Maps.newHashMap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2912 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 2913 | private List<Message> mUnreadyQueue = new ArrayList<Message>(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2914 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 2915 | void onBootCompleted() { |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 2916 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2917 | Slog.v(TAG, "Boot completed."); |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 2918 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2919 | checkIfDeviceReady(); |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | void onDeviceProvisioned() { |
| 2923 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 2924 | Log.d(TAG, "mProvisioned=" + mProvisioned); |
| 2925 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2926 | checkIfDeviceReady(); |
| 2927 | } |
| 2928 | |
| 2929 | void checkIfDeviceReady() { |
Shreyas Basarge | a4ac5ab | 2016-04-21 20:31:44 +0100 | [diff] [blame] | 2930 | if (mProvisioned && mBootCompleted && mJobServiceReady) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2931 | synchronized(this) { |
Shreyas Basarge | 3147bbc | 2016-02-19 23:51:40 +0000 | [diff] [blame] | 2932 | mSyncStorageEngine.restoreAllPeriodicSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2933 | // Dispatch any stashed messages. |
| 2934 | obtainMessage(MESSAGE_RELEASE_MESSAGES_FROM_QUEUE).sendToTarget(); |
| 2935 | } |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 2936 | } |
| 2937 | } |
| 2938 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2939 | /** |
| 2940 | * Stash any messages that come to the handler before boot is complete or before the device |
| 2941 | * is properly provisioned (i.e. out of set-up wizard). |
| 2942 | * {@link #onBootCompleted()} and {@link SyncHandler#onDeviceProvisioned} both |
| 2943 | * need to come in before we start syncing. |
| 2944 | * @param msg Message to dispatch at a later point. |
| 2945 | * @return true if a message was enqueued, false otherwise. This is to avoid losing the |
| 2946 | * message if we manage to acquire the lock but by the time we do boot has completed. |
| 2947 | */ |
| 2948 | private boolean tryEnqueueMessageUntilReadyToRun(Message msg) { |
| 2949 | synchronized (this) { |
Shreyas Basarge | a4ac5ab | 2016-04-21 20:31:44 +0100 | [diff] [blame] | 2950 | if (!mBootCompleted || !mProvisioned || !mJobServiceReady) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2951 | // Need to copy the message bc looper will recycle it. |
| 2952 | Message m = Message.obtain(msg); |
| 2953 | mUnreadyQueue.add(m); |
| 2954 | return true; |
| 2955 | } else { |
| 2956 | return false; |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 2957 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | |
| 2961 | public SyncHandler(Looper looper) { |
| 2962 | super(looper); |
| 2963 | } |
| 2964 | |
| 2965 | public void handleMessage(Message msg) { |
| 2966 | try { |
| 2967 | mSyncManagerWakeLock.acquire(); |
| 2968 | // We only want to enqueue sync related messages until device is ready. |
| 2969 | // Other messages are handled without enqueuing. |
| 2970 | if (msg.what == MESSAGE_JOBSERVICE_OBJECT) { |
| 2971 | Slog.i(TAG, "Got SyncJobService instance."); |
| 2972 | mSyncJobService = (SyncJobService) msg.obj; |
Shreyas Basarge | a4ac5ab | 2016-04-21 20:31:44 +0100 | [diff] [blame] | 2973 | mJobServiceReady = true; |
| 2974 | checkIfDeviceReady(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 2975 | } else if (msg.what == SyncHandler.MESSAGE_ACCOUNTS_UPDATED) { |
| 2976 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2977 | Slog.v(TAG, "handleSyncHandlerMessage: MESSAGE_ACCOUNTS_UPDATED"); |
| 2978 | } |
| 2979 | EndPoint targets = (EndPoint) msg.obj; |
| 2980 | updateRunningAccountsH(targets); |
| 2981 | } else if (msg.what == MESSAGE_RELEASE_MESSAGES_FROM_QUEUE) { |
| 2982 | if (mUnreadyQueue != null) { |
| 2983 | for (Message m : mUnreadyQueue) { |
| 2984 | handleSyncMessage(m); |
| 2985 | } |
| 2986 | mUnreadyQueue = null; |
| 2987 | } |
| 2988 | } else if (tryEnqueueMessageUntilReadyToRun(msg)) { |
| 2989 | // No work to be done. |
| 2990 | } else { |
| 2991 | handleSyncMessage(msg); |
| 2992 | } |
| 2993 | } finally { |
| 2994 | mSyncManagerWakeLock.release(); |
| 2995 | } |
| 2996 | } |
| 2997 | |
| 2998 | private void handleSyncMessage(Message msg) { |
| 2999 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
| 3000 | |
| 3001 | try { |
| 3002 | mDataConnectionIsConnected = readDataConnectionState(); |
| 3003 | switch (msg.what) { |
| 3004 | case MESSAGE_SCHEDULE_SYNC: |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 3005 | ScheduleSyncMessagePayload syncPayload = |
| 3006 | (ScheduleSyncMessagePayload) msg.obj; |
| 3007 | SyncOperation op = syncPayload.syncOperation; |
| 3008 | scheduleSyncOperationH(op, syncPayload.minDelayMillis); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3009 | break; |
| 3010 | |
| 3011 | case MESSAGE_START_SYNC: |
| 3012 | op = (SyncOperation) msg.obj; |
| 3013 | startSyncH(op); |
| 3014 | break; |
| 3015 | |
| 3016 | case MESSAGE_STOP_SYNC: |
| 3017 | op = (SyncOperation) msg.obj; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3018 | if (isLoggable) { |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3019 | Slog.v(TAG, "Stop sync received."); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3020 | } |
| 3021 | ActiveSyncContext asc = findActiveSyncContextH(op.jobId); |
| 3022 | if (asc != null) { |
| 3023 | runSyncFinishedOrCanceledH(null /* no result */, asc); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3024 | boolean reschedule = msg.arg1 != 0; |
| 3025 | boolean applyBackoff = msg.arg2 != 0; |
| 3026 | if (isLoggable) { |
| 3027 | Slog.v(TAG, "Stopping sync. Reschedule: " + reschedule |
| 3028 | + "Backoff: " + applyBackoff); |
| 3029 | } |
| 3030 | if (applyBackoff) { |
| 3031 | increaseBackoffSetting(op.target); |
| 3032 | } |
| 3033 | if (reschedule) { |
| 3034 | deferStoppedSyncH(op, 0); |
| 3035 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3036 | } |
| 3037 | break; |
| 3038 | |
| 3039 | case MESSAGE_UPDATE_PERIODIC_SYNC: |
| 3040 | UpdatePeriodicSyncMessagePayload data = |
| 3041 | (UpdatePeriodicSyncMessagePayload) msg.obj; |
| 3042 | updateOrAddPeriodicSyncH(data.target, data.pollFrequency, |
| 3043 | data.flex, data.extras); |
| 3044 | break; |
| 3045 | case MESSAGE_REMOVE_PERIODIC_SYNC: |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3046 | Pair<EndPoint, String> args = (Pair<EndPoint, String>) (msg.obj); |
| 3047 | removePeriodicSyncH(args.first, msg.getData(), args.second); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3048 | break; |
| 3049 | |
| 3050 | case SyncHandler.MESSAGE_CANCEL: |
| 3051 | SyncStorageEngine.EndPoint endpoint = (SyncStorageEngine.EndPoint) msg.obj; |
| 3052 | Bundle extras = msg.peekData(); |
| 3053 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3054 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_CANCEL: " |
| 3055 | + endpoint + " bundle: " + extras); |
| 3056 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3057 | cancelActiveSyncH(endpoint, extras, "MESSAGE_CANCEL"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3058 | break; |
| 3059 | |
| 3060 | case SyncHandler.MESSAGE_SYNC_FINISHED: |
| 3061 | SyncFinishedOrCancelledMessagePayload payload = |
| 3062 | (SyncFinishedOrCancelledMessagePayload) msg.obj; |
| 3063 | if (!isSyncStillActiveH(payload.activeSyncContext)) { |
| 3064 | Log.d(TAG, "handleSyncHandlerMessage: dropping since the " |
| 3065 | + "sync is no longer active: " |
| 3066 | + payload.activeSyncContext); |
| 3067 | break; |
| 3068 | } |
| 3069 | if (isLoggable) { |
| 3070 | Slog.v(TAG, "syncFinished" + payload.activeSyncContext.mSyncOperation); |
| 3071 | } |
| 3072 | mSyncJobService.callJobFinished( |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3073 | payload.activeSyncContext.mSyncOperation.jobId, false, |
| 3074 | "sync finished"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3075 | runSyncFinishedOrCanceledH(payload.syncResult, |
| 3076 | payload.activeSyncContext); |
| 3077 | break; |
| 3078 | |
| 3079 | case SyncHandler.MESSAGE_SERVICE_CONNECTED: { |
| 3080 | ServiceConnectionData msgData = (ServiceConnectionData) msg.obj; |
| 3081 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3082 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_CONNECTED: " |
| 3083 | + msgData.activeSyncContext); |
| 3084 | } |
| 3085 | // Check that this isn't an old message. |
| 3086 | if (isSyncStillActiveH(msgData.activeSyncContext)) { |
| 3087 | runBoundToAdapterH( |
| 3088 | msgData.activeSyncContext, |
| 3089 | msgData.adapter); |
| 3090 | } |
| 3091 | break; |
| 3092 | } |
| 3093 | |
| 3094 | case SyncHandler.MESSAGE_SERVICE_DISCONNECTED: { |
| 3095 | final ActiveSyncContext currentSyncContext = |
| 3096 | ((ServiceConnectionData) msg.obj).activeSyncContext; |
| 3097 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3098 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_DISCONNECTED: " |
| 3099 | + currentSyncContext); |
| 3100 | } |
| 3101 | // Check that this isn't an old message. |
| 3102 | if (isSyncStillActiveH(currentSyncContext)) { |
| 3103 | // cancel the sync if we have a syncadapter, which means one is |
| 3104 | // outstanding |
| 3105 | try { |
| 3106 | if (currentSyncContext.mSyncAdapter != null) { |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3107 | mLogger.log("Calling cancelSync for SERVICE_DISCONNECTED ", |
| 3108 | currentSyncContext, |
| 3109 | " adapter=", currentSyncContext.mSyncAdapter); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3110 | currentSyncContext.mSyncAdapter.cancelSync(currentSyncContext); |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3111 | mLogger.log("Canceled"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3112 | } |
| 3113 | } catch (RemoteException e) { |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3114 | mLogger.log("RemoteException ", Log.getStackTraceString(e)); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3115 | // We don't need to retry this in this case. |
| 3116 | } |
| 3117 | |
| 3118 | // Pretend that the sync failed with an IOException, |
| 3119 | // which is a soft error. |
| 3120 | SyncResult syncResult = new SyncResult(); |
| 3121 | syncResult.stats.numIoExceptions++; |
| 3122 | mSyncJobService.callJobFinished( |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3123 | currentSyncContext.mSyncOperation.jobId, false, |
| 3124 | "service disconnected"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3125 | runSyncFinishedOrCanceledH(syncResult, currentSyncContext); |
| 3126 | } |
| 3127 | break; |
| 3128 | } |
| 3129 | |
| 3130 | case SyncHandler.MESSAGE_MONITOR_SYNC: |
| 3131 | ActiveSyncContext monitoredSyncContext = (ActiveSyncContext) msg.obj; |
| 3132 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3133 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_MONITOR_SYNC: " + |
| 3134 | monitoredSyncContext.mSyncOperation.target); |
| 3135 | } |
| 3136 | |
| 3137 | if (isSyncNotUsingNetworkH(monitoredSyncContext)) { |
| 3138 | Log.w(TAG, String.format( |
| 3139 | "Detected sync making no progress for %s. cancelling.", |
| 3140 | monitoredSyncContext)); |
| 3141 | mSyncJobService.callJobFinished( |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3142 | monitoredSyncContext.mSyncOperation.jobId, false, |
| 3143 | "no network activity"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3144 | runSyncFinishedOrCanceledH( |
| 3145 | null /* cancel => no result */, monitoredSyncContext); |
| 3146 | } else { |
| 3147 | // Repost message to check again. |
| 3148 | postMonitorSyncProgressMessage(monitoredSyncContext); |
| 3149 | } |
| 3150 | break; |
| 3151 | |
| 3152 | } |
| 3153 | } finally { |
| 3154 | mSyncTimeTracker.update(); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 3155 | } |
| 3156 | } |
| 3157 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3158 | private PowerManager.WakeLock getSyncWakeLock(SyncOperation operation) { |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 3159 | final String wakeLockKey = operation.wakeLockName(); |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 3160 | PowerManager.WakeLock wakeLock = mWakeLocks.get(wakeLockKey); |
| 3161 | if (wakeLock == null) { |
Dianne Hackborn | d45665b | 2014-02-26 12:35:32 -0800 | [diff] [blame] | 3162 | final String name = SYNC_WAKE_LOCK_PREFIX + wakeLockKey; |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 3163 | wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name); |
| 3164 | wakeLock.setReferenceCounted(false); |
| 3165 | mWakeLocks.put(wakeLockKey, wakeLock); |
| 3166 | } |
| 3167 | return wakeLock; |
| 3168 | } |
| 3169 | |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 3170 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3171 | * Defer the specified SyncOperation by rescheduling it on the JobScheduler with some |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3172 | * delay. This is equivalent to a failure. If this is a periodic sync, a delayed one-off |
| 3173 | * sync will be scheduled. |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 3174 | */ |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3175 | private void deferSyncH(SyncOperation op, long delay, String why) { |
| 3176 | mLogger.log("deferSyncH() ", (op.isPeriodic ? "periodic " : ""), |
| 3177 | "sync. op=", op, " delay=", delay, " why=", why); |
| 3178 | mSyncJobService.callJobFinished(op.jobId, false, why); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3179 | if (op.isPeriodic) { |
| 3180 | scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay); |
| 3181 | } else { |
Shreyas Basarge | bd4c3ea | 2016-06-16 11:54:35 +0100 | [diff] [blame] | 3182 | // mSyncJobService.callJobFinished is async, so cancel the job to ensure we don't |
| 3183 | // find the this job in the pending jobs list while looking for duplicates |
| 3184 | // before scheduling it at a later time. |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3185 | cancelJob(op, "deferSyncH()"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3186 | scheduleSyncOperationH(op, delay); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 3187 | } |
| 3188 | } |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 3189 | |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3190 | /* Same as deferSyncH, but assumes that job is no longer running on JobScheduler. */ |
| 3191 | private void deferStoppedSyncH(SyncOperation op, long delay) { |
| 3192 | if (op.isPeriodic) { |
| 3193 | scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay); |
| 3194 | } else { |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3195 | scheduleSyncOperationH(op, delay); |
| 3196 | } |
| 3197 | } |
| 3198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3199 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3200 | * Cancel an active sync and reschedule it on the JobScheduler with some delay. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3201 | */ |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3202 | private void deferActiveSyncH(ActiveSyncContext asc, String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3203 | SyncOperation op = asc.mSyncOperation; |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3204 | runSyncFinishedOrCanceledH(null, asc); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3205 | deferSyncH(op, SYNC_DELAY_ON_CONFLICT, why); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3206 | } |
| 3207 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3208 | private void startSyncH(SyncOperation op) { |
| 3209 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
| 3210 | if (isLoggable) Slog.v(TAG, op.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3211 | |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 3212 | // At this point, we know the device has been connected to the server, so |
| 3213 | // assume the clock is correct. |
| 3214 | mSyncStorageEngine.setClockValid(); |
| 3215 | |
Makoto Onuki | 5397be1 | 2017-12-20 16:22:34 +0900 | [diff] [blame] | 3216 | mSyncJobService.markSyncStarted(op.jobId); |
| 3217 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3218 | if (mStorageIsLow) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3219 | deferSyncH(op, SYNC_DELAY_ON_LOW_STORAGE, "storage low"); |
Matthew Williams | 8704fc3 | 2013-09-27 11:32:35 -0700 | [diff] [blame] | 3220 | return; |
| 3221 | } |
| 3222 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3223 | if (op.isPeriodic) { |
| 3224 | // Don't allow this periodic to run if a previous instance failed and is currently |
| 3225 | // scheduled according to some backoff criteria. |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 3226 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3227 | for (SyncOperation syncOperation: ops) { |
| 3228 | if (syncOperation.sourcePeriodicId == op.jobId) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3229 | mSyncJobService.callJobFinished(op.jobId, false, |
| 3230 | "periodic sync, pending"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3231 | return; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3232 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3233 | } |
| 3234 | // Don't allow this periodic to run if a previous instance failed and is currently |
| 3235 | // executing according to some backoff criteria. |
| 3236 | for (ActiveSyncContext asc: mActiveSyncContexts) { |
| 3237 | if (asc.mSyncOperation.sourcePeriodicId == op.jobId) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3238 | mSyncJobService.callJobFinished(op.jobId, false, |
| 3239 | "periodic sync, already running"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3240 | return; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3241 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3242 | } |
| 3243 | // Check for adapter delays. |
| 3244 | if (isAdapterDelayed(op.target)) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3245 | deferSyncH(op, 0 /* No minimum delay */, "backing off"); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3246 | return; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3247 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3248 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3249 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3250 | // Check for conflicting syncs. |
| 3251 | for (ActiveSyncContext asc: mActiveSyncContexts) { |
| 3252 | if (asc.mSyncOperation.isConflict(op)) { |
| 3253 | // If the provided SyncOperation conflicts with a running one, the lower |
| 3254 | // priority sync is pre-empted. |
| 3255 | if (asc.mSyncOperation.findPriority() >= op.findPriority()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3256 | if (isLoggable) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3257 | Slog.v(TAG, "Rescheduling sync due to conflict " + op.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3258 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3259 | deferSyncH(op, SYNC_DELAY_ON_CONFLICT, "delay on conflict"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3260 | return; |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 3261 | } else { |
Matthew Williams | fa77418 | 2013-06-18 15:44:11 -0700 | [diff] [blame] | 3262 | if (isLoggable) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3263 | Slog.v(TAG, "Pushing back running sync due to a higher priority sync"); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3264 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3265 | deferActiveSyncH(asc, "preempted"); |
Shreyas Basarge | 7680dd9 | 2016-02-11 14:52:47 +0000 | [diff] [blame] | 3266 | break; |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 3267 | } |
| 3268 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3269 | } |
| 3270 | |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3271 | final int syncOpState = computeSyncOpState(op); |
| 3272 | switch (syncOpState) { |
| 3273 | case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS: |
| 3274 | case SYNC_OP_STATE_INVALID: { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3275 | mSyncJobService.callJobFinished(op.jobId, false, |
| 3276 | "invalid op state: " + syncOpState); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3277 | } return; |
| 3278 | } |
| 3279 | |
| 3280 | if (!dispatchSyncOperation(op)) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3281 | mSyncJobService.callJobFinished(op.jobId, false, "dispatchSyncOperation() failed"); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3282 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3283 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3284 | setAuthorityPendingState(op.target); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3285 | } |
| 3286 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3287 | private ActiveSyncContext findActiveSyncContextH(int jobId) { |
| 3288 | for (ActiveSyncContext asc: mActiveSyncContexts) { |
| 3289 | SyncOperation op = asc.mSyncOperation; |
| 3290 | if (op != null && op.jobId == jobId) { |
| 3291 | return asc; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3292 | } |
| 3293 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3294 | return null; |
Matthew Williams | a474554 | 2015-12-10 20:29:02 +0000 | [diff] [blame] | 3295 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3296 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3297 | private void updateRunningAccountsH(EndPoint syncTargets) { |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3298 | AccountAndUser[] oldAccounts = mRunningAccounts; |
振淦王 | 60a7431 | 2015-12-01 16:37:31 +0800 | [diff] [blame] | 3299 | mRunningAccounts = AccountManagerService.getSingleton().getRunningAccounts(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3300 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3301 | Slog.v(TAG, "Accounts list: "); |
| 3302 | for (AccountAndUser acc : mRunningAccounts) { |
| 3303 | Slog.v(TAG, acc.toString()); |
| 3304 | } |
| 3305 | } |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 3306 | if (mLogger.enabled()) { |
| 3307 | mLogger.log("updateRunningAccountsH: ", Arrays.toString(mRunningAccounts)); |
| 3308 | } |
振淦王 | 60a7431 | 2015-12-01 16:37:31 +0800 | [diff] [blame] | 3309 | if (mBootCompleted) { |
| 3310 | doDatabaseCleanup(); |
| 3311 | } |
| 3312 | |
| 3313 | AccountAndUser[] accounts = mRunningAccounts; |
| 3314 | for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) { |
| 3315 | if (!containsAccountAndUser(accounts, |
| 3316 | currentSyncContext.mSyncOperation.target.account, |
| 3317 | currentSyncContext.mSyncOperation.target.userId)) { |
| 3318 | Log.d(TAG, "canceling sync since the account is no longer running"); |
| 3319 | sendSyncFinishedOrCanceledMessage(currentSyncContext, |
| 3320 | null /* no result since this is a cancel */); |
| 3321 | } |
| 3322 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3323 | |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3324 | // On account add, check if there are any settings to be restored. |
| 3325 | for (AccountAndUser aau : mRunningAccounts) { |
| 3326 | if (!containsAccountAndUser(oldAccounts, aau.account, aau.userId)) { |
| 3327 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3328 | Log.d(TAG, "Account " + aau.account + " added, checking sync restore data"); |
| 3329 | } |
| 3330 | AccountSyncSettingsBackupHelper.accountAdded(mContext); |
| 3331 | break; |
| 3332 | } |
| 3333 | } |
| 3334 | |
Rubin Xu | 2c548e0 | 2016-04-01 17:47:28 +0100 | [diff] [blame] | 3335 | // Cancel all jobs from non-existent accounts. |
| 3336 | AccountAndUser[] allAccounts = AccountManagerService.getSingleton().getAllAccounts(); |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 3337 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3338 | for (SyncOperation op: ops) { |
Rubin Xu | 2c548e0 | 2016-04-01 17:47:28 +0100 | [diff] [blame] | 3339 | if (!containsAccountAndUser(allAccounts, op.target.account, op.target.userId)) { |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 3340 | mLogger.log("canceling: ", op); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3341 | cancelJob(op, "updateRunningAccountsH()"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | if (syncTargets != null) { |
| 3346 | scheduleSync(syncTargets.account, syncTargets.userId, |
Svet Ganov | f6d424f1 | 2016-09-20 20:18:53 -0700 | [diff] [blame] | 3347 | SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider, |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 3348 | null, AuthorityInfo.NOT_INITIALIZED, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 3349 | ContentResolver.SYNC_EXEMPTION_NONE); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | /** |
| 3354 | * The given SyncOperation will be removed and a new one scheduled in its place if |
| 3355 | * an updated period or flex is specified. |
| 3356 | * @param syncOperation SyncOperation whose period and flex is to be updated. |
| 3357 | * @param pollFrequencyMillis new period in milliseconds. |
| 3358 | * @param flexMillis new flex time in milliseconds. |
| 3359 | */ |
| 3360 | private void maybeUpdateSyncPeriodH(SyncOperation syncOperation, long pollFrequencyMillis, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3361 | long flexMillis) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3362 | if (!(pollFrequencyMillis == syncOperation.periodMillis |
| 3363 | && flexMillis == syncOperation.flexMillis)) { |
| 3364 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 3365 | Slog.v(TAG, "updating period " + syncOperation + " to " + pollFrequencyMillis |
| 3366 | + " and flex to " + flexMillis); |
| 3367 | } |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3368 | SyncOperation newOp = new SyncOperation(syncOperation, pollFrequencyMillis, |
| 3369 | flexMillis); |
| 3370 | newOp.jobId = syncOperation.jobId; |
| 3371 | scheduleSyncOperationH(newOp); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | private void updateOrAddPeriodicSyncH(EndPoint target, long pollFrequency, long flex, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3376 | Bundle extras) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3377 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
| 3378 | verifyJobScheduler(); // Will fill in mScheduledSyncs cache if it is not already filled. |
| 3379 | final long pollFrequencyMillis = pollFrequency * 1000L; |
| 3380 | final long flexMillis = flex * 1000L; |
| 3381 | if (isLoggable) { |
| 3382 | Slog.v(TAG, "Addition to periodic syncs requested: " + target |
| 3383 | + " period: " + pollFrequency |
| 3384 | + " flexMillis: " + flex |
| 3385 | + " extras: " + extras.toString()); |
| 3386 | } |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 3387 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3388 | for (SyncOperation op: ops) { |
| 3389 | if (op.isPeriodic && op.target.matchesSpec(target) |
| 3390 | && syncExtrasEquals(op.extras, extras, true /* includeSyncSettings */)) { |
| 3391 | maybeUpdateSyncPeriodH(op, pollFrequencyMillis, flexMillis); |
| 3392 | return; |
| 3393 | } |
| 3394 | } |
| 3395 | |
| 3396 | if (isLoggable) { |
| 3397 | Slog.v(TAG, "Adding new periodic sync: " + target |
| 3398 | + " period: " + pollFrequency |
| 3399 | + " flexMillis: " + flex |
| 3400 | + " extras: " + extras.toString()); |
| 3401 | } |
| 3402 | |
| 3403 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> |
| 3404 | syncAdapterInfo = mSyncAdapters.getServiceInfo( |
| 3405 | SyncAdapterType.newKey( |
| 3406 | target.provider, target.account.type), |
| 3407 | target.userId); |
| 3408 | if (syncAdapterInfo == null) { |
| 3409 | return; |
| 3410 | } |
| 3411 | |
| 3412 | SyncOperation op = new SyncOperation(target, syncAdapterInfo.uid, |
| 3413 | syncAdapterInfo.componentName.getPackageName(), SyncOperation.REASON_PERIODIC, |
| 3414 | SyncStorageEngine.SOURCE_PERIODIC, extras, |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3415 | syncAdapterInfo.type.allowParallelSyncs(), true, SyncOperation.NO_JOB_ID, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 3416 | pollFrequencyMillis, flexMillis, ContentResolver.SYNC_EXEMPTION_NONE); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3417 | |
| 3418 | final int syncOpState = computeSyncOpState(op); |
| 3419 | switch (syncOpState) { |
| 3420 | case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS: { |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 3421 | String packageName = op.owningPackage; |
| 3422 | final int userId = UserHandle.getUserId(op.owningUid); |
| 3423 | // If the app did not run and has no account access, done |
| 3424 | if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) { |
| 3425 | return; |
| 3426 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3427 | mAccountManagerInternal.requestAccountAccess(op.target.account, |
Svet Ganov | 973edd19 | 2016-09-08 20:15:55 -0700 | [diff] [blame] | 3428 | packageName, userId, new RemoteCallback((Bundle result) -> { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3429 | if (result != null |
| 3430 | && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 3431 | updateOrAddPeriodicSync(target, pollFrequency, flex, extras); |
| 3432 | } |
| 3433 | } |
| 3434 | )); |
| 3435 | } return; |
| 3436 | |
| 3437 | case SYNC_OP_STATE_INVALID: { |
| 3438 | return; |
| 3439 | } |
| 3440 | } |
| 3441 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3442 | scheduleSyncOperationH(op); |
| 3443 | mSyncStorageEngine.reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS); |
| 3444 | } |
| 3445 | |
| 3446 | /** |
| 3447 | * Remove this periodic sync operation and all one-off operations initiated by it. |
| 3448 | */ |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3449 | private void removePeriodicSyncInternalH(SyncOperation syncOperation, String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3450 | // Remove this periodic sync and all one-off syncs initiated by it. |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 3451 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3452 | for (SyncOperation op: ops) { |
| 3453 | if (op.sourcePeriodicId == syncOperation.jobId || op.jobId == syncOperation.jobId) { |
| 3454 | ActiveSyncContext asc = findActiveSyncContextH(syncOperation.jobId); |
| 3455 | if (asc != null) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3456 | mSyncJobService.callJobFinished(syncOperation.jobId, false, |
| 3457 | "removePeriodicSyncInternalH"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3458 | runSyncFinishedOrCanceledH(null, asc); |
| 3459 | } |
Makoto Onuki | bbf6d8c | 2017-08-11 12:11:39 -0700 | [diff] [blame] | 3460 | mLogger.log("removePeriodicSyncInternalH-canceling: ", op); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3461 | cancelJob(op, why); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3462 | } |
| 3463 | } |
| 3464 | } |
| 3465 | |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3466 | private void removePeriodicSyncH(EndPoint target, Bundle extras, String why) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3467 | verifyJobScheduler(); |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 3468 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3469 | for (SyncOperation op: ops) { |
| 3470 | if (op.isPeriodic && op.target.matchesSpec(target) |
| 3471 | && syncExtrasEquals(op.extras, extras, true /* includeSyncSettings */)) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3472 | removePeriodicSyncInternalH(op, why); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3473 | } |
| 3474 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 3475 | } |
| 3476 | |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3477 | private boolean isSyncNotUsingNetworkH(ActiveSyncContext activeSyncContext) { |
| 3478 | final long bytesTransferredCurrent = |
| 3479 | getTotalBytesTransferredByUid(activeSyncContext.mSyncAdapterUid); |
| 3480 | final long deltaBytesTransferred = |
| 3481 | bytesTransferredCurrent - activeSyncContext.mBytesTransferredAtLastPoll; |
| 3482 | |
| 3483 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 3484 | // Bytes transferred |
| 3485 | long remainder = deltaBytesTransferred; |
| 3486 | long mb = remainder / (1024 * 1024); |
| 3487 | remainder %= 1024 * 1024; |
| 3488 | long kb = remainder / 1024; |
| 3489 | remainder %= 1024; |
| 3490 | long b = remainder; |
| 3491 | Log.d(TAG, String.format( |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3492 | "Time since last update: %ds. Delta transferred: %dMBs,%dKBs,%dBs", |
| 3493 | (SystemClock.elapsedRealtime() |
| 3494 | - activeSyncContext.mLastPolledTimeElapsed)/1000, |
| 3495 | mb, kb, b) |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3496 | ); |
| 3497 | } |
| 3498 | return (deltaBytesTransferred <= SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES); |
| 3499 | } |
| 3500 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3501 | /** |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3502 | * Determine if a sync is no longer valid and should be dropped. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3503 | */ |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3504 | private int computeSyncOpState(SyncOperation op) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3505 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3506 | int state; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3507 | final EndPoint target = op.target; |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3508 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3509 | // Drop the sync if the account of this operation no longer exists. |
| 3510 | AccountAndUser[] accounts = mRunningAccounts; |
| 3511 | if (!containsAccountAndUser(accounts, target.account, target.userId)) { |
| 3512 | if (isLoggable) { |
| 3513 | Slog.v(TAG, " Dropping sync operation: account doesn't exist."); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3514 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3515 | return SYNC_OP_STATE_INVALID; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3516 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3517 | // Drop this sync request if it isn't syncable. |
Philip P. Moltmann | 486b241 | 2018-01-03 11:29:01 -0800 | [diff] [blame] | 3518 | state = computeSyncable(target.account, target.userId, target.provider, true); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3519 | if (state == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3520 | if (isLoggable) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3521 | Slog.v(TAG, " Dropping sync operation: " |
| 3522 | + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3523 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3524 | return SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3525 | } |
Svet Ganov | dfed1c7 | 2016-08-25 15:40:52 -0700 | [diff] [blame] | 3526 | if (state == AuthorityInfo.NOT_SYNCABLE) { |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3527 | if (isLoggable) { |
Svet Ganov | dfed1c7 | 2016-08-25 15:40:52 -0700 | [diff] [blame] | 3528 | Slog.v(TAG, " Dropping sync operation: isSyncable == NOT_SYNCABLE"); |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3529 | } |
| 3530 | return SYNC_OP_STATE_INVALID; |
| 3531 | } |
| 3532 | |
| 3533 | final boolean syncEnabled = mSyncStorageEngine.getMasterSyncAutomatically(target.userId) |
| 3534 | && mSyncStorageEngine.getSyncAutomatically(target.account, |
| 3535 | target.userId, target.provider); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3536 | |
| 3537 | // We ignore system settings that specify the sync is invalid if: |
| 3538 | // 1) It's manual - we try it anyway. When/if it fails it will be rescheduled. |
| 3539 | // or |
| 3540 | // 2) it's an initialisation sync - we just need to connect to it. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3541 | final boolean ignoreSystemConfiguration = op.isIgnoreSettings() || (state < 0); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3542 | |
| 3543 | // Sync not enabled. |
| 3544 | if (!syncEnabled && !ignoreSystemConfiguration) { |
| 3545 | if (isLoggable) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3546 | Slog.v(TAG, " Dropping sync operation: disallowed by settings/network."); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3547 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3548 | return SYNC_OP_STATE_INVALID; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3549 | } |
Svetoslav Ganov | 5cb2973 | 2016-07-11 19:32:30 -0700 | [diff] [blame] | 3550 | return SYNC_OP_STATE_VALID; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3551 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3552 | |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 3553 | private boolean dispatchSyncOperation(SyncOperation op) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3554 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3555 | Slog.v(TAG, "dispatchSyncOperation: we are going to sync " + op); |
| 3556 | Slog.v(TAG, "num active syncs: " + mActiveSyncContexts.size()); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3557 | for (ActiveSyncContext syncContext : mActiveSyncContexts) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3558 | Slog.v(TAG, syncContext.toString()); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3559 | } |
| 3560 | } |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 3561 | if (op.isAppStandbyExempted()) { |
| 3562 | final UsageStatsManagerInternal usmi = LocalServices.getService( |
| 3563 | UsageStatsManagerInternal.class); |
| 3564 | if (usmi != null) { |
| 3565 | usmi.reportExemptedSyncStart(op.owningPackage, |
| 3566 | UserHandle.getUserId(op.owningUid)); |
| 3567 | } |
| 3568 | } |
| 3569 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3570 | // Connect to the sync adapter. |
| 3571 | int targetUid; |
| 3572 | ComponentName targetComponent; |
| 3573 | final SyncStorageEngine.EndPoint info = op.target; |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3574 | SyncAdapterType syncAdapterType = |
| 3575 | SyncAdapterType.newKey(info.provider, info.account.type); |
| 3576 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo; |
| 3577 | syncAdapterInfo = mSyncAdapters.getServiceInfo(syncAdapterType, info.userId); |
| 3578 | if (syncAdapterInfo == null) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3579 | mLogger.log("dispatchSyncOperation() failed: no sync adapter info for ", |
| 3580 | syncAdapterType); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3581 | Log.d(TAG, "can't find a sync adapter for " + syncAdapterType |
| 3582 | + ", removing settings for it"); |
| 3583 | mSyncStorageEngine.removeAuthority(info); |
| 3584 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3585 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3586 | targetUid = syncAdapterInfo.uid; |
| 3587 | targetComponent = syncAdapterInfo.componentName; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3588 | ActiveSyncContext activeSyncContext = |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3589 | new ActiveSyncContext(op, insertStartSyncEvent(op), targetUid); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3590 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3591 | Slog.v(TAG, "dispatchSyncOperation: starting " + activeSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3592 | } |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3593 | |
| 3594 | activeSyncContext.mSyncInfo = mSyncStorageEngine.addActiveSync(activeSyncContext); |
| 3595 | mActiveSyncContexts.add(activeSyncContext); |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3596 | |
| 3597 | // Post message to begin monitoring this sync's progress. |
| 3598 | postMonitorSyncProgressMessage(activeSyncContext); |
| 3599 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3600 | if (!activeSyncContext.bindToSyncAdapter(targetComponent, info.userId)) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3601 | mLogger.log("dispatchSyncOperation() failed: bind failed. target: ", |
| 3602 | targetComponent); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3603 | Slog.e(TAG, "Bind attempt failed - target: " + targetComponent); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3604 | closeActiveSyncContext(activeSyncContext); |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 3605 | return false; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3606 | } |
| 3607 | |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 3608 | return true; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3611 | private void runBoundToAdapterH(final ActiveSyncContext activeSyncContext, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3612 | IBinder syncAdapter) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3613 | final SyncOperation syncOperation = activeSyncContext.mSyncOperation; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3614 | try { |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 3615 | activeSyncContext.mIsLinkedToDeath = true; |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3616 | syncAdapter.linkToDeath(activeSyncContext, 0); |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 3617 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3618 | mLogger.log("Sync start: account=" + syncOperation.target.account, |
| 3619 | " authority=", syncOperation.target.provider, |
| 3620 | " reason=", SyncOperation.reasonToString(null, syncOperation.reason), |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3621 | " extras=", SyncOperation.extrasToString(syncOperation.extras), |
| 3622 | " adapter=", activeSyncContext.mSyncAdapter); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3623 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3624 | activeSyncContext.mSyncAdapter = ISyncAdapter.Stub.asInterface(syncAdapter); |
| 3625 | activeSyncContext.mSyncAdapter |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3626 | .startSync(activeSyncContext, syncOperation.target.provider, |
| 3627 | syncOperation.target.account, syncOperation.extras); |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3628 | |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3629 | mLogger.log("Sync is running now..."); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3630 | } catch (RemoteException remoteExc) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3631 | mLogger.log("Sync failed with RemoteException: ", remoteExc.toString()); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3632 | Log.d(TAG, "maybeStartNextSync: caught a RemoteException, rescheduling", remoteExc); |
| 3633 | closeActiveSyncContext(activeSyncContext); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3634 | increaseBackoffSetting(syncOperation.target); |
| 3635 | scheduleSyncOperationH(syncOperation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3636 | } catch (RuntimeException exc) { |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3637 | mLogger.log("Sync failed with RuntimeException: ", exc.toString()); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3638 | closeActiveSyncContext(activeSyncContext); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3639 | Slog.e(TAG, "Caught RuntimeException while starting the sync " + syncOperation, exc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3640 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3641 | } |
| 3642 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3643 | /** |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3644 | * Cancel the sync for the provided target that matches the given bundle. |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3645 | * @param info Can have null fields to indicate all the active syncs for that field. |
| 3646 | * @param extras Can be null to indicate <strong>all</strong> syncs for the given endpoint. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3647 | */ |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3648 | private void cancelActiveSyncH(SyncStorageEngine.EndPoint info, Bundle extras, |
| 3649 | String why) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3650 | ArrayList<ActiveSyncContext> activeSyncs = |
| 3651 | new ArrayList<ActiveSyncContext>(mActiveSyncContexts); |
| 3652 | for (ActiveSyncContext activeSyncContext : activeSyncs) { |
| 3653 | if (activeSyncContext != null) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3654 | final SyncStorageEngine.EndPoint opInfo = |
| 3655 | activeSyncContext.mSyncOperation.target; |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3656 | if (!opInfo.matchesSpec(info)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3657 | continue; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3658 | } |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3659 | if (extras != null && |
| 3660 | !syncExtrasEquals(activeSyncContext.mSyncOperation.extras, |
| 3661 | extras, |
| 3662 | false /* no config settings */)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3663 | continue; |
| 3664 | } |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3665 | mSyncJobService.callJobFinished(activeSyncContext.mSyncOperation.jobId, false, |
| 3666 | why); |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3667 | runSyncFinishedOrCanceledH(null /* cancel => no result */, activeSyncContext); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3668 | } |
| 3669 | } |
| 3670 | } |
| 3671 | |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3672 | /** |
| 3673 | * Should be called when a one-off instance of a periodic sync completes successfully. |
| 3674 | */ |
| 3675 | private void reschedulePeriodicSyncH(SyncOperation syncOperation) { |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3676 | // Ensure that the periodic sync wasn't removed. |
| 3677 | SyncOperation periodicSync = null; |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 3678 | List<SyncOperation> ops = getAllPendingSyncs(); |
Shreyas Basarge | 2b4c8f9 | 2016-02-08 23:52:27 +0000 | [diff] [blame] | 3679 | for (SyncOperation op: ops) { |
| 3680 | if (op.isPeriodic && syncOperation.matchesPeriodicOperation(op)) { |
| 3681 | periodicSync = op; |
| 3682 | break; |
| 3683 | } |
| 3684 | } |
| 3685 | if (periodicSync == null) { |
| 3686 | return; |
| 3687 | } |
| 3688 | scheduleSyncOperationH(periodicSync); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3689 | } |
| 3690 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 3691 | private void runSyncFinishedOrCanceledH(SyncResult syncResult, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3692 | ActiveSyncContext activeSyncContext) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3693 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 3694 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3695 | final SyncOperation syncOperation = activeSyncContext.mSyncOperation; |
| 3696 | final SyncStorageEngine.EndPoint info = syncOperation.target; |
| 3697 | |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 3698 | if (activeSyncContext.mIsLinkedToDeath) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3699 | activeSyncContext.mSyncAdapter.asBinder().unlinkToDeath(activeSyncContext, 0); |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 3700 | activeSyncContext.mIsLinkedToDeath = false; |
| 3701 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3702 | final long elapsedTime = SystemClock.elapsedRealtime() - activeSyncContext.mStartTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3703 | String historyMessage; |
| 3704 | int downstreamActivity; |
| 3705 | int upstreamActivity; |
Shreyas Basarge | bd4c3ea | 2016-06-16 11:54:35 +0100 | [diff] [blame] | 3706 | |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3707 | mLogger.log("runSyncFinishedOrCanceledH() op=", syncOperation, " result=", syncResult); |
Shreyas Basarge | bd4c3ea | 2016-06-16 11:54:35 +0100 | [diff] [blame] | 3708 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3709 | if (syncResult != null) { |
| 3710 | if (isLoggable) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3711 | Slog.v(TAG, "runSyncFinishedOrCanceled [finished]: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3712 | + syncOperation + ", result " + syncResult); |
| 3713 | } |
| 3714 | |
Makoto Onuki | 9243d9c | 2017-08-15 14:56:47 -0700 | [diff] [blame] | 3715 | // In the non-canceled case, close the active sync context before doing the rest |
| 3716 | // of the stuff. |
| 3717 | closeActiveSyncContext(activeSyncContext); |
| 3718 | |
| 3719 | // Note this part is probably okay to do before closeActiveSyncContext()... |
| 3720 | // But moved here to restore OC-dev's behavior. See b/64597061. |
| 3721 | if (!syncOperation.isPeriodic) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3722 | cancelJob(syncOperation, "runSyncFinishedOrCanceledH()-finished"); |
Makoto Onuki | 9243d9c | 2017-08-15 14:56:47 -0700 | [diff] [blame] | 3723 | } |
| 3724 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3725 | if (!syncResult.hasError()) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3726 | historyMessage = SyncStorageEngine.MESG_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3727 | // TODO: set these correctly when the SyncResult is extended to include it |
| 3728 | downstreamActivity = 0; |
| 3729 | upstreamActivity = 0; |
Makoto Onuki | a9dca24 | 2017-06-21 17:06:49 -0700 | [diff] [blame] | 3730 | clearBackoffSetting(syncOperation.target, "sync success"); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3731 | |
| 3732 | // If the operation completes successfully and it was scheduled due to |
| 3733 | // a periodic operation failing, we reschedule the periodic operation to |
| 3734 | // start from now. |
| 3735 | if (syncOperation.isDerivedFromFailedPeriodicSync()) { |
| 3736 | reschedulePeriodicSyncH(syncOperation); |
| 3737 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3738 | } else { |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 3739 | Log.w(TAG, "failed sync operation " + syncOperation + ", " + syncResult); |
| 3740 | |
| 3741 | syncOperation.retries++; |
| 3742 | if (syncOperation.retries > mConstants.getMaxRetriesWithAppStandbyExemption()) { |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 3743 | syncOperation.syncExemptionFlag = ContentResolver.SYNC_EXEMPTION_NONE; |
Makoto Onuki | aad2b51 | 2018-02-07 09:31:46 -0800 | [diff] [blame] | 3744 | } |
| 3745 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 3746 | // the operation failed so increase the backoff time |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3747 | increaseBackoffSetting(syncOperation.target); |
| 3748 | if (!syncOperation.isPeriodic) { |
| 3749 | // reschedule the sync if so indicated by the syncResult |
| 3750 | maybeRescheduleSync(syncResult, syncOperation); |
| 3751 | } else { |
| 3752 | // create a normal sync instance that will respect adapter backoffs |
Shreyas Basarge | ba1f790 | 2016-10-01 00:19:44 +0100 | [diff] [blame] | 3753 | postScheduleSyncMessage(syncOperation.createOneTimeSyncOperation(), |
| 3754 | 0 /* min delay */); |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3755 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 3756 | historyMessage = ContentResolver.syncErrorToString( |
| 3757 | syncResultToErrorNumber(syncResult)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3758 | // TODO: set these correctly when the SyncResult is extended to include it |
| 3759 | downstreamActivity = 0; |
| 3760 | upstreamActivity = 0; |
| 3761 | } |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3762 | setDelayUntilTime(syncOperation.target, syncResult.delayUntil); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3763 | } else { |
| 3764 | if (isLoggable) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3765 | Slog.v(TAG, "runSyncFinishedOrCanceled [canceled]: " + syncOperation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3766 | } |
Makoto Onuki | 9243d9c | 2017-08-15 14:56:47 -0700 | [diff] [blame] | 3767 | |
| 3768 | if (!syncOperation.isPeriodic) { |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 3769 | cancelJob(syncOperation, "runSyncFinishedOrCanceledH()-canceled"); |
Makoto Onuki | 9243d9c | 2017-08-15 14:56:47 -0700 | [diff] [blame] | 3770 | } |
| 3771 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3772 | if (activeSyncContext.mSyncAdapter != null) { |
| 3773 | try { |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3774 | mLogger.log("Calling cancelSync for runSyncFinishedOrCanceled ", |
| 3775 | activeSyncContext, " adapter=", activeSyncContext.mSyncAdapter); |
Fred Quintana | 21bb0de | 2009-06-16 10:24:58 -0700 | [diff] [blame] | 3776 | activeSyncContext.mSyncAdapter.cancelSync(activeSyncContext); |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3777 | mLogger.log("Canceled"); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3778 | } catch (RemoteException e) { |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3779 | mLogger.log("RemoteException ", Log.getStackTraceString(e)); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 3780 | // we don't need to retry this in this case |
| 3781 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3782 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3783 | historyMessage = SyncStorageEngine.MESG_CANCELED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3784 | downstreamActivity = 0; |
| 3785 | upstreamActivity = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3786 | |
Makoto Onuki | 9243d9c | 2017-08-15 14:56:47 -0700 | [diff] [blame] | 3787 | // In the cancel sync case, close it after calling cancelSync(). |
| 3788 | closeActiveSyncContext(activeSyncContext); |
| 3789 | } |
Makoto Onuki | 6a6ae04 | 2017-07-20 13:30:12 -0700 | [diff] [blame] | 3790 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3791 | stopSyncEvent(activeSyncContext.mHistoryRowId, syncOperation, historyMessage, |
| 3792 | upstreamActivity, downstreamActivity, elapsedTime); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3793 | // Check for full-resync and schedule it after closing off the last sync. |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3794 | if (syncResult != null && syncResult.tooManyDeletions) { |
| 3795 | installHandleTooManyDeletesNotification(info.account, |
| 3796 | info.provider, syncResult.stats.numDeletes, |
| 3797 | info.userId); |
| 3798 | } else { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 3799 | mNotificationMgr.cancelAsUser( |
| 3800 | Integer.toString(info.account.hashCode() ^ info.provider.hashCode()), |
| 3801 | SystemMessage.NOTE_SYNC_ERROR, |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3802 | new UserHandle(info.userId)); |
| 3803 | } |
| 3804 | if (syncResult != null && syncResult.fullSyncRequested) { |
| 3805 | scheduleSyncOperationH( |
| 3806 | new SyncOperation(info.account, info.userId, |
| 3807 | syncOperation.owningUid, syncOperation.owningPackage, |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3808 | syncOperation.reason, |
| 3809 | syncOperation.syncSource, info.provider, new Bundle(), |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 3810 | syncOperation.allowParallelSyncs, |
Makoto Onuki | 75ad249 | 2018-03-28 14:42:42 -0700 | [diff] [blame] | 3811 | syncOperation.syncExemptionFlag)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3812 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3813 | } |
| 3814 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3815 | private void closeActiveSyncContext(ActiveSyncContext activeSyncContext) { |
| 3816 | activeSyncContext.close(); |
| 3817 | mActiveSyncContexts.remove(activeSyncContext); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 3818 | mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo, |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3819 | activeSyncContext.mSyncOperation.target.userId); |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3820 | |
| 3821 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Shreyas Basarge | 8c834c0 | 2016-01-07 13:53:16 +0000 | [diff] [blame] | 3822 | Slog.v(TAG, "removing all MESSAGE_MONITOR_SYNC & MESSAGE_SYNC_EXPIRED for " |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3823 | + activeSyncContext.toString()); |
| 3824 | } |
Matthew Williams | 1967c8d | 2015-06-19 19:03:13 -0700 | [diff] [blame] | 3825 | mSyncHandler.removeMessages(SyncHandler.MESSAGE_MONITOR_SYNC, activeSyncContext); |
Makoto Onuki | 9243d9c | 2017-08-15 14:56:47 -0700 | [diff] [blame] | 3826 | |
| 3827 | mLogger.log("closeActiveSyncContext: ", activeSyncContext); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3828 | } |
| 3829 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | /** |
| 3831 | * Convert the error-containing SyncResult into the Sync.History error number. Since |
| 3832 | * the SyncResult may indicate multiple errors at once, this method just returns the |
| 3833 | * most "serious" error. |
| 3834 | * @param syncResult the SyncResult from which to read |
| 3835 | * @return the most "serious" error set in the SyncResult |
| 3836 | * @throws IllegalStateException if the SyncResult does not indicate any errors. |
| 3837 | * If SyncResult.error() is true then it is safe to call this. |
| 3838 | */ |
| 3839 | private int syncResultToErrorNumber(SyncResult syncResult) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3840 | if (syncResult.syncAlreadyInProgress) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3841 | return ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3842 | if (syncResult.stats.numAuthExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3843 | return ContentResolver.SYNC_ERROR_AUTHENTICATION; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3844 | if (syncResult.stats.numIoExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3845 | return ContentResolver.SYNC_ERROR_IO; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3846 | if (syncResult.stats.numParseExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3847 | return ContentResolver.SYNC_ERROR_PARSE; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3848 | if (syncResult.stats.numConflictDetectedExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3849 | return ContentResolver.SYNC_ERROR_CONFLICT; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3850 | if (syncResult.tooManyDeletions) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3851 | return ContentResolver.SYNC_ERROR_TOO_MANY_DELETIONS; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3852 | if (syncResult.tooManyRetries) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3853 | return ContentResolver.SYNC_ERROR_TOO_MANY_RETRIES; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 3854 | if (syncResult.databaseError) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 3855 | return ContentResolver.SYNC_ERROR_INTERNAL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3856 | throw new IllegalStateException("we are not in an error state, " + syncResult); |
| 3857 | } |
| 3858 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 3859 | private void installHandleTooManyDeletesNotification(Account account, String authority, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3860 | long numDeletes, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3861 | if (mNotificationMgr == null) return; |
Fred Quintana | c848b70 | 2009-08-25 20:18:46 -0700 | [diff] [blame] | 3862 | |
| 3863 | final ProviderInfo providerInfo = mContext.getPackageManager().resolveContentProvider( |
| 3864 | authority, 0 /* flags */); |
| 3865 | if (providerInfo == null) { |
| 3866 | return; |
| 3867 | } |
| 3868 | CharSequence authorityName = providerInfo.loadLabel(mContext.getPackageManager()); |
| 3869 | |
Fred Quintana | b19e62a | 2010-12-16 13:54:43 -0800 | [diff] [blame] | 3870 | Intent clickIntent = new Intent(mContext, SyncActivityTooManyDeletes.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3871 | clickIntent.putExtra("account", account); |
Tadashi G. Takaoka | 86135d3 | 2009-09-24 19:31:44 -0700 | [diff] [blame] | 3872 | clickIntent.putExtra("authority", authority); |
Fred Quintana | c848b70 | 2009-08-25 20:18:46 -0700 | [diff] [blame] | 3873 | clickIntent.putExtra("provider", authorityName.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3874 | clickIntent.putExtra("numDeletes", numDeletes); |
| 3875 | |
| 3876 | if (!isActivityAvailable(clickIntent)) { |
| 3877 | Log.w(TAG, "No activity found to handle too many deletes."); |
| 3878 | return; |
| 3879 | } |
| 3880 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3881 | UserHandle user = new UserHandle(userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3882 | final PendingIntent pendingIntent = PendingIntent |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 3883 | .getActivityAsUser(mContext, 0, clickIntent, |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3884 | PendingIntent.FLAG_CANCEL_CURRENT, null, user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3885 | |
| 3886 | CharSequence tooManyDeletesDescFormat = mContext.getResources().getText( |
| 3887 | R.string.contentServiceTooManyDeletesNotificationDesc); |
| 3888 | |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3889 | Context contextForUser = getContextForUser(user); |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 3890 | Notification notification = |
| 3891 | new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 3892 | .setSmallIcon(R.drawable.stat_notify_sync_error) |
| 3893 | .setTicker(mContext.getString(R.string.contentServiceSync)) |
| 3894 | .setWhen(System.currentTimeMillis()) |
| 3895 | .setColor(contextForUser.getColor( |
| 3896 | com.android.internal.R.color.system_notification_accent_color)) |
| 3897 | .setContentTitle(contextForUser.getString( |
| 3898 | R.string.contentServiceSyncNotificationTitle)) |
| 3899 | .setContentText( |
| 3900 | String.format(tooManyDeletesDescFormat.toString(), authorityName)) |
| 3901 | .setContentIntent(pendingIntent) |
| 3902 | .build(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3903 | notification.flags |= Notification.FLAG_ONGOING_EVENT; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 3904 | mNotificationMgr.notifyAsUser( |
| 3905 | Integer.toString(account.hashCode() ^ authority.hashCode()), |
| 3906 | SystemMessage.NOTE_SYNC_ERROR, |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 3907 | notification, user); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3908 | } |
| 3909 | |
| 3910 | /** |
| 3911 | * Checks whether an activity exists on the system image for the given intent. |
| 3912 | * |
| 3913 | * @param intent The intent for an activity. |
| 3914 | * @return Whether or not an activity exists. |
| 3915 | */ |
| 3916 | private boolean isActivityAvailable(Intent intent) { |
| 3917 | PackageManager pm = mContext.getPackageManager(); |
| 3918 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 3919 | int listSize = list.size(); |
| 3920 | for (int i = 0; i < listSize; i++) { |
| 3921 | ResolveInfo resolveInfo = list.get(i); |
| 3922 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 3923 | != 0) { |
| 3924 | return true; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | return false; |
| 3929 | } |
| 3930 | |
| 3931 | public long insertStartSyncEvent(SyncOperation syncOperation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3932 | final long now = System.currentTimeMillis(); |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3933 | EventLog.writeEvent(2720, |
| 3934 | syncOperation.toEventLog(SyncStorageEngine.EVENT_START)); |
| 3935 | return mSyncStorageEngine.insertStartSyncEvent(syncOperation, now); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage, |
Ritesh Reddy | ae0bcdd | 2015-12-23 18:24:23 +0000 | [diff] [blame] | 3939 | int upstreamActivity, int downstreamActivity, long elapsedTime) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3940 | EventLog.writeEvent(2720, |
| 3941 | syncOperation.toEventLog(SyncStorageEngine.EVENT_STOP)); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 3942 | mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime, |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 3943 | resultMessage, downstreamActivity, upstreamActivity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3944 | } |
| 3945 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3946 | |
Matthew Williams | 8b76d20 | 2015-05-03 18:16:25 -0700 | [diff] [blame] | 3947 | private boolean isSyncStillActiveH(ActiveSyncContext activeSyncContext) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 3948 | for (ActiveSyncContext sync : mActiveSyncContexts) { |
| 3949 | if (sync == activeSyncContext) { |
| 3950 | return true; |
| 3951 | } |
| 3952 | } |
| 3953 | return false; |
| 3954 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 3955 | |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3956 | /** |
| 3957 | * Sync extra comparison function. |
| 3958 | * @param b1 bundle to compare |
| 3959 | * @param b2 other bundle to compare |
| 3960 | * @param includeSyncSettings if false, ignore system settings in bundle. |
| 3961 | */ |
| 3962 | public static boolean syncExtrasEquals(Bundle b1, Bundle b2, boolean includeSyncSettings) { |
| 3963 | if (b1 == b2) { |
| 3964 | return true; |
| 3965 | } |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3966 | // Exit early if we can. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3967 | if (includeSyncSettings && b1.size() != b2.size()) { |
| 3968 | return false; |
| 3969 | } |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3970 | Bundle bigger = b1.size() > b2.size() ? b1 : b2; |
| 3971 | Bundle smaller = b1.size() > b2.size() ? b2 : b1; |
| 3972 | for (String key : bigger.keySet()) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3973 | if (!includeSyncSettings && isSyncSetting(key)) { |
| 3974 | continue; |
| 3975 | } |
Matthew Williams | 8ef2204 | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3976 | if (!smaller.containsKey(key)) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3977 | return false; |
| 3978 | } |
Matthew Williams | 9ad2c84 | 2015-10-16 12:01:31 -0700 | [diff] [blame] | 3979 | if (!Objects.equals(bigger.get(key), smaller.get(key))) { |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 3980 | return false; |
| 3981 | } |
| 3982 | } |
| 3983 | return true; |
| 3984 | } |
| 3985 | |
| 3986 | /** |
| 3987 | * @return true if the provided key is used by the SyncManager in scheduling the sync. |
| 3988 | */ |
| 3989 | private static boolean isSyncSetting(String key) { |
| 3990 | if (key.equals(ContentResolver.SYNC_EXTRAS_EXPEDITED)) { |
| 3991 | return true; |
| 3992 | } |
| 3993 | if (key.equals(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS)) { |
| 3994 | return true; |
| 3995 | } |
| 3996 | if (key.equals(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF)) { |
| 3997 | return true; |
| 3998 | } |
| 3999 | if (key.equals(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY)) { |
| 4000 | return true; |
| 4001 | } |
| 4002 | if (key.equals(ContentResolver.SYNC_EXTRAS_MANUAL)) { |
| 4003 | return true; |
| 4004 | } |
| 4005 | if (key.equals(ContentResolver.SYNC_EXTRAS_UPLOAD)) { |
| 4006 | return true; |
| 4007 | } |
| 4008 | if (key.equals(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS)) { |
| 4009 | return true; |
| 4010 | } |
| 4011 | if (key.equals(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS)) { |
| 4012 | return true; |
| 4013 | } |
| 4014 | if (key.equals(ContentResolver.SYNC_EXTRAS_EXPECTED_UPLOAD)) { |
| 4015 | return true; |
| 4016 | } |
| 4017 | if (key.equals(ContentResolver.SYNC_EXTRAS_EXPECTED_DOWNLOAD)) { |
| 4018 | return true; |
| 4019 | } |
| 4020 | if (key.equals(ContentResolver.SYNC_EXTRAS_PRIORITY)) { |
| 4021 | return true; |
| 4022 | } |
| 4023 | if (key.equals(ContentResolver.SYNC_EXTRAS_DISALLOW_METERED)) { |
| 4024 | return true; |
| 4025 | } |
| 4026 | if (key.equals(ContentResolver.SYNC_EXTRAS_INITIALIZE)) { |
| 4027 | return true; |
| 4028 | } |
Makoto Onuki | 61283ec | 2018-01-31 17:22:36 -0800 | [diff] [blame] | 4029 | // if (key.equals(ContentResolver.SYNC_EXTRAS_APP_STANDBY_EXEMPTED)) { |
| 4030 | // return true; |
| 4031 | // } |
| 4032 | // No need to check virtual flags such as SYNC_VIRTUAL_EXTRAS_FORCE_FG_SYNC. |
Matthew Williams | 56dbf8f | 2013-07-26 12:56:39 -0700 | [diff] [blame] | 4033 | return false; |
| 4034 | } |
| 4035 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 4036 | static class PrintTable { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 4037 | private ArrayList<String[]> mTable = Lists.newArrayList(); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 4038 | private final int mCols; |
| 4039 | |
| 4040 | PrintTable(int cols) { |
| 4041 | mCols = cols; |
| 4042 | } |
| 4043 | |
| 4044 | void set(int row, int col, Object... values) { |
| 4045 | if (col + values.length > mCols) { |
| 4046 | throw new IndexOutOfBoundsException("Table only has " + mCols + |
| 4047 | " columns. can't set " + values.length + " at column " + col); |
| 4048 | } |
| 4049 | for (int i = mTable.size(); i <= row; i++) { |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 4050 | final String[] list = new String[mCols]; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 4051 | mTable.add(list); |
| 4052 | for (int j = 0; j < mCols; j++) { |
| 4053 | list[j] = ""; |
| 4054 | } |
| 4055 | } |
Makoto Onuki | 15e7a25 | 2017-06-08 17:12:05 -0700 | [diff] [blame] | 4056 | final String[] rowArray = mTable.get(row); |
| 4057 | for (int i = 0; i < values.length; i++) { |
| 4058 | final Object value = values[i]; |
| 4059 | rowArray[col + i] = (value == null) ? "" : value.toString(); |
| 4060 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | void writeTo(PrintWriter out) { |
| 4064 | final String[] formats = new String[mCols]; |
| 4065 | int totalLength = 0; |
| 4066 | for (int col = 0; col < mCols; ++col) { |
| 4067 | int maxLength = 0; |
| 4068 | for (Object[] row : mTable) { |
| 4069 | final int length = row[col].toString().length(); |
| 4070 | if (length > maxLength) { |
| 4071 | maxLength = length; |
| 4072 | } |
| 4073 | } |
| 4074 | totalLength += maxLength; |
| 4075 | formats[col] = String.format("%%-%ds", maxLength); |
| 4076 | } |
Patrick Tjin | 3106816 | 2014-01-30 13:28:46 -0800 | [diff] [blame] | 4077 | formats[mCols - 1] = "%s"; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 4078 | printRow(out, formats, mTable.get(0)); |
| 4079 | totalLength += (mCols - 1) * 2; |
| 4080 | for (int i = 0; i < totalLength; ++i) { |
| 4081 | out.print("-"); |
| 4082 | } |
| 4083 | out.println(); |
| 4084 | for (int i = 1, mTableSize = mTable.size(); i < mTableSize; i++) { |
| 4085 | Object[] row = mTable.get(i); |
| 4086 | printRow(out, formats, row); |
| 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | private void printRow(PrintWriter out, String[] formats, Object[] row) { |
| 4091 | for (int j = 0, rowLength = row.length; j < rowLength; j++) { |
| 4092 | out.printf(String.format(formats[j], row[j].toString())); |
| 4093 | out.print(" "); |
| 4094 | } |
| 4095 | out.println(); |
| 4096 | } |
| 4097 | |
| 4098 | public int getNumRows() { |
| 4099 | return mTable.size(); |
| 4100 | } |
| 4101 | } |
Kenny Guy | 07ad8dc | 2014-09-01 20:56:12 +0100 | [diff] [blame] | 4102 | |
| 4103 | private Context getContextForUser(UserHandle user) { |
| 4104 | try { |
| 4105 | return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user); |
| 4106 | } catch (NameNotFoundException e) { |
| 4107 | // Default to mContext, not finding the package system is running as is unlikely. |
| 4108 | return mContext; |
| 4109 | } |
| 4110 | } |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 4111 | |
| 4112 | private void cancelJob(SyncOperation op, String why) { |
| 4113 | if (op == null) { |
| 4114 | Slog.wtf(TAG, "Null sync operation detected."); |
| 4115 | return; |
| 4116 | } |
| 4117 | if (op.isPeriodic) { |
| 4118 | mLogger.log("Removing periodic sync ", op, " for ", why); |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 4119 | } |
| 4120 | getJobScheduler().cancel(op.jobId); |
| 4121 | } |
| 4122 | |
Makoto Onuki | dd4b14f | 2017-08-17 14:03:48 -0700 | [diff] [blame] | 4123 | private void wtfWithLog(String message) { |
| 4124 | Slog.wtf(TAG, message); |
| 4125 | mLogger.log("WTF: ", message); |
| 4126 | } |
Makoto Onuki | 9498621 | 2018-04-11 16:24:46 -0700 | [diff] [blame] | 4127 | |
| 4128 | public void resetTodayStats() { |
| 4129 | mSyncStorageEngine.resetTodayStats(/*force=*/ true); |
| 4130 | } |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 4131 | } |