SyncAdaptersCache.java revision 0c4d04ac2e8aa62560d8d767fa1c87e5361b0b08
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 */ 34/* package private */ 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 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 return new SyncAdapterType(authority, accountType, userVisible, supportsUploading, 70 isAlwaysSyncable, allowParallelSyncs); 71 } finally { 72 sa.recycle(); 73 } 74 } 75 76 static class MySerializer implements XmlSerializerAndParser<SyncAdapterType> { 77 public void writeAsXml(SyncAdapterType item, XmlSerializer out) throws IOException { 78 out.attribute(null, "authority", item.authority); 79 out.attribute(null, "accountType", item.accountType); 80 } 81 82 public SyncAdapterType createFromXml(XmlPullParser parser) 83 throws IOException, XmlPullParserException { 84 final String authority = parser.getAttributeValue(null, "authority"); 85 final String accountType = parser.getAttributeValue(null, "accountType"); 86 return SyncAdapterType.newKey(authority, accountType); 87 } 88 } 89} 90