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