From 104e04dab4248c6fd388a26d934654c870c5cf95 Mon Sep 17 00:00:00 2001 From: Jason Chiu Date: Wed, 18 Jul 2018 16:39:29 +0800 Subject: [PATCH] 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 --- .../print/PrintServiceSettingsFragment.java | 2 +- .../settings/print/PrintServicesLoader.java | 126 ------------------ .../settings/print/PrintSettingsFragment.java | 2 +- .../print/SettingsPrintServicesLoader.java | 82 ++++++++++++ 4 files changed, 84 insertions(+), 128 deletions(-) delete mode 100644 src/com/android/settings/print/PrintServicesLoader.java create mode 100644 src/com/android/settings/print/SettingsPrintServicesLoader.java diff --git a/src/com/android/settings/print/PrintServiceSettingsFragment.java b/src/com/android/settings/print/PrintServiceSettingsFragment.java index 1311be0af87..345b4aea927 100644 --- a/src/com/android/settings/print/PrintServiceSettingsFragment.java +++ b/src/com/android/settings/print/PrintServiceSettingsFragment.java @@ -286,7 +286,7 @@ public class PrintServiceSettingsFragment extends SettingsPreferenceFragment @Override public Loader> onCreateLoader(int id, Bundle args) { - return new PrintServicesLoader( + return new SettingsPrintServicesLoader( (PrintManager) getContext().getSystemService(Context.PRINT_SERVICE), getContext(), PrintManager.ALL_SERVICES); } diff --git a/src/com/android/settings/print/PrintServicesLoader.java b/src/com/android/settings/print/PrintServicesLoader.java deleted file mode 100644 index 57cddb9b167..00000000000 --- a/src/com/android/settings/print/PrintServicesLoader.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -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.printservice.PrintServiceInfo; - -import com.android.internal.util.Preconditions; - -import java.util.List; - -import androidx.loader.content.Loader; - -/** - * Loader for the list of print services. Can be parametrized to select a subset. - * - */ -public class PrintServicesLoader extends Loader> { - /** What type of services to load. */ - private final int mSelectionFlags; - - /** The print manager to be used by this object */ - private final @NonNull PrintManager mPrintManager; - - /** Handler to sequentialize the delivery of the results to the main thread */ - private final @NonNull Handler mHandler; - - /** 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, - int selectionFlags) { - super(Preconditions.checkNotNull(context)); - mHandler = new MyHandler(); - mPrintManager = Preconditions.checkNotNull(printManager); - mSelectionFlags = Preconditions.checkFlagsArgument(selectionFlags, - PrintManager.ALL_SERVICES); - } - - @Override - protected void onForceLoad() { - queueNewResult(); - } - - /** - * 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); - } - - @Override - protected void onStartLoading() { - mListener = new PrintManager.PrintServicesChangeListener() { - @Override public void onPrintServicesChanged() { - queueNewResult(); - } - }; - - mPrintManager.addPrintServicesChangeListener(mListener, null); - - // Immediately deliver a result - deliverResult(mPrintManager.getPrintServices(mSelectionFlags)); - } - - @Override - protected void onStopLoading() { - if (mListener != null) { - mPrintManager.removePrintServicesChangeListener(mListener); - mListener = null; - } - - mHandler.removeMessages(0); - } - - @Override - protected void onReset() { - onStopLoading(); - } - - /** - * 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 - public void handleMessage(Message msg) { - if (isStarted()) { - deliverResult((List) msg.obj); - } - } - } -} diff --git a/src/com/android/settings/print/PrintSettingsFragment.java b/src/com/android/settings/print/PrintSettingsFragment.java index a6b3d7eea07..899acc70942 100644 --- a/src/com/android/settings/print/PrintSettingsFragment.java +++ b/src/com/android/settings/print/PrintSettingsFragment.java @@ -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; diff --git a/src/com/android/settings/print/SettingsPrintServicesLoader.java b/src/com/android/settings/print/SettingsPrintServicesLoader.java new file mode 100644 index 00000000000..758f4d3f4f0 --- /dev/null +++ b/src/com/android/settings/print/SettingsPrintServicesLoader.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.settings.print; + +import android.annotation.NonNull; +import android.content.Context; +import android.print.PrintManager; +import android.print.PrintServicesLoader; +import android.printservice.PrintServiceInfo; + +import com.android.internal.util.Preconditions; + +import java.util.List; + +import androidx.loader.content.Loader; + +/** + * Loader for the list of print services. Can be parametrized to select a subset. + */ +public class SettingsPrintServicesLoader extends Loader> { + + private PrintServicesLoader mLoader; + + public SettingsPrintServicesLoader(@NonNull PrintManager printManager, @NonNull Context context, + int selectionFlags) { + super(Preconditions.checkNotNull(context)); + + mLoader = new PrintServicesLoader(printManager, context, selectionFlags) { + @Override + public void deliverResult(List data) { + super.deliverResult(data); + + // deliver the result to outer Loader class + SettingsPrintServicesLoader.this.deliverResult(data); + } + }; + } + + @Override + protected void onForceLoad() { + mLoader.forceLoad(); + } + + @Override + protected void onStartLoading() { + mLoader.startLoading(); + } + + @Override + protected void onStopLoading() { + mLoader.stopLoading(); + } + + @Override + protected boolean onCancelLoad() { + return mLoader.cancelLoad(); + } + + @Override + protected void onAbandon() { + mLoader.abandon(); + } + + @Override + protected void onReset() { + mLoader.reset(); + } +}