Skip to content

Commit fcf0061

Browse files
alonalbertAndroid (Google) Code Review
authored andcommitted
Merge "Skip operations for users who are not running" into jb-mr1-dev
2 parents 0ff6537 + 8e28555 commit fcf0061

File tree

2 files changed

+65
-19
lines changed

2 files changed

+65
-19
lines changed

core/java/android/content/SyncManager.java

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616

1717
package android.content;
1818

19-
import com.android.internal.R;
20-
import com.google.android.collect.Lists;
21-
import com.google.android.collect.Maps;
22-
2319
import android.accounts.Account;
2420
import android.accounts.AccountAndUser;
2521
import android.accounts.AccountManager;
2622
import android.accounts.AccountManagerService;
2723
import android.accounts.OnAccountsUpdateListener;
2824
import android.app.ActivityManager;
2925
import android.app.AlarmManager;
30-
import android.app.AppGlobals;
3126
import android.app.Notification;
3227
import android.app.NotificationManager;
3328
import android.app.PendingIntent;
@@ -62,6 +57,11 @@
6257
import android.util.Log;
6358
import android.util.Pair;
6459

60+
import com.android.internal.R;
61+
import com.google.android.collect.Lists;
62+
import com.google.android.collect.Maps;
63+
import com.google.android.collect.Sets;
64+
6565
import java.io.FileDescriptor;
6666
import java.io.PrintWriter;
6767
import java.util.ArrayList;
@@ -75,6 +75,7 @@
7575
import java.util.List;
7676
import java.util.Map;
7777
import java.util.Random;
78+
import java.util.Set;
7879
import java.util.concurrent.CountDownLatch;
7980

8081
/**
@@ -151,7 +152,7 @@ public class SyncManager implements OnAccountsUpdateListener {
151152
private AlarmManager mAlarmService = null;
152153

153154
private SyncStorageEngine mSyncStorageEngine;
154-
public SyncQueue mSyncQueue;
155+
final public SyncQueue mSyncQueue;
155156

156157
protected final ArrayList<ActiveSyncContext> mActiveSyncContexts = Lists.newArrayList();
157158

@@ -328,7 +329,21 @@ public void onReceive(Context context, Intent intent) {
328329
private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() {
329330
@Override
330331
public void onReceive(Context context, Intent intent) {
331-
onUserRemoved(intent);
332+
String action = intent.getAction();
333+
final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
334+
if (Intent.ACTION_USER_REMOVED.equals(action)) {
335+
Log.i(TAG, "User removed - cleanup: u" + userId);
336+
onUserRemoved(intent);
337+
} else if (Intent.ACTION_USER_STARTED.equals(action)) {
338+
Log.i(TAG, "User started - check alarms: u" + userId);
339+
sendCheckAlarmsMessage();
340+
} else if (Intent.ACTION_USER_STOPPED.equals(action)) {
341+
Log.i(TAG, "User stopped - stop syncs: u" + userId);
342+
cancelActiveSync(
343+
null /* any account */,
344+
userId,
345+
null /* any authority */);
346+
}
332347
}
333348
};
334349

@@ -401,7 +416,9 @@ public void onServiceChanged(SyncAdapterType type, boolean removed) {
401416

402417
intentFilter = new IntentFilter();
403418
intentFilter.addAction(Intent.ACTION_USER_REMOVED);
404-
mContext.registerReceiver(mUserIntentReceiver, intentFilter);
419+
intentFilter.addAction(Intent.ACTION_USER_STARTED);
420+
mContext.registerReceiverAsUser(
421+
mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null);
405422

406423
if (!factoryTest) {
407424
mNotificationMgr = (NotificationManager)
@@ -897,7 +914,10 @@ void maybeRescheduleSync(SyncResult syncResult, SyncOperation operation) {
897914
private void onUserRemoved(Intent intent) {
898915
int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
899916
if (userId == -1) return;
917+
removeUser(userId);
918+
}
900919

920+
private void removeUser(int userId) {
901921
// Clean up the storage engine database
902922
mSyncStorageEngine.doDatabaseCleanup(new Account[0], userId);
903923
onAccountsUpdated(null);
@@ -1267,7 +1287,8 @@ private void dumpRecentHistory(PrintWriter pw) {
12671287
final String accountKey;
12681288
if (authority != null) {
12691289
authorityName = authority.authority;
1270-
accountKey = authority.account.name + "/" + authority.account.type;
1290+
accountKey = authority.account.name + "/" + authority.account.type
1291+
+ " u" + authority.userId;
12711292
} else {
12721293
authorityName = "Unknown";
12731294
accountKey = "Unknown";
@@ -1394,7 +1415,8 @@ public int compare(AccountSyncStats lhs, AccountSyncStats rhs) {
13941415
final String accountKey;
13951416
if (authority != null) {
13961417
authorityName = authority.authority;
1397-
accountKey = authority.account.name + "/" + authority.account.type;
1418+
accountKey = authority.account.name + "/" + authority.account.type
1419+
+ " u" + authority.userId;
13981420
} else {
13991421
authorityName = "Unknown";
14001422
accountKey = "Unknown";
@@ -1924,6 +1946,10 @@ private long maybeStartNextSyncLocked() {
19241946
}
19251947
Iterator<SyncOperation> operationIterator =
19261948
mSyncQueue.mOperationsMap.values().iterator();
1949+
1950+
final ActivityManager activityManager
1951+
= (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
1952+
final Set<Integer> removedUsers = Sets.newHashSet();
19271953
while (operationIterator.hasNext()) {
19281954
final SyncOperation op = operationIterator.next();
19291955

@@ -1943,6 +1969,15 @@ private long maybeStartNextSyncLocked() {
19431969
continue;
19441970
}
19451971

1972+
// if the user in not running, drop the request
1973+
if (!activityManager.isUserRunning(op.userId)) {
1974+
final UserInfo userInfo = mUserManager.getUserInfo(op.userId);
1975+
if (userInfo == null) {
1976+
removedUsers.add(op.userId);
1977+
}
1978+
continue;
1979+
}
1980+
19461981
// if the next run time is in the future, meaning there are no syncs ready
19471982
// to run, return the time
19481983
if (op.effectiveRunTime > now) {
@@ -1983,6 +2018,12 @@ private long maybeStartNextSyncLocked() {
19832018

19842019
operations.add(op);
19852020
}
2021+
for (Integer user : removedUsers) {
2022+
// if it's still removed
2023+
if (mUserManager.getUserInfo(user) == null) {
2024+
removeUser(user);
2025+
}
2026+
}
19862027
}
19872028

19882029
// find the next operation to dispatch, if one is ready
@@ -2168,13 +2209,13 @@ private void cancelActiveSyncLocked(Account account, int userId, String authorit
21682209
new ArrayList<ActiveSyncContext>(mActiveSyncContexts);
21692210
for (ActiveSyncContext activeSyncContext : activeSyncs) {
21702211
if (activeSyncContext != null) {
2171-
// if an authority was specified then only cancel the sync if it matches
2212+
// if an account was specified then only cancel the sync if it matches
21722213
if (account != null) {
21732214
if (!account.equals(activeSyncContext.mSyncOperation.account)) {
21742215
continue;
21752216
}
21762217
}
2177-
// if an account was specified then only cancel the sync if it matches
2218+
// if an authority was specified then only cancel the sync if it matches
21782219
if (authority != null) {
21792220
if (!authority.equals(activeSyncContext.mSyncOperation.authority)) {
21802221
continue;

core/java/android/content/SyncOperation.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,18 @@ public String toString() {
9595
}
9696

9797
public String dump(boolean useOneLine) {
98-
StringBuilder sb = new StringBuilder();
99-
sb.append(account.name);
100-
sb.append(" (" + account.type + ")");
101-
sb.append(", " + authority);
102-
sb.append(", ");
103-
sb.append(SyncStorageEngine.SOURCES[syncSource]);
104-
sb.append(", earliestRunTime " + earliestRunTime);
98+
StringBuilder sb = new StringBuilder()
99+
.append(account.name)
100+
.append(" u")
101+
.append(userId).append(" (")
102+
.append(account.type)
103+
.append(")")
104+
.append(", ")
105+
.append(authority)
106+
.append(", ")
107+
.append(SyncStorageEngine.SOURCES[syncSource])
108+
.append(", earliestRunTime ")
109+
.append(earliestRunTime);
105110
if (expedited) {
106111
sb.append(", EXPEDITED");
107112
}

0 commit comments

Comments
 (0)