1 /* 2 * Copyright (C) 2007 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.preference; 18 19 import com.android.internal.util.XmlUtils; 20 21 import java.io.IOException; 22 23 import org.xmlpull.v1.XmlPullParser; 24 import org.xmlpull.v1.XmlPullParserException; 25 26 import android.content.Context; 27 import android.content.Intent; 28 import android.util.AttributeSet; 29 30 /** 31 * The {@link PreferenceInflater} is used to inflate preference hierarchies from 32 * XML files. 33 * <p> 34 * Do not construct this directly, instead use 35 * {@link Context#getSystemService(String)} with 36 * {@link Context#PREFERENCE_INFLATER_SERVICE}. 37 */ 38 class PreferenceInflater extends GenericInflater<Preference, PreferenceGroup> { 39 private static final String TAG = "PreferenceInflater"; 40 private static final String INTENT_TAG_NAME = "intent"; 41 private static final String EXTRA_TAG_NAME = "extra"; 42 43 private PreferenceManager mPreferenceManager; 44 PreferenceInflater(Context context, PreferenceManager preferenceManager)45 public PreferenceInflater(Context context, PreferenceManager preferenceManager) { 46 super(context); 47 init(preferenceManager); 48 } 49 PreferenceInflater(GenericInflater<Preference, PreferenceGroup> original, PreferenceManager preferenceManager, Context newContext)50 PreferenceInflater(GenericInflater<Preference, PreferenceGroup> original, PreferenceManager preferenceManager, Context newContext) { 51 super(original, newContext); 52 init(preferenceManager); 53 } 54 55 @Override cloneInContext(Context newContext)56 public GenericInflater<Preference, PreferenceGroup> cloneInContext(Context newContext) { 57 return new PreferenceInflater(this, mPreferenceManager, newContext); 58 } 59 init(PreferenceManager preferenceManager)60 private void init(PreferenceManager preferenceManager) { 61 mPreferenceManager = preferenceManager; 62 setDefaultPackage("android.preference."); 63 } 64 65 @Override onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference, AttributeSet attrs)66 protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference, 67 AttributeSet attrs) throws XmlPullParserException { 68 final String tag = parser.getName(); 69 70 if (tag.equals(INTENT_TAG_NAME)) { 71 Intent intent = null; 72 73 try { 74 intent = Intent.parseIntent(getContext().getResources(), parser, attrs); 75 } catch (IOException e) { 76 XmlPullParserException ex = new XmlPullParserException( 77 "Error parsing preference"); 78 ex.initCause(e); 79 throw ex; 80 } 81 82 if (intent != null) { 83 parentPreference.setIntent(intent); 84 } 85 86 return true; 87 } else if (tag.equals(EXTRA_TAG_NAME)) { 88 getContext().getResources().parseBundleExtra(EXTRA_TAG_NAME, attrs, 89 parentPreference.getExtras()); 90 try { 91 XmlUtils.skipCurrentTag(parser); 92 } catch (IOException e) { 93 XmlPullParserException ex = new XmlPullParserException( 94 "Error parsing preference"); 95 ex.initCause(e); 96 throw ex; 97 } 98 return true; 99 } 100 101 return false; 102 } 103 104 @Override onMergeRoots(PreferenceGroup givenRoot, boolean attachToGivenRoot, PreferenceGroup xmlRoot)105 protected PreferenceGroup onMergeRoots(PreferenceGroup givenRoot, boolean attachToGivenRoot, 106 PreferenceGroup xmlRoot) { 107 // If we were given a Preferences, use it as the root (ignoring the root 108 // Preferences from the XML file). 109 if (givenRoot == null) { 110 xmlRoot.onAttachedToHierarchy(mPreferenceManager); 111 return xmlRoot; 112 } else { 113 return givenRoot; 114 } 115 } 116 117 } 118