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 onCreateItem(String name, AttributeSet attrs)33 protected Preference onCreateItem(String name, 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 if (attrs instanceof BridgeXmlBlockParser) { 43 viewKey = ((BridgeXmlBlockParser) attrs).getViewCookie(); 44 } 45 46 Preference preference = null; 47 try { 48 preference = super.onCreateItem(name, attrs); 49 } catch (ClassNotFoundException | InflateException exception) { 50 // name is probably not a valid preference type 51 if ("SwitchPreferenceCompat".equals(name)) { 52 preference = super.onCreateItem("SwitchPreference", attrs); 53 } 54 } 55 56 if (viewKey != null && bc != null) { 57 bc.addCookie(preference, viewKey); 58 } 59 return preference; 60 } 61 } 62