1 /* 2 * Copyright (C) 2024 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 com.android.systemui.car.systembar.element; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.content.Context; 22 import android.content.res.TypedArray; 23 import android.text.TextUtils; 24 import android.util.AttributeSet; 25 import android.util.Log; 26 27 import com.android.systemui.R; 28 29 /** Helper class for resolving element controllers */ 30 public class CarSystemBarElementResolver { 31 private static final String TAG = CarSystemBarElementResolver.class.getSimpleName(); 32 33 /** Convert a class string to a class instance of CarSystemBarElementController */ getElementControllerClassFromString(String str)34 public static Class<?> getElementControllerClassFromString(String str) { 35 if (!TextUtils.isEmpty(str)) { 36 try { 37 Class<?> clazz = Class.forName(str); 38 if (clazz != null && CarSystemBarElementController.class.isAssignableFrom(clazz)) { 39 return clazz; 40 } 41 } catch (ClassNotFoundException e) { 42 Log.w(TAG, "cannot find class for string " + str, e); 43 } 44 } 45 return null; 46 } 47 48 /** Get the element controller class from the specified view attributes */ 49 @Nullable getElementControllerClassFromAttributes(@onNull Context context, @Nullable AttributeSet attrs)50 public static Class<?> getElementControllerClassFromAttributes(@NonNull Context context, 51 @Nullable AttributeSet attrs) { 52 if (attrs == null) { 53 return null; 54 } 55 TypedArray typedArray = context.obtainStyledAttributes(attrs, 56 R.styleable.CarSystemBarElement); 57 String str = typedArray.getString(R.styleable.CarSystemBarElement_controller); 58 typedArray.recycle(); 59 if (TextUtils.isEmpty(str)) { 60 return null; 61 } 62 return getElementControllerClassFromString(str); 63 } 64 } 65