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