SyncAdaptersCache.java revision 9db3d07b9620b4269ab33f78604a36327e536ce1
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
17package android.content;
18
19import android.content.pm.RegisteredServicesCache;
20import android.content.pm.XmlSerializerAndParser;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlSerializer;
25import org.xmlpull.v1.XmlPullParserException;
26
27import java.io.IOException;
28
29/**
30 * A cache of services that export the {@link android.content.ISyncAdapter} interface.
31 * @hide
32 */
33/* package private */ class SyncAdaptersCache extends RegisteredServicesCache<SyncAdapterType> {
34    private static final String TAG = "Account";
35
36    private static final String SERVICE_INTERFACE = "android.content.SyncAdapter";
37    private static final String SERVICE_META_DATA = "android.content.SyncAdapter";
38    private static final String ATTRIBUTES_NAME = "sync-adapter";
39    private static final MySerializer sSerializer = new MySerializer();
40
41    SyncAdaptersCache(Context context) {
42        super(context, SERVICE_INTERFACE, SERVICE_META_DATA, ATTRIBUTES_NAME, sSerializer);
43    }
44
45    public SyncAdapterType parseServiceAttributes(String packageName, AttributeSet attrs) {
46        TypedArray sa = mContext.getResources().obtainAttributes(attrs,
47                com.android.internal.R.styleable.SyncAdapter);
48        try {
49            final String authority =
50                    sa.getString(com.android.internal.R.styleable.SyncAdapter_contentAuthority);
51            final String accountType =
52                    sa.getString(com.android.internal.R.styleable.SyncAdapter_accountType);
53            if (authority == null || accountType == null) {
54                return null;
55            }
56            final boolean userVisible =
57                    sa.getBoolean(com.android.internal.R.styleable.SyncAdapter_userVisible, true);
58            final boolean supportsUploading =
59                    sa.getBoolean(com.android.internal.R.styleable.SyncAdapter_supportsUploading,
60                            true);
61            return new SyncAdapterType(authority, accountType, userVisible, supportsUploading);
62        } finally {
63            sa.recycle();
64        }
65    }
66
67    static class MySerializer implements XmlSerializerAndParser<SyncAdapterType> {
68        public void writeAsXml(SyncAdapterType item, XmlSerializer out) throws IOException {
69            out.attribute(null, "authority", item.authority);
70            out.attribute(null, "accountType", item.accountType);
71        }
72
73        public SyncAdapterType createFromXml(XmlPullParser parser)
74                throws IOException, XmlPullParserException {
75            final String authority = parser.getAttributeValue(null, "authority");
76            final String accountType = parser.getAttributeValue(null, "accountType");
77            return SyncAdapterType.newKey(authority, accountType);
78        }
79    }
80}