expose some sync control methods

- ActiveSyncInfo
- ContentResolver.addStatusChangeListener
  - SYNC_OBSERVER_TYPE_SETTINGS
  - SYNC_OBSERVER_TYPE_PENDING
  - SYNC_OBSERVER_TYPE_ACTIVE
- make the ContentService resilient to nulls passed in to the
  status change listener registration and unregistration calls

bug http://b/issue?id=2337197
This commit is contained in:
Fred Quintana
2010-02-26 10:57:55 -08:00
parent f8219914f2
commit 1b487ec44b
6 changed files with 164 additions and 17 deletions

View File

@@ -20,13 +20,50 @@ import android.accounts.Account;
import android.os.Parcel;
import android.os.Parcelable.Creator;
/** @hide */
/**
* Information about the sync operation that is currently underway.
*/
public class ActiveSyncInfo {
public final int authorityId;
public final Account account;
public final String authority;
public final long startTime;
/** @hide */
private final int authorityId;
/** @hide */
private final Account account;
/** @hide */
private final String authority;
/** @hide */
private final long startTime;
/**
* Get the {@link Account} that is currently being synced.
* @return the account
*/
public Account getAccount() {
return new Account(account.name, account.type);
}
/** @hide */
public int getAuthorityId() {
return authorityId;
}
/**
* Get the authority of the provider that is currently being synced.
* @return the authority
*/
public String getAuthority() {
return authority;
}
/**
* Get the start time of the current sync operation. This is represented in elapsed real time.
* See {@link android.os.SystemClock#elapsedRealtime()}.
* @return the start time in milliseconds since boot
*/
public long getStartTime() {
return startTime;
}
/** @hide */
ActiveSyncInfo(int authorityId, Account account, String authority,
long startTime) {
this.authorityId = authorityId;
@@ -34,11 +71,13 @@ public class ActiveSyncInfo {
this.authority = authority;
this.startTime = startTime;
}
/** @hide */
public int describeContents() {
return 0;
}
/** @hide */
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeInt(authorityId);
account.writeToParcel(parcel, 0);
@@ -46,13 +85,15 @@ public class ActiveSyncInfo {
parcel.writeLong(startTime);
}
/** @hide */
ActiveSyncInfo(Parcel parcel) {
authorityId = parcel.readInt();
account = new Account(parcel);
authority = parcel.readString();
startTime = parcel.readLong();
}
/** @hide */
public static final Creator<ActiveSyncInfo> CREATOR = new Creator<ActiveSyncInfo>() {
public ActiveSyncInfo createFromParcel(Parcel in) {
return new ActiveSyncInfo(in);

View File

@@ -156,11 +156,8 @@ public abstract class ContentResolver {
/** @hide */
public static final int SYNC_ERROR_INTERNAL = 8;
/** @hide */
public static final int SYNC_OBSERVER_TYPE_SETTINGS = 1<<0;
/** @hide */
public static final int SYNC_OBSERVER_TYPE_PENDING = 1<<1;
/** @hide */
public static final int SYNC_OBSERVER_TYPE_ACTIVE = 1<<2;
/** @hide */
public static final int SYNC_OBSERVER_TYPE_STATUS = 1<<3;
@@ -1183,7 +1180,6 @@ public abstract class ContentResolver {
/**
* If a sync is active returns the information about it, otherwise returns false.
* @return the ActiveSyncInfo for the currently active sync or null if one is not active.
* @hide
*/
public static ActiveSyncInfo getActiveSync() {
try {
@@ -1222,7 +1218,24 @@ public abstract class ContentResolver {
}
}
/**
* Request notifications when the different aspects of the SyncManager change. The
* different items that can be requested are:
* <ul>
* <li> {@link #SYNC_OBSERVER_TYPE_PENDING}
* <li> {@link #SYNC_OBSERVER_TYPE_ACTIVE}
* <li> {@link #SYNC_OBSERVER_TYPE_SETTINGS}
* </ul>
* The caller can set one or more of the status types in the mask for any
* given listener registration.
* @param mask the status change types that will cause the callback to be invoked
* @param callback observer to be invoked when the status changes
* @return a handle that can be used to remove the listener at a later time
*/
public static Object addStatusChangeListener(int mask, final SyncStatusObserver callback) {
if (callback == null) {
throw new IllegalArgumentException("you passed in a null callback");
}
try {
ISyncStatusObserver.Stub observer = new ISyncStatusObserver.Stub() {
public void onStatusChanged(int which) throws RemoteException {
@@ -1236,7 +1249,14 @@ public abstract class ContentResolver {
}
}
/**
* Remove a previously registered status change listener.
* @param handle the handle that was returned by {@link #addStatusChangeListener}
*/
public static void removeStatusChangeListener(Object handle) {
if (handle == null) {
throw new IllegalArgumentException("you passed in a null handle");
}
try {
getContentService().removeStatusChangeListener((ISyncStatusObserver.Stub) handle);
} catch (RemoteException e) {

View File

@@ -437,7 +437,7 @@ public final class ContentService extends IContentService.Stub {
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager != null) {
if (syncManager != null && callback != null) {
syncManager.getSyncStorageEngine().addStatusChangeListener(mask, callback);
}
} finally {
@@ -449,7 +449,7 @@ public final class ContentService extends IContentService.Stub {
long identityToken = clearCallingIdentity();
try {
SyncManager syncManager = getSyncManager();
if (syncManager != null) {
if (syncManager != null && callback != null) {
syncManager.getSyncStorageEngine().removeStatusChangeListener(callback);
}
} finally {

View File

@@ -982,8 +982,8 @@ public class SyncManager implements OnAccountsUpdateListener {
ActiveSyncInfo active = mSyncStorageEngine.getActiveSync();
if (active != null) {
SyncStorageEngine.AuthorityInfo authority
= mSyncStorageEngine.getAuthority(active.authorityId);
final long durationInSeconds = (now - active.startTime) / 1000;
= mSyncStorageEngine.getAuthority(active.getAuthorityId());
final long durationInSeconds = (now - active.getStartTime()) / 1000;
pw.print("Active sync: ");
pw.print(authority != null ? authority.account : "<no account>");
pw.print(" ");

View File

@@ -664,7 +664,7 @@ public class SyncStorageEngine extends Handler {
}
if (mActiveSync != null) {
AuthorityInfo ainfo = getAuthority(mActiveSync.authorityId);
AuthorityInfo ainfo = getAuthority(mActiveSync.getAuthorityId());
if (ainfo != null && ainfo.account.equals(account)
&& ainfo.authority.equals(authority)) {
return true;