blob: 5b539ff1976d10d04b80f70f16006b3cb95bfb94 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeff Sharkey7a96c392012-11-15 14:01:46 -080017package com.android.server.content;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Alon Alberte0bde332011-09-22 14:26:16 -070019import android.accounts.Account;
Amith Yamasanif29f2362012-04-05 18:29:52 -070020import android.accounts.AccountAndUser;
Alon Alberte0bde332011-09-22 14:26:16 -070021import android.accounts.AccountManager;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070022import android.accounts.AccountManagerInternal;
Fred Quintana33e44692011-12-05 15:04:16 -080023import android.app.ActivityManager;
Amith Yamasani9422bdc2013-04-10 16:58:19 -070024import android.app.AppGlobals;
Alon Alberte0bde332011-09-22 14:26:16 -070025import android.app.Notification;
26import android.app.NotificationManager;
27import android.app.PendingIntent;
Shreyas Basarge8c834c02016-01-07 13:53:16 +000028import android.app.job.JobInfo;
29import android.app.job.JobScheduler;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080030import android.content.BroadcastReceiver;
31import android.content.ComponentName;
32import android.content.ContentResolver;
33import android.content.Context;
34import android.content.ISyncAdapter;
35import android.content.ISyncContext;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080036import android.content.Intent;
37import android.content.IntentFilter;
Matthew Williamsfa774182013-06-18 15:44:11 -070038import android.content.PeriodicSync;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080039import android.content.ServiceConnection;
40import android.content.SyncActivityTooManyDeletes;
41import android.content.SyncAdapterType;
42import android.content.SyncAdaptersCache;
43import android.content.SyncInfo;
44import android.content.SyncResult;
45import android.content.SyncStatusInfo;
Alon Alberte0bde332011-09-22 14:26:16 -070046import android.content.pm.ApplicationInfo;
Amith Yamasani9422bdc2013-04-10 16:58:19 -070047import android.content.pm.PackageInfo;
Alon Alberte0bde332011-09-22 14:26:16 -070048import android.content.pm.PackageManager;
Kenny Guy07ad8dc2014-09-01 20:56:12 +010049import android.content.pm.PackageManager.NameNotFoundException;
Svet Ganov973edd192016-09-08 20:15:55 -070050import android.content.pm.PackageManagerInternal;
Alon Alberte0bde332011-09-22 14:26:16 -070051import android.content.pm.ProviderInfo;
52import android.content.pm.RegisteredServicesCache;
53import android.content.pm.RegisteredServicesCacheListener;
54import android.content.pm.ResolveInfo;
Amith Yamasani04e0d262012-02-14 11:50:53 -080055import android.content.pm.UserInfo;
Matthew Williams8b76d202015-05-03 18:16:25 -070056import android.database.ContentObserver;
Alon Alberte0bde332011-09-22 14:26:16 -070057import android.net.ConnectivityManager;
58import android.net.NetworkInfo;
Matthew Williams1967c8d2015-06-19 19:03:13 -070059import android.net.TrafficStats;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080060import android.os.BatteryStats;
Fred Quintana918339a2010-10-05 14:00:39 -070061import android.os.Bundle;
62import android.os.Handler;
Fred Quintana918339a2010-10-05 14:00:39 -070063import android.os.IBinder;
64import android.os.Looper;
65import android.os.Message;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000066import android.os.Messenger;
Fred Quintana918339a2010-10-05 14:00:39 -070067import android.os.PowerManager;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070068import android.os.RemoteCallback;
Fred Quintana918339a2010-10-05 14:00:39 -070069import android.os.RemoteException;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080070import android.os.ServiceManager;
Fred Quintana918339a2010-10-05 14:00:39 -070071import android.os.SystemClock;
72import android.os.SystemProperties;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Amith Yamasani258848d2012-08-10 17:06:33 -070074import android.os.UserManager;
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -070075import android.os.WorkSource;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.provider.Settings;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import android.text.format.DateUtils;
78import android.text.format.Time;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079import android.util.EventLog;
80import android.util.Log;
Fred Quintana307da1a2010-01-21 14:24:20 -080081import android.util.Pair;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000082import android.util.Slog;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000083
Svetoslav Ganov5cb29732016-07-11 19:32:30 -070084import com.android.internal.util.ArrayUtils;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +000085import com.android.server.LocalServices;
86import com.android.server.job.JobSchedulerInternal;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000087import com.google.android.collect.Lists;
88import com.google.android.collect.Maps;
89
Alon Albert8e285552012-09-17 15:05:27 -070090import com.android.internal.R;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080091import com.android.internal.app.IBatteryStats;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070092import com.android.internal.os.BackgroundThread;
Jeff Sharkey6ab72d72012-10-08 16:44:37 -070093import com.android.internal.util.IndentingPrintWriter;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080094import com.android.server.accounts.AccountManagerService;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +000095import com.android.server.backup.AccountSyncSettingsBackupHelper;
Georgi Nikolovdbe846b2013-06-25 14:09:56 -070096import com.android.server.content.SyncStorageEngine.AuthorityInfo;
Amith Yamasani96a0fd652015-04-10 16:16:30 -070097import com.android.server.content.SyncStorageEngine.EndPoint;
Jeff Sharkey7a96c392012-11-15 14:01:46 -080098import com.android.server.content.SyncStorageEngine.OnSyncRequestListener;
Alon Albert8e285552012-09-17 15:05:27 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100import java.io.FileDescriptor;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101import java.io.PrintWriter;
102import java.util.ArrayList;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000103import java.util.Arrays;
Fred Quintana918339a2010-10-05 14:00:39 -0700104import java.util.Collection;
105import java.util.Collections;
Alon Alberte0bde332011-09-22 14:26:16 -0700106import java.util.Comparator;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000107import java.util.HashMap;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000108import java.util.HashSet;
109import java.util.List;
110import java.util.Map;
Matthew Williams9ad2c842015-10-16 12:01:31 -0700111import java.util.Objects;
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000112import java.util.Random;
113import java.util.Set;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114
115/**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000116 * Implementation details:
117 * All scheduled syncs will be passed on to JobScheduler as jobs
118 * (See {@link #scheduleSyncOperationH(SyncOperation, long)}. This function schedules a job
119 * with JobScheduler with appropriate delay and constraints (according to backoffs and extras).
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000120 * The scheduleSyncOperationH function also assigns a unique jobId to each
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000121 * SyncOperation.
122 *
123 * Periodic Syncs:
124 * Each periodic sync is scheduled as a periodic job. If a periodic sync fails, we create a new
125 * one off SyncOperation and set its {@link SyncOperation#sourcePeriodicId} field to the jobId of the
126 * periodic sync. We don't allow the periodic job to run while any job initiated by it is pending.
127 *
128 * Backoffs:
129 * Each {@link EndPoint} has a backoff associated with it. When a SyncOperation fails, we increase
130 * the backoff on the authority. Then we reschedule all syncs associated with that authority to
131 * run at a later time. Similarly, when a sync succeeds, backoff is cleared and all associated syncs
132 * are rescheduled. A rescheduled sync will get a new jobId.
133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * @hide
135 */
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700136public class SyncManager {
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700137 static final String TAG = "SyncManager";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700139 private static final boolean DEBUG_ACCOUNT_ACCESS = false;
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /** Delay a sync due to local changes this long. In milliseconds */
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700142 private static final long LOCAL_SYNC_DELAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700144 static {
Fred Quintana918339a2010-10-05 14:00:39 -0700145 LOCAL_SYNC_DELAY =
146 SystemProperties.getLong("sync.local_sync_delay", 30 * 1000 /* 30 seconds */);
Debajit Ghosh44ee0f02009-09-14 14:58:31 -0700147 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 /**
150 * When retrying a sync for the first time use this delay. After that
151 * the retry time will double until it reached MAX_SYNC_RETRY_TIME.
152 * In milliseconds.
153 */
154 private static final long INITIAL_SYNC_RETRY_TIME_IN_MS = 30 * 1000; // 30 seconds
155
156 /**
157 * Default the max sync retry time to this value.
158 */
159 private static final long DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS = 60 * 60; // one hour
160
161 /**
Fred Quintana8570f742010-02-18 10:32:54 -0800162 * How long to wait before retrying a sync that failed due to one already being in progress.
163 */
164 private static final int DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS = 10;
165
Matthew Williams92a1c092014-08-25 19:18:32 -0700166 /**
Matthew Williams1967c8d2015-06-19 19:03:13 -0700167 * How often to periodically poll network traffic for an adapter performing a sync to determine
168 * whether progress is being made.
169 */
170 private static final long SYNC_MONITOR_WINDOW_LENGTH_MILLIS = 60 * 1000; // 60 seconds
171
172 /**
173 * How many bytes must be transferred (Tx + Rx) over the period of time defined by
174 * {@link #SYNC_MONITOR_WINDOW_LENGTH_MILLIS} for the sync to be considered to be making
175 * progress.
176 */
177 private static final int SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES = 10; // 10 bytes
178
179 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000180 * If a previously scheduled sync becomes ready and we are low on storage, it gets
181 * pushed back for this amount of time.
182 */
183 private static final long SYNC_DELAY_ON_LOW_STORAGE = 60*60*1000; // 1 hour
184
185 /**
186 * If a sync becomes ready and it conflicts with an already running sync, it gets
187 * pushed back for this amount of time.
188 */
189 private static final long SYNC_DELAY_ON_CONFLICT = 10*1000; // 10 seconds
Fred Quintana3ec47302010-03-10 10:08:31 -0800190
Shreyas Basargefa272532016-03-09 16:52:48 +0000191 /**
192 * Generate job ids in the range [MIN_SYNC_JOB_ID, MAX_SYNC_JOB_ID) to avoid conflicts with
193 * other jobs scheduled by the system process.
194 */
195 private static final int MIN_SYNC_JOB_ID = 100000;
196 private static final int MAX_SYNC_JOB_ID = 110000;
197
Dianne Hackbornd45665b2014-02-26 12:35:32 -0800198 private static final String SYNC_WAKE_LOCK_PREFIX = "*sync*/";
Dianne Hackborn7e9f4eb2010-09-10 18:43:00 -0700199 private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm";
Fred Quintana918339a2010-10-05 14:00:39 -0700200 private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock";
201
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700202
203 private static final int SYNC_OP_STATE_VALID = 0;
204 private static final int SYNC_OP_STATE_INVALID = 1;
205 private static final int SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS = 2;
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 private Context mContext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
Amith Yamasani04e0d262012-02-14 11:50:53 -0800209 private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0];
210
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700211 // TODO: add better locking around mRunningAccounts
212 private volatile AccountAndUser[] mRunningAccounts = INITIAL_ACCOUNTS_ARRAY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 volatile private PowerManager.WakeLock mHandleAlarmWakeLock;
Fred Quintana918339a2010-10-05 14:00:39 -0700215 volatile private PowerManager.WakeLock mSyncManagerWakeLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 volatile private boolean mDataConnectionIsConnected = false;
217 volatile private boolean mStorageIsLow = false;
Dianne Hackborn4870e9d2015-04-08 16:55:47 -0700218 volatile private boolean mDeviceIsIdle = false;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800219 volatile private boolean mReportedSyncActive = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220
221 private final NotificationManager mNotificationMgr;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800222 private final IBatteryStats mBatteryStats;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000223 private JobScheduler mJobScheduler;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000224 private JobSchedulerInternal mJobSchedulerInternal;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000225 private SyncJobService mSyncJobService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700227 private SyncStorageEngine mSyncStorageEngine;
Jeff Sharkeya706e2f2012-10-16 12:02:42 -0700228
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700229 protected final ArrayList<ActiveSyncContext> mActiveSyncContexts = Lists.newArrayList();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Fred Quintanaf892fb32009-08-27 21:32:08 -0700231 // Synchronized on "this". Instead of using this directly one should instead call
232 // its accessor, getConnManager().
233 private ConnectivityManager mConnManagerDoNotUseDirectly;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Matthew Williams8b76d202015-05-03 18:16:25 -0700235 /** Track whether the device has already been provisioned. */
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000236 private volatile boolean mProvisioned;
Matthew Williams8b76d202015-05-03 18:16:25 -0700237
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700238 protected SyncAdaptersCache mSyncAdapters;
Fred Quintana718d8a22009-04-29 17:53:20 -0700239
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000240 private final Random mRand;
241
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000242 private boolean isJobIdInUseLockedH(int jobId, List<JobInfo> pendingJobs) {
243 for (JobInfo job: pendingJobs) {
244 if (job.getId() == jobId) {
245 return true;
246 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000247 }
248 for (ActiveSyncContext asc: mActiveSyncContexts) {
249 if (asc.mSyncOperation.jobId == jobId) {
250 return true;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000251 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000252 }
253 return false;
254 }
255
256 private int getUnusedJobIdH() {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000257 int newJobId;
258 do {
259 newJobId = MIN_SYNC_JOB_ID + mRand.nextInt(MAX_SYNC_JOB_ID - MIN_SYNC_JOB_ID);
260 } while (isJobIdInUseLockedH(newJobId,
261 mJobSchedulerInternal.getSystemScheduledPendingJobs()));
262 return newJobId;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000263 }
264
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000265 private List<SyncOperation> getAllPendingSyncs() {
266 verifyJobScheduler();
267 List<JobInfo> pendingJobs = mJobSchedulerInternal.getSystemScheduledPendingJobs();
268 List<SyncOperation> pendingSyncs = new ArrayList<SyncOperation>(pendingJobs.size());
269 for (JobInfo job: pendingJobs) {
270 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras());
271 if (op != null) {
272 pendingSyncs.add(op);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000273 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000274 }
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000275 return pendingSyncs;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000276 }
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700277
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700278 private final BroadcastReceiver mStorageIntentReceiver =
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700280 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 public void onReceive(Context context, Intent intent) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 String action = intent.getAction();
283 if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) {
284 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000285 Slog.v(TAG, "Internal storage is low.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 }
287 mStorageIsLow = true;
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700288 cancelActiveSync(
289 SyncStorageEngine.EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL,
290 null /* any sync */);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) {
292 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000293 Slog.v(TAG, "Internal storage is ok.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 }
295 mStorageIsLow = false;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000296 rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 }
298 }
299 };
300
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700301 private final BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700302 @Override
Fred Quintana60307342009-03-24 22:48:12 -0700303 public void onReceive(Context context, Intent intent) {
Matthew Williams8b76d202015-05-03 18:16:25 -0700304 mBootCompleted = true;
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000305 // Called because it gets all pending jobs and stores them in mScheduledSyncs cache.
306 verifyJobScheduler();
Fred Quintanae91ebe22009-09-29 20:44:30 -0700307 mSyncHandler.onBootCompleted();
Fred Quintana60307342009-03-24 22:48:12 -0700308 }
309 };
310
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700311 private final BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700312 @Override
Amith Yamasanid648a602012-09-26 15:06:10 -0700313 public void onReceive(Context context, Intent intent) {
Shreyas Basargedcb88c82016-02-03 00:09:18 +0000314 EndPoint target = new EndPoint(null, null, context.getUserId());
315 updateRunningAccounts(target /* sync targets for user */);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700316 }
317 };
318
Fred Quintanab3029c32010-04-06 13:27:12 -0700319 private final PowerManager mPowerManager;
Ashish Sharma69d95de2012-04-11 17:27:24 -0700320
Amith Yamasani9535c912012-10-10 21:48:33 -0700321 private final UserManager mUserManager;
Amith Yamasani258848d2012-08-10 17:06:33 -0700322
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700323 private final AccountManager mAccountManager;
324
325 private final AccountManagerInternal mAccountManagerInternal;
326
Svet Ganov973edd192016-09-08 20:15:55 -0700327 private final PackageManagerInternal mPackageManagerInternal;
328
Amith Yamasani258848d2012-08-10 17:06:33 -0700329 private List<UserInfo> getAllUsers() {
Amith Yamasani9535c912012-10-10 21:48:33 -0700330 return mUserManager.getUsers();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800331 }
332
333 private boolean containsAccountAndUser(AccountAndUser[] accounts, Account account, int userId) {
334 boolean found = false;
335 for (int i = 0; i < accounts.length; i++) {
336 if (accounts[i].userId == userId
337 && accounts[i].account.equals(account)) {
338 found = true;
339 break;
340 }
341 }
342 return found;
343 }
344
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000345 /** target indicates endpoints that should be synced after account info is updated. */
346 private void updateRunningAccounts(EndPoint target) {
347 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_ACCOUNTS_UPDATED");
振淦王60a74312015-12-01 16:37:31 +0800348 // Update accounts in handler thread.
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000349 Message m = mSyncHandler.obtainMessage(SyncHandler.MESSAGE_ACCOUNTS_UPDATED);
350 m.obj = target;
351 m.sendToTarget();
Fred Quintanad9d2f112009-04-23 13:36:27 -0700352 }
353
Jeff Sharkey8f55d112012-10-11 11:00:21 -0700354 private void doDatabaseCleanup() {
Amith Yamasanidb6a14c2012-10-17 21:16:52 -0700355 for (UserInfo user : mUserManager.getUsers(true)) {
356 // Skip any partially created/removed users
357 if (user.partial) continue;
Svetoslavf3f02ac2015-09-08 14:36:35 -0700358 Account[] accountsForUser = AccountManagerService.getSingleton().getAccounts(
359 user.id, mContext.getOpPackageName());
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000360
Jeff Sharkey8f55d112012-10-11 11:00:21 -0700361 mSyncStorageEngine.doDatabaseCleanup(accountsForUser, user.id);
362 }
363 }
364
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 private BroadcastReceiver mConnectivityIntentReceiver =
366 new BroadcastReceiver() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000367 @Override
368 public void onReceive(Context context, Intent intent) {
369 final boolean wasConnected = mDataConnectionIsConnected;
Alon Alberted1d2532011-02-15 14:02:14 -0800370
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000371 // Don't use the intent to figure out if network is connected, just check
372 // ConnectivityManager directly.
373 mDataConnectionIsConnected = readDataConnectionState();
374 if (mDataConnectionIsConnected) {
375 if (!wasConnected) {
376 if (Log.isLoggable(TAG, Log.VERBOSE)) {
377 Slog.v(TAG, "Reconnection detected: clearing all backoffs");
378 }
379 }
380 clearAllBackoffs();
Matthew Williams119aac92014-09-28 20:42:23 -0700381 }
Alon Alberted1d2532011-02-15 14:02:14 -0800382 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000383 };
384
385 private void clearAllBackoffs() {
386 mSyncStorageEngine.clearAllBackoffsLocked();
387 rescheduleSyncs(EndPoint.USER_ALL_PROVIDER_ALL_ACCOUNTS_ALL);
388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
Alon Albert1bad83a2011-02-16 10:29:56 -0800390 private boolean readDataConnectionState() {
Alon Alberted1d2532011-02-15 14:02:14 -0800391 NetworkInfo networkInfo = getConnectivityManager().getActiveNetworkInfo();
392 return (networkInfo != null) && networkInfo.isConnected();
393 }
394
Dianne Hackborn55280a92009-05-07 15:53:46 -0700395 private BroadcastReceiver mShutdownIntentReceiver =
396 new BroadcastReceiver() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000397 @Override
398 public void onReceive(Context context, Intent intent) {
399 Log.w(TAG, "Writing sync state before shutdown...");
400 getSyncStorageEngine().writeAllState();
401 }
402 };
Dianne Hackborn55280a92009-05-07 15:53:46 -0700403
Amith Yamasani13593602012-03-22 16:16:17 -0700404 private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() {
405 @Override
406 public void onReceive(Context context, Intent intent) {
Alon Albert8e285552012-09-17 15:05:27 -0700407 String action = intent.getAction();
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700408 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL);
409 if (userId == UserHandle.USER_NULL) return;
410
Alon Albert8e285552012-09-17 15:05:27 -0700411 if (Intent.ACTION_USER_REMOVED.equals(action)) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700412 onUserRemoved(userId);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700413 } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) {
414 onUserUnlocked(userId);
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -0700415 } else if (Intent.ACTION_USER_STOPPED.equals(action)) {
416 onUserStopped(userId);
Alon Albert8e285552012-09-17 15:05:27 -0700417 }
Amith Yamasani13593602012-03-22 16:16:17 -0700418 }
419 };
420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 private final SyncHandler mSyncHandler;
422
Fred Quintana4f9cfc52009-09-02 15:20:23 -0700423 private volatile boolean mBootCompleted = false;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +0100424 private volatile boolean mJobServiceReady = false;
Fred Quintana4f9cfc52009-09-02 15:20:23 -0700425
Fred Quintanaf892fb32009-08-27 21:32:08 -0700426 private ConnectivityManager getConnectivityManager() {
427 synchronized (this) {
428 if (mConnManagerDoNotUseDirectly == null) {
429 mConnManagerDoNotUseDirectly = (ConnectivityManager)mContext.getSystemService(
430 Context.CONNECTIVITY_SERVICE);
431 }
432 return mConnManagerDoNotUseDirectly;
433 }
434 }
435
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000436 /**
437 * Cancel all unnecessary jobs. This function will be run once after every boot.
438 */
439 private void cleanupJobs() {
440 // O(n^2) in number of jobs, so we run this on the background thread.
441 mSyncHandler.postAtFrontOfQueue(new Runnable() {
442 @Override
443 public void run() {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000444 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000445 Set<String> cleanedKeys = new HashSet<String>();
446 for (SyncOperation opx: ops) {
447 if (cleanedKeys.contains(opx.key)) {
448 continue;
449 }
450 cleanedKeys.add(opx.key);
451 for (SyncOperation opy: ops) {
452 if (opx == opy) {
453 continue;
454 }
455 if (opx.key.equals(opy.key)) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000456 mJobScheduler.cancel(opy.jobId);
457 }
458 }
459 }
460 }
461 });
462 }
463
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000464 private synchronized void verifyJobScheduler() {
465 if (mJobScheduler != null) {
466 return;
467 }
468 if (Log.isLoggable(TAG, Log.VERBOSE)) {
469 Log.d(TAG, "initializing JobScheduler object.");
470 }
471 mJobScheduler = (JobScheduler) mContext.getSystemService(
472 Context.JOB_SCHEDULER_SERVICE);
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000473 mJobSchedulerInternal = LocalServices.getService(JobSchedulerInternal.class);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000474 // Get all persisted syncs from JobScheduler
475 List<JobInfo> pendingJobs = mJobScheduler.getAllPendingJobs();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000476 for (JobInfo job : pendingJobs) {
477 SyncOperation op = SyncOperation.maybeCreateFromJobExtras(job.getExtras());
478 if (op != null) {
479 if (!op.isPeriodic) {
480 // Set the pending status of this EndPoint to true. Pending icon is
481 // shown on the settings activity.
482 mSyncStorageEngine.markPending(op.target, true);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000483 }
484 }
485 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +0000486 cleanupJobs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000487 }
488
489 private JobScheduler getJobScheduler() {
490 verifyJobScheduler();
491 return mJobScheduler;
492 }
493
Jeff Sharkeye4996bb2012-10-17 14:16:28 -0700494 /**
495 * Should only be created after {@link ContentService#systemReady()} so that
496 * {@link PackageManager} is ready to query.
497 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 public SyncManager(Context context, boolean factoryTest) {
499 // Initialize the SyncStorageEngine first, before registering observers
500 // and creating threads and so on; it may fail if the disk is full.
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700501 mContext = context;
Amith Yamasani9535c912012-10-10 21:48:33 -0700502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 SyncStorageEngine.init(context);
504 mSyncStorageEngine = SyncStorageEngine.getSingleton();
Amith Yamasani04e0d262012-02-14 11:50:53 -0800505 mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() {
Matthew Williamsfa774182013-06-18 15:44:11 -0700506 @Override
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700507 public void onSyncRequest(SyncStorageEngine.EndPoint info, int reason, Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000508 scheduleSync(info.account, info.userId, reason, info.provider, extras,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700509 AuthorityInfo.UNDEFINED);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000510 }
511 });
512
513 mSyncStorageEngine.setPeriodicSyncAddedListener(
514 new SyncStorageEngine.PeriodicSyncAddedListener() {
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +0000515 @Override
516 public void onPeriodicSyncAdded(EndPoint target, Bundle extras, long pollFrequency,
517 long flex) {
518 updateOrAddPeriodicSync(target, pollFrequency, flex, extras);
519 }
520 });
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000521
522 mSyncStorageEngine.setOnAuthorityRemovedListener(new SyncStorageEngine.OnAuthorityRemovedListener() {
523 @Override
524 public void onAuthorityRemoved(EndPoint removedAuthority) {
525 removeSyncsForAuthority(removedAuthority);
Amith Yamasani04e0d262012-02-14 11:50:53 -0800526 }
527 });
528
Fred Quintana0c4d04a2010-11-03 17:02:55 -0700529 mSyncAdapters = new SyncAdaptersCache(mContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530
Dianne Hackborn8d044e82013-04-30 17:24:15 -0700531 mSyncHandler = new SyncHandler(BackgroundThread.get().getLooper());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532
Fred Quintana44037e62010-01-21 13:14:49 -0800533 mSyncAdapters.setListener(new RegisteredServicesCacheListener<SyncAdapterType>() {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -0700534 @Override
535 public void onServiceChanged(SyncAdapterType type, int userId, boolean removed) {
Fred Quintana44037e62010-01-21 13:14:49 -0800536 if (!removed) {
Alon Albert57286f92012-10-09 14:21:38 -0700537 scheduleSync(null, UserHandle.USER_ALL,
538 SyncOperation.REASON_SERVICE_CHANGED,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700539 type.authority, null, AuthorityInfo.UNDEFINED);
Fred Quintana44037e62010-01-21 13:14:49 -0800540 }
541 }
542 }, mSyncHandler);
Fred Quintana718d8a22009-04-29 17:53:20 -0700543
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000544 mRand = new Random(System.currentTimeMillis());
Amith Yamasani96a0fd652015-04-10 16:16:30 -0700545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
547 context.registerReceiver(mConnectivityIntentReceiver, intentFilter);
548
Fred Quintanae91ebe22009-09-29 20:44:30 -0700549 if (!factoryTest) {
550 intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
Dianne Hackbornd83a0962014-05-02 16:28:33 -0700551 intentFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
Fred Quintanae91ebe22009-09-29 20:44:30 -0700552 context.registerReceiver(mBootCompletedReceiver, intentFilter);
553 }
Fred Quintana60307342009-03-24 22:48:12 -0700554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW);
556 intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK);
557 context.registerReceiver(mStorageIntentReceiver, intentFilter);
558
Dianne Hackborn55280a92009-05-07 15:53:46 -0700559 intentFilter = new IntentFilter(Intent.ACTION_SHUTDOWN);
560 intentFilter.setPriority(100);
561 context.registerReceiver(mShutdownIntentReceiver, intentFilter);
562
Amith Yamasani13593602012-03-22 16:16:17 -0700563 intentFilter = new IntentFilter();
564 intentFilter.addAction(Intent.ACTION_USER_REMOVED);
Jeff Sharkey9d8a1042015-12-03 17:56:20 -0700565 intentFilter.addAction(Intent.ACTION_USER_UNLOCKED);
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -0700566 intentFilter.addAction(Intent.ACTION_USER_STOPPED);
Alon Albert8e285552012-09-17 15:05:27 -0700567 mContext.registerReceiverAsUser(
568 mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null);
Amith Yamasani13593602012-03-22 16:16:17 -0700569
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 if (!factoryTest) {
571 mNotificationMgr = (NotificationManager)
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000572 context.getSystemService(Context.NOTIFICATION_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 } else {
574 mNotificationMgr = null;
575 }
Fred Quintanab3029c32010-04-06 13:27:12 -0700576 mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
Amith Yamasani9535c912012-10-10 21:48:33 -0700577 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700578 mAccountManager = (AccountManager) mContext.getSystemService(Context.ACCOUNT_SERVICE);
579 mAccountManagerInternal = LocalServices.getService(AccountManagerInternal.class);
Svet Ganov973edd192016-09-08 20:15:55 -0700580 mPackageManagerInternal = LocalServices.getService(PackageManagerInternal.class);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700581
Svet Ganovf6d424f12016-09-20 20:18:53 -0700582 mAccountManagerInternal.addOnAppPermissionChangeListener((Account account, int uid) -> {
583 // If the UID gained access to the account kick-off syncs lacking account access
584 if (mAccountManagerInternal.hasAccountAccess(account, uid)) {
585 scheduleSync(account, UserHandle.getUserId(uid),
586 SyncOperation.REASON_ACCOUNTS_UPDATED,
587 null, null, AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS);
588 }
589 });
590
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -0800591 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
592 BatteryStats.SERVICE_NAME));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593
594 // This WakeLock is used to ensure that we stay awake between the time that we receive
595 // a sync alarm notification and when we finish processing it. We need to do this
596 // because we don't do the work in the alarm handler, rather we do it in a message
597 // handler.
Fred Quintanab3029c32010-04-06 13:27:12 -0700598 mHandleAlarmWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 HANDLE_SYNC_ALARM_WAKE_LOCK);
600 mHandleAlarmWakeLock.setReferenceCounted(false);
601
Fred Quintana918339a2010-10-05 14:00:39 -0700602 // This WakeLock is used to ensure that we stay awake while running the sync loop
603 // message handler. Normally we will hold a sync adapter wake lock while it is being
604 // synced but during the execution of the sync loop it might finish a sync for
605 // one sync adapter before starting the sync for the other sync adapter and we
606 // don't want the device to go to sleep during that window.
607 mSyncManagerWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
608 SYNC_LOOP_WAKE_LOCK);
609 mSyncManagerWakeLock.setReferenceCounted(false);
610
Matthew Williams8b76d202015-05-03 18:16:25 -0700611 mProvisioned = isDeviceProvisioned();
612 if (!mProvisioned) {
613 final ContentResolver resolver = context.getContentResolver();
614 ContentObserver provisionedObserver =
615 new ContentObserver(null /* current thread */) {
616 public void onChange(boolean selfChange) {
617 mProvisioned |= isDeviceProvisioned();
618 if (mProvisioned) {
619 mSyncHandler.onDeviceProvisioned();
620 resolver.unregisterContentObserver(this);
621 }
622 }
623 };
624
625 synchronized (mSyncHandler) {
626 resolver.registerContentObserver(
627 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED),
628 false /* notifyForDescendents */,
629 provisionedObserver);
630
631 // The device *may* have been provisioned while we were registering above observer.
632 // Check again to make sure.
633 mProvisioned |= isDeviceProvisioned();
634 if (mProvisioned) {
635 resolver.unregisterContentObserver(provisionedObserver);
636 }
Dianne Hackborn231cc602009-04-27 17:10:36 -0700637 }
Matthew Williams8b76d202015-05-03 18:16:25 -0700638 }
Fred Quintanae91ebe22009-09-29 20:44:30 -0700639
640 if (!factoryTest) {
Amith Yamasanid648a602012-09-26 15:06:10 -0700641 // Register for account list updates for all users
642 mContext.registerReceiverAsUser(mAccountsUpdatedReceiver,
643 UserHandle.ALL,
644 new IntentFilter(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION),
Matthew Williams5c6756f2014-10-02 04:12:28 +0000645 null, null);
Fred Quintanae91ebe22009-09-29 20:44:30 -0700646 }
Ashish Sharma69d95de2012-04-11 17:27:24 -0700647
Christopher Tate734c91d2016-02-02 12:46:18 -0800648 // Set up the communication channel between the scheduled job and the sync manager.
649 // This is posted to the *main* looper intentionally, to defer calling startService()
650 // until after the lengthy primary boot sequence completes on that thread, to avoid
651 // spurious ANR triggering.
652 final Intent startServiceIntent = new Intent(mContext, SyncJobService.class);
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000653 startServiceIntent.putExtra(SyncJobService.EXTRA_MESSENGER, new Messenger(mSyncHandler));
Christopher Tate734c91d2016-02-02 12:46:18 -0800654 new Handler(mContext.getMainLooper()).post(new Runnable() {
655 @Override
656 public void run() {
657 mContext.startService(startServiceIntent);
658 }
659 });
Svet Ganov65712b02016-09-01 10:24:11 -0700660
661 // Sync adapters were able to access the synced account without the accounts
662 // permission which circumvents our permission model. Therefore, we require
663 // sync adapters that don't have access to the account to get user consent.
664 // This can be noisy, therefore we will white-list sync adapters installed
665 // before we started checking for account access because they already know
666 // the account (they run before) which is the genie is out of the bottle.
667 whiteListExistingSyncAdaptersIfNeeded();
668 }
669
670 private void whiteListExistingSyncAdaptersIfNeeded() {
671 if (!mSyncStorageEngine.shouldGrantSyncAdaptersAccountAccess()) {
672 return;
673 }
674 List<UserInfo> users = mUserManager.getUsers(true);
675 final int userCount = users.size();
676 for (int i = 0; i < userCount; i++) {
677 UserHandle userHandle = users.get(i).getUserHandle();
678 final int userId = userHandle.getIdentifier();
679 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> service
680 : mSyncAdapters.getAllServices(userId)) {
681 String packageName = service.componentName.getPackageName();
682 for (Account account : mAccountManager.getAccountsByTypeAsUser(
683 service.type.accountType, userHandle)) {
684 if (!canAccessAccount(account, packageName, userId)) {
685 mAccountManager.updateAppPermission(account,
Svet Ganovf6d424f12016-09-20 20:18:53 -0700686 AccountManager.ACCOUNT_ACCESS_TOKEN_TYPE, service.uid, true);
Svet Ganov65712b02016-09-01 10:24:11 -0700687 }
688 }
689 }
690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 }
692
Matthew Williams8b76d202015-05-03 18:16:25 -0700693 private boolean isDeviceProvisioned() {
694 final ContentResolver resolver = mContext.getContentResolver();
695 return (Settings.Global.getInt(resolver, Settings.Global.DEVICE_PROVISIONED, 0) != 0);
696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 /**
698 * Return a random value v that satisfies minValue <= v < maxValue. The difference between
699 * maxValue and minValue must be less than Integer.MAX_VALUE.
700 */
701 private long jitterize(long minValue, long maxValue) {
702 Random random = new Random(SystemClock.elapsedRealtime());
703 long spread = maxValue - minValue;
704 if (spread > Integer.MAX_VALUE) {
705 throw new IllegalArgumentException("the difference between the maxValue and the "
706 + "minValue must be less than " + Integer.MAX_VALUE);
707 }
708 return minValue + random.nextInt((int)spread);
709 }
710
Dianne Hackborn231cc602009-04-27 17:10:36 -0700711 public SyncStorageEngine getSyncStorageEngine() {
712 return mSyncStorageEngine;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
Doug Zongker44f57472009-09-20 15:52:43 -0700714
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700715 private int getIsSyncable(Account account, int userId, String providerName) {
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700716 int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName);
717 UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId);
718
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000719 // If it's not a restricted user, return isSyncable.
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700720 if (userInfo == null || !userInfo.isRestricted()) return isSyncable;
721
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000722 // Else check if the sync adapter has opted-in or not.
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700723 RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
724 mSyncAdapters.getServiceInfo(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000725 SyncAdapterType.newKey(providerName, account.type), userId);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700726 if (syncAdapterInfo == null) return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700727
728 PackageInfo pInfo = null;
729 try {
730 pInfo = AppGlobals.getPackageManager().getPackageInfo(
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000731 syncAdapterInfo.componentName.getPackageName(), 0, userId);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700732 if (pInfo == null) return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700733 } catch (RemoteException re) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000734 // Shouldn't happen.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700735 return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700736 }
737 if (pInfo.restrictedAccountType != null
738 && pInfo.restrictedAccountType.equals(account.type)) {
739 return isSyncable;
740 } else {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700741 return AuthorityInfo.NOT_SYNCABLE;
Amith Yamasani9422bdc2013-04-10 16:58:19 -0700742 }
743 }
744
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000745 private void setAuthorityPendingState(EndPoint info) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +0000746 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000747 for (SyncOperation op: ops) {
748 if (!op.isPeriodic && op.target.matchesSpec(info)) {
749 getSyncStorageEngine().markPending(info, true);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700750 return;
751 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700752 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000753 getSyncStorageEngine().markPending(info, false);
Matthew Williamsfa774182013-06-18 15:44:11 -0700754 }
755
756 /**
757 * Initiate a sync. This can start a sync for all providers
758 * (pass null to url, set onlyTicklable to false), only those
759 * providers that are marked as ticklable (pass null to url,
760 * set onlyTicklable to true), or a specific provider (set url
761 * to the content url of the provider).
762 *
763 * <p>If the ContentResolver.SYNC_EXTRAS_UPLOAD boolean in extras is
764 * true then initiate a sync that just checks for local changes to send
765 * to the server, otherwise initiate a sync that first gets any
766 * changes from the server before sending local changes back to
767 * the server.
768 *
769 * <p>If a specific provider is being synced (the url is non-null)
770 * then the extras can contain SyncAdapter-specific information
771 * to control what gets synced (e.g. which specific feed to sync).
772 *
773 * <p>You'll start getting callbacks after this.
774 *
775 * @param requestedAccount the account to sync, may be null to signify all accounts
776 * @param userId the id of the user whose accounts are to be synced. If userId is USER_ALL,
777 * then all users' accounts are considered.
778 * @param reason for sync request. If this is a positive integer, it is the Linux uid
779 * assigned to the process that requested the sync. If it's negative, the sync was requested by
780 * the SyncManager itself and could be one of the following:
781 * {@link SyncOperation#REASON_BACKGROUND_DATA_SETTINGS_CHANGED}
782 * {@link SyncOperation#REASON_ACCOUNTS_UPDATED}
783 * {@link SyncOperation#REASON_SERVICE_CHANGED}
784 * {@link SyncOperation#REASON_PERIODIC}
785 * {@link SyncOperation#REASON_IS_SYNCABLE}
786 * {@link SyncOperation#REASON_SYNC_AUTO}
787 * {@link SyncOperation#REASON_MASTER_SYNC_AUTO}
788 * {@link SyncOperation#REASON_USER_START}
789 * @param requestedAuthority the authority to sync, may be null to indicate all authorities
790 * @param extras a Map of SyncAdapter-specific information to control
791 * syncs of a specific provider. Can be null. Is ignored
792 * if the url is null.
Svet Ganovf6d424f12016-09-20 20:18:53 -0700793 * @param targetSyncState Only sync authorities that have the specified sync state.
794 * Use {@link AuthorityInfo#UNDEFINED} to sync all authorities.
Matthew Williamsfa774182013-06-18 15:44:11 -0700795 */
796 public void scheduleSync(Account requestedAccount, int userId, int reason,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100797 String requestedAuthority, Bundle extras, int targetSyncState) {
798 scheduleSync(requestedAccount, userId, reason, requestedAuthority, extras, targetSyncState,
799 0 /* min delay */);
800 }
801
802 /**
803 * @param minDelayMillis The sync can't land before this delay expires.
804 */
805 private void scheduleSync(Account requestedAccount, int userId, int reason,
806 String requestedAuthority, Bundle extras, int targetSyncState,
807 final long minDelayMillis) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000808 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Matthew Williamsfa774182013-06-18 15:44:11 -0700809 if (extras == null) {
810 extras = new Bundle();
811 }
812 if (isLoggable) {
813 Log.d(TAG, "one-time sync for: " + requestedAccount + " " + extras.toString() + " "
814 + requestedAuthority);
815 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700816
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700817 AccountAndUser[] accounts = null;
818 if (requestedAccount != null) {
819 if (userId != UserHandle.USER_ALL) {
820 accounts = new AccountAndUser[]{new AccountAndUser(requestedAccount, userId)};
821 } else {
822 for (AccountAndUser runningAccount : mRunningAccounts) {
823 if (requestedAccount.equals(runningAccount.account)) {
824 accounts = ArrayUtils.appendElement(AccountAndUser.class,
825 accounts, runningAccount);
826 }
827 }
828 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700829 } else {
Matthew Williamsfa774182013-06-18 15:44:11 -0700830 accounts = mRunningAccounts;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700831 }
832
833 if (ArrayUtils.isEmpty(accounts)) {
834 if (isLoggable) {
835 Slog.v(TAG, "scheduleSync: no accounts configured, dropping");
Matthew Williamsfa774182013-06-18 15:44:11 -0700836 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700837 return;
Matthew Williamsfa774182013-06-18 15:44:11 -0700838 }
839
840 final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false);
841 final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false);
842 if (manualSync) {
843 extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true);
844 extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true);
845 }
846 final boolean ignoreSettings =
847 extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false);
848
849 int source;
850 if (uploadOnly) {
851 source = SyncStorageEngine.SOURCE_LOCAL;
852 } else if (manualSync) {
853 source = SyncStorageEngine.SOURCE_USER;
854 } else if (requestedAuthority == null) {
855 source = SyncStorageEngine.SOURCE_POLL;
856 } else {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000857 // This isn't strictly server, since arbitrary callers can (and do) request
858 // a non-forced two-way sync on a specific url.
Matthew Williamsfa774182013-06-18 15:44:11 -0700859 source = SyncStorageEngine.SOURCE_SERVER;
860 }
861
862 for (AccountAndUser account : accounts) {
Fyodor Kupolov6fde2982015-01-06 17:46:37 -0800863 // If userId is specified, do not sync accounts of other users
Xiaohui Chen98404fd2015-08-17 16:09:02 -0700864 if (userId >= UserHandle.USER_SYSTEM && account.userId >= UserHandle.USER_SYSTEM
Fyodor Kupolov6fde2982015-01-06 17:46:37 -0800865 && userId != account.userId) {
866 continue;
867 }
Matthew Williamsfa774182013-06-18 15:44:11 -0700868 // Compile a list of authorities that have sync adapters.
869 // For each authority sync each account that matches a sync adapter.
870 final HashSet<String> syncableAuthorities = new HashSet<String>();
871 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapter :
872 mSyncAdapters.getAllServices(account.userId)) {
873 syncableAuthorities.add(syncAdapter.type.authority);
874 }
875
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000876 // If the url was specified then replace the list of authorities
Matthew Williamsfa774182013-06-18 15:44:11 -0700877 // with just this authority or clear it if this authority isn't
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000878 // syncable.
Matthew Williamsfa774182013-06-18 15:44:11 -0700879 if (requestedAuthority != null) {
880 final boolean hasSyncAdapter = syncableAuthorities.contains(requestedAuthority);
881 syncableAuthorities.clear();
882 if (hasSyncAdapter) syncableAuthorities.add(requestedAuthority);
883 }
884
885 for (String authority : syncableAuthorities) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700886 int isSyncable = computeSyncable(account.account, account.userId, authority);
887
Matthew Williams53abfdb2015-06-10 20:06:37 -0700888 if (isSyncable == AuthorityInfo.NOT_SYNCABLE) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700889 continue;
890 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700891
892 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
893 mSyncAdapters.getServiceInfo(SyncAdapterType.newKey(authority,
894 account.account.type), account.userId);
Matthew Williamsfa774182013-06-18 15:44:11 -0700895 if (syncAdapterInfo == null) {
896 continue;
897 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700898
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700899 final int owningUid = syncAdapterInfo.uid;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700900
901 if (isSyncable == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
902 if (isLoggable) {
903 Slog.v(TAG, " Not scheduling sync operation: "
904 + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700905 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700906 Bundle finalExtras = new Bundle(extras);
Svet Ganov973edd192016-09-08 20:15:55 -0700907 String packageName = syncAdapterInfo.componentName.getPackageName();
908 // If the app did not run and has no account access, done
909 if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
910 continue;
911 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700912 mAccountManagerInternal.requestAccountAccess(account.account,
Svet Ganov973edd192016-09-08 20:15:55 -0700913 packageName, userId,
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700914 new RemoteCallback((Bundle result) -> {
915 if (result != null
916 && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
917 scheduleSync(account.account, userId, reason, authority,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100918 finalExtras, targetSyncState, minDelayMillis);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700919 }
920 }
921 ));
922 continue;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700923 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700924
Matthew Williamsfa774182013-06-18 15:44:11 -0700925 final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs();
926 final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable();
927 if (isSyncable < 0 && isAlwaysSyncable) {
Matthew Williams53abfdb2015-06-10 20:06:37 -0700928 mSyncStorageEngine.setIsSyncable(
929 account.account, account.userId, authority, AuthorityInfo.SYNCABLE);
930 isSyncable = AuthorityInfo.SYNCABLE;
Matthew Williamsfa774182013-06-18 15:44:11 -0700931 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700932
Svet Ganovf6d424f12016-09-20 20:18:53 -0700933 if (targetSyncState != AuthorityInfo.UNDEFINED && targetSyncState != isSyncable) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700934 continue;
935 }
Svet Ganovf6d424f12016-09-20 20:18:53 -0700936
Matthew Williamsfa774182013-06-18 15:44:11 -0700937 if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) {
938 continue;
939 }
940
Matthew Williamsfa774182013-06-18 15:44:11 -0700941 boolean syncAllowed =
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000942 (isSyncable < 0) // Always allow if the isSyncable state is unknown.
943 || ignoreSettings
944 || (mSyncStorageEngine.getMasterSyncAutomatically(account.userId)
Matthew Williamsfa774182013-06-18 15:44:11 -0700945 && mSyncStorageEngine.getSyncAutomatically(account.account,
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000946 account.userId, authority));
Matthew Williamsfa774182013-06-18 15:44:11 -0700947 if (!syncAllowed) {
948 if (isLoggable) {
949 Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority
950 + " is not allowed, dropping request");
951 }
952 continue;
953 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700954 SyncStorageEngine.EndPoint info =
955 new SyncStorageEngine.EndPoint(
956 account.account, authority, account.userId);
Matthew Williams56dbf8f2013-07-26 12:56:39 -0700957 long delayUntil =
958 mSyncStorageEngine.getDelayUntilTime(info);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -0700959
960 final String owningPackage = syncAdapterInfo.componentName.getPackageName();
961
Svet Ganovf6d424f12016-09-20 20:18:53 -0700962 if (isSyncable == AuthorityInfo.NOT_INITIALIZED) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700963 // Initialisation sync.
964 Bundle newExtras = new Bundle();
965 newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true);
966 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000967 Slog.v(TAG, "schedule initialisation Sync:"
Matthew Williamsfa774182013-06-18 15:44:11 -0700968 + ", delay until " + delayUntil
969 + ", run by " + 0
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000970 + ", flexMillis " + 0
Matthew Williamsfa774182013-06-18 15:44:11 -0700971 + ", source " + source
972 + ", account " + account
973 + ", authority " + authority
974 + ", extras " + newExtras);
975 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000976 postScheduleSyncMessage(
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700977 new SyncOperation(account.account, account.userId,
978 owningUid, owningPackage, reason, source,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100979 authority, newExtras, allowParallelSyncs),
980 minDelayMillis
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000981 );
Svet Ganovf6d424f12016-09-20 20:18:53 -0700982 } else if (targetSyncState == AuthorityInfo.UNDEFINED
983 || targetSyncState == isSyncable) {
Matthew Williamsfa774182013-06-18 15:44:11 -0700984 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000985 Slog.v(TAG, "scheduleSync:"
Matthew Williamsfa774182013-06-18 15:44:11 -0700986 + " delay until " + delayUntil
Matthew Williamsfa774182013-06-18 15:44:11 -0700987 + ", source " + source
988 + ", account " + account
989 + ", authority " + authority
990 + ", extras " + extras);
991 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000992 postScheduleSyncMessage(
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700993 new SyncOperation(account.account, account.userId,
994 owningUid, owningPackage, reason, source,
Shreyas Basargeba1f7902016-10-01 00:19:44 +0100995 authority, extras, allowParallelSyncs),
996 minDelayMillis
Shreyas Basarge8c834c02016-01-07 13:53:16 +0000997 );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 }
999 }
1000 }
1001 }
1002
Svet Ganov96b9c752016-10-17 19:29:58 -07001003 private int computeSyncable(Account account, int userId, String authority) {
1004 return computeSyncable(account, userId, authority, true);
1005 }
1006
1007 public int computeSyncable(Account account, int userId, String authority,
1008 boolean checkAccountAccess) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001009 final int status = getIsSyncable(account, userId, authority);
1010 if (status == AuthorityInfo.NOT_SYNCABLE) {
1011 return AuthorityInfo.NOT_SYNCABLE;
1012 }
1013 final SyncAdapterType type = SyncAdapterType.newKey(authority, account.type);
1014 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo =
1015 mSyncAdapters.getServiceInfo(type, userId);
1016 if (syncAdapterInfo == null) {
1017 return AuthorityInfo.NOT_SYNCABLE;
1018 }
1019 final int owningUid = syncAdapterInfo.uid;
1020 final String owningPackage = syncAdapterInfo.componentName.getPackageName();
1021 try {
Dianne Hackbornc3af19a2017-01-20 17:00:44 -08001022 if (ActivityManager.getService().isAppStartModeDisabled(owningUid, owningPackage)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001023 Slog.w(TAG, "Not scheduling job " + syncAdapterInfo.uid + ":"
1024 + syncAdapterInfo.componentName
1025 + " -- package not allowed to start");
1026 return AuthorityInfo.NOT_SYNCABLE;
1027 }
1028 } catch (RemoteException e) {
1029 /* ignore - local call */
1030 }
Svet Ganov96b9c752016-10-17 19:29:58 -07001031 if (checkAccountAccess && !canAccessAccount(account, owningPackage, owningUid)) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07001032 Log.w(TAG, "Access to " + account + " denied for package "
1033 + owningPackage + " in UID " + syncAdapterInfo.uid);
1034 return AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS;
1035 }
1036
1037 return status;
1038 }
1039
1040 private boolean canAccessAccount(Account account, String packageName, int uid) {
1041 if (mAccountManager.hasAccountAccess(account, packageName,
1042 UserHandle.getUserHandleForUid(uid))) {
1043 return true;
1044 }
1045 // We relax the account access rule to also include the system apps as
1046 // they are trusted and we want to minimize the cases where the user
1047 // involvement is required to grant access to the synced account.
1048 try {
1049 mContext.getPackageManager().getApplicationInfoAsUser(packageName,
1050 PackageManager.MATCH_SYSTEM_ONLY, UserHandle.getUserId(uid));
1051 return true;
1052 } catch (NameNotFoundException e) {
1053 return false;
1054 }
1055 }
1056
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001057 private void removeSyncsForAuthority(EndPoint info) {
1058 verifyJobScheduler();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001059 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001060 for (SyncOperation op: ops) {
1061 if (op.target.matchesSpec(info)) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001062 getJobScheduler().cancel(op.jobId);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001063 }
1064 }
1065 }
1066
1067 /**
1068 * Remove a specific periodic sync identified by its target and extras.
1069 */
1070 public void removePeriodicSync(EndPoint target, Bundle extras) {
1071 Message m = mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_REMOVE_PERIODIC_SYNC, target);
1072 m.setData(extras);
1073 m.sendToTarget();
1074 }
1075
1076 /**
1077 * Add a periodic sync. If a sync with same target and extras exists, its period and
1078 * flexMillis will be updated.
1079 */
1080 public void updateOrAddPeriodicSync(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001081 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001082 UpdatePeriodicSyncMessagePayload payload = new UpdatePeriodicSyncMessagePayload(target,
1083 pollFrequency, flex, extras);
1084 mSyncHandler.obtainMessage(SyncHandler.MESSAGE_UPDATE_PERIODIC_SYNC, payload)
1085 .sendToTarget();
1086 }
1087
1088 /**
1089 * Get a list of periodic syncs corresponding to the given target.
1090 */
1091 public List<PeriodicSync> getPeriodicSyncs(EndPoint target) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001092 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001093 List<PeriodicSync> periodicSyncs = new ArrayList<PeriodicSync>();
1094
1095 for (SyncOperation op: ops) {
1096 if (op.isPeriodic && op.target.matchesSpec(target)) {
1097 periodicSyncs.add(new PeriodicSync(op.target.account, op.target.provider,
1098 op.extras, op.periodMillis / 1000, op.flexMillis / 1000));
1099 }
1100 }
1101
1102 return periodicSyncs;
1103 }
1104
Matthew Williamsfa774182013-06-18 15:44:11 -07001105 /**
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001106 * Schedule sync based on local changes to a provider. We wait for at least LOCAL_SYNC_DELAY
1107 * ms to batch syncs.
Matthew Williamsfa774182013-06-18 15:44:11 -07001108 */
Alon Albert57286f92012-10-09 14:21:38 -07001109 public void scheduleLocalSync(Account account, int userId, int reason, String authority) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 final Bundle extras = new Bundle();
1111 extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true);
Matthew Williamsfa774182013-06-18 15:44:11 -07001112 scheduleSync(account, userId, reason, authority, extras,
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001113 AuthorityInfo.UNDEFINED, LOCAL_SYNC_DELAY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001116 public SyncAdapterType[] getSyncAdapterTypes(int userId) {
1117 final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos;
1118 serviceInfos = mSyncAdapters.getAllServices(userId);
Fred Quintanaac9385e2009-06-22 18:00:59 -07001119 SyncAdapterType[] types = new SyncAdapterType[serviceInfos.size()];
1120 int i = 0;
1121 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> serviceInfo : serviceInfos) {
1122 types[i] = serviceInfo.type;
1123 ++i;
1124 }
1125 return types;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 }
1127
Amith Yamasani37a40c22015-06-17 13:25:42 -07001128 public String[] getSyncAdapterPackagesForAuthorityAsUser(String authority, int userId) {
1129 return mSyncAdapters.getSyncAdapterPackagesForAuthority(authority, userId);
1130 }
1131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 private void sendSyncFinishedOrCanceledMessage(ActiveSyncContext syncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001133 SyncResult syncResult) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001134 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_SYNC_FINISHED");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 Message msg = mSyncHandler.obtainMessage();
1136 msg.what = SyncHandler.MESSAGE_SYNC_FINISHED;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001137 msg.obj = new SyncFinishedOrCancelledMessagePayload(syncContext, syncResult);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 mSyncHandler.sendMessage(msg);
1139 }
1140
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001141 private void sendCancelSyncsMessage(final SyncStorageEngine.EndPoint info, Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001142 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "sending MESSAGE_CANCEL");
Fred Quintana918339a2010-10-05 14:00:39 -07001143 Message msg = mSyncHandler.obtainMessage();
1144 msg.what = SyncHandler.MESSAGE_CANCEL;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001145 msg.setData(extras);
1146 msg.obj = info;
Fred Quintana918339a2010-10-05 14:00:39 -07001147 mSyncHandler.sendMessage(msg);
1148 }
1149
Matthew Williams92a1c092014-08-25 19:18:32 -07001150 /**
Matthew Williams1967c8d2015-06-19 19:03:13 -07001151 * Post a delayed message that will monitor the given sync context by periodically checking how
1152 * much network has been used by the uid.
Matthew Williams92a1c092014-08-25 19:18:32 -07001153 */
Matthew Williams1967c8d2015-06-19 19:03:13 -07001154 private void postMonitorSyncProgressMessage(ActiveSyncContext activeSyncContext) {
Matthew Williams92a1c092014-08-25 19:18:32 -07001155 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001156 Slog.v(TAG, "posting MESSAGE_SYNC_MONITOR in " +
Matthew Williams1967c8d2015-06-19 19:03:13 -07001157 (SYNC_MONITOR_WINDOW_LENGTH_MILLIS/1000) + "s");
Matthew Williams92a1c092014-08-25 19:18:32 -07001158 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07001159
1160 activeSyncContext.mBytesTransferredAtLastPoll =
1161 getTotalBytesTransferredByUid(activeSyncContext.mSyncAdapterUid);
1162 activeSyncContext.mLastPolledTimeElapsed = SystemClock.elapsedRealtime();
1163 Message monitorMessage =
1164 mSyncHandler.obtainMessage(
1165 SyncHandler.MESSAGE_MONITOR_SYNC,
1166 activeSyncContext);
1167 mSyncHandler.sendMessageDelayed(monitorMessage, SYNC_MONITOR_WINDOW_LENGTH_MILLIS);
Matthew Williams92a1c092014-08-25 19:18:32 -07001168 }
1169
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001170 private void postScheduleSyncMessage(SyncOperation syncOperation, long minDelayMillis) {
1171 ScheduleSyncMessagePayload payload =
1172 new ScheduleSyncMessagePayload(syncOperation, minDelayMillis);
1173 mSyncHandler.obtainMessage(mSyncHandler.MESSAGE_SCHEDULE_SYNC, payload).sendToTarget();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001174 }
1175
Matthew Williams1967c8d2015-06-19 19:03:13 -07001176 /**
1177 * Monitor sync progress by calculating how many bytes it is managing to send to and fro.
1178 */
1179 private long getTotalBytesTransferredByUid(int uid) {
1180 return (TrafficStats.getUidRxBytes(uid) + TrafficStats.getUidTxBytes(uid));
1181 }
1182
1183 /**
1184 * Convenience class for passing parameters for a finished or cancelled sync to the handler
1185 * to be processed.
1186 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001187 private class SyncFinishedOrCancelledMessagePayload {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 public final ActiveSyncContext activeSyncContext;
1189 public final SyncResult syncResult;
1190
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001191 SyncFinishedOrCancelledMessagePayload(ActiveSyncContext syncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001192 SyncResult syncResult) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001193 this.activeSyncContext = syncContext;
1194 this.syncResult = syncResult;
1195 }
1196 }
1197
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001198 private class UpdatePeriodicSyncMessagePayload {
1199 public final EndPoint target;
1200 public final long pollFrequency;
1201 public final long flex;
1202 public final Bundle extras;
1203
1204 UpdatePeriodicSyncMessagePayload(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001205 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001206 this.target = target;
1207 this.pollFrequency = pollFrequency;
1208 this.flex = flex;
1209 this.extras = extras;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 }
1211 }
1212
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001213 private static class ScheduleSyncMessagePayload {
1214 final SyncOperation syncOperation;
1215 final long minDelayMillis;
1216
1217 ScheduleSyncMessagePayload(SyncOperation syncOperation, long minDelayMillis) {
1218 this.syncOperation = syncOperation;
1219 this.minDelayMillis = minDelayMillis;
1220 }
1221 }
1222
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001223 private void clearBackoffSetting(EndPoint target) {
1224 Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target);
1225 if (backoff != null && backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE &&
1226 backoff.second == SyncStorageEngine.NOT_IN_BACKOFF_MODE) {
1227 return;
1228 }
1229 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1230 Slog.v(TAG, "Clearing backoffs for " + target);
1231 }
1232 mSyncStorageEngine.setBackoff(target,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001233 SyncStorageEngine.NOT_IN_BACKOFF_MODE,
1234 SyncStorageEngine.NOT_IN_BACKOFF_MODE);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001235
1236 rescheduleSyncs(target);
Alon Albert6e079a32010-11-12 12:41:09 -08001237 }
1238
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001239 private void increaseBackoffSetting(EndPoint target) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001240 final long now = SystemClock.elapsedRealtime();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241
Fred Quintana307da1a2010-01-21 14:24:20 -08001242 final Pair<Long, Long> previousSettings =
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001243 mSyncStorageEngine.getBackoff(target);
Alon Albertaeeb6202010-12-09 16:14:02 -08001244 long newDelayInMs = -1;
1245 if (previousSettings != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001246 // Don't increase backoff before current backoff is expired. This will happen for op's
Alon Albertaeeb6202010-12-09 16:14:02 -08001247 // with ignoreBackoff set.
1248 if (now < previousSettings.first) {
1249 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001250 Slog.v(TAG, "Still in backoff, do not increase it. "
1251 + "Remaining: " + ((previousSettings.first - now) / 1000) + " seconds.");
Alon Albertaeeb6202010-12-09 16:14:02 -08001252 }
1253 return;
1254 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001255 // Subsequent delays are the double of the previous delay.
Alon Albertaeeb6202010-12-09 16:14:02 -08001256 newDelayInMs = previousSettings.second * 2;
1257 }
1258 if (newDelayInMs <= 0) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001259 // The initial delay is the jitterized INITIAL_SYNC_RETRY_TIME_IN_MS.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 newDelayInMs = jitterize(INITIAL_SYNC_RETRY_TIME_IN_MS,
1261 (long)(INITIAL_SYNC_RETRY_TIME_IN_MS * 1.1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 }
1263
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001264 // Cap the delay.
Jeff Sharkey625239a2012-09-26 22:03:49 -07001265 long maxSyncRetryTimeInSeconds = Settings.Global.getLong(mContext.getContentResolver(),
1266 Settings.Global.SYNC_MAX_RETRY_DELAY_IN_SECONDS,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS);
1268 if (newDelayInMs > maxSyncRetryTimeInSeconds * 1000) {
1269 newDelayInMs = maxSyncRetryTimeInSeconds * 1000;
1270 }
1271
Alon Albertc1ac7762010-10-28 13:35:55 -07001272 final long backoff = now + newDelayInMs;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001273 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1274 Slog.v(TAG, "Backoff until: " + backoff + ", delayTime: " + newDelayInMs);
1275 }
1276 mSyncStorageEngine.setBackoff(target, backoff, newDelayInMs);
1277 rescheduleSyncs(target);
1278 }
Alon Albertc1ac7762010-10-28 13:35:55 -07001279
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001280 /**
1281 * Reschedule all scheduled syncs for this EndPoint. The syncs will be scheduled according
1282 * to current backoff and delayUntil values of this EndPoint.
1283 */
1284 private void rescheduleSyncs(EndPoint target) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001285 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001286 int count = 0;
1287 for (SyncOperation op: ops) {
1288 if (!op.isPeriodic && op.target.matchesSpec(target)) {
1289 count++;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001290 getJobScheduler().cancel(op.jobId);
Shreyas Basargeba1f7902016-10-01 00:19:44 +01001291 postScheduleSyncMessage(op, 0 /* min delay */);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001292 }
1293 }
1294 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1295 Slog.v(TAG, "Rescheduled " + count + " syncs for " + target);
Fred Quintana918339a2010-10-05 14:00:39 -07001296 }
Fred Quintana307da1a2010-01-21 14:24:20 -08001297 }
1298
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001299 private void setDelayUntilTime(EndPoint target, long delayUntilSeconds) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001300 final long delayUntil = delayUntilSeconds * 1000;
1301 final long absoluteNow = System.currentTimeMillis();
1302 long newDelayUntilTime;
1303 if (delayUntil > absoluteNow) {
1304 newDelayUntilTime = SystemClock.elapsedRealtime() + (delayUntil - absoluteNow);
1305 } else {
1306 newDelayUntilTime = 0;
1307 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001308 mSyncStorageEngine.setDelayUntilTime(target, newDelayUntilTime);
1309 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1310 Slog.v(TAG, "Delay Until time set to " + newDelayUntilTime + " for " + target);
Fred Quintana918339a2010-10-05 14:00:39 -07001311 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001312 rescheduleSyncs(target);
1313 }
1314
1315 private boolean isAdapterDelayed(EndPoint target) {
1316 long now = SystemClock.elapsedRealtime();
1317 Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(target);
1318 if (backoff != null && backoff.first != SyncStorageEngine.NOT_IN_BACKOFF_MODE
1319 && backoff.first > now) {
1320 return true;
1321 }
1322 if (mSyncStorageEngine.getDelayUntilTime(target) > now) {
1323 return true;
1324 }
1325 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 }
1327
1328 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07001329 * Cancel the active sync if it matches the target.
1330 * @param info object containing info about which syncs to cancel. The target can
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001331 * have null account/provider info to specify all accounts/providers.
1332 * @param extras if non-null, specifies the exact sync to remove.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001334 public void cancelActiveSync(SyncStorageEngine.EndPoint info, Bundle extras) {
1335 sendCancelSyncsMessage(info, extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 }
1337
1338 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001339 * Schedule a sync operation with JobScheduler.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001341 private void scheduleSyncOperationH(SyncOperation syncOperation) {
1342 scheduleSyncOperationH(syncOperation, 0L);
1343 }
1344
1345 private void scheduleSyncOperationH(SyncOperation syncOperation, long minDelay) {
1346 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
1347 if (syncOperation == null) {
1348 Slog.e(TAG, "Can't schedule null sync operation.");
1349 return;
1350 }
1351 if (!syncOperation.ignoreBackoff()) {
1352 Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff(syncOperation.target);
1353 if (backoff == null) {
1354 Slog.e(TAG, "Couldn't find backoff values for " + syncOperation.target);
1355 backoff = new Pair<Long, Long>(SyncStorageEngine.NOT_IN_BACKOFF_MODE,
1356 SyncStorageEngine.NOT_IN_BACKOFF_MODE);
1357 }
1358 long now = SystemClock.elapsedRealtime();
1359 long backoffDelay = backoff.first == SyncStorageEngine.NOT_IN_BACKOFF_MODE ? 0
1360 : backoff.first - now;
1361 long delayUntil = mSyncStorageEngine.getDelayUntilTime(syncOperation.target);
1362 long delayUntilDelay = delayUntil > now ? delayUntil - now : 0;
1363 if (isLoggable) {
1364 Slog.v(TAG, "backoff delay:" + backoffDelay
1365 + " delayUntil delay:" + delayUntilDelay);
1366 }
1367 minDelay = Math.max(minDelay, Math.max(backoffDelay, delayUntilDelay));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001370 if (minDelay < 0) {
1371 minDelay = 0;
1372 }
1373
1374 // Check if duplicate syncs are pending. If found, keep one with least expected run time.
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001375 if (!syncOperation.isPeriodic) {
1376 // Check currently running syncs
1377 for (ActiveSyncContext asc: mActiveSyncContexts) {
1378 if (asc.mSyncOperation.key.equals(syncOperation.key)) {
1379 if (isLoggable) {
1380 Log.v(TAG, "Duplicate sync is already running. Not scheduling "
1381 + syncOperation);
1382 }
1383 return;
1384 }
1385 }
1386
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001387 int duplicatesCount = 0;
1388 long now = SystemClock.elapsedRealtime();
1389 syncOperation.expectedRuntime = now + minDelay;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001390 List<SyncOperation> pending = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001391 SyncOperation opWithLeastExpectedRuntime = syncOperation;
1392 for (SyncOperation op : pending) {
1393 if (op.isPeriodic) {
1394 continue;
1395 }
1396 if (op.key.equals(syncOperation.key)) {
1397 if (opWithLeastExpectedRuntime.expectedRuntime > op.expectedRuntime) {
1398 opWithLeastExpectedRuntime = op;
1399 }
1400 duplicatesCount++;
1401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001403 if (duplicatesCount > 1) {
1404 Slog.e(TAG, "FATAL ERROR! File a bug if you see this.");
1405 }
1406 for (SyncOperation op : pending) {
1407 if (op.isPeriodic) {
1408 continue;
1409 }
1410 if (op.key.equals(syncOperation.key)) {
1411 if (op != opWithLeastExpectedRuntime) {
1412 if (isLoggable) {
1413 Slog.v(TAG, "Cancelling duplicate sync " + op);
1414 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001415 getJobScheduler().cancel(op.jobId);
1416 }
1417 }
1418 }
1419 if (opWithLeastExpectedRuntime != syncOperation) {
1420 // Don't schedule because a duplicate sync with earlier expected runtime exists.
1421 if (isLoggable) {
1422 Slog.v(TAG, "Not scheduling because a duplicate exists.");
1423 }
1424 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 }
1426 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001427
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001428 // Syncs that are re-scheduled shouldn't get a new job id.
1429 if (syncOperation.jobId == SyncOperation.NO_JOB_ID) {
1430 syncOperation.jobId = getUnusedJobIdH();
1431 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001432
1433 if (isLoggable) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001434 Slog.v(TAG, "scheduling sync operation " + syncOperation.toString());
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001435 }
1436
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001437 int priority = syncOperation.findPriority();
1438
1439 final int networkType = syncOperation.isNotAllowedOnMetered() ?
1440 JobInfo.NETWORK_TYPE_UNMETERED : JobInfo.NETWORK_TYPE_ANY;
1441
1442 JobInfo.Builder b = new JobInfo.Builder(syncOperation.jobId,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001443 new ComponentName(mContext, SyncJobService.class))
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001444 .setExtras(syncOperation.toJobInfoExtras())
1445 .setRequiredNetworkType(networkType)
1446 .setPersisted(true)
1447 .setPriority(priority);
1448
1449 if (syncOperation.isPeriodic) {
1450 b.setPeriodic(syncOperation.periodMillis, syncOperation.flexMillis);
1451 } else {
1452 if (minDelay > 0) {
1453 b.setMinimumLatency(minDelay);
1454 }
1455 getSyncStorageEngine().markPending(syncOperation.target, true);
1456 }
1457
1458 if (syncOperation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_REQUIRE_CHARGING)) {
1459 b.setRequiresCharging(true);
1460 }
1461
1462 getJobScheduler().scheduleAsPackage(b.build(), syncOperation.owningPackage,
Shreyas Basargeeda34e42016-04-26 00:14:02 +01001463 syncOperation.target.userId, syncOperation.wakeLockName());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 }
1465
1466 /**
Fred Quintanaac9385e2009-06-22 18:00:59 -07001467 * Remove scheduled sync operations.
Matthew Williams8ef22042013-07-26 12:56:39 -07001468 * @param info limit the removals to operations that match this target. The target can
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001469 * have null account/provider info to specify all accounts/providers.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 */
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001471 public void clearScheduledSyncOperations(SyncStorageEngine.EndPoint info) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001472 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001473 for (SyncOperation op: ops) {
1474 if (!op.isPeriodic && op.target.matchesSpec(info)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001475 getJobScheduler().cancel(op.jobId);
1476 getSyncStorageEngine().markPending(op.target, false);
1477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001478 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001479 mSyncStorageEngine.setBackoff(info,
Fred Quintana918339a2010-10-05 14:00:39 -07001480 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001481 }
1482
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001483 /**
1484 * Remove a specified sync, if it exists.
1485 * @param info Authority for which the sync is to be removed.
1486 * @param extras extras bundle to uniquely identify sync.
1487 */
1488 public void cancelScheduledSyncOperation(SyncStorageEngine.EndPoint info, Bundle extras) {
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001489 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001490 for (SyncOperation op: ops) {
1491 if (!op.isPeriodic && op.target.matchesSpec(info)
1492 && syncExtrasEquals(extras, op.extras, false)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001493 getJobScheduler().cancel(op.jobId);
1494 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001495 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001496 setAuthorityPendingState(info);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001497 // Reset the back-off if there are no more syncs pending.
1498 if (!mSyncStorageEngine.isSyncPending(info)) {
1499 mSyncStorageEngine.setBackoff(info,
1500 SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE);
1501 }
1502 }
1503
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001504 private void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) {
1505 final boolean isLoggable = Log.isLoggable(TAG, Log.DEBUG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001506 if (isLoggable) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001507 Log.d(TAG, "encountered error(s) during the sync: " + syncResult + ", " + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 }
1509
Fred Quintana53bd2522010-02-05 15:28:12 -08001510 // The SYNC_EXTRAS_IGNORE_BACKOFF only applies to the first attempt to sync a given
1511 // request. Retries of the request will always honor the backoff, so clear the
1512 // flag in case we retry this request.
1513 if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)) {
1514 operation.extras.remove(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF);
1515 }
1516
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01001517 if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)
1518 && !syncResult.syncAlreadyInProgress) {
1519 // syncAlreadyInProgress flag is set by AbstractThreadedSyncAdapter. The sync adapter
1520 // has no way of knowing that a sync error occured. So we DO retry if the error is
1521 // syncAlreadyInProgress.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001522 if (isLoggable) {
1523 Log.d(TAG, "not retrying sync operation because SYNC_EXTRAS_DO_NOT_RETRY was specified "
1524 + operation);
1525 }
Fred Quintana918339a2010-10-05 14:00:39 -07001526 } else if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false)
1527 && !syncResult.syncAlreadyInProgress) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001528 // If this was an upward sync then schedule a two-way sync immediately.
Fred Quintana53bd2522010-02-05 15:28:12 -08001529 operation.extras.remove(ContentResolver.SYNC_EXTRAS_UPLOAD);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001530 if (isLoggable) {
1531 Log.d(TAG, "retrying sync operation as a two-way sync because an upload-only sync "
1532 + "encountered an error: " + operation);
1533 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001534 scheduleSyncOperationH(operation);
Fred Quintana307da1a2010-01-21 14:24:20 -08001535 } else if (syncResult.tooManyRetries) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001536 // If this sync aborted because the internal sync loop retried too many times then
1537 // don't reschedule. Otherwise we risk getting into a retry loop.
1538 if (isLoggable) {
1539 Log.d(TAG, "not retrying sync operation because it retried too many times: "
1540 + operation);
1541 }
Fred Quintanaaa7edda2009-12-03 14:18:58 -08001542 } else if (syncResult.madeSomeProgress()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001543 // If the operation succeeded to some extent then retry immediately.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 if (isLoggable) {
Fred Quintana307da1a2010-01-21 14:24:20 -08001545 Log.d(TAG, "retrying sync operation because even though it had an error "
1546 + "it achieved some success");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001547 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00001548 scheduleSyncOperationH(operation);
Fred Quintana8570f742010-02-18 10:32:54 -08001549 } else if (syncResult.syncAlreadyInProgress) {
1550 if (isLoggable) {
1551 Log.d(TAG, "retrying sync operation that failed because there was already a "
1552 + "sync in progress: " + operation);
1553 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001554 scheduleSyncOperationH(operation, DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS * 1000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 } else if (syncResult.hasSoftError()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001556 // If this was a two-way sync then retry soft errors with an exponential backoff.
Fred Quintana307da1a2010-01-21 14:24:20 -08001557 if (isLoggable) {
1558 Log.d(TAG, "retrying sync operation because it encountered a soft error: "
1559 + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001561 scheduleSyncOperationH(operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001562 } else {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001563 // Otherwise do not reschedule.
Fred Quintana307da1a2010-01-21 14:24:20 -08001564 Log.d(TAG, "not retrying sync operation because the error is a hard error: "
1565 + operation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001566 }
1567 }
1568
Jeff Sharkey9d8a1042015-12-03 17:56:20 -07001569 private void onUserUnlocked(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001570 // Make sure that accounts we're about to use are valid.
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001571 AccountManagerService.getSingleton().validateAccounts(userId);
1572
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001573 mSyncAdapters.invalidateCache(userId);
1574
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001575 EndPoint target = new EndPoint(null, null, userId);
1576 updateRunningAccounts(target);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001577
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001578 // Schedule sync for any accounts under started user.
Svetoslavf3f02ac2015-09-08 14:36:35 -07001579 final Account[] accounts = AccountManagerService.getSingleton().getAccounts(userId,
1580 mContext.getOpPackageName());
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001581 for (Account account : accounts) {
Alon Albert57286f92012-10-09 14:21:38 -07001582 scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null,
Svet Ganovf6d424f12016-09-20 20:18:53 -07001583 AuthorityInfo.NOT_INITIALIZED);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001584 }
Alon Albert8e285552012-09-17 15:05:27 -07001585 }
Amith Yamasani13593602012-03-22 16:16:17 -07001586
Amith Yamasaniad2e4bf2016-04-26 14:35:54 -07001587 private void onUserStopped(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001588 updateRunningAccounts(null /* Don't sync any target */);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001589
1590 cancelActiveSync(
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001591 new SyncStorageEngine.EndPoint(
1592 null /* any account */,
1593 null /* any authority */,
1594 userId),
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001595 null /* any sync. */
1596 );
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001597 }
1598
1599 private void onUserRemoved(int userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001600 updateRunningAccounts(null /* Don't sync any target */);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001601
Amith Yamasani13593602012-03-22 16:16:17 -07001602 // Clean up the storage engine database
1603 mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId);
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001604 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001605 for (SyncOperation op: ops) {
1606 if (op.target.userId == userId) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001607 getJobScheduler().cancel(op.jobId);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001608 }
1609 }
1610 }
1611
Amith Yamasani96a0fd652015-04-10 16:16:30 -07001612 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 * @hide
1614 */
Alon Alberteca75112010-12-08 15:02:33 -08001615 class ActiveSyncContext extends ISyncContext.Stub
1616 implements ServiceConnection, IBinder.DeathRecipient {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 final SyncOperation mSyncOperation;
1618 final long mHistoryRowId;
Fred Quintana718d8a22009-04-29 17:53:20 -07001619 ISyncAdapter mSyncAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 final long mStartTime;
1621 long mTimeoutStartTime;
Fred Quintana3ec47302010-03-10 10:08:31 -08001622 boolean mBound;
Fred Quintana918339a2010-10-05 14:00:39 -07001623 final PowerManager.WakeLock mSyncWakeLock;
1624 final int mSyncAdapterUid;
1625 SyncInfo mSyncInfo;
Alon Alberteca75112010-12-08 15:02:33 -08001626 boolean mIsLinkedToDeath = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001627 String mEventName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001628
Matthew Williams1967c8d2015-06-19 19:03:13 -07001629 /** Total bytes transferred, counted at {@link #mLastPolledTimeElapsed} */
1630 long mBytesTransferredAtLastPoll;
1631 /**
1632 * Last point in {@link SystemClock#elapsedRealtime()} at which we checked the # of bytes
1633 * transferred to/fro by this adapter.
1634 */
1635 long mLastPolledTimeElapsed;
1636
Fred Quintana918339a2010-10-05 14:00:39 -07001637 /**
1638 * Create an ActiveSyncContext for an impending sync and grab the wakelock for that
1639 * sync adapter. Since this grabs the wakelock you need to be sure to call
1640 * close() when you are done with this ActiveSyncContext, whether the sync succeeded
1641 * or not.
1642 * @param syncOperation the SyncOperation we are about to sync
1643 * @param historyRowId the row in which to record the history info for this sync
1644 * @param syncAdapterUid the UID of the application that contains the sync adapter
1645 * for this sync. This is used to attribute the wakelock hold to that application.
1646 */
1647 public ActiveSyncContext(SyncOperation syncOperation, long historyRowId,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001648 int syncAdapterUid) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 super();
Fred Quintana918339a2010-10-05 14:00:39 -07001650 mSyncAdapterUid = syncAdapterUid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001651 mSyncOperation = syncOperation;
1652 mHistoryRowId = historyRowId;
Fred Quintana718d8a22009-04-29 17:53:20 -07001653 mSyncAdapter = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 mStartTime = SystemClock.elapsedRealtime();
1655 mTimeoutStartTime = mStartTime;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001656 mSyncWakeLock = mSyncHandler.getSyncWakeLock(mSyncOperation);
Fred Quintana918339a2010-10-05 14:00:39 -07001657 mSyncWakeLock.setWorkSource(new WorkSource(syncAdapterUid));
1658 mSyncWakeLock.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
1660
1661 public void sendHeartbeat() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001662 // Heartbeats are no longer used.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664
1665 public void onFinished(SyncResult result) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001666 if (Log.isLoggable(TAG, Log.VERBOSE)) Slog.v(TAG, "onFinished: " + this);
1667 // Include "this" in the message so that the handler can ignore it if this
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 // ActiveSyncContext is no longer the mActiveSyncContext at message handling
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001669 // time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001670 sendSyncFinishedOrCanceledMessage(this, result);
1671 }
1672
1673 public void toString(StringBuilder sb) {
1674 sb.append("startTime ").append(mStartTime)
1675 .append(", mTimeoutStartTime ").append(mTimeoutStartTime)
1676 .append(", mHistoryRowId ").append(mHistoryRowId)
1677 .append(", syncOperation ").append(mSyncOperation);
1678 }
1679
Fred Quintana718d8a22009-04-29 17:53:20 -07001680 public void onServiceConnected(ComponentName name, IBinder service) {
1681 Message msg = mSyncHandler.obtainMessage();
1682 msg.what = SyncHandler.MESSAGE_SERVICE_CONNECTED;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001683 msg.obj = new ServiceConnectionData(this, service);
Fred Quintana718d8a22009-04-29 17:53:20 -07001684 mSyncHandler.sendMessage(msg);
1685 }
1686
1687 public void onServiceDisconnected(ComponentName name) {
1688 Message msg = mSyncHandler.obtainMessage();
1689 msg.what = SyncHandler.MESSAGE_SERVICE_DISCONNECTED;
1690 msg.obj = new ServiceConnectionData(this, null);
1691 mSyncHandler.sendMessage(msg);
1692 }
1693
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001694 boolean bindToSyncAdapter(ComponentName serviceComponent, int userId) {
Fred Quintana718d8a22009-04-29 17:53:20 -07001695 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001696 Log.d(TAG, "bindToSyncAdapter: " + serviceComponent + ", connection " + this);
Fred Quintana718d8a22009-04-29 17:53:20 -07001697 }
1698 Intent intent = new Intent();
1699 intent.setAction("android.content.SyncAdapter");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001700 intent.setComponent(serviceComponent);
Dianne Hackborndd9b82c2009-09-03 00:18:47 -07001701 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
1702 com.android.internal.R.string.sync_binding_label);
Dianne Hackborn41203752012-08-31 14:05:51 -07001703 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser(
1704 mContext, 0, new Intent(Settings.ACTION_SYNC_SETTINGS), 0,
1705 null, new UserHandle(userId)));
Fred Quintana3ec47302010-03-10 10:08:31 -08001706 mBound = true;
Amith Yamasani27b89e62013-01-16 12:30:11 -08001707 final boolean bindResult = mContext.bindServiceAsUser(intent, this,
Dianne Hackborne02c88a2011-10-28 13:58:15 -07001708 Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001709 | Context.BIND_ALLOW_OOM_MANAGEMENT,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001710 new UserHandle(mSyncOperation.target.userId));
Fred Quintana3ec47302010-03-10 10:08:31 -08001711 if (!bindResult) {
1712 mBound = false;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001713 } else {
1714 try {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08001715 mEventName = mSyncOperation.wakeLockName();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001716 mBatteryStats.noteSyncStart(mEventName, mSyncAdapterUid);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001717 } catch (RemoteException e) {
1718 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001719 }
1720 return bindResult;
Fred Quintana718d8a22009-04-29 17:53:20 -07001721 }
1722
Fred Quintana918339a2010-10-05 14:00:39 -07001723 /**
1724 * Performs the required cleanup, which is the releasing of the wakelock and
1725 * unbinding from the sync adapter (if actually bound).
1726 */
Fred Quintana3ec47302010-03-10 10:08:31 -08001727 protected void close() {
Fred Quintana718d8a22009-04-29 17:53:20 -07001728 if (Log.isLoggable(TAG, Log.VERBOSE)) {
1729 Log.d(TAG, "unBindFromSyncAdapter: connection " + this);
1730 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001731 if (mBound) {
1732 mBound = false;
1733 mContext.unbindService(this);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001734 try {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001735 mBatteryStats.noteSyncFinish(mEventName, mSyncAdapterUid);
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -08001736 } catch (RemoteException e) {
1737 }
Fred Quintana3ec47302010-03-10 10:08:31 -08001738 }
Fred Quintana918339a2010-10-05 14:00:39 -07001739 mSyncWakeLock.release();
Dianne Hackbornc24ab862011-10-18 15:55:03 -07001740 mSyncWakeLock.setWorkSource(null);
Fred Quintana718d8a22009-04-29 17:53:20 -07001741 }
1742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001743 public String toString() {
1744 StringBuilder sb = new StringBuilder();
1745 toString(sb);
1746 return sb.toString();
1747 }
Alon Alberteca75112010-12-08 15:02:33 -08001748
1749 @Override
1750 public void binderDied() {
1751 sendSyncFinishedOrCanceledMessage(this, null);
1752 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 }
1754
1755 protected void dump(FileDescriptor fd, PrintWriter pw) {
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001756 final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " ");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001757 dumpPendingSyncs(pw);
1758 dumpPeriodicSyncs(pw);
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001759 dumpSyncState(ipw);
1760 dumpSyncHistory(ipw);
1761 dumpSyncAdapters(ipw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 }
1763
Dianne Hackborn231cc602009-04-27 17:10:36 -07001764 static String formatTime(long time) {
1765 Time tobj = new Time();
1766 tobj.set(time);
1767 return tobj.format("%Y-%m-%d %H:%M:%S");
1768 }
Doug Zongker44f57472009-09-20 15:52:43 -07001769
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001770 protected void dumpPendingSyncs(PrintWriter pw) {
1771 pw.println("Pending Syncs:");
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001772 List<SyncOperation> pendingSyncs = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001773 int count = 0;
1774 for (SyncOperation op: pendingSyncs) {
1775 if (!op.isPeriodic) {
1776 pw.println(op.dump(null, false));
1777 count++;
1778 }
1779 }
1780 pw.println("Total: " + count);
1781 pw.println();
1782 }
1783
1784 protected void dumpPeriodicSyncs(PrintWriter pw) {
1785 pw.println("Periodic Syncs:");
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00001786 List<SyncOperation> pendingSyncs = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001787 int count = 0;
1788 for (SyncOperation op: pendingSyncs) {
1789 if (op.isPeriodic) {
1790 pw.println(op.dump(null, false));
1791 count++;
1792 }
1793 }
1794 pw.println("Total: " + count);
1795 pw.println();
1796 }
1797
Alon Alberte0bde332011-09-22 14:26:16 -07001798 protected void dumpSyncState(PrintWriter pw) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001799 pw.print("data connected: "); pw.println(mDataConnectionIsConnected);
Amith Yamasani04e0d262012-02-14 11:50:53 -08001800 pw.print("auto sync: ");
1801 List<UserInfo> users = getAllUsers();
1802 if (users != null) {
1803 for (UserInfo user : users) {
1804 pw.print("u" + user.id + "="
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07001805 + mSyncStorageEngine.getMasterSyncAutomatically(user.id) + " ");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001806 }
1807 pw.println();
1808 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07001809 pw.print("memory low: "); pw.println(mStorageIsLow);
Dianne Hackborn4870e9d2015-04-08 16:55:47 -07001810 pw.print("device idle: "); pw.println(mDeviceIsIdle);
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001811 pw.print("reported active: "); pw.println(mReportedSyncActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001813 final AccountAndUser[] accounts = AccountManagerService.getSingleton().getAllAccounts();
Amith Yamasani04e0d262012-02-14 11:50:53 -08001814
Jeff Sharkey6eb96202012-10-10 13:13:54 -07001815 pw.print("accounts: ");
Fred Quintana53bd2522010-02-05 15:28:12 -08001816 if (accounts != INITIAL_ACCOUNTS_ARRAY) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07001817 pw.println(accounts.length);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 } else {
Fred Quintana53bd2522010-02-05 15:28:12 -08001819 pw.println("not known yet");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 final long now = SystemClock.elapsedRealtime();
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001822 pw.print("now: "); pw.print(now);
1823 pw.println(" (" + formatTime(System.currentTimeMillis()) + ")");
Ashish Sharma69d95de2012-04-11 17:27:24 -07001824 pw.println(" (HH:MM:SS)");
Matthew Williams665d0142015-08-03 15:56:36 -07001825 pw.print("uptime: "); pw.print(DateUtils.formatElapsedTime(now / 1000));
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001826 pw.println(" (HH:MM:SS)");
Dianne Hackborn231cc602009-04-27 17:10:36 -07001827 pw.print("time spent syncing: ");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001828 pw.print(DateUtils.formatElapsedTime(
1829 mSyncHandler.mSyncTimeTracker.timeSpentSyncing() / 1000));
1830 pw.print(" (HH:MM:SS), sync ");
1831 pw.print(mSyncHandler.mSyncTimeTracker.mLastWasSyncing ? "" : "not ");
1832 pw.println("in progress");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833
Fred Quintana918339a2010-10-05 14:00:39 -07001834 pw.println();
1835 pw.println("Active Syncs: " + mActiveSyncContexts.size());
Alon Albert57286f92012-10-09 14:21:38 -07001836 final PackageManager pm = mContext.getPackageManager();
Fred Quintana918339a2010-10-05 14:00:39 -07001837 for (SyncManager.ActiveSyncContext activeSyncContext : mActiveSyncContexts) {
1838 final long durationInSeconds = (now - activeSyncContext.mStartTime) / 1000;
1839 pw.print(" ");
1840 pw.print(DateUtils.formatElapsedTime(durationInSeconds));
1841 pw.print(" - ");
Alon Albert57286f92012-10-09 14:21:38 -07001842 pw.print(activeSyncContext.mSyncOperation.dump(pm, false));
Fred Quintana918339a2010-10-05 14:00:39 -07001843 pw.println();
1844 }
1845
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001846 // Join the installed sync adapter with the accounts list and emit for everything.
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001847 pw.println();
1848 pw.println("Sync Status");
Amith Yamasani04e0d262012-02-14 11:50:53 -08001849 for (AccountAndUser account : accounts) {
Alon Albert57286f92012-10-09 14:21:38 -07001850 pw.printf("Account %s u%d %s\n",
1851 account.account.name, account.userId, account.account.type);
1852
1853 pw.println("=======================================================================");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001854 final PrintTable table = new PrintTable(12);
Alon Albert57286f92012-10-09 14:21:38 -07001855 table.set(0, 0,
1856 "Authority", // 0
1857 "Syncable", // 1
1858 "Enabled", // 2
1859 "Delay", // 3
1860 "Loc", // 4
1861 "Poll", // 5
1862 "Per", // 6
1863 "Serv", // 7
1864 "User", // 8
1865 "Tot", // 9
1866 "Time", // 10
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001867 "Last Sync" // 11
Alon Albert57286f92012-10-09 14:21:38 -07001868 );
1869
1870 final List<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> sorted =
1871 Lists.newArrayList();
1872 sorted.addAll(mSyncAdapters.getAllServices(account.userId));
1873 Collections.sort(sorted,
1874 new Comparator<RegisteredServicesCache.ServiceInfo<SyncAdapterType>>() {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001875 @Override
1876 public int compare(RegisteredServicesCache.ServiceInfo<SyncAdapterType> lhs,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00001877 RegisteredServicesCache.ServiceInfo<SyncAdapterType> rhs) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001878 return lhs.type.authority.compareTo(rhs.type.authority);
1879 }
1880 });
Alon Albert57286f92012-10-09 14:21:38 -07001881 for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterType : sorted) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08001882 if (!syncAdapterType.type.accountType.equals(account.account.type)) {
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001883 continue;
1884 }
Alon Albert57286f92012-10-09 14:21:38 -07001885 int row = table.getNumRows();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001886 Pair<AuthorityInfo, SyncStatusInfo> syncAuthoritySyncStatus =
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07001887 mSyncStorageEngine.getCopyOfAuthorityWithSyncStatus(
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001888 new SyncStorageEngine.EndPoint(
1889 account.account,
1890 syncAdapterType.type.authority,
1891 account.userId));
Georgi Nikolovdbe846b2013-06-25 14:09:56 -07001892 SyncStorageEngine.AuthorityInfo settings = syncAuthoritySyncStatus.first;
1893 SyncStatusInfo status = syncAuthoritySyncStatus.second;
Matthew Williams8ef22042013-07-26 12:56:39 -07001894 String authority = settings.target.provider;
Alon Albert57286f92012-10-09 14:21:38 -07001895 if (authority.length() > 50) {
1896 authority = authority.substring(authority.length() - 50);
1897 }
1898 table.set(row, 0, authority, settings.syncable, settings.enabled);
1899 table.set(row, 4,
1900 status.numSourceLocal,
1901 status.numSourcePoll,
1902 status.numSourcePeriodic,
1903 status.numSourceServer,
1904 status.numSourceUser,
1905 status.numSyncs,
1906 DateUtils.formatElapsedTime(status.totalElapsedTime / 1000));
1907
Alon Albert57286f92012-10-09 14:21:38 -07001908 int row1 = row;
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001909 if (settings.delayUntil > now) {
Alon Albert57286f92012-10-09 14:21:38 -07001910 table.set(row1++, 12, "D: " + (settings.delayUntil - now) / 1000);
1911 if (settings.backoffTime > now) {
1912 table.set(row1++, 12, "B: " + (settings.backoffTime - now) / 1000);
1913 table.set(row1++, 12, settings.backoffDelay / 1000);
1914 }
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001915 }
Alon Albert57286f92012-10-09 14:21:38 -07001916
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001917 if (status.lastSuccessTime != 0) {
Alon Albert57286f92012-10-09 14:21:38 -07001918 table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastSuccessSource]
1919 + " " + "SUCCESS");
1920 table.set(row1++, 11, formatTime(status.lastSuccessTime));
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08001921 }
1922 if (status.lastFailureTime != 0) {
Alon Albert57286f92012-10-09 14:21:38 -07001923 table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastFailureSource]
1924 + " " + "FAILURE");
1925 table.set(row1++, 11, formatTime(status.lastFailureTime));
1926 //noinspection UnusedAssignment
1927 table.set(row1++, 11, status.lastFailureMesg);
Dianne Hackborn231cc602009-04-27 17:10:36 -07001928 }
1929 }
Alon Albert57286f92012-10-09 14:21:38 -07001930 table.writeTo(pw);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 }
1932 }
1933
Dianne Hackborn231cc602009-04-27 17:10:36 -07001934 private void dumpTimeSec(PrintWriter pw, long time) {
1935 pw.print(time/1000); pw.print('.'); pw.print((time/100)%10);
1936 pw.print('s');
1937 }
Doug Zongker44f57472009-09-20 15:52:43 -07001938
Dianne Hackborn231cc602009-04-27 17:10:36 -07001939 private void dumpDayStatistic(PrintWriter pw, SyncStorageEngine.DayStats ds) {
1940 pw.print("Success ("); pw.print(ds.successCount);
1941 if (ds.successCount > 0) {
1942 pw.print(" for "); dumpTimeSec(pw, ds.successTime);
1943 pw.print(" avg="); dumpTimeSec(pw, ds.successTime/ds.successCount);
1944 }
1945 pw.print(") Failure ("); pw.print(ds.failureCount);
1946 if (ds.failureCount > 0) {
1947 pw.print(" for "); dumpTimeSec(pw, ds.failureTime);
1948 pw.print(" avg="); dumpTimeSec(pw, ds.failureTime/ds.failureCount);
1949 }
1950 pw.println(")");
1951 }
Doug Zongker44f57472009-09-20 15:52:43 -07001952
Alon Alberte0bde332011-09-22 14:26:16 -07001953 protected void dumpSyncHistory(PrintWriter pw) {
1954 dumpRecentHistory(pw);
1955 dumpDayStatistics(pw);
1956 }
1957
1958 private void dumpRecentHistory(PrintWriter pw) {
1959 final ArrayList<SyncStorageEngine.SyncHistoryItem> items
1960 = mSyncStorageEngine.getSyncHistory();
1961 if (items != null && items.size() > 0) {
1962 final Map<String, AuthoritySyncStats> authorityMap = Maps.newHashMap();
1963 long totalElapsedTime = 0;
1964 long totalTimes = 0;
1965 final int N = items.size();
1966
1967 int maxAuthority = 0;
1968 int maxAccount = 0;
1969 for (SyncStorageEngine.SyncHistoryItem item : items) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001970 SyncStorageEngine.AuthorityInfo authorityInfo
Alon Alberte0bde332011-09-22 14:26:16 -07001971 = mSyncStorageEngine.getAuthority(item.authorityId);
1972 final String authorityName;
1973 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07001974 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00001975 authorityName = authorityInfo.target.provider;
1976 accountKey = authorityInfo.target.account.name + "/"
1977 + authorityInfo.target.account.type
1978 + " u" + authorityInfo.target.userId;
Alon Alberte0bde332011-09-22 14:26:16 -07001979 } else {
1980 authorityName = "Unknown";
1981 accountKey = "Unknown";
1982 }
1983
1984 int length = authorityName.length();
1985 if (length > maxAuthority) {
1986 maxAuthority = length;
1987 }
1988 length = accountKey.length();
1989 if (length > maxAccount) {
1990 maxAccount = length;
1991 }
1992
1993 final long elapsedTime = item.elapsedTime;
1994 totalElapsedTime += elapsedTime;
1995 totalTimes++;
1996 AuthoritySyncStats authoritySyncStats = authorityMap.get(authorityName);
1997 if (authoritySyncStats == null) {
1998 authoritySyncStats = new AuthoritySyncStats(authorityName);
1999 authorityMap.put(authorityName, authoritySyncStats);
2000 }
2001 authoritySyncStats.elapsedTime += elapsedTime;
2002 authoritySyncStats.times++;
2003 final Map<String, AccountSyncStats> accountMap = authoritySyncStats.accountMap;
2004 AccountSyncStats accountSyncStats = accountMap.get(accountKey);
2005 if (accountSyncStats == null) {
2006 accountSyncStats = new AccountSyncStats(accountKey);
2007 accountMap.put(accountKey, accountSyncStats);
2008 }
2009 accountSyncStats.elapsedTime += elapsedTime;
2010 accountSyncStats.times++;
2011
2012 }
2013
Alon Albert27096822012-01-11 18:06:41 -08002014 if (totalElapsedTime > 0) {
2015 pw.println();
2016 pw.printf("Detailed Statistics (Recent history): "
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002017 + "%d (# of times) %ds (sync time)\n",
Alon Albert27096822012-01-11 18:06:41 -08002018 totalTimes, totalElapsedTime / 1000);
Alon Alberte0bde332011-09-22 14:26:16 -07002019
Alon Albert27096822012-01-11 18:06:41 -08002020 final List<AuthoritySyncStats> sortedAuthorities =
2021 new ArrayList<AuthoritySyncStats>(authorityMap.values());
2022 Collections.sort(sortedAuthorities, new Comparator<AuthoritySyncStats>() {
Alon Albertbf976ba2011-10-03 13:06:43 -07002023 @Override
Alon Albert27096822012-01-11 18:06:41 -08002024 public int compare(AuthoritySyncStats lhs, AuthoritySyncStats rhs) {
Alon Albertbf976ba2011-10-03 13:06:43 -07002025 // reverse order
2026 int compare = Integer.compare(rhs.times, lhs.times);
2027 if (compare == 0) {
2028 compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
Alon Alberte0bde332011-09-22 14:26:16 -07002029 }
Alon Albertbf976ba2011-10-03 13:06:43 -07002030 return compare;
Alon Alberte0bde332011-09-22 14:26:16 -07002031 }
Alon Albertbf976ba2011-10-03 13:06:43 -07002032 });
Alon Albert27096822012-01-11 18:06:41 -08002033
2034 final int maxLength = Math.max(maxAuthority, maxAccount + 3);
2035 final int padLength = 2 + 2 + maxLength + 2 + 10 + 11;
2036 final char chars[] = new char[padLength];
2037 Arrays.fill(chars, '-');
2038 final String separator = new String(chars);
2039
2040 final String authorityFormat =
2041 String.format(" %%-%ds: %%-9s %%-11s\n", maxLength + 2);
2042 final String accountFormat =
2043 String.format(" %%-%ds: %%-9s %%-11s\n", maxLength);
2044
2045 pw.println(separator);
2046 for (AuthoritySyncStats authoritySyncStats : sortedAuthorities) {
2047 String name = authoritySyncStats.name;
2048 long elapsedTime;
2049 int times;
2050 String timeStr;
2051 String timesStr;
2052
2053 elapsedTime = authoritySyncStats.elapsedTime;
2054 times = authoritySyncStats.times;
Alon Albertbf976ba2011-10-03 13:06:43 -07002055 timeStr = String.format("%ds/%d%%",
2056 elapsedTime / 1000,
2057 elapsedTime * 100 / totalElapsedTime);
2058 timesStr = String.format("%d/%d%%",
2059 times,
2060 times * 100 / totalTimes);
Alon Albert27096822012-01-11 18:06:41 -08002061 pw.printf(authorityFormat, name, timesStr, timeStr);
2062
2063 final List<AccountSyncStats> sortedAccounts =
2064 new ArrayList<AccountSyncStats>(
2065 authoritySyncStats.accountMap.values());
2066 Collections.sort(sortedAccounts, new Comparator<AccountSyncStats>() {
2067 @Override
2068 public int compare(AccountSyncStats lhs, AccountSyncStats rhs) {
2069 // reverse order
2070 int compare = Integer.compare(rhs.times, lhs.times);
2071 if (compare == 0) {
2072 compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime);
2073 }
2074 return compare;
2075 }
2076 });
2077 for (AccountSyncStats stats: sortedAccounts) {
2078 elapsedTime = stats.elapsedTime;
2079 times = stats.times;
2080 timeStr = String.format("%ds/%d%%",
2081 elapsedTime / 1000,
2082 elapsedTime * 100 / totalElapsedTime);
2083 timesStr = String.format("%d/%d%%",
2084 times,
2085 times * 100 / totalTimes);
2086 pw.printf(accountFormat, stats.name, timesStr, timeStr);
2087 }
2088 pw.println(separator);
Alon Alberte0bde332011-09-22 14:26:16 -07002089 }
Alon Alberte0bde332011-09-22 14:26:16 -07002090 }
2091
2092 pw.println();
2093 pw.println("Recent Sync History");
Alon Albert57286f92012-10-09 14:21:38 -07002094 final String format = " %-" + maxAccount + "s %-" + maxAuthority + "s %s\n";
Alon Albertbf976ba2011-10-03 13:06:43 -07002095 final Map<String, Long> lastTimeMap = Maps.newHashMap();
Alon Albert57286f92012-10-09 14:21:38 -07002096 final PackageManager pm = mContext.getPackageManager();
Alon Alberte0bde332011-09-22 14:26:16 -07002097 for (int i = 0; i < N; i++) {
2098 SyncStorageEngine.SyncHistoryItem item = items.get(i);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002099 SyncStorageEngine.AuthorityInfo authorityInfo
Alon Alberte0bde332011-09-22 14:26:16 -07002100 = mSyncStorageEngine.getAuthority(item.authorityId);
2101 final String authorityName;
2102 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002103 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002104 authorityName = authorityInfo.target.provider;
2105 accountKey = authorityInfo.target.account.name + "/"
2106 + authorityInfo.target.account.type
2107 + " u" + authorityInfo.target.userId;
Alon Alberte0bde332011-09-22 14:26:16 -07002108 } else {
2109 authorityName = "Unknown";
2110 accountKey = "Unknown";
2111 }
2112 final long elapsedTime = item.elapsedTime;
2113 final Time time = new Time();
2114 final long eventTime = item.eventTime;
2115 time.set(eventTime);
2116
Alon Albertbf976ba2011-10-03 13:06:43 -07002117 final String key = authorityName + "/" + accountKey;
2118 final Long lastEventTime = lastTimeMap.get(key);
2119 final String diffString;
2120 if (lastEventTime == null) {
2121 diffString = "";
2122 } else {
2123 final long diff = (lastEventTime - eventTime) / 1000;
2124 if (diff < 60) {
2125 diffString = String.valueOf(diff);
2126 } else if (diff < 3600) {
2127 diffString = String.format("%02d:%02d", diff / 60, diff % 60);
2128 } else {
2129 final long sec = diff % 3600;
2130 diffString = String.format("%02d:%02d:%02d",
2131 diff / 3600, sec / 60, sec % 60);
2132 }
2133 }
2134 lastTimeMap.put(key, eventTime);
2135
2136 pw.printf(" #%-3d: %s %8s %5.1fs %8s",
Alon Alberte0bde332011-09-22 14:26:16 -07002137 i + 1,
2138 formatTime(eventTime),
2139 SyncStorageEngine.SOURCES[item.source],
Alon Albertbf976ba2011-10-03 13:06:43 -07002140 ((float) elapsedTime) / 1000,
2141 diffString);
Alon Albert57286f92012-10-09 14:21:38 -07002142 pw.printf(format, accountKey, authorityName,
2143 SyncOperation.reasonToString(pm, item.reason));
Alon Alberte0bde332011-09-22 14:26:16 -07002144
2145 if (item.event != SyncStorageEngine.EVENT_STOP
2146 || item.upstreamActivity != 0
2147 || item.downstreamActivity != 0) {
2148 pw.printf(" event=%d upstreamActivity=%d downstreamActivity=%d\n",
2149 item.event,
2150 item.upstreamActivity,
2151 item.downstreamActivity);
2152 }
2153 if (item.mesg != null
2154 && !SyncStorageEngine.MESG_SUCCESS.equals(item.mesg)) {
2155 pw.printf(" mesg=%s\n", item.mesg);
2156 }
2157 }
Alon Albert57286f92012-10-09 14:21:38 -07002158 pw.println();
2159 pw.println("Recent Sync History Extras");
2160 for (int i = 0; i < N; i++) {
2161 final SyncStorageEngine.SyncHistoryItem item = items.get(i);
2162 final Bundle extras = item.extras;
2163 if (extras == null || extras.size() == 0) {
2164 continue;
2165 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002166 final SyncStorageEngine.AuthorityInfo authorityInfo
Alon Albert57286f92012-10-09 14:21:38 -07002167 = mSyncStorageEngine.getAuthority(item.authorityId);
2168 final String authorityName;
2169 final String accountKey;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002170 if (authorityInfo != null) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002171 authorityName = authorityInfo.target.provider;
2172 accountKey = authorityInfo.target.account.name + "/"
2173 + authorityInfo.target.account.type
2174 + " u" + authorityInfo.target.userId;
Alon Albert57286f92012-10-09 14:21:38 -07002175 } else {
2176 authorityName = "Unknown";
2177 accountKey = "Unknown";
2178 }
2179 final Time time = new Time();
2180 final long eventTime = item.eventTime;
2181 time.set(eventTime);
2182
2183 pw.printf(" #%-3d: %s %8s ",
2184 i + 1,
2185 formatTime(eventTime),
2186 SyncStorageEngine.SOURCES[item.source]);
2187
2188 pw.printf(format, accountKey, authorityName, extras);
2189 }
Alon Alberte0bde332011-09-22 14:26:16 -07002190 }
2191 }
2192
2193 private void dumpDayStatistics(PrintWriter pw) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07002194 SyncStorageEngine.DayStats dses[] = mSyncStorageEngine.getDayStatistics();
2195 if (dses != null && dses[0] != null) {
2196 pw.println();
2197 pw.println("Sync Statistics");
2198 pw.print(" Today: "); dumpDayStatistic(pw, dses[0]);
2199 int today = dses[0].day;
2200 int i;
2201 SyncStorageEngine.DayStats ds;
Doug Zongker44f57472009-09-20 15:52:43 -07002202
Dianne Hackborn231cc602009-04-27 17:10:36 -07002203 // Print each day in the current week.
2204 for (i=1; i<=6 && i < dses.length; i++) {
2205 ds = dses[i];
2206 if (ds == null) break;
2207 int delta = today-ds.day;
2208 if (delta > 6) break;
Doug Zongker44f57472009-09-20 15:52:43 -07002209
Dianne Hackborn231cc602009-04-27 17:10:36 -07002210 pw.print(" Day-"); pw.print(delta); pw.print(": ");
2211 dumpDayStatistic(pw, ds);
2212 }
Doug Zongker44f57472009-09-20 15:52:43 -07002213
Dianne Hackborn231cc602009-04-27 17:10:36 -07002214 // Aggregate all following days into weeks and print totals.
2215 int weekDay = today;
2216 while (i < dses.length) {
2217 SyncStorageEngine.DayStats aggr = null;
2218 weekDay -= 7;
2219 while (i < dses.length) {
2220 ds = dses[i];
2221 if (ds == null) {
2222 i = dses.length;
2223 break;
2224 }
2225 int delta = weekDay-ds.day;
2226 if (delta > 6) break;
2227 i++;
Doug Zongker44f57472009-09-20 15:52:43 -07002228
Dianne Hackborn231cc602009-04-27 17:10:36 -07002229 if (aggr == null) {
2230 aggr = new SyncStorageEngine.DayStats(weekDay);
2231 }
2232 aggr.successCount += ds.successCount;
2233 aggr.successTime += ds.successTime;
2234 aggr.failureCount += ds.failureCount;
2235 aggr.failureTime += ds.failureTime;
2236 }
2237 if (aggr != null) {
2238 pw.print(" Week-"); pw.print((today-weekDay)/7); pw.print(": ");
2239 dumpDayStatistic(pw, aggr);
2240 }
2241 }
2242 }
Alon Alberte0bde332011-09-22 14:26:16 -07002243 }
Doug Zongker44f57472009-09-20 15:52:43 -07002244
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002245 private void dumpSyncAdapters(IndentingPrintWriter pw) {
2246 pw.println();
2247 final List<UserInfo> users = getAllUsers();
2248 if (users != null) {
2249 for (UserInfo user : users) {
2250 pw.println("Sync adapters for " + user + ":");
2251 pw.increaseIndent();
2252 for (RegisteredServicesCache.ServiceInfo<?> info :
2253 mSyncAdapters.getAllServices(user.id)) {
2254 pw.println(info);
2255 }
2256 pw.decreaseIndent();
2257 pw.println();
2258 }
2259 }
2260 }
2261
Alon Alberte0bde332011-09-22 14:26:16 -07002262 private static class AuthoritySyncStats {
2263 String name;
2264 long elapsedTime;
2265 int times;
2266 Map<String, AccountSyncStats> accountMap = Maps.newHashMap();
2267
2268 private AuthoritySyncStats(String name) {
2269 this.name = name;
2270 }
2271 }
2272
2273 private static class AccountSyncStats {
2274 String name;
2275 long elapsedTime;
2276 int times;
2277
2278 private AccountSyncStats(String name) {
2279 this.name = name;
Dianne Hackborn231cc602009-04-27 17:10:36 -07002280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 }
2282
2283 /**
2284 * A helper object to keep track of the time we have spent syncing since the last boot
2285 */
2286 private class SyncTimeTracker {
2287 /** True if a sync was in progress on the most recent call to update() */
2288 boolean mLastWasSyncing = false;
2289 /** Used to track when lastWasSyncing was last set */
2290 long mWhenSyncStarted = 0;
2291 /** The cumulative time we have spent syncing */
2292 private long mTimeSpentSyncing;
2293
2294 /** Call to let the tracker know that the sync state may have changed */
2295 public synchronized void update() {
Fred Quintana918339a2010-10-05 14:00:39 -07002296 final boolean isSyncInProgress = !mActiveSyncContexts.isEmpty();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 if (isSyncInProgress == mLastWasSyncing) return;
2298 final long now = SystemClock.elapsedRealtime();
2299 if (isSyncInProgress) {
2300 mWhenSyncStarted = now;
2301 } else {
2302 mTimeSpentSyncing += now - mWhenSyncStarted;
2303 }
2304 mLastWasSyncing = isSyncInProgress;
2305 }
2306
2307 /** Get how long we have been syncing, in ms */
2308 public synchronized long timeSpentSyncing() {
2309 if (!mLastWasSyncing) return mTimeSpentSyncing;
2310
2311 final long now = SystemClock.elapsedRealtime();
2312 return mTimeSpentSyncing + (now - mWhenSyncStarted);
2313 }
2314 }
2315
Fred Quintana718d8a22009-04-29 17:53:20 -07002316 class ServiceConnectionData {
2317 public final ActiveSyncContext activeSyncContext;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002318 public final IBinder adapter;
2319
2320 ServiceConnectionData(ActiveSyncContext activeSyncContext, IBinder adapter) {
Fred Quintana718d8a22009-04-29 17:53:20 -07002321 this.activeSyncContext = activeSyncContext;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002322 this.adapter = adapter;
Fred Quintana718d8a22009-04-29 17:53:20 -07002323 }
2324 }
2325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 /**
2327 * Handles SyncOperation Messages that are posted to the associated
2328 * HandlerThread.
2329 */
2330 class SyncHandler extends Handler {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002331 // Messages that can be sent on mHandler.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 private static final int MESSAGE_SYNC_FINISHED = 1;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002333 private static final int MESSAGE_RELEASE_MESSAGES_FROM_QUEUE = 2;
Fred Quintana718d8a22009-04-29 17:53:20 -07002334 private static final int MESSAGE_SERVICE_CONNECTED = 4;
2335 private static final int MESSAGE_SERVICE_DISCONNECTED = 5;
Fred Quintana918339a2010-10-05 14:00:39 -07002336 private static final int MESSAGE_CANCEL = 6;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002337 static final int MESSAGE_JOBSERVICE_OBJECT = 7;
2338 static final int MESSAGE_START_SYNC = 10;
2339 static final int MESSAGE_STOP_SYNC = 11;
2340 static final int MESSAGE_SCHEDULE_SYNC = 12;
2341 static final int MESSAGE_UPDATE_PERIODIC_SYNC = 13;
2342 static final int MESSAGE_REMOVE_PERIODIC_SYNC = 14;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002343
Matthew Williams1967c8d2015-06-19 19:03:13 -07002344 /**
2345 * Posted periodically to monitor network process for long-running syncs.
2346 * obj: {@link com.android.server.content.SyncManager.ActiveSyncContext}
2347 */
2348 private static final int MESSAGE_MONITOR_SYNC = 8;
振淦王60a74312015-12-01 16:37:31 +08002349 private static final int MESSAGE_ACCOUNTS_UPDATED = 9;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002352 private final HashMap<String, PowerManager.WakeLock> mWakeLocks = Maps.newHashMap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353
Matthew Williams8b76d202015-05-03 18:16:25 -07002354 private List<Message> mUnreadyQueue = new ArrayList<Message>();
Jeff Sharkey6ab72d72012-10-08 16:44:37 -07002355
Matthew Williams8b76d202015-05-03 18:16:25 -07002356 void onBootCompleted() {
Matthew Williams8704fc32013-09-27 11:32:35 -07002357 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002358 Slog.v(TAG, "Boot completed.");
Matthew Williams8704fc32013-09-27 11:32:35 -07002359 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002360 checkIfDeviceReady();
Matthew Williams8b76d202015-05-03 18:16:25 -07002361 }
2362
2363 void onDeviceProvisioned() {
2364 if (Log.isLoggable(TAG, Log.DEBUG)) {
2365 Log.d(TAG, "mProvisioned=" + mProvisioned);
2366 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002367 checkIfDeviceReady();
2368 }
2369
2370 void checkIfDeviceReady() {
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002371 if (mProvisioned && mBootCompleted && mJobServiceReady) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002372 synchronized(this) {
Shreyas Basarge3147bbc2016-02-19 23:51:40 +00002373 mSyncStorageEngine.restoreAllPeriodicSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002374 // Dispatch any stashed messages.
2375 obtainMessage(MESSAGE_RELEASE_MESSAGES_FROM_QUEUE).sendToTarget();
2376 }
Matthew Williams8b76d202015-05-03 18:16:25 -07002377 }
2378 }
2379
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002380 /**
2381 * Stash any messages that come to the handler before boot is complete or before the device
2382 * is properly provisioned (i.e. out of set-up wizard).
2383 * {@link #onBootCompleted()} and {@link SyncHandler#onDeviceProvisioned} both
2384 * need to come in before we start syncing.
2385 * @param msg Message to dispatch at a later point.
2386 * @return true if a message was enqueued, false otherwise. This is to avoid losing the
2387 * message if we manage to acquire the lock but by the time we do boot has completed.
2388 */
2389 private boolean tryEnqueueMessageUntilReadyToRun(Message msg) {
2390 synchronized (this) {
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002391 if (!mBootCompleted || !mProvisioned || !mJobServiceReady) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002392 // Need to copy the message bc looper will recycle it.
2393 Message m = Message.obtain(msg);
2394 mUnreadyQueue.add(m);
2395 return true;
2396 } else {
2397 return false;
Matthew Williams8704fc32013-09-27 11:32:35 -07002398 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002399 }
2400 }
2401
2402 public SyncHandler(Looper looper) {
2403 super(looper);
2404 }
2405
2406 public void handleMessage(Message msg) {
2407 try {
2408 mSyncManagerWakeLock.acquire();
2409 // We only want to enqueue sync related messages until device is ready.
2410 // Other messages are handled without enqueuing.
2411 if (msg.what == MESSAGE_JOBSERVICE_OBJECT) {
2412 Slog.i(TAG, "Got SyncJobService instance.");
2413 mSyncJobService = (SyncJobService) msg.obj;
Shreyas Basargea4ac5ab2016-04-21 20:31:44 +01002414 mJobServiceReady = true;
2415 checkIfDeviceReady();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002416 } else if (msg.what == SyncHandler.MESSAGE_ACCOUNTS_UPDATED) {
2417 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2418 Slog.v(TAG, "handleSyncHandlerMessage: MESSAGE_ACCOUNTS_UPDATED");
2419 }
2420 EndPoint targets = (EndPoint) msg.obj;
2421 updateRunningAccountsH(targets);
2422 } else if (msg.what == MESSAGE_RELEASE_MESSAGES_FROM_QUEUE) {
2423 if (mUnreadyQueue != null) {
2424 for (Message m : mUnreadyQueue) {
2425 handleSyncMessage(m);
2426 }
2427 mUnreadyQueue = null;
2428 }
2429 } else if (tryEnqueueMessageUntilReadyToRun(msg)) {
2430 // No work to be done.
2431 } else {
2432 handleSyncMessage(msg);
2433 }
2434 } finally {
2435 mSyncManagerWakeLock.release();
2436 }
2437 }
2438
2439 private void handleSyncMessage(Message msg) {
2440 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
2441
2442 try {
2443 mDataConnectionIsConnected = readDataConnectionState();
2444 switch (msg.what) {
2445 case MESSAGE_SCHEDULE_SYNC:
Shreyas Basargeba1f7902016-10-01 00:19:44 +01002446 ScheduleSyncMessagePayload syncPayload =
2447 (ScheduleSyncMessagePayload) msg.obj;
2448 SyncOperation op = syncPayload.syncOperation;
2449 scheduleSyncOperationH(op, syncPayload.minDelayMillis);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002450 break;
2451
2452 case MESSAGE_START_SYNC:
2453 op = (SyncOperation) msg.obj;
2454 startSyncH(op);
2455 break;
2456
2457 case MESSAGE_STOP_SYNC:
2458 op = (SyncOperation) msg.obj;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002459 if (isLoggable) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002460 Slog.v(TAG, "Stop sync received.");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002461 }
2462 ActiveSyncContext asc = findActiveSyncContextH(op.jobId);
2463 if (asc != null) {
2464 runSyncFinishedOrCanceledH(null /* no result */, asc);
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002465 boolean reschedule = msg.arg1 != 0;
2466 boolean applyBackoff = msg.arg2 != 0;
2467 if (isLoggable) {
2468 Slog.v(TAG, "Stopping sync. Reschedule: " + reschedule
2469 + "Backoff: " + applyBackoff);
2470 }
2471 if (applyBackoff) {
2472 increaseBackoffSetting(op.target);
2473 }
2474 if (reschedule) {
2475 deferStoppedSyncH(op, 0);
2476 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002477 }
2478 break;
2479
2480 case MESSAGE_UPDATE_PERIODIC_SYNC:
2481 UpdatePeriodicSyncMessagePayload data =
2482 (UpdatePeriodicSyncMessagePayload) msg.obj;
2483 updateOrAddPeriodicSyncH(data.target, data.pollFrequency,
2484 data.flex, data.extras);
2485 break;
2486 case MESSAGE_REMOVE_PERIODIC_SYNC:
2487 removePeriodicSyncH((EndPoint)msg.obj, msg.getData());
2488 break;
2489
2490 case SyncHandler.MESSAGE_CANCEL:
2491 SyncStorageEngine.EndPoint endpoint = (SyncStorageEngine.EndPoint) msg.obj;
2492 Bundle extras = msg.peekData();
2493 if (Log.isLoggable(TAG, Log.DEBUG)) {
2494 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_CANCEL: "
2495 + endpoint + " bundle: " + extras);
2496 }
2497 cancelActiveSyncH(endpoint, extras);
2498 break;
2499
2500 case SyncHandler.MESSAGE_SYNC_FINISHED:
2501 SyncFinishedOrCancelledMessagePayload payload =
2502 (SyncFinishedOrCancelledMessagePayload) msg.obj;
2503 if (!isSyncStillActiveH(payload.activeSyncContext)) {
2504 Log.d(TAG, "handleSyncHandlerMessage: dropping since the "
2505 + "sync is no longer active: "
2506 + payload.activeSyncContext);
2507 break;
2508 }
2509 if (isLoggable) {
2510 Slog.v(TAG, "syncFinished" + payload.activeSyncContext.mSyncOperation);
2511 }
2512 mSyncJobService.callJobFinished(
2513 payload.activeSyncContext.mSyncOperation.jobId, false);
2514 runSyncFinishedOrCanceledH(payload.syncResult,
2515 payload.activeSyncContext);
2516 break;
2517
2518 case SyncHandler.MESSAGE_SERVICE_CONNECTED: {
2519 ServiceConnectionData msgData = (ServiceConnectionData) msg.obj;
2520 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2521 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_CONNECTED: "
2522 + msgData.activeSyncContext);
2523 }
2524 // Check that this isn't an old message.
2525 if (isSyncStillActiveH(msgData.activeSyncContext)) {
2526 runBoundToAdapterH(
2527 msgData.activeSyncContext,
2528 msgData.adapter);
2529 }
2530 break;
2531 }
2532
2533 case SyncHandler.MESSAGE_SERVICE_DISCONNECTED: {
2534 final ActiveSyncContext currentSyncContext =
2535 ((ServiceConnectionData) msg.obj).activeSyncContext;
2536 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2537 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_DISCONNECTED: "
2538 + currentSyncContext);
2539 }
2540 // Check that this isn't an old message.
2541 if (isSyncStillActiveH(currentSyncContext)) {
2542 // cancel the sync if we have a syncadapter, which means one is
2543 // outstanding
2544 try {
2545 if (currentSyncContext.mSyncAdapter != null) {
2546 currentSyncContext.mSyncAdapter.cancelSync(currentSyncContext);
2547 }
2548 } catch (RemoteException e) {
2549 // We don't need to retry this in this case.
2550 }
2551
2552 // Pretend that the sync failed with an IOException,
2553 // which is a soft error.
2554 SyncResult syncResult = new SyncResult();
2555 syncResult.stats.numIoExceptions++;
2556 mSyncJobService.callJobFinished(
2557 currentSyncContext.mSyncOperation.jobId, false);
2558 runSyncFinishedOrCanceledH(syncResult, currentSyncContext);
2559 }
2560 break;
2561 }
2562
2563 case SyncHandler.MESSAGE_MONITOR_SYNC:
2564 ActiveSyncContext monitoredSyncContext = (ActiveSyncContext) msg.obj;
2565 if (Log.isLoggable(TAG, Log.DEBUG)) {
2566 Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_MONITOR_SYNC: " +
2567 monitoredSyncContext.mSyncOperation.target);
2568 }
2569
2570 if (isSyncNotUsingNetworkH(monitoredSyncContext)) {
2571 Log.w(TAG, String.format(
2572 "Detected sync making no progress for %s. cancelling.",
2573 monitoredSyncContext));
2574 mSyncJobService.callJobFinished(
2575 monitoredSyncContext.mSyncOperation.jobId, false);
2576 runSyncFinishedOrCanceledH(
2577 null /* cancel => no result */, monitoredSyncContext);
2578 } else {
2579 // Repost message to check again.
2580 postMonitorSyncProgressMessage(monitoredSyncContext);
2581 }
2582 break;
2583
2584 }
2585 } finally {
2586 mSyncTimeTracker.update();
Fred Quintanae91ebe22009-09-29 20:44:30 -07002587 }
2588 }
2589
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002590 private PowerManager.WakeLock getSyncWakeLock(SyncOperation operation) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002591 final String wakeLockKey = operation.wakeLockName();
Fred Quintanab3029c32010-04-06 13:27:12 -07002592 PowerManager.WakeLock wakeLock = mWakeLocks.get(wakeLockKey);
2593 if (wakeLock == null) {
Dianne Hackbornd45665b2014-02-26 12:35:32 -08002594 final String name = SYNC_WAKE_LOCK_PREFIX + wakeLockKey;
Fred Quintanab3029c32010-04-06 13:27:12 -07002595 wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name);
2596 wakeLock.setReferenceCounted(false);
2597 mWakeLocks.put(wakeLockKey, wakeLock);
2598 }
2599 return wakeLock;
2600 }
2601
Matthew Williams8704fc32013-09-27 11:32:35 -07002602 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002603 * Defer the specified SyncOperation by rescheduling it on the JobScheduler with some
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002604 * delay. This is equivalent to a failure. If this is a periodic sync, a delayed one-off
2605 * sync will be scheduled.
Matthew Williams8704fc32013-09-27 11:32:35 -07002606 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002607 private void deferSyncH(SyncOperation op, long delay) {
2608 mSyncJobService.callJobFinished(op.jobId, false);
2609 if (op.isPeriodic) {
2610 scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay);
2611 } else {
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01002612 // mSyncJobService.callJobFinished is async, so cancel the job to ensure we don't
2613 // find the this job in the pending jobs list while looking for duplicates
2614 // before scheduling it at a later time.
2615 getJobScheduler().cancel(op.jobId);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002616 scheduleSyncOperationH(op, delay);
Fred Quintanae91ebe22009-09-29 20:44:30 -07002617 }
2618 }
Matthew Williams8704fc32013-09-27 11:32:35 -07002619
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002620 /* Same as deferSyncH, but assumes that job is no longer running on JobScheduler. */
2621 private void deferStoppedSyncH(SyncOperation op, long delay) {
2622 if (op.isPeriodic) {
2623 scheduleSyncOperationH(op.createOneTimeSyncOperation(), delay);
2624 } else {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002625 scheduleSyncOperationH(op, delay);
2626 }
2627 }
2628
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002629 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002630 * Cancel an active sync and reschedule it on the JobScheduler with some delay.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 */
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002632 private void deferActiveSyncH(ActiveSyncContext asc) {
2633 SyncOperation op = asc.mSyncOperation;
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002634 runSyncFinishedOrCanceledH(null, asc);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002635 deferSyncH(op, SYNC_DELAY_ON_CONFLICT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002636 }
2637
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002638 private void startSyncH(SyncOperation op) {
2639 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
2640 if (isLoggable) Slog.v(TAG, op.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002641
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002642 if (mStorageIsLow) {
2643 deferSyncH(op, SYNC_DELAY_ON_LOW_STORAGE);
Matthew Williams8704fc32013-09-27 11:32:35 -07002644 return;
2645 }
2646
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002647 if (op.isPeriodic) {
2648 // Don't allow this periodic to run if a previous instance failed and is currently
2649 // scheduled according to some backoff criteria.
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002650 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002651 for (SyncOperation syncOperation: ops) {
2652 if (syncOperation.sourcePeriodicId == op.jobId) {
2653 mSyncJobService.callJobFinished(op.jobId, false);
2654 return;
Fred Quintana718d8a22009-04-29 17:53:20 -07002655 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002656 }
2657 // Don't allow this periodic to run if a previous instance failed and is currently
2658 // executing according to some backoff criteria.
2659 for (ActiveSyncContext asc: mActiveSyncContexts) {
2660 if (asc.mSyncOperation.sourcePeriodicId == op.jobId) {
2661 mSyncJobService.callJobFinished(op.jobId, false);
2662 return;
Fred Quintana718d8a22009-04-29 17:53:20 -07002663 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002664 }
2665 // Check for adapter delays.
2666 if (isAdapterDelayed(op.target)) {
2667 deferSyncH(op, 0 /* No minimum delay */);
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002668 return;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002669 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002670 }
Fred Quintana718d8a22009-04-29 17:53:20 -07002671
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002672 // Check for conflicting syncs.
2673 for (ActiveSyncContext asc: mActiveSyncContexts) {
2674 if (asc.mSyncOperation.isConflict(op)) {
2675 // If the provided SyncOperation conflicts with a running one, the lower
2676 // priority sync is pre-empted.
2677 if (asc.mSyncOperation.findPriority() >= op.findPriority()) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002679 Slog.v(TAG, "Rescheduling sync due to conflict " + op.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002681 deferSyncH(op, SYNC_DELAY_ON_CONFLICT);
2682 return;
Matthew Williamsfa774182013-06-18 15:44:11 -07002683 } else {
Matthew Williamsfa774182013-06-18 15:44:11 -07002684 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002685 Slog.v(TAG, "Pushing back running sync due to a higher priority sync");
Fred Quintana918339a2010-10-05 14:00:39 -07002686 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002687 deferActiveSyncH(asc);
Shreyas Basarge7680dd92016-02-11 14:52:47 +00002688 break;
Alon Albert8e285552012-09-17 15:05:27 -07002689 }
2690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002691 }
2692
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002693 final int syncOpState = computeSyncOpState(op);
2694 switch (syncOpState) {
2695 case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS:
2696 case SYNC_OP_STATE_INVALID: {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002697 mSyncJobService.callJobFinished(op.jobId, false);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002698 } return;
2699 }
2700
2701 if (!dispatchSyncOperation(op)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002702 mSyncJobService.callJobFinished(op.jobId, false);
Fred Quintana918339a2010-10-05 14:00:39 -07002703 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002704
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002705 setAuthorityPendingState(op.target);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002706 }
2707
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002708 private ActiveSyncContext findActiveSyncContextH(int jobId) {
2709 for (ActiveSyncContext asc: mActiveSyncContexts) {
2710 SyncOperation op = asc.mSyncOperation;
2711 if (op != null && op.jobId == jobId) {
2712 return asc;
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002713 }
2714 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002715 return null;
Matthew Williamsa4745542015-12-10 20:29:02 +00002716 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002717
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002718 private void updateRunningAccountsH(EndPoint syncTargets) {
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002719 AccountAndUser[] oldAccounts = mRunningAccounts;
振淦王60a74312015-12-01 16:37:31 +08002720 mRunningAccounts = AccountManagerService.getSingleton().getRunningAccounts();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002721 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2722 Slog.v(TAG, "Accounts list: ");
2723 for (AccountAndUser acc : mRunningAccounts) {
2724 Slog.v(TAG, acc.toString());
2725 }
2726 }
振淦王60a74312015-12-01 16:37:31 +08002727 if (mBootCompleted) {
2728 doDatabaseCleanup();
2729 }
2730
2731 AccountAndUser[] accounts = mRunningAccounts;
2732 for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) {
2733 if (!containsAccountAndUser(accounts,
2734 currentSyncContext.mSyncOperation.target.account,
2735 currentSyncContext.mSyncOperation.target.userId)) {
2736 Log.d(TAG, "canceling sync since the account is no longer running");
2737 sendSyncFinishedOrCanceledMessage(currentSyncContext,
2738 null /* no result since this is a cancel */);
2739 }
2740 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002741
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002742 // On account add, check if there are any settings to be restored.
2743 for (AccountAndUser aau : mRunningAccounts) {
2744 if (!containsAccountAndUser(oldAccounts, aau.account, aau.userId)) {
2745 if (Log.isLoggable(TAG, Log.DEBUG)) {
2746 Log.d(TAG, "Account " + aau.account + " added, checking sync restore data");
2747 }
2748 AccountSyncSettingsBackupHelper.accountAdded(mContext);
2749 break;
2750 }
2751 }
2752
Rubin Xu2c548e02016-04-01 17:47:28 +01002753 // Cancel all jobs from non-existent accounts.
2754 AccountAndUser[] allAccounts = AccountManagerService.getSingleton().getAllAccounts();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002755 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002756 for (SyncOperation op: ops) {
Rubin Xu2c548e02016-04-01 17:47:28 +01002757 if (!containsAccountAndUser(allAccounts, op.target.account, op.target.userId)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002758 getJobScheduler().cancel(op.jobId);
2759 }
2760 }
2761
2762 if (syncTargets != null) {
2763 scheduleSync(syncTargets.account, syncTargets.userId,
Svet Ganovf6d424f12016-09-20 20:18:53 -07002764 SyncOperation.REASON_ACCOUNTS_UPDATED, syncTargets.provider,
2765 null, AuthorityInfo.NOT_INITIALIZED);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002766 }
2767 }
2768
2769 /**
2770 * The given SyncOperation will be removed and a new one scheduled in its place if
2771 * an updated period or flex is specified.
2772 * @param syncOperation SyncOperation whose period and flex is to be updated.
2773 * @param pollFrequencyMillis new period in milliseconds.
2774 * @param flexMillis new flex time in milliseconds.
2775 */
2776 private void maybeUpdateSyncPeriodH(SyncOperation syncOperation, long pollFrequencyMillis,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002777 long flexMillis) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002778 if (!(pollFrequencyMillis == syncOperation.periodMillis
2779 && flexMillis == syncOperation.flexMillis)) {
2780 if (Log.isLoggable(TAG, Log.VERBOSE)) {
2781 Slog.v(TAG, "updating period " + syncOperation + " to " + pollFrequencyMillis
2782 + " and flex to " + flexMillis);
2783 }
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002784 SyncOperation newOp = new SyncOperation(syncOperation, pollFrequencyMillis,
2785 flexMillis);
2786 newOp.jobId = syncOperation.jobId;
2787 scheduleSyncOperationH(newOp);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002788 }
2789 }
2790
2791 private void updateOrAddPeriodicSyncH(EndPoint target, long pollFrequency, long flex,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00002792 Bundle extras) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002793 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
2794 verifyJobScheduler(); // Will fill in mScheduledSyncs cache if it is not already filled.
2795 final long pollFrequencyMillis = pollFrequency * 1000L;
2796 final long flexMillis = flex * 1000L;
2797 if (isLoggable) {
2798 Slog.v(TAG, "Addition to periodic syncs requested: " + target
2799 + " period: " + pollFrequency
2800 + " flexMillis: " + flex
2801 + " extras: " + extras.toString());
2802 }
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002803 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002804 for (SyncOperation op: ops) {
2805 if (op.isPeriodic && op.target.matchesSpec(target)
2806 && syncExtrasEquals(op.extras, extras, true /* includeSyncSettings */)) {
2807 maybeUpdateSyncPeriodH(op, pollFrequencyMillis, flexMillis);
2808 return;
2809 }
2810 }
2811
2812 if (isLoggable) {
2813 Slog.v(TAG, "Adding new periodic sync: " + target
2814 + " period: " + pollFrequency
2815 + " flexMillis: " + flex
2816 + " extras: " + extras.toString());
2817 }
2818
2819 final RegisteredServicesCache.ServiceInfo<SyncAdapterType>
2820 syncAdapterInfo = mSyncAdapters.getServiceInfo(
2821 SyncAdapterType.newKey(
2822 target.provider, target.account.type),
2823 target.userId);
2824 if (syncAdapterInfo == null) {
2825 return;
2826 }
2827
2828 SyncOperation op = new SyncOperation(target, syncAdapterInfo.uid,
2829 syncAdapterInfo.componentName.getPackageName(), SyncOperation.REASON_PERIODIC,
2830 SyncStorageEngine.SOURCE_PERIODIC, extras,
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00002831 syncAdapterInfo.type.allowParallelSyncs(), true, SyncOperation.NO_JOB_ID,
2832 pollFrequencyMillis, flexMillis);
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002833
2834 final int syncOpState = computeSyncOpState(op);
2835 switch (syncOpState) {
2836 case SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS: {
Svet Ganov973edd192016-09-08 20:15:55 -07002837 String packageName = op.owningPackage;
2838 final int userId = UserHandle.getUserId(op.owningUid);
2839 // If the app did not run and has no account access, done
2840 if (!mPackageManagerInternal.wasPackageEverLaunched(packageName, userId)) {
2841 return;
2842 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002843 mAccountManagerInternal.requestAccountAccess(op.target.account,
Svet Ganov973edd192016-09-08 20:15:55 -07002844 packageName, userId, new RemoteCallback((Bundle result) -> {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002845 if (result != null
2846 && result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) {
2847 updateOrAddPeriodicSync(target, pollFrequency, flex, extras);
2848 }
2849 }
2850 ));
2851 } return;
2852
2853 case SYNC_OP_STATE_INVALID: {
2854 return;
2855 }
2856 }
2857
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002858 scheduleSyncOperationH(op);
2859 mSyncStorageEngine.reportChange(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS);
2860 }
2861
2862 /**
2863 * Remove this periodic sync operation and all one-off operations initiated by it.
2864 */
2865 private void removePeriodicSyncInternalH(SyncOperation syncOperation) {
2866 // Remove this periodic sync and all one-off syncs initiated by it.
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002867 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002868 for (SyncOperation op: ops) {
2869 if (op.sourcePeriodicId == syncOperation.jobId || op.jobId == syncOperation.jobId) {
2870 ActiveSyncContext asc = findActiveSyncContextH(syncOperation.jobId);
2871 if (asc != null) {
2872 mSyncJobService.callJobFinished(syncOperation.jobId, false);
2873 runSyncFinishedOrCanceledH(null, asc);
2874 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002875 getJobScheduler().cancel(op.jobId);
2876 }
2877 }
2878 }
2879
2880 private void removePeriodicSyncH(EndPoint target, Bundle extras) {
2881 verifyJobScheduler();
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00002882 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002883 for (SyncOperation op: ops) {
2884 if (op.isPeriodic && op.target.matchesSpec(target)
2885 && syncExtrasEquals(op.extras, extras, true /* includeSyncSettings */)) {
2886 removePeriodicSyncInternalH(op);
2887 }
2888 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -08002889 }
2890
Matthew Williams1967c8d2015-06-19 19:03:13 -07002891 private boolean isSyncNotUsingNetworkH(ActiveSyncContext activeSyncContext) {
2892 final long bytesTransferredCurrent =
2893 getTotalBytesTransferredByUid(activeSyncContext.mSyncAdapterUid);
2894 final long deltaBytesTransferred =
2895 bytesTransferredCurrent - activeSyncContext.mBytesTransferredAtLastPoll;
2896
2897 if (Log.isLoggable(TAG, Log.DEBUG)) {
2898 // Bytes transferred
2899 long remainder = deltaBytesTransferred;
2900 long mb = remainder / (1024 * 1024);
2901 remainder %= 1024 * 1024;
2902 long kb = remainder / 1024;
2903 remainder %= 1024;
2904 long b = remainder;
2905 Log.d(TAG, String.format(
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002906 "Time since last update: %ds. Delta transferred: %dMBs,%dKBs,%dBs",
2907 (SystemClock.elapsedRealtime()
2908 - activeSyncContext.mLastPolledTimeElapsed)/1000,
2909 mb, kb, b)
Matthew Williams1967c8d2015-06-19 19:03:13 -07002910 );
2911 }
2912 return (deltaBytesTransferred <= SYNC_MONITOR_PROGRESS_THRESHOLD_BYTES);
2913 }
2914
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002915 /**
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002916 * Determine if a sync is no longer valid and should be dropped.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002917 */
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002918 private int computeSyncOpState(SyncOperation op) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002919 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002920 int state;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002921 final EndPoint target = op.target;
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002922
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002923 // Drop the sync if the account of this operation no longer exists.
2924 AccountAndUser[] accounts = mRunningAccounts;
2925 if (!containsAccountAndUser(accounts, target.account, target.userId)) {
2926 if (isLoggable) {
2927 Slog.v(TAG, " Dropping sync operation: account doesn't exist.");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002928 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002929 return SYNC_OP_STATE_INVALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002930 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002931 // Drop this sync request if it isn't syncable.
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002932 state = computeSyncable(target.account, target.userId, target.provider);
2933 if (state == AuthorityInfo.SYNCABLE_NO_ACCOUNT_ACCESS) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002934 if (isLoggable) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002935 Slog.v(TAG, " Dropping sync operation: "
2936 + "isSyncable == SYNCABLE_NO_ACCOUNT_ACCESS");
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002937 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002938 return SYNC_OP_STATE_INVALID_NO_ACCOUNT_ACCESS;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002939 }
Svet Ganovdfed1c72016-08-25 15:40:52 -07002940 if (state == AuthorityInfo.NOT_SYNCABLE) {
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002941 if (isLoggable) {
Svet Ganovdfed1c72016-08-25 15:40:52 -07002942 Slog.v(TAG, " Dropping sync operation: isSyncable == NOT_SYNCABLE");
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002943 }
2944 return SYNC_OP_STATE_INVALID;
2945 }
2946
2947 final boolean syncEnabled = mSyncStorageEngine.getMasterSyncAutomatically(target.userId)
2948 && mSyncStorageEngine.getSyncAutomatically(target.account,
2949 target.userId, target.provider);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002950
2951 // We ignore system settings that specify the sync is invalid if:
2952 // 1) It's manual - we try it anyway. When/if it fails it will be rescheduled.
2953 // or
2954 // 2) it's an initialisation sync - we just need to connect to it.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002955 final boolean ignoreSystemConfiguration = op.isIgnoreSettings() || (state < 0);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002956
2957 // Sync not enabled.
2958 if (!syncEnabled && !ignoreSystemConfiguration) {
2959 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002960 Slog.v(TAG, " Dropping sync operation: disallowed by settings/network.");
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002961 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002962 return SYNC_OP_STATE_INVALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002963 }
Svetoslav Ganov5cb29732016-07-11 19:32:30 -07002964 return SYNC_OP_STATE_VALID;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002965 }
Fred Quintana918339a2010-10-05 14:00:39 -07002966
Fred Quintana87b14662011-09-12 10:32:55 -07002967 private boolean dispatchSyncOperation(SyncOperation op) {
Fred Quintana918339a2010-10-05 14:00:39 -07002968 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002969 Slog.v(TAG, "dispatchSyncOperation: we are going to sync " + op);
2970 Slog.v(TAG, "num active syncs: " + mActiveSyncContexts.size());
Fred Quintana918339a2010-10-05 14:00:39 -07002971 for (ActiveSyncContext syncContext : mActiveSyncContexts) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002972 Slog.v(TAG, syncContext.toString());
Fred Quintana918339a2010-10-05 14:00:39 -07002973 }
2974 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002975 // Connect to the sync adapter.
2976 int targetUid;
2977 ComponentName targetComponent;
2978 final SyncStorageEngine.EndPoint info = op.target;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002979 SyncAdapterType syncAdapterType =
2980 SyncAdapterType.newKey(info.provider, info.account.type);
2981 final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo;
2982 syncAdapterInfo = mSyncAdapters.getServiceInfo(syncAdapterType, info.userId);
2983 if (syncAdapterInfo == null) {
2984 Log.d(TAG, "can't find a sync adapter for " + syncAdapterType
2985 + ", removing settings for it");
2986 mSyncStorageEngine.removeAuthority(info);
2987 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002988 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002989 targetUid = syncAdapterInfo.uid;
2990 targetComponent = syncAdapterInfo.componentName;
Fred Quintana718d8a22009-04-29 17:53:20 -07002991 ActiveSyncContext activeSyncContext =
Matthew Williams56dbf8f2013-07-26 12:56:39 -07002992 new ActiveSyncContext(op, insertStartSyncEvent(op), targetUid);
Fred Quintana718d8a22009-04-29 17:53:20 -07002993 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00002994 Slog.v(TAG, "dispatchSyncOperation: starting " + activeSyncContext);
Fred Quintana718d8a22009-04-29 17:53:20 -07002995 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07002996
2997 activeSyncContext.mSyncInfo = mSyncStorageEngine.addActiveSync(activeSyncContext);
2998 mActiveSyncContexts.add(activeSyncContext);
Matthew Williams1967c8d2015-06-19 19:03:13 -07002999
3000 // Post message to begin monitoring this sync's progress.
3001 postMonitorSyncProgressMessage(activeSyncContext);
3002
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003003 if (!activeSyncContext.bindToSyncAdapter(targetComponent, info.userId)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003004 Slog.e(TAG, "Bind attempt failed - target: " + targetComponent);
Fred Quintana918339a2010-10-05 14:00:39 -07003005 closeActiveSyncContext(activeSyncContext);
Fred Quintana87b14662011-09-12 10:32:55 -07003006 return false;
Fred Quintana718d8a22009-04-29 17:53:20 -07003007 }
3008
Fred Quintana87b14662011-09-12 10:32:55 -07003009 return true;
Fred Quintana718d8a22009-04-29 17:53:20 -07003010 }
3011
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003012 private void runBoundToAdapterH(final ActiveSyncContext activeSyncContext,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003013 IBinder syncAdapter) {
Fred Quintana918339a2010-10-05 14:00:39 -07003014 final SyncOperation syncOperation = activeSyncContext.mSyncOperation;
Fred Quintana718d8a22009-04-29 17:53:20 -07003015 try {
Alon Alberteca75112010-12-08 15:02:33 -08003016 activeSyncContext.mIsLinkedToDeath = true;
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003017 syncAdapter.linkToDeath(activeSyncContext, 0);
Alon Alberteca75112010-12-08 15:02:33 -08003018
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003019 activeSyncContext.mSyncAdapter = ISyncAdapter.Stub.asInterface(syncAdapter);
3020 activeSyncContext.mSyncAdapter
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003021 .startSync(activeSyncContext, syncOperation.target.provider,
3022 syncOperation.target.account, syncOperation.extras);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003023 } catch (RemoteException remoteExc) {
Fred Quintana918339a2010-10-05 14:00:39 -07003024 Log.d(TAG, "maybeStartNextSync: caught a RemoteException, rescheduling", remoteExc);
3025 closeActiveSyncContext(activeSyncContext);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003026 increaseBackoffSetting(syncOperation.target);
3027 scheduleSyncOperationH(syncOperation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003028 } catch (RuntimeException exc) {
Fred Quintana918339a2010-10-05 14:00:39 -07003029 closeActiveSyncContext(activeSyncContext);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003030 Slog.e(TAG, "Caught RuntimeException while starting the sync " + syncOperation, exc);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003031 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003032 }
3033
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003034 /**
Matthew Williams8ef22042013-07-26 12:56:39 -07003035 * Cancel the sync for the provided target that matches the given bundle.
Matthew Williams1967c8d2015-06-19 19:03:13 -07003036 * @param info Can have null fields to indicate all the active syncs for that field.
3037 * @param extras Can be null to indicate <strong>all</strong> syncs for the given endpoint.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003038 */
Matthew Williams1967c8d2015-06-19 19:03:13 -07003039 private void cancelActiveSyncH(SyncStorageEngine.EndPoint info, Bundle extras) {
Fred Quintana918339a2010-10-05 14:00:39 -07003040 ArrayList<ActiveSyncContext> activeSyncs =
3041 new ArrayList<ActiveSyncContext>(mActiveSyncContexts);
3042 for (ActiveSyncContext activeSyncContext : activeSyncs) {
3043 if (activeSyncContext != null) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003044 final SyncStorageEngine.EndPoint opInfo =
3045 activeSyncContext.mSyncOperation.target;
Matthew Williams8ef22042013-07-26 12:56:39 -07003046 if (!opInfo.matchesSpec(info)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003047 continue;
Fred Quintana918339a2010-10-05 14:00:39 -07003048 }
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003049 if (extras != null &&
3050 !syncExtrasEquals(activeSyncContext.mSyncOperation.extras,
3051 extras,
3052 false /* no config settings */)) {
Amith Yamasani04e0d262012-02-14 11:50:53 -08003053 continue;
3054 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003055 mSyncJobService.callJobFinished(activeSyncContext.mSyncOperation.jobId, false);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003056 runSyncFinishedOrCanceledH(null /* cancel => no result */, activeSyncContext);
Fred Quintana918339a2010-10-05 14:00:39 -07003057 }
3058 }
3059 }
3060
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003061 /**
3062 * Should be called when a one-off instance of a periodic sync completes successfully.
3063 */
3064 private void reschedulePeriodicSyncH(SyncOperation syncOperation) {
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003065 // Ensure that the periodic sync wasn't removed.
3066 SyncOperation periodicSync = null;
Shreyas Basargecbf5ae92016-03-08 16:13:06 +00003067 List<SyncOperation> ops = getAllPendingSyncs();
Shreyas Basarge2b4c8f92016-02-08 23:52:27 +00003068 for (SyncOperation op: ops) {
3069 if (op.isPeriodic && syncOperation.matchesPeriodicOperation(op)) {
3070 periodicSync = op;
3071 break;
3072 }
3073 }
3074 if (periodicSync == null) {
3075 return;
3076 }
3077 scheduleSyncOperationH(periodicSync);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003078 }
3079
Matthew Williams8b76d202015-05-03 18:16:25 -07003080 private void runSyncFinishedOrCanceledH(SyncResult syncResult,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003081 ActiveSyncContext activeSyncContext) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003082 final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE);
Alon Alberteca75112010-12-08 15:02:33 -08003083
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003084 final SyncOperation syncOperation = activeSyncContext.mSyncOperation;
3085 final SyncStorageEngine.EndPoint info = syncOperation.target;
3086
Alon Alberteca75112010-12-08 15:02:33 -08003087 if (activeSyncContext.mIsLinkedToDeath) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003088 activeSyncContext.mSyncAdapter.asBinder().unlinkToDeath(activeSyncContext, 0);
Alon Alberteca75112010-12-08 15:02:33 -08003089 activeSyncContext.mIsLinkedToDeath = false;
3090 }
Fred Quintana918339a2010-10-05 14:00:39 -07003091 closeActiveSyncContext(activeSyncContext);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003092 final long elapsedTime = SystemClock.elapsedRealtime() - activeSyncContext.mStartTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003093 String historyMessage;
3094 int downstreamActivity;
3095 int upstreamActivity;
Shreyas Basargebd4c3ea2016-06-16 11:54:35 +01003096
3097 if (!syncOperation.isPeriodic) {
3098 // mSyncJobService.jobFinidhed is async, we need to ensure that this job is
3099 // removed from JobScheduler's pending jobs list before moving forward and
3100 // potentially rescheduling all pending jobs to respect new backoff values.
3101 getJobScheduler().cancel(syncOperation.jobId);
3102 }
3103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003104 if (syncResult != null) {
3105 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003106 Slog.v(TAG, "runSyncFinishedOrCanceled [finished]: "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003107 + syncOperation + ", result " + syncResult);
3108 }
3109
3110 if (!syncResult.hasError()) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07003111 historyMessage = SyncStorageEngine.MESG_SUCCESS;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 // TODO: set these correctly when the SyncResult is extended to include it
3113 downstreamActivity = 0;
3114 upstreamActivity = 0;
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003115 clearBackoffSetting(syncOperation.target);
3116
3117 // If the operation completes successfully and it was scheduled due to
3118 // a periodic operation failing, we reschedule the periodic operation to
3119 // start from now.
3120 if (syncOperation.isDerivedFromFailedPeriodicSync()) {
3121 reschedulePeriodicSyncH(syncOperation);
3122 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003123 } else {
Fred Quintana307da1a2010-01-21 14:24:20 -08003124 Log.d(TAG, "failed sync operation " + syncOperation + ", " + syncResult);
3125 // the operation failed so increase the backoff time
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003126 increaseBackoffSetting(syncOperation.target);
3127 if (!syncOperation.isPeriodic) {
3128 // reschedule the sync if so indicated by the syncResult
3129 maybeRescheduleSync(syncResult, syncOperation);
3130 } else {
3131 // create a normal sync instance that will respect adapter backoffs
Shreyas Basargeba1f7902016-10-01 00:19:44 +01003132 postScheduleSyncMessage(syncOperation.createOneTimeSyncOperation(),
3133 0 /* min delay */);
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003134 }
Alon Albert57286f92012-10-09 14:21:38 -07003135 historyMessage = ContentResolver.syncErrorToString(
3136 syncResultToErrorNumber(syncResult));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003137 // TODO: set these correctly when the SyncResult is extended to include it
3138 downstreamActivity = 0;
3139 upstreamActivity = 0;
3140 }
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003141 setDelayUntilTime(syncOperation.target, syncResult.delayUntil);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003142 } else {
3143 if (isLoggable) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003144 Slog.v(TAG, "runSyncFinishedOrCanceled [canceled]: " + syncOperation);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003145 }
Fred Quintana718d8a22009-04-29 17:53:20 -07003146 if (activeSyncContext.mSyncAdapter != null) {
3147 try {
Fred Quintana21bb0de2009-06-16 10:24:58 -07003148 activeSyncContext.mSyncAdapter.cancelSync(activeSyncContext);
Fred Quintana718d8a22009-04-29 17:53:20 -07003149 } catch (RemoteException e) {
3150 // we don't need to retry this in this case
3151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003152 }
Dianne Hackborn231cc602009-04-27 17:10:36 -07003153 historyMessage = SyncStorageEngine.MESG_CANCELED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003154 downstreamActivity = 0;
3155 upstreamActivity = 0;
3156 }
3157
3158 stopSyncEvent(activeSyncContext.mHistoryRowId, syncOperation, historyMessage,
3159 upstreamActivity, downstreamActivity, elapsedTime);
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003160 // Check for full-resync and schedule it after closing off the last sync.
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003161 if (syncResult != null && syncResult.tooManyDeletions) {
3162 installHandleTooManyDeletesNotification(info.account,
3163 info.provider, syncResult.stats.numDeletes,
3164 info.userId);
3165 } else {
3166 mNotificationMgr.cancelAsUser(null,
3167 info.account.hashCode() ^ info.provider.hashCode(),
3168 new UserHandle(info.userId));
3169 }
3170 if (syncResult != null && syncResult.fullSyncRequested) {
3171 scheduleSyncOperationH(
3172 new SyncOperation(info.account, info.userId,
3173 syncOperation.owningUid, syncOperation.owningPackage,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003174 syncOperation.reason,
3175 syncOperation.syncSource, info.provider, new Bundle(),
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003176 syncOperation.allowParallelSyncs));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003178 }
3179
Fred Quintana918339a2010-10-05 14:00:39 -07003180 private void closeActiveSyncContext(ActiveSyncContext activeSyncContext) {
3181 activeSyncContext.close();
3182 mActiveSyncContexts.remove(activeSyncContext);
Amith Yamasani04e0d262012-02-14 11:50:53 -08003183 mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo,
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003184 activeSyncContext.mSyncOperation.target.userId);
Matthew Williams1967c8d2015-06-19 19:03:13 -07003185
3186 if (Log.isLoggable(TAG, Log.VERBOSE)) {
Shreyas Basarge8c834c02016-01-07 13:53:16 +00003187 Slog.v(TAG, "removing all MESSAGE_MONITOR_SYNC & MESSAGE_SYNC_EXPIRED for "
Matthew Williams1967c8d2015-06-19 19:03:13 -07003188 + activeSyncContext.toString());
3189 }
Matthew Williams1967c8d2015-06-19 19:03:13 -07003190 mSyncHandler.removeMessages(SyncHandler.MESSAGE_MONITOR_SYNC, activeSyncContext);
Fred Quintana918339a2010-10-05 14:00:39 -07003191 }
3192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 /**
3194 * Convert the error-containing SyncResult into the Sync.History error number. Since
3195 * the SyncResult may indicate multiple errors at once, this method just returns the
3196 * most "serious" error.
3197 * @param syncResult the SyncResult from which to read
3198 * @return the most "serious" error set in the SyncResult
3199 * @throws IllegalStateException if the SyncResult does not indicate any errors.
3200 * If SyncResult.error() is true then it is safe to call this.
3201 */
3202 private int syncResultToErrorNumber(SyncResult syncResult) {
Dianne Hackborn231cc602009-04-27 17:10:36 -07003203 if (syncResult.syncAlreadyInProgress)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003204 return ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003205 if (syncResult.stats.numAuthExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003206 return ContentResolver.SYNC_ERROR_AUTHENTICATION;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003207 if (syncResult.stats.numIoExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003208 return ContentResolver.SYNC_ERROR_IO;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003209 if (syncResult.stats.numParseExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003210 return ContentResolver.SYNC_ERROR_PARSE;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003211 if (syncResult.stats.numConflictDetectedExceptions > 0)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003212 return ContentResolver.SYNC_ERROR_CONFLICT;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003213 if (syncResult.tooManyDeletions)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003214 return ContentResolver.SYNC_ERROR_TOO_MANY_DELETIONS;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003215 if (syncResult.tooManyRetries)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003216 return ContentResolver.SYNC_ERROR_TOO_MANY_RETRIES;
Dianne Hackborn231cc602009-04-27 17:10:36 -07003217 if (syncResult.databaseError)
Fred Quintanaac9385e2009-06-22 18:00:59 -07003218 return ContentResolver.SYNC_ERROR_INTERNAL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 throw new IllegalStateException("we are not in an error state, " + syncResult);
3220 }
3221
Fred Quintanad9d2f112009-04-23 13:36:27 -07003222 private void installHandleTooManyDeletesNotification(Account account, String authority,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003223 long numDeletes, int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003224 if (mNotificationMgr == null) return;
Fred Quintanac848b702009-08-25 20:18:46 -07003225
3226 final ProviderInfo providerInfo = mContext.getPackageManager().resolveContentProvider(
3227 authority, 0 /* flags */);
3228 if (providerInfo == null) {
3229 return;
3230 }
3231 CharSequence authorityName = providerInfo.loadLabel(mContext.getPackageManager());
3232
Fred Quintanab19e62a2010-12-16 13:54:43 -08003233 Intent clickIntent = new Intent(mContext, SyncActivityTooManyDeletes.class);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003234 clickIntent.putExtra("account", account);
Tadashi G. Takaoka86135d32009-09-24 19:31:44 -07003235 clickIntent.putExtra("authority", authority);
Fred Quintanac848b702009-08-25 20:18:46 -07003236 clickIntent.putExtra("provider", authorityName.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003237 clickIntent.putExtra("numDeletes", numDeletes);
3238
3239 if (!isActivityAvailable(clickIntent)) {
3240 Log.w(TAG, "No activity found to handle too many deletes.");
3241 return;
3242 }
3243
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003244 UserHandle user = new UserHandle(userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003245 final PendingIntent pendingIntent = PendingIntent
Dianne Hackborn41203752012-08-31 14:05:51 -07003246 .getActivityAsUser(mContext, 0, clickIntent,
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003247 PendingIntent.FLAG_CANCEL_CURRENT, null, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003248
3249 CharSequence tooManyDeletesDescFormat = mContext.getResources().getText(
3250 R.string.contentServiceTooManyDeletesNotificationDesc);
3251
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003252 Context contextForUser = getContextForUser(user);
Chris Wren1ce4b6d2015-06-11 10:19:43 -04003253 Notification notification = new Notification.Builder(contextForUser)
3254 .setSmallIcon(R.drawable.stat_notify_sync_error)
3255 .setTicker(mContext.getString(R.string.contentServiceSync))
3256 .setWhen(System.currentTimeMillis())
3257 .setColor(contextForUser.getColor(
3258 com.android.internal.R.color.system_notification_accent_color))
3259 .setContentTitle(contextForUser.getString(
3260 R.string.contentServiceSyncNotificationTitle))
3261 .setContentText(
3262 String.format(tooManyDeletesDescFormat.toString(), authorityName))
3263 .setContentIntent(pendingIntent)
3264 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 notification.flags |= Notification.FLAG_ONGOING_EVENT;
Dianne Hackborn41203752012-08-31 14:05:51 -07003266 mNotificationMgr.notifyAsUser(null, account.hashCode() ^ authority.hashCode(),
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003267 notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003268 }
3269
3270 /**
3271 * Checks whether an activity exists on the system image for the given intent.
3272 *
3273 * @param intent The intent for an activity.
3274 * @return Whether or not an activity exists.
3275 */
3276 private boolean isActivityAvailable(Intent intent) {
3277 PackageManager pm = mContext.getPackageManager();
3278 List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
3279 int listSize = list.size();
3280 for (int i = 0; i < listSize; i++) {
3281 ResolveInfo resolveInfo = list.get(i);
3282 if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM)
3283 != 0) {
3284 return true;
3285 }
3286 }
3287
3288 return false;
3289 }
3290
3291 public long insertStartSyncEvent(SyncOperation syncOperation) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003292 final long now = System.currentTimeMillis();
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003293 EventLog.writeEvent(2720,
3294 syncOperation.toEventLog(SyncStorageEngine.EVENT_START));
3295 return mSyncStorageEngine.insertStartSyncEvent(syncOperation, now);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003296 }
3297
3298 public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage,
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003299 int upstreamActivity, int downstreamActivity, long elapsedTime) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003300 EventLog.writeEvent(2720,
3301 syncOperation.toEventLog(SyncStorageEngine.EVENT_STOP));
Fred Quintana77c560f2010-03-29 22:20:26 -07003302 mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime,
Fred Quintanac5d1c6d2010-01-27 12:17:49 -08003303 resultMessage, downstreamActivity, upstreamActivity);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003304 }
3305 }
Fred Quintana918339a2010-10-05 14:00:39 -07003306
Matthew Williams8b76d202015-05-03 18:16:25 -07003307 private boolean isSyncStillActiveH(ActiveSyncContext activeSyncContext) {
Fred Quintana918339a2010-10-05 14:00:39 -07003308 for (ActiveSyncContext sync : mActiveSyncContexts) {
3309 if (sync == activeSyncContext) {
3310 return true;
3311 }
3312 }
3313 return false;
3314 }
Alon Albert57286f92012-10-09 14:21:38 -07003315
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003316 /**
3317 * Sync extra comparison function.
3318 * @param b1 bundle to compare
3319 * @param b2 other bundle to compare
3320 * @param includeSyncSettings if false, ignore system settings in bundle.
3321 */
3322 public static boolean syncExtrasEquals(Bundle b1, Bundle b2, boolean includeSyncSettings) {
3323 if (b1 == b2) {
3324 return true;
3325 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003326 // Exit early if we can.
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003327 if (includeSyncSettings && b1.size() != b2.size()) {
3328 return false;
3329 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003330 Bundle bigger = b1.size() > b2.size() ? b1 : b2;
3331 Bundle smaller = b1.size() > b2.size() ? b2 : b1;
3332 for (String key : bigger.keySet()) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003333 if (!includeSyncSettings && isSyncSetting(key)) {
3334 continue;
3335 }
Matthew Williams8ef22042013-07-26 12:56:39 -07003336 if (!smaller.containsKey(key)) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003337 return false;
3338 }
Matthew Williams9ad2c842015-10-16 12:01:31 -07003339 if (!Objects.equals(bigger.get(key), smaller.get(key))) {
Matthew Williams56dbf8f2013-07-26 12:56:39 -07003340 return false;
3341 }
3342 }
3343 return true;
3344 }
3345
3346 /**
3347 * @return true if the provided key is used by the SyncManager in scheduling the sync.
3348 */
3349 private static boolean isSyncSetting(String key) {
3350 if (key.equals(ContentResolver.SYNC_EXTRAS_EXPEDITED)) {
3351 return true;
3352 }
3353 if (key.equals(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS)) {
3354 return true;
3355 }
3356 if (key.equals(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF)) {
3357 return true;
3358 }
3359 if (key.equals(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY)) {
3360 return true;
3361 }
3362 if (key.equals(ContentResolver.SYNC_EXTRAS_MANUAL)) {
3363 return true;
3364 }
3365 if (key.equals(ContentResolver.SYNC_EXTRAS_UPLOAD)) {
3366 return true;
3367 }
3368 if (key.equals(ContentResolver.SYNC_EXTRAS_OVERRIDE_TOO_MANY_DELETIONS)) {
3369 return true;
3370 }
3371 if (key.equals(ContentResolver.SYNC_EXTRAS_DISCARD_LOCAL_DELETIONS)) {
3372 return true;
3373 }
3374 if (key.equals(ContentResolver.SYNC_EXTRAS_EXPECTED_UPLOAD)) {
3375 return true;
3376 }
3377 if (key.equals(ContentResolver.SYNC_EXTRAS_EXPECTED_DOWNLOAD)) {
3378 return true;
3379 }
3380 if (key.equals(ContentResolver.SYNC_EXTRAS_PRIORITY)) {
3381 return true;
3382 }
3383 if (key.equals(ContentResolver.SYNC_EXTRAS_DISALLOW_METERED)) {
3384 return true;
3385 }
3386 if (key.equals(ContentResolver.SYNC_EXTRAS_INITIALIZE)) {
3387 return true;
3388 }
3389 return false;
3390 }
3391
Alon Albert57286f92012-10-09 14:21:38 -07003392 static class PrintTable {
3393 private ArrayList<Object[]> mTable = Lists.newArrayList();
3394 private final int mCols;
3395
3396 PrintTable(int cols) {
3397 mCols = cols;
3398 }
3399
3400 void set(int row, int col, Object... values) {
3401 if (col + values.length > mCols) {
3402 throw new IndexOutOfBoundsException("Table only has " + mCols +
3403 " columns. can't set " + values.length + " at column " + col);
3404 }
3405 for (int i = mTable.size(); i <= row; i++) {
3406 final Object[] list = new Object[mCols];
3407 mTable.add(list);
3408 for (int j = 0; j < mCols; j++) {
3409 list[j] = "";
3410 }
3411 }
3412 System.arraycopy(values, 0, mTable.get(row), col, values.length);
3413 }
3414
3415 void writeTo(PrintWriter out) {
3416 final String[] formats = new String[mCols];
3417 int totalLength = 0;
3418 for (int col = 0; col < mCols; ++col) {
3419 int maxLength = 0;
3420 for (Object[] row : mTable) {
3421 final int length = row[col].toString().length();
3422 if (length > maxLength) {
3423 maxLength = length;
3424 }
3425 }
3426 totalLength += maxLength;
3427 formats[col] = String.format("%%-%ds", maxLength);
3428 }
Patrick Tjin31068162014-01-30 13:28:46 -08003429 formats[mCols - 1] = "%s";
Alon Albert57286f92012-10-09 14:21:38 -07003430 printRow(out, formats, mTable.get(0));
3431 totalLength += (mCols - 1) * 2;
3432 for (int i = 0; i < totalLength; ++i) {
3433 out.print("-");
3434 }
3435 out.println();
3436 for (int i = 1, mTableSize = mTable.size(); i < mTableSize; i++) {
3437 Object[] row = mTable.get(i);
3438 printRow(out, formats, row);
3439 }
3440 }
3441
3442 private void printRow(PrintWriter out, String[] formats, Object[] row) {
3443 for (int j = 0, rowLength = row.length; j < rowLength; j++) {
3444 out.printf(String.format(formats[j], row[j].toString()));
3445 out.print(" ");
3446 }
3447 out.println();
3448 }
3449
3450 public int getNumRows() {
3451 return mTable.size();
3452 }
3453 }
Kenny Guy07ad8dc2014-09-01 20:56:12 +01003454
3455 private Context getContextForUser(UserHandle user) {
3456 try {
3457 return mContext.createPackageContextAsUser(mContext.getPackageName(), 0, user);
3458 } catch (NameNotFoundException e) {
3459 // Default to mContext, not finding the package system is running as is unlikely.
3460 return mContext;
3461 }
3462 }
Ritesh Reddyae0bcdd2015-12-23 18:24:23 +00003463}