The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 19 | import com.android.internal.util.XmlUtils; |
| 20 | |
Gilles Debunne | 76f0ce1 | 2010-05-03 18:50:25 -0700 | [diff] [blame] | 21 | import java.io.IOException; |
Gilles Debunne | 76f0ce1 | 2010-05-03 18:50:25 -0700 | [diff] [blame] | 22 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import org.xmlpull.v1.XmlPullParser; |
| 24 | import org.xmlpull.v1.XmlPullParserException; |
| 25 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Context; |
| 27 | import android.content.Intent; |
| 28 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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"; |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 41 | private static final String EXTRA_TAG_NAME = "extra"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
| 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 Debunne | 76f0ce1 | 2010-05-03 18:50:25 -0700 | [diff] [blame] | 66 | protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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) { |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 76 | XmlPullParserException ex = new XmlPullParserException( |
| 77 | "Error parsing preference"); |
| 78 | ex.initCause(e); |
| 79 | throw ex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (intent != null) { |
| 83 | parentPreference.setIntent(intent); |
| 84 | } |
| 85 | |
| 86 | return true; |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | } |
| 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 | } |