Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 65da3306 authored by Fan Zhang's avatar Fan Zhang Committed by Android (Google) Code Review
Browse files

Merge changes If1b263df,I95afbcf0

* changes:
  Move instantiation-through-reflection method to controller
  Minor cleanup in DashboardFragment: remove unused code
parents 302cf359 fc520ee3
Loading
Loading
Loading
Loading
+40 −0
Original line number Diff line number Diff line
@@ -25,6 +25,8 @@ import com.android.settingslib.core.lifecycle.Lifecycle;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
@@ -73,6 +75,44 @@ public abstract class BasePreferenceController extends AbstractPreferenceControl

    protected Lifecycle mLifecycle;

    /**
     * Instantiate a controller as specified controller type and user-defined key.
     * <p/>
     * This is done through reflection. Do not use this method unless you know what you are doing.
     */
    public static BasePreferenceController createInstance(Context context,
            String controllerName, String key) {
        try {
            final Class<?> clazz = Class.forName(controllerName);
            final Constructor<?> preferenceConstructor =
                    clazz.getConstructor(Context.class, String.class);
            final Object[] params = new Object[] {context, key};
            return (BasePreferenceController) preferenceConstructor.newInstance(params);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
                IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
            throw new IllegalStateException(
                    "Invalid preference controller: " + controllerName, e);
        }
    }

    /**
     * Instantiate a controller as specified controller type.
     * <p/>
     * This is done through reflection. Do not use this method unless you know what you are doing.
     */
    public static BasePreferenceController createInstance(Context context, String controllerName) {
        try {
            final Class<?> clazz = Class.forName(controllerName);
            final Constructor<?> preferenceConstructor = clazz.getConstructor(Context.class);
            final Object[] params = new Object[] {context};
            return (BasePreferenceController) preferenceConstructor.newInstance(params);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
                IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
            throw new IllegalStateException(
                    "Invalid preference controller: " + controllerName, e);
        }
    }

    public BasePreferenceController(Context context, String preferenceKey) {
        super(context);
        mPreferenceKey = preferenceKey;
+1 −11
Original line number Diff line number Diff line
@@ -27,9 +27,6 @@ import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.overlay.FeatureFactory;
@@ -58,7 +55,7 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
            new ArrayMap<>();
    private final Set<String> mDashboardTilePrefKeys = new ArraySet<>();

    protected DashboardFeatureProvider mDashboardFeatureProvider;
    private DashboardFeatureProvider mDashboardFeatureProvider;
    private DashboardTilePlaceholderPreferenceController mPlaceholderPreferenceController;
    private boolean mListeningToCategoryChange;
    private SummaryLoader mSummaryLoader;
@@ -94,13 +91,6 @@ public abstract class DashboardFragment extends SettingsPreferenceFragment
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = super.onCreateView(inflater, container, savedInstanceState);
        return view;
    }

    @Override
    public void onCategoriesChanged() {
        final DashboardCategory category =
+4 −37
Original line number Diff line number Diff line
@@ -33,9 +33,6 @@ import com.android.settings.core.TogglePreferenceController;
import com.android.settings.search.DatabaseIndexingUtils;
import com.android.settingslib.core.AbstractPreferenceController;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import androidx.app.slice.Slice;
import androidx.app.slice.builders.ListBuilder;
import androidx.app.slice.builders.ListBuilder.RowBuilder;
@@ -87,46 +84,16 @@ public class SliceBuilderUtils {
    public static BasePreferenceController getPreferenceController(Context context,
            SliceData sliceData) {
        try {
            return getController(context, sliceData, true /* isContextOnly */);
            return BasePreferenceController.createInstance(context,
                    sliceData.getPreferenceController());
        } catch (IllegalStateException e) {
            // Do nothing
            Log.d(TAG, "Could not find Context-only controller for preference controller: "
                    + sliceData.getKey());
        }

        return getController(context, sliceData, false /* isContextOnly */);
    }

    /**
     * Attempts to build a {@link BasePreferenceController} from {@param SliceData}.
     *
     * @param sliceData     Backing data for the Slice.
     * @param contextOnlyCtor {@code true} when the constructor for the
     *                      {@link BasePreferenceController}
     *                      only takes a {@link Context}. Else the constructor will be ({@link
     *                      Context}, {@code String}.
     */
    private static BasePreferenceController getController(Context context, SliceData sliceData,
            boolean contextOnlyCtor) {
        try {
            Class<?> clazz = Class.forName(sliceData.getPreferenceController());
            Constructor<?> preferenceConstructor;
            Object[] params;

            if (contextOnlyCtor) {
                preferenceConstructor = clazz.getConstructor(Context.class);
                params = new Object[]{context};
            } else {
                preferenceConstructor = clazz.getConstructor(Context.class, String.class);
                params = new Object[]{context, sliceData.getKey()};
            }

            return (BasePreferenceController) preferenceConstructor.newInstance(params);
        } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException |
                IllegalArgumentException | InvocationTargetException | IllegalAccessException e) {
            throw new IllegalStateException(
                    "Invalid preference controller: " + sliceData.getPreferenceController(), e);
        }
        return BasePreferenceController.createInstance(context, sliceData.getPreferenceController(),
                sliceData.getKey());
    }

    private static void addToggleAction(Context context, RowBuilder builder, boolean isChecked,