| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.content; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.pm.RegisteredServicesCache; |
| 21 |
import android.content.pm.XmlSerializerAndParser; |
| 22 |
import android.content.res.Resources; |
| 23 |
import android.content.res.TypedArray; |
| 24 |
import android.text.TextUtils; |
| 25 |
import android.util.ArrayMap; |
| 26 |
import android.util.AttributeSet; |
| 27 |
import android.util.SparseArray; |
| 28 |
import android.util.TypedXmlPullParser; |
| 29 |
import android.util.TypedXmlSerializer; |
| 30 |
|
| 31 |
import com.android.internal.annotations.GuardedBy; |
| 32 |
|
| 33 |
import org.xmlpull.v1.XmlPullParserException; |
| 34 |
|
| 35 |
import java.io.IOException; |
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.Collection; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class SyncAdaptersCache extends RegisteredServicesCache<SyncAdapterType> { |
| 44 |
private static final String TAG = "Account"; |
| 45 |
|
| 46 |
private static final String SERVICE_INTERFACE = "android.content.SyncAdapter"; |
| 47 |
private static final String SERVICE_META_DATA = "android.content.SyncAdapter"; |
| 48 |
private static final String ATTRIBUTES_NAME = "sync-adapter"; |
| 49 |
private static final MySerializer sSerializer = new MySerializer(); |
| 50 |
|
| 51 |
@GuardedBy("mServicesLock") |
| 52 |
private SparseArray<ArrayMap<String,String[]>> mAuthorityToSyncAdapters |
| 53 |
= new SparseArray<>(); |
| 54 |
|
| 55 |
@UnsupportedAppUsage |
| 56 |
public SyncAdaptersCache(Context context) { |
| 57 |
super(context, SERVICE_INTERFACE, SERVICE_META_DATA, ATTRIBUTES_NAME, sSerializer); |
| 58 |
} |
| 59 |
|
| 60 |
public SyncAdapterType parseServiceAttributes(Resources res, |
| 61 |
String packageName, AttributeSet attrs) { |
| 62 |
TypedArray sa = res.obtainAttributes(attrs, |
| 63 |
com.android.internal.R.styleable.SyncAdapter); |
| 64 |
try { |
| 65 |
final String authority = |
| 66 |
sa.getString(com.android.internal.R.styleable.SyncAdapter_contentAuthority); |
| 67 |
final String accountType = |
| 68 |
sa.getString(com.android.internal.R.styleable.SyncAdapter_accountType); |
| 69 |
if (TextUtils.isEmpty(authority) || TextUtils.isEmpty(accountType)) { |
| 70 |
return null; |
| 71 |
} |
| 72 |
final boolean userVisible = |
| 73 |
sa.getBoolean(com.android.internal.R.styleable.SyncAdapter_userVisible, true); |
| 74 |
final boolean supportsUploading = |
| 75 |
sa.getBoolean(com.android.internal.R.styleable.SyncAdapter_supportsUploading, |
| 76 |
true); |
| 77 |
final boolean isAlwaysSyncable = |
| 78 |
sa.getBoolean(com.android.internal.R.styleable.SyncAdapter_isAlwaysSyncable, |
| 79 |
false); |
| 80 |
final boolean allowParallelSyncs = |
| 81 |
sa.getBoolean(com.android.internal.R.styleable.SyncAdapter_allowParallelSyncs, |
| 82 |
false); |
| 83 |
final String settingsActivity = |
| 84 |
sa.getString(com.android.internal.R.styleable |
| 85 |
.SyncAdapter_settingsActivity); |
| 86 |
return new SyncAdapterType(authority, accountType, userVisible, supportsUploading, |
| 87 |
isAlwaysSyncable, allowParallelSyncs, settingsActivity, packageName); |
| 88 |
} finally { |
| 89 |
sa.recycle(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
protected void onServicesChangedLocked(int userId) { |
| 95 |
synchronized (mServicesLock) { |
| 96 |
ArrayMap<String,String[]> adapterMap = mAuthorityToSyncAdapters.get(userId); |
| 97 |
if (adapterMap != null) { |
| 98 |
adapterMap.clear(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
super.onServicesChangedLocked(userId); |
| 103 |
} |
| 104 |
|
| 105 |
public String[] getSyncAdapterPackagesForAuthority(String authority, int userId) { |
| 106 |
synchronized (mServicesLock) { |
| 107 |
ArrayMap<String,String[]> adapterMap = mAuthorityToSyncAdapters.get(userId); |
| 108 |
if (adapterMap == null) { |
| 109 |
adapterMap = new ArrayMap<>(); |
| 110 |
mAuthorityToSyncAdapters.put(userId, adapterMap); |
| 111 |
} |
| 112 |
|
| 113 |
if (adapterMap.containsKey(authority)) { |
| 114 |
return adapterMap.get(authority); |
| 115 |
} |
| 116 |
|
| 117 |
String[] syncAdapterPackages; |
| 118 |
final Collection<RegisteredServicesCache.ServiceInfo<SyncAdapterType>> serviceInfos; |
| 119 |
serviceInfos = getAllServices(userId); |
| 120 |
ArrayList<String> packages = new ArrayList<>(); |
| 121 |
for (RegisteredServicesCache.ServiceInfo<SyncAdapterType> serviceInfo : serviceInfos) { |
| 122 |
if (authority.equals(serviceInfo.type.authority) |
| 123 |
&& serviceInfo.componentName != null) { |
| 124 |
packages.add(serviceInfo.componentName.getPackageName()); |
| 125 |
} |
| 126 |
} |
| 127 |
syncAdapterPackages = new String[packages.size()]; |
| 128 |
packages.toArray(syncAdapterPackages); |
| 129 |
adapterMap.put(authority, syncAdapterPackages); |
| 130 |
|
| 131 |
return syncAdapterPackages; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
protected void onUserRemoved(int userId) { |
| 137 |
synchronized (mServicesLock) { |
| 138 |
mAuthorityToSyncAdapters.remove(userId); |
| 139 |
} |
| 140 |
|
| 141 |
super.onUserRemoved(userId); |
| 142 |
} |
| 143 |
|
| 144 |
static class MySerializer implements XmlSerializerAndParser<SyncAdapterType> { |
| 145 |
public void writeAsXml(SyncAdapterType item, TypedXmlSerializer out) throws IOException { |
| 146 |
out.attribute(null, "authority", item.authority); |
| 147 |
out.attribute(null, "accountType", item.accountType); |
| 148 |
} |
| 149 |
|
| 150 |
public SyncAdapterType createFromXml(TypedXmlPullParser parser) |
| 151 |
throws IOException, XmlPullParserException { |
| 152 |
final String authority = parser.getAttributeValue(null, "authority"); |
| 153 |
final String accountType = parser.getAttributeValue(null, "accountType"); |
| 154 |
return SyncAdapterType.newKey(authority, accountType); |
| 155 |
} |
| 156 |
} |
| 157 |
} |