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