diff --git a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java index c0e0587fac..bc351251c6 100644 --- a/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java +++ b/quickstep/src/com/android/launcher3/model/QuickstepModelDelegate.java @@ -111,12 +111,11 @@ public class QuickstepModelDelegate extends ModelDelegate { private final InvariantDeviceProfile mIDP; private final AppEventProducer mAppEventProducer; private final StatsManager mStatsManager; - private final Context mContext; protected boolean mActive = false; public QuickstepModelDelegate(Context context) { - mContext = context; + super(context); mAppEventProducer = new AppEventProducer(context, this::onAppTargetEvent); mIDP = InvariantDeviceProfile.INSTANCE.get(context); diff --git a/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java b/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java index e078a497c9..a09e02796b 100644 --- a/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java +++ b/quickstep/src/com/android/quickstep/logging/StatsLogCompatManager.java @@ -113,7 +113,7 @@ public class StatsLogCompatManager extends StatsLogManager { new CopyOnWriteArrayList<>(); public StatsLogCompatManager(Context context) { - mContext = context; + super(context); } @Override diff --git a/src/com/android/launcher3/logging/StatsLogManager.java b/src/com/android/launcher3/logging/StatsLogManager.java index 52fb122994..e8f8ae25ea 100644 --- a/src/com/android/launcher3/logging/StatsLogManager.java +++ b/src/com/android/launcher3/logging/StatsLogManager.java @@ -23,6 +23,7 @@ import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCH import android.content.Context; import android.view.View; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.slice.SliceItem; @@ -53,11 +54,18 @@ public class StatsLogManager implements ResourceBasedOverride { public static final int LAUNCHER_STATE_ALLAPPS = 4; public static final int LAUNCHER_STATE_UNCHANGED = 5; + @NonNull + protected final Context mContext; + @Nullable + protected final ActivityContext mActivityContext; + + private KeyboardStateManager mKeyboardStateManager; private InstanceId mInstanceId; - protected @Nullable ActivityContext mActivityContext = null; - protected @Nullable Context mContext = null; - private KeyboardStateManager mKeyboardStateManager; + public StatsLogManager(@NonNull Context context) { + mContext = context; + mActivityContext = ActivityContext.lookupContextNoThrow(context); + } /** * Returns event enum based on the two state transition information when swipe @@ -1194,10 +1202,7 @@ public class StatsLogManager implements ResourceBasedOverride { * Creates a new instance of {@link StatsLogManager} based on provided context. */ public static StatsLogManager newInstance(Context context) { - StatsLogManager manager = Overrides.getObject(StatsLogManager.class, + return Overrides.getObject(StatsLogManager.class, context.getApplicationContext(), R.string.stats_log_manager_class); - manager.mActivityContext = ActivityContext.lookupContextNoThrow(context); - manager.mContext = context; - return manager; } } diff --git a/src/com/android/launcher3/model/ModelDelegate.java b/src/com/android/launcher3/model/ModelDelegate.java index 7e7bfb398f..8360b14fa4 100644 --- a/src/com/android/launcher3/model/ModelDelegate.java +++ b/src/com/android/launcher3/model/ModelDelegate.java @@ -45,28 +45,29 @@ public class ModelDelegate implements ResourceBasedOverride { boolean isPrimaryInstance) { ModelDelegate delegate = Overrides.getObject( ModelDelegate.class, context, R.string.model_delegate_class); - delegate.init(context, app, appsList, dataModel, isPrimaryInstance); + delegate.init(app, appsList, dataModel, isPrimaryInstance); return delegate; } - protected Context mContext; + protected final Context mContext; protected LauncherAppState mApp; protected AllAppsList mAppsList; protected BgDataModel mDataModel; protected boolean mIsPrimaryInstance; - public ModelDelegate() { } + public ModelDelegate(Context context) { + mContext = context; + } /** * Initializes the object with the given params. */ - private void init(Context context, LauncherAppState app, AllAppsList appsList, + private void init(LauncherAppState app, AllAppsList appsList, BgDataModel dataModel, boolean isPrimaryInstance) { this.mApp = app; this.mAppsList = appsList; this.mDataModel = dataModel; this.mIsPrimaryInstance = isPrimaryInstance; - this.mContext = context; } /** Called periodically to validate and update any data */ diff --git a/src/com/android/launcher3/util/ResourceBasedOverride.java b/src/com/android/launcher3/util/ResourceBasedOverride.java index e2c4992a44..36b9cf7420 100644 --- a/src/com/android/launcher3/util/ResourceBasedOverride.java +++ b/src/com/android/launcher3/util/ResourceBasedOverride.java @@ -34,16 +34,20 @@ public interface ResourceBasedOverride { public static T getObject( 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) { + boolean isOverridden = !TextUtils.isEmpty(className); + + // First try to load the class with "Context" param + try { + Class cls = isOverridden ? Class.forName(className) : clazz; + return (T) cls.getDeclaredConstructor(Context.class).newInstance(context); + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException + | ClassCastException | NoSuchMethodException | InvocationTargetException e) { + if (isOverridden) { Log.e(TAG, "Bad overriden class", e); } } + // Load the base class with no parameter try { return clazz.newInstance(); } catch (InstantiationException|IllegalAccessException e) {