1 /*
2  * Copyright (C) 2018 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.launcher3.util;
17 
18 import android.content.Context;
19 import android.text.TextUtils;
20 import android.util.Log;
21 
22 import java.lang.reflect.InvocationTargetException;
23 
24 /**
25  * An interface to indicate that a class is dynamically loaded using resource overlay, hence its
26  * class name and constructor should be preserved by proguard
27  */
28 public interface ResourceBasedOverride {
29 
30     class Overrides {
31 
32         private static final String TAG = "Overrides";
33 
getObject( Class<T> clazz, Context context, int resId)34         public static <T extends ResourceBasedOverride> T getObject(
35                 Class<T> clazz, Context context, int resId) {
36             String className = context.getString(resId);
37             if (!TextUtils.isEmpty(className)) {
38                 try {
39                     Class<?> cls = Class.forName(className);
40                     return (T) cls.getDeclaredConstructor(Context.class).newInstance(context);
41                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
42                         | ClassCastException | NoSuchMethodException | InvocationTargetException e) {
43                     Log.e(TAG, "Bad overriden class", e);
44                 }
45             }
46 
47             try {
48                 return clazz.newInstance();
49             } catch (InstantiationException|IllegalAccessException e) {
50                 throw new RuntimeException(e);
51             }
52         }
53     }
54 }
55