Skip to content

Commit 57286f9

Browse files
committed
Add sync reason to dumpsys
Also: * add a second history section that logs * log mesg as text instead of number * dump Sync Status as a table Sample log: Recent Sync History #1 : 2012-10-11 15:06:11 USER 0.4s aagmtest1@gmail.com/com.google u0 com.android.calendar com.google.android.calendar #2 : 2012-10-11 15:06:11 USER 0.1s aagmtest1@gmail.com/com.google u0 subscribedfeeds android.uid.system:1000 mesg=parse-error zsol#3 : 2012-10-11 15:06:11 USER 0.0s aagmtest1@gmail.com/com.google u0 com.google.android.apps.uploader.PicasaUploadProvider android.uid.system:1000 #4 : 2012-10-11 15:06:10 USER 0.1s aagmtest1@gmail.com/com.google u0 com.google.android.gms.plus.action android.uid.system:1000 Recent Sync History Extras #1 : 2012-10-11 15:06:11 USER aagmtest1@gmail.com/com.google u0 com.android.calendar Bundle[{feed=aagmtest1@gmail.com, force=true, ignore_settings=true, ignore_backoff=true}] #2 : 2012-10-11 15:06:11 USER aagmtest1@gmail.com/com.google u0 subscribedfeeds Bundle[{ignore_backoff=true, force=true, ignore_settings=true}] zsol#3 : 2012-10-11 15:06:11 USER aagmtest1@gmail.com/com.google u0 com.google.android.apps.uploader.PicasaUploadProvider Bundle[{ignore_backoff=true, force=true, ignore_settings=true}] #4 : 2012-10-11 15:06:10 USER aagmtest1@gmail.com/com.google u0 com.google.android.gms.plus.action Bundle[{ignore_backoff=true, force=true, ignore_settings=true}] Sync Status Account aagmtest1@gmail.com u0 com.google ======================================================================= Authority Syncable Enabled Delay Loc Poll Per Serv User Tot Time Last Sync Periodic ------------------------------------------------------------------------------------------------------------------------------------------------------------- com.google.android.apps.currents 1 true 0 2 1 2 1 6 0:35 PERIODIC SUCCESS 86400 2012-10-12 14:59:40 2012-10-13 14:58:13 com.google.android.music.MusicContent 1 true 0 0 1 2 1 4 0:09 PERIODIC SUCCESS 86400 2012-10-12 14:59:18 2012-10-13 14:58:13 com.google.android.gms.plus.action 1 true 0 0 1 1 1 3 0:00 PERIODIC SUCCESS 86400 2012-10-12 14:59:15 2012-10-13 14:58:13 com.google.android.apps.magazines 1 true 0 1 1 2 1 5 0:14 PERIODIC SUCCESS 86400 2012-10-12 14:59:00 2012-10-13 14:58:13 Change-Id: Iffeb825e4b4f6217940a39b0dd71e06856f08f3f
1 parent 32d7f8c commit 57286f9

File tree

8 files changed

+344
-108
lines changed

8 files changed

+344
-108
lines changed

core/java/android/content/ContentResolver.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ public abstract class ContentResolver {
169169
/** @hide */
170170
public static final int SYNC_ERROR_INTERNAL = 8;
171171

172+
private static final String[] SYNC_ERROR_NAMES = new String[] {
173+
"already-in-progress",
174+
"authentication-error",
175+
"io-error",
176+
"parse-error",
177+
"conflict",
178+
"too-many-deletions",
179+
"too-many-retries",
180+
"internal-error",
181+
};
182+
183+
/** @hide */
184+
static String syncErrorToString(int error) {
185+
if (error < 1 || error > SYNC_ERROR_NAMES.length) {
186+
return String.valueOf(error);
187+
}
188+
return SYNC_ERROR_NAMES[error - 1];
189+
}
190+
172191
public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
173192
public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
174193
public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;

core/java/android/content/ContentService.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ public void notifyChange(Uri uri, IContentObserver observer,
226226
}
227227
}
228228

229+
final int uid = Binder.getCallingUid();
229230
// This makes it so that future permission checks will be in the context of this
230231
// process rather than the caller's process. We will restore this before returning.
231232
long identityToken = clearCallingIdentity();
@@ -264,7 +265,7 @@ public void notifyChange(Uri uri, IContentObserver observer,
264265
if (syncToNetwork) {
265266
SyncManager syncManager = getSyncManager();
266267
if (syncManager != null) {
267-
syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle,
268+
syncManager.scheduleLocalSync(null /* all accounts */, callingUserHandle, uid,
268269
uri.getAuthority());
269270
}
270271
}
@@ -300,14 +301,15 @@ public static final class ObserverCall {
300301
public void requestSync(Account account, String authority, Bundle extras) {
301302
ContentResolver.validateSyncExtrasBundle(extras);
302303
int userId = UserHandle.getCallingUserId();
304+
int uId = Binder.getCallingUid();
303305

304306
// This makes it so that future permission checks will be in the context of this
305307
// process rather than the caller's process. We will restore this before returning.
306308
long identityToken = clearCallingIdentity();
307309
try {
308310
SyncManager syncManager = getSyncManager();
309311
if (syncManager != null) {
310-
syncManager.scheduleSync(account, userId, authority, extras, 0 /* no delay */,
312+
syncManager.scheduleSync(account, userId, uId, authority, extras, 0 /* no delay */,
311313
false /* onlyThoseWithUnkownSyncableState */);
312314
}
313315
} finally {

core/java/android/content/SyncManager.java

Lines changed: 219 additions & 80 deletions
Large diffs are not rendered by default.

core/java/android/content/SyncOperation.java

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package android.content;
1818

1919
import android.accounts.Account;
20+
import android.content.pm.PackageManager;
2021
import android.os.Bundle;
2122
import android.os.SystemClock;
2223

@@ -25,8 +26,29 @@
2526
* @hide
2627
*/
2728
public class SyncOperation implements Comparable {
29+
public static final int REASON_BACKGROUND_DATA_SETTINGS_CHANGED = -1;
30+
public static final int REASON_ACCOUNTS_UPDATED = -2;
31+
public static final int REASON_SERVICE_CHANGED = -3;
32+
public static final int REASON_PERIODIC = -4;
33+
public static final int REASON_IS_SYNCABLE = -5;
34+
public static final int REASON_SYNC_AUTO = -6;
35+
public static final int REASON_MASTER_SYNC_AUTO = -7;
36+
public static final int REASON_USER_START = -8;
37+
38+
private static String[] REASON_NAMES = new String[] {
39+
"DataSettingsChanged",
40+
"AccountsUpdated",
41+
"ServiceChanged",
42+
"Periodic",
43+
"IsSyncable",
44+
"AutoSync",
45+
"MasterSyncAuto",
46+
"UserStart",
47+
};
48+
2849
public final Account account;
2950
public final int userId;
51+
public final int reason;
3052
public int syncSource;
3153
public String authority;
3254
public final boolean allowParallelSyncs;
@@ -39,10 +61,12 @@ public class SyncOperation implements Comparable {
3961
public long delayUntil;
4062
public long effectiveRunTime;
4163

42-
public SyncOperation(Account account, int userId, int source, String authority, Bundle extras,
43-
long delayInMs, long backoff, long delayUntil, boolean allowParallelSyncs) {
64+
public SyncOperation(Account account, int userId, int reason, int source, String authority,
65+
Bundle extras, long delayInMs, long backoff, long delayUntil,
66+
boolean allowParallelSyncs) {
4467
this.account = account;
4568
this.userId = userId;
69+
this.reason = reason;
4670
this.syncSource = source;
4771
this.authority = authority;
4872
this.allowParallelSyncs = allowParallelSyncs;
@@ -78,6 +102,7 @@ private void removeFalseExtra(String extraName) {
78102
SyncOperation(SyncOperation other) {
79103
this.account = other.account;
80104
this.userId = other.userId;
105+
this.reason = other.reason;
81106
this.syncSource = other.syncSource;
82107
this.authority = other.authority;
83108
this.extras = new Bundle(other.extras);
@@ -91,10 +116,10 @@ private void removeFalseExtra(String extraName) {
91116
}
92117

93118
public String toString() {
94-
return dump(true);
119+
return dump(null, true);
95120
}
96121

97-
public String dump(boolean useOneLine) {
122+
public String dump(PackageManager pm, boolean useOneLine) {
98123
StringBuilder sb = new StringBuilder()
99124
.append(account.name)
100125
.append(" u")
@@ -110,13 +135,40 @@ public String dump(boolean useOneLine) {
110135
if (expedited) {
111136
sb.append(", EXPEDITED");
112137
}
138+
sb.append(", reason: ");
139+
sb.append(reasonToString(pm, reason));
113140
if (!useOneLine && !extras.keySet().isEmpty()) {
114141
sb.append("\n ");
115142
extrasToStringBuilder(extras, sb);
116143
}
117144
return sb.toString();
118145
}
119146

147+
public static String reasonToString(PackageManager pm, int reason) {
148+
if (reason >= 0) {
149+
if (pm != null) {
150+
final String[] packages = pm.getPackagesForUid(reason);
151+
if (packages != null && packages.length == 1) {
152+
return packages[0];
153+
}
154+
final String name = pm.getNameForUid(reason);
155+
if (name != null) {
156+
return name;
157+
}
158+
return String.valueOf(reason);
159+
} else {
160+
return String.valueOf(reason);
161+
}
162+
} else {
163+
final int index = -reason - 1;
164+
if (index >= REASON_NAMES.length) {
165+
return String.valueOf(reason);
166+
} else {
167+
return REASON_NAMES[index];
168+
}
169+
}
170+
}
171+
120172
public boolean isInitialization() {
121173
return extras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false);
122174
}

core/java/android/content/SyncQueue.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package android.content;
1818

1919
import android.accounts.Account;
20+
import android.content.pm.PackageManager;
21+
import android.content.pm.RegisteredServicesCache;
2022
import android.content.pm.RegisteredServicesCache.ServiceInfo;
2123
import android.os.SystemClock;
2224
import android.os.UserHandle;
@@ -40,15 +42,17 @@
4042
*/
4143
public class SyncQueue {
4244
private static final String TAG = "SyncManager";
43-
4445
private final SyncStorageEngine mSyncStorageEngine;
4546
private final SyncAdaptersCache mSyncAdapters;
47+
private final PackageManager mPackageManager;
4648

4749
// A Map of SyncOperations operationKey -> SyncOperation that is designed for
4850
// quick lookup of an enqueued SyncOperation.
4951
private final HashMap<String, SyncOperation> mOperationsMap = Maps.newHashMap();
5052

51-
public SyncQueue(SyncStorageEngine syncStorageEngine, final SyncAdaptersCache syncAdapters) {
53+
public SyncQueue(PackageManager packageManager, SyncStorageEngine syncStorageEngine,
54+
final SyncAdaptersCache syncAdapters) {
55+
mPackageManager = packageManager;
5256
mSyncStorageEngine = syncStorageEngine;
5357
mSyncAdapters = syncAdapters;
5458
}
@@ -67,8 +71,8 @@ public void addPendingOperations(int userId) {
6771
continue;
6872
}
6973
SyncOperation syncOperation = new SyncOperation(
70-
op.account, op.userId, op.syncSource, op.authority, op.extras, 0 /* delay */,
71-
backoff != null ? backoff.first : 0,
74+
op.account, op.userId, op.reason, op.syncSource, op.authority, op.extras,
75+
0 /* delay */, backoff != null ? backoff.first : 0,
7276
mSyncStorageEngine.getDelayUntilTime(op.account, op.userId, op.authority),
7377
syncAdapterInfo.type.allowParallelSyncs());
7478
syncOperation.expedited = op.expedited;
@@ -112,7 +116,7 @@ private boolean add(SyncOperation operation,
112116
operation.pendingOperation = pop;
113117
if (operation.pendingOperation == null) {
114118
pop = new SyncStorageEngine.PendingOperation(
115-
operation.account, operation.userId, operation.syncSource,
119+
operation.account, operation.userId, operation.reason, operation.syncSource,
116120
operation.authority, operation.extras, operation.expedited);
117121
pop = mSyncStorageEngine.insertIntoPending(pop);
118122
if (pop == null) {
@@ -214,7 +218,7 @@ public void dump(StringBuilder sb) {
214218
sb.append(DateUtils.formatElapsedTime((operation.effectiveRunTime - now) / 1000));
215219
}
216220
sb.append(" - ");
217-
sb.append(operation.dump(false)).append("\n");
221+
sb.append(operation.dump(mPackageManager, false)).append("\n");
218222
}
219223
}
220224
}

0 commit comments

Comments
 (0)