The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 17 | package com.android.server.content; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
Amith Yamasani | f29f236 | 2012-04-05 18:29:52 -0700 | [diff] [blame] | 20 | import android.accounts.AccountAndUser; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 21 | import android.accounts.AccountManager; |
Fred Quintana | 33e4469 | 2011-12-05 15:04:16 -0800 | [diff] [blame] | 22 | import android.app.ActivityManager; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 23 | import android.app.AlarmManager; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 24 | import android.app.AppGlobals; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 25 | import android.app.Notification; |
| 26 | import android.app.NotificationManager; |
| 27 | import android.app.PendingIntent; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.ComponentName; |
| 30 | import android.content.ContentResolver; |
| 31 | import android.content.Context; |
| 32 | import android.content.ISyncAdapter; |
| 33 | import android.content.ISyncContext; |
| 34 | import android.content.ISyncStatusObserver; |
| 35 | import android.content.Intent; |
| 36 | import android.content.IntentFilter; |
| 37 | import android.content.ServiceConnection; |
| 38 | import android.content.SyncActivityTooManyDeletes; |
| 39 | import android.content.SyncAdapterType; |
| 40 | import android.content.SyncAdaptersCache; |
| 41 | import android.content.SyncInfo; |
| 42 | import android.content.SyncResult; |
| 43 | import android.content.SyncStatusInfo; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 44 | import android.content.pm.ApplicationInfo; |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 45 | import android.content.pm.PackageInfo; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
| 47 | import android.content.pm.ProviderInfo; |
| 48 | import android.content.pm.RegisteredServicesCache; |
| 49 | import android.content.pm.RegisteredServicesCacheListener; |
| 50 | import android.content.pm.ResolveInfo; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 51 | import android.content.pm.UserInfo; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 52 | import android.net.ConnectivityManager; |
| 53 | import android.net.NetworkInfo; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 54 | import android.os.Bundle; |
| 55 | import android.os.Handler; |
| 56 | import android.os.HandlerThread; |
| 57 | import android.os.IBinder; |
| 58 | import android.os.Looper; |
| 59 | import android.os.Message; |
| 60 | import android.os.PowerManager; |
| 61 | import android.os.Process; |
| 62 | import android.os.RemoteException; |
| 63 | import android.os.SystemClock; |
| 64 | import android.os.SystemProperties; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 65 | import android.os.UserHandle; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 66 | import android.os.UserManager; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 67 | import android.os.WorkSource; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.provider.Settings; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | import android.text.format.DateUtils; |
| 70 | import android.text.format.Time; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | import android.util.EventLog; |
| 72 | import android.util.Log; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 73 | import android.util.Pair; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 75 | import com.android.internal.R; |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 76 | import com.android.internal.annotations.GuardedBy; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 77 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 7a96c39 | 2012-11-15 14:01:46 -0800 | [diff] [blame] | 78 | import com.android.server.accounts.AccountManagerService; |
| 79 | import com.android.server.content.SyncStorageEngine.OnSyncRequestListener; |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 80 | import com.google.android.collect.Lists; |
| 81 | import com.google.android.collect.Maps; |
| 82 | import com.google.android.collect.Sets; |
| 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import java.io.FileDescriptor; |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 85 | import java.io.PrintStream; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import java.io.PrintWriter; |
| 87 | import java.util.ArrayList; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 88 | import java.util.Arrays; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 89 | import java.util.Collection; |
| 90 | import java.util.Collections; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 91 | import java.util.Comparator; |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 92 | import java.util.HashMap; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 93 | import java.util.HashSet; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 94 | import java.util.Iterator; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import java.util.List; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 96 | import java.util.Map; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import java.util.Random; |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 98 | import java.util.Set; |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 99 | import java.util.concurrent.CountDownLatch; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | |
| 101 | /** |
| 102 | * @hide |
| 103 | */ |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 104 | public class SyncManager { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | private static final String TAG = "SyncManager"; |
| 106 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | /** Delay a sync due to local changes this long. In milliseconds */ |
Debajit Ghosh | 44ee0f0 | 2009-09-14 14:58:31 -0700 | [diff] [blame] | 108 | private static final long LOCAL_SYNC_DELAY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * If a sync takes longer than this and the sync queue is not empty then we will |
| 112 | * cancel it and add it back to the end of the sync queue. In milliseconds. |
| 113 | */ |
Debajit Ghosh | 44ee0f0 | 2009-09-14 14:58:31 -0700 | [diff] [blame] | 114 | private static final long MAX_TIME_PER_SYNC; |
| 115 | |
| 116 | static { |
Fred Quintana | 33e4469 | 2011-12-05 15:04:16 -0800 | [diff] [blame] | 117 | final boolean isLargeRAM = ActivityManager.isLargeRAM(); |
| 118 | int defaultMaxInitSyncs = isLargeRAM ? 5 : 2; |
| 119 | int defaultMaxRegularSyncs = isLargeRAM ? 2 : 1; |
| 120 | MAX_SIMULTANEOUS_INITIALIZATION_SYNCS = |
| 121 | SystemProperties.getInt("sync.max_init_syncs", defaultMaxInitSyncs); |
| 122 | MAX_SIMULTANEOUS_REGULAR_SYNCS = |
| 123 | SystemProperties.getInt("sync.max_regular_syncs", defaultMaxRegularSyncs); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 124 | LOCAL_SYNC_DELAY = |
| 125 | SystemProperties.getLong("sync.local_sync_delay", 30 * 1000 /* 30 seconds */); |
| 126 | MAX_TIME_PER_SYNC = |
| 127 | SystemProperties.getLong("sync.max_time_per_sync", 5 * 60 * 1000 /* 5 minutes */); |
| 128 | SYNC_NOTIFICATION_DELAY = |
| 129 | SystemProperties.getLong("sync.notification_delay", 30 * 1000 /* 30 seconds */); |
Debajit Ghosh | 44ee0f0 | 2009-09-14 14:58:31 -0700 | [diff] [blame] | 130 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 132 | private static final long SYNC_NOTIFICATION_DELAY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * When retrying a sync for the first time use this delay. After that |
| 136 | * the retry time will double until it reached MAX_SYNC_RETRY_TIME. |
| 137 | * In milliseconds. |
| 138 | */ |
| 139 | private static final long INITIAL_SYNC_RETRY_TIME_IN_MS = 30 * 1000; // 30 seconds |
| 140 | |
| 141 | /** |
| 142 | * Default the max sync retry time to this value. |
| 143 | */ |
| 144 | private static final long DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS = 60 * 60; // one hour |
| 145 | |
| 146 | /** |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 147 | * How long to wait before retrying a sync that failed due to one already being in progress. |
| 148 | */ |
| 149 | private static final int DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS = 10; |
| 150 | |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 151 | private static final int INITIALIZATION_UNBIND_DELAY_MS = 5000; |
| 152 | |
Dianne Hackborn | e746f03 | 2010-09-13 16:02:57 -0700 | [diff] [blame] | 153 | private static final String SYNC_WAKE_LOCK_PREFIX = "*sync*"; |
Dianne Hackborn | 7e9f4eb | 2010-09-10 18:43:00 -0700 | [diff] [blame] | 154 | private static final String HANDLE_SYNC_ALARM_WAKE_LOCK = "SyncManagerHandleSyncAlarm"; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 155 | private static final String SYNC_LOOP_WAKE_LOCK = "SyncLoopWakeLock"; |
| 156 | |
| 157 | private static final int MAX_SIMULTANEOUS_REGULAR_SYNCS; |
| 158 | private static final int MAX_SIMULTANEOUS_INITIALIZATION_SYNCS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | |
| 160 | private Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 162 | private static final AccountAndUser[] INITIAL_ACCOUNTS_ARRAY = new AccountAndUser[0]; |
| 163 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 164 | // TODO: add better locking around mRunningAccounts |
| 165 | private volatile AccountAndUser[] mRunningAccounts = INITIAL_ACCOUNTS_ARRAY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | volatile private PowerManager.WakeLock mHandleAlarmWakeLock; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 168 | volatile private PowerManager.WakeLock mSyncManagerWakeLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | volatile private boolean mDataConnectionIsConnected = false; |
| 170 | volatile private boolean mStorageIsLow = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
| 172 | private final NotificationManager mNotificationMgr; |
| 173 | private AlarmManager mAlarmService = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 175 | private SyncStorageEngine mSyncStorageEngine; |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 176 | |
Jeff Sharkey | 8b2c3a14 | 2012-11-12 11:45:05 -0800 | [diff] [blame] | 177 | @GuardedBy("mSyncQueue") |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 178 | private final SyncQueue mSyncQueue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 180 | protected final ArrayList<ActiveSyncContext> mActiveSyncContexts = Lists.newArrayList(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | // set if the sync active indicator should be reported |
| 183 | private boolean mNeedSyncActiveNotification = false; |
| 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | private final PendingIntent mSyncAlarmIntent; |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 186 | // Synchronized on "this". Instead of using this directly one should instead call |
| 187 | // its accessor, getConnManager(). |
| 188 | private ConnectivityManager mConnManagerDoNotUseDirectly; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 190 | protected SyncAdaptersCache mSyncAdapters; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 191 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | private BroadcastReceiver mStorageIntentReceiver = |
| 193 | new BroadcastReceiver() { |
| 194 | public void onReceive(Context context, Intent intent) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | String action = intent.getAction(); |
| 196 | if (Intent.ACTION_DEVICE_STORAGE_LOW.equals(action)) { |
| 197 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 198 | Log.v(TAG, "Internal storage is low."); |
| 199 | } |
| 200 | mStorageIsLow = true; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 201 | cancelActiveSync(null /* any account */, UserHandle.USER_ALL, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 202 | null /* any authority */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | } else if (Intent.ACTION_DEVICE_STORAGE_OK.equals(action)) { |
| 204 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 205 | Log.v(TAG, "Internal storage is ok."); |
| 206 | } |
| 207 | mStorageIsLow = false; |
| 208 | sendCheckAlarmsMessage(); |
| 209 | } |
| 210 | } |
| 211 | }; |
| 212 | |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 213 | private BroadcastReceiver mBootCompletedReceiver = new BroadcastReceiver() { |
| 214 | public void onReceive(Context context, Intent intent) { |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 215 | mSyncHandler.onBootCompleted(); |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 216 | } |
| 217 | }; |
| 218 | |
Fred Quintana | 600dde0 | 2009-09-23 12:44:10 -0700 | [diff] [blame] | 219 | private BroadcastReceiver mBackgroundDataSettingChanged = new BroadcastReceiver() { |
| 220 | public void onReceive(Context context, Intent intent) { |
| 221 | if (getConnectivityManager().getBackgroundDataSetting()) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 222 | scheduleSync(null /* account */, UserHandle.USER_ALL, |
| 223 | SyncOperation.REASON_BACKGROUND_DATA_SETTINGS_CHANGED, |
| 224 | null /* authority */, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 225 | new Bundle(), 0 /* delay */, |
Fred Quintana | 600dde0 | 2009-09-23 12:44:10 -0700 | [diff] [blame] | 226 | false /* onlyThoseWithUnknownSyncableState */); |
| 227 | } |
| 228 | } |
| 229 | }; |
| 230 | |
Amith Yamasani | d648a60 | 2012-09-26 15:06:10 -0700 | [diff] [blame] | 231 | private BroadcastReceiver mAccountsUpdatedReceiver = new BroadcastReceiver() { |
| 232 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 233 | updateRunningAccounts(); |
| 234 | |
| 235 | // Kick off sync for everyone, since this was a radical account change |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 236 | scheduleSync(null, UserHandle.USER_ALL, SyncOperation.REASON_ACCOUNTS_UPDATED, null, |
| 237 | null, 0 /* no delay */, false); |
Amith Yamasani | d648a60 | 2012-09-26 15:06:10 -0700 | [diff] [blame] | 238 | } |
| 239 | }; |
| 240 | |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 241 | private final PowerManager mPowerManager; |
| 242 | |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 243 | // Use this as a random offset to seed all periodic syncs |
| 244 | private int mSyncRandomOffsetMillis; |
| 245 | |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 246 | private final UserManager mUserManager; |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 247 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 248 | private static final long SYNC_ALARM_TIMEOUT_MIN = 30 * 1000; // 30 seconds |
| 249 | private static final long SYNC_ALARM_TIMEOUT_MAX = 2 * 60 * 60 * 1000; // two hours |
| 250 | |
Amith Yamasani | 258848d | 2012-08-10 17:06:33 -0700 | [diff] [blame] | 251 | private List<UserInfo> getAllUsers() { |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 252 | return mUserManager.getUsers(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | private boolean containsAccountAndUser(AccountAndUser[] accounts, Account account, int userId) { |
| 256 | boolean found = false; |
| 257 | for (int i = 0; i < accounts.length; i++) { |
| 258 | if (accounts[i].userId == userId |
| 259 | && accounts[i].account.equals(account)) { |
| 260 | found = true; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | return found; |
| 265 | } |
| 266 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 267 | public void updateRunningAccounts() { |
| 268 | mRunningAccounts = AccountManagerService.getSingleton().getRunningAccounts(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 269 | |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 270 | if (mBootCompleted) { |
| 271 | doDatabaseCleanup(); |
| 272 | } |
| 273 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 274 | for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 275 | if (!containsAccountAndUser(mRunningAccounts, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 276 | currentSyncContext.mSyncOperation.account, |
| 277 | currentSyncContext.mSyncOperation.userId)) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 278 | Log.d(TAG, "canceling sync since the account is no longer running"); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 279 | sendSyncFinishedOrCanceledMessage(currentSyncContext, |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 280 | null /* no result since this is a cancel */); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // we must do this since we don't bother scheduling alarms when |
| 285 | // the accounts are not set yet |
| 286 | sendCheckAlarmsMessage(); |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 289 | private void doDatabaseCleanup() { |
Amith Yamasani | db6a14c | 2012-10-17 21:16:52 -0700 | [diff] [blame] | 290 | for (UserInfo user : mUserManager.getUsers(true)) { |
| 291 | // Skip any partially created/removed users |
| 292 | if (user.partial) continue; |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 293 | Account[] accountsForUser = AccountManagerService.getSingleton().getAccounts(user.id); |
| 294 | mSyncStorageEngine.doDatabaseCleanup(accountsForUser, user.id); |
| 295 | } |
| 296 | } |
| 297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | private BroadcastReceiver mConnectivityIntentReceiver = |
| 299 | new BroadcastReceiver() { |
| 300 | public void onReceive(Context context, Intent intent) { |
Alon Albert | ed1d253 | 2011-02-15 14:02:14 -0800 | [diff] [blame] | 301 | final boolean wasConnected = mDataConnectionIsConnected; |
| 302 | |
| 303 | // don't use the intent to figure out if network is connected, just check |
| 304 | // ConnectivityManager directly. |
Alon Albert | 1bad83a | 2011-02-16 10:29:56 -0800 | [diff] [blame] | 305 | mDataConnectionIsConnected = readDataConnectionState(); |
Alon Albert | ed1d253 | 2011-02-15 14:02:14 -0800 | [diff] [blame] | 306 | if (mDataConnectionIsConnected) { |
| 307 | if (!wasConnected) { |
| 308 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 309 | Log.v(TAG, "Reconnection detected: clearing all backoffs"); |
| 310 | } |
| 311 | mSyncStorageEngine.clearAllBackoffs(mSyncQueue); |
| 312 | } |
| 313 | sendCheckAlarmsMessage(); |
| 314 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 315 | } |
| 316 | }; |
| 317 | |
Alon Albert | 1bad83a | 2011-02-16 10:29:56 -0800 | [diff] [blame] | 318 | private boolean readDataConnectionState() { |
Alon Albert | ed1d253 | 2011-02-15 14:02:14 -0800 | [diff] [blame] | 319 | NetworkInfo networkInfo = getConnectivityManager().getActiveNetworkInfo(); |
| 320 | return (networkInfo != null) && networkInfo.isConnected(); |
| 321 | } |
| 322 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 323 | private BroadcastReceiver mShutdownIntentReceiver = |
| 324 | new BroadcastReceiver() { |
| 325 | public void onReceive(Context context, Intent intent) { |
| 326 | Log.w(TAG, "Writing sync state before shutdown..."); |
| 327 | getSyncStorageEngine().writeAllState(); |
| 328 | } |
| 329 | }; |
| 330 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 331 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 332 | @Override |
| 333 | public void onReceive(Context context, Intent intent) { |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 334 | String action = intent.getAction(); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 335 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 336 | if (userId == UserHandle.USER_NULL) return; |
| 337 | |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 338 | if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 339 | onUserRemoved(userId); |
| 340 | } else if (Intent.ACTION_USER_STARTING.equals(action)) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 341 | onUserStarting(userId); |
| 342 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 343 | onUserStopping(userId); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 344 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 345 | } |
| 346 | }; |
| 347 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | private static final String ACTION_SYNC_ALARM = "android.content.syncmanager.SYNC_ALARM"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | private final SyncHandler mSyncHandler; |
| 350 | |
Fred Quintana | 4f9cfc5 | 2009-09-02 15:20:23 -0700 | [diff] [blame] | 351 | private volatile boolean mBootCompleted = false; |
| 352 | |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 353 | private ConnectivityManager getConnectivityManager() { |
| 354 | synchronized (this) { |
| 355 | if (mConnManagerDoNotUseDirectly == null) { |
| 356 | mConnManagerDoNotUseDirectly = (ConnectivityManager)mContext.getSystemService( |
| 357 | Context.CONNECTIVITY_SERVICE); |
| 358 | } |
| 359 | return mConnManagerDoNotUseDirectly; |
| 360 | } |
| 361 | } |
| 362 | |
Jeff Sharkey | e4996bb | 2012-10-17 14:16:28 -0700 | [diff] [blame] | 363 | /** |
| 364 | * Should only be created after {@link ContentService#systemReady()} so that |
| 365 | * {@link PackageManager} is ready to query. |
| 366 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 367 | public SyncManager(Context context, boolean factoryTest) { |
| 368 | // Initialize the SyncStorageEngine first, before registering observers |
| 369 | // and creating threads and so on; it may fail if the disk is full. |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 370 | mContext = context; |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 371 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 372 | SyncStorageEngine.init(context); |
| 373 | mSyncStorageEngine = SyncStorageEngine.getSingleton(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 374 | mSyncStorageEngine.setOnSyncRequestListener(new OnSyncRequestListener() { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 375 | public void onSyncRequest(Account account, int userId, int reason, String authority, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 376 | Bundle extras) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 377 | scheduleSync(account, userId, reason, authority, extras, 0, false); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 378 | } |
| 379 | }); |
| 380 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 381 | mSyncAdapters = new SyncAdaptersCache(mContext); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 382 | mSyncQueue = new SyncQueue(mContext.getPackageManager(), mSyncStorageEngine, mSyncAdapters); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 384 | HandlerThread syncThread = new HandlerThread("SyncHandlerThread", |
| 385 | Process.THREAD_PRIORITY_BACKGROUND); |
| 386 | syncThread.start(); |
| 387 | mSyncHandler = new SyncHandler(syncThread.getLooper()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 388 | |
Fred Quintana | 44037e6 | 2010-01-21 13:14:49 -0800 | [diff] [blame] | 389 | mSyncAdapters.setListener(new RegisteredServicesCacheListener<SyncAdapterType>() { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 390 | @Override |
| 391 | public void onServiceChanged(SyncAdapterType type, int userId, boolean removed) { |
Fred Quintana | 44037e6 | 2010-01-21 13:14:49 -0800 | [diff] [blame] | 392 | if (!removed) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 393 | scheduleSync(null, UserHandle.USER_ALL, |
| 394 | SyncOperation.REASON_SERVICE_CHANGED, |
| 395 | type.authority, null, 0 /* no delay */, |
Fred Quintana | 44037e6 | 2010-01-21 13:14:49 -0800 | [diff] [blame] | 396 | false /* onlyThoseWithUnkownSyncableState */); |
| 397 | } |
| 398 | } |
| 399 | }, mSyncHandler); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 400 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 401 | mSyncAlarmIntent = PendingIntent.getBroadcast( |
| 402 | mContext, 0 /* ignored */, new Intent(ACTION_SYNC_ALARM), 0); |
| 403 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 404 | IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); |
| 405 | context.registerReceiver(mConnectivityIntentReceiver, intentFilter); |
| 406 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 407 | if (!factoryTest) { |
| 408 | intentFilter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED); |
| 409 | context.registerReceiver(mBootCompletedReceiver, intentFilter); |
| 410 | } |
Fred Quintana | 6030734 | 2009-03-24 22:48:12 -0700 | [diff] [blame] | 411 | |
Fred Quintana | 600dde0 | 2009-09-23 12:44:10 -0700 | [diff] [blame] | 412 | intentFilter = new IntentFilter(ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); |
| 413 | context.registerReceiver(mBackgroundDataSettingChanged, intentFilter); |
| 414 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | intentFilter = new IntentFilter(Intent.ACTION_DEVICE_STORAGE_LOW); |
| 416 | intentFilter.addAction(Intent.ACTION_DEVICE_STORAGE_OK); |
| 417 | context.registerReceiver(mStorageIntentReceiver, intentFilter); |
| 418 | |
Dianne Hackborn | 55280a9 | 2009-05-07 15:53:46 -0700 | [diff] [blame] | 419 | intentFilter = new IntentFilter(Intent.ACTION_SHUTDOWN); |
| 420 | intentFilter.setPriority(100); |
| 421 | context.registerReceiver(mShutdownIntentReceiver, intentFilter); |
| 422 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 423 | intentFilter = new IntentFilter(); |
| 424 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 425 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 426 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 427 | mContext.registerReceiverAsUser( |
| 428 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | if (!factoryTest) { |
| 431 | mNotificationMgr = (NotificationManager) |
| 432 | context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 433 | context.registerReceiver(new SyncAlarmIntentReceiver(), |
| 434 | new IntentFilter(ACTION_SYNC_ALARM)); |
| 435 | } else { |
| 436 | mNotificationMgr = null; |
| 437 | } |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 438 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Amith Yamasani | 9535c91 | 2012-10-10 21:48:33 -0700 | [diff] [blame] | 439 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | |
| 441 | // This WakeLock is used to ensure that we stay awake between the time that we receive |
| 442 | // a sync alarm notification and when we finish processing it. We need to do this |
| 443 | // because we don't do the work in the alarm handler, rather we do it in a message |
| 444 | // handler. |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 445 | mHandleAlarmWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 | HANDLE_SYNC_ALARM_WAKE_LOCK); |
| 447 | mHandleAlarmWakeLock.setReferenceCounted(false); |
| 448 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 449 | // This WakeLock is used to ensure that we stay awake while running the sync loop |
| 450 | // message handler. Normally we will hold a sync adapter wake lock while it is being |
| 451 | // synced but during the execution of the sync loop it might finish a sync for |
| 452 | // one sync adapter before starting the sync for the other sync adapter and we |
| 453 | // don't want the device to go to sleep during that window. |
| 454 | mSyncManagerWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, |
| 455 | SYNC_LOOP_WAKE_LOCK); |
| 456 | mSyncManagerWakeLock.setReferenceCounted(false); |
| 457 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 458 | mSyncStorageEngine.addStatusChangeListener( |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 459 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, new ISyncStatusObserver.Stub() { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 460 | public void onStatusChanged(int which) { |
| 461 | // force the sync loop to run if the settings change |
| 462 | sendCheckAlarmsMessage(); |
| 463 | } |
| 464 | }); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 465 | |
| 466 | if (!factoryTest) { |
Amith Yamasani | d648a60 | 2012-09-26 15:06:10 -0700 | [diff] [blame] | 467 | // Register for account list updates for all users |
| 468 | mContext.registerReceiverAsUser(mAccountsUpdatedReceiver, |
| 469 | UserHandle.ALL, |
| 470 | new IntentFilter(AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION), |
| 471 | null, null); |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 472 | } |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 473 | |
| 474 | // Pick a random second in a day to seed all periodic syncs |
| 475 | mSyncRandomOffsetMillis = mSyncStorageEngine.getSyncRandomOffset() * 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | } |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | /** |
| 479 | * Return a random value v that satisfies minValue <= v < maxValue. The difference between |
| 480 | * maxValue and minValue must be less than Integer.MAX_VALUE. |
| 481 | */ |
| 482 | private long jitterize(long minValue, long maxValue) { |
| 483 | Random random = new Random(SystemClock.elapsedRealtime()); |
| 484 | long spread = maxValue - minValue; |
| 485 | if (spread > Integer.MAX_VALUE) { |
| 486 | throw new IllegalArgumentException("the difference between the maxValue and the " |
| 487 | + "minValue must be less than " + Integer.MAX_VALUE); |
| 488 | } |
| 489 | return minValue + random.nextInt((int)spread); |
| 490 | } |
| 491 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 492 | public SyncStorageEngine getSyncStorageEngine() { |
| 493 | return mSyncStorageEngine; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 495 | |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 496 | public int getIsSyncable(Account account, int userId, String providerName) { |
| 497 | int isSyncable = mSyncStorageEngine.getIsSyncable(account, userId, providerName); |
| 498 | UserInfo userInfo = UserManager.get(mContext).getUserInfo(userId); |
| 499 | |
| 500 | // If it's not a restricted user, return isSyncable |
| 501 | if (userInfo == null || !userInfo.isRestricted()) return isSyncable; |
| 502 | |
| 503 | // Else check if the sync adapter has opted-in or not |
| 504 | RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo = |
| 505 | mSyncAdapters.getServiceInfo( |
| 506 | SyncAdapterType.newKey(providerName, account.type), userId); |
| 507 | if (syncAdapterInfo == null) return isSyncable; |
| 508 | |
| 509 | PackageInfo pInfo = null; |
| 510 | try { |
| 511 | pInfo = AppGlobals.getPackageManager().getPackageInfo( |
| 512 | syncAdapterInfo.componentName.getPackageName(), 0, userId); |
| 513 | if (pInfo == null) return isSyncable; |
| 514 | } catch (RemoteException re) { |
| 515 | // Shouldn't happen |
| 516 | return isSyncable; |
| 517 | } |
| 518 | if (pInfo.restrictedAccountType != null |
| 519 | && pInfo.restrictedAccountType.equals(account.type)) { |
| 520 | return isSyncable; |
| 521 | } else { |
| 522 | return 0; |
| 523 | } |
| 524 | } |
| 525 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 526 | private void ensureAlarmService() { |
| 527 | if (mAlarmService == null) { |
| 528 | mAlarmService = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 529 | } |
| 530 | } |
| 531 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | * Initiate a sync. This can start a sync for all providers |
| 534 | * (pass null to url, set onlyTicklable to false), only those |
| 535 | * providers that are marked as ticklable (pass null to url, |
| 536 | * set onlyTicklable to true), or a specific provider (set url |
| 537 | * to the content url of the provider). |
| 538 | * |
| 539 | * <p>If the ContentResolver.SYNC_EXTRAS_UPLOAD boolean in extras is |
| 540 | * true then initiate a sync that just checks for local changes to send |
| 541 | * to the server, otherwise initiate a sync that first gets any |
| 542 | * changes from the server before sending local changes back to |
| 543 | * the server. |
| 544 | * |
| 545 | * <p>If a specific provider is being synced (the url is non-null) |
| 546 | * then the extras can contain SyncAdapter-specific information |
| 547 | * to control what gets synced (e.g. which specific feed to sync). |
| 548 | * |
| 549 | * <p>You'll start getting callbacks after this. |
| 550 | * |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 551 | * @param requestedAccount the account to sync, may be null to signify all accounts |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 552 | * @param userId the id of the user whose accounts are to be synced. If userId is USER_ALL, |
| 553 | * then all users' accounts are considered. |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 554 | * @param reason for sync request. If this is a positive integer, it is the Linux uid |
| 555 | * assigned to the process that requested the sync. If it's negative, the sync was requested by |
| 556 | * the SyncManager itself and could be one of the following: |
| 557 | * {@link SyncOperation#REASON_BACKGROUND_DATA_SETTINGS_CHANGED} |
| 558 | * {@link SyncOperation#REASON_ACCOUNTS_UPDATED} |
| 559 | * {@link SyncOperation#REASON_SERVICE_CHANGED} |
| 560 | * {@link SyncOperation#REASON_PERIODIC} |
| 561 | * {@link SyncOperation#REASON_IS_SYNCABLE} |
| 562 | * {@link SyncOperation#REASON_SYNC_AUTO} |
| 563 | * {@link SyncOperation#REASON_MASTER_SYNC_AUTO} |
| 564 | * {@link SyncOperation#REASON_USER_START} |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 565 | * @param requestedAuthority the authority to sync, may be null to indicate all authorities |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | * @param extras a Map of SyncAdapter-specific information to control |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 567 | * syncs of a specific provider. Can be null. Is ignored |
| 568 | * if the url is null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 569 | * @param delay how many milliseconds in the future to wait before performing this |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 570 | * @param onlyThoseWithUnkownSyncableState |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | */ |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 572 | public void scheduleSync(Account requestedAccount, int userId, int reason, |
| 573 | String requestedAuthority, Bundle extras, long delay, |
| 574 | boolean onlyThoseWithUnkownSyncableState) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 576 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 577 | final boolean backgroundDataUsageAllowed = !mBootCompleted || |
Jim Miller | d2a3a8a | 2009-09-15 17:34:42 -0700 | [diff] [blame] | 578 | getConnectivityManager().getBackgroundDataSetting(); |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 579 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | if (extras == null) extras = new Bundle(); |
| 581 | |
| 582 | Boolean expedited = extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, false); |
| 583 | if (expedited) { |
| 584 | delay = -1; // this means schedule at the front of the queue |
| 585 | } |
| 586 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 587 | AccountAndUser[] accounts; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 588 | if (requestedAccount != null && userId != UserHandle.USER_ALL) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 589 | accounts = new AccountAndUser[] { new AccountAndUser(requestedAccount, userId) }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | } else { |
| 591 | // if the accounts aren't configured yet then we can't support an account-less |
| 592 | // sync request |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 593 | accounts = mRunningAccounts; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | if (accounts.length == 0) { |
| 595 | if (isLoggable) { |
| 596 | Log.v(TAG, "scheduleSync: no accounts configured, dropping"); |
| 597 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 598 | return; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | final boolean uploadOnly = extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 603 | final boolean manualSync = extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 604 | if (manualSync) { |
| 605 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, true); |
| 606 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, true); |
| 607 | } |
| 608 | final boolean ignoreSettings = |
| 609 | extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 610 | |
| 611 | int source; |
| 612 | if (uploadOnly) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 613 | source = SyncStorageEngine.SOURCE_LOCAL; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 614 | } else if (manualSync) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 615 | source = SyncStorageEngine.SOURCE_USER; |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 616 | } else if (requestedAuthority == null) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 617 | source = SyncStorageEngine.SOURCE_POLL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 618 | } else { |
| 619 | // this isn't strictly server, since arbitrary callers can (and do) request |
| 620 | // a non-forced two-way sync on a specific url |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 621 | source = SyncStorageEngine.SOURCE_SERVER; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 624 | for (AccountAndUser account : accounts) { |
| 625 | // Compile a list of authorities that have sync adapters. |
| 626 | // For each authority sync each account that matches a sync adapter. |
| 627 | final HashSet<String> syncableAuthorities = new HashSet<String>(); |
| 628 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapter : |
| 629 | mSyncAdapters.getAllServices(account.userId)) { |
| 630 | syncableAuthorities.add(syncAdapter.type.authority); |
| 631 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 632 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 633 | // if the url was specified then replace the list of authorities |
| 634 | // with just this authority or clear it if this authority isn't |
| 635 | // syncable |
| 636 | if (requestedAuthority != null) { |
| 637 | final boolean hasSyncAdapter = syncableAuthorities.contains(requestedAuthority); |
| 638 | syncableAuthorities.clear(); |
| 639 | if (hasSyncAdapter) syncableAuthorities.add(requestedAuthority); |
| 640 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 641 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 642 | for (String authority : syncableAuthorities) { |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 643 | int isSyncable = getIsSyncable(account.account, account.userId, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 644 | authority); |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 645 | if (isSyncable == 0) { |
Fred Quintana | 5e787c4 | 2009-08-16 23:13:53 -0700 | [diff] [blame] | 646 | continue; |
| 647 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 648 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo; |
| 649 | syncAdapterInfo = mSyncAdapters.getServiceInfo( |
| 650 | SyncAdapterType.newKey(authority, account.account.type), account.userId); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 651 | if (syncAdapterInfo == null) { |
| 652 | continue; |
| 653 | } |
| 654 | final boolean allowParallelSyncs = syncAdapterInfo.type.allowParallelSyncs(); |
| 655 | final boolean isAlwaysSyncable = syncAdapterInfo.type.isAlwaysSyncable(); |
| 656 | if (isSyncable < 0 && isAlwaysSyncable) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 657 | mSyncStorageEngine.setIsSyncable(account.account, account.userId, authority, 1); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 658 | isSyncable = 1; |
| 659 | } |
| 660 | if (onlyThoseWithUnkownSyncableState && isSyncable >= 0) { |
| 661 | continue; |
| 662 | } |
| 663 | if (!syncAdapterInfo.type.supportsUploading() && uploadOnly) { |
| 664 | continue; |
| 665 | } |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 666 | |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 667 | // always allow if the isSyncable state is unknown |
| 668 | boolean syncAllowed = |
| 669 | (isSyncable < 0) |
| 670 | || ignoreSettings |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 671 | || (backgroundDataUsageAllowed |
| 672 | && mSyncStorageEngine.getMasterSyncAutomatically(account.userId) |
| 673 | && mSyncStorageEngine.getSyncAutomatically(account.account, |
| 674 | account.userId, authority)); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 675 | if (!syncAllowed) { |
| 676 | if (isLoggable) { |
| 677 | Log.d(TAG, "scheduleSync: sync of " + account + ", " + authority |
| 678 | + " is not allowed, dropping request"); |
Fred Quintana | e7424ff | 2009-10-14 15:59:21 -0700 | [diff] [blame] | 679 | } |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 680 | continue; |
| 681 | } |
Fred Quintana | e7424ff | 2009-10-14 15:59:21 -0700 | [diff] [blame] | 682 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 683 | Pair<Long, Long> backoff = mSyncStorageEngine |
| 684 | .getBackoff(account.account, account.userId, authority); |
| 685 | long delayUntil = mSyncStorageEngine.getDelayUntilTime(account.account, |
| 686 | account.userId, authority); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 687 | final long backoffTime = backoff != null ? backoff.first : 0; |
| 688 | if (isSyncable < 0) { |
| 689 | Bundle newExtras = new Bundle(); |
| 690 | newExtras.putBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, true); |
| 691 | if (isLoggable) { |
| 692 | Log.v(TAG, "scheduleSync:" |
| 693 | + " delay " + delay |
| 694 | + ", source " + source |
| 695 | + ", account " + account |
| 696 | + ", authority " + authority |
| 697 | + ", extras " + newExtras); |
Fred Quintana | e0616ff | 2009-08-19 13:13:18 -0700 | [diff] [blame] | 698 | } |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 699 | scheduleSyncOperation( |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 700 | new SyncOperation(account.account, account.userId, reason, source, |
| 701 | authority, newExtras, 0, backoffTime, delayUntil, |
| 702 | allowParallelSyncs)); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 703 | } |
| 704 | if (!onlyThoseWithUnkownSyncableState) { |
| 705 | if (isLoggable) { |
| 706 | Log.v(TAG, "scheduleSync:" |
| 707 | + " delay " + delay |
| 708 | + ", source " + source |
| 709 | + ", account " + account |
| 710 | + ", authority " + authority |
| 711 | + ", extras " + extras); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 712 | } |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 713 | scheduleSyncOperation( |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 714 | new SyncOperation(account.account, account.userId, reason, source, |
| 715 | authority, extras, delay, backoffTime, delayUntil, |
| 716 | allowParallelSyncs)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | } |
| 720 | } |
| 721 | |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 722 | public void scheduleLocalSync(Account account, int userId, int reason, String authority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 723 | final Bundle extras = new Bundle(); |
| 724 | extras.putBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, true); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 725 | scheduleSync(account, userId, reason, authority, extras, LOCAL_SYNC_DELAY, |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 726 | false /* onlyThoseWithUnkownSyncableState */); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | } |
| 728 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 729 | public SyncAdapterType[] getSyncAdapterTypes(int userId) { |
| 730 | final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos; |
| 731 | serviceInfos = mSyncAdapters.getAllServices(userId); |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 732 | SyncAdapterType[] types = new SyncAdapterType[serviceInfos.size()]; |
| 733 | int i = 0; |
| 734 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> serviceInfo : serviceInfos) { |
| 735 | types[i] = serviceInfo.type; |
| 736 | ++i; |
| 737 | } |
| 738 | return types; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 739 | } |
| 740 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | private void sendSyncAlarmMessage() { |
| 742 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "sending MESSAGE_SYNC_ALARM"); |
| 743 | mSyncHandler.sendEmptyMessage(SyncHandler.MESSAGE_SYNC_ALARM); |
| 744 | } |
| 745 | |
| 746 | private void sendCheckAlarmsMessage() { |
| 747 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "sending MESSAGE_CHECK_ALARMS"); |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 748 | mSyncHandler.removeMessages(SyncHandler.MESSAGE_CHECK_ALARMS); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 749 | mSyncHandler.sendEmptyMessage(SyncHandler.MESSAGE_CHECK_ALARMS); |
| 750 | } |
| 751 | |
| 752 | private void sendSyncFinishedOrCanceledMessage(ActiveSyncContext syncContext, |
| 753 | SyncResult syncResult) { |
| 754 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "sending MESSAGE_SYNC_FINISHED"); |
| 755 | Message msg = mSyncHandler.obtainMessage(); |
| 756 | msg.what = SyncHandler.MESSAGE_SYNC_FINISHED; |
| 757 | msg.obj = new SyncHandlerMessagePayload(syncContext, syncResult); |
| 758 | mSyncHandler.sendMessage(msg); |
| 759 | } |
| 760 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 761 | private void sendCancelSyncsMessage(final Account account, final int userId, |
| 762 | final String authority) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 763 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "sending MESSAGE_CANCEL"); |
| 764 | Message msg = mSyncHandler.obtainMessage(); |
| 765 | msg.what = SyncHandler.MESSAGE_CANCEL; |
| 766 | msg.obj = Pair.create(account, authority); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 767 | msg.arg1 = userId; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 768 | mSyncHandler.sendMessage(msg); |
| 769 | } |
| 770 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 771 | class SyncHandlerMessagePayload { |
| 772 | public final ActiveSyncContext activeSyncContext; |
| 773 | public final SyncResult syncResult; |
| 774 | |
| 775 | SyncHandlerMessagePayload(ActiveSyncContext syncContext, SyncResult syncResult) { |
| 776 | this.activeSyncContext = syncContext; |
| 777 | this.syncResult = syncResult; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | class SyncAlarmIntentReceiver extends BroadcastReceiver { |
| 782 | public void onReceive(Context context, Intent intent) { |
| 783 | mHandleAlarmWakeLock.acquire(); |
| 784 | sendSyncAlarmMessage(); |
| 785 | } |
| 786 | } |
| 787 | |
Alon Albert | 6e079a3 | 2010-11-12 12:41:09 -0800 | [diff] [blame] | 788 | private void clearBackoffSetting(SyncOperation op) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 789 | mSyncStorageEngine.setBackoff(op.account, op.userId, op.authority, |
Alon Albert | 6e079a3 | 2010-11-12 12:41:09 -0800 | [diff] [blame] | 790 | SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE); |
| 791 | synchronized (mSyncQueue) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 792 | mSyncQueue.onBackoffChanged(op.account, op.userId, op.authority, 0); |
Alon Albert | 6e079a3 | 2010-11-12 12:41:09 -0800 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 796 | private void increaseBackoffSetting(SyncOperation op) { |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 797 | // TODO: Use this function to align it to an already scheduled sync |
| 798 | // operation in the specified window |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 799 | final long now = SystemClock.elapsedRealtime(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 800 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 801 | final Pair<Long, Long> previousSettings = |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 802 | mSyncStorageEngine.getBackoff(op.account, op.userId, op.authority); |
Alon Albert | aeeb620 | 2010-12-09 16:14:02 -0800 | [diff] [blame] | 803 | long newDelayInMs = -1; |
| 804 | if (previousSettings != null) { |
| 805 | // don't increase backoff before current backoff is expired. This will happen for op's |
| 806 | // with ignoreBackoff set. |
| 807 | if (now < previousSettings.first) { |
| 808 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 809 | Log.v(TAG, "Still in backoff, do not increase it. " |
| 810 | + "Remaining: " + ((previousSettings.first - now) / 1000) + " seconds."); |
| 811 | } |
| 812 | return; |
| 813 | } |
| 814 | // Subsequent delays are the double of the previous delay |
| 815 | newDelayInMs = previousSettings.second * 2; |
| 816 | } |
| 817 | if (newDelayInMs <= 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 818 | // The initial delay is the jitterized INITIAL_SYNC_RETRY_TIME_IN_MS |
| 819 | newDelayInMs = jitterize(INITIAL_SYNC_RETRY_TIME_IN_MS, |
| 820 | (long)(INITIAL_SYNC_RETRY_TIME_IN_MS * 1.1)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | // Cap the delay |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 824 | long maxSyncRetryTimeInSeconds = Settings.Global.getLong(mContext.getContentResolver(), |
| 825 | Settings.Global.SYNC_MAX_RETRY_DELAY_IN_SECONDS, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | DEFAULT_MAX_SYNC_RETRY_TIME_IN_SECONDS); |
| 827 | if (newDelayInMs > maxSyncRetryTimeInSeconds * 1000) { |
| 828 | newDelayInMs = maxSyncRetryTimeInSeconds * 1000; |
| 829 | } |
| 830 | |
Alon Albert | c1ac776 | 2010-10-28 13:35:55 -0700 | [diff] [blame] | 831 | final long backoff = now + newDelayInMs; |
| 832 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 833 | mSyncStorageEngine.setBackoff(op.account, op.userId, op.authority, |
Alon Albert | c1ac776 | 2010-10-28 13:35:55 -0700 | [diff] [blame] | 834 | backoff, newDelayInMs); |
| 835 | |
| 836 | op.backoff = backoff; |
| 837 | op.updateEffectiveRunTime(); |
| 838 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 839 | synchronized (mSyncQueue) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 840 | mSyncQueue.onBackoffChanged(op.account, op.userId, op.authority, backoff); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 841 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | private void setDelayUntilTime(SyncOperation op, long delayUntilSeconds) { |
| 845 | final long delayUntil = delayUntilSeconds * 1000; |
| 846 | final long absoluteNow = System.currentTimeMillis(); |
| 847 | long newDelayUntilTime; |
| 848 | if (delayUntil > absoluteNow) { |
| 849 | newDelayUntilTime = SystemClock.elapsedRealtime() + (delayUntil - absoluteNow); |
| 850 | } else { |
| 851 | newDelayUntilTime = 0; |
| 852 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 853 | mSyncStorageEngine |
| 854 | .setDelayUntilTime(op.account, op.userId, op.authority, newDelayUntilTime); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 855 | synchronized (mSyncQueue) { |
| 856 | mSyncQueue.onDelayUntilTimeChanged(op.account, op.authority, newDelayUntilTime); |
| 857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 861 | * Cancel the active sync if it matches the authority and account. |
| 862 | * @param account limit the cancelations to syncs with this account, if non-null |
| 863 | * @param authority limit the cancelations to syncs with this authority, if non-null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 865 | public void cancelActiveSync(Account account, int userId, String authority) { |
| 866 | sendCancelSyncsMessage(account, userId, authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /** |
| 870 | * Create and schedule a SyncOperation. |
| 871 | * |
| 872 | * @param syncOperation the SyncOperation to schedule |
| 873 | */ |
| 874 | public void scheduleSyncOperation(SyncOperation syncOperation) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 875 | boolean queueChanged; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | synchronized (mSyncQueue) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 877 | queueChanged = mSyncQueue.add(syncOperation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 878 | } |
| 879 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 880 | if (queueChanged) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 882 | Log.v(TAG, "scheduleSyncOperation: enqueued " + syncOperation); |
| 883 | } |
| 884 | sendCheckAlarmsMessage(); |
| 885 | } else { |
| 886 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 887 | Log.v(TAG, "scheduleSyncOperation: dropping duplicate sync operation " |
| 888 | + syncOperation); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | /** |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 894 | * Remove scheduled sync operations. |
| 895 | * @param account limit the removals to operations with this account, if non-null |
| 896 | * @param authority limit the removals to operations with this authority, if non-null |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | */ |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 898 | public void clearScheduledSyncOperations(Account account, int userId, String authority) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | synchronized (mSyncQueue) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 900 | mSyncQueue.remove(account, userId, authority); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 902 | mSyncStorageEngine.setBackoff(account, userId, authority, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 903 | SyncStorageEngine.NOT_IN_BACKOFF_MODE, SyncStorageEngine.NOT_IN_BACKOFF_MODE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 904 | } |
| 905 | |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 906 | void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | boolean isLoggable = Log.isLoggable(TAG, Log.DEBUG); |
| 908 | if (isLoggable) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 909 | Log.d(TAG, "encountered error(s) during the sync: " + syncResult + ", " + operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | } |
| 911 | |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 912 | operation = new SyncOperation(operation); |
| 913 | |
| 914 | // The SYNC_EXTRAS_IGNORE_BACKOFF only applies to the first attempt to sync a given |
| 915 | // request. Retries of the request will always honor the backoff, so clear the |
| 916 | // flag in case we retry this request. |
| 917 | if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF, false)) { |
| 918 | operation.extras.remove(ContentResolver.SYNC_EXTRAS_IGNORE_BACKOFF); |
| 919 | } |
| 920 | |
Fred Quintana | aa7edda | 2009-12-03 14:18:58 -0800 | [diff] [blame] | 921 | // If this sync aborted because the internal sync loop retried too many times then |
| 922 | // don't reschedule. Otherwise we risk getting into a retry loop. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 923 | // If the operation succeeded to some extent then retry immediately. |
| 924 | // If this was a two-way sync then retry soft errors with an exponential backoff. |
| 925 | // If this was an upward sync then schedule a two-way sync immediately. |
| 926 | // Otherwise do not reschedule. |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 927 | if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_DO_NOT_RETRY, false)) { |
| 928 | Log.d(TAG, "not retrying sync operation because SYNC_EXTRAS_DO_NOT_RETRY was specified " |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 929 | + operation); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 930 | } else if (operation.extras.getBoolean(ContentResolver.SYNC_EXTRAS_UPLOAD, false) |
| 931 | && !syncResult.syncAlreadyInProgress) { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 932 | operation.extras.remove(ContentResolver.SYNC_EXTRAS_UPLOAD); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 933 | Log.d(TAG, "retrying sync operation as a two-way sync because an upload-only sync " |
| 934 | + "encountered an error: " + operation); |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 935 | scheduleSyncOperation(operation); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 936 | } else if (syncResult.tooManyRetries) { |
Fred Quintana | aa7edda | 2009-12-03 14:18:58 -0800 | [diff] [blame] | 937 | Log.d(TAG, "not retrying sync operation because it retried too many times: " |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 938 | + operation); |
Fred Quintana | aa7edda | 2009-12-03 14:18:58 -0800 | [diff] [blame] | 939 | } else if (syncResult.madeSomeProgress()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | if (isLoggable) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 941 | Log.d(TAG, "retrying sync operation because even though it had an error " |
| 942 | + "it achieved some success"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 943 | } |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 944 | scheduleSyncOperation(operation); |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 945 | } else if (syncResult.syncAlreadyInProgress) { |
| 946 | if (isLoggable) { |
| 947 | Log.d(TAG, "retrying sync operation that failed because there was already a " |
| 948 | + "sync in progress: " + operation); |
| 949 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 950 | scheduleSyncOperation(new SyncOperation(operation.account, operation.userId, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 951 | operation.reason, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 952 | operation.syncSource, |
Fred Quintana | 8570f74 | 2010-02-18 10:32:54 -0800 | [diff] [blame] | 953 | operation.authority, operation.extras, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 954 | DELAY_RETRY_SYNC_IN_PROGRESS_IN_SECONDS * 1000, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 955 | operation.backoff, operation.delayUntil, operation.allowParallelSyncs)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 956 | } else if (syncResult.hasSoftError()) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 957 | if (isLoggable) { |
| 958 | Log.d(TAG, "retrying sync operation because it encountered a soft error: " |
| 959 | + operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 961 | scheduleSyncOperation(operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | } else { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 963 | Log.d(TAG, "not retrying sync operation because the error is a hard error: " |
| 964 | + operation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 968 | private void onUserStarting(int userId) { |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 969 | // Make sure that accounts we're about to use are valid |
| 970 | AccountManagerService.getSingleton().validateAccounts(userId); |
| 971 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 972 | mSyncAdapters.invalidateCache(userId); |
| 973 | |
| 974 | updateRunningAccounts(); |
| 975 | |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 976 | synchronized (mSyncQueue) { |
| 977 | mSyncQueue.addPendingOperations(userId); |
| 978 | } |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 979 | |
| 980 | // Schedule sync for any accounts under started user |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 981 | final Account[] accounts = AccountManagerService.getSingleton().getAccounts(userId); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 982 | for (Account account : accounts) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 983 | scheduleSync(account, userId, SyncOperation.REASON_USER_START, null, null, |
| 984 | 0 /* no delay */, true /* onlyThoseWithUnknownSyncableState */); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | sendCheckAlarmsMessage(); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 988 | } |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 989 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 990 | private void onUserStopping(int userId) { |
| 991 | updateRunningAccounts(); |
| 992 | |
| 993 | cancelActiveSync( |
| 994 | null /* any account */, |
| 995 | userId, |
| 996 | null /* any authority */); |
| 997 | } |
| 998 | |
| 999 | private void onUserRemoved(int userId) { |
| 1000 | updateRunningAccounts(); |
| 1001 | |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1002 | // Clean up the storage engine database |
| 1003 | mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId); |
Amith Yamasani | 1359360 | 2012-03-22 16:16:17 -0700 | [diff] [blame] | 1004 | synchronized (mSyncQueue) { |
| 1005 | mSyncQueue.removeUser(userId); |
| 1006 | } |
| 1007 | } |
| 1008 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | /** |
| 1010 | * @hide |
| 1011 | */ |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 1012 | class ActiveSyncContext extends ISyncContext.Stub |
| 1013 | implements ServiceConnection, IBinder.DeathRecipient { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | final SyncOperation mSyncOperation; |
| 1015 | final long mHistoryRowId; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1016 | ISyncAdapter mSyncAdapter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | final long mStartTime; |
| 1018 | long mTimeoutStartTime; |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1019 | boolean mBound; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1020 | final PowerManager.WakeLock mSyncWakeLock; |
| 1021 | final int mSyncAdapterUid; |
| 1022 | SyncInfo mSyncInfo; |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 1023 | boolean mIsLinkedToDeath = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1024 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1025 | /** |
| 1026 | * Create an ActiveSyncContext for an impending sync and grab the wakelock for that |
| 1027 | * sync adapter. Since this grabs the wakelock you need to be sure to call |
| 1028 | * close() when you are done with this ActiveSyncContext, whether the sync succeeded |
| 1029 | * or not. |
| 1030 | * @param syncOperation the SyncOperation we are about to sync |
| 1031 | * @param historyRowId the row in which to record the history info for this sync |
| 1032 | * @param syncAdapterUid the UID of the application that contains the sync adapter |
| 1033 | * for this sync. This is used to attribute the wakelock hold to that application. |
| 1034 | */ |
| 1035 | public ActiveSyncContext(SyncOperation syncOperation, long historyRowId, |
| 1036 | int syncAdapterUid) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | super(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1038 | mSyncAdapterUid = syncAdapterUid; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | mSyncOperation = syncOperation; |
| 1040 | mHistoryRowId = historyRowId; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1041 | mSyncAdapter = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | mStartTime = SystemClock.elapsedRealtime(); |
| 1043 | mTimeoutStartTime = mStartTime; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1044 | mSyncWakeLock = mSyncHandler.getSyncWakeLock( |
Fred Quintana | fdb2dca | 2011-09-08 15:59:56 -0700 | [diff] [blame] | 1045 | mSyncOperation.account, mSyncOperation.authority); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1046 | mSyncWakeLock.setWorkSource(new WorkSource(syncAdapterUid)); |
| 1047 | mSyncWakeLock.acquire(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | public void sendHeartbeat() { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 1051 | // heartbeats are no longer used |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1052 | } |
| 1053 | |
| 1054 | public void onFinished(SyncResult result) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1055 | if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "onFinished: " + this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | // include "this" in the message so that the handler can ignore it if this |
| 1057 | // ActiveSyncContext is no longer the mActiveSyncContext at message handling |
| 1058 | // time |
| 1059 | sendSyncFinishedOrCanceledMessage(this, result); |
| 1060 | } |
| 1061 | |
| 1062 | public void toString(StringBuilder sb) { |
| 1063 | sb.append("startTime ").append(mStartTime) |
| 1064 | .append(", mTimeoutStartTime ").append(mTimeoutStartTime) |
| 1065 | .append(", mHistoryRowId ").append(mHistoryRowId) |
| 1066 | .append(", syncOperation ").append(mSyncOperation); |
| 1067 | } |
| 1068 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1069 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 1070 | Message msg = mSyncHandler.obtainMessage(); |
| 1071 | msg.what = SyncHandler.MESSAGE_SERVICE_CONNECTED; |
| 1072 | msg.obj = new ServiceConnectionData(this, ISyncAdapter.Stub.asInterface(service)); |
| 1073 | mSyncHandler.sendMessage(msg); |
| 1074 | } |
| 1075 | |
| 1076 | public void onServiceDisconnected(ComponentName name) { |
| 1077 | Message msg = mSyncHandler.obtainMessage(); |
| 1078 | msg.what = SyncHandler.MESSAGE_SERVICE_DISCONNECTED; |
| 1079 | msg.obj = new ServiceConnectionData(this, null); |
| 1080 | mSyncHandler.sendMessage(msg); |
| 1081 | } |
| 1082 | |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1083 | boolean bindToSyncAdapter(RegisteredServicesCache.ServiceInfo info, int userId) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1084 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1085 | Log.d(TAG, "bindToSyncAdapter: " + info.componentName + ", connection " + this); |
| 1086 | } |
| 1087 | Intent intent = new Intent(); |
| 1088 | intent.setAction("android.content.SyncAdapter"); |
| 1089 | intent.setComponent(info.componentName); |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 1090 | intent.putExtra(Intent.EXTRA_CLIENT_LABEL, |
| 1091 | com.android.internal.R.string.sync_binding_label); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1092 | intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivityAsUser( |
| 1093 | mContext, 0, new Intent(Settings.ACTION_SYNC_SETTINGS), 0, |
| 1094 | null, new UserHandle(userId))); |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1095 | mBound = true; |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1096 | final boolean bindResult = mContext.bindServiceAsUser(intent, this, |
Dianne Hackborn | e02c88a | 2011-10-28 13:58:15 -0700 | [diff] [blame] | 1097 | Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1098 | | Context.BIND_ALLOW_OOM_MANAGEMENT, |
Amith Yamasani | 27b89e6 | 2013-01-16 12:30:11 -0800 | [diff] [blame] | 1099 | new UserHandle(mSyncOperation.userId)); |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1100 | if (!bindResult) { |
| 1101 | mBound = false; |
| 1102 | } |
| 1103 | return bindResult; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1106 | /** |
| 1107 | * Performs the required cleanup, which is the releasing of the wakelock and |
| 1108 | * unbinding from the sync adapter (if actually bound). |
| 1109 | */ |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1110 | protected void close() { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1111 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1112 | Log.d(TAG, "unBindFromSyncAdapter: connection " + this); |
| 1113 | } |
Fred Quintana | 3ec4730 | 2010-03-10 10:08:31 -0800 | [diff] [blame] | 1114 | if (mBound) { |
| 1115 | mBound = false; |
| 1116 | mContext.unbindService(this); |
| 1117 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1118 | mSyncWakeLock.release(); |
Dianne Hackborn | c24ab86 | 2011-10-18 15:55:03 -0700 | [diff] [blame] | 1119 | mSyncWakeLock.setWorkSource(null); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | @Override |
| 1123 | public String toString() { |
| 1124 | StringBuilder sb = new StringBuilder(); |
| 1125 | toString(sb); |
| 1126 | return sb.toString(); |
| 1127 | } |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 1128 | |
| 1129 | @Override |
| 1130 | public void binderDied() { |
| 1131 | sendSyncFinishedOrCanceledMessage(this, null); |
| 1132 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | protected void dump(FileDescriptor fd, PrintWriter pw) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1136 | final IndentingPrintWriter ipw = new IndentingPrintWriter(pw, " "); |
| 1137 | dumpSyncState(ipw); |
| 1138 | dumpSyncHistory(ipw); |
| 1139 | dumpSyncAdapters(ipw); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1142 | static String formatTime(long time) { |
| 1143 | Time tobj = new Time(); |
| 1144 | tobj.set(time); |
| 1145 | return tobj.format("%Y-%m-%d %H:%M:%S"); |
| 1146 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1147 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1148 | protected void dumpSyncState(PrintWriter pw) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1149 | pw.print("data connected: "); pw.println(mDataConnectionIsConnected); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1150 | pw.print("auto sync: "); |
| 1151 | List<UserInfo> users = getAllUsers(); |
| 1152 | if (users != null) { |
| 1153 | for (UserInfo user : users) { |
| 1154 | pw.print("u" + user.id + "=" |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1155 | + mSyncStorageEngine.getMasterSyncAutomatically(user.id) + " "); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1156 | } |
| 1157 | pw.println(); |
| 1158 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1159 | pw.print("memory low: "); pw.println(mStorageIsLow); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1161 | final AccountAndUser[] accounts = AccountManagerService.getSingleton().getAllAccounts(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1162 | |
Jeff Sharkey | 6eb9620 | 2012-10-10 13:13:54 -0700 | [diff] [blame] | 1163 | pw.print("accounts: "); |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1164 | if (accounts != INITIAL_ACCOUNTS_ARRAY) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1165 | pw.println(accounts.length); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | } else { |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 1167 | pw.println("not known yet"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1168 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1169 | final long now = SystemClock.elapsedRealtime(); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1170 | pw.print("now: "); pw.print(now); |
| 1171 | pw.println(" (" + formatTime(System.currentTimeMillis()) + ")"); |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 1172 | pw.print("offset: "); pw.print(DateUtils.formatElapsedTime(mSyncRandomOffsetMillis/1000)); |
| 1173 | pw.println(" (HH:MM:SS)"); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1174 | pw.print("uptime: "); pw.print(DateUtils.formatElapsedTime(now/1000)); |
| 1175 | pw.println(" (HH:MM:SS)"); |
| 1176 | pw.print("time spent syncing: "); |
| 1177 | pw.print(DateUtils.formatElapsedTime( |
| 1178 | mSyncHandler.mSyncTimeTracker.timeSpentSyncing() / 1000)); |
| 1179 | pw.print(" (HH:MM:SS), sync "); |
| 1180 | pw.print(mSyncHandler.mSyncTimeTracker.mLastWasSyncing ? "" : "not "); |
| 1181 | pw.println("in progress"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | if (mSyncHandler.mAlarmScheduleTime != null) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1183 | pw.print("next alarm time: "); pw.print(mSyncHandler.mAlarmScheduleTime); |
| 1184 | pw.print(" ("); |
| 1185 | pw.print(DateUtils.formatElapsedTime((mSyncHandler.mAlarmScheduleTime-now)/1000)); |
| 1186 | pw.println(" (HH:MM:SS) from now)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } else { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1188 | pw.println("no alarm is scheduled (there had better not be any pending syncs)"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1189 | } |
| 1190 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1191 | pw.print("notification info: "); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1192 | final StringBuilder sb = new StringBuilder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | mSyncHandler.mSyncNotificationInfo.toString(sb); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1194 | pw.println(sb.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1195 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1196 | pw.println(); |
| 1197 | pw.println("Active Syncs: " + mActiveSyncContexts.size()); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1198 | final PackageManager pm = mContext.getPackageManager(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1199 | for (SyncManager.ActiveSyncContext activeSyncContext : mActiveSyncContexts) { |
| 1200 | final long durationInSeconds = (now - activeSyncContext.mStartTime) / 1000; |
| 1201 | pw.print(" "); |
| 1202 | pw.print(DateUtils.formatElapsedTime(durationInSeconds)); |
| 1203 | pw.print(" - "); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1204 | pw.print(activeSyncContext.mSyncOperation.dump(pm, false)); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1205 | pw.println(); |
| 1206 | } |
| 1207 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | synchronized (mSyncQueue) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1209 | sb.setLength(0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | mSyncQueue.dump(sb); |
| 1211 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1212 | pw.println(); |
| 1213 | pw.print(sb.toString()); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1214 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1215 | // join the installed sync adapter with the accounts list and emit for everything |
| 1216 | pw.println(); |
| 1217 | pw.println("Sync Status"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1218 | for (AccountAndUser account : accounts) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1219 | pw.printf("Account %s u%d %s\n", |
| 1220 | account.account.name, account.userId, account.account.type); |
| 1221 | |
| 1222 | pw.println("======================================================================="); |
| 1223 | final PrintTable table = new PrintTable(13); |
| 1224 | table.set(0, 0, |
| 1225 | "Authority", // 0 |
| 1226 | "Syncable", // 1 |
| 1227 | "Enabled", // 2 |
| 1228 | "Delay", // 3 |
| 1229 | "Loc", // 4 |
| 1230 | "Poll", // 5 |
| 1231 | "Per", // 6 |
| 1232 | "Serv", // 7 |
| 1233 | "User", // 8 |
| 1234 | "Tot", // 9 |
| 1235 | "Time", // 10 |
| 1236 | "Last Sync", // 11 |
| 1237 | "Periodic" // 12 |
| 1238 | ); |
| 1239 | |
| 1240 | final List<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> sorted = |
| 1241 | Lists.newArrayList(); |
| 1242 | sorted.addAll(mSyncAdapters.getAllServices(account.userId)); |
| 1243 | Collections.sort(sorted, |
| 1244 | new Comparator<RegisteredServicesCache.ServiceInfo<SyncAdapterType>>() { |
| 1245 | @Override |
| 1246 | public int compare(RegisteredServicesCache.ServiceInfo<SyncAdapterType> lhs, |
| 1247 | RegisteredServicesCache.ServiceInfo<SyncAdapterType> rhs) { |
| 1248 | return lhs.type.authority.compareTo(rhs.type.authority); |
| 1249 | } |
| 1250 | }); |
| 1251 | for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterType : sorted) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1252 | if (!syncAdapterType.type.accountType.equals(account.account.type)) { |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1253 | continue; |
| 1254 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1255 | int row = table.getNumRows(); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1256 | SyncStorageEngine.AuthorityInfo settings = |
| 1257 | mSyncStorageEngine.getOrCreateAuthority( |
| 1258 | account.account, account.userId, syncAdapterType.type.authority); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1259 | SyncStatusInfo status = mSyncStorageEngine.getOrCreateSyncStatus(settings); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1260 | |
| 1261 | String authority = settings.authority; |
| 1262 | if (authority.length() > 50) { |
| 1263 | authority = authority.substring(authority.length() - 50); |
| 1264 | } |
| 1265 | table.set(row, 0, authority, settings.syncable, settings.enabled); |
| 1266 | table.set(row, 4, |
| 1267 | status.numSourceLocal, |
| 1268 | status.numSourcePoll, |
| 1269 | status.numSourcePeriodic, |
| 1270 | status.numSourceServer, |
| 1271 | status.numSourceUser, |
| 1272 | status.numSyncs, |
| 1273 | DateUtils.formatElapsedTime(status.totalElapsedTime / 1000)); |
| 1274 | |
| 1275 | |
| 1276 | for (int i = 0; i < settings.periodicSyncs.size(); i++) { |
| 1277 | final Pair<Bundle, Long> pair = settings.periodicSyncs.get(0); |
| 1278 | final String period = String.valueOf(pair.second); |
| 1279 | final String extras = pair.first.size() > 0 ? pair.first.toString() : ""; |
| 1280 | final String next = formatTime(status.getPeriodicSyncTime(0) |
| 1281 | + pair.second * 1000); |
| 1282 | table.set(row + i * 2, 12, period + extras); |
| 1283 | table.set(row + i * 2 + 1, 12, next); |
| 1284 | } |
| 1285 | |
| 1286 | int row1 = row; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1287 | if (settings.delayUntil > now) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1288 | table.set(row1++, 12, "D: " + (settings.delayUntil - now) / 1000); |
| 1289 | if (settings.backoffTime > now) { |
| 1290 | table.set(row1++, 12, "B: " + (settings.backoffTime - now) / 1000); |
| 1291 | table.set(row1++, 12, settings.backoffDelay / 1000); |
| 1292 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1293 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1294 | |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1295 | if (status.lastSuccessTime != 0) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1296 | table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastSuccessSource] |
| 1297 | + " " + "SUCCESS"); |
| 1298 | table.set(row1++, 11, formatTime(status.lastSuccessTime)); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1299 | } |
| 1300 | if (status.lastFailureTime != 0) { |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1301 | table.set(row1++, 11, SyncStorageEngine.SOURCES[status.lastFailureSource] |
| 1302 | + " " + "FAILURE"); |
| 1303 | table.set(row1++, 11, formatTime(status.lastFailureTime)); |
| 1304 | //noinspection UnusedAssignment |
| 1305 | table.set(row1++, 11, status.lastFailureMesg); |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1308 | table.writeTo(pw); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
Fabrice Di Meglio | dc2dd88 | 2011-01-26 14:31:48 -0800 | [diff] [blame] | 1312 | private String getLastFailureMessage(int code) { |
| 1313 | switch (code) { |
| 1314 | case ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS: |
| 1315 | return "sync already in progress"; |
| 1316 | |
| 1317 | case ContentResolver.SYNC_ERROR_AUTHENTICATION: |
| 1318 | return "authentication error"; |
| 1319 | |
| 1320 | case ContentResolver.SYNC_ERROR_IO: |
| 1321 | return "I/O error"; |
| 1322 | |
| 1323 | case ContentResolver.SYNC_ERROR_PARSE: |
| 1324 | return "parse error"; |
| 1325 | |
| 1326 | case ContentResolver.SYNC_ERROR_CONFLICT: |
| 1327 | return "conflict error"; |
| 1328 | |
| 1329 | case ContentResolver.SYNC_ERROR_TOO_MANY_DELETIONS: |
| 1330 | return "too many deletions error"; |
| 1331 | |
| 1332 | case ContentResolver.SYNC_ERROR_TOO_MANY_RETRIES: |
| 1333 | return "too many retries error"; |
| 1334 | |
| 1335 | case ContentResolver.SYNC_ERROR_INTERNAL: |
| 1336 | return "internal error"; |
| 1337 | |
| 1338 | default: |
| 1339 | return "unknown"; |
| 1340 | } |
| 1341 | } |
| 1342 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1343 | private void dumpTimeSec(PrintWriter pw, long time) { |
| 1344 | pw.print(time/1000); pw.print('.'); pw.print((time/100)%10); |
| 1345 | pw.print('s'); |
| 1346 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1347 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1348 | private void dumpDayStatistic(PrintWriter pw, SyncStorageEngine.DayStats ds) { |
| 1349 | pw.print("Success ("); pw.print(ds.successCount); |
| 1350 | if (ds.successCount > 0) { |
| 1351 | pw.print(" for "); dumpTimeSec(pw, ds.successTime); |
| 1352 | pw.print(" avg="); dumpTimeSec(pw, ds.successTime/ds.successCount); |
| 1353 | } |
| 1354 | pw.print(") Failure ("); pw.print(ds.failureCount); |
| 1355 | if (ds.failureCount > 0) { |
| 1356 | pw.print(" for "); dumpTimeSec(pw, ds.failureTime); |
| 1357 | pw.print(" avg="); dumpTimeSec(pw, ds.failureTime/ds.failureCount); |
| 1358 | } |
| 1359 | pw.println(")"); |
| 1360 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1361 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1362 | protected void dumpSyncHistory(PrintWriter pw) { |
| 1363 | dumpRecentHistory(pw); |
| 1364 | dumpDayStatistics(pw); |
| 1365 | } |
| 1366 | |
| 1367 | private void dumpRecentHistory(PrintWriter pw) { |
| 1368 | final ArrayList<SyncStorageEngine.SyncHistoryItem> items |
| 1369 | = mSyncStorageEngine.getSyncHistory(); |
| 1370 | if (items != null && items.size() > 0) { |
| 1371 | final Map<String, AuthoritySyncStats> authorityMap = Maps.newHashMap(); |
| 1372 | long totalElapsedTime = 0; |
| 1373 | long totalTimes = 0; |
| 1374 | final int N = items.size(); |
| 1375 | |
| 1376 | int maxAuthority = 0; |
| 1377 | int maxAccount = 0; |
| 1378 | for (SyncStorageEngine.SyncHistoryItem item : items) { |
| 1379 | SyncStorageEngine.AuthorityInfo authority |
| 1380 | = mSyncStorageEngine.getAuthority(item.authorityId); |
| 1381 | final String authorityName; |
| 1382 | final String accountKey; |
| 1383 | if (authority != null) { |
| 1384 | authorityName = authority.authority; |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 1385 | accountKey = authority.account.name + "/" + authority.account.type |
| 1386 | + " u" + authority.userId; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1387 | } else { |
| 1388 | authorityName = "Unknown"; |
| 1389 | accountKey = "Unknown"; |
| 1390 | } |
| 1391 | |
| 1392 | int length = authorityName.length(); |
| 1393 | if (length > maxAuthority) { |
| 1394 | maxAuthority = length; |
| 1395 | } |
| 1396 | length = accountKey.length(); |
| 1397 | if (length > maxAccount) { |
| 1398 | maxAccount = length; |
| 1399 | } |
| 1400 | |
| 1401 | final long elapsedTime = item.elapsedTime; |
| 1402 | totalElapsedTime += elapsedTime; |
| 1403 | totalTimes++; |
| 1404 | AuthoritySyncStats authoritySyncStats = authorityMap.get(authorityName); |
| 1405 | if (authoritySyncStats == null) { |
| 1406 | authoritySyncStats = new AuthoritySyncStats(authorityName); |
| 1407 | authorityMap.put(authorityName, authoritySyncStats); |
| 1408 | } |
| 1409 | authoritySyncStats.elapsedTime += elapsedTime; |
| 1410 | authoritySyncStats.times++; |
| 1411 | final Map<String, AccountSyncStats> accountMap = authoritySyncStats.accountMap; |
| 1412 | AccountSyncStats accountSyncStats = accountMap.get(accountKey); |
| 1413 | if (accountSyncStats == null) { |
| 1414 | accountSyncStats = new AccountSyncStats(accountKey); |
| 1415 | accountMap.put(accountKey, accountSyncStats); |
| 1416 | } |
| 1417 | accountSyncStats.elapsedTime += elapsedTime; |
| 1418 | accountSyncStats.times++; |
| 1419 | |
| 1420 | } |
| 1421 | |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 1422 | if (totalElapsedTime > 0) { |
| 1423 | pw.println(); |
| 1424 | pw.printf("Detailed Statistics (Recent history): " |
| 1425 | + "%d (# of times) %ds (sync time)\n", |
| 1426 | totalTimes, totalElapsedTime / 1000); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1427 | |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 1428 | final List<AuthoritySyncStats> sortedAuthorities = |
| 1429 | new ArrayList<AuthoritySyncStats>(authorityMap.values()); |
| 1430 | Collections.sort(sortedAuthorities, new Comparator<AuthoritySyncStats>() { |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1431 | @Override |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 1432 | public int compare(AuthoritySyncStats lhs, AuthoritySyncStats rhs) { |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1433 | // reverse order |
| 1434 | int compare = Integer.compare(rhs.times, lhs.times); |
| 1435 | if (compare == 0) { |
| 1436 | compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1437 | } |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1438 | return compare; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1439 | } |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1440 | }); |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 1441 | |
| 1442 | final int maxLength = Math.max(maxAuthority, maxAccount + 3); |
| 1443 | final int padLength = 2 + 2 + maxLength + 2 + 10 + 11; |
| 1444 | final char chars[] = new char[padLength]; |
| 1445 | Arrays.fill(chars, '-'); |
| 1446 | final String separator = new String(chars); |
| 1447 | |
| 1448 | final String authorityFormat = |
| 1449 | String.format(" %%-%ds: %%-9s %%-11s\n", maxLength + 2); |
| 1450 | final String accountFormat = |
| 1451 | String.format(" %%-%ds: %%-9s %%-11s\n", maxLength); |
| 1452 | |
| 1453 | pw.println(separator); |
| 1454 | for (AuthoritySyncStats authoritySyncStats : sortedAuthorities) { |
| 1455 | String name = authoritySyncStats.name; |
| 1456 | long elapsedTime; |
| 1457 | int times; |
| 1458 | String timeStr; |
| 1459 | String timesStr; |
| 1460 | |
| 1461 | elapsedTime = authoritySyncStats.elapsedTime; |
| 1462 | times = authoritySyncStats.times; |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1463 | timeStr = String.format("%ds/%d%%", |
| 1464 | elapsedTime / 1000, |
| 1465 | elapsedTime * 100 / totalElapsedTime); |
| 1466 | timesStr = String.format("%d/%d%%", |
| 1467 | times, |
| 1468 | times * 100 / totalTimes); |
Alon Albert | 2709682 | 2012-01-11 18:06:41 -0800 | [diff] [blame] | 1469 | pw.printf(authorityFormat, name, timesStr, timeStr); |
| 1470 | |
| 1471 | final List<AccountSyncStats> sortedAccounts = |
| 1472 | new ArrayList<AccountSyncStats>( |
| 1473 | authoritySyncStats.accountMap.values()); |
| 1474 | Collections.sort(sortedAccounts, new Comparator<AccountSyncStats>() { |
| 1475 | @Override |
| 1476 | public int compare(AccountSyncStats lhs, AccountSyncStats rhs) { |
| 1477 | // reverse order |
| 1478 | int compare = Integer.compare(rhs.times, lhs.times); |
| 1479 | if (compare == 0) { |
| 1480 | compare = Long.compare(rhs.elapsedTime, lhs.elapsedTime); |
| 1481 | } |
| 1482 | return compare; |
| 1483 | } |
| 1484 | }); |
| 1485 | for (AccountSyncStats stats: sortedAccounts) { |
| 1486 | elapsedTime = stats.elapsedTime; |
| 1487 | times = stats.times; |
| 1488 | timeStr = String.format("%ds/%d%%", |
| 1489 | elapsedTime / 1000, |
| 1490 | elapsedTime * 100 / totalElapsedTime); |
| 1491 | timesStr = String.format("%d/%d%%", |
| 1492 | times, |
| 1493 | times * 100 / totalTimes); |
| 1494 | pw.printf(accountFormat, stats.name, timesStr, timeStr); |
| 1495 | } |
| 1496 | pw.println(separator); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1497 | } |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1498 | } |
| 1499 | |
| 1500 | pw.println(); |
| 1501 | pw.println("Recent Sync History"); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1502 | final String format = " %-" + maxAccount + "s %-" + maxAuthority + "s %s\n"; |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1503 | final Map<String, Long> lastTimeMap = Maps.newHashMap(); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1504 | final PackageManager pm = mContext.getPackageManager(); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1505 | for (int i = 0; i < N; i++) { |
| 1506 | SyncStorageEngine.SyncHistoryItem item = items.get(i); |
| 1507 | SyncStorageEngine.AuthorityInfo authority |
| 1508 | = mSyncStorageEngine.getAuthority(item.authorityId); |
| 1509 | final String authorityName; |
| 1510 | final String accountKey; |
| 1511 | if (authority != null) { |
| 1512 | authorityName = authority.authority; |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 1513 | accountKey = authority.account.name + "/" + authority.account.type |
| 1514 | + " u" + authority.userId; |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1515 | } else { |
| 1516 | authorityName = "Unknown"; |
| 1517 | accountKey = "Unknown"; |
| 1518 | } |
| 1519 | final long elapsedTime = item.elapsedTime; |
| 1520 | final Time time = new Time(); |
| 1521 | final long eventTime = item.eventTime; |
| 1522 | time.set(eventTime); |
| 1523 | |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1524 | final String key = authorityName + "/" + accountKey; |
| 1525 | final Long lastEventTime = lastTimeMap.get(key); |
| 1526 | final String diffString; |
| 1527 | if (lastEventTime == null) { |
| 1528 | diffString = ""; |
| 1529 | } else { |
| 1530 | final long diff = (lastEventTime - eventTime) / 1000; |
| 1531 | if (diff < 60) { |
| 1532 | diffString = String.valueOf(diff); |
| 1533 | } else if (diff < 3600) { |
| 1534 | diffString = String.format("%02d:%02d", diff / 60, diff % 60); |
| 1535 | } else { |
| 1536 | final long sec = diff % 3600; |
| 1537 | diffString = String.format("%02d:%02d:%02d", |
| 1538 | diff / 3600, sec / 60, sec % 60); |
| 1539 | } |
| 1540 | } |
| 1541 | lastTimeMap.put(key, eventTime); |
| 1542 | |
| 1543 | pw.printf(" #%-3d: %s %8s %5.1fs %8s", |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1544 | i + 1, |
| 1545 | formatTime(eventTime), |
| 1546 | SyncStorageEngine.SOURCES[item.source], |
Alon Albert | bf976ba | 2011-10-03 13:06:43 -0700 | [diff] [blame] | 1547 | ((float) elapsedTime) / 1000, |
| 1548 | diffString); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1549 | pw.printf(format, accountKey, authorityName, |
| 1550 | SyncOperation.reasonToString(pm, item.reason)); |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1551 | |
| 1552 | if (item.event != SyncStorageEngine.EVENT_STOP |
| 1553 | || item.upstreamActivity != 0 |
| 1554 | || item.downstreamActivity != 0) { |
| 1555 | pw.printf(" event=%d upstreamActivity=%d downstreamActivity=%d\n", |
| 1556 | item.event, |
| 1557 | item.upstreamActivity, |
| 1558 | item.downstreamActivity); |
| 1559 | } |
| 1560 | if (item.mesg != null |
| 1561 | && !SyncStorageEngine.MESG_SUCCESS.equals(item.mesg)) { |
| 1562 | pw.printf(" mesg=%s\n", item.mesg); |
| 1563 | } |
| 1564 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 1565 | pw.println(); |
| 1566 | pw.println("Recent Sync History Extras"); |
| 1567 | for (int i = 0; i < N; i++) { |
| 1568 | final SyncStorageEngine.SyncHistoryItem item = items.get(i); |
| 1569 | final Bundle extras = item.extras; |
| 1570 | if (extras == null || extras.size() == 0) { |
| 1571 | continue; |
| 1572 | } |
| 1573 | final SyncStorageEngine.AuthorityInfo authority |
| 1574 | = mSyncStorageEngine.getAuthority(item.authorityId); |
| 1575 | final String authorityName; |
| 1576 | final String accountKey; |
| 1577 | if (authority != null) { |
| 1578 | authorityName = authority.authority; |
| 1579 | accountKey = authority.account.name + "/" + authority.account.type |
| 1580 | + " u" + authority.userId; |
| 1581 | } else { |
| 1582 | authorityName = "Unknown"; |
| 1583 | accountKey = "Unknown"; |
| 1584 | } |
| 1585 | final Time time = new Time(); |
| 1586 | final long eventTime = item.eventTime; |
| 1587 | time.set(eventTime); |
| 1588 | |
| 1589 | pw.printf(" #%-3d: %s %8s ", |
| 1590 | i + 1, |
| 1591 | formatTime(eventTime), |
| 1592 | SyncStorageEngine.SOURCES[item.source]); |
| 1593 | |
| 1594 | pw.printf(format, accountKey, authorityName, extras); |
| 1595 | } |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | private void dumpDayStatistics(PrintWriter pw) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1600 | SyncStorageEngine.DayStats dses[] = mSyncStorageEngine.getDayStatistics(); |
| 1601 | if (dses != null && dses[0] != null) { |
| 1602 | pw.println(); |
| 1603 | pw.println("Sync Statistics"); |
| 1604 | pw.print(" Today: "); dumpDayStatistic(pw, dses[0]); |
| 1605 | int today = dses[0].day; |
| 1606 | int i; |
| 1607 | SyncStorageEngine.DayStats ds; |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1608 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1609 | // Print each day in the current week. |
| 1610 | for (i=1; i<=6 && i < dses.length; i++) { |
| 1611 | ds = dses[i]; |
| 1612 | if (ds == null) break; |
| 1613 | int delta = today-ds.day; |
| 1614 | if (delta > 6) break; |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1615 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1616 | pw.print(" Day-"); pw.print(delta); pw.print(": "); |
| 1617 | dumpDayStatistic(pw, ds); |
| 1618 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1619 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1620 | // Aggregate all following days into weeks and print totals. |
| 1621 | int weekDay = today; |
| 1622 | while (i < dses.length) { |
| 1623 | SyncStorageEngine.DayStats aggr = null; |
| 1624 | weekDay -= 7; |
| 1625 | while (i < dses.length) { |
| 1626 | ds = dses[i]; |
| 1627 | if (ds == null) { |
| 1628 | i = dses.length; |
| 1629 | break; |
| 1630 | } |
| 1631 | int delta = weekDay-ds.day; |
| 1632 | if (delta > 6) break; |
| 1633 | i++; |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1634 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1635 | if (aggr == null) { |
| 1636 | aggr = new SyncStorageEngine.DayStats(weekDay); |
| 1637 | } |
| 1638 | aggr.successCount += ds.successCount; |
| 1639 | aggr.successTime += ds.successTime; |
| 1640 | aggr.failureCount += ds.failureCount; |
| 1641 | aggr.failureTime += ds.failureTime; |
| 1642 | } |
| 1643 | if (aggr != null) { |
| 1644 | pw.print(" Week-"); pw.print((today-weekDay)/7); pw.print(": "); |
| 1645 | dumpDayStatistic(pw, aggr); |
| 1646 | } |
| 1647 | } |
| 1648 | } |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1649 | } |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 1650 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1651 | private void dumpSyncAdapters(IndentingPrintWriter pw) { |
| 1652 | pw.println(); |
| 1653 | final List<UserInfo> users = getAllUsers(); |
| 1654 | if (users != null) { |
| 1655 | for (UserInfo user : users) { |
| 1656 | pw.println("Sync adapters for " + user + ":"); |
| 1657 | pw.increaseIndent(); |
| 1658 | for (RegisteredServicesCache.ServiceInfo<?> info : |
| 1659 | mSyncAdapters.getAllServices(user.id)) { |
| 1660 | pw.println(info); |
| 1661 | } |
| 1662 | pw.decreaseIndent(); |
| 1663 | pw.println(); |
| 1664 | } |
| 1665 | } |
| 1666 | } |
| 1667 | |
Alon Albert | e0bde33 | 2011-09-22 14:26:16 -0700 | [diff] [blame] | 1668 | private static class AuthoritySyncStats { |
| 1669 | String name; |
| 1670 | long elapsedTime; |
| 1671 | int times; |
| 1672 | Map<String, AccountSyncStats> accountMap = Maps.newHashMap(); |
| 1673 | |
| 1674 | private AuthoritySyncStats(String name) { |
| 1675 | this.name = name; |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | private static class AccountSyncStats { |
| 1680 | String name; |
| 1681 | long elapsedTime; |
| 1682 | int times; |
| 1683 | |
| 1684 | private AccountSyncStats(String name) { |
| 1685 | this.name = name; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 1686 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * A helper object to keep track of the time we have spent syncing since the last boot |
| 1691 | */ |
| 1692 | private class SyncTimeTracker { |
| 1693 | /** True if a sync was in progress on the most recent call to update() */ |
| 1694 | boolean mLastWasSyncing = false; |
| 1695 | /** Used to track when lastWasSyncing was last set */ |
| 1696 | long mWhenSyncStarted = 0; |
| 1697 | /** The cumulative time we have spent syncing */ |
| 1698 | private long mTimeSpentSyncing; |
| 1699 | |
| 1700 | /** Call to let the tracker know that the sync state may have changed */ |
| 1701 | public synchronized void update() { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1702 | final boolean isSyncInProgress = !mActiveSyncContexts.isEmpty(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | if (isSyncInProgress == mLastWasSyncing) return; |
| 1704 | final long now = SystemClock.elapsedRealtime(); |
| 1705 | if (isSyncInProgress) { |
| 1706 | mWhenSyncStarted = now; |
| 1707 | } else { |
| 1708 | mTimeSpentSyncing += now - mWhenSyncStarted; |
| 1709 | } |
| 1710 | mLastWasSyncing = isSyncInProgress; |
| 1711 | } |
| 1712 | |
| 1713 | /** Get how long we have been syncing, in ms */ |
| 1714 | public synchronized long timeSpentSyncing() { |
| 1715 | if (!mLastWasSyncing) return mTimeSpentSyncing; |
| 1716 | |
| 1717 | final long now = SystemClock.elapsedRealtime(); |
| 1718 | return mTimeSpentSyncing + (now - mWhenSyncStarted); |
| 1719 | } |
| 1720 | } |
| 1721 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1722 | class ServiceConnectionData { |
| 1723 | public final ActiveSyncContext activeSyncContext; |
| 1724 | public final ISyncAdapter syncAdapter; |
| 1725 | ServiceConnectionData(ActiveSyncContext activeSyncContext, ISyncAdapter syncAdapter) { |
| 1726 | this.activeSyncContext = activeSyncContext; |
| 1727 | this.syncAdapter = syncAdapter; |
| 1728 | } |
| 1729 | } |
| 1730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1731 | /** |
| 1732 | * Handles SyncOperation Messages that are posted to the associated |
| 1733 | * HandlerThread. |
| 1734 | */ |
| 1735 | class SyncHandler extends Handler { |
| 1736 | // Messages that can be sent on mHandler |
| 1737 | private static final int MESSAGE_SYNC_FINISHED = 1; |
| 1738 | private static final int MESSAGE_SYNC_ALARM = 2; |
| 1739 | private static final int MESSAGE_CHECK_ALARMS = 3; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1740 | private static final int MESSAGE_SERVICE_CONNECTED = 4; |
| 1741 | private static final int MESSAGE_SERVICE_DISCONNECTED = 5; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1742 | private static final int MESSAGE_CANCEL = 6; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1743 | |
| 1744 | public final SyncNotificationInfo mSyncNotificationInfo = new SyncNotificationInfo(); |
| 1745 | private Long mAlarmScheduleTime = null; |
| 1746 | public final SyncTimeTracker mSyncTimeTracker = new SyncTimeTracker(); |
Fred Quintana | fdb2dca | 2011-09-08 15:59:56 -0700 | [diff] [blame] | 1747 | private final HashMap<Pair<Account, String>, PowerManager.WakeLock> mWakeLocks = |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 1748 | Maps.newHashMap(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1749 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 1750 | private volatile CountDownLatch mReadyToRunLatch = new CountDownLatch(1); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1751 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 1752 | public void onBootCompleted() { |
| 1753 | mBootCompleted = true; |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1754 | |
Jeff Sharkey | 8f55d11 | 2012-10-11 11:00:21 -0700 | [diff] [blame] | 1755 | doDatabaseCleanup(); |
| 1756 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 1757 | if (mReadyToRunLatch != null) { |
| 1758 | mReadyToRunLatch.countDown(); |
| 1759 | } |
| 1760 | } |
| 1761 | |
Fred Quintana | fdb2dca | 2011-09-08 15:59:56 -0700 | [diff] [blame] | 1762 | private PowerManager.WakeLock getSyncWakeLock(Account account, String authority) { |
| 1763 | final Pair<Account, String> wakeLockKey = Pair.create(account, authority); |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 1764 | PowerManager.WakeLock wakeLock = mWakeLocks.get(wakeLockKey); |
| 1765 | if (wakeLock == null) { |
Fred Quintana | fdb2dca | 2011-09-08 15:59:56 -0700 | [diff] [blame] | 1766 | final String name = SYNC_WAKE_LOCK_PREFIX + "_" + authority + "_" + account; |
Fred Quintana | b3029c3 | 2010-04-06 13:27:12 -0700 | [diff] [blame] | 1767 | wakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, name); |
| 1768 | wakeLock.setReferenceCounted(false); |
| 1769 | mWakeLocks.put(wakeLockKey, wakeLock); |
| 1770 | } |
| 1771 | return wakeLock; |
| 1772 | } |
| 1773 | |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 1774 | private void waitUntilReadyToRun() { |
| 1775 | CountDownLatch latch = mReadyToRunLatch; |
| 1776 | if (latch != null) { |
| 1777 | while (true) { |
| 1778 | try { |
| 1779 | latch.await(); |
| 1780 | mReadyToRunLatch = null; |
| 1781 | return; |
| 1782 | } catch (InterruptedException e) { |
| 1783 | Thread.currentThread().interrupt(); |
| 1784 | } |
| 1785 | } |
| 1786 | } |
| 1787 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1788 | /** |
| 1789 | * Used to keep track of whether a sync notification is active and who it is for. |
| 1790 | */ |
| 1791 | class SyncNotificationInfo { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1792 | // true iff the notification manager has been asked to send the notification |
| 1793 | public boolean isActive = false; |
| 1794 | |
| 1795 | // Set when we transition from not running a sync to running a sync, and cleared on |
| 1796 | // the opposite transition. |
| 1797 | public Long startTime = null; |
| 1798 | |
| 1799 | public void toString(StringBuilder sb) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1800 | sb.append("isActive ").append(isActive).append(", startTime ").append(startTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1801 | } |
| 1802 | |
| 1803 | @Override |
| 1804 | public String toString() { |
| 1805 | StringBuilder sb = new StringBuilder(); |
| 1806 | toString(sb); |
| 1807 | return sb.toString(); |
| 1808 | } |
| 1809 | } |
| 1810 | |
| 1811 | public SyncHandler(Looper looper) { |
| 1812 | super(looper); |
| 1813 | } |
| 1814 | |
| 1815 | public void handleMessage(Message msg) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1816 | long earliestFuturePollTime = Long.MAX_VALUE; |
| 1817 | long nextPendingSyncTime = Long.MAX_VALUE; |
Subir Jhanb | 753c57b | 2011-01-10 12:09:26 -0800 | [diff] [blame] | 1818 | |
| 1819 | // Setting the value here instead of a method because we want the dumpsys logs |
| 1820 | // to have the most recent value used. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1821 | try { |
Fred Quintana | e91ebe2 | 2009-09-29 20:44:30 -0700 | [diff] [blame] | 1822 | waitUntilReadyToRun(); |
Alon Albert | 1bad83a | 2011-02-16 10:29:56 -0800 | [diff] [blame] | 1823 | mDataConnectionIsConnected = readDataConnectionState(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1824 | mSyncManagerWakeLock.acquire(); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1825 | // Always do this first so that we be sure that any periodic syncs that |
| 1826 | // are ready to run have been converted into pending syncs. This allows the |
| 1827 | // logic that considers the next steps to take based on the set of pending syncs |
| 1828 | // to also take into account the periodic syncs. |
| 1829 | earliestFuturePollTime = scheduleReadyPeriodicSyncs(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1830 | switch (msg.what) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1831 | case SyncHandler.MESSAGE_CANCEL: { |
| 1832 | Pair<Account, String> payload = (Pair<Account, String>)msg.obj; |
| 1833 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1834 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_CANCEL: " |
| 1835 | + payload.first + ", " + payload.second); |
| 1836 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1837 | cancelActiveSyncLocked(payload.first, msg.arg1, payload.second); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1838 | nextPendingSyncTime = maybeStartNextSyncLocked(); |
| 1839 | break; |
| 1840 | } |
| 1841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1842 | case SyncHandler.MESSAGE_SYNC_FINISHED: |
| 1843 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1844 | Log.v(TAG, "handleSyncHandlerMessage: MESSAGE_SYNC_FINISHED"); |
| 1845 | } |
| 1846 | SyncHandlerMessagePayload payload = (SyncHandlerMessagePayload)msg.obj; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1847 | if (!isSyncStillActive(payload.activeSyncContext)) { |
| 1848 | Log.d(TAG, "handleSyncHandlerMessage: dropping since the " |
| 1849 | + "sync is no longer active: " |
| 1850 | + payload.activeSyncContext); |
| 1851 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1852 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1853 | runSyncFinishedOrCanceledLocked(payload.syncResult, payload.activeSyncContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1854 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1855 | // since a sync just finished check if it is time to start a new sync |
| 1856 | nextPendingSyncTime = maybeStartNextSyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | break; |
| 1858 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1859 | case SyncHandler.MESSAGE_SERVICE_CONNECTED: { |
| 1860 | ServiceConnectionData msgData = (ServiceConnectionData)msg.obj; |
| 1861 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1862 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_CONNECTED: " |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1863 | + msgData.activeSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1864 | } |
| 1865 | // check that this isn't an old message |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1866 | if (isSyncStillActive(msgData.activeSyncContext)) { |
| 1867 | runBoundToSyncAdapter(msgData.activeSyncContext, msgData.syncAdapter); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1868 | } |
| 1869 | break; |
| 1870 | } |
| 1871 | |
| 1872 | case SyncHandler.MESSAGE_SERVICE_DISCONNECTED: { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1873 | final ActiveSyncContext currentSyncContext = |
| 1874 | ((ServiceConnectionData)msg.obj).activeSyncContext; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1875 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1876 | Log.d(TAG, "handleSyncHandlerMessage: MESSAGE_SERVICE_DISCONNECTED: " |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1877 | + currentSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1878 | } |
| 1879 | // check that this isn't an old message |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1880 | if (isSyncStillActive(currentSyncContext)) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1881 | // cancel the sync if we have a syncadapter, which means one is |
| 1882 | // outstanding |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1883 | if (currentSyncContext.mSyncAdapter != null) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1884 | try { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1885 | currentSyncContext.mSyncAdapter.cancelSync(currentSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1886 | } catch (RemoteException e) { |
| 1887 | // we don't need to retry this in this case |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | // pretend that the sync failed with an IOException, |
| 1892 | // which is a soft error |
| 1893 | SyncResult syncResult = new SyncResult(); |
| 1894 | syncResult.stats.numIoExceptions++; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1895 | runSyncFinishedOrCanceledLocked(syncResult, currentSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1896 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1897 | // since a sync just finished check if it is time to start a new sync |
| 1898 | nextPendingSyncTime = maybeStartNextSyncLocked(); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | break; |
| 1902 | } |
| 1903 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1904 | case SyncHandler.MESSAGE_SYNC_ALARM: { |
| 1905 | boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
| 1906 | if (isLoggable) { |
| 1907 | Log.v(TAG, "handleSyncHandlerMessage: MESSAGE_SYNC_ALARM"); |
| 1908 | } |
| 1909 | mAlarmScheduleTime = null; |
| 1910 | try { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1911 | nextPendingSyncTime = maybeStartNextSyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1912 | } finally { |
| 1913 | mHandleAlarmWakeLock.release(); |
| 1914 | } |
| 1915 | break; |
| 1916 | } |
| 1917 | |
| 1918 | case SyncHandler.MESSAGE_CHECK_ALARMS: |
| 1919 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 1920 | Log.v(TAG, "handleSyncHandlerMessage: MESSAGE_CHECK_ALARMS"); |
| 1921 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1922 | nextPendingSyncTime = maybeStartNextSyncLocked(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | break; |
| 1924 | } |
| 1925 | } finally { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1926 | manageSyncNotificationLocked(); |
| 1927 | manageSyncAlarmLocked(earliestFuturePollTime, nextPendingSyncTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1928 | mSyncTimeTracker.update(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1929 | mSyncManagerWakeLock.release(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1933 | /** |
| 1934 | * Turn any periodic sync operations that are ready to run into pending sync operations. |
| 1935 | * @return the desired start time of the earliest future periodic sync operation, |
| 1936 | * in milliseconds since boot |
| 1937 | */ |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1938 | private long scheduleReadyPeriodicSyncs() { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1939 | final boolean backgroundDataUsageAllowed = |
| 1940 | getConnectivityManager().getBackgroundDataSetting(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 1941 | long earliestFuturePollTime = Long.MAX_VALUE; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1942 | if (!backgroundDataUsageAllowed) { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1943 | return earliestFuturePollTime; |
| 1944 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1945 | |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 1946 | AccountAndUser[] accounts = mRunningAccounts; |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1947 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1948 | final long nowAbsolute = System.currentTimeMillis(); |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 1949 | final long shiftedNowAbsolute = (0 < nowAbsolute - mSyncRandomOffsetMillis) |
| 1950 | ? (nowAbsolute - mSyncRandomOffsetMillis) : 0; |
| 1951 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1952 | ArrayList<SyncStorageEngine.AuthorityInfo> infos = mSyncStorageEngine.getAuthorities(); |
| 1953 | for (SyncStorageEngine.AuthorityInfo info : infos) { |
| 1954 | // skip the sync if the account of this operation no longer exists |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1955 | if (!containsAccountAndUser(accounts, info.account, info.userId)) { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1956 | continue; |
| 1957 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 1958 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1959 | if (!mSyncStorageEngine.getMasterSyncAutomatically(info.userId) |
| 1960 | || !mSyncStorageEngine.getSyncAutomatically(info.account, info.userId, |
| 1961 | info.authority)) { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1962 | continue; |
| 1963 | } |
| 1964 | |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 1965 | if (getIsSyncable(info.account, info.userId, info.authority) |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1966 | == 0) { |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1967 | continue; |
| 1968 | } |
| 1969 | |
| 1970 | SyncStatusInfo status = mSyncStorageEngine.getOrCreateSyncStatus(info); |
| 1971 | for (int i = 0, N = info.periodicSyncs.size(); i < N; i++) { |
| 1972 | final Bundle extras = info.periodicSyncs.get(i).first; |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 1973 | final Long periodInMillis = info.periodicSyncs.get(i).second * 1000; |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 1974 | // find when this periodic sync was last scheduled to run |
| 1975 | final long lastPollTimeAbsolute = status.getPeriodicSyncTime(i); |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 1976 | |
| 1977 | long remainingMillis |
| 1978 | = periodInMillis - (shiftedNowAbsolute % periodInMillis); |
| 1979 | |
| 1980 | /* |
| 1981 | * Sync scheduling strategy: |
| 1982 | * Set the next periodic sync based on a random offset (in seconds). |
| 1983 | * |
| 1984 | * Also sync right now if any of the following cases hold |
| 1985 | * and mark it as having been scheduled |
| 1986 | * |
| 1987 | * Case 1: This sync is ready to run now. |
| 1988 | * Case 2: If the lastPollTimeAbsolute is in the future, |
| 1989 | * sync now and reinitialize. This can happen for |
| 1990 | * example if the user changed the time, synced and |
| 1991 | * changed back. |
| 1992 | * Case 3: If we failed to sync at the last scheduled time |
| 1993 | */ |
| 1994 | if (remainingMillis == periodInMillis // Case 1 |
| 1995 | || lastPollTimeAbsolute > nowAbsolute // Case 2 |
| 1996 | || (nowAbsolute - lastPollTimeAbsolute |
| 1997 | >= periodInMillis)) { // Case 3 |
| 1998 | // Sync now |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 1999 | final Pair<Long, Long> backoff = mSyncStorageEngine.getBackoff( |
| 2000 | info.account, info.userId, info.authority); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2001 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo; |
| 2002 | syncAdapterInfo = mSyncAdapters.getServiceInfo( |
| 2003 | SyncAdapterType.newKey(info.authority, info.account.type), |
| 2004 | info.userId); |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 2005 | if (syncAdapterInfo == null) { |
| 2006 | continue; |
| 2007 | } |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2008 | scheduleSyncOperation( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2009 | new SyncOperation(info.account, info.userId, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2010 | SyncOperation.REASON_PERIODIC, |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2011 | SyncStorageEngine.SOURCE_PERIODIC, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2012 | info.authority, extras, 0 /* delay */, |
| 2013 | backoff != null ? backoff.first : 0, |
| 2014 | mSyncStorageEngine.getDelayUntilTime( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2015 | info.account, info.userId, info.authority), |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 2016 | syncAdapterInfo.type.allowParallelSyncs())); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2017 | status.setPeriodicSyncTime(i, nowAbsolute); |
Ashish Sharma | 69d95de | 2012-04-11 17:27:24 -0700 | [diff] [blame] | 2018 | } |
| 2019 | // Compute when this periodic sync should next run |
| 2020 | final long nextPollTimeAbsolute = nowAbsolute + remainingMillis; |
| 2021 | |
| 2022 | // remember this time if it is earlier than earliestFuturePollTime |
| 2023 | if (nextPollTimeAbsolute < earliestFuturePollTime) { |
| 2024 | earliestFuturePollTime = nextPollTimeAbsolute; |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2025 | } |
| 2026 | } |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2027 | } |
| 2028 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2029 | if (earliestFuturePollTime == Long.MAX_VALUE) { |
| 2030 | return Long.MAX_VALUE; |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2031 | } |
| 2032 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2033 | // convert absolute time to elapsed time |
| 2034 | return SystemClock.elapsedRealtime() |
| 2035 | + ((earliestFuturePollTime < nowAbsolute) |
| 2036 | ? 0 |
| 2037 | : (earliestFuturePollTime - nowAbsolute)); |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2038 | } |
| 2039 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2040 | private long maybeStartNextSyncLocked() { |
| 2041 | final boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
| 2042 | if (isLoggable) Log.v(TAG, "maybeStartNextSync"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | |
| 2044 | // If we aren't ready to run (e.g. the data connection is down), get out. |
| 2045 | if (!mDataConnectionIsConnected) { |
| 2046 | if (isLoggable) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2047 | Log.v(TAG, "maybeStartNextSync: no data connection, skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2048 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2049 | return Long.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | if (mStorageIsLow) { |
| 2053 | if (isLoggable) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2054 | Log.v(TAG, "maybeStartNextSync: memory low, skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2055 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2056 | return Long.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | // If the accounts aren't known yet then we aren't ready to run. We will be kicked |
| 2060 | // when the account lookup request does complete. |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2061 | AccountAndUser[] accounts = mRunningAccounts; |
Fred Quintana | 53bd252 | 2010-02-05 15:28:12 -0800 | [diff] [blame] | 2062 | if (accounts == INITIAL_ACCOUNTS_ARRAY) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2063 | if (isLoggable) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2064 | Log.v(TAG, "maybeStartNextSync: accounts not known, skipping"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2066 | return Long.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | // Otherwise consume SyncOperations from the head of the SyncQueue until one is |
| 2070 | // found that is runnable (not disabled, etc). If that one is ready to run then |
| 2071 | // start it, otherwise just get out. |
Fred Quintana | f892fb3 | 2009-08-27 21:32:08 -0700 | [diff] [blame] | 2072 | final boolean backgroundDataUsageAllowed = |
| 2073 | getConnectivityManager().getBackgroundDataSetting(); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2074 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2075 | final long now = SystemClock.elapsedRealtime(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2077 | // will be set to the next time that a sync should be considered for running |
| 2078 | long nextReadyToRunTime = Long.MAX_VALUE; |
| 2079 | |
| 2080 | // order the sync queue, dropping syncs that are not allowed |
| 2081 | ArrayList<SyncOperation> operations = new ArrayList<SyncOperation>(); |
| 2082 | synchronized (mSyncQueue) { |
| 2083 | if (isLoggable) { |
| 2084 | Log.v(TAG, "build the operation array, syncQueue size is " |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 2085 | + mSyncQueue.getOperations().size()); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2086 | } |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 2087 | final Iterator<SyncOperation> operationIterator = mSyncQueue.getOperations() |
| 2088 | .iterator(); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 2089 | |
| 2090 | final ActivityManager activityManager |
| 2091 | = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); |
| 2092 | final Set<Integer> removedUsers = Sets.newHashSet(); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2093 | while (operationIterator.hasNext()) { |
| 2094 | final SyncOperation op = operationIterator.next(); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2095 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2096 | // drop the sync if the account of this operation no longer exists |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2097 | if (!containsAccountAndUser(accounts, op.account, op.userId)) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2098 | operationIterator.remove(); |
| 2099 | mSyncStorageEngine.deleteFromPending(op.pendingOperation); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2100 | continue; |
| 2101 | } |
| 2102 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2103 | // drop this sync request if it isn't syncable |
Amith Yamasani | 9422bdc | 2013-04-10 16:58:19 -0700 | [diff] [blame] | 2104 | int syncableState = getIsSyncable( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2105 | op.account, op.userId, op.authority); |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2106 | if (syncableState == 0) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2107 | operationIterator.remove(); |
| 2108 | mSyncStorageEngine.deleteFromPending(op.pendingOperation); |
| 2109 | continue; |
| 2110 | } |
| 2111 | |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 2112 | // if the user in not running, drop the request |
| 2113 | if (!activityManager.isUserRunning(op.userId)) { |
| 2114 | final UserInfo userInfo = mUserManager.getUserInfo(op.userId); |
| 2115 | if (userInfo == null) { |
| 2116 | removedUsers.add(op.userId); |
| 2117 | } |
| 2118 | continue; |
| 2119 | } |
| 2120 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2121 | // if the next run time is in the future, meaning there are no syncs ready |
| 2122 | // to run, return the time |
| 2123 | if (op.effectiveRunTime > now) { |
| 2124 | if (nextReadyToRunTime > op.effectiveRunTime) { |
| 2125 | nextReadyToRunTime = op.effectiveRunTime; |
| 2126 | } |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2127 | continue; |
| 2128 | } |
| 2129 | |
Jeff Sharkey | 3482188 | 2011-07-13 10:44:07 -0700 | [diff] [blame] | 2130 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo; |
| 2131 | syncAdapterInfo = mSyncAdapters.getServiceInfo( |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2132 | SyncAdapterType.newKey(op.authority, op.account.type), op.userId); |
Jeff Sharkey | 3482188 | 2011-07-13 10:44:07 -0700 | [diff] [blame] | 2133 | |
| 2134 | // only proceed if network is connected for requesting UID |
| 2135 | final boolean uidNetworkConnected; |
| 2136 | if (syncAdapterInfo != null) { |
| 2137 | final NetworkInfo networkInfo = getConnectivityManager() |
| 2138 | .getActiveNetworkInfoForUid(syncAdapterInfo.uid); |
| 2139 | uidNetworkConnected = networkInfo != null && networkInfo.isConnected(); |
| 2140 | } else { |
| 2141 | uidNetworkConnected = false; |
| 2142 | } |
| 2143 | |
| 2144 | // skip the sync if it isn't manual, and auto sync or |
| 2145 | // background data usage is disabled or network is |
| 2146 | // disconnected for the target UID. |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2147 | if (!op.extras.getBoolean(ContentResolver.SYNC_EXTRAS_IGNORE_SETTINGS, false) |
| 2148 | && (syncableState > 0) |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2149 | && (!mSyncStorageEngine.getMasterSyncAutomatically(op.userId) |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2150 | || !backgroundDataUsageAllowed |
Jeff Sharkey | 3482188 | 2011-07-13 10:44:07 -0700 | [diff] [blame] | 2151 | || !uidNetworkConnected |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2152 | || !mSyncStorageEngine.getSyncAutomatically( |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2153 | op.account, op.userId, op.authority))) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2154 | operationIterator.remove(); |
| 2155 | mSyncStorageEngine.deleteFromPending(op.pendingOperation); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2156 | continue; |
| 2157 | } |
| 2158 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2159 | operations.add(op); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2160 | } |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 2161 | for (Integer user : removedUsers) { |
| 2162 | // if it's still removed |
| 2163 | if (mUserManager.getUserInfo(user) == null) { |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2164 | onUserRemoved(user); |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 2165 | } |
| 2166 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2167 | } |
| 2168 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2169 | // find the next operation to dispatch, if one is ready |
| 2170 | // iterate from the top, keep issuing (while potentially cancelling existing syncs) |
| 2171 | // until the quotas are filled. |
| 2172 | // once the quotas are filled iterate once more to find when the next one would be |
| 2173 | // (also considering pre-emption reasons). |
| 2174 | if (isLoggable) Log.v(TAG, "sort the candidate operations, size " + operations.size()); |
| 2175 | Collections.sort(operations); |
| 2176 | if (isLoggable) Log.v(TAG, "dispatch all ready sync operations"); |
| 2177 | for (int i = 0, N = operations.size(); i < N; i++) { |
| 2178 | final SyncOperation candidate = operations.get(i); |
| 2179 | final boolean candidateIsInitialization = candidate.isInitialization(); |
| 2180 | |
| 2181 | int numInit = 0; |
| 2182 | int numRegular = 0; |
| 2183 | ActiveSyncContext conflict = null; |
| 2184 | ActiveSyncContext longRunning = null; |
| 2185 | ActiveSyncContext toReschedule = null; |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 2186 | ActiveSyncContext oldestNonExpeditedRegular = null; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2187 | |
| 2188 | for (ActiveSyncContext activeSyncContext : mActiveSyncContexts) { |
| 2189 | final SyncOperation activeOp = activeSyncContext.mSyncOperation; |
| 2190 | if (activeOp.isInitialization()) { |
| 2191 | numInit++; |
| 2192 | } else { |
| 2193 | numRegular++; |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 2194 | if (!activeOp.isExpedited()) { |
| 2195 | if (oldestNonExpeditedRegular == null |
| 2196 | || (oldestNonExpeditedRegular.mStartTime |
| 2197 | > activeSyncContext.mStartTime)) { |
| 2198 | oldestNonExpeditedRegular = activeSyncContext; |
| 2199 | } |
| 2200 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2201 | } |
| 2202 | if (activeOp.account.type.equals(candidate.account.type) |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 2203 | && activeOp.authority.equals(candidate.authority) |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2204 | && activeOp.userId == candidate.userId |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 2205 | && (!activeOp.allowParallelSyncs |
| 2206 | || activeOp.account.name.equals(candidate.account.name))) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2207 | conflict = activeSyncContext; |
| 2208 | // don't break out since we want to do a full count of the varieties |
| 2209 | } else { |
| 2210 | if (candidateIsInitialization == activeOp.isInitialization() |
| 2211 | && activeSyncContext.mStartTime + MAX_TIME_PER_SYNC < now) { |
| 2212 | longRunning = activeSyncContext; |
| 2213 | // don't break out since we want to do a full count of the varieties |
| 2214 | } |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | if (isLoggable) { |
| 2219 | Log.v(TAG, "candidate " + (i + 1) + " of " + N + ": " + candidate); |
| 2220 | Log.v(TAG, " numActiveInit=" + numInit + ", numActiveRegular=" + numRegular); |
| 2221 | Log.v(TAG, " longRunning: " + longRunning); |
| 2222 | Log.v(TAG, " conflict: " + conflict); |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 2223 | Log.v(TAG, " oldestNonExpeditedRegular: " + oldestNonExpeditedRegular); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2224 | } |
| 2225 | |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 2226 | final boolean roomAvailable = candidateIsInitialization |
| 2227 | ? numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS |
| 2228 | : numRegular < MAX_SIMULTANEOUS_REGULAR_SYNCS; |
| 2229 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2230 | if (conflict != null) { |
| 2231 | if (candidateIsInitialization && !conflict.mSyncOperation.isInitialization() |
| 2232 | && numInit < MAX_SIMULTANEOUS_INITIALIZATION_SYNCS) { |
| 2233 | toReschedule = conflict; |
| 2234 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2235 | Log.v(TAG, "canceling and rescheduling sync since an initialization " |
| 2236 | + "takes higher priority, " + conflict); |
| 2237 | } |
| 2238 | } else if (candidate.expedited && !conflict.mSyncOperation.expedited |
| 2239 | && (candidateIsInitialization |
| 2240 | == conflict.mSyncOperation.isInitialization())) { |
| 2241 | toReschedule = conflict; |
| 2242 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2243 | Log.v(TAG, "canceling and rescheduling sync since an expedited " |
| 2244 | + "takes higher priority, " + conflict); |
| 2245 | } |
| 2246 | } else { |
| 2247 | continue; |
| 2248 | } |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 2249 | } else if (roomAvailable) { |
| 2250 | // dispatch candidate |
| 2251 | } else if (candidate.isExpedited() && oldestNonExpeditedRegular != null |
| 2252 | && !candidateIsInitialization) { |
| 2253 | // We found an active, non-expedited regular sync. We also know that the |
| 2254 | // candidate doesn't conflict with this active sync since conflict |
| 2255 | // is null. Reschedule the active sync and start the candidate. |
| 2256 | toReschedule = oldestNonExpeditedRegular; |
| 2257 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2258 | Log.v(TAG, "canceling and rescheduling sync since an expedited is ready to run, " |
| 2259 | + oldestNonExpeditedRegular); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2260 | } |
Fred Quintana | dc47556 | 2012-05-04 15:51:54 -0700 | [diff] [blame] | 2261 | } else if (longRunning != null |
| 2262 | && (candidateIsInitialization |
| 2263 | == longRunning.mSyncOperation.isInitialization())) { |
| 2264 | // We found an active, long-running sync. Reschedule the active |
| 2265 | // sync and start the candidate. |
| 2266 | toReschedule = longRunning; |
| 2267 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2268 | Log.v(TAG, "canceling and rescheduling sync since it ran roo long, " |
| 2269 | + longRunning); |
| 2270 | } |
| 2271 | } else { |
| 2272 | // we were unable to find or make space to run this candidate, go on to |
| 2273 | // the next one |
| 2274 | continue; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | if (toReschedule != null) { |
| 2278 | runSyncFinishedOrCanceledLocked(null, toReschedule); |
| 2279 | scheduleSyncOperation(toReschedule.mSyncOperation); |
| 2280 | } |
Jeff Sharkey | a706e2f | 2012-10-16 12:02:42 -0700 | [diff] [blame] | 2281 | synchronized (mSyncQueue) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2282 | mSyncQueue.remove(candidate); |
| 2283 | } |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 2284 | dispatchSyncOperation(candidate); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | return nextReadyToRunTime; |
| 2288 | } |
| 2289 | |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 2290 | private boolean dispatchSyncOperation(SyncOperation op) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2291 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 2292 | Log.v(TAG, "dispatchSyncOperation: we are going to sync " + op); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2293 | Log.v(TAG, "num active syncs: " + mActiveSyncContexts.size()); |
| 2294 | for (ActiveSyncContext syncContext : mActiveSyncContexts) { |
| 2295 | Log.v(TAG, syncContext.toString()); |
| 2296 | } |
| 2297 | } |
| 2298 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2299 | // connect to the sync adapter |
Fred Quintana | 4a6679b | 2009-08-17 13:05:39 -0700 | [diff] [blame] | 2300 | SyncAdapterType syncAdapterType = SyncAdapterType.newKey(op.authority, op.account.type); |
Jeff Sharkey | 6ab72d7 | 2012-10-08 16:44:37 -0700 | [diff] [blame] | 2301 | final RegisteredServicesCache.ServiceInfo<SyncAdapterType> syncAdapterInfo; |
| 2302 | syncAdapterInfo = mSyncAdapters.getServiceInfo(syncAdapterType, op.userId); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2303 | if (syncAdapterInfo == null) { |
Fred Quintana | 7620f1a | 2010-03-16 15:58:44 -0700 | [diff] [blame] | 2304 | Log.d(TAG, "can't find a sync adapter for " + syncAdapterType |
| 2305 | + ", removing settings for it"); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2306 | mSyncStorageEngine.removeAuthority(op.account, op.userId, op.authority); |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 2307 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2308 | } |
| 2309 | |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2310 | ActiveSyncContext activeSyncContext = |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2311 | new ActiveSyncContext(op, insertStartSyncEvent(op), syncAdapterInfo.uid); |
| 2312 | activeSyncContext.mSyncInfo = mSyncStorageEngine.addActiveSync(activeSyncContext); |
| 2313 | mActiveSyncContexts.add(activeSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2314 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2315 | Log.v(TAG, "dispatchSyncOperation: starting " + activeSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2316 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2317 | if (!activeSyncContext.bindToSyncAdapter(syncAdapterInfo, op.userId)) { |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2318 | Log.e(TAG, "Bind attempt failed to " + syncAdapterInfo); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2319 | closeActiveSyncContext(activeSyncContext); |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 2320 | return false; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Fred Quintana | 87b1466 | 2011-09-12 10:32:55 -0700 | [diff] [blame] | 2323 | return true; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 2326 | private void runBoundToSyncAdapter(final ActiveSyncContext activeSyncContext, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2327 | ISyncAdapter syncAdapter) { |
| 2328 | activeSyncContext.mSyncAdapter = syncAdapter; |
| 2329 | final SyncOperation syncOperation = activeSyncContext.mSyncOperation; |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2330 | try { |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 2331 | activeSyncContext.mIsLinkedToDeath = true; |
| 2332 | syncAdapter.asBinder().linkToDeath(activeSyncContext, 0); |
| 2333 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2334 | syncAdapter.startSync(activeSyncContext, syncOperation.authority, |
Fred Quintana | 21bb0de | 2009-06-16 10:24:58 -0700 | [diff] [blame] | 2335 | syncOperation.account, syncOperation.extras); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2336 | } catch (RemoteException remoteExc) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2337 | Log.d(TAG, "maybeStartNextSync: caught a RemoteException, rescheduling", remoteExc); |
| 2338 | closeActiveSyncContext(activeSyncContext); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2339 | increaseBackoffSetting(syncOperation); |
| 2340 | scheduleSyncOperation(new SyncOperation(syncOperation)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2341 | } catch (RuntimeException exc) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2342 | closeActiveSyncContext(activeSyncContext); |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2343 | Log.e(TAG, "Caught RuntimeException while starting the sync " + syncOperation, exc); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2344 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2345 | } |
| 2346 | |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2347 | private void cancelActiveSyncLocked(Account account, int userId, String authority) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2348 | ArrayList<ActiveSyncContext> activeSyncs = |
| 2349 | new ArrayList<ActiveSyncContext>(mActiveSyncContexts); |
| 2350 | for (ActiveSyncContext activeSyncContext : activeSyncs) { |
| 2351 | if (activeSyncContext != null) { |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 2352 | // if an account was specified then only cancel the sync if it matches |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2353 | if (account != null) { |
| 2354 | if (!account.equals(activeSyncContext.mSyncOperation.account)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2355 | continue; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2356 | } |
| 2357 | } |
Alon Albert | 8e28555 | 2012-09-17 15:05:27 -0700 | [diff] [blame] | 2358 | // if an authority was specified then only cancel the sync if it matches |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2359 | if (authority != null) { |
| 2360 | if (!authority.equals(activeSyncContext.mSyncOperation.authority)) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2361 | continue; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2362 | } |
| 2363 | } |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2364 | // check if the userid matches |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 2365 | if (userId != UserHandle.USER_ALL |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2366 | && userId != activeSyncContext.mSyncOperation.userId) { |
| 2367 | continue; |
| 2368 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2369 | runSyncFinishedOrCanceledLocked(null /* no result since this is a cancel */, |
| 2370 | activeSyncContext); |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | private void runSyncFinishedOrCanceledLocked(SyncResult syncResult, |
| 2376 | ActiveSyncContext activeSyncContext) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2377 | boolean isLoggable = Log.isLoggable(TAG, Log.VERBOSE); |
Alon Albert | eca7511 | 2010-12-08 15:02:33 -0800 | [diff] [blame] | 2378 | |
| 2379 | if (activeSyncContext.mIsLinkedToDeath) { |
| 2380 | activeSyncContext.mSyncAdapter.asBinder().unlinkToDeath(activeSyncContext, 0); |
| 2381 | activeSyncContext.mIsLinkedToDeath = false; |
| 2382 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2383 | closeActiveSyncContext(activeSyncContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | |
| 2385 | final SyncOperation syncOperation = activeSyncContext.mSyncOperation; |
| 2386 | |
| 2387 | final long elapsedTime = SystemClock.elapsedRealtime() - activeSyncContext.mStartTime; |
| 2388 | |
| 2389 | String historyMessage; |
| 2390 | int downstreamActivity; |
| 2391 | int upstreamActivity; |
| 2392 | if (syncResult != null) { |
| 2393 | if (isLoggable) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2394 | Log.v(TAG, "runSyncFinishedOrCanceled [finished]: " |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2395 | + syncOperation + ", result " + syncResult); |
| 2396 | } |
| 2397 | |
| 2398 | if (!syncResult.hasError()) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2399 | historyMessage = SyncStorageEngine.MESG_SUCCESS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2400 | // TODO: set these correctly when the SyncResult is extended to include it |
| 2401 | downstreamActivity = 0; |
| 2402 | upstreamActivity = 0; |
Alon Albert | 6e079a3 | 2010-11-12 12:41:09 -0800 | [diff] [blame] | 2403 | clearBackoffSetting(syncOperation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2404 | } else { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2405 | Log.d(TAG, "failed sync operation " + syncOperation + ", " + syncResult); |
| 2406 | // the operation failed so increase the backoff time |
| 2407 | if (!syncResult.syncAlreadyInProgress) { |
| 2408 | increaseBackoffSetting(syncOperation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2409 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2410 | // reschedule the sync if so indicated by the syncResult |
| 2411 | maybeRescheduleSync(syncResult, syncOperation); |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2412 | historyMessage = ContentResolver.syncErrorToString( |
| 2413 | syncResultToErrorNumber(syncResult)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2414 | // TODO: set these correctly when the SyncResult is extended to include it |
| 2415 | downstreamActivity = 0; |
| 2416 | upstreamActivity = 0; |
| 2417 | } |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2418 | |
| 2419 | setDelayUntilTime(syncOperation, syncResult.delayUntil); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2420 | } else { |
| 2421 | if (isLoggable) { |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2422 | Log.v(TAG, "runSyncFinishedOrCanceled [canceled]: " + syncOperation); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2423 | } |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2424 | if (activeSyncContext.mSyncAdapter != null) { |
| 2425 | try { |
Fred Quintana | 21bb0de | 2009-06-16 10:24:58 -0700 | [diff] [blame] | 2426 | activeSyncContext.mSyncAdapter.cancelSync(activeSyncContext); |
Fred Quintana | 718d8a2 | 2009-04-29 17:53:20 -0700 | [diff] [blame] | 2427 | } catch (RemoteException e) { |
| 2428 | // we don't need to retry this in this case |
| 2429 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2430 | } |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2431 | historyMessage = SyncStorageEngine.MESG_CANCELED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2432 | downstreamActivity = 0; |
| 2433 | upstreamActivity = 0; |
| 2434 | } |
| 2435 | |
| 2436 | stopSyncEvent(activeSyncContext.mHistoryRowId, syncOperation, historyMessage, |
| 2437 | upstreamActivity, downstreamActivity, elapsedTime); |
| 2438 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2439 | if (syncResult != null && syncResult.tooManyDeletions) { |
| 2440 | installHandleTooManyDeletesNotification(syncOperation.account, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2441 | syncOperation.authority, syncResult.stats.numDeletes, |
| 2442 | syncOperation.userId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2443 | } else { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2444 | mNotificationMgr.cancelAsUser(null, |
| 2445 | syncOperation.account.hashCode() ^ syncOperation.authority.hashCode(), |
| 2446 | new UserHandle(syncOperation.userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2447 | } |
| 2448 | |
| 2449 | if (syncResult != null && syncResult.fullSyncRequested) { |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2450 | scheduleSyncOperation(new SyncOperation(syncOperation.account, syncOperation.userId, |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2451 | syncOperation.reason, |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2452 | syncOperation.syncSource, syncOperation.authority, new Bundle(), 0, |
Fred Quintana | 0c4d04a | 2010-11-03 17:02:55 -0700 | [diff] [blame] | 2453 | syncOperation.backoff, syncOperation.delayUntil, |
| 2454 | syncOperation.allowParallelSyncs)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2455 | } |
| 2456 | // no need to schedule an alarm, as that will be done by our caller. |
| 2457 | } |
| 2458 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2459 | private void closeActiveSyncContext(ActiveSyncContext activeSyncContext) { |
| 2460 | activeSyncContext.close(); |
| 2461 | mActiveSyncContexts.remove(activeSyncContext); |
Amith Yamasani | 04e0d26 | 2012-02-14 11:50:53 -0800 | [diff] [blame] | 2462 | mSyncStorageEngine.removeActiveSync(activeSyncContext.mSyncInfo, |
| 2463 | activeSyncContext.mSyncOperation.userId); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2466 | /** |
| 2467 | * Convert the error-containing SyncResult into the Sync.History error number. Since |
| 2468 | * the SyncResult may indicate multiple errors at once, this method just returns the |
| 2469 | * most "serious" error. |
| 2470 | * @param syncResult the SyncResult from which to read |
| 2471 | * @return the most "serious" error set in the SyncResult |
| 2472 | * @throws IllegalStateException if the SyncResult does not indicate any errors. |
| 2473 | * If SyncResult.error() is true then it is safe to call this. |
| 2474 | */ |
| 2475 | private int syncResultToErrorNumber(SyncResult syncResult) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2476 | if (syncResult.syncAlreadyInProgress) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2477 | return ContentResolver.SYNC_ERROR_SYNC_ALREADY_IN_PROGRESS; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2478 | if (syncResult.stats.numAuthExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2479 | return ContentResolver.SYNC_ERROR_AUTHENTICATION; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2480 | if (syncResult.stats.numIoExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2481 | return ContentResolver.SYNC_ERROR_IO; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2482 | if (syncResult.stats.numParseExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2483 | return ContentResolver.SYNC_ERROR_PARSE; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2484 | if (syncResult.stats.numConflictDetectedExceptions > 0) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2485 | return ContentResolver.SYNC_ERROR_CONFLICT; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2486 | if (syncResult.tooManyDeletions) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2487 | return ContentResolver.SYNC_ERROR_TOO_MANY_DELETIONS; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2488 | if (syncResult.tooManyRetries) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2489 | return ContentResolver.SYNC_ERROR_TOO_MANY_RETRIES; |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2490 | if (syncResult.databaseError) |
Fred Quintana | ac9385e | 2009-06-22 18:00:59 -0700 | [diff] [blame] | 2491 | return ContentResolver.SYNC_ERROR_INTERNAL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2492 | throw new IllegalStateException("we are not in an error state, " + syncResult); |
| 2493 | } |
| 2494 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2495 | private void manageSyncNotificationLocked() { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2496 | boolean shouldCancel; |
| 2497 | boolean shouldInstall; |
| 2498 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2499 | if (mActiveSyncContexts.isEmpty()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2500 | mSyncNotificationInfo.startTime = null; |
| 2501 | |
| 2502 | // we aren't syncing. if the notification is active then remember that we need |
| 2503 | // to cancel it and then clear out the info |
| 2504 | shouldCancel = mSyncNotificationInfo.isActive; |
| 2505 | shouldInstall = false; |
| 2506 | } else { |
| 2507 | // we are syncing |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2508 | final long now = SystemClock.elapsedRealtime(); |
| 2509 | if (mSyncNotificationInfo.startTime == null) { |
| 2510 | mSyncNotificationInfo.startTime = now; |
| 2511 | } |
| 2512 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2513 | // there are three cases: |
| 2514 | // - the notification is up: do nothing |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2515 | // - the notification is not up but it isn't time yet: don't install |
| 2516 | // - the notification is not up and it is time: need to install |
| 2517 | |
| 2518 | if (mSyncNotificationInfo.isActive) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2519 | shouldInstall = shouldCancel = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2520 | } else { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2521 | // it isn't currently up, so there is nothing to cancel |
| 2522 | shouldCancel = false; |
| 2523 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2524 | final boolean timeToShowNotification = |
| 2525 | now > mSyncNotificationInfo.startTime + SYNC_NOTIFICATION_DELAY; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2526 | if (timeToShowNotification) { |
| 2527 | shouldInstall = true; |
| 2528 | } else { |
| 2529 | // show the notification immediately if this is a manual sync |
| 2530 | shouldInstall = false; |
| 2531 | for (ActiveSyncContext activeSyncContext : mActiveSyncContexts) { |
| 2532 | final boolean manualSync = activeSyncContext.mSyncOperation.extras |
| 2533 | .getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, false); |
| 2534 | if (manualSync) { |
| 2535 | shouldInstall = true; |
| 2536 | break; |
| 2537 | } |
| 2538 | } |
| 2539 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | if (shouldCancel && !shouldInstall) { |
| 2544 | mNeedSyncActiveNotification = false; |
| 2545 | sendSyncStateIntent(); |
| 2546 | mSyncNotificationInfo.isActive = false; |
| 2547 | } |
| 2548 | |
| 2549 | if (shouldInstall) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2550 | mNeedSyncActiveNotification = true; |
| 2551 | sendSyncStateIntent(); |
| 2552 | mSyncNotificationInfo.isActive = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2553 | } |
| 2554 | } |
| 2555 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2556 | private void manageSyncAlarmLocked(long nextPeriodicEventElapsedTime, |
| 2557 | long nextPendingEventElapsedTime) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2558 | // in each of these cases the sync loop will be kicked, which will cause this |
| 2559 | // method to be called again |
| 2560 | if (!mDataConnectionIsConnected) return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2561 | if (mStorageIsLow) return; |
| 2562 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2563 | // When the status bar notification should be raised |
| 2564 | final long notificationTime = |
| 2565 | (!mSyncHandler.mSyncNotificationInfo.isActive |
| 2566 | && mSyncHandler.mSyncNotificationInfo.startTime != null) |
| 2567 | ? mSyncHandler.mSyncNotificationInfo.startTime + SYNC_NOTIFICATION_DELAY |
| 2568 | : Long.MAX_VALUE; |
Fred Quintana | 307da1a | 2010-01-21 14:24:20 -0800 | [diff] [blame] | 2569 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2570 | // When we should consider canceling an active sync |
| 2571 | long earliestTimeoutTime = Long.MAX_VALUE; |
| 2572 | for (ActiveSyncContext currentSyncContext : mActiveSyncContexts) { |
| 2573 | final long currentSyncTimeoutTime = |
| 2574 | currentSyncContext.mTimeoutStartTime + MAX_TIME_PER_SYNC; |
| 2575 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2576 | Log.v(TAG, "manageSyncAlarm: active sync, mTimeoutStartTime + MAX is " |
| 2577 | + currentSyncTimeoutTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2578 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2579 | if (earliestTimeoutTime > currentSyncTimeoutTime) { |
| 2580 | earliestTimeoutTime = currentSyncTimeoutTime; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2584 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2585 | Log.v(TAG, "manageSyncAlarm: notificationTime is " + notificationTime); |
| 2586 | } |
| 2587 | |
| 2588 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2589 | Log.v(TAG, "manageSyncAlarm: earliestTimeoutTime is " + earliestTimeoutTime); |
| 2590 | } |
| 2591 | |
| 2592 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2593 | Log.v(TAG, "manageSyncAlarm: nextPeriodicEventElapsedTime is " |
| 2594 | + nextPeriodicEventElapsedTime); |
| 2595 | } |
| 2596 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2597 | Log.v(TAG, "manageSyncAlarm: nextPendingEventElapsedTime is " |
| 2598 | + nextPendingEventElapsedTime); |
| 2599 | } |
| 2600 | |
| 2601 | long alarmTime = Math.min(notificationTime, earliestTimeoutTime); |
| 2602 | alarmTime = Math.min(alarmTime, nextPeriodicEventElapsedTime); |
| 2603 | alarmTime = Math.min(alarmTime, nextPendingEventElapsedTime); |
| 2604 | |
| 2605 | // Bound the alarm time. |
| 2606 | final long now = SystemClock.elapsedRealtime(); |
| 2607 | if (alarmTime < now + SYNC_ALARM_TIMEOUT_MIN) { |
| 2608 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2609 | Log.v(TAG, "manageSyncAlarm: the alarmTime is too small, " |
| 2610 | + alarmTime + ", setting to " + (now + SYNC_ALARM_TIMEOUT_MIN)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2611 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2612 | alarmTime = now + SYNC_ALARM_TIMEOUT_MIN; |
| 2613 | } else if (alarmTime > now + SYNC_ALARM_TIMEOUT_MAX) { |
| 2614 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2615 | Log.v(TAG, "manageSyncAlarm: the alarmTime is too large, " |
| 2616 | + alarmTime + ", setting to " + (now + SYNC_ALARM_TIMEOUT_MIN)); |
| 2617 | } |
| 2618 | alarmTime = now + SYNC_ALARM_TIMEOUT_MAX; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2619 | } |
| 2620 | |
| 2621 | // determine if we need to set or cancel the alarm |
| 2622 | boolean shouldSet = false; |
| 2623 | boolean shouldCancel = false; |
| 2624 | final boolean alarmIsActive = mAlarmScheduleTime != null; |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2625 | final boolean needAlarm = alarmTime != Long.MAX_VALUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2626 | if (needAlarm) { |
| 2627 | if (!alarmIsActive || alarmTime < mAlarmScheduleTime) { |
| 2628 | shouldSet = true; |
| 2629 | } |
| 2630 | } else { |
| 2631 | shouldCancel = alarmIsActive; |
| 2632 | } |
| 2633 | |
| 2634 | // set or cancel the alarm as directed |
| 2635 | ensureAlarmService(); |
| 2636 | if (shouldSet) { |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2637 | if (Log.isLoggable(TAG, Log.VERBOSE)) { |
| 2638 | Log.v(TAG, "requesting that the alarm manager wake us up at elapsed time " |
| 2639 | + alarmTime + ", now is " + now + ", " + ((alarmTime - now) / 1000) |
| 2640 | + " secs from now"); |
| 2641 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2642 | mAlarmScheduleTime = alarmTime; |
| 2643 | mAlarmService.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTime, |
| 2644 | mSyncAlarmIntent); |
| 2645 | } else if (shouldCancel) { |
| 2646 | mAlarmScheduleTime = null; |
| 2647 | mAlarmService.cancel(mSyncAlarmIntent); |
| 2648 | } |
| 2649 | } |
| 2650 | |
| 2651 | private void sendSyncStateIntent() { |
| 2652 | Intent syncStateIntent = new Intent(Intent.ACTION_SYNC_STATE_CHANGED); |
Dianne Hackborn | a34f1ad | 2009-09-02 13:26:28 -0700 | [diff] [blame] | 2653 | syncStateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2654 | syncStateIntent.putExtra("active", mNeedSyncActiveNotification); |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2655 | syncStateIntent.putExtra("failing", false); |
Amith Yamasani | cd75706 | 2012-10-19 18:23:52 -0700 | [diff] [blame] | 2656 | mContext.sendBroadcastAsUser(syncStateIntent, UserHandle.OWNER); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2657 | } |
| 2658 | |
Fred Quintana | d9d2f11 | 2009-04-23 13:36:27 -0700 | [diff] [blame] | 2659 | private void installHandleTooManyDeletesNotification(Account account, String authority, |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2660 | long numDeletes, int userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2661 | if (mNotificationMgr == null) return; |
Fred Quintana | c848b70 | 2009-08-25 20:18:46 -0700 | [diff] [blame] | 2662 | |
| 2663 | final ProviderInfo providerInfo = mContext.getPackageManager().resolveContentProvider( |
| 2664 | authority, 0 /* flags */); |
| 2665 | if (providerInfo == null) { |
| 2666 | return; |
| 2667 | } |
| 2668 | CharSequence authorityName = providerInfo.loadLabel(mContext.getPackageManager()); |
| 2669 | |
Fred Quintana | b19e62a | 2010-12-16 13:54:43 -0800 | [diff] [blame] | 2670 | Intent clickIntent = new Intent(mContext, SyncActivityTooManyDeletes.class); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2671 | clickIntent.putExtra("account", account); |
Tadashi G. Takaoka | 86135d3 | 2009-09-24 19:31:44 -0700 | [diff] [blame] | 2672 | clickIntent.putExtra("authority", authority); |
Fred Quintana | c848b70 | 2009-08-25 20:18:46 -0700 | [diff] [blame] | 2673 | clickIntent.putExtra("provider", authorityName.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2674 | clickIntent.putExtra("numDeletes", numDeletes); |
| 2675 | |
| 2676 | if (!isActivityAvailable(clickIntent)) { |
| 2677 | Log.w(TAG, "No activity found to handle too many deletes."); |
| 2678 | return; |
| 2679 | } |
| 2680 | |
| 2681 | final PendingIntent pendingIntent = PendingIntent |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2682 | .getActivityAsUser(mContext, 0, clickIntent, |
| 2683 | PendingIntent.FLAG_CANCEL_CURRENT, null, new UserHandle(userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2684 | |
| 2685 | CharSequence tooManyDeletesDescFormat = mContext.getResources().getText( |
| 2686 | R.string.contentServiceTooManyDeletesNotificationDesc); |
| 2687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2688 | Notification notification = |
| 2689 | new Notification(R.drawable.stat_notify_sync_error, |
| 2690 | mContext.getString(R.string.contentServiceSync), |
| 2691 | System.currentTimeMillis()); |
| 2692 | notification.setLatestEventInfo(mContext, |
| 2693 | mContext.getString(R.string.contentServiceSyncNotificationTitle), |
Fred Quintana | c848b70 | 2009-08-25 20:18:46 -0700 | [diff] [blame] | 2694 | String.format(tooManyDeletesDescFormat.toString(), authorityName), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2695 | pendingIntent); |
| 2696 | notification.flags |= Notification.FLAG_ONGOING_EVENT; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 2697 | mNotificationMgr.notifyAsUser(null, account.hashCode() ^ authority.hashCode(), |
| 2698 | notification, new UserHandle(userId)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | /** |
| 2702 | * Checks whether an activity exists on the system image for the given intent. |
| 2703 | * |
| 2704 | * @param intent The intent for an activity. |
| 2705 | * @return Whether or not an activity exists. |
| 2706 | */ |
| 2707 | private boolean isActivityAvailable(Intent intent) { |
| 2708 | PackageManager pm = mContext.getPackageManager(); |
| 2709 | List<ResolveInfo> list = pm.queryIntentActivities(intent, 0); |
| 2710 | int listSize = list.size(); |
| 2711 | for (int i = 0; i < listSize; i++) { |
| 2712 | ResolveInfo resolveInfo = list.get(i); |
| 2713 | if ((resolveInfo.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) |
| 2714 | != 0) { |
| 2715 | return true; |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | return false; |
| 2720 | } |
| 2721 | |
| 2722 | public long insertStartSyncEvent(SyncOperation syncOperation) { |
| 2723 | final int source = syncOperation.syncSource; |
| 2724 | final long now = System.currentTimeMillis(); |
| 2725 | |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2726 | EventLog.writeEvent(2720, syncOperation.authority, |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2727 | SyncStorageEngine.EVENT_START, source, |
| 2728 | syncOperation.account.name.hashCode()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2729 | |
| 2730 | return mSyncStorageEngine.insertStartSyncEvent( |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2731 | syncOperation.account, syncOperation.userId, syncOperation.reason, |
| 2732 | syncOperation.authority, |
| 2733 | now, source, syncOperation.isInitialization(), syncOperation.extras |
| 2734 | ); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | } |
| 2736 | |
| 2737 | public void stopSyncEvent(long rowId, SyncOperation syncOperation, String resultMessage, |
| 2738 | int upstreamActivity, int downstreamActivity, long elapsedTime) { |
Dianne Hackborn | 231cc60 | 2009-04-27 17:10:36 -0700 | [diff] [blame] | 2739 | EventLog.writeEvent(2720, syncOperation.authority, |
Doug Zongker | 44f5747 | 2009-09-20 15:52:43 -0700 | [diff] [blame] | 2740 | SyncStorageEngine.EVENT_STOP, syncOperation.syncSource, |
| 2741 | syncOperation.account.name.hashCode()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2742 | |
Fred Quintana | 77c560f | 2010-03-29 22:20:26 -0700 | [diff] [blame] | 2743 | mSyncStorageEngine.stopSyncEvent(rowId, elapsedTime, |
Fred Quintana | c5d1c6d | 2010-01-27 12:17:49 -0800 | [diff] [blame] | 2744 | resultMessage, downstreamActivity, upstreamActivity); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2745 | } |
| 2746 | } |
Fred Quintana | 918339a | 2010-10-05 14:00:39 -0700 | [diff] [blame] | 2747 | |
| 2748 | private boolean isSyncStillActive(ActiveSyncContext activeSyncContext) { |
| 2749 | for (ActiveSyncContext sync : mActiveSyncContexts) { |
| 2750 | if (sync == activeSyncContext) { |
| 2751 | return true; |
| 2752 | } |
| 2753 | } |
| 2754 | return false; |
| 2755 | } |
Alon Albert | 57286f9 | 2012-10-09 14:21:38 -0700 | [diff] [blame] | 2756 | |
| 2757 | static class PrintTable { |
| 2758 | private ArrayList<Object[]> mTable = Lists.newArrayList(); |
| 2759 | private final int mCols; |
| 2760 | |
| 2761 | PrintTable(int cols) { |
| 2762 | mCols = cols; |
| 2763 | } |
| 2764 | |
| 2765 | void set(int row, int col, Object... values) { |
| 2766 | if (col + values.length > mCols) { |
| 2767 | throw new IndexOutOfBoundsException("Table only has " + mCols + |
| 2768 | " columns. can't set " + values.length + " at column " + col); |
| 2769 | } |
| 2770 | for (int i = mTable.size(); i <= row; i++) { |
| 2771 | final Object[] list = new Object[mCols]; |
| 2772 | mTable.add(list); |
| 2773 | for (int j = 0; j < mCols; j++) { |
| 2774 | list[j] = ""; |
| 2775 | } |
| 2776 | } |
| 2777 | System.arraycopy(values, 0, mTable.get(row), col, values.length); |
| 2778 | } |
| 2779 | |
| 2780 | void writeTo(PrintWriter out) { |
| 2781 | final String[] formats = new String[mCols]; |
| 2782 | int totalLength = 0; |
| 2783 | for (int col = 0; col < mCols; ++col) { |
| 2784 | int maxLength = 0; |
| 2785 | for (Object[] row : mTable) { |
| 2786 | final int length = row[col].toString().length(); |
| 2787 | if (length > maxLength) { |
| 2788 | maxLength = length; |
| 2789 | } |
| 2790 | } |
| 2791 | totalLength += maxLength; |
| 2792 | formats[col] = String.format("%%-%ds", maxLength); |
| 2793 | } |
| 2794 | printRow(out, formats, mTable.get(0)); |
| 2795 | totalLength += (mCols - 1) * 2; |
| 2796 | for (int i = 0; i < totalLength; ++i) { |
| 2797 | out.print("-"); |
| 2798 | } |
| 2799 | out.println(); |
| 2800 | for (int i = 1, mTableSize = mTable.size(); i < mTableSize; i++) { |
| 2801 | Object[] row = mTable.get(i); |
| 2802 | printRow(out, formats, row); |
| 2803 | } |
| 2804 | } |
| 2805 | |
| 2806 | private void printRow(PrintWriter out, String[] formats, Object[] row) { |
| 2807 | for (int j = 0, rowLength = row.length; j < rowLength; j++) { |
| 2808 | out.printf(String.format(formats[j], row[j].toString())); |
| 2809 | out.print(" "); |
| 2810 | } |
| 2811 | out.println(); |
| 2812 | } |
| 2813 | |
| 2814 | public int getNumRows() { |
| 2815 | return mTable.size(); |
| 2816 | } |
| 2817 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2818 | } |