Update print setting summary to show number of services

-Also some minor cleanup in print setting fragment (create pref using
correct context)

Change-Id: Ic05027ee53fd318da6ccd38c9ed98ce10ca49ba8
Fix: 36234108
Test: make RunSettingsRoboTests
This commit is contained in:
Fan Zhang
2017-03-22 17:43:20 -07:00
parent 679ed4c30c
commit 01ece62faa
4 changed files with 137 additions and 50 deletions

View File

@@ -4240,10 +4240,13 @@
<!-- Title in main settings screen for printing settings [CHAR LIMIT=15] --> <!-- Title in main settings screen for printing settings [CHAR LIMIT=15] -->
<string name="print_settings">Printing</string> <string name="print_settings">Printing</string>
<!-- Print frameworks summary in settings screen [CHAR LIMIT=50] --> <!-- Print setting summary in settings screen [CHAR LIMIT=50] -->
<plurals name="print_settings_title"> <string name="print_settings_summary_no_service">No print service</string>
<item quantity="one">1 print job</item>
<item quantity="other">%d print jobs</item> <!-- 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> </plurals>
<!-- Title for print service settings screen [CHAR LIMIT=25] --> <!-- Title for print service settings screen [CHAR LIMIT=25] -->

View File

@@ -17,9 +17,7 @@
package com.android.settings.location; package com.android.settings.location;
import android.app.Activity; import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
@@ -27,7 +25,6 @@ import android.location.SettingInjectorService;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceGroup; import android.support.v7.preference.PreferenceGroup;
@@ -37,6 +34,7 @@ import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Switch; import android.widget.Switch;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.DimmableIconPreference; import com.android.settings.DimmableIconPreference;
import com.android.settings.R; import com.android.settings.R;

View File

@@ -16,7 +16,6 @@
package com.android.settings.print; package com.android.settings.print;
import android.app.Activity;
import android.app.LoaderManager.LoaderCallbacks; import android.app.LoaderManager.LoaderCallbacks;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.AsyncTaskLoader; import android.content.AsyncTaskLoader;
@@ -37,9 +36,9 @@ import android.print.PrintServicesLoader;
import android.printservice.PrintServiceInfo; import android.printservice.PrintServiceInfo;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.provider.Settings; import android.provider.Settings;
import android.support.annotation.VisibleForTesting;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory; import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceScreen;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
@@ -191,12 +190,14 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
mPrintServicesCategory.removeAll(); mPrintServicesCategory.removeAll();
PackageManager pm = getActivity().getPackageManager(); 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 (PrintServiceInfo service : services) {
for (int i = 0; i < numServices; i++) { Preference preference = new Preference(context);
PrintServiceInfo service = services.get(i);
PreferenceScreen preference = getPreferenceManager().createPreferenceScreen(
getActivity());
String title = service.getResolveInfo().loadLabel(pm).toString(); String title = service.getResolveInfo().loadLabel(pm).toString();
preference.setTitle(title); preference.setTitle(title);
@@ -310,13 +311,14 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
} }
mActivePrintJobsCategory.removeAll(); 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 (PrintJobInfo printJob : printJobs) {
for (int i = 0; i < printJobCount; i++) { Preference preference = new Preference(context);
PrintJobInfo printJob = printJobs.get(i);
PreferenceScreen preference = getPreferenceManager()
.createPreferenceScreen(getActivity());
preference.setPersistent(false); preference.setPersistent(false);
preference.setFragment(PrintJobSettingsFragment.class.getName()); preference.setFragment(PrintJobSettingsFragment.class.getName());
@@ -503,10 +505,10 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
/** /**
* Provider for the print settings summary * Provider for the print settings summary
*/ */
private static class PrintSummaryProvider @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
implements SummaryLoader.SummaryProvider, PrintJobStateChangeListener { static class PrintSummaryProvider implements SummaryLoader.SummaryProvider {
private final Context mContext; private final Context mContext;
private final PrintManager mPrintManager; private final PrintManagerWrapper mPrintManager;
private final SummaryLoader mSummaryLoader; private final SummaryLoader mSummaryLoader;
/** /**
@@ -515,39 +517,44 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
* @param context The context this provider is for * @param context The context this provider is for
* @param summaryLoader The summary load using this provider * @param summaryLoader The summary load using this provider
*/ */
public PrintSummaryProvider(Context context, SummaryLoader summaryLoader) { PrintSummaryProvider(Context context, SummaryLoader summaryLoader,
PrintManagerWrapper printManager) {
mContext = context; mContext = context;
mSummaryLoader = summaryLoader; mSummaryLoader = summaryLoader;
mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE)) mPrintManager = printManager;
.getGlobalPrintManagerForUser(context.getUserId());
} }
@Override @Override
public void setListening(boolean isListening) { public void setListening(boolean isListening) {
if (mPrintManager != null) { if (mPrintManager != null) {
if (isListening) { if (isListening) {
mPrintManager.addPrintJobStateChangeListener(this); List<PrintServiceInfo> services =
onPrintJobStateChanged(null); mPrintManager.getPrintServices(PrintManager.ALL_SERVICES);
if (services == null || services.isEmpty()) {
mSummaryLoader.setSummary(this,
mContext.getString(R.string.print_settings_summary_no_service));
} else { } else {
mPrintManager.removePrintJobStateChangeListener(this); final int count = services.size();
mSummaryLoader.setSummary(this,
mContext.getResources().getQuantityString(
R.plurals.print_settings_summary, count, count));
}
} }
} }
} }
@Override static class PrintManagerWrapper {
public void onPrintJobStateChanged(PrintJobId printJobId) {
List<PrintJob> printJobs = mPrintManager.getPrintJobs();
int numActivePrintJobs = 0; private final PrintManager mPrintManager;
final int numPrintJobs = printJobs.size();
for (int i = 0; i < numPrintJobs; i++) { PrintManagerWrapper(Context context) {
if (shouldShowToUser(printJobs.get(i).getInfo())) { mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
numActivePrintJobs++; .getGlobalPrintManagerForUser(context.getUserId());
}
} }
mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString( public List<PrintServiceInfo> getPrintServices(int selectionFlags) {
R.plurals.print_settings_title, numActivePrintJobs, numActivePrintJobs)); 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 * A factory for {@link PrintSummaryProvider providers} the settings app can use to read the
* print summary. * print summary.
*/ */
public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY =
= new SummaryLoader.SummaryProviderFactory() { (activity, summaryLoader) -> new PrintSummaryProvider(activity, summaryLoader,
@Override new PrintSummaryProvider.PrintManagerWrapper(activity));
public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
SummaryLoader summaryLoader) {
return new PrintSummaryProvider(activity, summaryLoader);
}
};
public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() { new BaseSearchIndexProvider() {

View File

@@ -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);
}
}