diff options
author | Amith Yamasani <yamasani@google.com> | 2015-06-17 13:25:42 -0700 |
---|---|---|
committer | Amith Yamasani <yamasani@google.com> | 2015-06-19 15:04:58 -0700 |
commit | 37a40c24deb02bca3868a8085069afae112f22e4 (patch) | |
tree | 7e38587e99825bf5234c1f130bcf1972271b0795 /core/java/android/content/SyncAdaptersCache.java | |
parent | b0ff3a6cb37aa45add4b0c5135bd978442fcc441 (diff) | |
download | frameworks_base-37a40c24deb02bca3868a8085069afae112f22e4.zip frameworks_base-37a40c24deb02bca3868a8085069afae112f22e4.tar.gz frameworks_base-37a40c24deb02bca3868a8085069afae112f22e4.tar.bz2 |
App Standby : Association between content providers and their sync adapter
Set sync adapters to active if the associated content providers are used
at foreground process state.
Minimize how frequently published content providers are reported by
keeping track of last reported time.
Also cache sync adapters associated with an authority in SyncManager.
Bug: 21785111
Change-Id: Ic2c8cb6a27f005d1a1d0aad21d36b1510160753a
Diffstat (limited to 'core/java/android/content/SyncAdaptersCache.java')
-rw-r--r-- | core/java/android/content/SyncAdaptersCache.java | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/core/java/android/content/SyncAdaptersCache.java b/core/java/android/content/SyncAdaptersCache.java index 8bb3ee7..6704b75 100644 --- a/core/java/android/content/SyncAdaptersCache.java +++ b/core/java/android/content/SyncAdaptersCache.java @@ -20,12 +20,19 @@ import android.content.pm.RegisteredServicesCache; import android.content.pm.XmlSerializerAndParser; import android.content.res.Resources; import android.content.res.TypedArray; +import android.util.ArrayMap; import android.util.AttributeSet; +import android.util.SparseArray; + +import com.android.internal.annotations.GuardedBy; + import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; /** * A cache of services that export the {@link android.content.ISyncAdapter} interface. @@ -39,6 +46,10 @@ public class SyncAdaptersCache extends RegisteredServicesCache<SyncAdapterType> private static final String ATTRIBUTES_NAME = "sync-adapter"; private static final MySerializer sSerializer = new MySerializer(); + @GuardedBy("mServicesLock") + private SparseArray<ArrayMap<String,String[]>> mAuthorityToSyncAdapters + = new SparseArray<>(); + public SyncAdaptersCache(Context context) { super(context, SERVICE_INTERFACE, SERVICE_META_DATA, ATTRIBUTES_NAME, sSerializer); } @@ -76,6 +87,57 @@ public class SyncAdaptersCache extends RegisteredServicesCache<SyncAdapterType> } } + @Override + protected void onServicesChangedLocked(int userId) { + synchronized (mServicesLock) { + ArrayMap<String,String[]> adapterMap = mAuthorityToSyncAdapters.get(userId); + if (adapterMap != null) { + adapterMap.clear(); + } + } + + super.onServicesChangedLocked(userId); + } + + public String[] getSyncAdapterPackagesForAuthority(String authority, int userId) { + synchronized (mServicesLock) { + ArrayMap<String,String[]> adapterMap = mAuthorityToSyncAdapters.get(userId); + if (adapterMap == null) { + adapterMap = new ArrayMap<>(); + mAuthorityToSyncAdapters.put(userId, adapterMap); + } + // If the mapping exists, return it + if (adapterMap.containsKey(authority)) { + return adapterMap.get(authority); + } + // Create the mapping and cache it + String[] syncAdapterPackages; + final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos; + serviceInfos = getAllServices(userId); + ArrayList<String> packages = new ArrayList<>(); + for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> serviceInfo : serviceInfos) { + if (authority.equals(serviceInfo.type.authority) + && serviceInfo.componentName != null) { + packages.add(serviceInfo.componentName.getPackageName()); + } + } + syncAdapterPackages = new String[packages.size()]; + packages.toArray(syncAdapterPackages); + adapterMap.put(authority, syncAdapterPackages); + + return syncAdapterPackages; + } + } + + @Override + protected void onUserRemoved(int userId) { + synchronized (mServicesLock) { + mAuthorityToSyncAdapters.remove(userId); + } + + super.onUserRemoved(userId); + } + static class MySerializer implements XmlSerializerAndParser<SyncAdapterType> { public void writeAsXml(SyncAdapterType item, XmlSerializer out) throws IOException { out.attribute(null, "authority", item.authority); |