1 /*
2  * Copyright (C) 2016 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.search2;
19 
20 import android.content.Context;
21 import android.content.res.TypedArray;
22 import android.util.AttributeSet;
23 import android.util.TypedValue;
24 
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 
getDataKey(Context context, AttributeSet attrs)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 
getDataTitle(Context context, AttributeSet attrs)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 
getDataSummary(Context context, AttributeSet attrs)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 
getDataSummaryOn(Context context, AttributeSet attrs)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 
getDataSummaryOff(Context context, AttributeSet attrs)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 
getDataEntries(Context context, AttributeSet attrs)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 
getDataKeywords(Context context, AttributeSet attrs)70     public static String getDataKeywords(Context context, AttributeSet attrs) {
71         return getData(context, attrs, R.styleable.Preference, R.styleable.Preference_keywords);
72     }
73 
getDataIcon(Context context, AttributeSet attrs)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      */
getDataChildFragment(Context context, AttributeSet attrs)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 
getData(Context context, AttributeSet set, int[] attrs, int resId)90     private static String getData(Context context, AttributeSet set, int[] attrs, int resId) {
91         final TypedArray ta = context.obtainStyledAttributes(set, attrs);
92         String data = ta.getString(resId);
93         ta.recycle();
94         return (data != null) ? data.toString() : null;
95     }
96 
getDataEntries(Context context, AttributeSet set, int[] attrs, int resId)97     private static String getDataEntries(Context context, AttributeSet set, int[] attrs, int resId) {
98         final TypedArray sa = context.obtainStyledAttributes(set, attrs);
99         final TypedValue tv = sa.peekValue(resId);
100 
101         String[] data = null;
102         if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
103             if (tv.resourceId != 0) {
104                 data = context.getResources().getStringArray(tv.resourceId);
105             }
106         }
107         final int count = (data == null ) ? 0 : data.length;
108         if (count == 0) {
109             return null;
110         }
111         final StringBuilder result = new StringBuilder();
112         for (int n = 0; n < count; n++) {
113             result.append(data[n]);
114             result.append(ENTRIES_SEPARATOR);
115         }
116         return result.toString();
117     }
118 }
119