1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 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 29 import com.android.internal.annotations.GuardedBy; 30 31 import org.xmlpull.v1.XmlPullParser; 32 import org.xmlpull.v1.XmlPullParserException; 33 import org.xmlpull.v1.XmlSerializer; 34 35 import java.io.IOException; 36 import java.util.ArrayList; 37 import java.util.Collection; 38 39 /** 40 * A cache of services that export the {@link android.content.ISyncAdapter} interface. 41 * @hide 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 SyncAdaptersCache(Context context)56 public SyncAdaptersCache(Context context) { 57 super(context, SERVICE_INTERFACE, SERVICE_META_DATA, ATTRIBUTES_NAME, sSerializer); 58 } 59 parseServiceAttributes(Resources res, String packageName, AttributeSet attrs)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 onServicesChangedLocked(int userId)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 getSyncAdapterPackagesForAuthority(String authority, int userId)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 // If the mapping exists, return it 113 if (adapterMap.containsKey(authority)) { 114 return adapterMap.get(authority); 115 } 116 // Create the mapping and cache it 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 onUserRemoved(int userId)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> { writeAsXml(SyncAdapterType item, XmlSerializer out)145 public void writeAsXml(SyncAdapterType item, XmlSerializer out) throws IOException { 146 out.attribute(null, "authority", item.authority); 147 out.attribute(null, "accountType", item.accountType); 148 } 149 createFromXml(XmlPullParser parser)150 public SyncAdapterType createFromXml(XmlPullParser 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 } 158