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

Commit 104e04da authored by Jason Chiu's avatar Jason Chiu
Browse files

Delegate PrintServicesLoader to the framework's one

1. Remove the duplicated PrintServicesLoader in Settings
2. Make a androidx.loader in Settings and internally just delegate all calls to the real PrintServicesLoader
3. Get the result from the real loader

Test: manual
Fixes: 111581651
Change-Id: I2a9b3653f5c68f8383a468cd16ef5f7c3fd4bc3a
parent ec7a9df6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -286,7 +286,7 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment

    @Override
    public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) {
        return new PrintServicesLoader(
        return new SettingsPrintServicesLoader(
                (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE), getContext(),
                PrintManager.ALL_SERVICES);
    }
+1 −1
Original line number Diff line number Diff line
@@ -168,7 +168,7 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
            PrintManager printManager =
                    (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE);
            if (printManager != null) {
                return new PrintServicesLoader(printManager, getContext(),
                return new SettingsPrintServicesLoader(printManager, getContext(),
                        PrintManager.ALL_SERVICES);
            } else {
                return null;
+82 −0
Original line number Diff line number Diff line
@@ -18,9 +18,8 @@ package com.android.settings.print;

import android.annotation.NonNull;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.print.PrintManager;
import android.print.PrintServicesLoader;
import android.printservice.PrintServiceInfo;

import com.android.internal.util.Preconditions;
@@ -31,96 +30,53 @@ import androidx.loader.content.Loader;

/**
 * Loader for the list of print services. Can be parametrized to select a subset.
 *
 */
public class PrintServicesLoader extends Loader<List<PrintServiceInfo>> {
    /** What type of services to load. */
    private final int mSelectionFlags;

    /** The print manager to be used by this object */
    private final @NonNull PrintManager mPrintManager;
public class SettingsPrintServicesLoader extends Loader<List<PrintServiceInfo>> {

    /** Handler to sequentialize the delivery of the results to the main thread */
    private final @NonNull Handler mHandler;
    private PrintServicesLoader mLoader;

    /** Listens for updates to the data from the platform */
    private PrintManager.PrintServicesChangeListener mListener;

    /**
     * Create a new PrintServicesLoader.
     *
     * @param printManager   The print manager supplying the data
     * @param context        Context of the using object
     * @param selectionFlags What type of services to load.
     */
    public PrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
    public SettingsPrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context,
            int selectionFlags) {
        super(Preconditions.checkNotNull(context));
        mHandler = new MyHandler();
        mPrintManager = Preconditions.checkNotNull(printManager);
        mSelectionFlags = Preconditions.checkFlagsArgument(selectionFlags,
                PrintManager.ALL_SERVICES);
    }

        mLoader = new PrintServicesLoader(printManager, context, selectionFlags) {
            @Override
    protected void onForceLoad() {
        queueNewResult();
    }
            public void deliverResult(List<PrintServiceInfo> data) {
                super.deliverResult(data);

    /**
     * Read the print services and queue it to be delivered on the main thread.
     */
    private void queueNewResult() {
        Message m = mHandler.obtainMessage(0);
        m.obj = mPrintManager.getPrintServices(mSelectionFlags);
        mHandler.sendMessage(m);
                // deliver the result to outer Loader class
                SettingsPrintServicesLoader.this.deliverResult(data);
            }
        };
    }

    @Override
    protected void onStartLoading() {
        mListener = new PrintManager.PrintServicesChangeListener() {
            @Override public void onPrintServicesChanged() {
                queueNewResult();
    protected void onForceLoad() {
        mLoader.forceLoad();
    }
        };

        mPrintManager.addPrintServicesChangeListener(mListener, null);

        // Immediately deliver a result
        deliverResult(mPrintManager.getPrintServices(mSelectionFlags));
    @Override
    protected void onStartLoading() {
        mLoader.startLoading();
    }

    @Override
    protected void onStopLoading() {
        if (mListener != null) {
            mPrintManager.removePrintServicesChangeListener(mListener);
            mListener = null;
        }

        mHandler.removeMessages(0);
        mLoader.stopLoading();
    }

    @Override
    protected void onReset() {
        onStopLoading();
    protected boolean onCancelLoad() {
        return mLoader.cancelLoad();
    }

    /**
     * Handler to sequentialize all the updates to the main thread.
     */
    private class MyHandler extends Handler {
        /**
         * Create a new handler on the main thread.
         */
        public MyHandler() {
            super(getContext().getMainLooper());
    @Override
    protected void onAbandon() {
        mLoader.abandon();
    }

    @Override
        public void handleMessage(Message msg) {
            if (isStarted()) {
                deliverResult((List<PrintServiceInfo>) msg.obj);
            }
        }
    protected void onReset() {
        mLoader.reset();
    }
}