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

Commit b3f13f9f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Update print setting summary to show number of services"

parents ae2850ae 01ece62f
Loading
Loading
Loading
Loading
+7 −4
Original line number Diff line number Diff line
@@ -4239,10 +4239,13 @@
    <!-- Title in main settings screen for printing settings [CHAR LIMIT=15] -->
    <string name="print_settings">Printing</string>
    <!-- Print frameworks summary in settings screen [CHAR LIMIT=50] -->
    <plurals name="print_settings_title">
        <item quantity="one">1 print job</item>
        <item quantity="other">%d print jobs</item>
    <!-- Print setting summary in settings screen [CHAR LIMIT=50] -->
    <string name="print_settings_summary_no_service">No print service</string>
    <!-- Print setting summary in settings screen [CHAR LIMIT=50] -->
    <plurals name="print_settings_summary">
        <item quantity="one">1 print service</item>
        <item quantity="other"><xliff:g id="count">%1$d</xliff:g> print services</item>
    </plurals>
    <!-- Title for print service settings screen [CHAR LIMIT=25] -->
+45 −43
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.settings.print;

import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.ActivityNotFoundException;
import android.content.AsyncTaskLoader;
@@ -37,9 +36,9 @@ import android.print.PrintServicesLoader;
import android.printservice.PrintServiceInfo;
import android.provider.SearchIndexableResource;
import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
@@ -191,12 +190,14 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment

            mPrintServicesCategory.removeAll();
            PackageManager pm = getActivity().getPackageManager();
            final Context context = getPrefContext();
            if (context == null) {
                Log.w(TAG, "No preference context, skip adding print services");
                return;
            }

            final int numServices = services.size();
            for (int i = 0; i < numServices; i++) {
                PrintServiceInfo service = services.get(i);
                PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(
                        getActivity());
            for (PrintServiceInfo service : services) {
                Preference preference = new Preference(context);

                String title = service.getResolveInfo().loadLabel(pm).toString();
                preference.setTitle(title);
@@ -310,13 +311,14 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
                }

                mActivePrintJobsCategory.removeAll();
                final Context context = getPrefContext();
                if (context == null) {
                    Log.w(TAG, "No preference context, skip adding print jobs");
                    return;
                }

                final int printJobCount = printJobs.size();
                for (int i = 0; i < printJobCount; i++) {
                    PrintJobInfo printJob = printJobs.get(i);

                    PreferenceScreen preference = getPreferenceManager()
                            .createPreferenceScreen(getActivity());
                for (PrintJobInfo printJob : printJobs) {
                    Preference preference = new Preference(context);

                    preference.setPersistent(false);
                    preference.setFragment(PrintJobSettingsFragment.class.getName());
@@ -503,10 +505,10 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
    /**
     * Provider for the print settings summary
     */
    private static class PrintSummaryProvider
            implements SummaryLoader.SummaryProvider, PrintJobStateChangeListener {
    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
    static class PrintSummaryProvider implements SummaryLoader.SummaryProvider {
        private final Context mContext;
        private final PrintManager mPrintManager;
        private final PrintManagerWrapper mPrintManager;
        private final SummaryLoader mSummaryLoader;

        /**
@@ -515,39 +517,44 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
         * @param context       The context this provider is for
         * @param summaryLoader The summary load using this provider
         */
        public PrintSummaryProvider(Context context, SummaryLoader summaryLoader) {
        PrintSummaryProvider(Context context, SummaryLoader summaryLoader,
                PrintManagerWrapper printManager) {
            mContext = context;
            mSummaryLoader = summaryLoader;
            mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
                    .getGlobalPrintManagerForUser(context.getUserId());
            mPrintManager = printManager;
        }

        @Override
        public void setListening(boolean isListening) {
            if (mPrintManager != null) {
                if (isListening) {
                    mPrintManager.addPrintJobStateChangeListener(this);
                    onPrintJobStateChanged(null);
                    List<PrintServiceInfo> services =
                            mPrintManager.getPrintServices(PrintManager.ALL_SERVICES);
                    if (services == null || services.isEmpty()) {
                        mSummaryLoader.setSummary(this,
                                mContext.getString(R.string.print_settings_summary_no_service));
                    } else {
                    mPrintManager.removePrintJobStateChangeListener(this);
                        final int count = services.size();
                        mSummaryLoader.setSummary(this,
                                mContext.getResources().getQuantityString(
                                        R.plurals.print_settings_summary, count, count));
                    }
                }
            }
        }

        @Override
        public void onPrintJobStateChanged(PrintJobId printJobId) {
            List<PrintJob> printJobs = mPrintManager.getPrintJobs();
        static class PrintManagerWrapper {

            int numActivePrintJobs = 0;
            final int numPrintJobs = printJobs.size();
            for (int i = 0; i < numPrintJobs; i++) {
                if (shouldShowToUser(printJobs.get(i).getInfo())) {
                    numActivePrintJobs++;
                }
            private final PrintManager mPrintManager;

            PrintManagerWrapper(Context context) {
                mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
                        .getGlobalPrintManagerForUser(context.getUserId());
            }

            mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString(
                    R.plurals.print_settings_title, numActivePrintJobs, numActivePrintJobs));
            public List<PrintServiceInfo> getPrintServices(int selectionFlags) {
                return mPrintManager.getPrintServices(selectionFlags);
            }
        }
    }

@@ -555,14 +562,9 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
     * A factory for {@link PrintSummaryProvider providers} the settings app can use to read the
     * print summary.
     */
    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
            = new SummaryLoader.SummaryProviderFactory() {
        @Override
        public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
                SummaryLoader summaryLoader) {
            return new PrintSummaryProvider(activity, summaryLoader);
        }
    };
    public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =
            (activity, summaryLoader) -> new PrintSummaryProvider(activity, summaryLoader,
                    new PrintSummaryProvider.PrintManagerWrapper(activity));

    public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider() {
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Activity;
import android.content.res.Resources;
import android.print.PrintManager;
import android.printservice.PrintServiceInfo;

import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.dashboard.SummaryLoader;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.annotation.Config;

import java.util.List;


@RunWith(SettingsRobolectricTestRunner.class)
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
public class PrintSettingsFragmentTest {

    @Mock
    private PrintSettingsFragment.PrintSummaryProvider.PrintManagerWrapper mPrintManager;
    @Mock
    private Activity mActivity;
    @Mock
    private Resources mRes;
    @Mock
    private SummaryLoader mSummaryLoader;
    private SummaryLoader.SummaryProvider mSummaryProvider;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        when(mActivity.getResources()).thenReturn(mRes);
        mSummaryProvider = new PrintSettingsFragment.PrintSummaryProvider(mActivity, mSummaryLoader,
                mPrintManager);
    }

    @Test
    public void testSummary_shouldSetSummaryToNumberOfPrintServices() {
        final List<PrintServiceInfo> printServices = mock(List.class);
        when(printServices.isEmpty()).thenReturn(false);
        when(printServices.size()).thenReturn(2);
        // 2 services
        when(mPrintManager.getPrintServices(PrintManager.ALL_SERVICES)).thenReturn(printServices);

        mSummaryProvider.setListening(true);

        verify(mRes).getQuantityString(R.plurals.print_settings_summary, 2, 2);

        // No service
        when(mPrintManager.getPrintServices(PrintManager.ALL_SERVICES)).thenReturn(null);

        mSummaryProvider.setListening(true);

        verify(mActivity).getString(R.string.print_settings_summary_no_service);
    }
}