diff options
| author | 2016-03-30 22:42:48 +0000 | |
|---|---|---|
| committer | 2016-03-30 22:42:49 +0000 | |
| commit | 02add6aa91a5468458d6aa47b05701579c6da7d8 (patch) | |
| tree | e2229dbea816039b58f22cdce4692d8e039153b3 | |
| parent | ea113376ccd4b177f72e7fc1ece5ef65964c0d35 (diff) | |
| parent | 63ce0b737c022281ce4c36e1d90ed9b0270512f0 (diff) | |
Merge "Rewrite workflow on how the destination printer is selected." into nyc-dev
| -rw-r--r-- | packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java | 95 | ||||
| -rw-r--r-- | packages/PrintSpooler/src/com/android/printspooler/ui/SelectPrinterActivity.java | 15 |
2 files changed, 72 insertions, 38 deletions
diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java index 66c1dbe84754..dba6a8bfbe22 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/PrintActivity.java @@ -128,8 +128,6 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat private static final boolean DEBUG = false; - public static final String INTENT_EXTRA_PRINTER_ID = "INTENT_EXTRA_PRINTER_ID"; - private static final String FRAGMENT_TAG = "FRAGMENT_TAG"; private static final String HAS_PRINTED_PREF = "has_printed"; @@ -707,21 +705,17 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat private void onSelectPrinterActivityResult(int resultCode, Intent data) { if (resultCode == RESULT_OK && data != null) { - PrinterId printerId = data.getParcelableExtra(INTENT_EXTRA_PRINTER_ID); - if (printerId != null) { - mDestinationSpinnerAdapter.ensurePrinterInVisibleAdapterPosition(printerId); - final int index = mDestinationSpinnerAdapter.getPrinterIndex(printerId); - if (index != AdapterView.INVALID_POSITION) { - mDestinationSpinner.setSelection(index); - return; - } + PrinterInfo printerInfo = data.getParcelableExtra( + SelectPrinterActivity.INTENT_EXTRA_PRINTER); + if (printerInfo != null) { + mCurrentPrinter = printerInfo; + mDestinationSpinnerAdapter.ensurePrinterInVisibleAdapterPosition(printerInfo); } } if (mCurrentPrinter != null) { - PrinterId printerId = mCurrentPrinter.getId(); - final int index = mDestinationSpinnerAdapter.getPrinterIndex(printerId); - mDestinationSpinner.setSelection(index); + // Trigger PrintersObserver.onChanged() to adjust selection back to current printer + mDestinationSpinnerAdapter.notifyDataSetChanged(); } } @@ -2246,23 +2240,36 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat return AdapterView.INVALID_POSITION; } - public void ensurePrinterInVisibleAdapterPosition(PrinterId printerId) { + public void ensurePrinterInVisibleAdapterPosition(PrinterInfo printer) { final int printerCount = mPrinterHolders.size(); + boolean isKnownPrinter = false; for (int i = 0; i < printerCount; i++) { PrinterHolder printerHolder = mPrinterHolders.get(i); - if (printerHolder.printer.getId().equals(printerId)) { + + if (printerHolder.printer.getId().equals(printer.getId())) { + isKnownPrinter = true; + // If already in the list - do nothing. if (i < getCount() - 2) { - return; + break; } // Else replace the last one (two items are not printers). final int lastPrinterIndex = getCount() - 3; mPrinterHolders.set(i, mPrinterHolders.get(lastPrinterIndex)); mPrinterHolders.set(lastPrinterIndex, printerHolder); - notifyDataSetChanged(); - return; + break; } } + + if (!isKnownPrinter) { + PrinterHolder printerHolder = new PrinterHolder(printer); + printerHolder.removed = true; + + mPrinterHolders.add(Math.max(0, getCount() - 3), printerHolder); + } + + // Force reload to adjust selection in PrintersObserver.onChanged() + notifyDataSetChanged(); } @Override @@ -2445,8 +2452,7 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat List<PrinterHolder> newPrinterHolders = new ArrayList<>(); // Update printers we already have which are either updated or removed. - // We do not remove printers if the currently selected printer is removed - // to prevent the user printing to a wrong printer. + // We do not remove the currently selected printer. final int oldPrinterCount = mPrinterHolders.size(); for (int i = 0; i < oldPrinterCount; i++) { PrinterHolder printerHolder = mPrinterHolders.get(i); @@ -2455,10 +2461,11 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat if (updatedPrinter != null) { printerHolder.printer = updatedPrinter; printerHolder.removed = false; - } else { + newPrinterHolders.add(printerHolder); + } else if (mCurrentPrinter != null && mCurrentPrinter.getId().equals(oldPrinterId)){ printerHolder.removed = true; + newPrinterHolders.add(printerHolder); } - newPrinterHolders.add(printerHolder); } // Add the rest of the new printers, i.e. what is left. @@ -2490,14 +2497,25 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat return null; } - public void pruneRemovedPrinters() { + /** + * Remove a printer from the holders if it is marked as removed. + * + * @param printerId the id of the printer to remove. + * + * @return true iff the printer was removed. + */ + public boolean pruneRemovedPrinter(PrinterId printerId) { final int holderCounts = mPrinterHolders.size(); for (int i = holderCounts - 1; i >= 0; i--) { PrinterHolder printerHolder = mPrinterHolders.get(i); - if (printerHolder.removed) { + + if (printerHolder.printer.getId().equals(printerId) && printerHolder.removed) { mPrinterHolders.remove(i); + return true; } } + + return false; } private void addPrinters(List<PrinterHolder> list, Collection<PrinterInfo> printers) { @@ -2542,17 +2560,17 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat PrinterHolder printerHolder = mDestinationSpinnerAdapter.getPrinterHolder( oldPrinterState.getId()); - if (printerHolder == null) { - return; - } PrinterInfo newPrinterState = printerHolder.printer; - if (!printerHolder.removed) { - mDestinationSpinnerAdapter.pruneRemovedPrinters(); - } else { + if (printerHolder.removed) { onPrinterUnavailable(newPrinterState); } + if (mDestinationSpinner.getSelectedItem() != printerHolder) { + mDestinationSpinner.setSelection( + mDestinationSpinnerAdapter.getPrinterIndex(newPrinterState.getId())); + } + if (oldPrinterState.equals(newPrinterState)) { return; } @@ -2644,13 +2662,28 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat return; } + PrinterId oldId = null; + if (mCurrentPrinter != null) { + oldId = mCurrentPrinter.getId(); + } + mCurrentPrinter = currentPrinter; + if (oldId != null) { + boolean printerRemoved = mDestinationSpinnerAdapter.pruneRemovedPrinter(oldId); + + if (printerRemoved) { + // Trigger PrinterObserver.onChanged to adjust selection. This will call + // this function again. + mDestinationSpinnerAdapter.notifyDataSetChanged(); + return; + } + } + PrinterHolder printerHolder = mDestinationSpinnerAdapter.getPrinterHolder( currentPrinter.getId()); if (!printerHolder.removed) { setState(STATE_CONFIGURING); - mDestinationSpinnerAdapter.pruneRemovedPrinters(); ensurePreviewUiShown(); } diff --git a/packages/PrintSpooler/src/com/android/printspooler/ui/SelectPrinterActivity.java b/packages/PrintSpooler/src/com/android/printspooler/ui/SelectPrinterActivity.java index e53a522adb12..cee16c8b29e9 100644 --- a/packages/PrintSpooler/src/com/android/printspooler/ui/SelectPrinterActivity.java +++ b/packages/PrintSpooler/src/com/android/printspooler/ui/SelectPrinterActivity.java @@ -73,8 +73,9 @@ public final class SelectPrinterActivity extends Activity implements private static final int LOADER_ID_PRINT_REGISTRY_INT = 2; private static final int LOADER_ID_ENABLED_PRINT_SERVICES = 3; - public static final String INTENT_EXTRA_PRINTER_ID = "INTENT_EXTRA_PRINTER_ID"; + public static final String INTENT_EXTRA_PRINTER = "INTENT_EXTRA_PRINTER"; + private static final String EXTRA_PRINTER = "EXTRA_PRINTER"; private static final String EXTRA_PRINTER_ID = "EXTRA_PRINTER_ID"; private static final String KEY_NOT_FIRST_CREATE = "KEY_NOT_FIRST_CREATE"; @@ -134,7 +135,7 @@ public final class SelectPrinterActivity extends Activity implements if (printer == null) { startAddPrinterActivity(); } else { - onPrinterSelected(printer.getId()); + onPrinterSelected(printer); } } }); @@ -244,7 +245,7 @@ public final class SelectPrinterActivity extends Activity implements MenuItem selectItem = menu.add(Menu.NONE, R.string.print_select_printer, Menu.NONE, R.string.print_select_printer); Intent intent = new Intent(); - intent.putExtra(EXTRA_PRINTER_ID, printer.getId()); + intent.putExtra(EXTRA_PRINTER, printer); selectItem.setIntent(intent); } @@ -263,8 +264,8 @@ public final class SelectPrinterActivity extends Activity implements public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.string.print_select_printer: { - PrinterId printerId = item.getIntent().getParcelableExtra(EXTRA_PRINTER_ID); - onPrinterSelected(printerId); + PrinterInfo printer = item.getIntent().getParcelableExtra(EXTRA_PRINTER); + onPrinterSelected(printer); } return true; case R.string.print_forget_printer: { @@ -302,9 +303,9 @@ public final class SelectPrinterActivity extends Activity implements super.onStop(); } - private void onPrinterSelected(PrinterId printerId) { + private void onPrinterSelected(PrinterInfo printer) { Intent intent = new Intent(); - intent.putExtra(INTENT_EXTRA_PRINTER_ID, printerId); + intent.putExtra(INTENT_EXTRA_PRINTER, printer); setResult(RESULT_OK, intent); finish(); } |