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.AttributeSet; 24 import org.xmlpull.v1.XmlPullParser; 25 import org.xmlpull.v1.XmlSerializer; 26 import org.xmlpull.v1.XmlPullParserException; 27 28 import 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 return new SyncAdapterType(authority, accountType, userVisible, supportsUploading); 64 } finally { 65 sa.recycle(); 66 } 67 } 68 69 static class MySerializer implements XmlSerializerAndParser<SyncAdapterType> { 70 public void writeAsXml(SyncAdapterType item, XmlSerializer out) throws IOException { 71 out.attribute(null, "authority", item.authority); 72 out.attribute(null, "accountType", item.accountType); 73 } 74 75 public SyncAdapterType createFromXml(XmlPullParser parser) 76 throws IOException, XmlPullParserException { 77 final String authority = parser.getAttributeValue(null, "authority"); 78 final String accountType = parser.getAttributeValue(null, "accountType"); 79 return SyncAdapterType.newKey(authority, accountType); 80 } 81 } 82 }