OSDN Git Service

Adding a generic method to initiate overridable objects
authorSunny Goyal <sunnygoyal@google.com>
Fri, 2 Dec 2016 13:59:43 +0000 (19:29 +0530)
committerSunny Goyal <sunnygoyal@google.com>
Mon, 12 Dec 2016 17:01:11 +0000 (17:01 +0000)
Change-Id: Ia433427c65ad38804f2eed9c6bc209df232758c0

src/com/android/launcher3/AllAppsList.java
src/com/android/launcher3/AppFilter.java
src/com/android/launcher3/IconCache.java
src/com/android/launcher3/IconProvider.java
src/com/android/launcher3/LauncherAppState.java
src/com/android/launcher3/Utilities.java
src/com/android/launcher3/graphics/DrawableFactory.java
src/com/android/launcher3/model/WidgetsModel.java

index b13c20b..bbc8650 100644 (file)
@@ -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)) {
index e01436d..db8f5dd 100644 (file)
@@ -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;
     }
-
 }
index db72b2f..4b09bf8 100644 (file)
@@ -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);
index 0a273bb..005bbaa 100644 (file)
@@ -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();
     }
index 5937d78..ca5cd74 100644 (file)
@@ -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);
 
index 416ca8e..197aaf7 100644 (file)
@@ -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> T getOverrideObject(Class<T> 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);
+        }
+    }
 }
index 2926a29..f6a4b84 100644 (file)
@@ -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.
      */
index 64043f4..5ad6f0f 100644 (file)
@@ -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));