Merge "Add # of active print jobs back to print setting summary"
This commit is contained in:
committed by
Android (Google) Code Review
commit
f6b63db187
@@ -164,8 +164,7 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
/**
|
||||
* Adds preferences for all print services to the {@value PRINT_SERVICES_CATEGORY} cathegory.
|
||||
*/
|
||||
private final class PrintServicesController implements
|
||||
LoaderCallbacks<List<PrintServiceInfo>> {
|
||||
private final class PrintServicesController implements LoaderCallbacks<List<PrintServiceInfo>> {
|
||||
@Override
|
||||
public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) {
|
||||
PrintManager printManager =
|
||||
@@ -326,7 +325,7 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
|
||||
switch (printJob.getState()) {
|
||||
case PrintJobInfo.STATE_QUEUED:
|
||||
case PrintJobInfo.STATE_STARTED: {
|
||||
case PrintJobInfo.STATE_STARTED:
|
||||
if (!printJob.isCancelling()) {
|
||||
preference.setTitle(getString(
|
||||
R.string.print_printing_state_title_template,
|
||||
@@ -336,15 +335,13 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
R.string.print_cancelling_state_title_template,
|
||||
printJob.getLabel()));
|
||||
}
|
||||
} break;
|
||||
|
||||
case PrintJobInfo.STATE_FAILED: {
|
||||
break;
|
||||
case PrintJobInfo.STATE_FAILED:
|
||||
preference.setTitle(getString(
|
||||
R.string.print_failed_state_title_template,
|
||||
printJob.getLabel()));
|
||||
} break;
|
||||
|
||||
case PrintJobInfo.STATE_BLOCKED: {
|
||||
break;
|
||||
case PrintJobInfo.STATE_BLOCKED:
|
||||
if (!printJob.isCancelling()) {
|
||||
preference.setTitle(getString(
|
||||
R.string.print_blocked_state_title_template,
|
||||
@@ -354,7 +351,7 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
R.string.print_cancelling_state_title_template,
|
||||
printJob.getLabel()));
|
||||
}
|
||||
} break;
|
||||
break;
|
||||
}
|
||||
|
||||
preference.setSummary(getString(R.string.print_job_summary,
|
||||
@@ -364,14 +361,13 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
|
||||
switch (printJob.getState()) {
|
||||
case PrintJobInfo.STATE_QUEUED:
|
||||
case PrintJobInfo.STATE_STARTED: {
|
||||
case PrintJobInfo.STATE_STARTED:
|
||||
preference.setIcon(R.drawable.ic_print);
|
||||
} break;
|
||||
|
||||
break;
|
||||
case PrintJobInfo.STATE_FAILED:
|
||||
case PrintJobInfo.STATE_BLOCKED: {
|
||||
case PrintJobInfo.STATE_BLOCKED:
|
||||
preference.setIcon(R.drawable.ic_print_error);
|
||||
} break;
|
||||
break;
|
||||
}
|
||||
|
||||
Bundle extras = preference.getExtras();
|
||||
@@ -475,7 +471,7 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
PrintJobInfo printJob = printJobs.get(i).getInfo();
|
||||
if (shouldShowToUser(printJob)) {
|
||||
if (printJobInfos == null) {
|
||||
printJobInfos = new ArrayList<PrintJobInfo>();
|
||||
printJobInfos = new ArrayList<>();
|
||||
}
|
||||
printJobInfos.add(printJob);
|
||||
}
|
||||
@@ -505,8 +501,9 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
/**
|
||||
* Provider for the print settings summary
|
||||
*/
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
static class PrintSummaryProvider implements SummaryLoader.SummaryProvider {
|
||||
@VisibleForTesting
|
||||
static class PrintSummaryProvider
|
||||
implements SummaryLoader.SummaryProvider, PrintJobStateChangeListener {
|
||||
private final Context mContext;
|
||||
private final PrintManagerWrapper mPrintManager;
|
||||
private final SummaryLoader mSummaryLoader;
|
||||
@@ -528,6 +525,31 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
public void setListening(boolean isListening) {
|
||||
if (mPrintManager != null) {
|
||||
if (isListening) {
|
||||
mPrintManager.addPrintJobStateChanegListner(this);
|
||||
onPrintJobStateChanged(null);
|
||||
} else {
|
||||
mPrintManager.removePrintJobStateChangeListener(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPrintJobStateChanged(PrintJobId printJobId) {
|
||||
final List<PrintJob> printJobs = mPrintManager.getPrintJobs();
|
||||
|
||||
int numActivePrintJobs = 0;
|
||||
if (printJobs != null) {
|
||||
for (PrintJob job : printJobs) {
|
||||
if (shouldShowToUser(job.getInfo())) {
|
||||
numActivePrintJobs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (numActivePrintJobs > 0) {
|
||||
mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString(
|
||||
R.plurals.print_jobs_summary, numActivePrintJobs, numActivePrintJobs));
|
||||
} else {
|
||||
List<PrintServiceInfo> services =
|
||||
mPrintManager.getPrintServices(PrintManager.ENABLED_SERVICES);
|
||||
if (services == null || services.isEmpty()) {
|
||||
@@ -541,7 +563,6 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static class PrintManagerWrapper {
|
||||
|
||||
@@ -555,6 +576,18 @@ public class PrintSettingsFragment extends ProfileSettingsPreferenceFragment
|
||||
public List<PrintServiceInfo> getPrintServices(int selectionFlags) {
|
||||
return mPrintManager.getPrintServices(selectionFlags);
|
||||
}
|
||||
|
||||
public void addPrintJobStateChanegListner(PrintJobStateChangeListener listener) {
|
||||
mPrintManager.addPrintJobStateChangeListener(listener);
|
||||
}
|
||||
|
||||
public void removePrintJobStateChangeListener(PrintJobStateChangeListener listener) {
|
||||
mPrintManager.removePrintJobStateChangeListener(listener);
|
||||
}
|
||||
|
||||
public List<PrintJob> getPrintJobs() {
|
||||
return mPrintManager.getPrintJobs();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -22,6 +22,8 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.res.Resources;
|
||||
import android.print.PrintJob;
|
||||
import android.print.PrintJobInfo;
|
||||
import android.print.PrintManager;
|
||||
import android.printservice.PrintServiceInfo;
|
||||
|
||||
@@ -34,9 +36,11 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -62,6 +66,19 @@ public class PrintSettingsFragmentTest {
|
||||
mPrintManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSummary_hasActiveJob_shouldSetSummaryToNumberOfJobs() {
|
||||
final List<PrintJob> printJobs = new ArrayList<>();
|
||||
final PrintJob job = mock(PrintJob.class, Mockito.RETURNS_DEEP_STUBS);
|
||||
printJobs.add(job);
|
||||
when(job.getInfo().getState()).thenReturn(PrintJobInfo.STATE_STARTED);
|
||||
when(mPrintManager.getPrintJobs()).thenReturn(printJobs);
|
||||
|
||||
mSummaryProvider.setListening(true);
|
||||
|
||||
verify(mRes).getQuantityString(R.plurals.print_jobs_summary, 1, 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSummary_shouldSetSummaryToNumberOfPrintServices() {
|
||||
final List<PrintServiceInfo> printServices = mock(List.class);
|
||||
|
Reference in New Issue
Block a user