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