diff options
author | Matthew Williams <mjwilliams@google.com> | 2014-06-04 09:25:11 -0700 |
---|---|---|
committer | Matthew Williams <mjwilliams@google.com> | 2014-06-04 14:55:53 -0700 |
commit | 5a9decd589f3f6a512168fd669ee2c5d8daa238b (patch) | |
tree | fe49786bcf13ee82b786a68f5a27eff8778c5421 /core/java/android/content | |
parent | 676f92e9a77a547b80a3a22bb3d4e4d0d2aa0bed (diff) | |
download | frameworks_base-5a9decd589f3f6a512168fd669ee2c5d8daa238b.zip frameworks_base-5a9decd589f3f6a512168fd669ee2c5d8daa238b.tar.gz frameworks_base-5a9decd589f3f6a512168fd669ee2c5d8daa238b.tar.bz2 |
Redact SyncService stuff from master
This changes the API surface area, deleting things we aren't releasing.
I'll do the internal clean-up at the same time I do the SyncManager
refactor to sit on top of the TaskManager.
Bug: 14997851
Change-Id: Ibefbb246f0e98d3159399151744279902468a23c
Diffstat (limited to 'core/java/android/content')
-rw-r--r-- | core/java/android/content/ContentResolver.java | 92 | ||||
-rw-r--r-- | core/java/android/content/IContentService.aidl | 13 | ||||
-rw-r--r-- | core/java/android/content/PeriodicSync.java | 73 | ||||
-rw-r--r-- | core/java/android/content/SyncInfo.java | 20 | ||||
-rw-r--r-- | core/java/android/content/SyncRequest.java | 137 | ||||
-rw-r--r-- | core/java/android/content/SyncService.java | 211 |
6 files changed, 32 insertions, 514 deletions
diff --git a/core/java/android/content/ContentResolver.java b/core/java/android/content/ContentResolver.java index 7642e13..392bfbc 100644 --- a/core/java/android/content/ContentResolver.java +++ b/core/java/android/content/ContentResolver.java @@ -1839,19 +1839,6 @@ public abstract class ContentResolver { } /** - * Cancel any active or pending syncs that are running on this service. - * - * @param cname the service for which to cancel all active/pending operations. - */ - public static void cancelSync(ComponentName cname) { - try { - getContentService().cancelSync(null, null, cname); - } catch (RemoteException e) { - - } - } - - /** * Get information about the SyncAdapters that are known to the system. * @return an array of SyncAdapters that have registered with the system */ @@ -1991,13 +1978,13 @@ public abstract class ContentResolver { /** * Remove the specified sync. This will cancel any pending or active syncs. If the request is * for a periodic sync, this call will remove any future occurrences. - * <p>If a periodic sync is specified, the caller must hold the permission - * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. If this SyncRequest targets a - * SyncService adapter,the calling application must be signed with the same certificate as the - * adapter. - *</p>It is possible to cancel a sync using a SyncRequest object that is not the same object + * <p> + * If a periodic sync is specified, the caller must hold the permission + * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. + *</p> + * It is possible to cancel a sync using a SyncRequest object that is not the same object * with which you requested the sync. Do so by building a SyncRequest with the same - * service/adapter, frequency, <b>and</b> extras bundle. + * adapter, frequency, <b>and</b> extras bundle. * * @param request SyncRequest object containing information about sync to cancel. */ @@ -2031,22 +2018,6 @@ public abstract class ContentResolver { } /** - * Return periodic syncs associated with the provided component. - * <p>The calling application must be signed with the same certificate as the target component, - * otherwise this call will fail. - */ - public static List<PeriodicSync> getPeriodicSyncs(ComponentName cname) { - if (cname == null) { - throw new IllegalArgumentException("Component must not be null"); - } - try { - return getContentService().getPeriodicSyncs(null, null, cname); - } catch (RemoteException e) { - throw new RuntimeException("the ContentService should always be reachable", e); - } - } - - /** * Check if this account/provider is syncable. * <p>This method requires the caller to hold the permission * {@link android.Manifest.permission#READ_SYNC_SETTINGS}. @@ -2076,38 +2047,6 @@ public abstract class ContentResolver { } /** - * Set whether the provided {@link SyncService} is available to process work. - * <p>This method requires the caller to hold the permission - * {@link android.Manifest.permission#WRITE_SYNC_SETTINGS}. - * <p>The calling application must be signed with the same certificate as the target component, - * otherwise this call will fail. - */ - public static void setServiceActive(ComponentName cname, boolean active) { - try { - getContentService().setServiceActive(cname, active); - } catch (RemoteException e) { - // exception ignored; if this is thrown then it means the runtime is in the midst of - // being restarted - } - } - - /** - * Query the state of this sync service. - * <p>Set with {@link #setServiceActive(ComponentName cname, boolean active)}. - * <p>The calling application must be signed with the same certificate as the target component, - * otherwise this call will fail. - * @param cname ComponentName referring to a {@link SyncService} - * @return true if jobs will be run on this service, false otherwise. - */ - public static boolean isServiceActive(ComponentName cname) { - try { - return getContentService().isServiceActive(cname); - } catch (RemoteException e) { - throw new RuntimeException("the ContentService should always be reachable", e); - } - } - - /** * Gets the master auto-sync setting that applies to all the providers and accounts. * If this is false then the per-provider auto-sync setting is ignored. * <p>This method requires the caller to hold the permission @@ -2164,17 +2103,6 @@ public abstract class ContentResolver { } } - public static boolean isSyncActive(ComponentName cname) { - if (cname == null) { - throw new IllegalArgumentException("component name must not be null"); - } - try { - return getContentService().isSyncActive(null, null, cname); - } catch (RemoteException e) { - throw new RuntimeException("the ContentService should always be reachable", e); - } - } - /** * If a sync is active returns the information about it, otherwise returns null. * <p> @@ -2249,14 +2177,6 @@ public abstract class ContentResolver { } } - public static boolean isSyncPending(ComponentName cname) { - try { - return getContentService().isSyncPending(null, null, cname); - } catch (RemoteException e) { - throw new RuntimeException("the ContentService should always be reachable", e); - } - } - /** * Request notifications when the different aspects of the SyncManager change. The * different items that can be requested are: diff --git a/core/java/android/content/IContentService.aidl b/core/java/android/content/IContentService.aidl index 73a76e8..373f2fb 100644 --- a/core/java/android/content/IContentService.aidl +++ b/core/java/android/content/IContentService.aidl @@ -121,19 +121,6 @@ interface IContentService { */ void setIsSyncable(in Account account, String providerName, int syncable); - /** - * Corresponds roughly to setIsSyncable(String account, String provider) for syncs that bind - * to a SyncService. - */ - void setServiceActive(in ComponentName cname, boolean active); - - /** - * Corresponds roughly to getIsSyncable(String account, String provider) for syncs that bind - * to a SyncService. - * @return 0 if this SyncService is not enabled, 1 if enabled, <0 if unknown. - */ - boolean isServiceActive(in ComponentName cname); - void setMasterSyncAutomatically(boolean flag); boolean getMasterSyncAutomatically(); diff --git a/core/java/android/content/PeriodicSync.java b/core/java/android/content/PeriodicSync.java index 836c6f8..3efd89a 100644 --- a/core/java/android/content/PeriodicSync.java +++ b/core/java/android/content/PeriodicSync.java @@ -29,14 +29,10 @@ public class PeriodicSync implements Parcelable { public final Account account; /** The authority of the sync. Can be null. */ public final String authority; - /** The service for syncing, if this is an anonymous sync. Can be null.*/ - public final ComponentName service; /** Any extras that parameters that are to be passed to the sync adapter. */ public final Bundle extras; /** How frequently the sync should be scheduled, in seconds. Kept around for API purposes. */ public final long period; - /** Whether this periodic sync runs on a {@link SyncService}. */ - public final boolean isService; /** * How much flexibility can be taken in scheduling the sync, in seconds. * {@hide} @@ -44,16 +40,11 @@ public class PeriodicSync implements Parcelable { public final long flexTime; /** - * Creates a new PeriodicSync, copying the Bundle. SM no longer uses this ctor - kept around - * becuse it is part of the API. - * Note - even calls to the old API will not use this ctor, as - * they are given a default flex time. + * Creates a new PeriodicSync, copying the Bundle. This constructor is no longer used. */ public PeriodicSync(Account account, String authority, Bundle extras, long periodInSeconds) { this.account = account; this.authority = authority; - this.service = null; - this.isService = false; if (extras == null) { this.extras = new Bundle(); } else { @@ -71,8 +62,6 @@ public class PeriodicSync implements Parcelable { public PeriodicSync(PeriodicSync other) { this.account = other.account; this.authority = other.authority; - this.service = other.service; - this.isService = other.isService; this.extras = new Bundle(other.extras); this.period = other.period; this.flexTime = other.flexTime; @@ -86,40 +75,14 @@ public class PeriodicSync implements Parcelable { long period, long flexTime) { this.account = account; this.authority = authority; - this.service = null; - this.isService = false; - this.extras = new Bundle(extras); - this.period = period; - this.flexTime = flexTime; - } - - /** - * A PeriodicSync for a sync with a specified SyncService. - * {@hide} - */ - public PeriodicSync(ComponentName service, Bundle extras, - long period, - long flexTime) { - this.account = null; - this.authority = null; - this.service = service; - this.isService = true; this.extras = new Bundle(extras); this.period = period; this.flexTime = flexTime; } private PeriodicSync(Parcel in) { - this.isService = (in.readInt() != 0); - if (this.isService) { - this.service = in.readParcelable(null); - this.account = null; - this.authority = null; - } else { - this.account = in.readParcelable(null); - this.authority = in.readString(); - this.service = null; - } + this.account = in.readParcelable(null); + this.authority = in.readString(); this.extras = in.readBundle(); this.period = in.readLong(); this.flexTime = in.readLong(); @@ -132,13 +95,8 @@ public class PeriodicSync implements Parcelable { @Override public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(isService ? 1 : 0); - if (account == null && authority == null) { - dest.writeParcelable(service, flags); - } else { - dest.writeParcelable(account, flags); - dest.writeString(authority); - } + dest.writeParcelable(account, flags); + dest.writeString(authority); dest.writeBundle(extras); dest.writeLong(period); dest.writeLong(flexTime); @@ -165,24 +123,14 @@ public class PeriodicSync implements Parcelable { return false; } final PeriodicSync other = (PeriodicSync) o; - if (this.isService != other.isService) { - return false; - } - boolean equal = false; - if (this.isService) { - equal = service.equals(other.service); - } else { - equal = account.equals(other.account) - && authority.equals(other.authority); - } - return equal - && period == other.period - && syncExtrasEquals(extras, other.extras); + return account.equals(other.account) + && authority.equals(other.authority) + && period == other.period + && syncExtrasEquals(extras, other.extras); } /** - * Periodic sync extra comparison function. Duplicated from - * {@link com.android.server.content.SyncManager#syncExtrasEquals(Bundle b1, Bundle b2)} + * Periodic sync extra comparison function. * {@hide} */ public static boolean syncExtrasEquals(Bundle b1, Bundle b2) { @@ -207,7 +155,6 @@ public class PeriodicSync implements Parcelable { public String toString() { return "account: " + account + ", authority: " + authority + - ", service: " + service + ". period: " + period + "s " + ", flex: " + flexTime; } diff --git a/core/java/android/content/SyncInfo.java b/core/java/android/content/SyncInfo.java index 146dd99..a586d6f 100644 --- a/core/java/android/content/SyncInfo.java +++ b/core/java/android/content/SyncInfo.java @@ -28,24 +28,16 @@ public class SyncInfo implements Parcelable { public final int authorityId; /** - * The {@link Account} that is currently being synced. Will be null if this sync is running via - * a {@link SyncService}. + * The {@link Account} that is currently being synced. */ public final Account account; /** - * The authority of the provider that is currently being synced. Will be null if this sync - * is running via a {@link SyncService}. + * The authority of the provider that is currently being synced. */ public final String authority; /** - * The {@link SyncService} that is targeted by this operation. Null if this sync is running via - * a {@link AbstractThreadedSyncAdapter}. - */ - public final ComponentName service; - - /** * The start time of the current sync operation in milliseconds since boot. * This is represented in elapsed real time. * See {@link android.os.SystemClock#elapsedRealtime()}. @@ -53,13 +45,11 @@ public class SyncInfo implements Parcelable { public final long startTime; /** @hide */ - public SyncInfo(int authorityId, Account account, String authority, ComponentName service, - long startTime) { + public SyncInfo(int authorityId, Account account, String authority, long startTime) { this.authorityId = authorityId; this.account = account; this.authority = authority; this.startTime = startTime; - this.service = service; } /** @hide */ @@ -68,7 +58,6 @@ public class SyncInfo implements Parcelable { this.account = new Account(other.account.name, other.account.type); this.authority = other.authority; this.startTime = other.startTime; - this.service = other.service; } /** @hide */ @@ -82,8 +71,6 @@ public class SyncInfo implements Parcelable { parcel.writeParcelable(account, flags); parcel.writeString(authority); parcel.writeLong(startTime); - parcel.writeParcelable(service, flags); - } /** @hide */ @@ -92,7 +79,6 @@ public class SyncInfo implements Parcelable { account = parcel.readParcelable(Account.class.getClassLoader()); authority = parcel.readString(); startTime = parcel.readLong(); - service = parcel.readParcelable(ComponentName.class.getClassLoader()); } /** @hide */ diff --git a/core/java/android/content/SyncRequest.java b/core/java/android/content/SyncRequest.java index 9ba45ca..869f85c 100644 --- a/core/java/android/content/SyncRequest.java +++ b/core/java/android/content/SyncRequest.java @@ -27,23 +27,11 @@ public class SyncRequest implements Parcelable { private final Account mAccountToSync; /** Authority string that corresponds to a ContentProvider. */ private final String mAuthority; - /** {@link SyncService} identifier. */ - private final ComponentName mComponentInfo; /** Bundle containing user info as well as sync settings. */ private final Bundle mExtras; /** Don't allow this sync request on metered networks. */ private final boolean mDisallowMetered; /** - * Anticipated upload size in bytes. - * TODO: Not yet used - we put this information into the bundle for simplicity. - */ - private final long mTxBytes; - /** - * Anticipated download size in bytes. - * TODO: Not yet used - we put this information into the bundle. - */ - private final long mRxBytes; - /** * Amount of time before {@link #mSyncRunTimeSecs} from which the sync may optionally be * started. */ @@ -75,25 +63,12 @@ public class SyncRequest implements Parcelable { /** * {@hide} - * @return true if this sync uses an account/authority pair, or false if - * this is an anonymous sync bound to an @link AnonymousSyncService. - */ - public boolean hasAuthority() { - return mIsAuthority; - } - - /** - * {@hide} * * @return account object for this sync. * @throws IllegalArgumentException if this function is called for a request that targets a * sync service. */ public Account getAccount() { - if (!hasAuthority()) { - throw new IllegalArgumentException("Cannot getAccount() for a sync that targets a sync" - + "service."); - } return mAccountToSync; } @@ -105,30 +80,11 @@ public class SyncRequest implements Parcelable { * sync service. */ public String getProvider() { - if (!hasAuthority()) { - throw new IllegalArgumentException("Cannot getProvider() for a sync that targets a" - + "sync service."); - } return mAuthority; } /** * {@hide} - * Throws a runtime IllegalArgumentException if this function is called for a - * SyncRequest that is bound to an account/provider. - * - * @return ComponentName for the service that this sync will bind to. - */ - public ComponentName getService() { - if (hasAuthority()) { - throw new IllegalArgumentException( - "Cannot getAnonymousService() for a sync that has specified a provider."); - } - return mComponentInfo; - } - - /** - * {@hide} * Retrieve bundle for this SyncRequest. Will not be null. */ public Bundle getBundle() { @@ -175,16 +131,10 @@ public class SyncRequest implements Parcelable { parcel.writeLong(mSyncRunTimeSecs); parcel.writeInt((mIsPeriodic ? 1 : 0)); parcel.writeInt((mDisallowMetered ? 1 : 0)); - parcel.writeLong(mTxBytes); - parcel.writeLong(mRxBytes); parcel.writeInt((mIsAuthority ? 1 : 0)); parcel.writeInt((mIsExpedited? 1 : 0)); - if (mIsAuthority) { - parcel.writeParcelable(mAccountToSync, flags); - parcel.writeString(mAuthority); - } else { - parcel.writeParcelable(mComponentInfo, flags); - } + parcel.writeParcelable(mAccountToSync, flags); + parcel.writeString(mAuthority); } private SyncRequest(Parcel in) { @@ -193,19 +143,10 @@ public class SyncRequest implements Parcelable { mSyncRunTimeSecs = in.readLong(); mIsPeriodic = (in.readInt() != 0); mDisallowMetered = (in.readInt() != 0); - mTxBytes = in.readLong(); - mRxBytes = in.readLong(); mIsAuthority = (in.readInt() != 0); mIsExpedited = (in.readInt() != 0); - if (mIsAuthority) { - mComponentInfo = null; - mAccountToSync = in.readParcelable(null); - mAuthority = in.readString(); - } else { - mComponentInfo = in.readParcelable(null); - mAccountToSync = null; - mAuthority = null; - } + mAccountToSync = in.readParcelable(null); + mAuthority = in.readString(); } /** {@hide} Protected ctor to instantiate anonymous SyncRequest. */ @@ -214,7 +155,6 @@ public class SyncRequest implements Parcelable { mSyncRunTimeSecs = b.mSyncRunTimeSecs; mAccountToSync = b.mAccount; mAuthority = b.mAuthority; - mComponentInfo = b.mComponentName; mIsPeriodic = (b.mSyncType == Builder.SYNC_TYPE_PERIODIC); mIsAuthority = (b.mSyncTarget == Builder.SYNC_TARGET_ADAPTER); mIsExpedited = b.mExpedited; @@ -223,8 +163,6 @@ public class SyncRequest implements Parcelable { // TODO: pass the configuration extras through separately. mExtras.putAll(b.mSyncConfigExtras); mDisallowMetered = b.mDisallowMetered; - mTxBytes = b.mTxBytes; - mRxBytes = b.mRxBytes; } /** @@ -240,8 +178,6 @@ public class SyncRequest implements Parcelable { private static final int SYNC_TYPE_ONCE = 2; /** Unknown sync target. */ private static final int SYNC_TARGET_UNKNOWN = 0; - /** Specify that this is an anonymous sync. */ - private static final int SYNC_TARGET_SERVICE = 1; /** Specify that this is a sync with a provider. */ private static final int SYNC_TARGET_ADAPTER = 2; /** @@ -275,7 +211,7 @@ public class SyncRequest implements Parcelable { * Whether this builder is building a periodic sync, or a one-time sync. */ private int mSyncType = SYNC_TYPE_UNKNOWN; - /** Whether this will go to a sync adapter or to a sync service. */ + /** Whether this will go to a sync adapter. */ private int mSyncTarget = SYNC_TARGET_UNKNOWN; /** Whether this is a user-activated sync. */ private boolean mIsManual; @@ -298,12 +234,6 @@ public class SyncRequest implements Parcelable { private boolean mExpedited; /** - * The {@link SyncService} component that - * contains the sync logic if this is a provider-less sync, otherwise - * null. - */ - private ComponentName mComponentName; - /** * The Account object that together with an Authority name define the SyncAdapter (if * this sync is bound to a provider), otherwise null. */ @@ -336,7 +266,7 @@ public class SyncRequest implements Parcelable { /** * Build a periodic sync. Either this or syncOnce() <b>must</b> be called for this builder. - * Syncs are identified by target {@link SyncService}/{@link android.provider} and by the + * Syncs are identified by target {@link android.provider} and by the * contents of the extras bundle. * You cannot reuse the same builder for one-time syncs after having specified a periodic * sync (by calling this function). If you do, an <code>IllegalArgumentException</code> @@ -395,23 +325,10 @@ public class SyncRequest implements Parcelable { } /** - * Developer can provide insight into their payload size; optional. -1 specifies unknown, - * so that you are not restricted to defining both fields. - * - * @param rxBytes Bytes expected to be downloaded. - * @param txBytes Bytes expected to be uploaded. - */ - public Builder setTransferSize(long rxBytes, long txBytes) { - mRxBytes = rxBytes; - mTxBytes = txBytes; - return this; - } - - /** * Will throw an <code>IllegalArgumentException</code> if called and * {@link #setIgnoreSettings(boolean ignoreSettings)} has already been called. * @param disallow true to allow this transfer on metered networks. Default false. - * + * */ public Builder setDisallowMetered(boolean disallow) { if (mIgnoreSettings && disallow) { @@ -423,10 +340,9 @@ public class SyncRequest implements Parcelable { } /** - * Specify an authority and account for this transfer. Cannot be used with - * {@link #setSyncAdapter(ComponentName cname)}. + * Specify an authority and account for this transfer. * - * @param authority + * @param authority A String identifying the content provider to be synced. * @param account Account to sync. Can be null unless this is a periodic * sync, for which verification by the ContentResolver will * fail. If a sync is performed without an account, the @@ -441,25 +357,6 @@ public class SyncRequest implements Parcelable { mSyncTarget = SYNC_TARGET_ADAPTER; mAccount = account; mAuthority = authority; - mComponentName = null; - return this; - } - - /** - * Specify the {@link SyncService} component for this sync. This is not validated until - * sync time so providing an incorrect component name here will not fail. Cannot be used - * with {@link #setSyncAdapter(Account account, String authority)}. - * - * @param cname ComponentName to identify your Anonymous service - */ - public Builder setSyncAdapter(ComponentName cname) { - if (mSyncTarget != SYNC_TARGET_UNKNOWN) { - throw new IllegalArgumentException("Sync target has already been defined."); - } - mSyncTarget = SYNC_TARGET_SERVICE; - mComponentName = cname; - mAccount = null; - mAuthority = null; return this; } @@ -630,25 +527,17 @@ public class SyncRequest implements Parcelable { mSyncConfigExtras.putInt(ContentResolver.SYNC_EXTRAS_PRIORITY, mPriority); if (mSyncType == SYNC_TYPE_PERIODIC) { // If this is a periodic sync ensure than invalid extras were not set. - if (ContentResolver.invalidPeriodicExtras(mCustomExtras) || + if (ContentResolver.invalidPeriodicExtras(mCustomExtras) || ContentResolver.invalidPeriodicExtras(mSyncConfigExtras)) { throw new IllegalArgumentException("Illegal extras were set"); } - } else if (mSyncType == SYNC_TYPE_UNKNOWN) { - throw new IllegalArgumentException("Must call either syncOnce() or syncPeriodic()"); - } - if (mSyncTarget == SYNC_TARGET_SERVICE) { - if (mSyncConfigExtras.getBoolean(ContentResolver.SYNC_EXTRAS_INITIALIZE, false)) { - throw new IllegalArgumentException("Cannot specify an initialisation sync" - + " that targets a service."); - } } // Ensure that a target for the sync has been set. if (mSyncTarget == SYNC_TARGET_UNKNOWN) { - throw new IllegalArgumentException("Must specify an adapter with one of" - + "setSyncAdapter(ComponentName) or setSyncAdapter(Account, String"); + throw new IllegalArgumentException("Must specify an adapter with" + + " setSyncAdapter(Account, String"); } return new SyncRequest(this); } - } + } } diff --git a/core/java/android/content/SyncService.java b/core/java/android/content/SyncService.java deleted file mode 100644 index 4df998c..0000000 --- a/core/java/android/content/SyncService.java +++ /dev/null @@ -1,211 +0,0 @@ -/* - * Copyright (C) 2013 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package android.content; - -import android.app.Service; -import android.os.Bundle; -import android.os.IBinder; -import android.os.Process; -import android.os.Trace; -import android.util.SparseArray; -import android.util.Log; - -import com.android.internal.annotations.GuardedBy; - -/** - * Simplified @link android.content.AbstractThreadedSyncAdapter. Folds that - * behaviour into a service to which the system can bind when requesting an - * anonymous (providerless/accountless) sync. - * <p> - * In order to perform an anonymous sync operation you must extend this service, implementing the - * abstract methods. This service must be declared in the application's manifest as usual. You - * can use this service for other work, however you <b> must not </b> override the onBind() method - * unless you know what you're doing, which limits the usefulness of this service for other work. - * <p>A {@link SyncService} can either be active or inactive. Different to an - * {@link AbstractThreadedSyncAdapter}, there is no - * {@link ContentResolver#setSyncAutomatically(android.accounts.Account account, String provider, boolean sync)}, - * as well as no concept of initialisation (you can handle your own if needed). - * - * <pre> - * <service android:name=".MySyncService"/> - * </pre> - * Like @link android.content.AbstractThreadedSyncAdapter this service supports - * multiple syncs at the same time. Each incoming startSync() with a unique tag - * will spawn a thread to do the work of that sync. If startSync() is called - * with a tag that already exists, a SyncResult.ALREADY_IN_PROGRESS is returned. - * Remember that your service will spawn multiple threads if you schedule multiple syncs - * at once, so if you mutate local objects you must ensure synchronization. - */ -public abstract class SyncService extends Service { - private static final String TAG = "SyncService"; - - private final SyncAdapterImpl mSyncAdapter = new SyncAdapterImpl(); - - /** Keep track of on-going syncs, keyed by bundle. */ - @GuardedBy("mSyncThreadLock") - private final SparseArray<SyncThread> - mSyncThreads = new SparseArray<SyncThread>(); - /** Lock object for accessing the SyncThreads HashMap. */ - private final Object mSyncThreadLock = new Object(); - /** - * Default key for if this sync service does not support parallel operations. Currently not - * sure if null keys will make it into the ArrayMap for KLP, so keeping our default for now. - */ - private static final int KEY_DEFAULT = 0; - /** Identifier for this sync service. */ - private ComponentName mServiceComponent; - - /** {@hide} */ - public IBinder onBind(Intent intent) { - mServiceComponent = new ComponentName(this, getClass()); - return mSyncAdapter.asBinder(); - } - - /** {@hide} */ - private class SyncAdapterImpl extends ISyncServiceAdapter.Stub { - @Override - public void startSync(ISyncContext syncContext, Bundle extras) { - // Wrap the provided Sync Context because it may go away by the time - // we call it. - final SyncContext syncContextClient = new SyncContext(syncContext); - boolean alreadyInProgress = false; - final int extrasAsKey = extrasToKey(extras); - synchronized (mSyncThreadLock) { - if (mSyncThreads.get(extrasAsKey) == null) { - if (Log.isLoggable(TAG, Log.VERBOSE)) { - Log.v(TAG, "starting sync for : " + mServiceComponent); - } - // Start sync. - SyncThread syncThread = new SyncThread(syncContextClient, extras); - mSyncThreads.put(extrasAsKey, syncThread); - syncThread.start(); - } else { - // Don't want to call back to SyncManager while still - // holding lock. - alreadyInProgress = true; - } - } - if (alreadyInProgress) { - syncContextClient.onFinished(SyncResult.ALREADY_IN_PROGRESS); - } - } - - /** - * Used by the SM to cancel a specific sync using the - * com.android.server.content.SyncManager.ActiveSyncContext as a handle. - */ - @Override - public void cancelSync(ISyncContext syncContext) { - SyncThread runningSync = null; - synchronized (mSyncThreadLock) { - for (int i = 0; i < mSyncThreads.size(); i++) { - SyncThread thread = mSyncThreads.valueAt(i); - if (thread.mSyncContext.getSyncContextBinder() == syncContext.asBinder()) { - runningSync = thread; - break; - } - } - } - if (runningSync != null) { - runningSync.interrupt(); - } - } - } - - /** - * - * @param extras Bundle for which to compute hash - * @return an integer hash that is equal to that of another bundle if they both contain the - * same key -> value mappings, however, not necessarily in order. - * Based on the toString() representation of the value mapped. - */ - private int extrasToKey(Bundle extras) { - int hash = KEY_DEFAULT; // Empty bundle, or no parallel operations enabled. - if (parallelSyncsEnabled()) { - for (String key : extras.keySet()) { - String mapping = key + " " + extras.get(key).toString(); - hash += mapping.hashCode(); - } - } - return hash; - } - - /** - * {@hide} - * Similar to {@link android.content.AbstractThreadedSyncAdapter.SyncThread}. However while - * the ATSA considers an already in-progress sync to be if the account provided is currently - * syncing, this anonymous sync has no notion of account and considers a sync unique if the - * provided bundle is different. - */ - private class SyncThread extends Thread { - private final SyncContext mSyncContext; - private final Bundle mExtras; - private final int mThreadsKey; - - public SyncThread(SyncContext syncContext, Bundle extras) { - mSyncContext = syncContext; - mExtras = extras; - mThreadsKey = extrasToKey(extras); - } - - @Override - public void run() { - Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - - Trace.traceBegin(Trace.TRACE_TAG_SYNC_MANAGER, getApplication().getPackageName()); - - SyncResult syncResult = new SyncResult(); - try { - if (isCancelled()) return; - // Run the sync. - SyncService.this.onPerformSync(mExtras, syncResult); - } finally { - Trace.traceEnd(Trace.TRACE_TAG_SYNC_MANAGER); - if (!isCancelled()) { - mSyncContext.onFinished(syncResult); - } - // Synchronize so that the assignment will be seen by other - // threads that also synchronize accesses to mSyncThreads. - synchronized (mSyncThreadLock) { - mSyncThreads.remove(mThreadsKey); - } - } - } - - private boolean isCancelled() { - return Thread.currentThread().isInterrupted(); - } - } - - /** - * Initiate an anonymous sync using this service. SyncAdapter-specific - * parameters may be specified in extras, which is guaranteed to not be - * null. - */ - public abstract void onPerformSync(Bundle extras, SyncResult syncResult); - - /** - * Override this function to indicated whether you want to support parallel syncs. - * <p>If you override and return true multiple threads will be spawned within your Service to - * handle each concurrent sync request. - * - * @return false to indicate that this service does not support parallel operations by default. - */ - protected boolean parallelSyncsEnabled() { - return false; - } -} |