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