Small corrections to the code that generates the print summary in the settings.

- Optimize when the listener is active and rely on it to be cleaned up
  automatically
- Only count print jobs that should be shown (i.e. not ones that are
  already finished.)

Change-Id: Idba995f1036e736f47ca7402d493210600eae91c
This commit is contained in:
Philip P. Moltmann
2016-01-07 10:09:12 -08:00
parent fae4a88530
commit 43335444ef

View File

@@ -590,18 +590,24 @@ public class PrintSettingsFragment extends SettingsPreferenceFragment
} }
return printJobInfos; return printJobInfos;
} }
}
private static boolean shouldShowToUser(PrintJobInfo printJob) { /**
switch (printJob.getState()) { * Should the print job the shown to the user in the settings app.
case PrintJobInfo.STATE_QUEUED: *
case PrintJobInfo.STATE_STARTED: * @param printJob The print job in question.
case PrintJobInfo.STATE_BLOCKED: * @return true iff the print job should be shown.
case PrintJobInfo.STATE_FAILED: { */
return true; private static boolean shouldShowToUser(PrintJobInfo printJob) {
} switch (printJob.getState()) {
case PrintJobInfo.STATE_QUEUED:
case PrintJobInfo.STATE_STARTED:
case PrintJobInfo.STATE_BLOCKED:
case PrintJobInfo.STATE_FAILED: {
return true;
} }
return false;
} }
return false;
} }
/** /**
@@ -624,29 +630,32 @@ public class PrintSettingsFragment extends SettingsPreferenceFragment
mSummaryLoader = summaryLoader; mSummaryLoader = summaryLoader;
mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE)) mPrintManager = ((PrintManager) context.getSystemService(Context.PRINT_SERVICE))
.getGlobalPrintManagerForUser(context.getUserId()); .getGlobalPrintManagerForUser(context.getUserId());
mPrintManager.addPrintJobStateChangeListener(this);
} }
@Override @Override
public void setListening(boolean isListening) { public void setListening(boolean isListening) {
mPrintManager.removePrintJobStateChangeListener(this);
if (isListening) { if (isListening) {
mPrintManager.addPrintJobStateChangeListener(this); mPrintManager.addPrintJobStateChangeListener(this);
onPrintJobStateChanged(null);
} else {
mPrintManager.removePrintJobStateChangeListener(this);
} }
onPrintJobStateChanged(null);
} }
@Override @Override
public void onPrintJobStateChanged(PrintJobId printJobId) { public void onPrintJobStateChanged(PrintJobId printJobId) {
int numPrintJobs = mPrintManager.getPrintJobs().size(); List<PrintJob> printJobs = mPrintManager.getPrintJobs();
mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString(
R.plurals.print_settings_title, numPrintJobs, numPrintJobs));
}
@Override int numActivePrintJobs = 0;
protected void finalize() { final int numPrintJobs = printJobs.size();
mPrintManager.removePrintJobStateChangeListener(this); for (int i = 0; i < numPrintJobs; i++) {
if (shouldShowToUser(printJobs.get(i).getInfo())) {
numActivePrintJobs++;
}
}
mSummaryLoader.setSummary(this, mContext.getResources().getQuantityString(
R.plurals.print_settings_title, numActivePrintJobs, numActivePrintJobs));
} }
} }