1 /* 2 * Copyright (C) 2014 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.layoutlib.bridge.android.BridgeContext; 20 import com.android.layoutlib.bridge.android.BridgeXmlBlockParser; 21 22 import android.content.Context; 23 import android.util.AttributeSet; 24 import android.view.InflateException; 25 26 public class BridgePreferenceInflater extends PreferenceInflater { 27 BridgePreferenceInflater(Context context, PreferenceManager preferenceManager)28 public BridgePreferenceInflater(Context context, PreferenceManager preferenceManager) { 29 super(context, preferenceManager); 30 } 31 32 @Override createItem(String name, String prefix, AttributeSet attrs)33 public Preference createItem(String name, String prefix, AttributeSet attrs) 34 throws ClassNotFoundException { 35 Object viewKey = null; 36 BridgeContext bc = null; 37 38 Context context = getContext(); 39 if (context instanceof BridgeContext) { 40 bc = (BridgeContext) context; 41 } 42 43 if (attrs instanceof BridgeXmlBlockParser) { 44 viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie(); 45 } 46 47 Preference preference = null; 48 try { 49 preference = super.createItem(name, prefix, attrs); 50 } catch (ClassNotFoundException | InflateException exception) { 51 // name is probably not a valid preference type 52 if (("android.support.v7.preference".equals(prefix) || 53 "androidx.preference".equals(prefix)) && 54 "SwitchPreferenceCompat".equals(name)) { 55 preference = super.createItem("SwitchPreference", prefix, attrs); 56 } 57 } 58 59 if (viewKey != null && bc != null) { 60 bc.addCookie(preference, viewKey); 61 } 62 return preference; 63 } 64 } 65