Home | History | Annotate | Download | only in search
      1 /*
      2  * Copyright (C) 2017 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 
     18 package com.android.settings.search;
     19 
     20 import android.annotation.Nullable;
     21 import android.content.Context;
     22 import android.content.res.TypedArray;
     23 import android.util.AttributeSet;
     24 import android.util.TypedValue;
     25 import com.android.settings.R;
     26 
     27 /**
     28  * Utility class to parse elements of XML preferences
     29  */
     30 public class XmlParserUtils {
     31 
     32     private static final String ENTRIES_SEPARATOR = "|";
     33 
     34     public static String getDataKey(Context context, AttributeSet attrs) {
     35         return getData(context, attrs,
     36                 com.android.internal.R.styleable.Preference,
     37                 com.android.internal.R.styleable.Preference_key);
     38     }
     39 
     40     public static String getDataTitle(Context context, AttributeSet attrs) {
     41         return getData(context, attrs,
     42                 com.android.internal.R.styleable.Preference,
     43                 com.android.internal.R.styleable.Preference_title);
     44     }
     45 
     46     public static String getDataSummary(Context context, AttributeSet attrs) {
     47         return getData(context, attrs,
     48                 com.android.internal.R.styleable.Preference,
     49                 com.android.internal.R.styleable.Preference_summary);
     50     }
     51 
     52     public static String getDataSummaryOn(Context context, AttributeSet attrs) {
     53         return getData(context, attrs,
     54                 com.android.internal.R.styleable.CheckBoxPreference,
     55                 com.android.internal.R.styleable.CheckBoxPreference_summaryOn);
     56     }
     57 
     58     public static String getDataSummaryOff(Context context, AttributeSet attrs) {
     59         return getData(context, attrs,
     60                 com.android.internal.R.styleable.CheckBoxPreference,
     61                 com.android.internal.R.styleable.CheckBoxPreference_summaryOff);
     62     }
     63 
     64     public static String getDataEntries(Context context, AttributeSet attrs) {
     65         return getDataEntries(context, attrs,
     66                 com.android.internal.R.styleable.ListPreference,
     67                 com.android.internal.R.styleable.ListPreference_entries);
     68     }
     69 
     70     public static String getDataKeywords(Context context, AttributeSet attrs) {
     71         return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_keywords);
     72     }
     73 
     74     public static int getDataIcon(Context context, AttributeSet attrs) {
     75         final TypedArray ta = context.obtainStyledAttributes(attrs,
     76                 com.android.internal.R.styleable.Preference);
     77         final int dataIcon = ta.getResourceId(com.android.internal.R.styleable.Icon_icon, 0);
     78         ta.recycle();
     79         return dataIcon;
     80     }
     81 
     82     /**
     83      * Returns the fragment name if this preference launches a child fragment.
     84      */
     85     public static String getDataChildFragment(Context context, AttributeSet attrs) {
     86         return getData(context, attrs, R.styleable.Preference,
     87                 R.styleable.Preference_android_fragment);
     88     }
     89 
     90     @Nullable
     91     private static String getData(Context context, AttributeSet set, int[] attrs, int resId) {
     92         final TypedArray ta = context.obtainStyledAttributes(set, attrs);
     93         String data = ta.getString(resId);
     94         ta.recycle();
     95         return data;
     96     }
     97 
     98     private static String getDataEntries(Context context, AttributeSet set, int[] attrs, int resId) {
     99         final TypedArray sa = context.obtainStyledAttributes(set, attrs);
    100         final TypedValue tv = sa.peekValue(resId);
    101         sa.recycle();
    102         String[] data = null;
    103         if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
    104             if (tv.resourceId != 0) {
    105                 data = context.getResources().getStringArray(tv.resourceId);
    106             }
    107         }
    108         final int count = (data == null ) ? 0 : data.length;
    109         if (count == 0) {
    110             return null;
    111         }
    112         final StringBuilder result = new StringBuilder();
    113         for (int n = 0; n < count; n++) {
    114             result.append(data[n]);
    115             result.append(ENTRIES_SEPARATOR);
    116         }
    117         return result.toString();
    118     }
    119 }
    120