• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.content.Context;
19 import android.util.AttributeSet;
20 import android.view.LayoutInflater;
21 import android.view.View;
22 
23 import androidx.annotation.Nullable;
24 import androidx.appcompat.app.AppCompatViewInflater;
25 
26 import com.android.car.ui.recyclerview.CarUiRecyclerView;
27 import com.android.car.ui.sharedlibrarysupport.SharedLibraryFactorySingleton;
28 
29 /**
30  * A custom {@link LayoutInflater.Factory2} that will create CarUi components such as {@link
31  * CarUiRecyclerView}. It extends AppCompatViewInflater so that it can still let AppCompat
32  * components be created correctly.
33  */
34 public class CarUiLayoutInflaterFactory extends AppCompatViewInflater
35         implements LayoutInflater.Factory2 {
36 
37     @Nullable
createView(Context context, String name, AttributeSet attrs)38     protected View createView(Context context, String name, AttributeSet attrs) {
39         View view = null;
40 
41         // Don't use CarUiTextView.class.getSimpleName(), as when proguard obfuscates the class name
42         // it will no longer match what's in xml.
43         if (CarUiRecyclerView.class.getName().equals(name)) {
44             view = SharedLibraryFactorySingleton.get(context)
45                     .createRecyclerView(context, attrs);
46         } else if (name.contentEquals("CarUiTextView")) {
47             view = SharedLibraryFactorySingleton.get(context).createTextView(context, attrs);
48         }
49 
50         return view;
51     }
52 
53     @Override
onCreateView(String name, Context context, AttributeSet attrs)54     public View onCreateView(String name, Context context, AttributeSet attrs) {
55         // Deprecated, do nothing.
56         return null;
57     }
58 
59     @Override
onCreateView(View parent, String name, Context context, AttributeSet attrs)60     public View onCreateView(View parent, String name, Context context,
61             AttributeSet attrs) {
62         return createView(context, name, attrs);
63     }
64 }
65