OSDN Git Service

Update print setting summary to show number of services
authorFan Zhang <zhfan@google.com>
Thu, 23 Mar 2017 00:43:20 +0000 (17:43 -0700)
committerFan Zhang <zhfan@google.com>
Thu, 23 Mar 2017 01:27:19 +0000 (18:27 -0700)
-Also some minor cleanup in print setting fragment (create pref using
correct context)

Change-Id: Ic05027ee53fd318da6ccd38c9ed98ce10ca49ba8
Fix: 36234108
Test: make RunSettingsRoboTests

res/values/strings.xml
src/com/android/settings/location/LocationSettings.java
src/com/android/settings/print/PrintSettingsFragment.java
tests/robotests/src/com/android/settings/print/PrintSettingsFragmentTest.java [new file with mode: 0644]

index d37d9d9..d097ad2 100644 (file)
     <!-- 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] -->
index 2185f50..31b7d34 100644 (file)
@@ -17,9 +17,7 @@
 package com.android.settings.location;
 
 import android.app.Activity;
-import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
-import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -27,7 +25,6 @@ import android.location.SettingInjectorService;
 import android.os.Bundle;
 import android.os.UserHandle;
 import android.os.UserManager;
-import android.provider.Settings;
 import android.support.v7.preference.Preference;
 import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceGroup;
@@ -37,6 +34,7 @@ import android.view.Menu;
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.Switch;
+
 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
 import com.android.settings.DimmableIconPreference;
 import com.android.settings.R;
index 07d6931..fad577c 100644 (file)
@@ -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,51 +505,56 @@ 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;
 
         /**
          * Create a new {@link PrintSummaryProvider}.
          *
-         * @param context The context this provider is for
+         * @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);
-                } else {
-                    mPrintManager.removePrintJobStateChangeListener(this);
+                    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 {
+                        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() {
diff --git a/tests/robotests/src/com/android/settings/print/PrintSettingsFragmentTest.java b/tests/robotests/src/com/android/settings/print/PrintSettingsFragmentTest.java
new file mode 100644 (file)
index 0000000..265472e
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * 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);
+    }
+}