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; |
| 22 | import java.util.Map; |
| 23 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import org.xmlpull.v1.XmlPullParser; |
| 25 | import org.xmlpull.v1.XmlPullParserException; |
| 26 | |
Gilles Debunne | 76f0ce1 | 2010-05-03 18:50:25 -0700 | [diff] [blame] | 27 | import android.app.AliasActivity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.util.AttributeSet; |
| 31 | import 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 | */ |
| 41 | class PreferenceInflater extends GenericInflater<Preference, PreferenceGroup> { |
| 42 | private static final String TAG = "PreferenceInflater"; |
| 43 | private static final String INTENT_TAG_NAME = "intent"; |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 44 | private static final String EXTRA_TAG_NAME = "extra"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | |
| 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 Debunne | 76f0ce1 | 2010-05-03 18:50:25 -0700 | [diff] [blame] | 69 | protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | 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 Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 79 | XmlPullParserException ex = new XmlPullParserException( |
| 80 | "Error parsing preference"); |
| 81 | ex.initCause(e); |
| 82 | throw ex; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | if (intent != null) { |
| 86 | parentPreference.setIntent(intent); |
| 87 | } |
| 88 | |
| 89 | return true; |
Dianne Hackborn | def1537 | 2010-08-15 12:43:52 -0700 | [diff] [blame] | 90 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | } |
| 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 | } |