1 /* 2 * Copyright (C) 2021 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 package com.android.car.ui; 17 18 import static com.android.car.ui.core.CarUi.MIN_TARGET_API; 19 20 import android.annotation.TargetApi; 21 import android.content.Context; 22 import android.util.AttributeSet; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 26 import androidx.annotation.NonNull; 27 import androidx.annotation.Nullable; 28 import androidx.appcompat.app.AppCompatViewInflater; 29 import androidx.appcompat.widget.AppCompatTextView; 30 import androidx.recyclerview.widget.RecyclerView; 31 32 import com.android.car.ui.pluginsupport.PluginFactorySingleton; 33 import com.android.car.ui.recyclerview.CarUiRecyclerView; 34 import com.android.car.ui.widget.CarUiTextView; 35 36 /** 37 * A custom {@link LayoutInflater.Factory2} that will create CarUi components such as {@link 38 * CarUiRecyclerView}. It extends AppCompatViewInflater so that it can still let AppCompat 39 * components be created correctly. 40 */ 41 @TargetApi(MIN_TARGET_API) 42 public class CarUiLayoutInflaterFactory extends AppCompatViewInflater 43 implements LayoutInflater.Factory2 { 44 45 @Nullable createView(Context context, String name, AttributeSet attrs)46 protected View createView(Context context, String name, AttributeSet attrs) { 47 View view = null; 48 49 // Don't use CarUiTextView.class.getSimpleName(), as when proguard obfuscates the class name 50 // it will no longer match what's in xml. 51 if (CarUiRecyclerView.class.getName().equals(name)) { 52 view = PluginFactorySingleton.get(context) 53 .createRecyclerView(context, attrs).getView(); 54 } else if (name.contentEquals("CarUiTextView")) { 55 view = PluginFactorySingleton.get(context).createTextView(context, attrs); 56 } else if (("androidx.recyclerview.widget." + "RecyclerView").equals(name)) { 57 // Some apps use the old android.support.v7.widget.RecyclerView package name for the 58 // RecyclerView. When RROs are applied, they must also use that old package name to 59 // instantiate the RecyclerView. So if an RRO is found using the new package name, 60 // inflate a RecyclerView using the old package name. We are using the new package name 61 // here, but when car-ui-lib is included in one of those apps that uses the old package 62 // name, the RecyclerView class is renamed. 63 view = new RecyclerView(context, attrs); 64 } else if ("TextView".equals(name)) { 65 // Replace all TextView occurrences with CarUiTextView to support older RROs that still 66 // use TextView where CarUiTextView is now expected. ie. `car_ui_list_item.xml`. 67 view = PluginFactorySingleton.get(context).createTextView(context, attrs); 68 } 69 70 return view; 71 } 72 73 @NonNull 74 @Override createTextView(Context context, AttributeSet attrs)75 protected AppCompatTextView createTextView(Context context, AttributeSet attrs) { 76 // Replace all TextView occurrences with CarUiTextView to support older RROs that still 77 // use TextView where CarUiTextView is now expected. ie. `car_ui_list_item.xml`. 78 return CarUiTextView.create(context, attrs); 79 } 80 81 @Override onCreateView(String name, Context context, AttributeSet attrs)82 public View onCreateView(String name, Context context, AttributeSet attrs) { 83 // Deprecated, do nothing. 84 return null; 85 } 86 87 @Override onCreateView(View parent, String name, Context context, AttributeSet attrs)88 public View onCreateView(View parent, String name, Context context, 89 AttributeSet attrs) { 90 return createView(context, name, attrs); 91 } 92 } 93