Home | History | Annotate | Download | only in preference
      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 import java.util.Map;
     23 
     24 import org.xmlpull.v1.XmlPullParser;
     25 import org.xmlpull.v1.XmlPullParserException;
     26 
     27 import android.app.AliasActivity;
     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";
     44     private static final String EXTRA_TAG_NAME = "extra";
     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
     69     protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference,
     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) {
     79                 XmlPullParserException ex = new XmlPullParserException(
     80                         "Error parsing preference");
     81                 ex.initCause(e);
     82                 throw ex;
     83             }
     84 
     85             if (intent != null) {
     86                 parentPreference.setIntent(intent);
     87             }
     88 
     89             return true;
     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;
    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 }
    121