From ab121c17159a7f655b772fa78c04a6d2479e2a8e Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Fri, 2 Dec 2016 19:29:43 +0530 Subject: [PATCH] Adding a generic method to initiate overridable objects Change-Id: Ia433427c65ad38804f2eed9c6bc209df232758c0 --- src/com/android/launcher3/AllAppsList.java | 2 +- src/com/android/launcher3/AppFilter.java | 31 ++----------------- src/com/android/launcher3/IconCache.java | 5 ++- src/com/android/launcher3/IconProvider.java | 17 ---------- .../android/launcher3/LauncherAppState.java | 5 ++- src/com/android/launcher3/Utilities.java | 20 ++++++++++++ .../launcher3/graphics/DrawableFactory.java | 22 ++----------- .../android/launcher3/model/WidgetsModel.java | 2 +- 8 files changed, 32 insertions(+), 72 deletions(-) diff --git a/src/com/android/launcher3/AllAppsList.java b/src/com/android/launcher3/AllAppsList.java index b13c20b204..bbc865036d 100644 --- a/src/com/android/launcher3/AllAppsList.java +++ b/src/com/android/launcher3/AllAppsList.java @@ -66,7 +66,7 @@ public class AllAppsList { * If the app is already in the list, doesn't add it. */ public void add(AppInfo info) { - if (mAppFilter != null && !mAppFilter.shouldShowApp(info.componentName)) { + if (!mAppFilter.shouldShowApp(info.componentName)) { return; } if (findActivity(data, info.componentName, info.user)) { diff --git a/src/com/android/launcher3/AppFilter.java b/src/com/android/launcher3/AppFilter.java index e01436d7ab..db8f5dd0e0 100644 --- a/src/com/android/launcher3/AppFilter.java +++ b/src/com/android/launcher3/AppFilter.java @@ -1,35 +1,10 @@ package com.android.launcher3; import android.content.ComponentName; -import android.text.TextUtils; -import android.util.Log; -public abstract class AppFilter { +public class AppFilter { - private static final boolean DBG = false; - private static final String TAG = "AppFilter"; - - public abstract boolean shouldShowApp(ComponentName app); - - public static AppFilter loadByName(String className) { - if (TextUtils.isEmpty(className)) return null; - if (DBG) Log.d(TAG, "Loading AppFilter: " + className); - try { - Class cls = Class.forName(className); - return (AppFilter) cls.newInstance(); - } catch (ClassNotFoundException e) { - Log.e(TAG, "Bad AppFilter class", e); - return null; - } catch (InstantiationException e) { - Log.e(TAG, "Bad AppFilter class", e); - return null; - } catch (IllegalAccessException e) { - Log.e(TAG, "Bad AppFilter class", e); - return null; - } catch (ClassCastException e) { - Log.e(TAG, "Bad AppFilter class", e); - return null; - } + public boolean shouldShowApp(ComponentName app) { + return true; } - } diff --git a/src/com/android/launcher3/IconCache.java b/src/com/android/launcher3/IconCache.java index db72b2f997..4b09bf82d9 100644 --- a/src/com/android/launcher3/IconCache.java +++ b/src/com/android/launcher3/IconCache.java @@ -123,9 +123,8 @@ public class IconCache { mLowResCanvas = new Canvas(); mLowResPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG); - mIconProvider = IconProvider.loadByName(context.getString(R.string.icon_provider_class), - context); - + mIconProvider = Utilities.getOverrideObject( + IconProvider.class, context, R.string.icon_provider_class); mWorkerHandler = new Handler(LauncherModel.getWorkerLooper()); mActivityBgColor = context.getResources().getColor(R.color.quantum_panel_bg_color); diff --git a/src/com/android/launcher3/IconProvider.java b/src/com/android/launcher3/IconProvider.java index 0a273bbc3b..005bbaa02c 100644 --- a/src/com/android/launcher3/IconProvider.java +++ b/src/com/android/launcher3/IconProvider.java @@ -1,13 +1,9 @@ package com.android.launcher3; -import android.content.Context; import android.graphics.drawable.Drawable; -import android.text.TextUtils; -import android.util.Log; import com.android.launcher3.compat.LauncherActivityInfoCompat; -import java.lang.reflect.InvocationTargetException; import java.util.Locale; public class IconProvider { @@ -21,19 +17,6 @@ public class IconProvider { updateSystemStateString(); } - public static IconProvider loadByName(String className, Context context) { - if (TextUtils.isEmpty(className)) return new IconProvider(); - if (DBG) Log.d(TAG, "Loading IconProvider: " + className); - try { - Class cls = Class.forName(className); - return (IconProvider) cls.getDeclaredConstructor(Context.class).newInstance(context); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException - | ClassCastException | NoSuchMethodException | InvocationTargetException e) { - Log.e(TAG, "Bad IconProvider class", e); - return new IconProvider(); - } - } - public void updateSystemStateString() { mSystemState = Locale.getDefault().toString(); } diff --git a/src/com/android/launcher3/LauncherAppState.java b/src/com/android/launcher3/LauncherAppState.java index 5937d78fcd..ca5cd74304 100644 --- a/src/com/android/launcher3/LauncherAppState.java +++ b/src/com/android/launcher3/LauncherAppState.java @@ -38,7 +38,6 @@ public class LauncherAppState { public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD; - private final AppFilter mAppFilter; @Thunk final LauncherModel mModel; private final IconCache mIconCache; private final WidgetPreviewLoader mWidgetCache; @@ -96,8 +95,8 @@ public class LauncherAppState { mIconCache = new IconCache(sContext, mInvariantDeviceProfile); mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache); - mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class)); - mModel = new LauncherModel(this, mIconCache, mAppFilter); + mModel = new LauncherModel(this, mIconCache, + Utilities.getOverrideObject(AppFilter.class, sContext, R.string.app_filter_class)); LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel); diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java index 416ca8e3ec..197aaf752c 100644 --- a/src/com/android/launcher3/Utilities.java +++ b/src/com/android/launcher3/Utilities.java @@ -60,6 +60,7 @@ import com.android.launcher3.config.ProviderConfig; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Collection; import java.util.Locale; @@ -655,4 +656,23 @@ public final class Utilities { return e.getCause() instanceof TransactionTooLargeException || e.getCause() instanceof DeadObjectException; } + + public static T getOverrideObject(Class clazz, Context context, int resId) { + String className = context.getString(resId); + if (!TextUtils.isEmpty(className)) { + try { + Class cls = Class.forName(className); + return (T) cls.getDeclaredConstructor(Context.class).newInstance(context); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | ClassCastException | NoSuchMethodException | InvocationTargetException e) { + Log.e(TAG, "Bad overriden class", e); + } + } + + try { + return clazz.newInstance(); + } catch (InstantiationException|IllegalAccessException e) { + throw new RuntimeException(e); + } + } } diff --git a/src/com/android/launcher3/graphics/DrawableFactory.java b/src/com/android/launcher3/graphics/DrawableFactory.java index 2926a29d6a..f6a4b844f9 100644 --- a/src/com/android/launcher3/graphics/DrawableFactory.java +++ b/src/com/android/launcher3/graphics/DrawableFactory.java @@ -18,13 +18,11 @@ package com.android.launcher3.graphics; import android.content.Context; import android.graphics.Bitmap; -import android.text.TextUtils; import com.android.launcher3.FastBitmapDrawable; import com.android.launcher3.ItemInfo; import com.android.launcher3.R; - -import java.lang.reflect.InvocationTargetException; +import com.android.launcher3.Utilities; /** * Factory for creating new drawables. @@ -37,27 +35,13 @@ public class DrawableFactory { public static DrawableFactory get(Context context) { synchronized (LOCK) { if (sInstance == null) { - context = context.getApplicationContext(); - sInstance = loadByName(context.getString(R.string.drawable_factory_class), context); + sInstance = Utilities.getOverrideObject(DrawableFactory.class, + context.getApplicationContext(), R.string.drawable_factory_class); } return sInstance; } } - public static DrawableFactory loadByName(String className, Context context) { - if (!TextUtils.isEmpty(className)) { - try { - Class cls = Class.forName(className); - return (DrawableFactory) - cls.getDeclaredConstructor(Context.class).newInstance(context); - } catch (ClassNotFoundException | InstantiationException | IllegalAccessException - | ClassCastException | NoSuchMethodException | InvocationTargetException e) { - return new DrawableFactory(); - } - } - return new DrawableFactory(); - } - /** * Returns a FastBitmapDrawable with the icon. */ diff --git a/src/com/android/launcher3/model/WidgetsModel.java b/src/com/android/launcher3/model/WidgetsModel.java index 64043f4843..5ad6f0fff2 100644 --- a/src/com/android/launcher3/model/WidgetsModel.java +++ b/src/com/android/launcher3/model/WidgetsModel.java @@ -117,7 +117,7 @@ public class WidgetsModel { } } - if (mAppFilter != null && !mAppFilter.shouldShowApp(item.componentName)) { + if (!mAppFilter.shouldShowApp(item.componentName)) { if (DEBUG) { Log.d(TAG, String.format("%s is filtered and not added to the widget tray.", item.componentName));