blob: c250005204ba1acafad3256bf44a947cdbf1bc8f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.content;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Alon Alberte0bde332011-09-22 14:26:16 -070019import android.accounts.Account;
Amith Yamasanif29f2362012-04-05 18:29:52 -070020import android.accounts.AccountAndUser;
Alon Alberte0bde332011-09-22 14:26:16 -070021import android.accounts.AccountManager;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070022import android.accounts.AccountManagerInternal;
Fred Quintana33e44692011-12-05 15:04:16 -080023import android.app.ActivityManager;
Amith Yamasani9422bdc2013-04-10 16:58:19 -070024import android.app.AppGlobals;
Alon Alberte0bde332011-09-22 14:26:16 -070025import android.app.Notification;
26import android.app.NotificationManager;
27import android.app.PendingIntent;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000028import android.app.job.JobInfo;
29import android.app.job.JobScheduler;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080030import android.content.BroadcastReceiver;
31import android.content.ComponentName;
32import android.content.ContentResolver;
33import android.content.Context;
34import android.content.ISyncAdapter;
35import android.content.ISyncContext;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080036import android.content.Intent;
37import android.content.IntentFilter;
Matthew Williamsfa774182013-06-18 15:44:11 -070038import android.content.PeriodicSync;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080039import android.content.ServiceConnection;
40import android.content.SyncActivityTooManyDeletes;
41import android.content.SyncAdapterType;
42import android.content.SyncAdaptersCache;
43import android.content.SyncInfo;
44import android.content.SyncResult;
45import android.content.SyncStatusInfo;
Alon Alberte0bde332011-09-22 14:26:16 -070046import android.content.pm.ApplicationInfo;
Amith Yamasani9422bdc2013-04-10 16:58:19 -070047import android.content.pm.PackageInfo;
Alon Alberte0bde332011-09-22 14:26:16 -070048import android.content.pm.PackageManager;
Kenny Guy07ad8dc2014-09-01 20:56:12 +010049import android.content.pm.PackageManager.NameNotFoundException;
Svet Ganov973edd192016-09-08 20:15:55 -070050import android.content.pm.PackageManagerInternal;
Alon Alberte0bde332011-09-22 14:26:16 -070051import android.content.pm.ProviderInfo;
52import android.content.pm.RegisteredServicesCache;
53import android.content.pm.RegisteredServicesCacheListener;
54import android.content.pm.ResolveInfo;
Amith Yamasani04e0d262012-02-14 11:50:53 -080055import android.content.pm.UserInfo;
Matthew Williams8b76d202015-05-03 18:16:25 -070056import android.database.ContentObserver;
Alon Alberte0bde332011-09-22 14:26:16 -070057import android.net.ConnectivityManager;
58import android.net.NetworkInfo;
Matthew Williams1967c8d2015-06-19 19:03:13 -070059import android.net.TrafficStats;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080060import android.os.BatteryStats;
Fred Quintana918339a2010-10-05 14:00:39 -070061import android.os.Bundle;
62import android.os.Handler;
Fred Quintana918339a2010-10-05 14:00:39 -070063import android.os.IBinder;
64import android.os.Looper;
65import android.os.Message;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000066import android.os.Messenger;
Fred Quintana918339a2010-10-05 14:00:39 -070067import android.os.PowerManager;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070068import android.os.RemoteCallback;
Fred Quintana918339a2010-10-05 14:00:39 -070069import android.os.RemoteException;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080070import android.os.ServiceManager;
Fred Quintana918339a2010-10-05 14:00:39 -070071import android.os.SystemClock;
72import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -070074import android.os.UserManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070075import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.text.format.Time;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.util.EventLog;
79import android.util.Log;
Fred Quintana307da1a2010-01-21 14:24:20 -080080import android.util.Pair;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000081import android.util.Slog;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000082
Chris Wren282cfef2017-03-27 15:01:44 -040083import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050084import com.android.internal.notification.SystemNotificationChannels;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070085import com.android.internal.util.ArrayUtils;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +000086import com.android.server.LocalServices;
87import com.android.server.job.JobSchedulerInternal;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000088import com.google.android.collect.Lists;
89import com.google.android.collect.Maps;
90
Alon Albert8e285552012-09-17 15:05:27 -070091import com.android.internal.R;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080092import com.android.internal.app.IBatteryStats;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070093import com.android.internal.os.BackgroundThread;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070094import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080095import com.android.server.accounts.AccountManagerService;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000096import com.android.server.backup.AccountSyncSettingsBackupHelper;
Georgi Nikolovdbe846b2013-06-25 14:09:56 -070097import com.android.server.content.SyncStorageEngine.AuthorityInfo;
Amith Yamasani96a0fd652015-04-10 16:16:30 -070098import com.android.server.content.SyncStorageEngine.EndPoint;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080099import com.android.server.content.SyncStorageEngine.OnSyncRequestListener;
Alon Albert8e285552012-09-17 15:05:27 -0700100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102import java.io.PrintWriter;
103import java.util.ArrayList;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000104import java.util.Arrays;
Fred Quintana918339a2010-10-05 14:00:39 -0700105import java.util.Collection;
106import java.util.Collections;
Alon Alberte0bde332011-09-22 14:26:16 -0700107import java.util.Comparator;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000108import java.util.HashMap;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000109import java.util.HashSet;
110import java.util.List;
111import java.util.Map;
Matthew Williams9ad2c842015-10-16 12:01:31 -0700112import java.util.Objects;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000113import java.util.Random;
114import java.util.Set;
Makoto Onuki15e7a252017-06-08 17:12:05 -0700115import java.util.function.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
117/**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000118 * 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 Basargecbf5ae92016-03-08 16:13:06 +0000122 * The scheduleSyncOperationH function also assigns a unique jobId to each
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000123 * 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 Project9066cfe2009-03-03 19:31:44 -0800136 * @hide
137 */
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700138public class SyncManager {
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700139 static final String TAG = "SyncManager";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700141 private static final boolean DEBUG_ACCOUNT_ACCESS = false;
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /** Delay a sync due to local changes this long. In milliseconds */
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700144 private static final long LOCAL_SYNC_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700146 static {
Fred Quintana918339a2010-10-05 14:00:39 -0700147 LOCAL_SYNC_DELAY =
148 SystemProperties.getLong("sync.local_sync_delay", 30 * 1000 /* 30 seconds */);
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
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 Quintana8570f742010-02-18 10:32:54 -0800164 * 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 Williams92a1c092014-08-25 19:18:32 -0700168 /**
Matthew Williams1967c8d2015-06-19 19:03:13 -0700169 * 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 Basarge8c834c02016-01-07 13:53:16 +0000182 * 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 Quintana3ec47302010-03-10 10:08:31 -0800192
Shreyas Basargefa272532016-03-09 16:52:48 +0000193 /**
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 Hackbornd45665b2014-02-26 12:35:32 -0800200 private static final String SYNC_WAKE_LOCK_PREFIX = "*sync*/";
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700201 private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm";
Fred Quintana918339a2010-10-05 14:00:39 -0700202 private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock";
203
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700204
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 Project9066cfe2009-03-03 19:31:44 -0800209 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
Amith Yamasani04e0d262012-02-14 11:50:53 -0800211 private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0];
212
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700213 // TODO: add better locking around mRunningAccounts
214 private volatile AccountAndUser[] mRunningAccounts = INITIAL_ACCOUNTS_ARRAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 volatile private PowerManager.WakeLock mHandleAlarmWakeLock;
Fred Quintana918339a2010-10-05 14:00:39 -0700217 volatile private PowerManager.WakeLock mSyncManagerWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 volatile private boolean mDataConnectionIsConnected = false;
219 volatile private boolean mStorageIsLow = false;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700220 volatile private boolean mDeviceIsIdle = false;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800221 volatile private boolean mReportedSyncActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 private final NotificationManager mNotificationMgr;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800224 private final IBatteryStats mBatteryStats;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000225 private JobScheduler mJobScheduler;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000226 private JobSchedulerInternal mJobSchedulerInternal;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000227 private SyncJobService mSyncJobService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700229 private SyncStorageEngine mSyncStorageEngine;
Jeff Sharkeya706e2f2012-10-16 12:02:42 -0700230
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700231 protected final ArrayList<ActiveSyncContext> mActiveSyncContexts = Lists.newArrayList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Fred Quintanaf892fb32009-08-27 21:32:08 -0700233 // Synchronized on "this". Instead of using this directly one should instead call
234 // its accessor, getConnManager().
235 private ConnectivityManager mConnManagerDoNotUseDirectly;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Matthew Williams8b76d202015-05-03 18:16:25 -0700237 /** Track whether the device has already been provisioned. */
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000238 private volatile boolean mProvisioned;
Matthew Williams8b76d202015-05-03 18:16:25 -0700239
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700240 protected SyncAdaptersCache mSyncAdapters;
Fred Quintana718d8a22009-04-29 17:53:20 -0700241
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000242 private final Random mRand;
243
Makoto Onukia9dca242017-06-21 17:06:49 -0700244 private final SyncLogger mLogger;
245
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000246 private boolean isJobIdInUseLockedH(int jobId, List<JobInfo> pendingJobs) {
247 for (JobInfo job: pendingJobs) {
248 if (job.getId() == jobId) {
249 return true;
250 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000251 }
252 for (ActiveSyncContext asc: mActiveSyncContexts) {
253 if (asc.mSyncOperation.jobId == jobId) {
254 return true;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000255 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000256 }
257 return false;
258 }
259
260 private int getUnusedJobIdH() {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000261 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 Basarge8c834c02016-01-07 13:53:16 +0000267 }
268
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000269 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 Basarge8c834c02016-01-07 13:53:16 +0000277 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000278 }
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000279 return pendingSyncs;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000280 }
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700281
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700282 private final BroadcastReceiver mStorageIntentReceiver =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700284 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 String action = intent.getAction();
287 if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
288 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000289 Slog.v(TAG, "Internal storage is low.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 }
291 mStorageIsLow = true;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700292 cancelActiveSync(
293 SyncStorageEngine.EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL,
Makoto Onukia9dca242017-06-21 17:06:49 -0700294 null /* any sync */,
295 "storage low");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) {
297 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000298 Slog.v(TAG, "Internal storage is ok.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 }
300 mStorageIsLow = false;
Makoto Onukia9dca242017-06-21 17:06:49 -0700301 rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL,
302 "storage ok");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 }
304 }
305 };
306
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700307 private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700308 @Override
Fred Quintana60307342009-03-24 22:48:12 -0700309 public void onReceive(Context context, Intent intent) {
Matthew Williams8b76d202015-05-03 18:16:25 -0700310 mBootCompleted = true;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000311 // Called because it gets all pending jobs and stores them in mScheduledSyncs cache.
312 verifyJobScheduler();
Fred Quintanae91ebe22009-09-29 20:44:30 -0700313 mSyncHandler.onBootCompleted();
Fred Quintana60307342009-03-24 22:48:12 -0700314 }
315 };
316
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700317 private final BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700318 @Override
Amith Yamasanid648a602012-09-26 15:06:10 -0700319 public void onReceive(Context context, Intent intent) {
Shreyas Basargedcb88c82016-02-03 00:09:18 +0000320 EndPoint target = new EndPoint(null, null, context.getUserId());
321 updateRunningAccounts(target /* sync targets for user */);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700322 }
323 };
324
Fred Quintanab3029c32010-04-06 13:27:12 -0700325 private final PowerManager mPowerManager;
Ashish Sharma69d95de2012-04-11 17:27:24 -0700326
Amith Yamasani9535c912012-10-10 21:48:33 -0700327 private final UserManager mUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -0700328
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700329 private final AccountManager mAccountManager;
330
331 private final AccountManagerInternal mAccountManagerInternal;
332
Svet Ganov973edd192016-09-08 20:15:55 -0700333 private final PackageManagerInternal mPackageManagerInternal;
334
Amith Yamasani258848d2012-08-10 17:06:33 -0700335 private List<UserInfo> getAllUsers() {
Amith Yamasani9535c912012-10-10 21:48:33 -0700336 return mUserManager.getUsers();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800337 }
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 Basarge8c834c02016-01-07 13:53:16 +0000351 /** 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");
振淦王60a74312015-12-01 16:37:31 +0800354 // Update accounts in handler thread.
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000355 Message m = mSyncHandler.obtainMessage(SyncHandler.MESSAGE_ACCOUNTS_UPDATED);
356 m.obj = target;
357 m.sendToTarget();
Fred Quintanad9d2f112009-04-23 13:36:27 -0700358 }
359
Jeff Sharkey8f55d112012-10-11 11:00:21 -0700360 private void doDatabaseCleanup() {
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700361 for (UserInfo user : mUserManager.getUsers(true)) {
362 // Skip any partially created/removed users
363 if (user.partial) continue;
Svetoslavf3f02ac2015-09-08 14:36:35 -0700364 Account[] accountsForUser = AccountManagerService.getSingleton().getAccounts(
365 user.id, mContext.getOpPackageName());
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000366
Jeff Sharkey8f55d112012-10-11 11:00:21 -0700367 mSyncStorageEngine.doDatabaseCleanup(accountsForUser, user.id);
368 }
369 }
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 private BroadcastReceiver mConnectivityIntentReceiver =
372 new BroadcastReceiver() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000373 @Override
374 public void onReceive(Context context, Intent intent) {
375 final boolean wasConnected = mDataConnectionIsConnected;
Alon Alberted1d2532011-02-15 14:02:14 -0800376
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000377 // 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 Onukia9dca242017-06-21 17:06:49 -0700385 // Note the location of this code was wrong from nyc to oc; fixed in DR.
386 clearAllBackoffs("network reconnect");
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000387 }
Matthew Williams119aac92014-09-28 20:42:23 -0700388 }
Alon Alberted1d2532011-02-15 14:02:14 -0800389 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000390 };
391
Makoto Onukia9dca242017-06-21 17:06:49 -0700392 private void clearAllBackoffs(String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000393 mSyncStorageEngine.clearAllBackoffsLocked();
Makoto Onukia9dca242017-06-21 17:06:49 -0700394 rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL, why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396
Alon Albert1bad83a2011-02-16 10:29:56 -0800397 private boolean readDataConnectionState() {
Alon Alberted1d2532011-02-15 14:02:14 -0800398 NetworkInfo networkInfo = getConnectivityManager().getActiveNetworkInfo();
399 return (networkInfo != null) && networkInfo.isConnected();
400 }
401
Dianne Hackborn55280a92009-05-07 15:53:46 -0700402 private BroadcastReceiver mShutdownIntentReceiver =
403 new BroadcastReceiver() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000404 @Override
405 public void onReceive(Context context, Intent intent) {
406 Log.w(TAG, "Writing sync state before shutdown...");
407 getSyncStorageEngine().writeAllState();
Makoto Onukife224e02017-06-29 14:11:14 -0700408 mLogger.log("Shutting down.");
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000409 }
410 };
Dianne Hackborn55280a92009-05-07 15:53:46 -0700411
Amith Yamasani13593602012-03-22 16:16:17 -0700412 private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() {
413 @Override
414 public void onReceive(Context context, Intent intent) {
Alon Albert8e285552012-09-17 15:05:27 -0700415 String action = intent.getAction();
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700416 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
417 if (userId == UserHandle.USER_NULL) return;
418
Alon Albert8e285552012-09-17 15:05:27 -0700419 if (Intent.ACTION_USER_REMOVED.equals(action)) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700420 onUserRemoved(userId);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700421 } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) {
422 onUserUnlocked(userId);
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -0700423 } else if (Intent.ACTION_USER_STOPPED.equals(action)) {
424 onUserStopped(userId);
Alon Albert8e285552012-09-17 15:05:27 -0700425 }
Amith Yamasani13593602012-03-22 16:16:17 -0700426 }
427 };
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 private final SyncHandler mSyncHandler;
430
Fred Quintana4f9cfc52009-09-02 15:20:23 -0700431 private volatile boolean mBootCompleted = false;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +0100432 private volatile boolean mJobServiceReady = false;
Fred Quintana4f9cfc52009-09-02 15:20:23 -0700433
Fred Quintanaf892fb32009-08-27 21:32:08 -0700434 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 Basarge2b4c8f92016-02-08 23:52:27 +0000444 /**
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 Basargecbf5ae92016-03-08 16:13:06 +0000452 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000453 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 Basarge2b4c8f92016-02-08 23:52:27 +0000464 mJobScheduler.cancel(opy.jobId);
465 }
466 }
467 }
468 }
469 });
470 }
471
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000472 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 Basargecbf5ae92016-03-08 16:13:06 +0000481 mJobSchedulerInternal = LocalServices.getService(JobSchedulerInternal.class);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000482 // Get all persisted syncs from JobScheduler
483 List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000484 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 Basarge8c834c02016-01-07 13:53:16 +0000491 }
492 }
493 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000494 cleanupJobs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000495 }
496
497 private JobScheduler getJobScheduler() {
498 verifyJobScheduler();
499 return mJobScheduler;
500 }
501
Jeff Sharkeye4996bb2012-10-17 14:16:28 -0700502 /**
503 * Should only be created after {@link ContentService#systemReady()} so that
504 * {@link PackageManager} is ready to query.
505 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 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 Quintana0c4d04a2010-11-03 17:02:55 -0700509 mContext = context;
Amith Yamasani9535c912012-10-10 21:48:33 -0700510
Makoto Onukia9dca242017-06-21 17:06:49 -0700511 mLogger = SyncLogger.getInstance();
512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 SyncStorageEngine.init(context);
514 mSyncStorageEngine = SyncStorageEngine.getSingleton();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800515 mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700516 @Override
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700517 public void onSyncRequest(SyncStorageEngine.EndPoint info, int reason, Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000518 scheduleSync(info.account, info.userId, reason, info.provider, extras,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700519 AuthorityInfo.UNDEFINED);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000520 }
521 });
522
523 mSyncStorageEngine.setPeriodicSyncAddedListener(
524 new SyncStorageEngine.PeriodicSyncAddedListener() {
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000525 @Override
526 public void onPeriodicSyncAdded(EndPoint target, Bundle extras, long pollFrequency,
527 long flex) {
528 updateOrAddPeriodicSync(target, pollFrequency, flex, extras);
529 }
530 });
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000531
532 mSyncStorageEngine.setOnAuthorityRemovedListener(new SyncStorageEngine.OnAuthorityRemovedListener() {
533 @Override
534 public void onAuthorityRemoved(EndPoint removedAuthority) {
535 removeSyncsForAuthority(removedAuthority);
Amith Yamasani04e0d262012-02-14 11:50:53 -0800536 }
537 });
538
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700539 mSyncAdapters = new SyncAdaptersCache(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700541 mSyncHandler = new SyncHandler(BackgroundThread.get().getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
Fred Quintana44037e62010-01-21 13:14:49 -0800543 mSyncAdapters.setListener(new RegisteredServicesCacheListener<SyncAdapterType>() {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700544 @Override
545 public void onServiceChanged(SyncAdapterType type, int userId, boolean removed) {
Fred Quintana44037e62010-01-21 13:14:49 -0800546 if (!removed) {
Alon Albert57286f92012-10-09 14:21:38 -0700547 scheduleSync(null, UserHandle.USER_ALL,
548 SyncOperation.REASON_SERVICE_CHANGED,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700549 type.authority, null, AuthorityInfo.UNDEFINED);
Fred Quintana44037e62010-01-21 13:14:49 -0800550 }
551 }
552 }, mSyncHandler);
Fred Quintana718d8a22009-04-29 17:53:20 -0700553
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000554 mRand = new Random(System.currentTimeMillis());
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700555
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
557 context.registerReceiver(mConnectivityIntentReceiver, intentFilter);
558
Fred Quintanae91ebe22009-09-29 20:44:30 -0700559 if (!factoryTest) {
560 intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Dianne Hackbornd83a0962014-05-02 16:28:33 -0700561 intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Fred Quintanae91ebe22009-09-29 20:44:30 -0700562 context.registerReceiver(mBootCompletedReceiver, intentFilter);
563 }
Fred Quintana60307342009-03-24 22:48:12 -0700564
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
566 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
567 context.registerReceiver(mStorageIntentReceiver, intentFilter);
568
Dianne Hackborn55280a92009-05-07 15:53:46 -0700569 intentFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
570 intentFilter.setPriority(100);
571 context.registerReceiver(mShutdownIntentReceiver, intentFilter);
572
Amith Yamasani13593602012-03-22 16:16:17 -0700573 intentFilter = new IntentFilter();
574 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700575 intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -0700576 intentFilter.addAction(Intent.ACTION_USER_STOPPED);
Alon Albert8e285552012-09-17 15:05:27 -0700577 mContext.registerReceiverAsUser(
578 mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null);
Amith Yamasani13593602012-03-22 16:16:17 -0700579
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580 if (!factoryTest) {
581 mNotificationMgr = (NotificationManager)
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000582 context.getSystemService(Context.NOTIFICATION_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 } else {
584 mNotificationMgr = null;
585 }
Fred Quintanab3029c32010-04-06 13:27:12 -0700586 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Amith Yamasani9535c912012-10-10 21:48:33 -0700587 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700588 mAccountManager = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
589 mAccountManagerInternal = LocalServices.getService(AccountManagerInternal.class);
Svet Ganov973edd192016-09-08 20:15:55 -0700590 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700591
Svet Ganovf6d424f12016-09-20 20:18:53 -0700592 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 Hackborna1f1a3c2014-02-24 18:12:28 -0800601 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
602 BatteryStats.SERVICE_NAME));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603
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 Quintanab3029c32010-04-06 13:27:12 -0700608 mHandleAlarmWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 HANDLE_SYNC_ALARM_WAKE_LOCK);
610 mHandleAlarmWakeLock.setReferenceCounted(false);
611
Fred Quintana918339a2010-10-05 14:00:39 -0700612 // 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 Williams8b76d202015-05-03 18:16:25 -0700621 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 Hackborn231cc602009-04-27 17:10:36 -0700647 }
Matthew Williams8b76d202015-05-03 18:16:25 -0700648 }
Fred Quintanae91ebe22009-09-29 20:44:30 -0700649
650 if (!factoryTest) {
Amith Yamasanid648a602012-09-26 15:06:10 -0700651 // Register for account list updates for all users
652 mContext.registerReceiverAsUser(mAccountsUpdatedReceiver,
653 UserHandle.ALL,
654 new IntentFilter(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION),
Matthew Williams5c6756f2014-10-02 04:12:28 +0000655 null, null);
Fred Quintanae91ebe22009-09-29 20:44:30 -0700656 }
Ashish Sharma69d95de2012-04-11 17:27:24 -0700657
Christopher Tate734c91d2016-02-02 12:46:18 -0800658 // 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 Basarge8c834c02016-01-07 13:53:16 +0000663 startServiceIntent.putExtra(SyncJobService.EXTRA_MESSENGER, new Messenger(mSyncHandler));
Christopher Tate734c91d2016-02-02 12:46:18 -0800664 new Handler(mContext.getMainLooper()).post(new Runnable() {
665 @Override
666 public void run() {
667 mContext.startService(startServiceIntent);
668 }
669 });
Svet Ganov65712b02016-09-01 10:24:11 -0700670
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 Onukife224e02017-06-29 14:11:14 -0700678
679 mLogger.log("Sync manager initialized.");
Svet Ganov65712b02016-09-01 10:24:11 -0700680 }
681
Makoto Onukife224e02017-06-29 14:11:14 -0700682 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 Ganov65712b02016-09-01 10:24:11 -0700695 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 Ganovf6d424f12016-09-20 20:18:53 -0700711 AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, service.uid, true);
Svet Ganov65712b02016-09-01 10:24:11 -0700712 }
713 }
714 }
715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800716 }
717
Matthew Williams8b76d202015-05-03 18:16:25 -0700718 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 Project9066cfe2009-03-03 19:31:44 -0800722 /**
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 Hackborn231cc602009-04-27 17:10:36 -0700736 public SyncStorageEngine getSyncStorageEngine() {
737 return mSyncStorageEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
Doug Zongker44f57472009-09-20 15:52:43 -0700739
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700740 private int getIsSyncable(Account account, int userId, String providerName) {
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700741 int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName);
742 UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId);
743
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000744 // If it's not a restricted user, return isSyncable.
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700745 if (userInfo == null || !userInfo.isRestricted()) return isSyncable;
746
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000747 // Else check if the sync adapter has opted-in or not.
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700748 RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
749 mSyncAdapters.getServiceInfo(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000750 SyncAdapterType.newKey(providerName, account.type), userId);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700751 if (syncAdapterInfo == null) return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700752
753 PackageInfo pInfo = null;
754 try {
755 pInfo = AppGlobals.getPackageManager().getPackageInfo(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000756 syncAdapterInfo.componentName.getPackageName(), 0, userId);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700757 if (pInfo == null) return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700758 } catch (RemoteException re) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000759 // Shouldn't happen.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700760 return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700761 }
762 if (pInfo.restrictedAccountType != null
763 && pInfo.restrictedAccountType.equals(account.type)) {
764 return isSyncable;
765 } else {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700766 return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700767 }
768 }
769
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000770 private void setAuthorityPendingState(EndPoint info) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000771 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000772 for (SyncOperation op: ops) {
773 if (!op.isPeriodic && op.target.matchesSpec(info)) {
774 getSyncStorageEngine().markPending(info, true);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700775 return;
776 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700777 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000778 getSyncStorageEngine().markPending(info, false);
Matthew Williamsfa774182013-06-18 15:44:11 -0700779 }
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 Ganovf6d424f12016-09-20 20:18:53 -0700818 * @param targetSyncState Only sync authorities that have the specified sync state.
819 * Use {@link AuthorityInfo#UNDEFINED} to sync all authorities.
Matthew Williamsfa774182013-06-18 15:44:11 -0700820 */
821 public void scheduleSync(Account requestedAccount, int userId, int reason,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100822 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 Basarge8c834c02016-01-07 13:53:16 +0000833 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Matthew Williamsfa774182013-06-18 15:44:11 -0700834 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 Williamsfa774182013-06-18 15:44:11 -0700841
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700842 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 Williamsfa774182013-06-18 15:44:11 -0700854 } else {
Matthew Williamsfa774182013-06-18 15:44:11 -0700855 accounts = mRunningAccounts;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700856 }
857
858 if (ArrayUtils.isEmpty(accounts)) {
859 if (isLoggable) {
860 Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
Matthew Williamsfa774182013-06-18 15:44:11 -0700861 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700862 return;
Matthew Williamsfa774182013-06-18 15:44:11 -0700863 }
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 Basarge8c834c02016-01-07 13:53:16 +0000882 // This isn't strictly server, since arbitrary callers can (and do) request
883 // a non-forced two-way sync on a specific url.
Matthew Williamsfa774182013-06-18 15:44:11 -0700884 source = SyncStorageEngine.SOURCE_SERVER;
885 }
886
887 for (AccountAndUser account : accounts) {
Fyodor Kupolov6fde2982015-01-06 17:46:37 -0800888 // If userId is specified, do not sync accounts of other users
Xiaohui Chen98404fd2015-08-17 16:09:02 -0700889 if (userId >= UserHandle.USER_SYSTEM && account.userId >= UserHandle.USER_SYSTEM
Fyodor Kupolov6fde2982015-01-06 17:46:37 -0800890 && userId != account.userId) {
891 continue;
892 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700893 // 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 Basarge8c834c02016-01-07 13:53:16 +0000901 // If the url was specified then replace the list of authorities
Matthew Williamsfa774182013-06-18 15:44:11 -0700902 // with just this authority or clear it if this authority isn't
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000903 // syncable.
Matthew Williamsfa774182013-06-18 15:44:11 -0700904 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 Ganov5cb29732016-07-11 19:32:30 -0700911 int isSyncable = computeSyncable(account.account, account.userId, authority);
912
Matthew Williams53abfdb2015-06-10 20:06:37 -0700913 if (isSyncable == AuthorityInfo.NOT_SYNCABLE) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700914 continue;
915 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700916
917 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
918 mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(authority,
919 account.account.type), account.userId);
Matthew Williamsfa774182013-06-18 15:44:11 -0700920 if (syncAdapterInfo == null) {
921 continue;
922 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700923
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700924 final int owningUid = syncAdapterInfo.uid;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700925
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 Hackbornbef28fe2015-10-29 17:57:11 -0700930 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700931 Bundle finalExtras = new Bundle(extras);
Svet Ganov973edd192016-09-08 20:15:55 -0700932 String packageName = syncAdapterInfo.componentName.getPackageName();
933 // If the app did not run and has no account access, done
Amith Yamasani2cbfa1e2017-03-28 10:34:01 -0700934 try {
935 if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
936 continue;
937 }
938 } catch (IllegalArgumentException e) {
939 // Package not found, race with an uninstall
Svet Ganov973edd192016-09-08 20:15:55 -0700940 continue;
941 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700942 mAccountManagerInternal.requestAccountAccess(account.account,
Svet Ganov973edd192016-09-08 20:15:55 -0700943 packageName, userId,
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700944 new RemoteCallback((Bundle result) -> {
945 if (result != null
946 && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
947 scheduleSync(account.account, userId, reason, authority,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100948 finalExtras, targetSyncState, minDelayMillis);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700949 }
950 }
951 ));
952 continue;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700953 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700954
Matthew Williamsfa774182013-06-18 15:44:11 -0700955 final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs();
956 final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable();
957 if (isSyncable < 0 && isAlwaysSyncable) {
Matthew Williams53abfdb2015-06-10 20:06:37 -0700958 mSyncStorageEngine.setIsSyncable(
959 account.account, account.userId, authority, AuthorityInfo.SYNCABLE);
960 isSyncable = AuthorityInfo.SYNCABLE;
Matthew Williamsfa774182013-06-18 15:44:11 -0700961 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700962
Svet Ganovf6d424f12016-09-20 20:18:53 -0700963 if (targetSyncState != AuthorityInfo.UNDEFINED && targetSyncState != isSyncable) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700964 continue;
965 }
Svet Ganovf6d424f12016-09-20 20:18:53 -0700966
Matthew Williamsfa774182013-06-18 15:44:11 -0700967 if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) {
968 continue;
969 }
970
Matthew Williamsfa774182013-06-18 15:44:11 -0700971 boolean syncAllowed =
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000972 (isSyncable < 0) // Always allow if the isSyncable state is unknown.
973 || ignoreSettings
974 || (mSyncStorageEngine.getMasterSyncAutomatically(account.userId)
Matthew Williamsfa774182013-06-18 15:44:11 -0700975 && mSyncStorageEngine.getSyncAutomatically(account.account,
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000976 account.userId, authority));
Matthew Williamsfa774182013-06-18 15:44:11 -0700977 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 Williams56dbf8f2013-07-26 12:56:39 -0700984 SyncStorageEngine.EndPoint info =
985 new SyncStorageEngine.EndPoint(
986 account.account, authority, account.userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700987 long delayUntil =
988 mSyncStorageEngine.getDelayUntilTime(info);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700989
990 final String owningPackage = syncAdapterInfo.componentName.getPackageName();
991
Svet Ganovf6d424f12016-09-20 20:18:53 -0700992 if (isSyncable == AuthorityInfo.NOT_INITIALIZED) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700993 // Initialisation sync.
994 Bundle newExtras = new Bundle();
995 newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
996 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000997 Slog.v(TAG, "schedule initialisation Sync:"
Matthew Williamsfa774182013-06-18 15:44:11 -0700998 + ", delay until " + delayUntil
999 + ", run by " + 0
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001000 + ", flexMillis " + 0
Matthew Williamsfa774182013-06-18 15:44:11 -07001001 + ", source " + source
1002 + ", account " + account
1003 + ", authority " + authority
1004 + ", extras " + newExtras);
1005 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001006 postScheduleSyncMessage(
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001007 new SyncOperation(account.account, account.userId,
1008 owningUid, owningPackage, reason, source,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001009 authority, newExtras, allowParallelSyncs),
1010 minDelayMillis
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001011 );
Svet Ganovf6d424f12016-09-20 20:18:53 -07001012 } else if (targetSyncState == AuthorityInfo.UNDEFINED
1013 || targetSyncState == isSyncable) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001014 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001015 Slog.v(TAG, "scheduleSync:"
Matthew Williamsfa774182013-06-18 15:44:11 -07001016 + " delay until " + delayUntil
Matthew Williamsfa774182013-06-18 15:44:11 -07001017 + ", source " + source
1018 + ", account " + account
1019 + ", authority " + authority
1020 + ", extras " + extras);
1021 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001022 postScheduleSyncMessage(
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001023 new SyncOperation(account.account, account.userId,
1024 owningUid, owningPackage, reason, source,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001025 authority, extras, allowParallelSyncs),
1026 minDelayMillis
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001027 );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 }
1029 }
1030 }
1031 }
1032
Svet Ganov96b9c752016-10-17 19:29:58 -07001033 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 Ganov5cb29732016-07-11 19:32:30 -07001039 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 Hackbornc3af19a2017-01-20 17:00:44 -08001052 if (ActivityManager.getService().isAppStartModeDisabled(owningUid, owningPackage)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001053 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 Ganov96b9c752016-10-17 19:29:58 -07001061 if (checkAccountAccess && !canAccessAccount(account, owningPackage, owningUid)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001062 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 Basarge8c834c02016-01-07 13:53:16 +00001087 private void removeSyncsForAuthority(EndPoint info) {
1088 verifyJobScheduler();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001089 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001090 for (SyncOperation op: ops) {
1091 if (op.target.matchesSpec(info)) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001092 getJobScheduler().cancel(op.jobId);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001093 }
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 Reddyae0bcdd2015-12-23 18:24:23 +00001111 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001112 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 Basargecbf5ae92016-03-08 16:13:06 +00001122 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001123 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 Williamsfa774182013-06-18 15:44:11 -07001135 /**
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001136 * Schedule sync based on local changes to a provider. We wait for at least LOCAL_SYNC_DELAY
1137 * ms to batch syncs.
Matthew Williamsfa774182013-06-18 15:44:11 -07001138 */
Alon Albert57286f92012-10-09 14:21:38 -07001139 public void scheduleLocalSync(Account account, int userId, int reason, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 final Bundle extras = new Bundle();
1141 extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
Matthew Williamsfa774182013-06-18 15:44:11 -07001142 scheduleSync(account, userId, reason, authority, extras,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001143 AuthorityInfo.UNDEFINED, LOCAL_SYNC_DELAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 }
1145
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001146 public SyncAdapterType[] getSyncAdapterTypes(int userId) {
1147 final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos;
1148 serviceInfos = mSyncAdapters.getAllServices(userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07001149 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 Project9066cfe2009-03-03 19:31:44 -08001156 }
1157
Amith Yamasani37a40c22015-06-17 13:25:42 -07001158 public String[] getSyncAdapterPackagesForAuthorityAsUser(String authority, int userId) {
1159 return mSyncAdapters.getSyncAdapterPackagesForAuthority(authority, userId);
1160 }
1161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 private void sendSyncFinishedOrCanceledMessage(ActiveSyncContext syncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001163 SyncResult syncResult) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001164 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_SYNC_FINISHED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 Message msg = mSyncHandler.obtainMessage();
1166 msg.what = SyncHandler.MESSAGE_SYNC_FINISHED;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001167 msg.obj = new SyncFinishedOrCancelledMessagePayload(syncContext, syncResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 mSyncHandler.sendMessage(msg);
1169 }
1170
Makoto Onukia9dca242017-06-21 17:06:49 -07001171 private void sendCancelSyncsMessage(final SyncStorageEngine.EndPoint info, Bundle extras,
1172 String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001173 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_CANCEL");
Makoto Onukia9dca242017-06-21 17:06:49 -07001174
1175 mLogger.log("sendCancelSyncsMessage() ep=", info, " why=", why);
1176
Fred Quintana918339a2010-10-05 14:00:39 -07001177 Message msg = mSyncHandler.obtainMessage();
1178 msg.what = SyncHandler.MESSAGE_CANCEL;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001179 msg.setData(extras);
1180 msg.obj = info;
Fred Quintana918339a2010-10-05 14:00:39 -07001181 mSyncHandler.sendMessage(msg);
1182 }
1183
Matthew Williams92a1c092014-08-25 19:18:32 -07001184 /**
Matthew Williams1967c8d2015-06-19 19:03:13 -07001185 * 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 Williams92a1c092014-08-25 19:18:32 -07001187 */
Matthew Williams1967c8d2015-06-19 19:03:13 -07001188 private void postMonitorSyncProgressMessage(ActiveSyncContext activeSyncContext) {
Matthew Williams92a1c092014-08-25 19:18:32 -07001189 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001190 Slog.v(TAG, "posting MESSAGE_SYNC_MONITOR in " +
Matthew Williams1967c8d2015-06-19 19:03:13 -07001191 (SYNC_MONITOR_WINDOW_LENGTH_MILLIS/1000) + "s");
Matthew Williams92a1c092014-08-25 19:18:32 -07001192 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07001193
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 Williams92a1c092014-08-25 19:18:32 -07001202 }
1203
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001204 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 Basarge8c834c02016-01-07 13:53:16 +00001208 }
1209
Matthew Williams1967c8d2015-06-19 19:03:13 -07001210 /**
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 Basarge8c834c02016-01-07 13:53:16 +00001221 private class SyncFinishedOrCancelledMessagePayload {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 public final ActiveSyncContext activeSyncContext;
1223 public final SyncResult syncResult;
1224
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001225 SyncFinishedOrCancelledMessagePayload(ActiveSyncContext syncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001226 SyncResult syncResult) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 this.activeSyncContext = syncContext;
1228 this.syncResult = syncResult;
1229 }
1230 }
1231
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001232 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 Reddyae0bcdd2015-12-23 18:24:23 +00001239 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001240 this.target = target;
1241 this.pollFrequency = pollFrequency;
1242 this.flex = flex;
1243 this.extras = extras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 }
1245 }
1246
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001247 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 Onukia9dca242017-06-21 17:06:49 -07001257 private void clearBackoffSetting(EndPoint target, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001258 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 Williams56dbf8f2013-07-26 12:56:39 -07001267 SyncStorageEngine.NOT_IN_BACKOFF_MODE,
1268 SyncStorageEngine.NOT_IN_BACKOFF_MODE);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001269
Makoto Onukia9dca242017-06-21 17:06:49 -07001270 rescheduleSyncs(target, why);
Alon Albert6e079a32010-11-12 12:41:09 -08001271 }
1272
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001273 private void increaseBackoffSetting(EndPoint target) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001274 final long now = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275
Fred Quintana307da1a2010-01-21 14:24:20 -08001276 final Pair<Long, Long> previousSettings =
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001277 mSyncStorageEngine.getBackoff(target);
Alon Albertaeeb6202010-12-09 16:14:02 -08001278 long newDelayInMs = -1;
1279 if (previousSettings != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001280 // Don't increase backoff before current backoff is expired. This will happen for op's
Alon Albertaeeb6202010-12-09 16:14:02 -08001281 // with ignoreBackoff set.
1282 if (now < previousSettings.first) {
1283 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001284 Slog.v(TAG, "Still in backoff, do not increase it. "
1285 + "Remaining: " + ((previousSettings.first - now) / 1000) + " seconds.");
Alon Albertaeeb6202010-12-09 16:14:02 -08001286 }
1287 return;
1288 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001289 // Subsequent delays are the double of the previous delay.
Alon Albertaeeb6202010-12-09 16:14:02 -08001290 newDelayInMs = previousSettings.second * 2;
1291 }
1292 if (newDelayInMs <= 0) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001293 // The initial delay is the jitterized INITIAL_SYNC_RETRY_TIME_IN_MS.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 newDelayInMs = jitterize(INITIAL_SYNC_RETRY_TIME_IN_MS,
1295 (long)(INITIAL_SYNC_RETRY_TIME_IN_MS * 1.1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 }
1297
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001298 // Cap the delay.
Jeff Sharkey625239a2012-09-26 22:03:49 -07001299 long maxSyncRetryTimeInSeconds = Settings.Global.getLong(mContext.getContentResolver(),
1300 Settings.Global.SYNC_MAX_RETRY_DELAY_IN_SECONDS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS);
1302 if (newDelayInMs > maxSyncRetryTimeInSeconds * 1000) {
1303 newDelayInMs = maxSyncRetryTimeInSeconds * 1000;
1304 }
1305
Alon Albertc1ac7762010-10-28 13:35:55 -07001306 final long backoff = now + newDelayInMs;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001307 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1308 Slog.v(TAG, "Backoff until: " + backoff + ", delayTime: " + newDelayInMs);
1309 }
1310 mSyncStorageEngine.setBackoff(target, backoff, newDelayInMs);
Makoto Onukia9dca242017-06-21 17:06:49 -07001311 rescheduleSyncs(target, "increaseBackoffSetting");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001312 }
Alon Albertc1ac7762010-10-28 13:35:55 -07001313
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001314 /**
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 Onukia9dca242017-06-21 17:06:49 -07001318 private void rescheduleSyncs(EndPoint target, String why) {
1319 mLogger.log("rescheduleSyncs() ep=", target, " why=", why);
1320
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001321 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001322 int count = 0;
1323 for (SyncOperation op: ops) {
1324 if (!op.isPeriodic && op.target.matchesSpec(target)) {
1325 count++;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001326 getJobScheduler().cancel(op.jobId);
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001327 postScheduleSyncMessage(op, 0 /* min delay */);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001328 }
1329 }
1330 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1331 Slog.v(TAG, "Rescheduled " + count + " syncs for " + target);
Fred Quintana918339a2010-10-05 14:00:39 -07001332 }
Fred Quintana307da1a2010-01-21 14:24:20 -08001333 }
1334
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001335 private void setDelayUntilTime(EndPoint target, long delayUntilSeconds) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001336 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 Basarge8c834c02016-01-07 13:53:16 +00001344 mSyncStorageEngine.setDelayUntilTime(target, newDelayUntilTime);
1345 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1346 Slog.v(TAG, "Delay Until time set to " + newDelayUntilTime + " for " + target);
Fred Quintana918339a2010-10-05 14:00:39 -07001347 }
Makoto Onukia9dca242017-06-21 17:06:49 -07001348 rescheduleSyncs(target, "delayUntil newDelayUntilTime: " + newDelayUntilTime);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001349 }
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 Project9066cfe2009-03-03 19:31:44 -08001362 }
1363
1364 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001365 * Cancel the active sync if it matches the target.
1366 * @param info object containing info about which syncs to cancel. The target can
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001367 * 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 Project9066cfe2009-03-03 19:31:44 -08001369 */
Makoto Onukia9dca242017-06-21 17:06:49 -07001370 public void cancelActiveSync(SyncStorageEngine.EndPoint info, Bundle extras, String why) {
1371 sendCancelSyncsMessage(info, extras, why);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372 }
1373
1374 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001375 * Schedule a sync operation with JobScheduler.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001377 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 Project9066cfe2009-03-03 19:31:44 -08001404 }
1405
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001406 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 Basarge2b4c8f92016-02-08 23:52:27 +00001411 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 Basarge8c834c02016-01-07 13:53:16 +00001423 int duplicatesCount = 0;
1424 long now = SystemClock.elapsedRealtime();
1425 syncOperation.expectedRuntime = now + minDelay;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001426 List<SyncOperation> pending = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001427 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 Project9066cfe2009-03-03 19:31:44 -08001438 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001439 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 Basarge8c834c02016-01-07 13:53:16 +00001451 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 Project9066cfe2009-03-03 19:31:44 -08001461 }
1462 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001463
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001464 // 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 Basarge8c834c02016-01-07 13:53:16 +00001468
1469 if (isLoggable) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001470 Slog.v(TAG, "scheduling sync operation " + syncOperation.toString());
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001471 }
1472
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001473 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 Reddyae0bcdd2015-12-23 18:24:23 +00001479 new ComponentName(mContext, SyncJobService.class))
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001480 .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 Basargeeda34e42016-04-26 00:14:02 +01001499 syncOperation.target.userId, syncOperation.wakeLockName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001500 }
1501
1502 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07001503 * Remove scheduled sync operations.
Matthew Williams8ef22042013-07-26 12:56:39 -07001504 * @param info limit the removals to operations that match this target. The target can
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001505 * have null account/provider info to specify all accounts/providers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001507 public void clearScheduledSyncOperations(SyncStorageEngine.EndPoint info) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001508 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001509 for (SyncOperation op: ops) {
1510 if (!op.isPeriodic && op.target.matchesSpec(info)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001511 getJobScheduler().cancel(op.jobId);
1512 getSyncStorageEngine().markPending(op.target, false);
1513 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001515 mSyncStorageEngine.setBackoff(info,
Fred Quintana918339a2010-10-05 14:00:39 -07001516 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 }
1518
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001519 /**
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 Basargecbf5ae92016-03-08 16:13:06 +00001525 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001526 for (SyncOperation op: ops) {
1527 if (!op.isPeriodic && op.target.matchesSpec(info)
1528 && syncExtrasEquals(extras, op.extras, false)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001529 getJobScheduler().cancel(op.jobId);
1530 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001531 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001532 setAuthorityPendingState(info);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001533 // 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 Basarge8c834c02016-01-07 13:53:16 +00001540 private void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) {
1541 final boolean isLoggable = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 if (isLoggable) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001543 Log.d(TAG, "encountered error(s) during the sync: " + syncResult + ", " + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
Fred Quintana53bd2522010-02-05 15:28:12 -08001546 // 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 Basargebd4c3ea2016-06-16 11:54:35 +01001553 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 Williams56dbf8f2013-07-26 12:56:39 -07001558 if (isLoggable) {
1559 Log.d(TAG, "not retrying sync operation because SYNC_EXTRAS_DO_NOT_RETRY was specified "
1560 + operation);
1561 }
Fred Quintana918339a2010-10-05 14:00:39 -07001562 } else if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false)
1563 && !syncResult.syncAlreadyInProgress) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001564 // If this was an upward sync then schedule a two-way sync immediately.
Fred Quintana53bd2522010-02-05 15:28:12 -08001565 operation.extras.remove(ContentResolver.SYNC_EXTRAS_UPLOAD);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001566 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 Basarge2b4c8f92016-02-08 23:52:27 +00001570 scheduleSyncOperationH(operation);
Fred Quintana307da1a2010-01-21 14:24:20 -08001571 } else if (syncResult.tooManyRetries) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001572 // 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 Quintanaaa7edda2009-12-03 14:18:58 -08001578 } else if (syncResult.madeSomeProgress()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001579 // If the operation succeeded to some extent then retry immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001580 if (isLoggable) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001581 Log.d(TAG, "retrying sync operation because even though it had an error "
1582 + "it achieved some success");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001583 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001584 scheduleSyncOperationH(operation);
Fred Quintana8570f742010-02-18 10:32:54 -08001585 } 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 Basarge8c834c02016-01-07 13:53:16 +00001590 scheduleSyncOperationH(operation, DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 } else if (syncResult.hasSoftError()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001592 // If this was a two-way sync then retry soft errors with an exponential backoff.
Fred Quintana307da1a2010-01-21 14:24:20 -08001593 if (isLoggable) {
1594 Log.d(TAG, "retrying sync operation because it encountered a soft error: "
1595 + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001597 scheduleSyncOperationH(operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598 } else {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001599 // Otherwise do not reschedule.
Fred Quintana307da1a2010-01-21 14:24:20 -08001600 Log.d(TAG, "not retrying sync operation because the error is a hard error: "
1601 + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
1603 }
1604
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07001605 private void onUserUnlocked(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001606 // Make sure that accounts we're about to use are valid.
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001607 AccountManagerService.getSingleton().validateAccounts(userId);
1608
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001609 mSyncAdapters.invalidateCache(userId);
1610
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001611 EndPoint target = new EndPoint(null, null, userId);
1612 updateRunningAccounts(target);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001613
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001614 // Schedule sync for any accounts under started user.
Svetoslavf3f02ac2015-09-08 14:36:35 -07001615 final Account[] accounts = AccountManagerService.getSingleton().getAccounts(userId,
1616 mContext.getOpPackageName());
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001617 for (Account account : accounts) {
Alon Albert57286f92012-10-09 14:21:38 -07001618 scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null,
Svet Ganovf6d424f12016-09-20 20:18:53 -07001619 AuthorityInfo.NOT_INITIALIZED);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001620 }
Alon Albert8e285552012-09-17 15:05:27 -07001621 }
Amith Yamasani13593602012-03-22 16:16:17 -07001622
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -07001623 private void onUserStopped(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001624 updateRunningAccounts(null /* Don't sync any target */);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001625
1626 cancelActiveSync(
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001627 new SyncStorageEngine.EndPoint(
1628 null /* any account */,
1629 null /* any authority */,
1630 userId),
Makoto Onukia9dca242017-06-21 17:06:49 -07001631 null /* any sync. */,
1632 "onUserStopped"
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001633 );
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001634 }
1635
1636 private void onUserRemoved(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001637 updateRunningAccounts(null /* Don't sync any target */);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001638
Amith Yamasani13593602012-03-22 16:16:17 -07001639 // Clean up the storage engine database
1640 mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId);
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001641 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001642 for (SyncOperation op: ops) {
1643 if (op.target.userId == userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001644 getJobScheduler().cancel(op.jobId);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001645 }
1646 }
1647 }
1648
Amith Yamasani96a0fd652015-04-10 16:16:30 -07001649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001650 * @hide
1651 */
Alon Alberteca75112010-12-08 15:02:33 -08001652 class ActiveSyncContext extends ISyncContext.Stub
1653 implements ServiceConnection, IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 final SyncOperation mSyncOperation;
1655 final long mHistoryRowId;
Fred Quintana718d8a22009-04-29 17:53:20 -07001656 ISyncAdapter mSyncAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 final long mStartTime;
1658 long mTimeoutStartTime;
Fred Quintana3ec47302010-03-10 10:08:31 -08001659 boolean mBound;
Fred Quintana918339a2010-10-05 14:00:39 -07001660 final PowerManager.WakeLock mSyncWakeLock;
1661 final int mSyncAdapterUid;
1662 SyncInfo mSyncInfo;
Alon Alberteca75112010-12-08 15:02:33 -08001663 boolean mIsLinkedToDeath = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001664 String mEventName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665
Matthew Williams1967c8d2015-06-19 19:03:13 -07001666 /** 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 Quintana918339a2010-10-05 14:00:39 -07001674 /**
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 Reddyae0bcdd2015-12-23 18:24:23 +00001685 int syncAdapterUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001686 super();
Fred Quintana918339a2010-10-05 14:00:39 -07001687 mSyncAdapterUid = syncAdapterUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 mSyncOperation = syncOperation;
1689 mHistoryRowId = historyRowId;
Fred Quintana718d8a22009-04-29 17:53:20 -07001690 mSyncAdapter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001691 mStartTime = SystemClock.elapsedRealtime();
1692 mTimeoutStartTime = mStartTime;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001693 mSyncWakeLock = mSyncHandler.getSyncWakeLock(mSyncOperation);
Fred Quintana918339a2010-10-05 14:00:39 -07001694 mSyncWakeLock.setWorkSource(new WorkSource(syncAdapterUid));
1695 mSyncWakeLock.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001696 }
1697
1698 public void sendHeartbeat() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001699 // Heartbeats are no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700 }
1701
1702 public void onFinished(SyncResult result) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001703 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 Project9066cfe2009-03-03 19:31:44 -08001705 // ActiveSyncContext is no longer the mActiveSyncContext at message handling
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001706 // time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 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 Quintana718d8a22009-04-29 17:53:20 -07001717 public void onServiceConnected(ComponentName name, IBinder service) {
1718 Message msg = mSyncHandler.obtainMessage();
1719 msg.what = SyncHandler.MESSAGE_SERVICE_CONNECTED;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001720 msg.obj = new ServiceConnectionData(this, service);
Fred Quintana718d8a22009-04-29 17:53:20 -07001721 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 Williams56dbf8f2013-07-26 12:56:39 -07001731 boolean bindToSyncAdapter(ComponentName serviceComponent, int userId) {
Fred Quintana718d8a22009-04-29 17:53:20 -07001732 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001733 Log.d(TAG, "bindToSyncAdapter: " + serviceComponent + ", connection " + this);
Fred Quintana718d8a22009-04-29 17:53:20 -07001734 }
1735 Intent intent = new Intent();
1736 intent.setAction("android.content.SyncAdapter");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001737 intent.setComponent(serviceComponent);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001738 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1739 com.android.internal.R.string.sync_binding_label);
Dianne Hackborn41203752012-08-31 14:05:51 -07001740 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
1741 mContext, 0, new Intent(Settings.ACTION_SYNC_SETTINGS), 0,
1742 null, new UserHandle(userId)));
Fred Quintana3ec47302010-03-10 10:08:31 -08001743 mBound = true;
Amith Yamasani27b89e62013-01-16 12:30:11 -08001744 final boolean bindResult = mContext.bindServiceAsUser(intent, this,
Dianne Hackborne02c88a2011-10-28 13:58:15 -07001745 Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001746 | Context.BIND_ALLOW_OOM_MANAGEMENT,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001747 new UserHandle(mSyncOperation.target.userId));
Fred Quintana3ec47302010-03-10 10:08:31 -08001748 if (!bindResult) {
1749 mBound = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001750 } else {
1751 try {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001752 mEventName = mSyncOperation.wakeLockName();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001753 mBatteryStats.noteSyncStart(mEventName, mSyncAdapterUid);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001754 } catch (RemoteException e) {
1755 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001756 }
1757 return bindResult;
Fred Quintana718d8a22009-04-29 17:53:20 -07001758 }
1759
Fred Quintana918339a2010-10-05 14:00:39 -07001760 /**
1761 * Performs the required cleanup, which is the releasing of the wakelock and
1762 * unbinding from the sync adapter (if actually bound).
1763 */
Fred Quintana3ec47302010-03-10 10:08:31 -08001764 protected void close() {
Fred Quintana718d8a22009-04-29 17:53:20 -07001765 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1766 Log.d(TAG, "unBindFromSyncAdapter: connection " + this);
1767 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001768 if (mBound) {
1769 mBound = false;
1770 mContext.unbindService(this);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001771 try {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001772 mBatteryStats.noteSyncFinish(mEventName, mSyncAdapterUid);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001773 } catch (RemoteException e) {
1774 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001775 }
Fred Quintana918339a2010-10-05 14:00:39 -07001776 mSyncWakeLock.release();
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001777 mSyncWakeLock.setWorkSource(null);
Fred Quintana718d8a22009-04-29 17:53:20 -07001778 }
1779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 public String toString() {
1781 StringBuilder sb = new StringBuilder();
1782 toString(sb);
1783 return sb.toString();
1784 }
Alon Alberteca75112010-12-08 15:02:33 -08001785
1786 @Override
1787 public void binderDied() {
1788 sendSyncFinishedOrCanceledMessage(this, null);
1789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 }
1791
Makoto Onukia9dca242017-06-21 17:06:49 -07001792 protected void dump(FileDescriptor fd, PrintWriter pw, boolean dumpAll) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001793 final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
1794 dumpSyncState(ipw);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001795 dumpSyncAdapters(ipw);
Makoto Onukia9dca242017-06-21 17:06:49 -07001796
1797 if (dumpAll) {
1798 ipw.println("Detailed Sync History");
1799 mLogger.dumpAll(pw);
1800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 }
1802
Dianne Hackborn231cc602009-04-27 17:10:36 -07001803 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 Zongker44f57472009-09-20 15:52:43 -07001808
Makoto Onuki15e7a252017-06-08 17:12:05 -07001809 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 Basarge8c834c02016-01-07 13:53:16 +00001854 protected void dumpPendingSyncs(PrintWriter pw) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001855 List<SyncOperation> pendingSyncs = getAllPendingSyncs();
Makoto Onuki15e7a252017-06-08 17:12:05 -07001856
1857 pw.print("Pending Syncs: ");
1858 pw.println(countIf(pendingSyncs, op -> !op.isPeriodic));
1859
1860 Collections.sort(pendingSyncs, sOpRuntimeComparator);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001861 int count = 0;
1862 for (SyncOperation op: pendingSyncs) {
1863 if (!op.isPeriodic) {
1864 pw.println(op.dump(null, false));
1865 count++;
1866 }
1867 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001868 pw.println();
1869 }
1870
1871 protected void dumpPeriodicSyncs(PrintWriter pw) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001872 List<SyncOperation> pendingSyncs = getAllPendingSyncs();
Makoto Onuki15e7a252017-06-08 17:12:05 -07001873
1874 pw.print("Periodic Syncs: ");
1875 pw.println(countIf(pendingSyncs, op -> op.isPeriodic));
1876
1877 Collections.sort(pendingSyncs, sOpDumpComparator);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001878 int count = 0;
1879 for (SyncOperation op: pendingSyncs) {
1880 if (op.isPeriodic) {
1881 pw.println(op.dump(null, false));
1882 count++;
1883 }
1884 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001885 pw.println();
1886 }
1887
Makoto Onuki15e7a252017-06-08 17:12:05 -07001888 /**
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 Alberte0bde332011-09-22 14:26:16 -07001938 protected void dumpSyncState(PrintWriter pw) {
Makoto Onuki15e7a252017-06-08 17:12:05 -07001939 final StringBuilder sb = new StringBuilder();
1940
Dianne Hackborn231cc602009-04-27 17:10:36 -07001941 pw.print("data connected: "); pw.println(mDataConnectionIsConnected);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001942 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 Sharkey6ab72d72012-10-08 16:44:37 -07001947 + mSyncStorageEngine.getMasterSyncAutomatically(user.id) + " ");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001948 }
1949 pw.println();
1950 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001951 pw.print("memory low: "); pw.println(mStorageIsLow);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001952 pw.print("device idle: "); pw.println(mDeviceIsIdle);
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001953 pw.print("reported active: "); pw.println(mReportedSyncActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001955 final AccountAndUser[] accounts = AccountManagerService.getSingleton().getAllAccounts();
Amith Yamasani04e0d262012-02-14 11:50:53 -08001956
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001957 pw.print("accounts: ");
Fred Quintana53bd2522010-02-05 15:28:12 -08001958 if (accounts != INITIAL_ACCOUNTS_ARRAY) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001959 pw.println(accounts.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 } else {
Fred Quintana53bd2522010-02-05 15:28:12 -08001961 pw.println("not known yet");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 final long now = SystemClock.elapsedRealtime();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001964 pw.print("now: "); pw.print(now);
1965 pw.println(" (" + formatTime(System.currentTimeMillis()) + ")");
Makoto Onuki15e7a252017-06-08 17:12:05 -07001966
1967 sb.setLength(0);
1968 pw.print("uptime: "); pw.print(formatDurationHMS(sb, now));
1969 pw.println();
Dianne Hackborn231cc602009-04-27 17:10:36 -07001970 pw.print("time spent syncing: ");
Makoto Onuki15e7a252017-06-08 17:12:05 -07001971
1972 sb.setLength(0);
1973 pw.print(formatDurationHMS(sb,
1974 mSyncHandler.mSyncTimeTracker.timeSpentSyncing()));
1975 pw.print(", sync ");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001976 pw.print(mSyncHandler.mSyncTimeTracker.mLastWasSyncing ? "" : "not ");
1977 pw.println("in progress");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978
Fred Quintana918339a2010-10-05 14:00:39 -07001979 pw.println();
1980 pw.println("Active Syncs: " + mActiveSyncContexts.size());
Alon Albert57286f92012-10-09 14:21:38 -07001981 final PackageManager pm = mContext.getPackageManager();
Fred Quintana918339a2010-10-05 14:00:39 -07001982 for (SyncManager.ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
Makoto Onuki15e7a252017-06-08 17:12:05 -07001983 final long durationInSeconds = (now - activeSyncContext.mStartTime);
Fred Quintana918339a2010-10-05 14:00:39 -07001984 pw.print(" ");
Makoto Onuki15e7a252017-06-08 17:12:05 -07001985 sb.setLength(0);
1986 pw.print(formatDurationHMS(sb, durationInSeconds));
Fred Quintana918339a2010-10-05 14:00:39 -07001987 pw.print(" - ");
Alon Albert57286f92012-10-09 14:21:38 -07001988 pw.print(activeSyncContext.mSyncOperation.dump(pm, false));
Fred Quintana918339a2010-10-05 14:00:39 -07001989 pw.println();
1990 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07001991 pw.println();
1992
1993 dumpPendingSyncs(pw);
1994 dumpPeriodicSyncs(pw);
Fred Quintana918339a2010-10-05 14:00:39 -07001995
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001996 // Join the installed sync adapter with the accounts list and emit for everything.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001997 pw.println("Sync Status");
Makoto Onuki15e7a252017-06-08 17:12:05 -07001998
1999 final ArrayList<Pair<EndPoint, SyncStatusInfo>> statuses = new ArrayList<>();
2000
Amith Yamasani04e0d262012-02-14 11:50:53 -08002001 for (AccountAndUser account : accounts) {
Alon Albert57286f92012-10-09 14:21:38 -07002002 pw.printf("Account %s u%d %s\n",
2003 account.account.name, account.userId, account.account.type);
2004
2005 pw.println("=======================================================================");
Makoto Onuki010291d2017-06-06 16:32:47 -07002006 final PrintTable table = new PrintTable(13);
Alon Albert57286f92012-10-09 14:21:38 -07002007 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 Onuki010291d2017-06-06 16:32:47 -07002019 "Last Sync", // 11
Makoto Onuki15e7a252017-06-08 17:12:05 -07002020 "Backoff" // 12
Alon Albert57286f92012-10-09 14:21:38 -07002021 );
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 Basarge8c834c02016-01-07 13:53:16 +00002028 @Override
2029 public int compare(RegisteredServicesCache.ServiceInfo<SyncAdapterType> lhs,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002030 RegisteredServicesCache.ServiceInfo<SyncAdapterType> rhs) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002031 return lhs.type.authority.compareTo(rhs.type.authority);
2032 }
2033 });
Alon Albert57286f92012-10-09 14:21:38 -07002034 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterType : sorted) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08002035 if (!syncAdapterType.type.accountType.equals(account.account.type)) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002036 continue;
2037 }
Alon Albert57286f92012-10-09 14:21:38 -07002038 int row = table.getNumRows();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002039 Pair<AuthorityInfo, SyncStatusInfo> syncAuthoritySyncStatus =
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07002040 mSyncStorageEngine.getCopyOfAuthorityWithSyncStatus(
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002041 new SyncStorageEngine.EndPoint(
2042 account.account,
2043 syncAdapterType.type.authority,
2044 account.userId));
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07002045 SyncStorageEngine.AuthorityInfo settings = syncAuthoritySyncStatus.first;
2046 SyncStatusInfo status = syncAuthoritySyncStatus.second;
Makoto Onuki15e7a252017-06-08 17:12:05 -07002047 statuses.add(Pair.create(settings.target, status));
Matthew Williams8ef22042013-07-26 12:56:39 -07002048 String authority = settings.target.provider;
Alon Albert57286f92012-10-09 14:21:38 -07002049 if (authority.length() > 50) {
2050 authority = authority.substring(authority.length() - 50);
2051 }
2052 table.set(row, 0, authority, settings.syncable, settings.enabled);
Makoto Onuki15e7a252017-06-08 17:12:05 -07002053
2054 sb.setLength(0);
Alon Albert57286f92012-10-09 14:21:38 -07002055 table.set(row, 4,
2056 status.numSourceLocal,
2057 status.numSourcePoll,
2058 status.numSourcePeriodic,
2059 status.numSourceServer,
2060 status.numSourceUser,
2061 status.numSyncs,
Makoto Onuki15e7a252017-06-08 17:12:05 -07002062 formatDurationHMS(sb, status.totalElapsedTime));
Alon Albert57286f92012-10-09 14:21:38 -07002063
Alon Albert57286f92012-10-09 14:21:38 -07002064 int row1 = row;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002065 if (settings.delayUntil > now) {
Alon Albert57286f92012-10-09 14:21:38 -07002066 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 Quintanac5d1c6d2010-01-27 12:17:49 -08002071 }
Alon Albert57286f92012-10-09 14:21:38 -07002072
Makoto Onuki010291d2017-06-06 16:32:47 -07002073 row1 = row;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002074 if (status.lastSuccessTime != 0) {
Alon Albert57286f92012-10-09 14:21:38 -07002075 table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastSuccessSource]
2076 + " " + "SUCCESS");
2077 table.set(row1++, 11, formatTime(status.lastSuccessTime));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002078 }
2079 if (status.lastFailureTime != 0) {
Alon Albert57286f92012-10-09 14:21:38 -07002080 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 Hackborn231cc602009-04-27 17:10:36 -07002085 }
2086 }
Alon Albert57286f92012-10-09 14:21:38 -07002087 table.writeTo(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07002089
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 Project9066cfe2009-03-03 19:31:44 -08002117 }
2118
Dianne Hackborn231cc602009-04-27 17:10:36 -07002119 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 Zongker44f57472009-09-20 15:52:43 -07002123
Dianne Hackborn231cc602009-04-27 17:10:36 -07002124 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 Zongker44f57472009-09-20 15:52:43 -07002137
Alon Alberte0bde332011-09-22 14:26:16 -07002138 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 Williams56dbf8f2013-07-26 12:56:39 -07002155 SyncStorageEngine.AuthorityInfo authorityInfo
Alon Alberte0bde332011-09-22 14:26:16 -07002156 = mSyncStorageEngine.getAuthority(item.authorityId);
2157 final String authorityName;
2158 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002159 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002160 authorityName = authorityInfo.target.provider;
2161 accountKey = authorityInfo.target.account.name + "/"
2162 + authorityInfo.target.account.type
2163 + " u" + authorityInfo.target.userId;
Alon Alberte0bde332011-09-22 14:26:16 -07002164 } 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 Albert27096822012-01-11 18:06:41 -08002199 if (totalElapsedTime > 0) {
2200 pw.println();
2201 pw.printf("Detailed Statistics (Recent history): "
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002202 + "%d (# of times) %ds (sync time)\n",
Alon Albert27096822012-01-11 18:06:41 -08002203 totalTimes, totalElapsedTime / 1000);
Alon Alberte0bde332011-09-22 14:26:16 -07002204
Alon Albert27096822012-01-11 18:06:41 -08002205 final List<AuthoritySyncStats> sortedAuthorities =
2206 new ArrayList<AuthoritySyncStats>(authorityMap.values());
2207 Collections.sort(sortedAuthorities, new Comparator<AuthoritySyncStats>() {
Alon Albertbf976ba2011-10-03 13:06:43 -07002208 @Override
Alon Albert27096822012-01-11 18:06:41 -08002209 public int compare(AuthoritySyncStats lhs, AuthoritySyncStats rhs) {
Alon Albertbf976ba2011-10-03 13:06:43 -07002210 // reverse order
2211 int compare = Integer.compare(rhs.times, lhs.times);
2212 if (compare == 0) {
2213 compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
Alon Alberte0bde332011-09-22 14:26:16 -07002214 }
Alon Albertbf976ba2011-10-03 13:06:43 -07002215 return compare;
Alon Alberte0bde332011-09-22 14:26:16 -07002216 }
Alon Albertbf976ba2011-10-03 13:06:43 -07002217 });
Alon Albert27096822012-01-11 18:06:41 -08002218
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 Albertbf976ba2011-10-03 13:06:43 -07002240 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 Albert27096822012-01-11 18:06:41 -08002246 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 Alberte0bde332011-09-22 14:26:16 -07002274 }
Alon Alberte0bde332011-09-22 14:26:16 -07002275 }
2276
2277 pw.println();
2278 pw.println("Recent Sync History");
Alon Albert57286f92012-10-09 14:21:38 -07002279 final String format = " %-" + maxAccount + "s %-" + maxAuthority + "s %s\n";
Alon Albertbf976ba2011-10-03 13:06:43 -07002280 final Map<String, Long> lastTimeMap = Maps.newHashMap();
Alon Albert57286f92012-10-09 14:21:38 -07002281 final PackageManager pm = mContext.getPackageManager();
Alon Alberte0bde332011-09-22 14:26:16 -07002282 for (int i = 0; i < N; i++) {
2283 SyncStorageEngine.SyncHistoryItem item = items.get(i);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002284 SyncStorageEngine.AuthorityInfo authorityInfo
Alon Alberte0bde332011-09-22 14:26:16 -07002285 = mSyncStorageEngine.getAuthority(item.authorityId);
2286 final String authorityName;
2287 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002288 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002289 authorityName = authorityInfo.target.provider;
2290 accountKey = authorityInfo.target.account.name + "/"
2291 + authorityInfo.target.account.type
2292 + " u" + authorityInfo.target.userId;
Alon Alberte0bde332011-09-22 14:26:16 -07002293 } 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 Albertbf976ba2011-10-03 13:06:43 -07002302 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 Alberte0bde332011-09-22 14:26:16 -07002322 i + 1,
2323 formatTime(eventTime),
2324 SyncStorageEngine.SOURCES[item.source],
Alon Albertbf976ba2011-10-03 13:06:43 -07002325 ((float) elapsedTime) / 1000,
2326 diffString);
Alon Albert57286f92012-10-09 14:21:38 -07002327 pw.printf(format, accountKey, authorityName,
2328 SyncOperation.reasonToString(pm, item.reason));
Alon Alberte0bde332011-09-22 14:26:16 -07002329
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 Albert57286f92012-10-09 14:21:38 -07002343 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 Williams56dbf8f2013-07-26 12:56:39 -07002351 final SyncStorageEngine.AuthorityInfo authorityInfo
Alon Albert57286f92012-10-09 14:21:38 -07002352 = mSyncStorageEngine.getAuthority(item.authorityId);
2353 final String authorityName;
2354 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002355 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002356 authorityName = authorityInfo.target.provider;
2357 accountKey = authorityInfo.target.account.name + "/"
2358 + authorityInfo.target.account.type
2359 + " u" + authorityInfo.target.userId;
Alon Albert57286f92012-10-09 14:21:38 -07002360 } 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 Alberte0bde332011-09-22 14:26:16 -07002375 }
2376 }
2377
2378 private void dumpDayStatistics(PrintWriter pw) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002379 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 Zongker44f57472009-09-20 15:52:43 -07002387
Dianne Hackborn231cc602009-04-27 17:10:36 -07002388 // 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 Zongker44f57472009-09-20 15:52:43 -07002394
Dianne Hackborn231cc602009-04-27 17:10:36 -07002395 pw.print(" Day-"); pw.print(delta); pw.print(": ");
2396 dumpDayStatistic(pw, ds);
2397 }
Doug Zongker44f57472009-09-20 15:52:43 -07002398
Dianne Hackborn231cc602009-04-27 17:10:36 -07002399 // 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 Zongker44f57472009-09-20 15:52:43 -07002413
Dianne Hackborn231cc602009-04-27 17:10:36 -07002414 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 Alberte0bde332011-09-22 14:26:16 -07002428 }
Doug Zongker44f57472009-09-20 15:52:43 -07002429
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002430 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 Alberte0bde332011-09-22 14:26:16 -07002447 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 Hackborn231cc602009-04-27 17:10:36 -07002465 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 }
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 Quintana918339a2010-10-05 14:00:39 -07002481 final boolean isSyncInProgress = !mActiveSyncContexts.isEmpty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002482 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 Quintana718d8a22009-04-29 17:53:20 -07002501 class ServiceConnectionData {
2502 public final ActiveSyncContext activeSyncContext;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002503 public final IBinder adapter;
2504
2505 ServiceConnectionData(ActiveSyncContext activeSyncContext, IBinder adapter) {
Fred Quintana718d8a22009-04-29 17:53:20 -07002506 this.activeSyncContext = activeSyncContext;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002507 this.adapter = adapter;
Fred Quintana718d8a22009-04-29 17:53:20 -07002508 }
2509 }
2510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002511 /**
2512 * Handles SyncOperation Messages that are posted to the associated
2513 * HandlerThread.
2514 */
2515 class SyncHandler extends Handler {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002516 // Messages that can be sent on mHandler.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 private static final int MESSAGE_SYNC_FINISHED = 1;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002518 private static final int MESSAGE_RELEASE_MESSAGES_FROM_QUEUE = 2;
Fred Quintana718d8a22009-04-29 17:53:20 -07002519 private static final int MESSAGE_SERVICE_CONNECTED = 4;
2520 private static final int MESSAGE_SERVICE_DISCONNECTED = 5;
Fred Quintana918339a2010-10-05 14:00:39 -07002521 private static final int MESSAGE_CANCEL = 6;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002522 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 Basargea4ac5ab2016-04-21 20:31:44 +01002528
Matthew Williams1967c8d2015-06-19 19:03:13 -07002529 /**
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;
振淦王60a74312015-12-01 16:37:31 +08002534 private static final int MESSAGE_ACCOUNTS_UPDATED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002536 public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002537 private final HashMap<String, PowerManager.WakeLock> mWakeLocks = Maps.newHashMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002538
Matthew Williams8b76d202015-05-03 18:16:25 -07002539 private List<Message> mUnreadyQueue = new ArrayList<Message>();
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002540
Matthew Williams8b76d202015-05-03 18:16:25 -07002541 void onBootCompleted() {
Matthew Williams8704fc32013-09-27 11:32:35 -07002542 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002543 Slog.v(TAG, "Boot completed.");
Matthew Williams8704fc32013-09-27 11:32:35 -07002544 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002545 checkIfDeviceReady();
Matthew Williams8b76d202015-05-03 18:16:25 -07002546 }
2547
2548 void onDeviceProvisioned() {
2549 if (Log.isLoggable(TAG, Log.DEBUG)) {
2550 Log.d(TAG, "mProvisioned=" + mProvisioned);
2551 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002552 checkIfDeviceReady();
2553 }
2554
2555 void checkIfDeviceReady() {
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002556 if (mProvisioned && mBootCompleted && mJobServiceReady) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002557 synchronized(this) {
Shreyas Basarge3147bbc2016-02-19 23:51:40 +00002558 mSyncStorageEngine.restoreAllPeriodicSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002559 // Dispatch any stashed messages.
2560 obtainMessage(MESSAGE_RELEASE_MESSAGES_FROM_QUEUE).sendToTarget();
2561 }
Matthew Williams8b76d202015-05-03 18:16:25 -07002562 }
2563 }
2564
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002565 /**
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 Basargea4ac5ab2016-04-21 20:31:44 +01002576 if (!mBootCompleted || !mProvisioned || !mJobServiceReady) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002577 // 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 Williams8704fc32013-09-27 11:32:35 -07002583 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002584 }
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 Basargea4ac5ab2016-04-21 20:31:44 +01002599 mJobServiceReady = true;
2600 checkIfDeviceReady();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002601 } 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 Basargeba1f7902016-10-01 00:19:44 +01002631 ScheduleSyncMessagePayload syncPayload =
2632 (ScheduleSyncMessagePayload) msg.obj;
2633 SyncOperation op = syncPayload.syncOperation;
2634 scheduleSyncOperationH(op, syncPayload.minDelayMillis);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002635 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 Basarge8c834c02016-01-07 13:53:16 +00002644 if (isLoggable) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002645 Slog.v(TAG, "Stop sync received.");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002646 }
2647 ActiveSyncContext asc = findActiveSyncContextH(op.jobId);
2648 if (asc != null) {
2649 runSyncFinishedOrCanceledH(null /* no result */, asc);
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002650 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 Basarge8c834c02016-01-07 13:53:16 +00002662 }
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 Onukia9dca242017-06-21 17:06:49 -07002682 cancelActiveSyncH(endpoint, extras, "MESSAGE_CANCEL");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002683 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 Onukia9dca242017-06-21 17:06:49 -07002698 payload.activeSyncContext.mSyncOperation.jobId, false,
2699 "sync finished");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002700 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 Onukia9dca242017-06-21 17:06:49 -07002743 currentSyncContext.mSyncOperation.jobId, false,
2744 "service disconnected");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002745 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 Onukia9dca242017-06-21 17:06:49 -07002762 monitoredSyncContext.mSyncOperation.jobId, false,
2763 "no network activity");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002764 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 Quintanae91ebe22009-09-29 20:44:30 -07002775 }
2776 }
2777
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002778 private PowerManager.WakeLock getSyncWakeLock(SyncOperation operation) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002779 final String wakeLockKey = operation.wakeLockName();
Fred Quintanab3029c32010-04-06 13:27:12 -07002780 PowerManager.WakeLock wakeLock = mWakeLocks.get(wakeLockKey);
2781 if (wakeLock == null) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002782 final String name = SYNC_WAKE_LOCK_PREFIX + wakeLockKey;
Fred Quintanab3029c32010-04-06 13:27:12 -07002783 wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
2784 wakeLock.setReferenceCounted(false);
2785 mWakeLocks.put(wakeLockKey, wakeLock);
2786 }
2787 return wakeLock;
2788 }
2789
Matthew Williams8704fc32013-09-27 11:32:35 -07002790 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002791 * Defer the specified SyncOperation by rescheduling it on the JobScheduler with some
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002792 * delay. This is equivalent to a failure. If this is a periodic sync, a delayed one-off
2793 * sync will be scheduled.
Matthew Williams8704fc32013-09-27 11:32:35 -07002794 */
Makoto Onukia9dca242017-06-21 17:06:49 -07002795 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 Basarge8c834c02016-01-07 13:53:16 +00002799 if (op.isPeriodic) {
2800 scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay);
2801 } else {
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01002802 // 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 Basarge8c834c02016-01-07 13:53:16 +00002806 scheduleSyncOperationH(op, delay);
Fred Quintanae91ebe22009-09-29 20:44:30 -07002807 }
2808 }
Matthew Williams8704fc32013-09-27 11:32:35 -07002809
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002810 /* 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 Basarge2b4c8f92016-02-08 23:52:27 +00002815 scheduleSyncOperationH(op, delay);
2816 }
2817 }
2818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002819 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002820 * Cancel an active sync and reschedule it on the JobScheduler with some delay.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002821 */
Makoto Onukia9dca242017-06-21 17:06:49 -07002822 private void deferActiveSyncH(ActiveSyncContext asc, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002823 SyncOperation op = asc.mSyncOperation;
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002824 runSyncFinishedOrCanceledH(null, asc);
Makoto Onukia9dca242017-06-21 17:06:49 -07002825 deferSyncH(op, SYNC_DELAY_ON_CONFLICT, why);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 }
2827
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002828 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 Project9066cfe2009-03-03 19:31:44 -08002831
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002832 if (mStorageIsLow) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002833 deferSyncH(op, SYNC_DELAY_ON_LOW_STORAGE, "storage low");
Matthew Williams8704fc32013-09-27 11:32:35 -07002834 return;
2835 }
2836
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002837 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 Basargecbf5ae92016-03-08 16:13:06 +00002840 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002841 for (SyncOperation syncOperation: ops) {
2842 if (syncOperation.sourcePeriodicId == op.jobId) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002843 mSyncJobService.callJobFinished(op.jobId, false,
2844 "periodic sync, pending");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002845 return;
Fred Quintana718d8a22009-04-29 17:53:20 -07002846 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002847 }
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 Onukia9dca242017-06-21 17:06:49 -07002852 mSyncJobService.callJobFinished(op.jobId, false,
2853 "periodic sync, already running");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002854 return;
Fred Quintana718d8a22009-04-29 17:53:20 -07002855 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002856 }
2857 // Check for adapter delays.
2858 if (isAdapterDelayed(op.target)) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002859 deferSyncH(op, 0 /* No minimum delay */, "backing off");
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002860 return;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002861 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002862 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002863
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002864 // 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 Project9066cfe2009-03-03 19:31:44 -08002870 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002871 Slog.v(TAG, "Rescheduling sync due to conflict " + op.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 }
Makoto Onukia9dca242017-06-21 17:06:49 -07002873 deferSyncH(op, SYNC_DELAY_ON_CONFLICT, "delay on conflict");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002874 return;
Matthew Williamsfa774182013-06-18 15:44:11 -07002875 } else {
Matthew Williamsfa774182013-06-18 15:44:11 -07002876 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002877 Slog.v(TAG, "Pushing back running sync due to a higher priority sync");
Fred Quintana918339a2010-10-05 14:00:39 -07002878 }
Makoto Onukia9dca242017-06-21 17:06:49 -07002879 deferActiveSyncH(asc, "preempted");
Shreyas Basarge7680dd92016-02-11 14:52:47 +00002880 break;
Alon Albert8e285552012-09-17 15:05:27 -07002881 }
2882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002883 }
2884
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002885 final int syncOpState = computeSyncOpState(op);
2886 switch (syncOpState) {
2887 case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS:
2888 case SYNC_OP_STATE_INVALID: {
Makoto Onukia9dca242017-06-21 17:06:49 -07002889 mSyncJobService.callJobFinished(op.jobId, false,
2890 "invalid op state: " + syncOpState);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002891 } return;
2892 }
2893
2894 if (!dispatchSyncOperation(op)) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002895 mSyncJobService.callJobFinished(op.jobId, false, "dispatchSyncOperation() failed");
Fred Quintana918339a2010-10-05 14:00:39 -07002896 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002897
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002898 setAuthorityPendingState(op.target);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002899 }
2900
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002901 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 Hackborn627dfa12015-11-11 18:10:30 -08002906 }
2907 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002908 return null;
Matthew Williamsa4745542015-12-10 20:29:02 +00002909 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002910
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002911 private void updateRunningAccountsH(EndPoint syncTargets) {
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002912 AccountAndUser[] oldAccounts = mRunningAccounts;
振淦王60a74312015-12-01 16:37:31 +08002913 mRunningAccounts = AccountManagerService.getSingleton().getRunningAccounts();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002914 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 }
振淦王60a74312015-12-01 16:37:31 +08002920 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 Basarge8c834c02016-01-07 13:53:16 +00002934
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002935 // 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 Xu2c548e02016-04-01 17:47:28 +01002946 // Cancel all jobs from non-existent accounts.
2947 AccountAndUser[] allAccounts = AccountManagerService.getSingleton().getAllAccounts();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002948 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002949 for (SyncOperation op: ops) {
Rubin Xu2c548e02016-04-01 17:47:28 +01002950 if (!containsAccountAndUser(allAccounts, op.target.account, op.target.userId)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002951 getJobScheduler().cancel(op.jobId);
2952 }
2953 }
2954
2955 if (syncTargets != null) {
2956 scheduleSync(syncTargets.account, syncTargets.userId,
Svet Ganovf6d424f12016-09-20 20:18:53 -07002957 SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider,
2958 null, AuthorityInfo.NOT_INITIALIZED);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002959 }
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 Reddyae0bcdd2015-12-23 18:24:23 +00002970 long flexMillis) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002971 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 Basarge2b4c8f92016-02-08 23:52:27 +00002977 SyncOperation newOp = new SyncOperation(syncOperation, pollFrequencyMillis,
2978 flexMillis);
2979 newOp.jobId = syncOperation.jobId;
2980 scheduleSyncOperationH(newOp);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002981 }
2982 }
2983
2984 private void updateOrAddPeriodicSyncH(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002985 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002986 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 Basargecbf5ae92016-03-08 16:13:06 +00002996 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002997 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 Basarge2b4c8f92016-02-08 23:52:27 +00003024 syncAdapterInfo.type.allowParallelSyncs(), true, SyncOperation.NO_JOB_ID,
3025 pollFrequencyMillis, flexMillis);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003026
3027 final int syncOpState = computeSyncOpState(op);
3028 switch (syncOpState) {
3029 case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS: {
Svet Ganov973edd192016-09-08 20:15:55 -07003030 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 Ganov5cb29732016-07-11 19:32:30 -07003036 mAccountManagerInternal.requestAccountAccess(op.target.account,
Svet Ganov973edd192016-09-08 20:15:55 -07003037 packageName, userId, new RemoteCallback((Bundle result) -> {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003038 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 Basarge8c834c02016-01-07 13:53:16 +00003051 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 Basargecbf5ae92016-03-08 16:13:06 +00003060 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003061 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 Onukia9dca242017-06-21 17:06:49 -07003065 mSyncJobService.callJobFinished(syncOperation.jobId, false,
3066 "removePeriodicSyncInternalH");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003067 runSyncFinishedOrCanceledH(null, asc);
3068 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003069 getJobScheduler().cancel(op.jobId);
3070 }
3071 }
3072 }
3073
3074 private void removePeriodicSyncH(EndPoint target, Bundle extras) {
3075 verifyJobScheduler();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003076 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003077 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 Hackborn627dfa12015-11-11 18:10:30 -08003083 }
3084
Matthew Williams1967c8d2015-06-19 19:03:13 -07003085 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 Basarge8c834c02016-01-07 13:53:16 +00003100 "Time since last update: %ds. Delta transferred: %dMBs,%dKBs,%dBs",
3101 (SystemClock.elapsedRealtime()
3102 - activeSyncContext.mLastPolledTimeElapsed)/1000,
3103 mb, kb, b)
Matthew Williams1967c8d2015-06-19 19:03:13 -07003104 );
3105 }
3106 return (deltaBytesTransferred <= SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES);
3107 }
3108
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003109 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003110 * Determine if a sync is no longer valid and should be dropped.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003111 */
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003112 private int computeSyncOpState(SyncOperation op) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003113 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003114 int state;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003115 final EndPoint target = op.target;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003116
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003117 // 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 Williams56dbf8f2013-07-26 12:56:39 -07003122 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003123 return SYNC_OP_STATE_INVALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003124 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003125 // Drop this sync request if it isn't syncable.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003126 state = computeSyncable(target.account, target.userId, target.provider);
3127 if (state == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003128 if (isLoggable) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003129 Slog.v(TAG, " Dropping sync operation: "
3130 + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003131 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003132 return SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003133 }
Svet Ganovdfed1c72016-08-25 15:40:52 -07003134 if (state == AuthorityInfo.NOT_SYNCABLE) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003135 if (isLoggable) {
Svet Ganovdfed1c72016-08-25 15:40:52 -07003136 Slog.v(TAG, " Dropping sync operation: isSyncable == NOT_SYNCABLE");
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003137 }
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 Williams56dbf8f2013-07-26 12:56:39 -07003144
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 Basarge8c834c02016-01-07 13:53:16 +00003149 final boolean ignoreSystemConfiguration = op.isIgnoreSettings() || (state < 0);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003150
3151 // Sync not enabled.
3152 if (!syncEnabled && !ignoreSystemConfiguration) {
3153 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003154 Slog.v(TAG, " Dropping sync operation: disallowed by settings/network.");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003155 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003156 return SYNC_OP_STATE_INVALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003157 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003158 return SYNC_OP_STATE_VALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003159 }
Fred Quintana918339a2010-10-05 14:00:39 -07003160
Fred Quintana87b14662011-09-12 10:32:55 -07003161 private boolean dispatchSyncOperation(SyncOperation op) {
Fred Quintana918339a2010-10-05 14:00:39 -07003162 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003163 Slog.v(TAG, "dispatchSyncOperation: we are going to sync " + op);
3164 Slog.v(TAG, "num active syncs: " + mActiveSyncContexts.size());
Fred Quintana918339a2010-10-05 14:00:39 -07003165 for (ActiveSyncContext syncContext : mActiveSyncContexts) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003166 Slog.v(TAG, syncContext.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07003167 }
3168 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003169 // Connect to the sync adapter.
3170 int targetUid;
3171 ComponentName targetComponent;
3172 final SyncStorageEngine.EndPoint info = op.target;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003173 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 Onukia9dca242017-06-21 17:06:49 -07003178 mLogger.log("dispatchSyncOperation() failed: no sync adapter info for ",
3179 syncAdapterType);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003180 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 Project9066cfe2009-03-03 19:31:44 -08003184 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003185 targetUid = syncAdapterInfo.uid;
3186 targetComponent = syncAdapterInfo.componentName;
Fred Quintana718d8a22009-04-29 17:53:20 -07003187 ActiveSyncContext activeSyncContext =
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003188 new ActiveSyncContext(op, insertStartSyncEvent(op), targetUid);
Fred Quintana718d8a22009-04-29 17:53:20 -07003189 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003190 Slog.v(TAG, "dispatchSyncOperation: starting " + activeSyncContext);
Fred Quintana718d8a22009-04-29 17:53:20 -07003191 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07003192
3193 activeSyncContext.mSyncInfo = mSyncStorageEngine.addActiveSync(activeSyncContext);
3194 mActiveSyncContexts.add(activeSyncContext);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003195
3196 // Post message to begin monitoring this sync's progress.
3197 postMonitorSyncProgressMessage(activeSyncContext);
3198
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003199 if (!activeSyncContext.bindToSyncAdapter(targetComponent, info.userId)) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003200 mLogger.log("dispatchSyncOperation() failed: bind failed. target: ",
3201 targetComponent);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003202 Slog.e(TAG, "Bind attempt failed - target: " + targetComponent);
Fred Quintana918339a2010-10-05 14:00:39 -07003203 closeActiveSyncContext(activeSyncContext);
Fred Quintana87b14662011-09-12 10:32:55 -07003204 return false;
Fred Quintana718d8a22009-04-29 17:53:20 -07003205 }
3206
Fred Quintana87b14662011-09-12 10:32:55 -07003207 return true;
Fred Quintana718d8a22009-04-29 17:53:20 -07003208 }
3209
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003210 private void runBoundToAdapterH(final ActiveSyncContext activeSyncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003211 IBinder syncAdapter) {
Fred Quintana918339a2010-10-05 14:00:39 -07003212 final SyncOperation syncOperation = activeSyncContext.mSyncOperation;
Fred Quintana718d8a22009-04-29 17:53:20 -07003213 try {
Alon Alberteca75112010-12-08 15:02:33 -08003214 activeSyncContext.mIsLinkedToDeath = true;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003215 syncAdapter.linkToDeath(activeSyncContext, 0);
Alon Alberteca75112010-12-08 15:02:33 -08003216
Makoto Onukia9dca242017-06-21 17:06:49 -07003217 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 Basarge8c834c02016-01-07 13:53:16 +00003222 activeSyncContext.mSyncAdapter = ISyncAdapter.Stub.asInterface(syncAdapter);
3223 activeSyncContext.mSyncAdapter
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003224 .startSync(activeSyncContext, syncOperation.target.provider,
3225 syncOperation.target.account, syncOperation.extras);
Makoto Onukia9dca242017-06-21 17:06:49 -07003226
3227 mLogger.log("Sync finish");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 } catch (RemoteException remoteExc) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003229 mLogger.log("Sync failed with RemoteException: ", remoteExc.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07003230 Log.d(TAG, "maybeStartNextSync: caught a RemoteException, rescheduling", remoteExc);
3231 closeActiveSyncContext(activeSyncContext);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003232 increaseBackoffSetting(syncOperation.target);
3233 scheduleSyncOperationH(syncOperation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 } catch (RuntimeException exc) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003235 mLogger.log("Sync failed with RuntimeException: ", exc.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07003236 closeActiveSyncContext(activeSyncContext);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003237 Slog.e(TAG, "Caught RuntimeException while starting the sync " + syncOperation, exc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003239 }
3240
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003241 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07003242 * Cancel the sync for the provided target that matches the given bundle.
Matthew Williams1967c8d2015-06-19 19:03:13 -07003243 * @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 Williams56dbf8f2013-07-26 12:56:39 -07003245 */
Makoto Onukia9dca242017-06-21 17:06:49 -07003246 private void cancelActiveSyncH(SyncStorageEngine.EndPoint info, Bundle extras,
3247 String why) {
Fred Quintana918339a2010-10-05 14:00:39 -07003248 ArrayList<ActiveSyncContext> activeSyncs =
3249 new ArrayList<ActiveSyncContext>(mActiveSyncContexts);
3250 for (ActiveSyncContext activeSyncContext : activeSyncs) {
3251 if (activeSyncContext != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003252 final SyncStorageEngine.EndPoint opInfo =
3253 activeSyncContext.mSyncOperation.target;
Matthew Williams8ef22042013-07-26 12:56:39 -07003254 if (!opInfo.matchesSpec(info)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003255 continue;
Fred Quintana918339a2010-10-05 14:00:39 -07003256 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003257 if (extras != null &&
3258 !syncExtrasEquals(activeSyncContext.mSyncOperation.extras,
3259 extras,
3260 false /* no config settings */)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08003261 continue;
3262 }
Makoto Onukia9dca242017-06-21 17:06:49 -07003263 mSyncJobService.callJobFinished(activeSyncContext.mSyncOperation.jobId, false,
3264 why);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003265 runSyncFinishedOrCanceledH(null /* cancel => no result */, activeSyncContext);
Fred Quintana918339a2010-10-05 14:00:39 -07003266 }
3267 }
3268 }
3269
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003270 /**
3271 * Should be called when a one-off instance of a periodic sync completes successfully.
3272 */
3273 private void reschedulePeriodicSyncH(SyncOperation syncOperation) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003274 // Ensure that the periodic sync wasn't removed.
3275 SyncOperation periodicSync = null;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003276 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003277 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 Basarge8c834c02016-01-07 13:53:16 +00003287 }
3288
Matthew Williams8b76d202015-05-03 18:16:25 -07003289 private void runSyncFinishedOrCanceledH(SyncResult syncResult,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003290 ActiveSyncContext activeSyncContext) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003291 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Alon Alberteca75112010-12-08 15:02:33 -08003292
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003293 final SyncOperation syncOperation = activeSyncContext.mSyncOperation;
3294 final SyncStorageEngine.EndPoint info = syncOperation.target;
3295
Alon Alberteca75112010-12-08 15:02:33 -08003296 if (activeSyncContext.mIsLinkedToDeath) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003297 activeSyncContext.mSyncAdapter.asBinder().unlinkToDeath(activeSyncContext, 0);
Alon Alberteca75112010-12-08 15:02:33 -08003298 activeSyncContext.mIsLinkedToDeath = false;
3299 }
Fred Quintana918339a2010-10-05 14:00:39 -07003300 closeActiveSyncContext(activeSyncContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003301 final long elapsedTime = SystemClock.elapsedRealtime() - activeSyncContext.mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 String historyMessage;
3303 int downstreamActivity;
3304 int upstreamActivity;
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01003305
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 Onukia9dca242017-06-21 17:06:49 -07003312 mLogger.log("runSyncFinishedOrCanceledH() op=", syncOperation, " result=", syncResult);
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01003313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 if (syncResult != null) {
3315 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003316 Slog.v(TAG, "runSyncFinishedOrCanceled [finished]: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003317 + syncOperation + ", result " + syncResult);
3318 }
3319
3320 if (!syncResult.hasError()) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07003321 historyMessage = SyncStorageEngine.MESG_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003322 // TODO: set these correctly when the SyncResult is extended to include it
3323 downstreamActivity = 0;
3324 upstreamActivity = 0;
Makoto Onukia9dca242017-06-21 17:06:49 -07003325 clearBackoffSetting(syncOperation.target, "sync success");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003326
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 Project9066cfe2009-03-03 19:31:44 -08003333 } else {
Fred Quintana307da1a2010-01-21 14:24:20 -08003334 Log.d(TAG, "failed sync operation " + syncOperation + ", " + syncResult);
3335 // the operation failed so increase the backoff time
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003336 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 Basargeba1f7902016-10-01 00:19:44 +01003342 postScheduleSyncMessage(syncOperation.createOneTimeSyncOperation(),
3343 0 /* min delay */);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003344 }
Alon Albert57286f92012-10-09 14:21:38 -07003345 historyMessage = ContentResolver.syncErrorToString(
3346 syncResultToErrorNumber(syncResult));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003347 // TODO: set these correctly when the SyncResult is extended to include it
3348 downstreamActivity = 0;
3349 upstreamActivity = 0;
3350 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003351 setDelayUntilTime(syncOperation.target, syncResult.delayUntil);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 } else {
3353 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003354 Slog.v(TAG, "runSyncFinishedOrCanceled [canceled]: " + syncOperation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 }
Fred Quintana718d8a22009-04-29 17:53:20 -07003356 if (activeSyncContext.mSyncAdapter != null) {
3357 try {
Fred Quintana21bb0de2009-06-16 10:24:58 -07003358 activeSyncContext.mSyncAdapter.cancelSync(activeSyncContext);
Fred Quintana718d8a22009-04-29 17:53:20 -07003359 } catch (RemoteException e) {
3360 // we don't need to retry this in this case
3361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07003363 historyMessage = SyncStorageEngine.MESG_CANCELED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003364 downstreamActivity = 0;
3365 upstreamActivity = 0;
3366 }
3367
3368 stopSyncEvent(activeSyncContext.mHistoryRowId, syncOperation, historyMessage,
3369 upstreamActivity, downstreamActivity, elapsedTime);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003370 // Check for full-resync and schedule it after closing off the last sync.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003371 if (syncResult != null && syncResult.tooManyDeletions) {
3372 installHandleTooManyDeletesNotification(info.account,
3373 info.provider, syncResult.stats.numDeletes,
3374 info.userId);
3375 } else {
Chris Wren282cfef2017-03-27 15:01:44 -04003376 mNotificationMgr.cancelAsUser(
3377 Integer.toString(info.account.hashCode() ^ info.provider.hashCode()),
3378 SystemMessage.NOTE_SYNC_ERROR,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003379 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 Williams56dbf8f2013-07-26 12:56:39 -07003385 syncOperation.reason,
3386 syncOperation.syncSource, info.provider, new Bundle(),
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003387 syncOperation.allowParallelSyncs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003389 }
3390
Fred Quintana918339a2010-10-05 14:00:39 -07003391 private void closeActiveSyncContext(ActiveSyncContext activeSyncContext) {
3392 activeSyncContext.close();
3393 mActiveSyncContexts.remove(activeSyncContext);
Amith Yamasani04e0d262012-02-14 11:50:53 -08003394 mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003395 activeSyncContext.mSyncOperation.target.userId);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003396
3397 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003398 Slog.v(TAG, "removing all MESSAGE_MONITOR_SYNC & MESSAGE_SYNC_EXPIRED for "
Matthew Williams1967c8d2015-06-19 19:03:13 -07003399 + activeSyncContext.toString());
3400 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07003401 mSyncHandler.removeMessages(SyncHandler.MESSAGE_MONITOR_SYNC, activeSyncContext);
Fred Quintana918339a2010-10-05 14:00:39 -07003402 }
3403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 /**
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 Hackborn231cc602009-04-27 17:10:36 -07003414 if (syncResult.syncAlreadyInProgress)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003415 return ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003416 if (syncResult.stats.numAuthExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003417 return ContentResolver.SYNC_ERROR_AUTHENTICATION;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003418 if (syncResult.stats.numIoExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003419 return ContentResolver.SYNC_ERROR_IO;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003420 if (syncResult.stats.numParseExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003421 return ContentResolver.SYNC_ERROR_PARSE;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003422 if (syncResult.stats.numConflictDetectedExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003423 return ContentResolver.SYNC_ERROR_CONFLICT;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003424 if (syncResult.tooManyDeletions)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003425 return ContentResolver.SYNC_ERROR_TOO_MANY_DELETIONS;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003426 if (syncResult.tooManyRetries)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003427 return ContentResolver.SYNC_ERROR_TOO_MANY_RETRIES;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003428 if (syncResult.databaseError)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003429 return ContentResolver.SYNC_ERROR_INTERNAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 throw new IllegalStateException("we are not in an error state, " + syncResult);
3431 }
3432
Fred Quintanad9d2f112009-04-23 13:36:27 -07003433 private void installHandleTooManyDeletesNotification(Account account, String authority,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003434 long numDeletes, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 if (mNotificationMgr == null) return;
Fred Quintanac848b702009-08-25 20:18:46 -07003436
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 Quintanab19e62a2010-12-16 13:54:43 -08003444 Intent clickIntent = new Intent(mContext, SyncActivityTooManyDeletes.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 clickIntent.putExtra("account", account);
Tadashi G. Takaoka86135d32009-09-24 19:31:44 -07003446 clickIntent.putExtra("authority", authority);
Fred Quintanac848b702009-08-25 20:18:46 -07003447 clickIntent.putExtra("provider", authorityName.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 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 Guy07ad8dc2014-09-01 20:56:12 +01003455 UserHandle user = new UserHandle(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 final PendingIntent pendingIntent = PendingIntent
Dianne Hackborn41203752012-08-31 14:05:51 -07003457 .getActivityAsUser(mContext, 0, clickIntent,
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003458 PendingIntent.FLAG_CANCEL_CURRENT, null, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003459
3460 CharSequence tooManyDeletesDescFormat = mContext.getResources().getText(
3461 R.string.contentServiceTooManyDeletesNotificationDesc);
3462
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003463 Context contextForUser = getContextForUser(user);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05003464 Notification notification =
3465 new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT)
Chris Wren1ce4b6d2015-06-11 10:19:43 -04003466 .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 Project9066cfe2009-03-03 19:31:44 -08003477 notification.flags |= Notification.FLAG_ONGOING_EVENT;
Chris Wren282cfef2017-03-27 15:01:44 -04003478 mNotificationMgr.notifyAsUser(
3479 Integer.toString(account.hashCode() ^ authority.hashCode()),
3480 SystemMessage.NOTE_SYNC_ERROR,
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003481 notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003482 }
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 Project9066cfe2009-03-03 19:31:44 -08003506 final long now = System.currentTimeMillis();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003507 EventLog.writeEvent(2720,
3508 syncOperation.toEventLog(SyncStorageEngine.EVENT_START));
3509 return mSyncStorageEngine.insertStartSyncEvent(syncOperation, now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003510 }
3511
3512 public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003513 int upstreamActivity, int downstreamActivity, long elapsedTime) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003514 EventLog.writeEvent(2720,
3515 syncOperation.toEventLog(SyncStorageEngine.EVENT_STOP));
Fred Quintana77c560f2010-03-29 22:20:26 -07003516 mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime,
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003517 resultMessage, downstreamActivity, upstreamActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003518 }
3519 }
Fred Quintana918339a2010-10-05 14:00:39 -07003520
Matthew Williams8b76d202015-05-03 18:16:25 -07003521 private boolean isSyncStillActiveH(ActiveSyncContext activeSyncContext) {
Fred Quintana918339a2010-10-05 14:00:39 -07003522 for (ActiveSyncContext sync : mActiveSyncContexts) {
3523 if (sync == activeSyncContext) {
3524 return true;
3525 }
3526 }
3527 return false;
3528 }
Alon Albert57286f92012-10-09 14:21:38 -07003529
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003530 /**
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 Williams8ef22042013-07-26 12:56:39 -07003540 // Exit early if we can.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003541 if (includeSyncSettings && b1.size() != b2.size()) {
3542 return false;
3543 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003544 Bundle bigger = b1.size() > b2.size() ? b1 : b2;
3545 Bundle smaller = b1.size() > b2.size() ? b2 : b1;
3546 for (String key : bigger.keySet()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003547 if (!includeSyncSettings && isSyncSetting(key)) {
3548 continue;
3549 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003550 if (!smaller.containsKey(key)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003551 return false;
3552 }
Matthew Williams9ad2c842015-10-16 12:01:31 -07003553 if (!Objects.equals(bigger.get(key), smaller.get(key))) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003554 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 Albert57286f92012-10-09 14:21:38 -07003606 static class PrintTable {
Makoto Onuki15e7a252017-06-08 17:12:05 -07003607 private ArrayList<String[]> mTable = Lists.newArrayList();
Alon Albert57286f92012-10-09 14:21:38 -07003608 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 Onuki15e7a252017-06-08 17:12:05 -07003620 final String[] list = new String[mCols];
Alon Albert57286f92012-10-09 14:21:38 -07003621 mTable.add(list);
3622 for (int j = 0; j < mCols; j++) {
3623 list[j] = "";
3624 }
3625 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07003626 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 Albert57286f92012-10-09 14:21:38 -07003631 }
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 Tjin31068162014-01-30 13:28:46 -08003647 formats[mCols - 1] = "%s";
Alon Albert57286f92012-10-09 14:21:38 -07003648 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 Guy07ad8dc2014-09-01 20:56:12 +01003672
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 Pitschaf759c52017-02-15 09:35:38 -05003681}