blob: 965159bdfd7650454e667fd9e32ae952f46c610d [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;
Svet Ganov973edd192016-09-08 20:15:55 -070049import android.content.pm.PackageManagerInternal;
Todd Kennedy0eb97382017-10-03 16:57:22 -070050import android.content.pm.PackageManager.NameNotFoundException;
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;
Makoto Onukibbf6d8c2017-08-11 12:11:39 -070061import android.os.Binder;
Makoto Onukidd4b14f2017-08-17 14:03:48 -070062import android.os.Build;
Fred Quintana918339a2010-10-05 14:00:39 -070063import android.os.Bundle;
64import android.os.Handler;
Fred Quintana918339a2010-10-05 14:00:39 -070065import android.os.IBinder;
66import android.os.Looper;
67import android.os.Message;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000068import android.os.Messenger;
Fred Quintana918339a2010-10-05 14:00:39 -070069import android.os.PowerManager;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070070import android.os.RemoteCallback;
Fred Quintana918339a2010-10-05 14:00:39 -070071import android.os.RemoteException;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080072import android.os.ServiceManager;
Fred Quintana918339a2010-10-05 14:00:39 -070073import android.os.SystemClock;
74import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070075import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -070076import android.os.UserManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070077import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.text.format.Time;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import android.util.EventLog;
81import android.util.Log;
Fred Quintana307da1a2010-01-21 14:24:20 -080082import android.util.Pair;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000083import android.util.Slog;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000084
Chris Wren282cfef2017-03-27 15:01:44 -040085import com.android.internal.messages.nano.SystemMessageProto.SystemMessage;
Geoffrey Pitschaf759c52017-02-15 09:35:38 -050086import com.android.internal.notification.SystemNotificationChannels;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070087import com.android.internal.util.ArrayUtils;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +000088import com.android.server.LocalServices;
89import com.android.server.job.JobSchedulerInternal;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000090import com.google.android.collect.Lists;
91import com.google.android.collect.Maps;
92
Alon Albert8e285552012-09-17 15:05:27 -070093import com.android.internal.R;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080094import com.android.internal.app.IBatteryStats;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070095import com.android.internal.os.BackgroundThread;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070096import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080097import com.android.server.accounts.AccountManagerService;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000098import com.android.server.backup.AccountSyncSettingsBackupHelper;
Georgi Nikolovdbe846b2013-06-25 14:09:56 -070099import com.android.server.content.SyncStorageEngine.AuthorityInfo;
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700100import com.android.server.content.SyncStorageEngine.EndPoint;
Jeff Sharkey7a96c392012-11-15 14:01:46 -0800101import com.android.server.content.SyncStorageEngine.OnSyncRequestListener;
Makoto Onukie7b02982017-08-24 14:23:36 -0700102import com.android.server.job.JobSchedulerInternal.JobStorePersistStats;
Alon Albert8e285552012-09-17 15:05:27 -0700103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105import java.io.PrintWriter;
106import java.util.ArrayList;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000107import java.util.Arrays;
Fred Quintana918339a2010-10-05 14:00:39 -0700108import java.util.Collection;
109import java.util.Collections;
Alon Alberte0bde332011-09-22 14:26:16 -0700110import java.util.Comparator;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000111import java.util.HashMap;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000112import java.util.HashSet;
113import java.util.List;
114import java.util.Map;
Matthew Williams9ad2c842015-10-16 12:01:31 -0700115import java.util.Objects;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000116import java.util.Random;
117import java.util.Set;
Makoto Onuki15e7a252017-06-08 17:12:05 -0700118import java.util.function.Predicate;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120/**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000121 * Implementation details:
122 * All scheduled syncs will be passed on to JobScheduler as jobs
123 * (See {@link #scheduleSyncOperationH(SyncOperation, long)}. This function schedules a job
124 * with JobScheduler with appropriate delay and constraints (according to backoffs and extras).
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000125 * The scheduleSyncOperationH function also assigns a unique jobId to each
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000126 * SyncOperation.
127 *
128 * Periodic Syncs:
129 * Each periodic sync is scheduled as a periodic job. If a periodic sync fails, we create a new
130 * one off SyncOperation and set its {@link SyncOperation#sourcePeriodicId} field to the jobId of the
131 * periodic sync. We don't allow the periodic job to run while any job initiated by it is pending.
132 *
133 * Backoffs:
134 * Each {@link EndPoint} has a backoff associated with it. When a SyncOperation fails, we increase
135 * the backoff on the authority. Then we reschedule all syncs associated with that authority to
136 * run at a later time. Similarly, when a sync succeeds, backoff is cleared and all associated syncs
137 * are rescheduled. A rescheduled sync will get a new jobId.
138 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 * @hide
140 */
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700141public class SyncManager {
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700142 static final String TAG = "SyncManager";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700144 private static final boolean DEBUG_ACCOUNT_ACCESS = false;
145
Makoto Onukib47e8942017-09-18 14:03:03 -0700146 // Only do the check on a debuggable build.
Makoto Onukidd4b14f2017-08-17 14:03:48 -0700147 private static final boolean ENABLE_SUSPICIOUS_CHECK = Build.IS_DEBUGGABLE;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /** Delay a sync due to local changes this long. In milliseconds */
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700150 private static final long LOCAL_SYNC_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700152 static {
Fred Quintana918339a2010-10-05 14:00:39 -0700153 LOCAL_SYNC_DELAY =
154 SystemProperties.getLong("sync.local_sync_delay", 30 * 1000 /* 30 seconds */);
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 /**
158 * When retrying a sync for the first time use this delay. After that
159 * the retry time will double until it reached MAX_SYNC_RETRY_TIME.
160 * In milliseconds.
161 */
162 private static final long INITIAL_SYNC_RETRY_TIME_IN_MS = 30 * 1000; // 30 seconds
163
164 /**
165 * Default the max sync retry time to this value.
166 */
167 private static final long DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS = 60 * 60; // one hour
168
169 /**
Fred Quintana8570f742010-02-18 10:32:54 -0800170 * How long to wait before retrying a sync that failed due to one already being in progress.
171 */
172 private static final int DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS = 10;
173
Matthew Williams92a1c092014-08-25 19:18:32 -0700174 /**
Matthew Williams1967c8d2015-06-19 19:03:13 -0700175 * How often to periodically poll network traffic for an adapter performing a sync to determine
176 * whether progress is being made.
177 */
178 private static final long SYNC_MONITOR_WINDOW_LENGTH_MILLIS = 60 * 1000; // 60 seconds
179
180 /**
181 * How many bytes must be transferred (Tx + Rx) over the period of time defined by
182 * {@link #SYNC_MONITOR_WINDOW_LENGTH_MILLIS} for the sync to be considered to be making
183 * progress.
184 */
185 private static final int SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES = 10; // 10 bytes
186
187 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000188 * If a previously scheduled sync becomes ready and we are low on storage, it gets
189 * pushed back for this amount of time.
190 */
191 private static final long SYNC_DELAY_ON_LOW_STORAGE = 60*60*1000; // 1 hour
192
193 /**
194 * If a sync becomes ready and it conflicts with an already running sync, it gets
195 * pushed back for this amount of time.
196 */
197 private static final long SYNC_DELAY_ON_CONFLICT = 10*1000; // 10 seconds
Fred Quintana3ec47302010-03-10 10:08:31 -0800198
Shreyas Basargefa272532016-03-09 16:52:48 +0000199 /**
200 * Generate job ids in the range [MIN_SYNC_JOB_ID, MAX_SYNC_JOB_ID) to avoid conflicts with
201 * other jobs scheduled by the system process.
202 */
203 private static final int MIN_SYNC_JOB_ID = 100000;
204 private static final int MAX_SYNC_JOB_ID = 110000;
205
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800206 private static final String SYNC_WAKE_LOCK_PREFIX = "*sync*/";
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700207 private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm";
Fred Quintana918339a2010-10-05 14:00:39 -0700208 private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock";
209
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700210
211 private static final int SYNC_OP_STATE_VALID = 0;
212 private static final int SYNC_OP_STATE_INVALID = 1;
213 private static final int SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS = 2;
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216
Amith Yamasani04e0d262012-02-14 11:50:53 -0800217 private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0];
218
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700219 // TODO: add better locking around mRunningAccounts
220 private volatile AccountAndUser[] mRunningAccounts = INITIAL_ACCOUNTS_ARRAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 volatile private PowerManager.WakeLock mHandleAlarmWakeLock;
Fred Quintana918339a2010-10-05 14:00:39 -0700223 volatile private PowerManager.WakeLock mSyncManagerWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 volatile private boolean mDataConnectionIsConnected = false;
225 volatile private boolean mStorageIsLow = false;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700226 volatile private boolean mDeviceIsIdle = false;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800227 volatile private boolean mReportedSyncActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
229 private final NotificationManager mNotificationMgr;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800230 private final IBatteryStats mBatteryStats;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000231 private JobScheduler mJobScheduler;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000232 private JobSchedulerInternal mJobSchedulerInternal;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000233 private SyncJobService mSyncJobService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700235 private SyncStorageEngine mSyncStorageEngine;
Jeff Sharkeya706e2f2012-10-16 12:02:42 -0700236
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700237 protected final ArrayList<ActiveSyncContext> mActiveSyncContexts = Lists.newArrayList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
Fred Quintanaf892fb32009-08-27 21:32:08 -0700239 // Synchronized on "this". Instead of using this directly one should instead call
240 // its accessor, getConnManager().
241 private ConnectivityManager mConnManagerDoNotUseDirectly;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
Matthew Williams8b76d202015-05-03 18:16:25 -0700243 /** Track whether the device has already been provisioned. */
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000244 private volatile boolean mProvisioned;
Matthew Williams8b76d202015-05-03 18:16:25 -0700245
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700246 protected SyncAdaptersCache mSyncAdapters;
Fred Quintana718d8a22009-04-29 17:53:20 -0700247
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000248 private final Random mRand;
249
Makoto Onukia9dca242017-06-21 17:06:49 -0700250 private final SyncLogger mLogger;
251
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000252 private boolean isJobIdInUseLockedH(int jobId, List<JobInfo> pendingJobs) {
253 for (JobInfo job: pendingJobs) {
254 if (job.getId() == jobId) {
255 return true;
256 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000257 }
258 for (ActiveSyncContext asc: mActiveSyncContexts) {
259 if (asc.mSyncOperation.jobId == jobId) {
260 return true;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000261 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000262 }
263 return false;
264 }
265
266 private int getUnusedJobIdH() {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000267 int newJobId;
268 do {
269 newJobId = MIN_SYNC_JOB_ID + mRand.nextInt(MAX_SYNC_JOB_ID - MIN_SYNC_JOB_ID);
270 } while (isJobIdInUseLockedH(newJobId,
271 mJobSchedulerInternal.getSystemScheduledPendingJobs()));
272 return newJobId;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000273 }
274
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000275 private List<SyncOperation> getAllPendingSyncs() {
276 verifyJobScheduler();
277 List<JobInfo> pendingJobs = mJobSchedulerInternal.getSystemScheduledPendingJobs();
278 List<SyncOperation> pendingSyncs = new ArrayList<SyncOperation>(pendingJobs.size());
279 for (JobInfo job: pendingJobs) {
280 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras());
281 if (op != null) {
282 pendingSyncs.add(op);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000283 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000284 }
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000285 return pendingSyncs;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000286 }
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700287
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700288 private final BroadcastReceiver mStorageIntentReceiver =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700290 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 String action = intent.getAction();
293 if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
294 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000295 Slog.v(TAG, "Internal storage is low.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 }
297 mStorageIsLow = true;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700298 cancelActiveSync(
299 SyncStorageEngine.EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL,
Makoto Onukia9dca242017-06-21 17:06:49 -0700300 null /* any sync */,
301 "storage low");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) {
303 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000304 Slog.v(TAG, "Internal storage is ok.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 }
306 mStorageIsLow = false;
Makoto Onukia9dca242017-06-21 17:06:49 -0700307 rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL,
308 "storage ok");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 }
310 }
311 };
312
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700313 private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700314 @Override
Fred Quintana60307342009-03-24 22:48:12 -0700315 public void onReceive(Context context, Intent intent) {
Matthew Williams8b76d202015-05-03 18:16:25 -0700316 mBootCompleted = true;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000317 // Called because it gets all pending jobs and stores them in mScheduledSyncs cache.
318 verifyJobScheduler();
Fred Quintanae91ebe22009-09-29 20:44:30 -0700319 mSyncHandler.onBootCompleted();
Fred Quintana60307342009-03-24 22:48:12 -0700320 }
321 };
322
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700323 private final BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700324 @Override
Amith Yamasanid648a602012-09-26 15:06:10 -0700325 public void onReceive(Context context, Intent intent) {
Makoto Onukifb636cc2017-12-07 15:46:26 -0800326 EndPoint target = new EndPoint(null, null, getSendingUserId());
Shreyas Basargedcb88c82016-02-03 00:09:18 +0000327 updateRunningAccounts(target /* sync targets for user */);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700328 }
329 };
330
Fred Quintanab3029c32010-04-06 13:27:12 -0700331 private final PowerManager mPowerManager;
Ashish Sharma69d95de2012-04-11 17:27:24 -0700332
Amith Yamasani9535c912012-10-10 21:48:33 -0700333 private final UserManager mUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -0700334
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700335 private final AccountManager mAccountManager;
336
337 private final AccountManagerInternal mAccountManagerInternal;
338
Svet Ganov973edd192016-09-08 20:15:55 -0700339 private final PackageManagerInternal mPackageManagerInternal;
340
Amith Yamasani258848d2012-08-10 17:06:33 -0700341 private List<UserInfo> getAllUsers() {
Amith Yamasani9535c912012-10-10 21:48:33 -0700342 return mUserManager.getUsers();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800343 }
344
345 private boolean containsAccountAndUser(AccountAndUser[] accounts, Account account, int userId) {
346 boolean found = false;
347 for (int i = 0; i < accounts.length; i++) {
348 if (accounts[i].userId == userId
349 && accounts[i].account.equals(account)) {
350 found = true;
351 break;
352 }
353 }
354 return found;
355 }
356
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000357 /** target indicates endpoints that should be synced after account info is updated. */
358 private void updateRunningAccounts(EndPoint target) {
359 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_ACCOUNTS_UPDATED");
振淦王60a74312015-12-01 16:37:31 +0800360 // Update accounts in handler thread.
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000361 Message m = mSyncHandler.obtainMessage(SyncHandler.MESSAGE_ACCOUNTS_UPDATED);
362 m.obj = target;
363 m.sendToTarget();
Fred Quintanad9d2f112009-04-23 13:36:27 -0700364 }
365
Jeff Sharkey8f55d112012-10-11 11:00:21 -0700366 private void doDatabaseCleanup() {
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700367 for (UserInfo user : mUserManager.getUsers(true)) {
368 // Skip any partially created/removed users
369 if (user.partial) continue;
Svetoslavf3f02ac2015-09-08 14:36:35 -0700370 Account[] accountsForUser = AccountManagerService.getSingleton().getAccounts(
371 user.id, mContext.getOpPackageName());
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000372
Jeff Sharkey8f55d112012-10-11 11:00:21 -0700373 mSyncStorageEngine.doDatabaseCleanup(accountsForUser, user.id);
374 }
375 }
376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 private BroadcastReceiver mConnectivityIntentReceiver =
378 new BroadcastReceiver() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000379 @Override
380 public void onReceive(Context context, Intent intent) {
381 final boolean wasConnected = mDataConnectionIsConnected;
Alon Alberted1d2532011-02-15 14:02:14 -0800382
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000383 // Don't use the intent to figure out if network is connected, just check
384 // ConnectivityManager directly.
385 mDataConnectionIsConnected = readDataConnectionState();
386 if (mDataConnectionIsConnected) {
387 if (!wasConnected) {
388 if (Log.isLoggable(TAG, Log.VERBOSE)) {
389 Slog.v(TAG, "Reconnection detected: clearing all backoffs");
390 }
Makoto Onukia9dca242017-06-21 17:06:49 -0700391 // Note the location of this code was wrong from nyc to oc; fixed in DR.
392 clearAllBackoffs("network reconnect");
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000393 }
Matthew Williams119aac92014-09-28 20:42:23 -0700394 }
Alon Alberted1d2532011-02-15 14:02:14 -0800395 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000396 };
397
Makoto Onukia9dca242017-06-21 17:06:49 -0700398 private void clearAllBackoffs(String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000399 mSyncStorageEngine.clearAllBackoffsLocked();
Makoto Onukia9dca242017-06-21 17:06:49 -0700400 rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL, why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
Alon Albert1bad83a2011-02-16 10:29:56 -0800403 private boolean readDataConnectionState() {
Alon Alberted1d2532011-02-15 14:02:14 -0800404 NetworkInfo networkInfo = getConnectivityManager().getActiveNetworkInfo();
405 return (networkInfo != null) && networkInfo.isConnected();
406 }
407
Makoto Onukie7b02982017-08-24 14:23:36 -0700408 private String getJobStats() {
409 JobSchedulerInternal js = LocalServices.getService(JobSchedulerInternal.class);
410 return "JobStats: "
411 + ((js == null) ? "(JobSchedulerInternal==null)"
412 : js.getPersistStats().toString());
413 }
414
Dianne Hackborn55280a92009-05-07 15:53:46 -0700415 private BroadcastReceiver mShutdownIntentReceiver =
416 new BroadcastReceiver() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000417 @Override
418 public void onReceive(Context context, Intent intent) {
419 Log.w(TAG, "Writing sync state before shutdown...");
420 getSyncStorageEngine().writeAllState();
Makoto Onukie7b02982017-08-24 14:23:36 -0700421
422 mLogger.log(getJobStats());
Makoto Onukife224e02017-06-29 14:11:14 -0700423 mLogger.log("Shutting down.");
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000424 }
425 };
Dianne Hackborn55280a92009-05-07 15:53:46 -0700426
Amith Yamasani13593602012-03-22 16:16:17 -0700427 private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() {
428 @Override
429 public void onReceive(Context context, Intent intent) {
Alon Albert8e285552012-09-17 15:05:27 -0700430 String action = intent.getAction();
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700431 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
432 if (userId == UserHandle.USER_NULL) return;
433
Alon Albert8e285552012-09-17 15:05:27 -0700434 if (Intent.ACTION_USER_REMOVED.equals(action)) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700435 onUserRemoved(userId);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700436 } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) {
437 onUserUnlocked(userId);
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -0700438 } else if (Intent.ACTION_USER_STOPPED.equals(action)) {
439 onUserStopped(userId);
Alon Albert8e285552012-09-17 15:05:27 -0700440 }
Amith Yamasani13593602012-03-22 16:16:17 -0700441 }
442 };
443
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 private final SyncHandler mSyncHandler;
445
Fred Quintana4f9cfc52009-09-02 15:20:23 -0700446 private volatile boolean mBootCompleted = false;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +0100447 private volatile boolean mJobServiceReady = false;
Fred Quintana4f9cfc52009-09-02 15:20:23 -0700448
Fred Quintanaf892fb32009-08-27 21:32:08 -0700449 private ConnectivityManager getConnectivityManager() {
450 synchronized (this) {
451 if (mConnManagerDoNotUseDirectly == null) {
452 mConnManagerDoNotUseDirectly = (ConnectivityManager)mContext.getSystemService(
453 Context.CONNECTIVITY_SERVICE);
454 }
455 return mConnManagerDoNotUseDirectly;
456 }
457 }
458
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000459 /**
460 * Cancel all unnecessary jobs. This function will be run once after every boot.
461 */
462 private void cleanupJobs() {
463 // O(n^2) in number of jobs, so we run this on the background thread.
464 mSyncHandler.postAtFrontOfQueue(new Runnable() {
465 @Override
466 public void run() {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000467 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000468 Set<String> cleanedKeys = new HashSet<String>();
469 for (SyncOperation opx: ops) {
470 if (cleanedKeys.contains(opx.key)) {
471 continue;
472 }
473 cleanedKeys.add(opx.key);
474 for (SyncOperation opy: ops) {
475 if (opx == opy) {
476 continue;
477 }
478 if (opx.key.equals(opy.key)) {
Makoto Onukibbf6d8c2017-08-11 12:11:39 -0700479 mLogger.log("Removing duplicate sync: ", opy);
Makoto Onukidd4b14f2017-08-17 14:03:48 -0700480 cancelJob(opy, "cleanupJobs() x=" + opx + " y=" + opy);
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000481 }
482 }
483 }
484 }
485 });
486 }
487
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000488 private synchronized void verifyJobScheduler() {
489 if (mJobScheduler != null) {
490 return;
491 }
Makoto Onukibbf6d8c2017-08-11 12:11:39 -0700492 final long token = Binder.clearCallingIdentity();
493 try {
494 if (Log.isLoggable(TAG, Log.VERBOSE)) {
495 Log.d(TAG, "initializing JobScheduler object.");
496 }
497 mJobScheduler = (JobScheduler) mContext.getSystemService(
498 Context.JOB_SCHEDULER_SERVICE);
499 mJobSchedulerInternal = LocalServices.getService(JobSchedulerInternal.class);
500 // Get all persisted syncs from JobScheduler
501 List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs();
502
503 int numPersistedPeriodicSyncs = 0;
504 int numPersistedOneshotSyncs = 0;
505 for (JobInfo job : pendingJobs) {
506 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras());
507 if (op != null) {
508 if (op.isPeriodic) {
509 numPersistedPeriodicSyncs++;
510 } else {
511 numPersistedOneshotSyncs++;
512 // Set the pending status of this EndPoint to true. Pending icon is
513 // shown on the settings activity.
514 mSyncStorageEngine.markPending(op.target, true);
515 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000516 }
517 }
Makoto Onukidd4b14f2017-08-17 14:03:48 -0700518 final String summary = "Loaded persisted syncs: "
519 + numPersistedPeriodicSyncs + " periodic syncs, "
520 + numPersistedOneshotSyncs + " oneshot syncs, "
521 + (pendingJobs.size()) + " total system server jobs, "
Makoto Onukie7b02982017-08-24 14:23:36 -0700522 + getJobStats();
Makoto Onukidd4b14f2017-08-17 14:03:48 -0700523 Slog.i(TAG, summary);
524 mLogger.log(summary);
525
Makoto Onukibbf6d8c2017-08-11 12:11:39 -0700526 cleanupJobs();
527
Makoto Onukidd4b14f2017-08-17 14:03:48 -0700528 if (ENABLE_SUSPICIOUS_CHECK &&
529 (numPersistedPeriodicSyncs == 0) && likelyHasPeriodicSyncs()) {
530 Slog.wtf(TAG, "Device booted with no persisted periodic syncs: " + summary);
Makoto Onukibbf6d8c2017-08-11 12:11:39 -0700531 }
532 } finally {
533 Binder.restoreCallingIdentity(token);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000534 }
Makoto Onukibbf6d8c2017-08-11 12:11:39 -0700535 }
536
537 /**
538 * @return whether the device most likely has some periodic syncs.
539 */
540 private boolean likelyHasPeriodicSyncs() {
541 try {
Makoto Onukib47e8942017-09-18 14:03:03 -0700542 // Each sync adapter has a daily periodic sync by default, but sync adapters can remove
543 // them by themselves. So here, we use an arbitrary threshold. If there are more than
544 // this many sync endpoints, surely one of them should have a periodic sync...
545 return mSyncStorageEngine.getAuthorityCount() >= 6;
Makoto Onukibbf6d8c2017-08-11 12:11:39 -0700546 } catch (Throwable th) {
547 // Just in case.
548 }
549 return false;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000550 }
551
552 private JobScheduler getJobScheduler() {
553 verifyJobScheduler();
554 return mJobScheduler;
555 }
556
Jeff Sharkeye4996bb2012-10-17 14:16:28 -0700557 /**
558 * Should only be created after {@link ContentService#systemReady()} so that
559 * {@link PackageManager} is ready to query.
560 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 public SyncManager(Context context, boolean factoryTest) {
562 // Initialize the SyncStorageEngine first, before registering observers
563 // and creating threads and so on; it may fail if the disk is full.
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700564 mContext = context;
Amith Yamasani9535c912012-10-10 21:48:33 -0700565
Makoto Onukia9dca242017-06-21 17:06:49 -0700566 mLogger = SyncLogger.getInstance();
567
Makoto Onuki6963bea72017-12-12 10:42:39 -0800568 SyncStorageEngine.init(context, BackgroundThread.get().getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 mSyncStorageEngine = SyncStorageEngine.getSingleton();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800570 mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700571 @Override
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700572 public void onSyncRequest(SyncStorageEngine.EndPoint info, int reason, Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000573 scheduleSync(info.account, info.userId, reason, info.provider, extras,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700574 AuthorityInfo.UNDEFINED);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000575 }
576 });
577
578 mSyncStorageEngine.setPeriodicSyncAddedListener(
579 new SyncStorageEngine.PeriodicSyncAddedListener() {
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000580 @Override
581 public void onPeriodicSyncAdded(EndPoint target, Bundle extras, long pollFrequency,
582 long flex) {
583 updateOrAddPeriodicSync(target, pollFrequency, flex, extras);
584 }
585 });
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000586
587 mSyncStorageEngine.setOnAuthorityRemovedListener(new SyncStorageEngine.OnAuthorityRemovedListener() {
588 @Override
589 public void onAuthorityRemoved(EndPoint removedAuthority) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -0700590 removeSyncsForAuthority(removedAuthority, "onAuthorityRemoved");
Amith Yamasani04e0d262012-02-14 11:50:53 -0800591 }
592 });
593
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700594 mSyncAdapters = new SyncAdaptersCache(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700596 mSyncHandler = new SyncHandler(BackgroundThread.get().getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597
Fred Quintana44037e62010-01-21 13:14:49 -0800598 mSyncAdapters.setListener(new RegisteredServicesCacheListener<SyncAdapterType>() {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700599 @Override
600 public void onServiceChanged(SyncAdapterType type, int userId, boolean removed) {
Fred Quintana44037e62010-01-21 13:14:49 -0800601 if (!removed) {
Alon Albert57286f92012-10-09 14:21:38 -0700602 scheduleSync(null, UserHandle.USER_ALL,
603 SyncOperation.REASON_SERVICE_CHANGED,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700604 type.authority, null, AuthorityInfo.UNDEFINED);
Fred Quintana44037e62010-01-21 13:14:49 -0800605 }
606 }
607 }, mSyncHandler);
Fred Quintana718d8a22009-04-29 17:53:20 -0700608
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000609 mRand = new Random(System.currentTimeMillis());
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
612 context.registerReceiver(mConnectivityIntentReceiver, intentFilter);
613
Fred Quintanae91ebe22009-09-29 20:44:30 -0700614 if (!factoryTest) {
615 intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Dianne Hackbornd83a0962014-05-02 16:28:33 -0700616 intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Fred Quintanae91ebe22009-09-29 20:44:30 -0700617 context.registerReceiver(mBootCompletedReceiver, intentFilter);
618 }
Fred Quintana60307342009-03-24 22:48:12 -0700619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
621 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
622 context.registerReceiver(mStorageIntentReceiver, intentFilter);
623
Dianne Hackborn55280a92009-05-07 15:53:46 -0700624 intentFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
625 intentFilter.setPriority(100);
626 context.registerReceiver(mShutdownIntentReceiver, intentFilter);
627
Amith Yamasani13593602012-03-22 16:16:17 -0700628 intentFilter = new IntentFilter();
629 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700630 intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -0700631 intentFilter.addAction(Intent.ACTION_USER_STOPPED);
Alon Albert8e285552012-09-17 15:05:27 -0700632 mContext.registerReceiverAsUser(
633 mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null);
Amith Yamasani13593602012-03-22 16:16:17 -0700634
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 if (!factoryTest) {
636 mNotificationMgr = (NotificationManager)
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000637 context.getSystemService(Context.NOTIFICATION_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 } else {
639 mNotificationMgr = null;
640 }
Fred Quintanab3029c32010-04-06 13:27:12 -0700641 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Amith Yamasani9535c912012-10-10 21:48:33 -0700642 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700643 mAccountManager = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
644 mAccountManagerInternal = LocalServices.getService(AccountManagerInternal.class);
Svet Ganov973edd192016-09-08 20:15:55 -0700645 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700646
Svet Ganovf6d424f12016-09-20 20:18:53 -0700647 mAccountManagerInternal.addOnAppPermissionChangeListener((Account account, int uid) -> {
648 // If the UID gained access to the account kick-off syncs lacking account access
649 if (mAccountManagerInternal.hasAccountAccess(account, uid)) {
650 scheduleSync(account, UserHandle.getUserId(uid),
651 SyncOperation.REASON_ACCOUNTS_UPDATED,
652 null, null, AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS);
653 }
654 });
655
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800656 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
657 BatteryStats.SERVICE_NAME));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
659 // This WakeLock is used to ensure that we stay awake between the time that we receive
660 // a sync alarm notification and when we finish processing it. We need to do this
661 // because we don't do the work in the alarm handler, rather we do it in a message
662 // handler.
Fred Quintanab3029c32010-04-06 13:27:12 -0700663 mHandleAlarmWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 HANDLE_SYNC_ALARM_WAKE_LOCK);
665 mHandleAlarmWakeLock.setReferenceCounted(false);
666
Fred Quintana918339a2010-10-05 14:00:39 -0700667 // This WakeLock is used to ensure that we stay awake while running the sync loop
668 // message handler. Normally we will hold a sync adapter wake lock while it is being
669 // synced but during the execution of the sync loop it might finish a sync for
670 // one sync adapter before starting the sync for the other sync adapter and we
671 // don't want the device to go to sleep during that window.
672 mSyncManagerWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
673 SYNC_LOOP_WAKE_LOCK);
674 mSyncManagerWakeLock.setReferenceCounted(false);
675
Matthew Williams8b76d202015-05-03 18:16:25 -0700676 mProvisioned = isDeviceProvisioned();
677 if (!mProvisioned) {
678 final ContentResolver resolver = context.getContentResolver();
679 ContentObserver provisionedObserver =
680 new ContentObserver(null /* current thread */) {
681 public void onChange(boolean selfChange) {
682 mProvisioned |= isDeviceProvisioned();
683 if (mProvisioned) {
684 mSyncHandler.onDeviceProvisioned();
685 resolver.unregisterContentObserver(this);
686 }
687 }
688 };
689
690 synchronized (mSyncHandler) {
691 resolver.registerContentObserver(
692 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
693 false /* notifyForDescendents */,
694 provisionedObserver);
695
696 // The device *may* have been provisioned while we were registering above observer.
697 // Check again to make sure.
698 mProvisioned |= isDeviceProvisioned();
699 if (mProvisioned) {
700 resolver.unregisterContentObserver(provisionedObserver);
701 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700702 }
Matthew Williams8b76d202015-05-03 18:16:25 -0700703 }
Fred Quintanae91ebe22009-09-29 20:44:30 -0700704
705 if (!factoryTest) {
Amith Yamasanid648a602012-09-26 15:06:10 -0700706 // Register for account list updates for all users
707 mContext.registerReceiverAsUser(mAccountsUpdatedReceiver,
708 UserHandle.ALL,
709 new IntentFilter(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION),
Matthew Williams5c6756f2014-10-02 04:12:28 +0000710 null, null);
Fred Quintanae91ebe22009-09-29 20:44:30 -0700711 }
Ashish Sharma69d95de2012-04-11 17:27:24 -0700712
Christopher Tate734c91d2016-02-02 12:46:18 -0800713 // Set up the communication channel between the scheduled job and the sync manager.
714 // This is posted to the *main* looper intentionally, to defer calling startService()
715 // until after the lengthy primary boot sequence completes on that thread, to avoid
716 // spurious ANR triggering.
717 final Intent startServiceIntent = new Intent(mContext, SyncJobService.class);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000718 startServiceIntent.putExtra(SyncJobService.EXTRA_MESSENGER, new Messenger(mSyncHandler));
Christopher Tate734c91d2016-02-02 12:46:18 -0800719 new Handler(mContext.getMainLooper()).post(new Runnable() {
720 @Override
721 public void run() {
722 mContext.startService(startServiceIntent);
723 }
724 });
Svet Ganov65712b02016-09-01 10:24:11 -0700725
726 // Sync adapters were able to access the synced account without the accounts
727 // permission which circumvents our permission model. Therefore, we require
728 // sync adapters that don't have access to the account to get user consent.
729 // This can be noisy, therefore we will white-list sync adapters installed
730 // before we started checking for account access because they already know
731 // the account (they run before) which is the genie is out of the bottle.
732 whiteListExistingSyncAdaptersIfNeeded();
Makoto Onukife224e02017-06-29 14:11:14 -0700733
Makoto Onukie7b02982017-08-24 14:23:36 -0700734 mLogger.log("Sync manager initialized: " + Build.FINGERPRINT);
Svet Ganov65712b02016-09-01 10:24:11 -0700735 }
736
Makoto Onukife224e02017-06-29 14:11:14 -0700737 public void onStartUser(int userHandle) {
Makoto Onukif15a9422017-12-11 15:50:58 -0800738 mSyncHandler.post(() -> mLogger.log("onStartUser: user=", userHandle));
Makoto Onukife224e02017-06-29 14:11:14 -0700739 }
740
741 public void onUnlockUser(int userHandle) {
Makoto Onukif15a9422017-12-11 15:50:58 -0800742 mSyncHandler.post(() -> mLogger.log("onUnlockUser: user=", userHandle));
Makoto Onukife224e02017-06-29 14:11:14 -0700743 }
744
745 public void onStopUser(int userHandle) {
Makoto Onukif15a9422017-12-11 15:50:58 -0800746 mSyncHandler.post(() -> mLogger.log("onStopUser: user=", userHandle));
Makoto Onukife224e02017-06-29 14:11:14 -0700747 }
748
749
Svet Ganov65712b02016-09-01 10:24:11 -0700750 private void whiteListExistingSyncAdaptersIfNeeded() {
751 if (!mSyncStorageEngine.shouldGrantSyncAdaptersAccountAccess()) {
752 return;
753 }
754 List<UserInfo> users = mUserManager.getUsers(true);
755 final int userCount = users.size();
756 for (int i = 0; i < userCount; i++) {
757 UserHandle userHandle = users.get(i).getUserHandle();
758 final int userId = userHandle.getIdentifier();
759 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> service
760 : mSyncAdapters.getAllServices(userId)) {
761 String packageName = service.componentName.getPackageName();
762 for (Account account : mAccountManager.getAccountsByTypeAsUser(
763 service.type.accountType, userHandle)) {
764 if (!canAccessAccount(account, packageName, userId)) {
765 mAccountManager.updateAppPermission(account,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700766 AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, service.uid, true);
Svet Ganov65712b02016-09-01 10:24:11 -0700767 }
768 }
769 }
770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 }
772
Matthew Williams8b76d202015-05-03 18:16:25 -0700773 private boolean isDeviceProvisioned() {
774 final ContentResolver resolver = mContext.getContentResolver();
775 return (Settings.Global.getInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0) != 0);
776 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777 /**
778 * Return a random value v that satisfies minValue <= v < maxValue. The difference between
779 * maxValue and minValue must be less than Integer.MAX_VALUE.
780 */
781 private long jitterize(long minValue, long maxValue) {
782 Random random = new Random(SystemClock.elapsedRealtime());
783 long spread = maxValue - minValue;
784 if (spread > Integer.MAX_VALUE) {
785 throw new IllegalArgumentException("the difference between the maxValue and the "
786 + "minValue must be less than " + Integer.MAX_VALUE);
787 }
788 return minValue + random.nextInt((int)spread);
789 }
790
Dianne Hackborn231cc602009-04-27 17:10:36 -0700791 public SyncStorageEngine getSyncStorageEngine() {
792 return mSyncStorageEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
Doug Zongker44f57472009-09-20 15:52:43 -0700794
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700795 private int getIsSyncable(Account account, int userId, String providerName) {
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700796 int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName);
797 UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId);
798
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000799 // If it's not a restricted user, return isSyncable.
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700800 if (userInfo == null || !userInfo.isRestricted()) return isSyncable;
801
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000802 // Else check if the sync adapter has opted-in or not.
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700803 RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
804 mSyncAdapters.getServiceInfo(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000805 SyncAdapterType.newKey(providerName, account.type), userId);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700806 if (syncAdapterInfo == null) return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700807
808 PackageInfo pInfo = null;
809 try {
810 pInfo = AppGlobals.getPackageManager().getPackageInfo(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000811 syncAdapterInfo.componentName.getPackageName(), 0, userId);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700812 if (pInfo == null) return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700813 } catch (RemoteException re) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000814 // Shouldn't happen.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700815 return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700816 }
817 if (pInfo.restrictedAccountType != null
818 && pInfo.restrictedAccountType.equals(account.type)) {
819 return isSyncable;
820 } else {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700821 return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700822 }
823 }
824
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000825 private void setAuthorityPendingState(EndPoint info) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000826 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000827 for (SyncOperation op: ops) {
828 if (!op.isPeriodic && op.target.matchesSpec(info)) {
829 getSyncStorageEngine().markPending(info, true);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700830 return;
831 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700832 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000833 getSyncStorageEngine().markPending(info, false);
Matthew Williamsfa774182013-06-18 15:44:11 -0700834 }
835
836 /**
837 * Initiate a sync. This can start a sync for all providers
838 * (pass null to url, set onlyTicklable to false), only those
839 * providers that are marked as ticklable (pass null to url,
840 * set onlyTicklable to true), or a specific provider (set url
841 * to the content url of the provider).
842 *
843 * <p>If the ContentResolver.SYNC_EXTRAS_UPLOAD boolean in extras is
844 * true then initiate a sync that just checks for local changes to send
845 * to the server, otherwise initiate a sync that first gets any
846 * changes from the server before sending local changes back to
847 * the server.
848 *
849 * <p>If a specific provider is being synced (the url is non-null)
850 * then the extras can contain SyncAdapter-specific information
851 * to control what gets synced (e.g. which specific feed to sync).
852 *
853 * <p>You'll start getting callbacks after this.
854 *
855 * @param requestedAccount the account to sync, may be null to signify all accounts
856 * @param userId the id of the user whose accounts are to be synced. If userId is USER_ALL,
857 * then all users' accounts are considered.
858 * @param reason for sync request. If this is a positive integer, it is the Linux uid
859 * assigned to the process that requested the sync. If it's negative, the sync was requested by
860 * the SyncManager itself and could be one of the following:
861 * {@link SyncOperation#REASON_BACKGROUND_DATA_SETTINGS_CHANGED}
862 * {@link SyncOperation#REASON_ACCOUNTS_UPDATED}
863 * {@link SyncOperation#REASON_SERVICE_CHANGED}
864 * {@link SyncOperation#REASON_PERIODIC}
865 * {@link SyncOperation#REASON_IS_SYNCABLE}
866 * {@link SyncOperation#REASON_SYNC_AUTO}
867 * {@link SyncOperation#REASON_MASTER_SYNC_AUTO}
868 * {@link SyncOperation#REASON_USER_START}
869 * @param requestedAuthority the authority to sync, may be null to indicate all authorities
870 * @param extras a Map of SyncAdapter-specific information to control
871 * syncs of a specific provider. Can be null. Is ignored
872 * if the url is null.
Svet Ganovf6d424f12016-09-20 20:18:53 -0700873 * @param targetSyncState Only sync authorities that have the specified sync state.
874 * Use {@link AuthorityInfo#UNDEFINED} to sync all authorities.
Matthew Williamsfa774182013-06-18 15:44:11 -0700875 */
876 public void scheduleSync(Account requestedAccount, int userId, int reason,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100877 String requestedAuthority, Bundle extras, int targetSyncState) {
878 scheduleSync(requestedAccount, userId, reason, requestedAuthority, extras, targetSyncState,
879 0 /* min delay */);
880 }
881
882 /**
883 * @param minDelayMillis The sync can't land before this delay expires.
884 */
885 private void scheduleSync(Account requestedAccount, int userId, int reason,
886 String requestedAuthority, Bundle extras, int targetSyncState,
887 final long minDelayMillis) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000888 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Matthew Williamsfa774182013-06-18 15:44:11 -0700889 if (extras == null) {
890 extras = new Bundle();
891 }
892 if (isLoggable) {
893 Log.d(TAG, "one-time sync for: " + requestedAccount + " " + extras.toString() + " "
894 + requestedAuthority);
895 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700896
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700897 AccountAndUser[] accounts = null;
898 if (requestedAccount != null) {
899 if (userId != UserHandle.USER_ALL) {
900 accounts = new AccountAndUser[]{new AccountAndUser(requestedAccount, userId)};
901 } else {
902 for (AccountAndUser runningAccount : mRunningAccounts) {
903 if (requestedAccount.equals(runningAccount.account)) {
904 accounts = ArrayUtils.appendElement(AccountAndUser.class,
905 accounts, runningAccount);
906 }
907 }
908 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700909 } else {
Matthew Williamsfa774182013-06-18 15:44:11 -0700910 accounts = mRunningAccounts;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700911 }
912
913 if (ArrayUtils.isEmpty(accounts)) {
914 if (isLoggable) {
915 Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
Matthew Williamsfa774182013-06-18 15:44:11 -0700916 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700917 return;
Matthew Williamsfa774182013-06-18 15:44:11 -0700918 }
919
920 final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false);
921 final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
922 if (manualSync) {
923 extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true);
924 extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
925 }
926 final boolean ignoreSettings =
927 extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false);
928
929 int source;
930 if (uploadOnly) {
931 source = SyncStorageEngine.SOURCE_LOCAL;
932 } else if (manualSync) {
933 source = SyncStorageEngine.SOURCE_USER;
934 } else if (requestedAuthority == null) {
935 source = SyncStorageEngine.SOURCE_POLL;
936 } else {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000937 // This isn't strictly server, since arbitrary callers can (and do) request
938 // a non-forced two-way sync on a specific url.
Matthew Williamsfa774182013-06-18 15:44:11 -0700939 source = SyncStorageEngine.SOURCE_SERVER;
940 }
941
942 for (AccountAndUser account : accounts) {
Fyodor Kupolov6fde2982015-01-06 17:46:37 -0800943 // If userId is specified, do not sync accounts of other users
Xiaohui Chen98404fd2015-08-17 16:09:02 -0700944 if (userId >= UserHandle.USER_SYSTEM && account.userId >= UserHandle.USER_SYSTEM
Fyodor Kupolov6fde2982015-01-06 17:46:37 -0800945 && userId != account.userId) {
946 continue;
947 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700948 // Compile a list of authorities that have sync adapters.
949 // For each authority sync each account that matches a sync adapter.
950 final HashSet<String> syncableAuthorities = new HashSet<String>();
951 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapter :
952 mSyncAdapters.getAllServices(account.userId)) {
953 syncableAuthorities.add(syncAdapter.type.authority);
954 }
955
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000956 // If the url was specified then replace the list of authorities
Matthew Williamsfa774182013-06-18 15:44:11 -0700957 // with just this authority or clear it if this authority isn't
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000958 // syncable.
Matthew Williamsfa774182013-06-18 15:44:11 -0700959 if (requestedAuthority != null) {
960 final boolean hasSyncAdapter = syncableAuthorities.contains(requestedAuthority);
961 syncableAuthorities.clear();
962 if (hasSyncAdapter) syncableAuthorities.add(requestedAuthority);
963 }
964
965 for (String authority : syncableAuthorities) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700966 int isSyncable = computeSyncable(account.account, account.userId, authority);
967
Matthew Williams53abfdb2015-06-10 20:06:37 -0700968 if (isSyncable == AuthorityInfo.NOT_SYNCABLE) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700969 continue;
970 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700971
972 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
973 mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(authority,
974 account.account.type), account.userId);
Matthew Williamsfa774182013-06-18 15:44:11 -0700975 if (syncAdapterInfo == null) {
976 continue;
977 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700978
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700979 final int owningUid = syncAdapterInfo.uid;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700980
981 if (isSyncable == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
982 if (isLoggable) {
983 Slog.v(TAG, " Not scheduling sync operation: "
984 + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700985 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700986 Bundle finalExtras = new Bundle(extras);
Svet Ganov973edd192016-09-08 20:15:55 -0700987 String packageName = syncAdapterInfo.componentName.getPackageName();
988 // If the app did not run and has no account access, done
Amith Yamasani2cbfa1e2017-03-28 10:34:01 -0700989 try {
990 if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
991 continue;
992 }
993 } catch (IllegalArgumentException e) {
994 // Package not found, race with an uninstall
Svet Ganov973edd192016-09-08 20:15:55 -0700995 continue;
996 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700997 mAccountManagerInternal.requestAccountAccess(account.account,
Svet Ganov973edd192016-09-08 20:15:55 -0700998 packageName, userId,
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700999 new RemoteCallback((Bundle result) -> {
1000 if (result != null
1001 && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
1002 scheduleSync(account.account, userId, reason, authority,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001003 finalExtras, targetSyncState, minDelayMillis);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001004 }
1005 }
1006 ));
1007 continue;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001008 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001009
Matthew Williamsfa774182013-06-18 15:44:11 -07001010 final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs();
1011 final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable();
1012 if (isSyncable < 0 && isAlwaysSyncable) {
Matthew Williams53abfdb2015-06-10 20:06:37 -07001013 mSyncStorageEngine.setIsSyncable(
1014 account.account, account.userId, authority, AuthorityInfo.SYNCABLE);
1015 isSyncable = AuthorityInfo.SYNCABLE;
Matthew Williamsfa774182013-06-18 15:44:11 -07001016 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001017
Svet Ganovf6d424f12016-09-20 20:18:53 -07001018 if (targetSyncState != AuthorityInfo.UNDEFINED && targetSyncState != isSyncable) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001019 continue;
1020 }
Svet Ganovf6d424f12016-09-20 20:18:53 -07001021
Matthew Williamsfa774182013-06-18 15:44:11 -07001022 if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) {
1023 continue;
1024 }
1025
Matthew Williamsfa774182013-06-18 15:44:11 -07001026 boolean syncAllowed =
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001027 (isSyncable < 0) // Always allow if the isSyncable state is unknown.
1028 || ignoreSettings
1029 || (mSyncStorageEngine.getMasterSyncAutomatically(account.userId)
Matthew Williamsfa774182013-06-18 15:44:11 -07001030 && mSyncStorageEngine.getSyncAutomatically(account.account,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001031 account.userId, authority));
Matthew Williamsfa774182013-06-18 15:44:11 -07001032 if (!syncAllowed) {
1033 if (isLoggable) {
1034 Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority
1035 + " is not allowed, dropping request");
1036 }
1037 continue;
1038 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001039 SyncStorageEngine.EndPoint info =
1040 new SyncStorageEngine.EndPoint(
1041 account.account, authority, account.userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001042 long delayUntil =
1043 mSyncStorageEngine.getDelayUntilTime(info);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001044
1045 final String owningPackage = syncAdapterInfo.componentName.getPackageName();
1046
Svet Ganovf6d424f12016-09-20 20:18:53 -07001047 if (isSyncable == AuthorityInfo.NOT_INITIALIZED) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001048 // Initialisation sync.
1049 Bundle newExtras = new Bundle();
1050 newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
1051 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001052 Slog.v(TAG, "schedule initialisation Sync:"
Matthew Williamsfa774182013-06-18 15:44:11 -07001053 + ", delay until " + delayUntil
1054 + ", run by " + 0
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001055 + ", flexMillis " + 0
Matthew Williamsfa774182013-06-18 15:44:11 -07001056 + ", source " + source
1057 + ", account " + account
1058 + ", authority " + authority
1059 + ", extras " + newExtras);
1060 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001061 postScheduleSyncMessage(
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001062 new SyncOperation(account.account, account.userId,
1063 owningUid, owningPackage, reason, source,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001064 authority, newExtras, allowParallelSyncs),
1065 minDelayMillis
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001066 );
Svet Ganovf6d424f12016-09-20 20:18:53 -07001067 } else if (targetSyncState == AuthorityInfo.UNDEFINED
1068 || targetSyncState == isSyncable) {
Matthew Williamsfa774182013-06-18 15:44:11 -07001069 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001070 Slog.v(TAG, "scheduleSync:"
Matthew Williamsfa774182013-06-18 15:44:11 -07001071 + " delay until " + delayUntil
Matthew Williamsfa774182013-06-18 15:44:11 -07001072 + ", source " + source
1073 + ", account " + account
1074 + ", authority " + authority
1075 + ", extras " + extras);
1076 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001077 postScheduleSyncMessage(
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001078 new SyncOperation(account.account, account.userId,
1079 owningUid, owningPackage, reason, source,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001080 authority, extras, allowParallelSyncs),
1081 minDelayMillis
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001082 );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
1084 }
1085 }
1086 }
1087
Svet Ganov96b9c752016-10-17 19:29:58 -07001088 private int computeSyncable(Account account, int userId, String authority) {
1089 return computeSyncable(account, userId, authority, true);
1090 }
1091
1092 public int computeSyncable(Account account, int userId, String authority,
1093 boolean checkAccountAccess) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001094 final int status = getIsSyncable(account, userId, authority);
1095 if (status == AuthorityInfo.NOT_SYNCABLE) {
1096 return AuthorityInfo.NOT_SYNCABLE;
1097 }
1098 final SyncAdapterType type = SyncAdapterType.newKey(authority, account.type);
1099 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
1100 mSyncAdapters.getServiceInfo(type, userId);
1101 if (syncAdapterInfo == null) {
1102 return AuthorityInfo.NOT_SYNCABLE;
1103 }
1104 final int owningUid = syncAdapterInfo.uid;
1105 final String owningPackage = syncAdapterInfo.componentName.getPackageName();
1106 try {
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001107 if (ActivityManager.getService().isAppStartModeDisabled(owningUid, owningPackage)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001108 Slog.w(TAG, "Not scheduling job " + syncAdapterInfo.uid + ":"
1109 + syncAdapterInfo.componentName
1110 + " -- package not allowed to start");
1111 return AuthorityInfo.NOT_SYNCABLE;
1112 }
1113 } catch (RemoteException e) {
1114 /* ignore - local call */
1115 }
Svet Ganov96b9c752016-10-17 19:29:58 -07001116 if (checkAccountAccess && !canAccessAccount(account, owningPackage, owningUid)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001117 Log.w(TAG, "Access to " + account + " denied for package "
1118 + owningPackage + " in UID " + syncAdapterInfo.uid);
1119 return AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS;
1120 }
1121
1122 return status;
1123 }
1124
1125 private boolean canAccessAccount(Account account, String packageName, int uid) {
1126 if (mAccountManager.hasAccountAccess(account, packageName,
1127 UserHandle.getUserHandleForUid(uid))) {
1128 return true;
1129 }
1130 // We relax the account access rule to also include the system apps as
1131 // they are trusted and we want to minimize the cases where the user
1132 // involvement is required to grant access to the synced account.
1133 try {
1134 mContext.getPackageManager().getApplicationInfoAsUser(packageName,
1135 PackageManager.MATCH_SYSTEM_ONLY, UserHandle.getUserId(uid));
1136 return true;
1137 } catch (NameNotFoundException e) {
1138 return false;
1139 }
1140 }
1141
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001142 private void removeSyncsForAuthority(EndPoint info, String why) {
Makoto Onukibbf6d8c2017-08-11 12:11:39 -07001143 mLogger.log("removeSyncsForAuthority: ", info);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001144 verifyJobScheduler();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001145 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001146 for (SyncOperation op: ops) {
1147 if (op.target.matchesSpec(info)) {
Makoto Onukibbf6d8c2017-08-11 12:11:39 -07001148 mLogger.log("canceling: ", op);
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001149 cancelJob(op, why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001150 }
1151 }
1152 }
1153
1154 /**
1155 * Remove a specific periodic sync identified by its target and extras.
1156 */
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001157 public void removePeriodicSync(EndPoint target, Bundle extras, String why) {
1158 Message m = mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_REMOVE_PERIODIC_SYNC,
1159 Pair.create(target, why));
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001160 m.setData(extras);
1161 m.sendToTarget();
1162 }
1163
1164 /**
1165 * Add a periodic sync. If a sync with same target and extras exists, its period and
1166 * flexMillis will be updated.
1167 */
1168 public void updateOrAddPeriodicSync(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001169 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001170 UpdatePeriodicSyncMessagePayload payload = new UpdatePeriodicSyncMessagePayload(target,
1171 pollFrequency, flex, extras);
1172 mSyncHandler.obtainMessage(SyncHandler.MESSAGE_UPDATE_PERIODIC_SYNC, payload)
1173 .sendToTarget();
1174 }
1175
1176 /**
1177 * Get a list of periodic syncs corresponding to the given target.
1178 */
1179 public List<PeriodicSync> getPeriodicSyncs(EndPoint target) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001180 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001181 List<PeriodicSync> periodicSyncs = new ArrayList<PeriodicSync>();
1182
1183 for (SyncOperation op: ops) {
1184 if (op.isPeriodic && op.target.matchesSpec(target)) {
1185 periodicSyncs.add(new PeriodicSync(op.target.account, op.target.provider,
1186 op.extras, op.periodMillis / 1000, op.flexMillis / 1000));
1187 }
1188 }
1189
1190 return periodicSyncs;
1191 }
1192
Matthew Williamsfa774182013-06-18 15:44:11 -07001193 /**
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001194 * Schedule sync based on local changes to a provider. We wait for at least LOCAL_SYNC_DELAY
1195 * ms to batch syncs.
Matthew Williamsfa774182013-06-18 15:44:11 -07001196 */
Alon Albert57286f92012-10-09 14:21:38 -07001197 public void scheduleLocalSync(Account account, int userId, int reason, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 final Bundle extras = new Bundle();
1199 extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
Matthew Williamsfa774182013-06-18 15:44:11 -07001200 scheduleSync(account, userId, reason, authority, extras,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001201 AuthorityInfo.UNDEFINED, LOCAL_SYNC_DELAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 }
1203
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001204 public SyncAdapterType[] getSyncAdapterTypes(int userId) {
1205 final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos;
1206 serviceInfos = mSyncAdapters.getAllServices(userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07001207 SyncAdapterType[] types = new SyncAdapterType[serviceInfos.size()];
1208 int i = 0;
1209 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> serviceInfo : serviceInfos) {
1210 types[i] = serviceInfo.type;
1211 ++i;
1212 }
1213 return types;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215
Amith Yamasani37a40c22015-06-17 13:25:42 -07001216 public String[] getSyncAdapterPackagesForAuthorityAsUser(String authority, int userId) {
1217 return mSyncAdapters.getSyncAdapterPackagesForAuthority(authority, userId);
1218 }
1219
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 private void sendSyncFinishedOrCanceledMessage(ActiveSyncContext syncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001221 SyncResult syncResult) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001222 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_SYNC_FINISHED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 Message msg = mSyncHandler.obtainMessage();
1224 msg.what = SyncHandler.MESSAGE_SYNC_FINISHED;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001225 msg.obj = new SyncFinishedOrCancelledMessagePayload(syncContext, syncResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 mSyncHandler.sendMessage(msg);
1227 }
1228
Makoto Onukia9dca242017-06-21 17:06:49 -07001229 private void sendCancelSyncsMessage(final SyncStorageEngine.EndPoint info, Bundle extras,
1230 String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001231 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_CANCEL");
Makoto Onukia9dca242017-06-21 17:06:49 -07001232
1233 mLogger.log("sendCancelSyncsMessage() ep=", info, " why=", why);
1234
Fred Quintana918339a2010-10-05 14:00:39 -07001235 Message msg = mSyncHandler.obtainMessage();
1236 msg.what = SyncHandler.MESSAGE_CANCEL;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001237 msg.setData(extras);
1238 msg.obj = info;
Fred Quintana918339a2010-10-05 14:00:39 -07001239 mSyncHandler.sendMessage(msg);
1240 }
1241
Matthew Williams92a1c092014-08-25 19:18:32 -07001242 /**
Matthew Williams1967c8d2015-06-19 19:03:13 -07001243 * Post a delayed message that will monitor the given sync context by periodically checking how
1244 * much network has been used by the uid.
Matthew Williams92a1c092014-08-25 19:18:32 -07001245 */
Matthew Williams1967c8d2015-06-19 19:03:13 -07001246 private void postMonitorSyncProgressMessage(ActiveSyncContext activeSyncContext) {
Matthew Williams92a1c092014-08-25 19:18:32 -07001247 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001248 Slog.v(TAG, "posting MESSAGE_SYNC_MONITOR in " +
Matthew Williams1967c8d2015-06-19 19:03:13 -07001249 (SYNC_MONITOR_WINDOW_LENGTH_MILLIS/1000) + "s");
Matthew Williams92a1c092014-08-25 19:18:32 -07001250 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07001251
1252 activeSyncContext.mBytesTransferredAtLastPoll =
1253 getTotalBytesTransferredByUid(activeSyncContext.mSyncAdapterUid);
1254 activeSyncContext.mLastPolledTimeElapsed = SystemClock.elapsedRealtime();
1255 Message monitorMessage =
1256 mSyncHandler.obtainMessage(
1257 SyncHandler.MESSAGE_MONITOR_SYNC,
1258 activeSyncContext);
1259 mSyncHandler.sendMessageDelayed(monitorMessage, SYNC_MONITOR_WINDOW_LENGTH_MILLIS);
Matthew Williams92a1c092014-08-25 19:18:32 -07001260 }
1261
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001262 private void postScheduleSyncMessage(SyncOperation syncOperation, long minDelayMillis) {
1263 ScheduleSyncMessagePayload payload =
1264 new ScheduleSyncMessagePayload(syncOperation, minDelayMillis);
1265 mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_SCHEDULE_SYNC, payload).sendToTarget();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001266 }
1267
Matthew Williams1967c8d2015-06-19 19:03:13 -07001268 /**
1269 * Monitor sync progress by calculating how many bytes it is managing to send to and fro.
1270 */
1271 private long getTotalBytesTransferredByUid(int uid) {
1272 return (TrafficStats.getUidRxBytes(uid) + TrafficStats.getUidTxBytes(uid));
1273 }
1274
1275 /**
1276 * Convenience class for passing parameters for a finished or cancelled sync to the handler
1277 * to be processed.
1278 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001279 private class SyncFinishedOrCancelledMessagePayload {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 public final ActiveSyncContext activeSyncContext;
1281 public final SyncResult syncResult;
1282
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001283 SyncFinishedOrCancelledMessagePayload(ActiveSyncContext syncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001284 SyncResult syncResult) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 this.activeSyncContext = syncContext;
1286 this.syncResult = syncResult;
1287 }
1288 }
1289
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001290 private class UpdatePeriodicSyncMessagePayload {
1291 public final EndPoint target;
1292 public final long pollFrequency;
1293 public final long flex;
1294 public final Bundle extras;
1295
1296 UpdatePeriodicSyncMessagePayload(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001297 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001298 this.target = target;
1299 this.pollFrequency = pollFrequency;
1300 this.flex = flex;
1301 this.extras = extras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 }
1303 }
1304
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001305 private static class ScheduleSyncMessagePayload {
1306 final SyncOperation syncOperation;
1307 final long minDelayMillis;
1308
1309 ScheduleSyncMessagePayload(SyncOperation syncOperation, long minDelayMillis) {
1310 this.syncOperation = syncOperation;
1311 this.minDelayMillis = minDelayMillis;
1312 }
1313 }
1314
Makoto Onukia9dca242017-06-21 17:06:49 -07001315 private void clearBackoffSetting(EndPoint target, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001316 Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target);
1317 if (backoff != null && backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE &&
1318 backoff.second == SyncStorageEngine.NOT_IN_BACKOFF_MODE) {
1319 return;
1320 }
1321 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1322 Slog.v(TAG, "Clearing backoffs for " + target);
1323 }
1324 mSyncStorageEngine.setBackoff(target,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001325 SyncStorageEngine.NOT_IN_BACKOFF_MODE,
1326 SyncStorageEngine.NOT_IN_BACKOFF_MODE);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001327
Makoto Onukia9dca242017-06-21 17:06:49 -07001328 rescheduleSyncs(target, why);
Alon Albert6e079a32010-11-12 12:41:09 -08001329 }
1330
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001331 private void increaseBackoffSetting(EndPoint target) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001332 final long now = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333
Fred Quintana307da1a2010-01-21 14:24:20 -08001334 final Pair<Long, Long> previousSettings =
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001335 mSyncStorageEngine.getBackoff(target);
Alon Albertaeeb6202010-12-09 16:14:02 -08001336 long newDelayInMs = -1;
1337 if (previousSettings != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001338 // Don't increase backoff before current backoff is expired. This will happen for op's
Alon Albertaeeb6202010-12-09 16:14:02 -08001339 // with ignoreBackoff set.
1340 if (now < previousSettings.first) {
1341 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001342 Slog.v(TAG, "Still in backoff, do not increase it. "
1343 + "Remaining: " + ((previousSettings.first - now) / 1000) + " seconds.");
Alon Albertaeeb6202010-12-09 16:14:02 -08001344 }
1345 return;
1346 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001347 // Subsequent delays are the double of the previous delay.
Alon Albertaeeb6202010-12-09 16:14:02 -08001348 newDelayInMs = previousSettings.second * 2;
1349 }
1350 if (newDelayInMs <= 0) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001351 // The initial delay is the jitterized INITIAL_SYNC_RETRY_TIME_IN_MS.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 newDelayInMs = jitterize(INITIAL_SYNC_RETRY_TIME_IN_MS,
1353 (long)(INITIAL_SYNC_RETRY_TIME_IN_MS * 1.1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 }
1355
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001356 // Cap the delay.
Jeff Sharkey625239a2012-09-26 22:03:49 -07001357 long maxSyncRetryTimeInSeconds = Settings.Global.getLong(mContext.getContentResolver(),
1358 Settings.Global.SYNC_MAX_RETRY_DELAY_IN_SECONDS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS);
1360 if (newDelayInMs > maxSyncRetryTimeInSeconds * 1000) {
1361 newDelayInMs = maxSyncRetryTimeInSeconds * 1000;
1362 }
1363
Alon Albertc1ac7762010-10-28 13:35:55 -07001364 final long backoff = now + newDelayInMs;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001365 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1366 Slog.v(TAG, "Backoff until: " + backoff + ", delayTime: " + newDelayInMs);
1367 }
1368 mSyncStorageEngine.setBackoff(target, backoff, newDelayInMs);
Makoto Onukia9dca242017-06-21 17:06:49 -07001369 rescheduleSyncs(target, "increaseBackoffSetting");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001370 }
Alon Albertc1ac7762010-10-28 13:35:55 -07001371
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001372 /**
1373 * Reschedule all scheduled syncs for this EndPoint. The syncs will be scheduled according
1374 * to current backoff and delayUntil values of this EndPoint.
1375 */
Makoto Onukia9dca242017-06-21 17:06:49 -07001376 private void rescheduleSyncs(EndPoint target, String why) {
1377 mLogger.log("rescheduleSyncs() ep=", target, " why=", why);
1378
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001379 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001380 int count = 0;
1381 for (SyncOperation op: ops) {
1382 if (!op.isPeriodic && op.target.matchesSpec(target)) {
1383 count++;
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001384 cancelJob(op, why);
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001385 postScheduleSyncMessage(op, 0 /* min delay */);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001386 }
1387 }
1388 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1389 Slog.v(TAG, "Rescheduled " + count + " syncs for " + target);
Fred Quintana918339a2010-10-05 14:00:39 -07001390 }
Fred Quintana307da1a2010-01-21 14:24:20 -08001391 }
1392
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001393 private void setDelayUntilTime(EndPoint target, long delayUntilSeconds) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001394 final long delayUntil = delayUntilSeconds * 1000;
1395 final long absoluteNow = System.currentTimeMillis();
1396 long newDelayUntilTime;
1397 if (delayUntil > absoluteNow) {
1398 newDelayUntilTime = SystemClock.elapsedRealtime() + (delayUntil - absoluteNow);
1399 } else {
1400 newDelayUntilTime = 0;
1401 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001402 mSyncStorageEngine.setDelayUntilTime(target, newDelayUntilTime);
1403 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1404 Slog.v(TAG, "Delay Until time set to " + newDelayUntilTime + " for " + target);
Fred Quintana918339a2010-10-05 14:00:39 -07001405 }
Makoto Onukia9dca242017-06-21 17:06:49 -07001406 rescheduleSyncs(target, "delayUntil newDelayUntilTime: " + newDelayUntilTime);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001407 }
1408
1409 private boolean isAdapterDelayed(EndPoint target) {
1410 long now = SystemClock.elapsedRealtime();
1411 Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target);
1412 if (backoff != null && backoff.first != SyncStorageEngine.NOT_IN_BACKOFF_MODE
1413 && backoff.first > now) {
1414 return true;
1415 }
1416 if (mSyncStorageEngine.getDelayUntilTime(target) > now) {
1417 return true;
1418 }
1419 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001420 }
1421
1422 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001423 * Cancel the active sync if it matches the target.
1424 * @param info object containing info about which syncs to cancel. The target can
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001425 * have null account/provider info to specify all accounts/providers.
1426 * @param extras if non-null, specifies the exact sync to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001427 */
Makoto Onukia9dca242017-06-21 17:06:49 -07001428 public void cancelActiveSync(SyncStorageEngine.EndPoint info, Bundle extras, String why) {
1429 sendCancelSyncsMessage(info, extras, why);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 }
1431
1432 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001433 * Schedule a sync operation with JobScheduler.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001435 private void scheduleSyncOperationH(SyncOperation syncOperation) {
1436 scheduleSyncOperationH(syncOperation, 0L);
1437 }
1438
1439 private void scheduleSyncOperationH(SyncOperation syncOperation, long minDelay) {
1440 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
1441 if (syncOperation == null) {
1442 Slog.e(TAG, "Can't schedule null sync operation.");
1443 return;
1444 }
1445 if (!syncOperation.ignoreBackoff()) {
1446 Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(syncOperation.target);
1447 if (backoff == null) {
1448 Slog.e(TAG, "Couldn't find backoff values for " + syncOperation.target);
1449 backoff = new Pair<Long, Long>(SyncStorageEngine.NOT_IN_BACKOFF_MODE,
1450 SyncStorageEngine.NOT_IN_BACKOFF_MODE);
1451 }
1452 long now = SystemClock.elapsedRealtime();
1453 long backoffDelay = backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE ? 0
1454 : backoff.first - now;
1455 long delayUntil = mSyncStorageEngine.getDelayUntilTime(syncOperation.target);
1456 long delayUntilDelay = delayUntil > now ? delayUntil - now : 0;
1457 if (isLoggable) {
1458 Slog.v(TAG, "backoff delay:" + backoffDelay
1459 + " delayUntil delay:" + delayUntilDelay);
1460 }
1461 minDelay = Math.max(minDelay, Math.max(backoffDelay, delayUntilDelay));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 }
1463
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001464 if (minDelay < 0) {
1465 minDelay = 0;
1466 }
1467
1468 // Check if duplicate syncs are pending. If found, keep one with least expected run time.
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001469 if (!syncOperation.isPeriodic) {
1470 // Check currently running syncs
1471 for (ActiveSyncContext asc: mActiveSyncContexts) {
1472 if (asc.mSyncOperation.key.equals(syncOperation.key)) {
1473 if (isLoggable) {
1474 Log.v(TAG, "Duplicate sync is already running. Not scheduling "
1475 + syncOperation);
1476 }
1477 return;
1478 }
1479 }
1480
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001481 int duplicatesCount = 0;
1482 long now = SystemClock.elapsedRealtime();
1483 syncOperation.expectedRuntime = now + minDelay;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001484 List<SyncOperation> pending = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001485 SyncOperation opWithLeastExpectedRuntime = syncOperation;
1486 for (SyncOperation op : pending) {
1487 if (op.isPeriodic) {
1488 continue;
1489 }
1490 if (op.key.equals(syncOperation.key)) {
1491 if (opWithLeastExpectedRuntime.expectedRuntime > op.expectedRuntime) {
1492 opWithLeastExpectedRuntime = op;
1493 }
1494 duplicatesCount++;
1495 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001497 if (duplicatesCount > 1) {
1498 Slog.e(TAG, "FATAL ERROR! File a bug if you see this.");
1499 }
1500 for (SyncOperation op : pending) {
1501 if (op.isPeriodic) {
1502 continue;
1503 }
1504 if (op.key.equals(syncOperation.key)) {
1505 if (op != opWithLeastExpectedRuntime) {
1506 if (isLoggable) {
1507 Slog.v(TAG, "Cancelling duplicate sync " + op);
1508 }
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001509 cancelJob(op, "scheduleSyncOperationH-duplicate");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001510 }
1511 }
1512 }
1513 if (opWithLeastExpectedRuntime != syncOperation) {
1514 // Don't schedule because a duplicate sync with earlier expected runtime exists.
1515 if (isLoggable) {
1516 Slog.v(TAG, "Not scheduling because a duplicate exists.");
1517 }
1518 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
1520 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001521
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001522 // Syncs that are re-scheduled shouldn't get a new job id.
1523 if (syncOperation.jobId == SyncOperation.NO_JOB_ID) {
1524 syncOperation.jobId = getUnusedJobIdH();
1525 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001526
1527 if (isLoggable) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001528 Slog.v(TAG, "scheduling sync operation " + syncOperation.toString());
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001529 }
1530
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001531 int priority = syncOperation.findPriority();
1532
1533 final int networkType = syncOperation.isNotAllowedOnMetered() ?
1534 JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY;
1535
1536 JobInfo.Builder b = new JobInfo.Builder(syncOperation.jobId,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001537 new ComponentName(mContext, SyncJobService.class))
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001538 .setExtras(syncOperation.toJobInfoExtras())
1539 .setRequiredNetworkType(networkType)
1540 .setPersisted(true)
1541 .setPriority(priority);
1542
1543 if (syncOperation.isPeriodic) {
1544 b.setPeriodic(syncOperation.periodMillis, syncOperation.flexMillis);
1545 } else {
1546 if (minDelay > 0) {
1547 b.setMinimumLatency(minDelay);
1548 }
1549 getSyncStorageEngine().markPending(syncOperation.target, true);
1550 }
1551
1552 if (syncOperation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_REQUIRE_CHARGING)) {
1553 b.setRequiresCharging(true);
1554 }
1555
1556 getJobScheduler().scheduleAsPackage(b.build(), syncOperation.owningPackage,
Shreyas Basargeeda34e42016-04-26 00:14:02 +01001557 syncOperation.target.userId, syncOperation.wakeLockName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001558 }
1559
1560 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07001561 * Remove scheduled sync operations.
Matthew Williams8ef22042013-07-26 12:56:39 -07001562 * @param info limit the removals to operations that match this target. The target can
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001563 * have null account/provider info to specify all accounts/providers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001565 public void clearScheduledSyncOperations(SyncStorageEngine.EndPoint info) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001566 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001567 for (SyncOperation op: ops) {
1568 if (!op.isPeriodic && op.target.matchesSpec(info)) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001569 cancelJob(op, "clearScheduledSyncOperations");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001570 getSyncStorageEngine().markPending(op.target, false);
1571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001573 mSyncStorageEngine.setBackoff(info,
Fred Quintana918339a2010-10-05 14:00:39 -07001574 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001575 }
1576
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001577 /**
1578 * Remove a specified sync, if it exists.
1579 * @param info Authority for which the sync is to be removed.
1580 * @param extras extras bundle to uniquely identify sync.
1581 */
1582 public void cancelScheduledSyncOperation(SyncStorageEngine.EndPoint info, Bundle extras) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001583 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001584 for (SyncOperation op: ops) {
1585 if (!op.isPeriodic && op.target.matchesSpec(info)
1586 && syncExtrasEquals(extras, op.extras, false)) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001587 cancelJob(op, "cancelScheduledSyncOperation");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001588 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001589 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001590 setAuthorityPendingState(info);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001591 // Reset the back-off if there are no more syncs pending.
1592 if (!mSyncStorageEngine.isSyncPending(info)) {
1593 mSyncStorageEngine.setBackoff(info,
1594 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
1595 }
1596 }
1597
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001598 private void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) {
1599 final boolean isLoggable = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 if (isLoggable) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001601 Log.d(TAG, "encountered error(s) during the sync: " + syncResult + ", " + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 }
1603
Fred Quintana53bd2522010-02-05 15:28:12 -08001604 // The SYNC_EXTRAS_IGNORE_BACKOFF only applies to the first attempt to sync a given
1605 // request. Retries of the request will always honor the backoff, so clear the
1606 // flag in case we retry this request.
1607 if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)) {
1608 operation.extras.remove(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF);
1609 }
1610
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01001611 if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
1612 && !syncResult.syncAlreadyInProgress) {
1613 // syncAlreadyInProgress flag is set by AbstractThreadedSyncAdapter. The sync adapter
1614 // has no way of knowing that a sync error occured. So we DO retry if the error is
1615 // syncAlreadyInProgress.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001616 if (isLoggable) {
1617 Log.d(TAG, "not retrying sync operation because SYNC_EXTRAS_DO_NOT_RETRY was specified "
1618 + operation);
1619 }
Fred Quintana918339a2010-10-05 14:00:39 -07001620 } else if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false)
1621 && !syncResult.syncAlreadyInProgress) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001622 // If this was an upward sync then schedule a two-way sync immediately.
Fred Quintana53bd2522010-02-05 15:28:12 -08001623 operation.extras.remove(ContentResolver.SYNC_EXTRAS_UPLOAD);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001624 if (isLoggable) {
1625 Log.d(TAG, "retrying sync operation as a two-way sync because an upload-only sync "
1626 + "encountered an error: " + operation);
1627 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001628 scheduleSyncOperationH(operation);
Fred Quintana307da1a2010-01-21 14:24:20 -08001629 } else if (syncResult.tooManyRetries) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001630 // If this sync aborted because the internal sync loop retried too many times then
1631 // don't reschedule. Otherwise we risk getting into a retry loop.
1632 if (isLoggable) {
1633 Log.d(TAG, "not retrying sync operation because it retried too many times: "
1634 + operation);
1635 }
Fred Quintanaaa7edda2009-12-03 14:18:58 -08001636 } else if (syncResult.madeSomeProgress()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001637 // If the operation succeeded to some extent then retry immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001638 if (isLoggable) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001639 Log.d(TAG, "retrying sync operation because even though it had an error "
1640 + "it achieved some success");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001641 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001642 scheduleSyncOperationH(operation);
Fred Quintana8570f742010-02-18 10:32:54 -08001643 } else if (syncResult.syncAlreadyInProgress) {
1644 if (isLoggable) {
1645 Log.d(TAG, "retrying sync operation that failed because there was already a "
1646 + "sync in progress: " + operation);
1647 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001648 scheduleSyncOperationH(operation, DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 } else if (syncResult.hasSoftError()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001650 // If this was a two-way sync then retry soft errors with an exponential backoff.
Fred Quintana307da1a2010-01-21 14:24:20 -08001651 if (isLoggable) {
1652 Log.d(TAG, "retrying sync operation because it encountered a soft error: "
1653 + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001655 scheduleSyncOperationH(operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 } else {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001657 // Otherwise do not reschedule.
Fred Quintana307da1a2010-01-21 14:24:20 -08001658 Log.d(TAG, "not retrying sync operation because the error is a hard error: "
1659 + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001660 }
1661 }
1662
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07001663 private void onUserUnlocked(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001664 // Make sure that accounts we're about to use are valid.
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001665 AccountManagerService.getSingleton().validateAccounts(userId);
1666
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001667 mSyncAdapters.invalidateCache(userId);
1668
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001669 EndPoint target = new EndPoint(null, null, userId);
1670 updateRunningAccounts(target);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001671
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001672 // Schedule sync for any accounts under started user.
Svetoslavf3f02ac2015-09-08 14:36:35 -07001673 final Account[] accounts = AccountManagerService.getSingleton().getAccounts(userId,
1674 mContext.getOpPackageName());
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001675 for (Account account : accounts) {
Alon Albert57286f92012-10-09 14:21:38 -07001676 scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null,
Svet Ganovf6d424f12016-09-20 20:18:53 -07001677 AuthorityInfo.NOT_INITIALIZED);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001678 }
Alon Albert8e285552012-09-17 15:05:27 -07001679 }
Amith Yamasani13593602012-03-22 16:16:17 -07001680
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -07001681 private void onUserStopped(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001682 updateRunningAccounts(null /* Don't sync any target */);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001683
1684 cancelActiveSync(
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001685 new SyncStorageEngine.EndPoint(
1686 null /* any account */,
1687 null /* any authority */,
1688 userId),
Makoto Onukia9dca242017-06-21 17:06:49 -07001689 null /* any sync. */,
1690 "onUserStopped"
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001691 );
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001692 }
1693
1694 private void onUserRemoved(int userId) {
Makoto Onukibbf6d8c2017-08-11 12:11:39 -07001695 mLogger.log("onUserRemoved: u", userId);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001696 updateRunningAccounts(null /* Don't sync any target */);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001697
Amith Yamasani13593602012-03-22 16:16:17 -07001698 // Clean up the storage engine database
1699 mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId);
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001700 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001701 for (SyncOperation op: ops) {
1702 if (op.target.userId == userId) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -07001703 cancelJob(op, "user removed u" + userId);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001704 }
1705 }
1706 }
1707
Amith Yamasani96a0fd652015-04-10 16:16:30 -07001708 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001709 * @hide
1710 */
Alon Alberteca75112010-12-08 15:02:33 -08001711 class ActiveSyncContext extends ISyncContext.Stub
1712 implements ServiceConnection, IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 final SyncOperation mSyncOperation;
1714 final long mHistoryRowId;
Fred Quintana718d8a22009-04-29 17:53:20 -07001715 ISyncAdapter mSyncAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 final long mStartTime;
1717 long mTimeoutStartTime;
Fred Quintana3ec47302010-03-10 10:08:31 -08001718 boolean mBound;
Fred Quintana918339a2010-10-05 14:00:39 -07001719 final PowerManager.WakeLock mSyncWakeLock;
1720 final int mSyncAdapterUid;
1721 SyncInfo mSyncInfo;
Alon Alberteca75112010-12-08 15:02:33 -08001722 boolean mIsLinkedToDeath = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001723 String mEventName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001724
Matthew Williams1967c8d2015-06-19 19:03:13 -07001725 /** Total bytes transferred, counted at {@link #mLastPolledTimeElapsed} */
1726 long mBytesTransferredAtLastPoll;
1727 /**
1728 * Last point in {@link SystemClock#elapsedRealtime()} at which we checked the # of bytes
1729 * transferred to/fro by this adapter.
1730 */
1731 long mLastPolledTimeElapsed;
1732
Fred Quintana918339a2010-10-05 14:00:39 -07001733 /**
1734 * Create an ActiveSyncContext for an impending sync and grab the wakelock for that
1735 * sync adapter. Since this grabs the wakelock you need to be sure to call
1736 * close() when you are done with this ActiveSyncContext, whether the sync succeeded
1737 * or not.
1738 * @param syncOperation the SyncOperation we are about to sync
1739 * @param historyRowId the row in which to record the history info for this sync
1740 * @param syncAdapterUid the UID of the application that contains the sync adapter
1741 * for this sync. This is used to attribute the wakelock hold to that application.
1742 */
1743 public ActiveSyncContext(SyncOperation syncOperation, long historyRowId,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001744 int syncAdapterUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001745 super();
Fred Quintana918339a2010-10-05 14:00:39 -07001746 mSyncAdapterUid = syncAdapterUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 mSyncOperation = syncOperation;
1748 mHistoryRowId = historyRowId;
Fred Quintana718d8a22009-04-29 17:53:20 -07001749 mSyncAdapter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 mStartTime = SystemClock.elapsedRealtime();
1751 mTimeoutStartTime = mStartTime;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001752 mSyncWakeLock = mSyncHandler.getSyncWakeLock(mSyncOperation);
Fred Quintana918339a2010-10-05 14:00:39 -07001753 mSyncWakeLock.setWorkSource(new WorkSource(syncAdapterUid));
1754 mSyncWakeLock.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001755 }
1756
1757 public void sendHeartbeat() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001758 // Heartbeats are no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 }
1760
1761 public void onFinished(SyncResult result) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001762 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "onFinished: " + this);
1763 // Include "this" in the message so that the handler can ignore it if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 // ActiveSyncContext is no longer the mActiveSyncContext at message handling
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001765 // time.
Makoto Onuki6a6ae042017-07-20 13:30:12 -07001766 mLogger.log("onFinished result=", result, " endpoint=",
1767 (mSyncOperation == null ? "null" : mSyncOperation.target));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 sendSyncFinishedOrCanceledMessage(this, result);
1769 }
1770
1771 public void toString(StringBuilder sb) {
1772 sb.append("startTime ").append(mStartTime)
1773 .append(", mTimeoutStartTime ").append(mTimeoutStartTime)
1774 .append(", mHistoryRowId ").append(mHistoryRowId)
1775 .append(", syncOperation ").append(mSyncOperation);
1776 }
1777
Fred Quintana718d8a22009-04-29 17:53:20 -07001778 public void onServiceConnected(ComponentName name, IBinder service) {
1779 Message msg = mSyncHandler.obtainMessage();
1780 msg.what = SyncHandler.MESSAGE_SERVICE_CONNECTED;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001781 msg.obj = new ServiceConnectionData(this, service);
Fred Quintana718d8a22009-04-29 17:53:20 -07001782 mSyncHandler.sendMessage(msg);
1783 }
1784
1785 public void onServiceDisconnected(ComponentName name) {
1786 Message msg = mSyncHandler.obtainMessage();
1787 msg.what = SyncHandler.MESSAGE_SERVICE_DISCONNECTED;
1788 msg.obj = new ServiceConnectionData(this, null);
1789 mSyncHandler.sendMessage(msg);
1790 }
1791
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001792 boolean bindToSyncAdapter(ComponentName serviceComponent, int userId) {
Fred Quintana718d8a22009-04-29 17:53:20 -07001793 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001794 Log.d(TAG, "bindToSyncAdapter: " + serviceComponent + ", connection " + this);
Fred Quintana718d8a22009-04-29 17:53:20 -07001795 }
1796 Intent intent = new Intent();
1797 intent.setAction("android.content.SyncAdapter");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001798 intent.setComponent(serviceComponent);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001799 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1800 com.android.internal.R.string.sync_binding_label);
Dianne Hackborn41203752012-08-31 14:05:51 -07001801 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
1802 mContext, 0, new Intent(Settings.ACTION_SYNC_SETTINGS), 0,
1803 null, new UserHandle(userId)));
Fred Quintana3ec47302010-03-10 10:08:31 -08001804 mBound = true;
Amith Yamasani27b89e62013-01-16 12:30:11 -08001805 final boolean bindResult = mContext.bindServiceAsUser(intent, this,
Dianne Hackborne02c88a2011-10-28 13:58:15 -07001806 Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001807 | Context.BIND_ALLOW_OOM_MANAGEMENT,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001808 new UserHandle(mSyncOperation.target.userId));
Makoto Onuki6a6ae042017-07-20 13:30:12 -07001809 mLogger.log("bindService() returned=", mBound, " for ", this);
Fred Quintana3ec47302010-03-10 10:08:31 -08001810 if (!bindResult) {
1811 mBound = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001812 } else {
1813 try {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001814 mEventName = mSyncOperation.wakeLockName();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001815 mBatteryStats.noteSyncStart(mEventName, mSyncAdapterUid);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001816 } catch (RemoteException e) {
1817 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001818 }
1819 return bindResult;
Fred Quintana718d8a22009-04-29 17:53:20 -07001820 }
1821
Fred Quintana918339a2010-10-05 14:00:39 -07001822 /**
1823 * Performs the required cleanup, which is the releasing of the wakelock and
1824 * unbinding from the sync adapter (if actually bound).
1825 */
Fred Quintana3ec47302010-03-10 10:08:31 -08001826 protected void close() {
Fred Quintana718d8a22009-04-29 17:53:20 -07001827 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1828 Log.d(TAG, "unBindFromSyncAdapter: connection " + this);
1829 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001830 if (mBound) {
1831 mBound = false;
Makoto Onuki6a6ae042017-07-20 13:30:12 -07001832 mLogger.log("unbindService for ", this);
Fred Quintana3ec47302010-03-10 10:08:31 -08001833 mContext.unbindService(this);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001834 try {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001835 mBatteryStats.noteSyncFinish(mEventName, mSyncAdapterUid);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001836 } catch (RemoteException e) {
1837 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001838 }
Fred Quintana918339a2010-10-05 14:00:39 -07001839 mSyncWakeLock.release();
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001840 mSyncWakeLock.setWorkSource(null);
Fred Quintana718d8a22009-04-29 17:53:20 -07001841 }
1842
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 public String toString() {
1844 StringBuilder sb = new StringBuilder();
1845 toString(sb);
1846 return sb.toString();
1847 }
Alon Alberteca75112010-12-08 15:02:33 -08001848
1849 @Override
1850 public void binderDied() {
1851 sendSyncFinishedOrCanceledMessage(this, null);
1852 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
Makoto Onukia9dca242017-06-21 17:06:49 -07001855 protected void dump(FileDescriptor fd, PrintWriter pw, boolean dumpAll) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001856 final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
1857 dumpSyncState(ipw);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001858 dumpSyncAdapters(ipw);
Makoto Onukia9dca242017-06-21 17:06:49 -07001859
1860 if (dumpAll) {
1861 ipw.println("Detailed Sync History");
1862 mLogger.dumpAll(pw);
1863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 }
1865
Dianne Hackborn231cc602009-04-27 17:10:36 -07001866 static String formatTime(long time) {
1867 Time tobj = new Time();
1868 tobj.set(time);
1869 return tobj.format("%Y-%m-%d %H:%M:%S");
1870 }
Doug Zongker44f57472009-09-20 15:52:43 -07001871
Makoto Onuki15e7a252017-06-08 17:12:05 -07001872 private final static Comparator<SyncOperation> sOpDumpComparator = (op1, op2) -> {
1873 int res = Integer.compare(op1.target.userId, op2.target.userId);
1874 if (res != 0) return res;
1875
1876 final Comparator<String> stringComparator = String.CASE_INSENSITIVE_ORDER;
1877
1878 res = stringComparator.compare(op1.target.account.type, op2.target.account.type);
1879 if (res != 0) return res;
1880
1881 res = stringComparator.compare(op1.target.account.name, op2.target.account.name);
1882 if (res != 0) return res;
1883
1884 res = stringComparator.compare(op1.target.provider, op2.target.provider);
1885 if (res != 0) return res;
1886
1887 res = Integer.compare(op1.reason, op2.reason);
1888 if (res != 0) return res;
1889
1890 res = Long.compare(op1.periodMillis, op2.periodMillis);
1891 if (res != 0) return res;
1892
1893 res = Long.compare(op1.expectedRuntime, op2.expectedRuntime);
1894 if (res != 0) return res;
1895
1896 res = Long.compare(op1.jobId, op2.jobId);
1897 if (res != 0) return res;
1898
1899 return 0;
1900 };
1901
1902 private final static Comparator<SyncOperation> sOpRuntimeComparator = (op1, op2) -> {
1903 int res = Long.compare(op1.expectedRuntime, op2.expectedRuntime);
1904 if (res != 0) return res;
1905
1906 return sOpDumpComparator.compare(op1, op2);
1907 };
1908
1909 private static <T> int countIf(Collection<T> col, Predicate<T> p) {
1910 int ret = 0;
1911 for (T item : col) {
1912 if (p.test(item)) ret++;
1913 }
1914 return ret;
1915 }
1916
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001917 protected void dumpPendingSyncs(PrintWriter pw) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001918 List<SyncOperation> pendingSyncs = getAllPendingSyncs();
Makoto Onuki15e7a252017-06-08 17:12:05 -07001919
1920 pw.print("Pending Syncs: ");
1921 pw.println(countIf(pendingSyncs, op -> !op.isPeriodic));
1922
1923 Collections.sort(pendingSyncs, sOpRuntimeComparator);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001924 int count = 0;
1925 for (SyncOperation op: pendingSyncs) {
1926 if (!op.isPeriodic) {
1927 pw.println(op.dump(null, false));
1928 count++;
1929 }
1930 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001931 pw.println();
1932 }
1933
1934 protected void dumpPeriodicSyncs(PrintWriter pw) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001935 List<SyncOperation> pendingSyncs = getAllPendingSyncs();
Makoto Onuki15e7a252017-06-08 17:12:05 -07001936
1937 pw.print("Periodic Syncs: ");
1938 pw.println(countIf(pendingSyncs, op -> op.isPeriodic));
1939
1940 Collections.sort(pendingSyncs, sOpDumpComparator);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001941 int count = 0;
1942 for (SyncOperation op: pendingSyncs) {
1943 if (op.isPeriodic) {
1944 pw.println(op.dump(null, false));
1945 count++;
1946 }
1947 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001948 pw.println();
1949 }
1950
Makoto Onuki15e7a252017-06-08 17:12:05 -07001951 /**
1952 * Similar to {@link android.util.TimeUtils#formatDuration}, but it's more suitable and concise
1953 * for the sync manager dumpsys. (Don't add the leading + sign, don't show milliseconds.)
1954 */
1955 public static StringBuilder formatDurationHMS(StringBuilder sb, long duration) {
1956 duration /= 1000;
1957 if (duration < 0) {
1958 sb.append('-');
1959 duration = -duration;
1960 }
1961 final long seconds = duration % 60;
1962 duration /= 60;
1963
1964 final long minutes = duration % 60;
1965 duration /= 60;
1966
1967 final long hours = duration % 24;
1968 duration /= 24;
1969
1970 final long days = duration;
1971
1972 boolean print = false;
1973 if (days > 0) {
1974 sb.append(days);
1975 sb.append('d');
1976 print = true;
1977 }
1978 print = printTwoDigitNumber(sb, hours, 'h', print);
1979 print = printTwoDigitNumber(sb, minutes, 'm', print);
1980 print = printTwoDigitNumber(sb, seconds, 's', print);
1981 if (!print) {
1982 sb.append("0s");
1983 }
1984
1985 return sb;
1986 }
1987
1988 private static boolean printTwoDigitNumber(StringBuilder sb, long value, char unit,
1989 boolean always) {
1990 if (!always && (value == 0)) {
1991 return false;
1992 }
1993 if (always && (value < 10)) {
1994 sb.append('0');
1995 }
1996 sb.append(value);
1997 sb.append(unit);
1998 return true;
1999 }
2000
Alon Alberte0bde332011-09-22 14:26:16 -07002001 protected void dumpSyncState(PrintWriter pw) {
Makoto Onuki15e7a252017-06-08 17:12:05 -07002002 final StringBuilder sb = new StringBuilder();
2003
Dianne Hackborn231cc602009-04-27 17:10:36 -07002004 pw.print("data connected: "); pw.println(mDataConnectionIsConnected);
Amith Yamasani04e0d262012-02-14 11:50:53 -08002005 pw.print("auto sync: ");
2006 List<UserInfo> users = getAllUsers();
2007 if (users != null) {
2008 for (UserInfo user : users) {
2009 pw.print("u" + user.id + "="
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002010 + mSyncStorageEngine.getMasterSyncAutomatically(user.id) + " ");
Amith Yamasani04e0d262012-02-14 11:50:53 -08002011 }
2012 pw.println();
2013 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07002014 pw.print("memory low: "); pw.println(mStorageIsLow);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07002015 pw.print("device idle: "); pw.println(mDeviceIsIdle);
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002016 pw.print("reported active: "); pw.println(mReportedSyncActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017
Jeff Sharkey6eb96202012-10-10 13:13:54 -07002018 final AccountAndUser[] accounts = AccountManagerService.getSingleton().getAllAccounts();
Amith Yamasani04e0d262012-02-14 11:50:53 -08002019
Jeff Sharkey6eb96202012-10-10 13:13:54 -07002020 pw.print("accounts: ");
Fred Quintana53bd2522010-02-05 15:28:12 -08002021 if (accounts != INITIAL_ACCOUNTS_ARRAY) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002022 pw.println(accounts.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 } else {
Fred Quintana53bd2522010-02-05 15:28:12 -08002024 pw.println("not known yet");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002025 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 final long now = SystemClock.elapsedRealtime();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002027 pw.print("now: "); pw.print(now);
2028 pw.println(" (" + formatTime(System.currentTimeMillis()) + ")");
Makoto Onuki15e7a252017-06-08 17:12:05 -07002029
2030 sb.setLength(0);
2031 pw.print("uptime: "); pw.print(formatDurationHMS(sb, now));
2032 pw.println();
Dianne Hackborn231cc602009-04-27 17:10:36 -07002033 pw.print("time spent syncing: ");
Makoto Onuki15e7a252017-06-08 17:12:05 -07002034
2035 sb.setLength(0);
2036 pw.print(formatDurationHMS(sb,
2037 mSyncHandler.mSyncTimeTracker.timeSpentSyncing()));
2038 pw.print(", sync ");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002039 pw.print(mSyncHandler.mSyncTimeTracker.mLastWasSyncing ? "" : "not ");
2040 pw.println("in progress");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041
Fred Quintana918339a2010-10-05 14:00:39 -07002042 pw.println();
2043 pw.println("Active Syncs: " + mActiveSyncContexts.size());
Alon Albert57286f92012-10-09 14:21:38 -07002044 final PackageManager pm = mContext.getPackageManager();
Fred Quintana918339a2010-10-05 14:00:39 -07002045 for (SyncManager.ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
Makoto Onuki15e7a252017-06-08 17:12:05 -07002046 final long durationInSeconds = (now - activeSyncContext.mStartTime);
Fred Quintana918339a2010-10-05 14:00:39 -07002047 pw.print(" ");
Makoto Onuki15e7a252017-06-08 17:12:05 -07002048 sb.setLength(0);
2049 pw.print(formatDurationHMS(sb, durationInSeconds));
Fred Quintana918339a2010-10-05 14:00:39 -07002050 pw.print(" - ");
Alon Albert57286f92012-10-09 14:21:38 -07002051 pw.print(activeSyncContext.mSyncOperation.dump(pm, false));
Fred Quintana918339a2010-10-05 14:00:39 -07002052 pw.println();
2053 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07002054 pw.println();
2055
2056 dumpPendingSyncs(pw);
2057 dumpPeriodicSyncs(pw);
Fred Quintana918339a2010-10-05 14:00:39 -07002058
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002059 // Join the installed sync adapter with the accounts list and emit for everything.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002060 pw.println("Sync Status");
Makoto Onuki15e7a252017-06-08 17:12:05 -07002061
2062 final ArrayList<Pair<EndPoint, SyncStatusInfo>> statuses = new ArrayList<>();
2063
Amith Yamasani04e0d262012-02-14 11:50:53 -08002064 for (AccountAndUser account : accounts) {
Alon Albert57286f92012-10-09 14:21:38 -07002065 pw.printf("Account %s u%d %s\n",
2066 account.account.name, account.userId, account.account.type);
2067
2068 pw.println("=======================================================================");
Makoto Onuki010291d2017-06-06 16:32:47 -07002069 final PrintTable table = new PrintTable(13);
Alon Albert57286f92012-10-09 14:21:38 -07002070 table.set(0, 0,
2071 "Authority", // 0
2072 "Syncable", // 1
2073 "Enabled", // 2
2074 "Delay", // 3
2075 "Loc", // 4
2076 "Poll", // 5
2077 "Per", // 6
2078 "Serv", // 7
2079 "User", // 8
2080 "Tot", // 9
2081 "Time", // 10
Makoto Onuki010291d2017-06-06 16:32:47 -07002082 "Last Sync", // 11
Makoto Onuki15e7a252017-06-08 17:12:05 -07002083 "Backoff" // 12
Alon Albert57286f92012-10-09 14:21:38 -07002084 );
2085
2086 final List<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> sorted =
2087 Lists.newArrayList();
2088 sorted.addAll(mSyncAdapters.getAllServices(account.userId));
2089 Collections.sort(sorted,
2090 new Comparator<RegisteredServicesCache.ServiceInfo<SyncAdapterType>>() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002091 @Override
2092 public int compare(RegisteredServicesCache.ServiceInfo<SyncAdapterType> lhs,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002093 RegisteredServicesCache.ServiceInfo<SyncAdapterType> rhs) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002094 return lhs.type.authority.compareTo(rhs.type.authority);
2095 }
2096 });
Alon Albert57286f92012-10-09 14:21:38 -07002097 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterType : sorted) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08002098 if (!syncAdapterType.type.accountType.equals(account.account.type)) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002099 continue;
2100 }
Alon Albert57286f92012-10-09 14:21:38 -07002101 int row = table.getNumRows();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002102 Pair<AuthorityInfo, SyncStatusInfo> syncAuthoritySyncStatus =
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07002103 mSyncStorageEngine.getCopyOfAuthorityWithSyncStatus(
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002104 new SyncStorageEngine.EndPoint(
2105 account.account,
2106 syncAdapterType.type.authority,
2107 account.userId));
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07002108 SyncStorageEngine.AuthorityInfo settings = syncAuthoritySyncStatus.first;
2109 SyncStatusInfo status = syncAuthoritySyncStatus.second;
Makoto Onuki15e7a252017-06-08 17:12:05 -07002110 statuses.add(Pair.create(settings.target, status));
Matthew Williams8ef22042013-07-26 12:56:39 -07002111 String authority = settings.target.provider;
Alon Albert57286f92012-10-09 14:21:38 -07002112 if (authority.length() > 50) {
2113 authority = authority.substring(authority.length() - 50);
2114 }
2115 table.set(row, 0, authority, settings.syncable, settings.enabled);
Makoto Onuki15e7a252017-06-08 17:12:05 -07002116
2117 sb.setLength(0);
Alon Albert57286f92012-10-09 14:21:38 -07002118 table.set(row, 4,
2119 status.numSourceLocal,
2120 status.numSourcePoll,
2121 status.numSourcePeriodic,
2122 status.numSourceServer,
2123 status.numSourceUser,
2124 status.numSyncs,
Makoto Onuki15e7a252017-06-08 17:12:05 -07002125 formatDurationHMS(sb, status.totalElapsedTime));
Alon Albert57286f92012-10-09 14:21:38 -07002126
Alon Albert57286f92012-10-09 14:21:38 -07002127 int row1 = row;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002128 if (settings.delayUntil > now) {
Alon Albert57286f92012-10-09 14:21:38 -07002129 table.set(row1++, 12, "D: " + (settings.delayUntil - now) / 1000);
2130 if (settings.backoffTime > now) {
2131 table.set(row1++, 12, "B: " + (settings.backoffTime - now) / 1000);
2132 table.set(row1++, 12, settings.backoffDelay / 1000);
2133 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002134 }
Alon Albert57286f92012-10-09 14:21:38 -07002135
Makoto Onuki010291d2017-06-06 16:32:47 -07002136 row1 = row;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002137 if (status.lastSuccessTime != 0) {
Alon Albert57286f92012-10-09 14:21:38 -07002138 table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastSuccessSource]
2139 + " " + "SUCCESS");
2140 table.set(row1++, 11, formatTime(status.lastSuccessTime));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08002141 }
2142 if (status.lastFailureTime != 0) {
Alon Albert57286f92012-10-09 14:21:38 -07002143 table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastFailureSource]
2144 + " " + "FAILURE");
2145 table.set(row1++, 11, formatTime(status.lastFailureTime));
2146 //noinspection UnusedAssignment
2147 table.set(row1++, 11, status.lastFailureMesg);
Dianne Hackborn231cc602009-04-27 17:10:36 -07002148 }
2149 }
Alon Albert57286f92012-10-09 14:21:38 -07002150 table.writeTo(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07002152
2153 dumpSyncHistory(pw);
2154
2155 pw.println();
2156 pw.println("Per Adapter History");
2157
2158 for (int i = 0; i < statuses.size(); i++) {
2159 final Pair<EndPoint, SyncStatusInfo> event = statuses.get(i);
2160
2161 pw.print(" ");
2162 pw.print(event.first.account.name);
2163 pw.print('/');
2164 pw.print(event.first.account.type);
2165 pw.print(" u");
2166 pw.print(event.first.userId);
2167 pw.print(" [");
2168 pw.print(event.first.provider);
2169 pw.print("]");
2170 pw.println();
2171
2172 for (int j = 0; j < event.second.getEventCount(); j++) {
2173 pw.print(" ");
2174 pw.print(formatTime(event.second.getEventTime(j)));
2175 pw.print(' ');
2176 pw.print(event.second.getEvent(j));
2177 pw.println();
2178 }
2179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 }
2181
Dianne Hackborn231cc602009-04-27 17:10:36 -07002182 private void dumpTimeSec(PrintWriter pw, long time) {
2183 pw.print(time/1000); pw.print('.'); pw.print((time/100)%10);
2184 pw.print('s');
2185 }
Doug Zongker44f57472009-09-20 15:52:43 -07002186
Dianne Hackborn231cc602009-04-27 17:10:36 -07002187 private void dumpDayStatistic(PrintWriter pw, SyncStorageEngine.DayStats ds) {
2188 pw.print("Success ("); pw.print(ds.successCount);
2189 if (ds.successCount > 0) {
2190 pw.print(" for "); dumpTimeSec(pw, ds.successTime);
2191 pw.print(" avg="); dumpTimeSec(pw, ds.successTime/ds.successCount);
2192 }
2193 pw.print(") Failure ("); pw.print(ds.failureCount);
2194 if (ds.failureCount > 0) {
2195 pw.print(" for "); dumpTimeSec(pw, ds.failureTime);
2196 pw.print(" avg="); dumpTimeSec(pw, ds.failureTime/ds.failureCount);
2197 }
2198 pw.println(")");
2199 }
Doug Zongker44f57472009-09-20 15:52:43 -07002200
Alon Alberte0bde332011-09-22 14:26:16 -07002201 protected void dumpSyncHistory(PrintWriter pw) {
2202 dumpRecentHistory(pw);
2203 dumpDayStatistics(pw);
2204 }
2205
2206 private void dumpRecentHistory(PrintWriter pw) {
2207 final ArrayList<SyncStorageEngine.SyncHistoryItem> items
2208 = mSyncStorageEngine.getSyncHistory();
2209 if (items != null && items.size() > 0) {
2210 final Map<String, AuthoritySyncStats> authorityMap = Maps.newHashMap();
2211 long totalElapsedTime = 0;
2212 long totalTimes = 0;
2213 final int N = items.size();
2214
2215 int maxAuthority = 0;
2216 int maxAccount = 0;
2217 for (SyncStorageEngine.SyncHistoryItem item : items) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002218 SyncStorageEngine.AuthorityInfo authorityInfo
Alon Alberte0bde332011-09-22 14:26:16 -07002219 = mSyncStorageEngine.getAuthority(item.authorityId);
2220 final String authorityName;
2221 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002222 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002223 authorityName = authorityInfo.target.provider;
2224 accountKey = authorityInfo.target.account.name + "/"
2225 + authorityInfo.target.account.type
2226 + " u" + authorityInfo.target.userId;
Alon Alberte0bde332011-09-22 14:26:16 -07002227 } else {
2228 authorityName = "Unknown";
2229 accountKey = "Unknown";
2230 }
2231
2232 int length = authorityName.length();
2233 if (length > maxAuthority) {
2234 maxAuthority = length;
2235 }
2236 length = accountKey.length();
2237 if (length > maxAccount) {
2238 maxAccount = length;
2239 }
2240
2241 final long elapsedTime = item.elapsedTime;
2242 totalElapsedTime += elapsedTime;
2243 totalTimes++;
2244 AuthoritySyncStats authoritySyncStats = authorityMap.get(authorityName);
2245 if (authoritySyncStats == null) {
2246 authoritySyncStats = new AuthoritySyncStats(authorityName);
2247 authorityMap.put(authorityName, authoritySyncStats);
2248 }
2249 authoritySyncStats.elapsedTime += elapsedTime;
2250 authoritySyncStats.times++;
2251 final Map<String, AccountSyncStats> accountMap = authoritySyncStats.accountMap;
2252 AccountSyncStats accountSyncStats = accountMap.get(accountKey);
2253 if (accountSyncStats == null) {
2254 accountSyncStats = new AccountSyncStats(accountKey);
2255 accountMap.put(accountKey, accountSyncStats);
2256 }
2257 accountSyncStats.elapsedTime += elapsedTime;
2258 accountSyncStats.times++;
2259
2260 }
2261
Alon Albert27096822012-01-11 18:06:41 -08002262 if (totalElapsedTime > 0) {
2263 pw.println();
2264 pw.printf("Detailed Statistics (Recent history): "
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002265 + "%d (# of times) %ds (sync time)\n",
Alon Albert27096822012-01-11 18:06:41 -08002266 totalTimes, totalElapsedTime / 1000);
Alon Alberte0bde332011-09-22 14:26:16 -07002267
Alon Albert27096822012-01-11 18:06:41 -08002268 final List<AuthoritySyncStats> sortedAuthorities =
2269 new ArrayList<AuthoritySyncStats>(authorityMap.values());
2270 Collections.sort(sortedAuthorities, new Comparator<AuthoritySyncStats>() {
Alon Albertbf976ba2011-10-03 13:06:43 -07002271 @Override
Alon Albert27096822012-01-11 18:06:41 -08002272 public int compare(AuthoritySyncStats lhs, AuthoritySyncStats rhs) {
Alon Albertbf976ba2011-10-03 13:06:43 -07002273 // reverse order
2274 int compare = Integer.compare(rhs.times, lhs.times);
2275 if (compare == 0) {
2276 compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
Alon Alberte0bde332011-09-22 14:26:16 -07002277 }
Alon Albertbf976ba2011-10-03 13:06:43 -07002278 return compare;
Alon Alberte0bde332011-09-22 14:26:16 -07002279 }
Alon Albertbf976ba2011-10-03 13:06:43 -07002280 });
Alon Albert27096822012-01-11 18:06:41 -08002281
2282 final int maxLength = Math.max(maxAuthority, maxAccount + 3);
2283 final int padLength = 2 + 2 + maxLength + 2 + 10 + 11;
2284 final char chars[] = new char[padLength];
2285 Arrays.fill(chars, '-');
2286 final String separator = new String(chars);
2287
2288 final String authorityFormat =
2289 String.format(" %%-%ds: %%-9s %%-11s\n", maxLength + 2);
2290 final String accountFormat =
2291 String.format(" %%-%ds: %%-9s %%-11s\n", maxLength);
2292
2293 pw.println(separator);
2294 for (AuthoritySyncStats authoritySyncStats : sortedAuthorities) {
2295 String name = authoritySyncStats.name;
2296 long elapsedTime;
2297 int times;
2298 String timeStr;
2299 String timesStr;
2300
2301 elapsedTime = authoritySyncStats.elapsedTime;
2302 times = authoritySyncStats.times;
Alon Albertbf976ba2011-10-03 13:06:43 -07002303 timeStr = String.format("%ds/%d%%",
2304 elapsedTime / 1000,
2305 elapsedTime * 100 / totalElapsedTime);
2306 timesStr = String.format("%d/%d%%",
2307 times,
2308 times * 100 / totalTimes);
Alon Albert27096822012-01-11 18:06:41 -08002309 pw.printf(authorityFormat, name, timesStr, timeStr);
2310
2311 final List<AccountSyncStats> sortedAccounts =
2312 new ArrayList<AccountSyncStats>(
2313 authoritySyncStats.accountMap.values());
2314 Collections.sort(sortedAccounts, new Comparator<AccountSyncStats>() {
2315 @Override
2316 public int compare(AccountSyncStats lhs, AccountSyncStats rhs) {
2317 // reverse order
2318 int compare = Integer.compare(rhs.times, lhs.times);
2319 if (compare == 0) {
2320 compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
2321 }
2322 return compare;
2323 }
2324 });
2325 for (AccountSyncStats stats: sortedAccounts) {
2326 elapsedTime = stats.elapsedTime;
2327 times = stats.times;
2328 timeStr = String.format("%ds/%d%%",
2329 elapsedTime / 1000,
2330 elapsedTime * 100 / totalElapsedTime);
2331 timesStr = String.format("%d/%d%%",
2332 times,
2333 times * 100 / totalTimes);
2334 pw.printf(accountFormat, stats.name, timesStr, timeStr);
2335 }
2336 pw.println(separator);
Alon Alberte0bde332011-09-22 14:26:16 -07002337 }
Alon Alberte0bde332011-09-22 14:26:16 -07002338 }
2339
2340 pw.println();
2341 pw.println("Recent Sync History");
Alon Albert57286f92012-10-09 14:21:38 -07002342 final String format = " %-" + maxAccount + "s %-" + maxAuthority + "s %s\n";
Alon Albertbf976ba2011-10-03 13:06:43 -07002343 final Map<String, Long> lastTimeMap = Maps.newHashMap();
Alon Albert57286f92012-10-09 14:21:38 -07002344 final PackageManager pm = mContext.getPackageManager();
Alon Alberte0bde332011-09-22 14:26:16 -07002345 for (int i = 0; i < N; i++) {
2346 SyncStorageEngine.SyncHistoryItem item = items.get(i);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002347 SyncStorageEngine.AuthorityInfo authorityInfo
Alon Alberte0bde332011-09-22 14:26:16 -07002348 = mSyncStorageEngine.getAuthority(item.authorityId);
2349 final String authorityName;
2350 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002351 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002352 authorityName = authorityInfo.target.provider;
2353 accountKey = authorityInfo.target.account.name + "/"
2354 + authorityInfo.target.account.type
2355 + " u" + authorityInfo.target.userId;
Alon Alberte0bde332011-09-22 14:26:16 -07002356 } else {
2357 authorityName = "Unknown";
2358 accountKey = "Unknown";
2359 }
2360 final long elapsedTime = item.elapsedTime;
2361 final Time time = new Time();
2362 final long eventTime = item.eventTime;
2363 time.set(eventTime);
2364
Alon Albertbf976ba2011-10-03 13:06:43 -07002365 final String key = authorityName + "/" + accountKey;
2366 final Long lastEventTime = lastTimeMap.get(key);
2367 final String diffString;
2368 if (lastEventTime == null) {
2369 diffString = "";
2370 } else {
2371 final long diff = (lastEventTime - eventTime) / 1000;
2372 if (diff < 60) {
2373 diffString = String.valueOf(diff);
2374 } else if (diff < 3600) {
2375 diffString = String.format("%02d:%02d", diff / 60, diff % 60);
2376 } else {
2377 final long sec = diff % 3600;
2378 diffString = String.format("%02d:%02d:%02d",
2379 diff / 3600, sec / 60, sec % 60);
2380 }
2381 }
2382 lastTimeMap.put(key, eventTime);
2383
2384 pw.printf(" #%-3d: %s %8s %5.1fs %8s",
Alon Alberte0bde332011-09-22 14:26:16 -07002385 i + 1,
2386 formatTime(eventTime),
2387 SyncStorageEngine.SOURCES[item.source],
Alon Albertbf976ba2011-10-03 13:06:43 -07002388 ((float) elapsedTime) / 1000,
2389 diffString);
Alon Albert57286f92012-10-09 14:21:38 -07002390 pw.printf(format, accountKey, authorityName,
2391 SyncOperation.reasonToString(pm, item.reason));
Alon Alberte0bde332011-09-22 14:26:16 -07002392
2393 if (item.event != SyncStorageEngine.EVENT_STOP
2394 || item.upstreamActivity != 0
2395 || item.downstreamActivity != 0) {
2396 pw.printf(" event=%d upstreamActivity=%d downstreamActivity=%d\n",
2397 item.event,
2398 item.upstreamActivity,
2399 item.downstreamActivity);
2400 }
2401 if (item.mesg != null
2402 && !SyncStorageEngine.MESG_SUCCESS.equals(item.mesg)) {
2403 pw.printf(" mesg=%s\n", item.mesg);
2404 }
2405 }
Alon Albert57286f92012-10-09 14:21:38 -07002406 pw.println();
2407 pw.println("Recent Sync History Extras");
2408 for (int i = 0; i < N; i++) {
2409 final SyncStorageEngine.SyncHistoryItem item = items.get(i);
2410 final Bundle extras = item.extras;
2411 if (extras == null || extras.size() == 0) {
2412 continue;
2413 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002414 final SyncStorageEngine.AuthorityInfo authorityInfo
Alon Albert57286f92012-10-09 14:21:38 -07002415 = mSyncStorageEngine.getAuthority(item.authorityId);
2416 final String authorityName;
2417 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002418 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002419 authorityName = authorityInfo.target.provider;
2420 accountKey = authorityInfo.target.account.name + "/"
2421 + authorityInfo.target.account.type
2422 + " u" + authorityInfo.target.userId;
Alon Albert57286f92012-10-09 14:21:38 -07002423 } else {
2424 authorityName = "Unknown";
2425 accountKey = "Unknown";
2426 }
2427 final Time time = new Time();
2428 final long eventTime = item.eventTime;
2429 time.set(eventTime);
2430
2431 pw.printf(" #%-3d: %s %8s ",
2432 i + 1,
2433 formatTime(eventTime),
2434 SyncStorageEngine.SOURCES[item.source]);
2435
2436 pw.printf(format, accountKey, authorityName, extras);
2437 }
Alon Alberte0bde332011-09-22 14:26:16 -07002438 }
2439 }
2440
2441 private void dumpDayStatistics(PrintWriter pw) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002442 SyncStorageEngine.DayStats dses[] = mSyncStorageEngine.getDayStatistics();
2443 if (dses != null && dses[0] != null) {
2444 pw.println();
2445 pw.println("Sync Statistics");
2446 pw.print(" Today: "); dumpDayStatistic(pw, dses[0]);
2447 int today = dses[0].day;
2448 int i;
2449 SyncStorageEngine.DayStats ds;
Doug Zongker44f57472009-09-20 15:52:43 -07002450
Dianne Hackborn231cc602009-04-27 17:10:36 -07002451 // Print each day in the current week.
2452 for (i=1; i<=6 && i < dses.length; i++) {
2453 ds = dses[i];
2454 if (ds == null) break;
2455 int delta = today-ds.day;
2456 if (delta > 6) break;
Doug Zongker44f57472009-09-20 15:52:43 -07002457
Dianne Hackborn231cc602009-04-27 17:10:36 -07002458 pw.print(" Day-"); pw.print(delta); pw.print(": ");
2459 dumpDayStatistic(pw, ds);
2460 }
Doug Zongker44f57472009-09-20 15:52:43 -07002461
Dianne Hackborn231cc602009-04-27 17:10:36 -07002462 // Aggregate all following days into weeks and print totals.
2463 int weekDay = today;
2464 while (i < dses.length) {
2465 SyncStorageEngine.DayStats aggr = null;
2466 weekDay -= 7;
2467 while (i < dses.length) {
2468 ds = dses[i];
2469 if (ds == null) {
2470 i = dses.length;
2471 break;
2472 }
2473 int delta = weekDay-ds.day;
2474 if (delta > 6) break;
2475 i++;
Doug Zongker44f57472009-09-20 15:52:43 -07002476
Dianne Hackborn231cc602009-04-27 17:10:36 -07002477 if (aggr == null) {
2478 aggr = new SyncStorageEngine.DayStats(weekDay);
2479 }
2480 aggr.successCount += ds.successCount;
2481 aggr.successTime += ds.successTime;
2482 aggr.failureCount += ds.failureCount;
2483 aggr.failureTime += ds.failureTime;
2484 }
2485 if (aggr != null) {
2486 pw.print(" Week-"); pw.print((today-weekDay)/7); pw.print(": ");
2487 dumpDayStatistic(pw, aggr);
2488 }
2489 }
2490 }
Alon Alberte0bde332011-09-22 14:26:16 -07002491 }
Doug Zongker44f57472009-09-20 15:52:43 -07002492
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002493 private void dumpSyncAdapters(IndentingPrintWriter pw) {
2494 pw.println();
2495 final List<UserInfo> users = getAllUsers();
2496 if (users != null) {
2497 for (UserInfo user : users) {
2498 pw.println("Sync adapters for " + user + ":");
2499 pw.increaseIndent();
2500 for (RegisteredServicesCache.ServiceInfo<?> info :
2501 mSyncAdapters.getAllServices(user.id)) {
2502 pw.println(info);
2503 }
2504 pw.decreaseIndent();
2505 pw.println();
2506 }
2507 }
2508 }
2509
Alon Alberte0bde332011-09-22 14:26:16 -07002510 private static class AuthoritySyncStats {
2511 String name;
2512 long elapsedTime;
2513 int times;
2514 Map<String, AccountSyncStats> accountMap = Maps.newHashMap();
2515
2516 private AuthoritySyncStats(String name) {
2517 this.name = name;
2518 }
2519 }
2520
2521 private static class AccountSyncStats {
2522 String name;
2523 long elapsedTime;
2524 int times;
2525
2526 private AccountSyncStats(String name) {
2527 this.name = name;
Dianne Hackborn231cc602009-04-27 17:10:36 -07002528 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002529 }
2530
2531 /**
2532 * A helper object to keep track of the time we have spent syncing since the last boot
2533 */
2534 private class SyncTimeTracker {
2535 /** True if a sync was in progress on the most recent call to update() */
2536 boolean mLastWasSyncing = false;
2537 /** Used to track when lastWasSyncing was last set */
2538 long mWhenSyncStarted = 0;
2539 /** The cumulative time we have spent syncing */
2540 private long mTimeSpentSyncing;
2541
2542 /** Call to let the tracker know that the sync state may have changed */
2543 public synchronized void update() {
Fred Quintana918339a2010-10-05 14:00:39 -07002544 final boolean isSyncInProgress = !mActiveSyncContexts.isEmpty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002545 if (isSyncInProgress == mLastWasSyncing) return;
2546 final long now = SystemClock.elapsedRealtime();
2547 if (isSyncInProgress) {
2548 mWhenSyncStarted = now;
2549 } else {
2550 mTimeSpentSyncing += now - mWhenSyncStarted;
2551 }
2552 mLastWasSyncing = isSyncInProgress;
2553 }
2554
2555 /** Get how long we have been syncing, in ms */
2556 public synchronized long timeSpentSyncing() {
2557 if (!mLastWasSyncing) return mTimeSpentSyncing;
2558
2559 final long now = SystemClock.elapsedRealtime();
2560 return mTimeSpentSyncing + (now - mWhenSyncStarted);
2561 }
2562 }
2563
Fred Quintana718d8a22009-04-29 17:53:20 -07002564 class ServiceConnectionData {
2565 public final ActiveSyncContext activeSyncContext;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002566 public final IBinder adapter;
2567
2568 ServiceConnectionData(ActiveSyncContext activeSyncContext, IBinder adapter) {
Fred Quintana718d8a22009-04-29 17:53:20 -07002569 this.activeSyncContext = activeSyncContext;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002570 this.adapter = adapter;
Fred Quintana718d8a22009-04-29 17:53:20 -07002571 }
2572 }
2573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002574 /**
2575 * Handles SyncOperation Messages that are posted to the associated
2576 * HandlerThread.
2577 */
2578 class SyncHandler extends Handler {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002579 // Messages that can be sent on mHandler.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002580 private static final int MESSAGE_SYNC_FINISHED = 1;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002581 private static final int MESSAGE_RELEASE_MESSAGES_FROM_QUEUE = 2;
Fred Quintana718d8a22009-04-29 17:53:20 -07002582 private static final int MESSAGE_SERVICE_CONNECTED = 4;
2583 private static final int MESSAGE_SERVICE_DISCONNECTED = 5;
Fred Quintana918339a2010-10-05 14:00:39 -07002584 private static final int MESSAGE_CANCEL = 6;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002585 static final int MESSAGE_JOBSERVICE_OBJECT = 7;
2586 static final int MESSAGE_START_SYNC = 10;
2587 static final int MESSAGE_STOP_SYNC = 11;
2588 static final int MESSAGE_SCHEDULE_SYNC = 12;
2589 static final int MESSAGE_UPDATE_PERIODIC_SYNC = 13;
2590 static final int MESSAGE_REMOVE_PERIODIC_SYNC = 14;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002591
Matthew Williams1967c8d2015-06-19 19:03:13 -07002592 /**
2593 * Posted periodically to monitor network process for long-running syncs.
2594 * obj: {@link com.android.server.content.SyncManager.ActiveSyncContext}
2595 */
2596 private static final int MESSAGE_MONITOR_SYNC = 8;
振淦王60a74312015-12-01 16:37:31 +08002597 private static final int MESSAGE_ACCOUNTS_UPDATED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002598
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002599 public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002600 private final HashMap<String, PowerManager.WakeLock> mWakeLocks = Maps.newHashMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002601
Matthew Williams8b76d202015-05-03 18:16:25 -07002602 private List<Message> mUnreadyQueue = new ArrayList<Message>();
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002603
Matthew Williams8b76d202015-05-03 18:16:25 -07002604 void onBootCompleted() {
Matthew Williams8704fc32013-09-27 11:32:35 -07002605 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002606 Slog.v(TAG, "Boot completed.");
Matthew Williams8704fc32013-09-27 11:32:35 -07002607 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002608 checkIfDeviceReady();
Matthew Williams8b76d202015-05-03 18:16:25 -07002609 }
2610
2611 void onDeviceProvisioned() {
2612 if (Log.isLoggable(TAG, Log.DEBUG)) {
2613 Log.d(TAG, "mProvisioned=" + mProvisioned);
2614 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002615 checkIfDeviceReady();
2616 }
2617
2618 void checkIfDeviceReady() {
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002619 if (mProvisioned && mBootCompleted && mJobServiceReady) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002620 synchronized(this) {
Shreyas Basarge3147bbc2016-02-19 23:51:40 +00002621 mSyncStorageEngine.restoreAllPeriodicSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002622 // Dispatch any stashed messages.
2623 obtainMessage(MESSAGE_RELEASE_MESSAGES_FROM_QUEUE).sendToTarget();
2624 }
Matthew Williams8b76d202015-05-03 18:16:25 -07002625 }
2626 }
2627
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002628 /**
2629 * Stash any messages that come to the handler before boot is complete or before the device
2630 * is properly provisioned (i.e. out of set-up wizard).
2631 * {@link #onBootCompleted()} and {@link SyncHandler#onDeviceProvisioned} both
2632 * need to come in before we start syncing.
2633 * @param msg Message to dispatch at a later point.
2634 * @return true if a message was enqueued, false otherwise. This is to avoid losing the
2635 * message if we manage to acquire the lock but by the time we do boot has completed.
2636 */
2637 private boolean tryEnqueueMessageUntilReadyToRun(Message msg) {
2638 synchronized (this) {
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002639 if (!mBootCompleted || !mProvisioned || !mJobServiceReady) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002640 // Need to copy the message bc looper will recycle it.
2641 Message m = Message.obtain(msg);
2642 mUnreadyQueue.add(m);
2643 return true;
2644 } else {
2645 return false;
Matthew Williams8704fc32013-09-27 11:32:35 -07002646 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002647 }
2648 }
2649
2650 public SyncHandler(Looper looper) {
2651 super(looper);
2652 }
2653
2654 public void handleMessage(Message msg) {
2655 try {
2656 mSyncManagerWakeLock.acquire();
2657 // We only want to enqueue sync related messages until device is ready.
2658 // Other messages are handled without enqueuing.
2659 if (msg.what == MESSAGE_JOBSERVICE_OBJECT) {
2660 Slog.i(TAG, "Got SyncJobService instance.");
2661 mSyncJobService = (SyncJobService) msg.obj;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002662 mJobServiceReady = true;
2663 checkIfDeviceReady();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002664 } else if (msg.what == SyncHandler.MESSAGE_ACCOUNTS_UPDATED) {
2665 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2666 Slog.v(TAG, "handleSyncHandlerMessage: MESSAGE_ACCOUNTS_UPDATED");
2667 }
2668 EndPoint targets = (EndPoint) msg.obj;
2669 updateRunningAccountsH(targets);
2670 } else if (msg.what == MESSAGE_RELEASE_MESSAGES_FROM_QUEUE) {
2671 if (mUnreadyQueue != null) {
2672 for (Message m : mUnreadyQueue) {
2673 handleSyncMessage(m);
2674 }
2675 mUnreadyQueue = null;
2676 }
2677 } else if (tryEnqueueMessageUntilReadyToRun(msg)) {
2678 // No work to be done.
2679 } else {
2680 handleSyncMessage(msg);
2681 }
2682 } finally {
2683 mSyncManagerWakeLock.release();
2684 }
2685 }
2686
2687 private void handleSyncMessage(Message msg) {
2688 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
2689
2690 try {
2691 mDataConnectionIsConnected = readDataConnectionState();
2692 switch (msg.what) {
2693 case MESSAGE_SCHEDULE_SYNC:
Shreyas Basargeba1f7902016-10-01 00:19:44 +01002694 ScheduleSyncMessagePayload syncPayload =
2695 (ScheduleSyncMessagePayload) msg.obj;
2696 SyncOperation op = syncPayload.syncOperation;
2697 scheduleSyncOperationH(op, syncPayload.minDelayMillis);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002698 break;
2699
2700 case MESSAGE_START_SYNC:
2701 op = (SyncOperation) msg.obj;
2702 startSyncH(op);
2703 break;
2704
2705 case MESSAGE_STOP_SYNC:
2706 op = (SyncOperation) msg.obj;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002707 if (isLoggable) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002708 Slog.v(TAG, "Stop sync received.");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002709 }
2710 ActiveSyncContext asc = findActiveSyncContextH(op.jobId);
2711 if (asc != null) {
2712 runSyncFinishedOrCanceledH(null /* no result */, asc);
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002713 boolean reschedule = msg.arg1 != 0;
2714 boolean applyBackoff = msg.arg2 != 0;
2715 if (isLoggable) {
2716 Slog.v(TAG, "Stopping sync. Reschedule: " + reschedule
2717 + "Backoff: " + applyBackoff);
2718 }
2719 if (applyBackoff) {
2720 increaseBackoffSetting(op.target);
2721 }
2722 if (reschedule) {
2723 deferStoppedSyncH(op, 0);
2724 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002725 }
2726 break;
2727
2728 case MESSAGE_UPDATE_PERIODIC_SYNC:
2729 UpdatePeriodicSyncMessagePayload data =
2730 (UpdatePeriodicSyncMessagePayload) msg.obj;
2731 updateOrAddPeriodicSyncH(data.target, data.pollFrequency,
2732 data.flex, data.extras);
2733 break;
2734 case MESSAGE_REMOVE_PERIODIC_SYNC:
Makoto Onukidd4b14f2017-08-17 14:03:48 -07002735 Pair<EndPoint, String> args = (Pair<EndPoint, String>) (msg.obj);
2736 removePeriodicSyncH(args.first, msg.getData(), args.second);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002737 break;
2738
2739 case SyncHandler.MESSAGE_CANCEL:
2740 SyncStorageEngine.EndPoint endpoint = (SyncStorageEngine.EndPoint) msg.obj;
2741 Bundle extras = msg.peekData();
2742 if (Log.isLoggable(TAG, Log.DEBUG)) {
2743 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_CANCEL: "
2744 + endpoint + " bundle: " + extras);
2745 }
Makoto Onukia9dca242017-06-21 17:06:49 -07002746 cancelActiveSyncH(endpoint, extras, "MESSAGE_CANCEL");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002747 break;
2748
2749 case SyncHandler.MESSAGE_SYNC_FINISHED:
2750 SyncFinishedOrCancelledMessagePayload payload =
2751 (SyncFinishedOrCancelledMessagePayload) msg.obj;
2752 if (!isSyncStillActiveH(payload.activeSyncContext)) {
2753 Log.d(TAG, "handleSyncHandlerMessage: dropping since the "
2754 + "sync is no longer active: "
2755 + payload.activeSyncContext);
2756 break;
2757 }
2758 if (isLoggable) {
2759 Slog.v(TAG, "syncFinished" + payload.activeSyncContext.mSyncOperation);
2760 }
2761 mSyncJobService.callJobFinished(
Makoto Onukia9dca242017-06-21 17:06:49 -07002762 payload.activeSyncContext.mSyncOperation.jobId, false,
2763 "sync finished");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002764 runSyncFinishedOrCanceledH(payload.syncResult,
2765 payload.activeSyncContext);
2766 break;
2767
2768 case SyncHandler.MESSAGE_SERVICE_CONNECTED: {
2769 ServiceConnectionData msgData = (ServiceConnectionData) msg.obj;
2770 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2771 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_CONNECTED: "
2772 + msgData.activeSyncContext);
2773 }
2774 // Check that this isn't an old message.
2775 if (isSyncStillActiveH(msgData.activeSyncContext)) {
2776 runBoundToAdapterH(
2777 msgData.activeSyncContext,
2778 msgData.adapter);
2779 }
2780 break;
2781 }
2782
2783 case SyncHandler.MESSAGE_SERVICE_DISCONNECTED: {
2784 final ActiveSyncContext currentSyncContext =
2785 ((ServiceConnectionData) msg.obj).activeSyncContext;
2786 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2787 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_DISCONNECTED: "
2788 + currentSyncContext);
2789 }
2790 // Check that this isn't an old message.
2791 if (isSyncStillActiveH(currentSyncContext)) {
2792 // cancel the sync if we have a syncadapter, which means one is
2793 // outstanding
2794 try {
2795 if (currentSyncContext.mSyncAdapter != null) {
Makoto Onuki6a6ae042017-07-20 13:30:12 -07002796 mLogger.log("Calling cancelSync for SERVICE_DISCONNECTED ",
2797 currentSyncContext,
2798 " adapter=", currentSyncContext.mSyncAdapter);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002799 currentSyncContext.mSyncAdapter.cancelSync(currentSyncContext);
Makoto Onuki6a6ae042017-07-20 13:30:12 -07002800 mLogger.log("Canceled");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002801 }
2802 } catch (RemoteException e) {
Makoto Onuki6a6ae042017-07-20 13:30:12 -07002803 mLogger.log("RemoteException ", Log.getStackTraceString(e));
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002804 // We don't need to retry this in this case.
2805 }
2806
2807 // Pretend that the sync failed with an IOException,
2808 // which is a soft error.
2809 SyncResult syncResult = new SyncResult();
2810 syncResult.stats.numIoExceptions++;
2811 mSyncJobService.callJobFinished(
Makoto Onukia9dca242017-06-21 17:06:49 -07002812 currentSyncContext.mSyncOperation.jobId, false,
2813 "service disconnected");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002814 runSyncFinishedOrCanceledH(syncResult, currentSyncContext);
2815 }
2816 break;
2817 }
2818
2819 case SyncHandler.MESSAGE_MONITOR_SYNC:
2820 ActiveSyncContext monitoredSyncContext = (ActiveSyncContext) msg.obj;
2821 if (Log.isLoggable(TAG, Log.DEBUG)) {
2822 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_MONITOR_SYNC: " +
2823 monitoredSyncContext.mSyncOperation.target);
2824 }
2825
2826 if (isSyncNotUsingNetworkH(monitoredSyncContext)) {
2827 Log.w(TAG, String.format(
2828 "Detected sync making no progress for %s. cancelling.",
2829 monitoredSyncContext));
2830 mSyncJobService.callJobFinished(
Makoto Onukia9dca242017-06-21 17:06:49 -07002831 monitoredSyncContext.mSyncOperation.jobId, false,
2832 "no network activity");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002833 runSyncFinishedOrCanceledH(
2834 null /* cancel => no result */, monitoredSyncContext);
2835 } else {
2836 // Repost message to check again.
2837 postMonitorSyncProgressMessage(monitoredSyncContext);
2838 }
2839 break;
2840
2841 }
2842 } finally {
2843 mSyncTimeTracker.update();
Fred Quintanae91ebe22009-09-29 20:44:30 -07002844 }
2845 }
2846
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002847 private PowerManager.WakeLock getSyncWakeLock(SyncOperation operation) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002848 final String wakeLockKey = operation.wakeLockName();
Fred Quintanab3029c32010-04-06 13:27:12 -07002849 PowerManager.WakeLock wakeLock = mWakeLocks.get(wakeLockKey);
2850 if (wakeLock == null) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002851 final String name = SYNC_WAKE_LOCK_PREFIX + wakeLockKey;
Fred Quintanab3029c32010-04-06 13:27:12 -07002852 wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
2853 wakeLock.setReferenceCounted(false);
2854 mWakeLocks.put(wakeLockKey, wakeLock);
2855 }
2856 return wakeLock;
2857 }
2858
Matthew Williams8704fc32013-09-27 11:32:35 -07002859 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002860 * Defer the specified SyncOperation by rescheduling it on the JobScheduler with some
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002861 * delay. This is equivalent to a failure. If this is a periodic sync, a delayed one-off
2862 * sync will be scheduled.
Matthew Williams8704fc32013-09-27 11:32:35 -07002863 */
Makoto Onukia9dca242017-06-21 17:06:49 -07002864 private void deferSyncH(SyncOperation op, long delay, String why) {
2865 mLogger.log("deferSyncH() ", (op.isPeriodic ? "periodic " : ""),
2866 "sync. op=", op, " delay=", delay, " why=", why);
2867 mSyncJobService.callJobFinished(op.jobId, false, why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002868 if (op.isPeriodic) {
2869 scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay);
2870 } else {
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01002871 // mSyncJobService.callJobFinished is async, so cancel the job to ensure we don't
2872 // find the this job in the pending jobs list while looking for duplicates
2873 // before scheduling it at a later time.
Makoto Onukidd4b14f2017-08-17 14:03:48 -07002874 cancelJob(op, "deferSyncH()");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002875 scheduleSyncOperationH(op, delay);
Fred Quintanae91ebe22009-09-29 20:44:30 -07002876 }
2877 }
Matthew Williams8704fc32013-09-27 11:32:35 -07002878
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002879 /* Same as deferSyncH, but assumes that job is no longer running on JobScheduler. */
2880 private void deferStoppedSyncH(SyncOperation op, long delay) {
2881 if (op.isPeriodic) {
2882 scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay);
2883 } else {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002884 scheduleSyncOperationH(op, delay);
2885 }
2886 }
2887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002888 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002889 * Cancel an active sync and reschedule it on the JobScheduler with some delay.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 */
Makoto Onukia9dca242017-06-21 17:06:49 -07002891 private void deferActiveSyncH(ActiveSyncContext asc, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002892 SyncOperation op = asc.mSyncOperation;
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002893 runSyncFinishedOrCanceledH(null, asc);
Makoto Onukia9dca242017-06-21 17:06:49 -07002894 deferSyncH(op, SYNC_DELAY_ON_CONFLICT, why);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 }
2896
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002897 private void startSyncH(SyncOperation op) {
2898 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
2899 if (isLoggable) Slog.v(TAG, op.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002901 if (mStorageIsLow) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002902 deferSyncH(op, SYNC_DELAY_ON_LOW_STORAGE, "storage low");
Matthew Williams8704fc32013-09-27 11:32:35 -07002903 return;
2904 }
2905
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002906 if (op.isPeriodic) {
2907 // Don't allow this periodic to run if a previous instance failed and is currently
2908 // scheduled according to some backoff criteria.
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002909 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002910 for (SyncOperation syncOperation: ops) {
2911 if (syncOperation.sourcePeriodicId == op.jobId) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002912 mSyncJobService.callJobFinished(op.jobId, false,
2913 "periodic sync, pending");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002914 return;
Fred Quintana718d8a22009-04-29 17:53:20 -07002915 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002916 }
2917 // Don't allow this periodic to run if a previous instance failed and is currently
2918 // executing according to some backoff criteria.
2919 for (ActiveSyncContext asc: mActiveSyncContexts) {
2920 if (asc.mSyncOperation.sourcePeriodicId == op.jobId) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002921 mSyncJobService.callJobFinished(op.jobId, false,
2922 "periodic sync, already running");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002923 return;
Fred Quintana718d8a22009-04-29 17:53:20 -07002924 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002925 }
2926 // Check for adapter delays.
2927 if (isAdapterDelayed(op.target)) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002928 deferSyncH(op, 0 /* No minimum delay */, "backing off");
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002929 return;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002930 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002931 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002932
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002933 // Check for conflicting syncs.
2934 for (ActiveSyncContext asc: mActiveSyncContexts) {
2935 if (asc.mSyncOperation.isConflict(op)) {
2936 // If the provided SyncOperation conflicts with a running one, the lower
2937 // priority sync is pre-empted.
2938 if (asc.mSyncOperation.findPriority() >= op.findPriority()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002939 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002940 Slog.v(TAG, "Rescheduling sync due to conflict " + op.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002941 }
Makoto Onukia9dca242017-06-21 17:06:49 -07002942 deferSyncH(op, SYNC_DELAY_ON_CONFLICT, "delay on conflict");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002943 return;
Matthew Williamsfa774182013-06-18 15:44:11 -07002944 } else {
Matthew Williamsfa774182013-06-18 15:44:11 -07002945 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002946 Slog.v(TAG, "Pushing back running sync due to a higher priority sync");
Fred Quintana918339a2010-10-05 14:00:39 -07002947 }
Makoto Onukia9dca242017-06-21 17:06:49 -07002948 deferActiveSyncH(asc, "preempted");
Shreyas Basarge7680dd92016-02-11 14:52:47 +00002949 break;
Alon Albert8e285552012-09-17 15:05:27 -07002950 }
2951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 }
2953
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002954 final int syncOpState = computeSyncOpState(op);
2955 switch (syncOpState) {
2956 case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS:
2957 case SYNC_OP_STATE_INVALID: {
Makoto Onukia9dca242017-06-21 17:06:49 -07002958 mSyncJobService.callJobFinished(op.jobId, false,
2959 "invalid op state: " + syncOpState);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002960 } return;
2961 }
2962
2963 if (!dispatchSyncOperation(op)) {
Makoto Onukia9dca242017-06-21 17:06:49 -07002964 mSyncJobService.callJobFinished(op.jobId, false, "dispatchSyncOperation() failed");
Fred Quintana918339a2010-10-05 14:00:39 -07002965 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002966
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002967 setAuthorityPendingState(op.target);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002968 }
2969
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002970 private ActiveSyncContext findActiveSyncContextH(int jobId) {
2971 for (ActiveSyncContext asc: mActiveSyncContexts) {
2972 SyncOperation op = asc.mSyncOperation;
2973 if (op != null && op.jobId == jobId) {
2974 return asc;
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002975 }
2976 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002977 return null;
Matthew Williamsa4745542015-12-10 20:29:02 +00002978 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002979
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002980 private void updateRunningAccountsH(EndPoint syncTargets) {
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002981 AccountAndUser[] oldAccounts = mRunningAccounts;
振淦王60a74312015-12-01 16:37:31 +08002982 mRunningAccounts = AccountManagerService.getSingleton().getRunningAccounts();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002983 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2984 Slog.v(TAG, "Accounts list: ");
2985 for (AccountAndUser acc : mRunningAccounts) {
2986 Slog.v(TAG, acc.toString());
2987 }
2988 }
Makoto Onukibbf6d8c2017-08-11 12:11:39 -07002989 if (mLogger.enabled()) {
2990 mLogger.log("updateRunningAccountsH: ", Arrays.toString(mRunningAccounts));
2991 }
振淦王60a74312015-12-01 16:37:31 +08002992 if (mBootCompleted) {
2993 doDatabaseCleanup();
2994 }
2995
2996 AccountAndUser[] accounts = mRunningAccounts;
2997 for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) {
2998 if (!containsAccountAndUser(accounts,
2999 currentSyncContext.mSyncOperation.target.account,
3000 currentSyncContext.mSyncOperation.target.userId)) {
3001 Log.d(TAG, "canceling sync since the account is no longer running");
3002 sendSyncFinishedOrCanceledMessage(currentSyncContext,
3003 null /* no result since this is a cancel */);
3004 }
3005 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003006
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003007 // On account add, check if there are any settings to be restored.
3008 for (AccountAndUser aau : mRunningAccounts) {
3009 if (!containsAccountAndUser(oldAccounts, aau.account, aau.userId)) {
3010 if (Log.isLoggable(TAG, Log.DEBUG)) {
3011 Log.d(TAG, "Account " + aau.account + " added, checking sync restore data");
3012 }
3013 AccountSyncSettingsBackupHelper.accountAdded(mContext);
3014 break;
3015 }
3016 }
3017
Rubin Xu2c548e02016-04-01 17:47:28 +01003018 // Cancel all jobs from non-existent accounts.
3019 AccountAndUser[] allAccounts = AccountManagerService.getSingleton().getAllAccounts();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003020 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003021 for (SyncOperation op: ops) {
Rubin Xu2c548e02016-04-01 17:47:28 +01003022 if (!containsAccountAndUser(allAccounts, op.target.account, op.target.userId)) {
Makoto Onukibbf6d8c2017-08-11 12:11:39 -07003023 mLogger.log("canceling: ", op);
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003024 cancelJob(op, "updateRunningAccountsH()");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003025 }
3026 }
3027
3028 if (syncTargets != null) {
3029 scheduleSync(syncTargets.account, syncTargets.userId,
Svet Ganovf6d424f12016-09-20 20:18:53 -07003030 SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider,
3031 null, AuthorityInfo.NOT_INITIALIZED);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003032 }
3033 }
3034
3035 /**
3036 * The given SyncOperation will be removed and a new one scheduled in its place if
3037 * an updated period or flex is specified.
3038 * @param syncOperation SyncOperation whose period and flex is to be updated.
3039 * @param pollFrequencyMillis new period in milliseconds.
3040 * @param flexMillis new flex time in milliseconds.
3041 */
3042 private void maybeUpdateSyncPeriodH(SyncOperation syncOperation, long pollFrequencyMillis,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003043 long flexMillis) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003044 if (!(pollFrequencyMillis == syncOperation.periodMillis
3045 && flexMillis == syncOperation.flexMillis)) {
3046 if (Log.isLoggable(TAG, Log.VERBOSE)) {
3047 Slog.v(TAG, "updating period " + syncOperation + " to " + pollFrequencyMillis
3048 + " and flex to " + flexMillis);
3049 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003050 SyncOperation newOp = new SyncOperation(syncOperation, pollFrequencyMillis,
3051 flexMillis);
3052 newOp.jobId = syncOperation.jobId;
3053 scheduleSyncOperationH(newOp);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003054 }
3055 }
3056
3057 private void updateOrAddPeriodicSyncH(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003058 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003059 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
3060 verifyJobScheduler(); // Will fill in mScheduledSyncs cache if it is not already filled.
3061 final long pollFrequencyMillis = pollFrequency * 1000L;
3062 final long flexMillis = flex * 1000L;
3063 if (isLoggable) {
3064 Slog.v(TAG, "Addition to periodic syncs requested: " + target
3065 + " period: " + pollFrequency
3066 + " flexMillis: " + flex
3067 + " extras: " + extras.toString());
3068 }
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003069 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003070 for (SyncOperation op: ops) {
3071 if (op.isPeriodic && op.target.matchesSpec(target)
3072 && syncExtrasEquals(op.extras, extras, true /* includeSyncSettings */)) {
3073 maybeUpdateSyncPeriodH(op, pollFrequencyMillis, flexMillis);
3074 return;
3075 }
3076 }
3077
3078 if (isLoggable) {
3079 Slog.v(TAG, "Adding new periodic sync: " + target
3080 + " period: " + pollFrequency
3081 + " flexMillis: " + flex
3082 + " extras: " + extras.toString());
3083 }
3084
3085 final RegisteredServicesCache.ServiceInfo<SyncAdapterType>
3086 syncAdapterInfo = mSyncAdapters.getServiceInfo(
3087 SyncAdapterType.newKey(
3088 target.provider, target.account.type),
3089 target.userId);
3090 if (syncAdapterInfo == null) {
3091 return;
3092 }
3093
3094 SyncOperation op = new SyncOperation(target, syncAdapterInfo.uid,
3095 syncAdapterInfo.componentName.getPackageName(), SyncOperation.REASON_PERIODIC,
3096 SyncStorageEngine.SOURCE_PERIODIC, extras,
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003097 syncAdapterInfo.type.allowParallelSyncs(), true, SyncOperation.NO_JOB_ID,
3098 pollFrequencyMillis, flexMillis);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003099
3100 final int syncOpState = computeSyncOpState(op);
3101 switch (syncOpState) {
3102 case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS: {
Svet Ganov973edd192016-09-08 20:15:55 -07003103 String packageName = op.owningPackage;
3104 final int userId = UserHandle.getUserId(op.owningUid);
3105 // If the app did not run and has no account access, done
3106 if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
3107 return;
3108 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003109 mAccountManagerInternal.requestAccountAccess(op.target.account,
Svet Ganov973edd192016-09-08 20:15:55 -07003110 packageName, userId, new RemoteCallback((Bundle result) -> {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003111 if (result != null
3112 && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
3113 updateOrAddPeriodicSync(target, pollFrequency, flex, extras);
3114 }
3115 }
3116 ));
3117 } return;
3118
3119 case SYNC_OP_STATE_INVALID: {
3120 return;
3121 }
3122 }
3123
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003124 scheduleSyncOperationH(op);
3125 mSyncStorageEngine.reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
3126 }
3127
3128 /**
3129 * Remove this periodic sync operation and all one-off operations initiated by it.
3130 */
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003131 private void removePeriodicSyncInternalH(SyncOperation syncOperation, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003132 // Remove this periodic sync and all one-off syncs initiated by it.
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003133 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003134 for (SyncOperation op: ops) {
3135 if (op.sourcePeriodicId == syncOperation.jobId || op.jobId == syncOperation.jobId) {
3136 ActiveSyncContext asc = findActiveSyncContextH(syncOperation.jobId);
3137 if (asc != null) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003138 mSyncJobService.callJobFinished(syncOperation.jobId, false,
3139 "removePeriodicSyncInternalH");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003140 runSyncFinishedOrCanceledH(null, asc);
3141 }
Makoto Onukibbf6d8c2017-08-11 12:11:39 -07003142 mLogger.log("removePeriodicSyncInternalH-canceling: ", op);
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003143 cancelJob(op, why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003144 }
3145 }
3146 }
3147
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003148 private void removePeriodicSyncH(EndPoint target, Bundle extras, String why) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003149 verifyJobScheduler();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003150 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003151 for (SyncOperation op: ops) {
3152 if (op.isPeriodic && op.target.matchesSpec(target)
3153 && syncExtrasEquals(op.extras, extras, true /* includeSyncSettings */)) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003154 removePeriodicSyncInternalH(op, why);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003155 }
3156 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08003157 }
3158
Matthew Williams1967c8d2015-06-19 19:03:13 -07003159 private boolean isSyncNotUsingNetworkH(ActiveSyncContext activeSyncContext) {
3160 final long bytesTransferredCurrent =
3161 getTotalBytesTransferredByUid(activeSyncContext.mSyncAdapterUid);
3162 final long deltaBytesTransferred =
3163 bytesTransferredCurrent - activeSyncContext.mBytesTransferredAtLastPoll;
3164
3165 if (Log.isLoggable(TAG, Log.DEBUG)) {
3166 // Bytes transferred
3167 long remainder = deltaBytesTransferred;
3168 long mb = remainder / (1024 * 1024);
3169 remainder %= 1024 * 1024;
3170 long kb = remainder / 1024;
3171 remainder %= 1024;
3172 long b = remainder;
3173 Log.d(TAG, String.format(
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003174 "Time since last update: %ds. Delta transferred: %dMBs,%dKBs,%dBs",
3175 (SystemClock.elapsedRealtime()
3176 - activeSyncContext.mLastPolledTimeElapsed)/1000,
3177 mb, kb, b)
Matthew Williams1967c8d2015-06-19 19:03:13 -07003178 );
3179 }
3180 return (deltaBytesTransferred <= SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES);
3181 }
3182
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003183 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003184 * Determine if a sync is no longer valid and should be dropped.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003185 */
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003186 private int computeSyncOpState(SyncOperation op) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003187 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003188 int state;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003189 final EndPoint target = op.target;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003190
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003191 // Drop the sync if the account of this operation no longer exists.
3192 AccountAndUser[] accounts = mRunningAccounts;
3193 if (!containsAccountAndUser(accounts, target.account, target.userId)) {
3194 if (isLoggable) {
3195 Slog.v(TAG, " Dropping sync operation: account doesn't exist.");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003196 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003197 return SYNC_OP_STATE_INVALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003198 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003199 // Drop this sync request if it isn't syncable.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003200 state = computeSyncable(target.account, target.userId, target.provider);
3201 if (state == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003202 if (isLoggable) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003203 Slog.v(TAG, " Dropping sync operation: "
3204 + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003205 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003206 return SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003207 }
Svet Ganovdfed1c72016-08-25 15:40:52 -07003208 if (state == AuthorityInfo.NOT_SYNCABLE) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003209 if (isLoggable) {
Svet Ganovdfed1c72016-08-25 15:40:52 -07003210 Slog.v(TAG, " Dropping sync operation: isSyncable == NOT_SYNCABLE");
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003211 }
3212 return SYNC_OP_STATE_INVALID;
3213 }
3214
3215 final boolean syncEnabled = mSyncStorageEngine.getMasterSyncAutomatically(target.userId)
3216 && mSyncStorageEngine.getSyncAutomatically(target.account,
3217 target.userId, target.provider);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003218
3219 // We ignore system settings that specify the sync is invalid if:
3220 // 1) It's manual - we try it anyway. When/if it fails it will be rescheduled.
3221 // or
3222 // 2) it's an initialisation sync - we just need to connect to it.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003223 final boolean ignoreSystemConfiguration = op.isIgnoreSettings() || (state < 0);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003224
3225 // Sync not enabled.
3226 if (!syncEnabled && !ignoreSystemConfiguration) {
3227 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003228 Slog.v(TAG, " Dropping sync operation: disallowed by settings/network.");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003229 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003230 return SYNC_OP_STATE_INVALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003231 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07003232 return SYNC_OP_STATE_VALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003233 }
Fred Quintana918339a2010-10-05 14:00:39 -07003234
Fred Quintana87b14662011-09-12 10:32:55 -07003235 private boolean dispatchSyncOperation(SyncOperation op) {
Fred Quintana918339a2010-10-05 14:00:39 -07003236 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003237 Slog.v(TAG, "dispatchSyncOperation: we are going to sync " + op);
3238 Slog.v(TAG, "num active syncs: " + mActiveSyncContexts.size());
Fred Quintana918339a2010-10-05 14:00:39 -07003239 for (ActiveSyncContext syncContext : mActiveSyncContexts) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003240 Slog.v(TAG, syncContext.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07003241 }
3242 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003243 // Connect to the sync adapter.
3244 int targetUid;
3245 ComponentName targetComponent;
3246 final SyncStorageEngine.EndPoint info = op.target;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003247 SyncAdapterType syncAdapterType =
3248 SyncAdapterType.newKey(info.provider, info.account.type);
3249 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo;
3250 syncAdapterInfo = mSyncAdapters.getServiceInfo(syncAdapterType, info.userId);
3251 if (syncAdapterInfo == null) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003252 mLogger.log("dispatchSyncOperation() failed: no sync adapter info for ",
3253 syncAdapterType);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003254 Log.d(TAG, "can't find a sync adapter for " + syncAdapterType
3255 + ", removing settings for it");
3256 mSyncStorageEngine.removeAuthority(info);
3257 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003258 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003259 targetUid = syncAdapterInfo.uid;
3260 targetComponent = syncAdapterInfo.componentName;
Fred Quintana718d8a22009-04-29 17:53:20 -07003261 ActiveSyncContext activeSyncContext =
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003262 new ActiveSyncContext(op, insertStartSyncEvent(op), targetUid);
Fred Quintana718d8a22009-04-29 17:53:20 -07003263 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003264 Slog.v(TAG, "dispatchSyncOperation: starting " + activeSyncContext);
Fred Quintana718d8a22009-04-29 17:53:20 -07003265 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07003266
3267 activeSyncContext.mSyncInfo = mSyncStorageEngine.addActiveSync(activeSyncContext);
3268 mActiveSyncContexts.add(activeSyncContext);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003269
3270 // Post message to begin monitoring this sync's progress.
3271 postMonitorSyncProgressMessage(activeSyncContext);
3272
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003273 if (!activeSyncContext.bindToSyncAdapter(targetComponent, info.userId)) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003274 mLogger.log("dispatchSyncOperation() failed: bind failed. target: ",
3275 targetComponent);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003276 Slog.e(TAG, "Bind attempt failed - target: " + targetComponent);
Fred Quintana918339a2010-10-05 14:00:39 -07003277 closeActiveSyncContext(activeSyncContext);
Fred Quintana87b14662011-09-12 10:32:55 -07003278 return false;
Fred Quintana718d8a22009-04-29 17:53:20 -07003279 }
3280
Fred Quintana87b14662011-09-12 10:32:55 -07003281 return true;
Fred Quintana718d8a22009-04-29 17:53:20 -07003282 }
3283
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003284 private void runBoundToAdapterH(final ActiveSyncContext activeSyncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003285 IBinder syncAdapter) {
Fred Quintana918339a2010-10-05 14:00:39 -07003286 final SyncOperation syncOperation = activeSyncContext.mSyncOperation;
Fred Quintana718d8a22009-04-29 17:53:20 -07003287 try {
Alon Alberteca75112010-12-08 15:02:33 -08003288 activeSyncContext.mIsLinkedToDeath = true;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003289 syncAdapter.linkToDeath(activeSyncContext, 0);
Alon Alberteca75112010-12-08 15:02:33 -08003290
Makoto Onukia9dca242017-06-21 17:06:49 -07003291 mLogger.log("Sync start: account=" + syncOperation.target.account,
3292 " authority=", syncOperation.target.provider,
3293 " reason=", SyncOperation.reasonToString(null, syncOperation.reason),
Makoto Onuki6a6ae042017-07-20 13:30:12 -07003294 " extras=", SyncOperation.extrasToString(syncOperation.extras),
3295 " adapter=", activeSyncContext.mSyncAdapter);
Makoto Onukia9dca242017-06-21 17:06:49 -07003296
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003297 activeSyncContext.mSyncAdapter = ISyncAdapter.Stub.asInterface(syncAdapter);
3298 activeSyncContext.mSyncAdapter
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003299 .startSync(activeSyncContext, syncOperation.target.provider,
3300 syncOperation.target.account, syncOperation.extras);
Makoto Onukia9dca242017-06-21 17:06:49 -07003301
Makoto Onuki6a6ae042017-07-20 13:30:12 -07003302 mLogger.log("Sync is running now...");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003303 } catch (RemoteException remoteExc) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003304 mLogger.log("Sync failed with RemoteException: ", remoteExc.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07003305 Log.d(TAG, "maybeStartNextSync: caught a RemoteException, rescheduling", remoteExc);
3306 closeActiveSyncContext(activeSyncContext);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003307 increaseBackoffSetting(syncOperation.target);
3308 scheduleSyncOperationH(syncOperation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 } catch (RuntimeException exc) {
Makoto Onukia9dca242017-06-21 17:06:49 -07003310 mLogger.log("Sync failed with RuntimeException: ", exc.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07003311 closeActiveSyncContext(activeSyncContext);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003312 Slog.e(TAG, "Caught RuntimeException while starting the sync " + syncOperation, exc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003314 }
3315
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003316 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07003317 * Cancel the sync for the provided target that matches the given bundle.
Matthew Williams1967c8d2015-06-19 19:03:13 -07003318 * @param info Can have null fields to indicate all the active syncs for that field.
3319 * @param extras Can be null to indicate <strong>all</strong> syncs for the given endpoint.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003320 */
Makoto Onukia9dca242017-06-21 17:06:49 -07003321 private void cancelActiveSyncH(SyncStorageEngine.EndPoint info, Bundle extras,
3322 String why) {
Fred Quintana918339a2010-10-05 14:00:39 -07003323 ArrayList<ActiveSyncContext> activeSyncs =
3324 new ArrayList<ActiveSyncContext>(mActiveSyncContexts);
3325 for (ActiveSyncContext activeSyncContext : activeSyncs) {
3326 if (activeSyncContext != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003327 final SyncStorageEngine.EndPoint opInfo =
3328 activeSyncContext.mSyncOperation.target;
Matthew Williams8ef22042013-07-26 12:56:39 -07003329 if (!opInfo.matchesSpec(info)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003330 continue;
Fred Quintana918339a2010-10-05 14:00:39 -07003331 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003332 if (extras != null &&
3333 !syncExtrasEquals(activeSyncContext.mSyncOperation.extras,
3334 extras,
3335 false /* no config settings */)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08003336 continue;
3337 }
Makoto Onukia9dca242017-06-21 17:06:49 -07003338 mSyncJobService.callJobFinished(activeSyncContext.mSyncOperation.jobId, false,
3339 why);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003340 runSyncFinishedOrCanceledH(null /* cancel => no result */, activeSyncContext);
Fred Quintana918339a2010-10-05 14:00:39 -07003341 }
3342 }
3343 }
3344
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003345 /**
3346 * Should be called when a one-off instance of a periodic sync completes successfully.
3347 */
3348 private void reschedulePeriodicSyncH(SyncOperation syncOperation) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003349 // Ensure that the periodic sync wasn't removed.
3350 SyncOperation periodicSync = null;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003351 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003352 for (SyncOperation op: ops) {
3353 if (op.isPeriodic && syncOperation.matchesPeriodicOperation(op)) {
3354 periodicSync = op;
3355 break;
3356 }
3357 }
3358 if (periodicSync == null) {
3359 return;
3360 }
3361 scheduleSyncOperationH(periodicSync);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003362 }
3363
Matthew Williams8b76d202015-05-03 18:16:25 -07003364 private void runSyncFinishedOrCanceledH(SyncResult syncResult,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003365 ActiveSyncContext activeSyncContext) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003366 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Alon Alberteca75112010-12-08 15:02:33 -08003367
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003368 final SyncOperation syncOperation = activeSyncContext.mSyncOperation;
3369 final SyncStorageEngine.EndPoint info = syncOperation.target;
3370
Alon Alberteca75112010-12-08 15:02:33 -08003371 if (activeSyncContext.mIsLinkedToDeath) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003372 activeSyncContext.mSyncAdapter.asBinder().unlinkToDeath(activeSyncContext, 0);
Alon Alberteca75112010-12-08 15:02:33 -08003373 activeSyncContext.mIsLinkedToDeath = false;
3374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003375 final long elapsedTime = SystemClock.elapsedRealtime() - activeSyncContext.mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003376 String historyMessage;
3377 int downstreamActivity;
3378 int upstreamActivity;
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01003379
Makoto Onukia9dca242017-06-21 17:06:49 -07003380 mLogger.log("runSyncFinishedOrCanceledH() op=", syncOperation, " result=", syncResult);
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01003381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003382 if (syncResult != null) {
3383 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003384 Slog.v(TAG, "runSyncFinishedOrCanceled [finished]: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 + syncOperation + ", result " + syncResult);
3386 }
3387
Makoto Onuki9243d9c2017-08-15 14:56:47 -07003388 // In the non-canceled case, close the active sync context before doing the rest
3389 // of the stuff.
3390 closeActiveSyncContext(activeSyncContext);
3391
3392 // Note this part is probably okay to do before closeActiveSyncContext()...
3393 // But moved here to restore OC-dev's behavior. See b/64597061.
3394 if (!syncOperation.isPeriodic) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003395 cancelJob(syncOperation, "runSyncFinishedOrCanceledH()-finished");
Makoto Onuki9243d9c2017-08-15 14:56:47 -07003396 }
3397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 if (!syncResult.hasError()) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07003399 historyMessage = SyncStorageEngine.MESG_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 // TODO: set these correctly when the SyncResult is extended to include it
3401 downstreamActivity = 0;
3402 upstreamActivity = 0;
Makoto Onukia9dca242017-06-21 17:06:49 -07003403 clearBackoffSetting(syncOperation.target, "sync success");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003404
3405 // If the operation completes successfully and it was scheduled due to
3406 // a periodic operation failing, we reschedule the periodic operation to
3407 // start from now.
3408 if (syncOperation.isDerivedFromFailedPeriodicSync()) {
3409 reschedulePeriodicSyncH(syncOperation);
3410 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003411 } else {
Fred Quintana307da1a2010-01-21 14:24:20 -08003412 Log.d(TAG, "failed sync operation " + syncOperation + ", " + syncResult);
3413 // the operation failed so increase the backoff time
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003414 increaseBackoffSetting(syncOperation.target);
3415 if (!syncOperation.isPeriodic) {
3416 // reschedule the sync if so indicated by the syncResult
3417 maybeRescheduleSync(syncResult, syncOperation);
3418 } else {
3419 // create a normal sync instance that will respect adapter backoffs
Shreyas Basargeba1f7902016-10-01 00:19:44 +01003420 postScheduleSyncMessage(syncOperation.createOneTimeSyncOperation(),
3421 0 /* min delay */);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003422 }
Alon Albert57286f92012-10-09 14:21:38 -07003423 historyMessage = ContentResolver.syncErrorToString(
3424 syncResultToErrorNumber(syncResult));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003425 // TODO: set these correctly when the SyncResult is extended to include it
3426 downstreamActivity = 0;
3427 upstreamActivity = 0;
3428 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003429 setDelayUntilTime(syncOperation.target, syncResult.delayUntil);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 } else {
3431 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003432 Slog.v(TAG, "runSyncFinishedOrCanceled [canceled]: " + syncOperation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 }
Makoto Onuki9243d9c2017-08-15 14:56:47 -07003434
3435 if (!syncOperation.isPeriodic) {
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003436 cancelJob(syncOperation, "runSyncFinishedOrCanceledH()-canceled");
Makoto Onuki9243d9c2017-08-15 14:56:47 -07003437 }
3438
Fred Quintana718d8a22009-04-29 17:53:20 -07003439 if (activeSyncContext.mSyncAdapter != null) {
3440 try {
Makoto Onuki6a6ae042017-07-20 13:30:12 -07003441 mLogger.log("Calling cancelSync for runSyncFinishedOrCanceled ",
3442 activeSyncContext, " adapter=", activeSyncContext.mSyncAdapter);
Fred Quintana21bb0de2009-06-16 10:24:58 -07003443 activeSyncContext.mSyncAdapter.cancelSync(activeSyncContext);
Makoto Onuki6a6ae042017-07-20 13:30:12 -07003444 mLogger.log("Canceled");
Fred Quintana718d8a22009-04-29 17:53:20 -07003445 } catch (RemoteException e) {
Makoto Onuki6a6ae042017-07-20 13:30:12 -07003446 mLogger.log("RemoteException ", Log.getStackTraceString(e));
Fred Quintana718d8a22009-04-29 17:53:20 -07003447 // we don't need to retry this in this case
3448 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003449 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07003450 historyMessage = SyncStorageEngine.MESG_CANCELED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 downstreamActivity = 0;
3452 upstreamActivity = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003453
Makoto Onuki9243d9c2017-08-15 14:56:47 -07003454 // In the cancel sync case, close it after calling cancelSync().
3455 closeActiveSyncContext(activeSyncContext);
3456 }
Makoto Onuki6a6ae042017-07-20 13:30:12 -07003457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 stopSyncEvent(activeSyncContext.mHistoryRowId, syncOperation, historyMessage,
3459 upstreamActivity, downstreamActivity, elapsedTime);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003460 // Check for full-resync and schedule it after closing off the last sync.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003461 if (syncResult != null && syncResult.tooManyDeletions) {
3462 installHandleTooManyDeletesNotification(info.account,
3463 info.provider, syncResult.stats.numDeletes,
3464 info.userId);
3465 } else {
Chris Wren282cfef2017-03-27 15:01:44 -04003466 mNotificationMgr.cancelAsUser(
3467 Integer.toString(info.account.hashCode() ^ info.provider.hashCode()),
3468 SystemMessage.NOTE_SYNC_ERROR,
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003469 new UserHandle(info.userId));
3470 }
3471 if (syncResult != null && syncResult.fullSyncRequested) {
3472 scheduleSyncOperationH(
3473 new SyncOperation(info.account, info.userId,
3474 syncOperation.owningUid, syncOperation.owningPackage,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003475 syncOperation.reason,
3476 syncOperation.syncSource, info.provider, new Bundle(),
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003477 syncOperation.allowParallelSyncs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003479 }
3480
Fred Quintana918339a2010-10-05 14:00:39 -07003481 private void closeActiveSyncContext(ActiveSyncContext activeSyncContext) {
3482 activeSyncContext.close();
3483 mActiveSyncContexts.remove(activeSyncContext);
Amith Yamasani04e0d262012-02-14 11:50:53 -08003484 mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003485 activeSyncContext.mSyncOperation.target.userId);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003486
3487 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003488 Slog.v(TAG, "removing all MESSAGE_MONITOR_SYNC & MESSAGE_SYNC_EXPIRED for "
Matthew Williams1967c8d2015-06-19 19:03:13 -07003489 + activeSyncContext.toString());
3490 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07003491 mSyncHandler.removeMessages(SyncHandler.MESSAGE_MONITOR_SYNC, activeSyncContext);
Makoto Onuki9243d9c2017-08-15 14:56:47 -07003492
3493 mLogger.log("closeActiveSyncContext: ", activeSyncContext);
Fred Quintana918339a2010-10-05 14:00:39 -07003494 }
3495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003496 /**
3497 * Convert the error-containing SyncResult into the Sync.History error number. Since
3498 * the SyncResult may indicate multiple errors at once, this method just returns the
3499 * most "serious" error.
3500 * @param syncResult the SyncResult from which to read
3501 * @return the most "serious" error set in the SyncResult
3502 * @throws IllegalStateException if the SyncResult does not indicate any errors.
3503 * If SyncResult.error() is true then it is safe to call this.
3504 */
3505 private int syncResultToErrorNumber(SyncResult syncResult) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07003506 if (syncResult.syncAlreadyInProgress)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003507 return ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003508 if (syncResult.stats.numAuthExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003509 return ContentResolver.SYNC_ERROR_AUTHENTICATION;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003510 if (syncResult.stats.numIoExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003511 return ContentResolver.SYNC_ERROR_IO;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003512 if (syncResult.stats.numParseExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003513 return ContentResolver.SYNC_ERROR_PARSE;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003514 if (syncResult.stats.numConflictDetectedExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003515 return ContentResolver.SYNC_ERROR_CONFLICT;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003516 if (syncResult.tooManyDeletions)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003517 return ContentResolver.SYNC_ERROR_TOO_MANY_DELETIONS;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003518 if (syncResult.tooManyRetries)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003519 return ContentResolver.SYNC_ERROR_TOO_MANY_RETRIES;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003520 if (syncResult.databaseError)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003521 return ContentResolver.SYNC_ERROR_INTERNAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 throw new IllegalStateException("we are not in an error state, " + syncResult);
3523 }
3524
Fred Quintanad9d2f112009-04-23 13:36:27 -07003525 private void installHandleTooManyDeletesNotification(Account account, String authority,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003526 long numDeletes, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003527 if (mNotificationMgr == null) return;
Fred Quintanac848b702009-08-25 20:18:46 -07003528
3529 final ProviderInfo providerInfo = mContext.getPackageManager().resolveContentProvider(
3530 authority, 0 /* flags */);
3531 if (providerInfo == null) {
3532 return;
3533 }
3534 CharSequence authorityName = providerInfo.loadLabel(mContext.getPackageManager());
3535
Fred Quintanab19e62a2010-12-16 13:54:43 -08003536 Intent clickIntent = new Intent(mContext, SyncActivityTooManyDeletes.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003537 clickIntent.putExtra("account", account);
Tadashi G. Takaoka86135d32009-09-24 19:31:44 -07003538 clickIntent.putExtra("authority", authority);
Fred Quintanac848b702009-08-25 20:18:46 -07003539 clickIntent.putExtra("provider", authorityName.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003540 clickIntent.putExtra("numDeletes", numDeletes);
3541
3542 if (!isActivityAvailable(clickIntent)) {
3543 Log.w(TAG, "No activity found to handle too many deletes.");
3544 return;
3545 }
3546
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003547 UserHandle user = new UserHandle(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003548 final PendingIntent pendingIntent = PendingIntent
Dianne Hackborn41203752012-08-31 14:05:51 -07003549 .getActivityAsUser(mContext, 0, clickIntent,
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003550 PendingIntent.FLAG_CANCEL_CURRENT, null, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003551
3552 CharSequence tooManyDeletesDescFormat = mContext.getResources().getText(
3553 R.string.contentServiceTooManyDeletesNotificationDesc);
3554
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003555 Context contextForUser = getContextForUser(user);
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05003556 Notification notification =
3557 new Notification.Builder(contextForUser, SystemNotificationChannels.ACCOUNT)
Chris Wren1ce4b6d2015-06-11 10:19:43 -04003558 .setSmallIcon(R.drawable.stat_notify_sync_error)
3559 .setTicker(mContext.getString(R.string.contentServiceSync))
3560 .setWhen(System.currentTimeMillis())
3561 .setColor(contextForUser.getColor(
3562 com.android.internal.R.color.system_notification_accent_color))
3563 .setContentTitle(contextForUser.getString(
3564 R.string.contentServiceSyncNotificationTitle))
3565 .setContentText(
3566 String.format(tooManyDeletesDescFormat.toString(), authorityName))
3567 .setContentIntent(pendingIntent)
3568 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003569 notification.flags |= Notification.FLAG_ONGOING_EVENT;
Chris Wren282cfef2017-03-27 15:01:44 -04003570 mNotificationMgr.notifyAsUser(
3571 Integer.toString(account.hashCode() ^ authority.hashCode()),
3572 SystemMessage.NOTE_SYNC_ERROR,
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003573 notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003574 }
3575
3576 /**
3577 * Checks whether an activity exists on the system image for the given intent.
3578 *
3579 * @param intent The intent for an activity.
3580 * @return Whether or not an activity exists.
3581 */
3582 private boolean isActivityAvailable(Intent intent) {
3583 PackageManager pm = mContext.getPackageManager();
3584 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
3585 int listSize = list.size();
3586 for (int i = 0; i < listSize; i++) {
3587 ResolveInfo resolveInfo = list.get(i);
3588 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
3589 != 0) {
3590 return true;
3591 }
3592 }
3593
3594 return false;
3595 }
3596
3597 public long insertStartSyncEvent(SyncOperation syncOperation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003598 final long now = System.currentTimeMillis();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003599 EventLog.writeEvent(2720,
3600 syncOperation.toEventLog(SyncStorageEngine.EVENT_START));
3601 return mSyncStorageEngine.insertStartSyncEvent(syncOperation, now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003602 }
3603
3604 public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003605 int upstreamActivity, int downstreamActivity, long elapsedTime) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003606 EventLog.writeEvent(2720,
3607 syncOperation.toEventLog(SyncStorageEngine.EVENT_STOP));
Fred Quintana77c560f2010-03-29 22:20:26 -07003608 mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime,
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003609 resultMessage, downstreamActivity, upstreamActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003610 }
3611 }
Fred Quintana918339a2010-10-05 14:00:39 -07003612
Matthew Williams8b76d202015-05-03 18:16:25 -07003613 private boolean isSyncStillActiveH(ActiveSyncContext activeSyncContext) {
Fred Quintana918339a2010-10-05 14:00:39 -07003614 for (ActiveSyncContext sync : mActiveSyncContexts) {
3615 if (sync == activeSyncContext) {
3616 return true;
3617 }
3618 }
3619 return false;
3620 }
Alon Albert57286f92012-10-09 14:21:38 -07003621
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003622 /**
3623 * Sync extra comparison function.
3624 * @param b1 bundle to compare
3625 * @param b2 other bundle to compare
3626 * @param includeSyncSettings if false, ignore system settings in bundle.
3627 */
3628 public static boolean syncExtrasEquals(Bundle b1, Bundle b2, boolean includeSyncSettings) {
3629 if (b1 == b2) {
3630 return true;
3631 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003632 // Exit early if we can.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003633 if (includeSyncSettings && b1.size() != b2.size()) {
3634 return false;
3635 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003636 Bundle bigger = b1.size() > b2.size() ? b1 : b2;
3637 Bundle smaller = b1.size() > b2.size() ? b2 : b1;
3638 for (String key : bigger.keySet()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003639 if (!includeSyncSettings && isSyncSetting(key)) {
3640 continue;
3641 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003642 if (!smaller.containsKey(key)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003643 return false;
3644 }
Matthew Williams9ad2c842015-10-16 12:01:31 -07003645 if (!Objects.equals(bigger.get(key), smaller.get(key))) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003646 return false;
3647 }
3648 }
3649 return true;
3650 }
3651
3652 /**
3653 * @return true if the provided key is used by the SyncManager in scheduling the sync.
3654 */
3655 private static boolean isSyncSetting(String key) {
3656 if (key.equals(ContentResolver.SYNC_EXTRAS_EXPEDITED)) {
3657 return true;
3658 }
3659 if (key.equals(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS)) {
3660 return true;
3661 }
3662 if (key.equals(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF)) {
3663 return true;
3664 }
3665 if (key.equals(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY)) {
3666 return true;
3667 }
3668 if (key.equals(ContentResolver.SYNC_EXTRAS_MANUAL)) {
3669 return true;
3670 }
3671 if (key.equals(ContentResolver.SYNC_EXTRAS_UPLOAD)) {
3672 return true;
3673 }
3674 if (key.equals(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS)) {
3675 return true;
3676 }
3677 if (key.equals(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS)) {
3678 return true;
3679 }
3680 if (key.equals(ContentResolver.SYNC_EXTRAS_EXPECTED_UPLOAD)) {
3681 return true;
3682 }
3683 if (key.equals(ContentResolver.SYNC_EXTRAS_EXPECTED_DOWNLOAD)) {
3684 return true;
3685 }
3686 if (key.equals(ContentResolver.SYNC_EXTRAS_PRIORITY)) {
3687 return true;
3688 }
3689 if (key.equals(ContentResolver.SYNC_EXTRAS_DISALLOW_METERED)) {
3690 return true;
3691 }
3692 if (key.equals(ContentResolver.SYNC_EXTRAS_INITIALIZE)) {
3693 return true;
3694 }
3695 return false;
3696 }
3697
Alon Albert57286f92012-10-09 14:21:38 -07003698 static class PrintTable {
Makoto Onuki15e7a252017-06-08 17:12:05 -07003699 private ArrayList<String[]> mTable = Lists.newArrayList();
Alon Albert57286f92012-10-09 14:21:38 -07003700 private final int mCols;
3701
3702 PrintTable(int cols) {
3703 mCols = cols;
3704 }
3705
3706 void set(int row, int col, Object... values) {
3707 if (col + values.length > mCols) {
3708 throw new IndexOutOfBoundsException("Table only has " + mCols +
3709 " columns. can't set " + values.length + " at column " + col);
3710 }
3711 for (int i = mTable.size(); i <= row; i++) {
Makoto Onuki15e7a252017-06-08 17:12:05 -07003712 final String[] list = new String[mCols];
Alon Albert57286f92012-10-09 14:21:38 -07003713 mTable.add(list);
3714 for (int j = 0; j < mCols; j++) {
3715 list[j] = "";
3716 }
3717 }
Makoto Onuki15e7a252017-06-08 17:12:05 -07003718 final String[] rowArray = mTable.get(row);
3719 for (int i = 0; i < values.length; i++) {
3720 final Object value = values[i];
3721 rowArray[col + i] = (value == null) ? "" : value.toString();
3722 }
Alon Albert57286f92012-10-09 14:21:38 -07003723 }
3724
3725 void writeTo(PrintWriter out) {
3726 final String[] formats = new String[mCols];
3727 int totalLength = 0;
3728 for (int col = 0; col < mCols; ++col) {
3729 int maxLength = 0;
3730 for (Object[] row : mTable) {
3731 final int length = row[col].toString().length();
3732 if (length > maxLength) {
3733 maxLength = length;
3734 }
3735 }
3736 totalLength += maxLength;
3737 formats[col] = String.format("%%-%ds", maxLength);
3738 }
Patrick Tjin31068162014-01-30 13:28:46 -08003739 formats[mCols - 1] = "%s";
Alon Albert57286f92012-10-09 14:21:38 -07003740 printRow(out, formats, mTable.get(0));
3741 totalLength += (mCols - 1) * 2;
3742 for (int i = 0; i < totalLength; ++i) {
3743 out.print("-");
3744 }
3745 out.println();
3746 for (int i = 1, mTableSize = mTable.size(); i < mTableSize; i++) {
3747 Object[] row = mTable.get(i);
3748 printRow(out, formats, row);
3749 }
3750 }
3751
3752 private void printRow(PrintWriter out, String[] formats, Object[] row) {
3753 for (int j = 0, rowLength = row.length; j < rowLength; j++) {
3754 out.printf(String.format(formats[j], row[j].toString()));
3755 out.print(" ");
3756 }
3757 out.println();
3758 }
3759
3760 public int getNumRows() {
3761 return mTable.size();
3762 }
3763 }
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003764
3765 private Context getContextForUser(UserHandle user) {
3766 try {
3767 return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
3768 } catch (NameNotFoundException e) {
3769 // Default to mContext, not finding the package system is running as is unlikely.
3770 return mContext;
3771 }
3772 }
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003773
3774 private void cancelJob(SyncOperation op, String why) {
3775 if (op == null) {
3776 Slog.wtf(TAG, "Null sync operation detected.");
3777 return;
3778 }
3779 if (op.isPeriodic) {
3780 mLogger.log("Removing periodic sync ", op, " for ", why);
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003781 }
3782 getJobScheduler().cancel(op.jobId);
3783 }
3784
Makoto Onukidd4b14f2017-08-17 14:03:48 -07003785 private void wtfWithLog(String message) {
3786 Slog.wtf(TAG, message);
3787 mLogger.log("WTF: ", message);
3788 }
Geoffrey Pitschaf759c52017-02-15 09:35:38 -05003789}