blob: 727fbcad99575ee4b2c8545d38d608105c9e4386 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.preference;
18
Dianne Hackborndef15372010-08-15 12:43:52 -070019import com.android.internal.util.XmlUtils;
20
Gilles Debunne76f0ce12010-05-03 18:50:25 -070021import java.io.IOException;
Gilles Debunne76f0ce12010-05-03 18:50:25 -070022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import org.xmlpull.v1.XmlPullParser;
24import org.xmlpull.v1.XmlPullParserException;
25
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
28import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
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 */
38class PreferenceInflater extends GenericInflater<Preference, PreferenceGroup> {
39 private static final String TAG = "PreferenceInflater";
40 private static final String INTENT_TAG_NAME = "intent";
Dianne Hackborndef15372010-08-15 12:43:52 -070041 private static final String EXTRA_TAG_NAME = "extra";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
43 private PreferenceManager mPreferenceManager;
44
45 public PreferenceInflater(Context context, PreferenceManager preferenceManager) {
46 super(context);
47 init(preferenceManager);
48 }
49
50 PreferenceInflater(GenericInflater<Preference, PreferenceGroup> original, PreferenceManager preferenceManager, Context newContext) {
51 super(original, newContext);
52 init(preferenceManager);
53 }
54
55 @Override
56 public GenericInflater<Preference, PreferenceGroup> cloneInContext(Context newContext) {
57 return new PreferenceInflater(this, mPreferenceManager, newContext);
58 }
59
60 private void init(PreferenceManager preferenceManager) {
61 mPreferenceManager = preferenceManager;
62 setDefaultPackage("android.preference.");
63 }
64
65 @Override
Gilles Debunne76f0ce12010-05-03 18:50:25 -070066 protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 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) {
Dianne Hackborndef15372010-08-15 12:43:52 -070076 XmlPullParserException ex = new XmlPullParserException(
77 "Error parsing preference");
78 ex.initCause(e);
79 throw ex;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 }
81
82 if (intent != null) {
83 parentPreference.setIntent(intent);
84 }
85
86 return true;
Dianne Hackborndef15372010-08-15 12:43:52 -070087 } 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100
101 return false;
102 }
103
104 @Override
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}