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 java.io.IOException;
     20 import java.util.Map;
     21 
     22 import org.xmlpull.v1.XmlPullParser;
     23 import org.xmlpull.v1.XmlPullParserException;
     24 
     25 import android.app.AliasActivity;
     26 import android.content.Context;
     27 import android.content.Intent;
     28 import android.util.AttributeSet;
     29 import android.util.Log;
     30 
     31 /**
     32  * The {@link PreferenceInflater} is used to inflate preference hierarchies from
     33  * XML files.
     34  * <p>
     35  * Do not construct this directly, instead use
     36  * {@link Context#getSystemService(String)} with
     37  * {@link Context#PREFERENCE_INFLATER_SERVICE}.
     38  */
     39 class PreferenceInflater extends GenericInflater<Preference, PreferenceGroup> {
     40     private static final String TAG = "PreferenceInflater";
     41     private static final String INTENT_TAG_NAME = "intent";
     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
     66     protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference,
     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) {
     76                 Log.w(TAG, "Could not parse Intent.");
     77                 Log.w(TAG, e);
     78             }
     79 
     80             if (intent != null) {
     81                 parentPreference.setIntent(intent);
     82             }
     83 
     84             return true;
     85         }
     86 
     87         return false;
     88     }
     89 
     90     @Override
     91     protected PreferenceGroup onMergeRoots(PreferenceGroup givenRoot, boolean attachToGivenRoot,
     92             PreferenceGroup xmlRoot) {
     93         // If we were given a Preferences, use it as the root (ignoring the root
     94         // Preferences from the XML file).
     95         if (givenRoot == null) {
     96             xmlRoot.onAttachedToHierarchy(mPreferenceManager);
     97             return xmlRoot;
     98         } else {
     99             return givenRoot;
    100         }
    101     }
    102 
    103 }
    104