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