Allowing base class for ResourceBasedOverride to have Context based constructors

Bug: 330920490
Flag: None
Test: Presubmit
Change-Id: Ib4d1ef80133596a114a4fb32ea8ae918852a77f5
This commit is contained in:
Sunny Goyal
2024-04-05 00:42:00 -07:00
parent e0330e17e3
commit 9d28eee769
5 changed files with 30 additions and 21 deletions
@@ -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);
@@ -113,7 +113,7 @@ public class StatsLogCompatManager extends StatsLogManager {
new CopyOnWriteArrayList<>();
public StatsLogCompatManager(Context context) {
mContext = context;
super(context);
}
@Override
@@ -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;
}
}
@@ -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 */
@@ -34,16 +34,20 @@ public interface ResourceBasedOverride {
public static <T extends ResourceBasedOverride> T getObject(
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) {
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) {