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 android.content.Context;
     20 import android.content.Intent;
     21 import android.util.AttributeSet;
     22 
     23 import com.android.internal.util.XmlUtils;
     24 
     25 import org.xmlpull.v1.XmlPullParser;
     26 import org.xmlpull.v1.XmlPullParserException;
     27 
     28 import java.io.IOException;
     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  * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
     39  *      <a href="{@docRoot}reference/androidx/preference/package-summary.html">
     40  *      Preference Library</a> for consistent behavior across all devices. For more information on
     41  *      using the AndroidX Preference Library see
     42  *      <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
     43  */
     44 @Deprecated
     45 class PreferenceInflater extends GenericInflater<Preference, PreferenceGroup> {
     46     private static final String TAG = "PreferenceInflater";
     47     private static final String INTENT_TAG_NAME = "intent";
     48     private static final String EXTRA_TAG_NAME = "extra";
     49 
     50     private PreferenceManager mPreferenceManager;
     51 
     52     public PreferenceInflater(Context context, PreferenceManager preferenceManager) {
     53         super(context);
     54         init(preferenceManager);
     55     }
     56 
     57     PreferenceInflater(GenericInflater<Preference, PreferenceGroup> original, PreferenceManager preferenceManager, Context newContext) {
     58         super(original, newContext);
     59         init(preferenceManager);
     60     }
     61 
     62     @Override
     63     public GenericInflater<Preference, PreferenceGroup> cloneInContext(Context newContext) {
     64         return new PreferenceInflater(this, mPreferenceManager, newContext);
     65     }
     66 
     67     private void init(PreferenceManager preferenceManager) {
     68         mPreferenceManager = preferenceManager;
     69         setDefaultPackage("android.preference.");
     70     }
     71 
     72     @Override
     73     protected boolean onCreateCustomFromTag(XmlPullParser parser, Preference parentPreference,
     74             AttributeSet attrs) throws XmlPullParserException {
     75         final String tag = parser.getName();
     76 
     77         if (tag.equals(INTENT_TAG_NAME)) {
     78             Intent intent = null;
     79 
     80             try {
     81                 intent = Intent.parseIntent(getContext().getResources(), parser, attrs);
     82             } catch (IOException e) {
     83                 XmlPullParserException ex = new XmlPullParserException(
     84                         "Error parsing preference");
     85                 ex.initCause(e);
     86                 throw ex;
     87             }
     88 
     89             if (intent != null) {
     90                 parentPreference.setIntent(intent);
     91             }
     92 
     93             return true;
     94         } else if (tag.equals(EXTRA_TAG_NAME)) {
     95             getContext().getResources().parseBundleExtra(EXTRA_TAG_NAME, attrs,
     96                     parentPreference.getExtras());
     97             try {
     98                 XmlUtils.skipCurrentTag(parser);
     99             } catch (IOException e) {
    100                 XmlPullParserException ex = new XmlPullParserException(
    101                         "Error parsing preference");
    102                 ex.initCause(e);
    103                 throw ex;
    104             }
    105             return true;
    106         }
    107 
    108         return false;
    109     }
    110 
    111     @Override
    112     protected PreferenceGroup onMergeRoots(PreferenceGroup givenRoot, boolean attachToGivenRoot,
    113             PreferenceGroup xmlRoot) {
    114         // If we were given a Preferences, use it as the root (ignoring the root
    115         // Preferences from the XML file).
    116         if (givenRoot == null) {
    117             xmlRoot.onAttachedToHierarchy(mPreferenceManager);
    118             return xmlRoot;
    119         } else {
    120             return givenRoot;
    121         }
    122     }
    123 
    124 }
    125