OSDN Git Service

Update all printers activity's empty state and avoid selected printer flicker.
authorSvetoslav Ganov <svetoslavganov@google.com>
Sun, 6 Oct 2013 01:52:06 +0000 (18:52 -0700)
committerSvetoslav Ganov <svetoslavganov@google.com>
Sun, 6 Oct 2013 02:18:08 +0000 (19:18 -0700)
1. Updated the empty state of the all printers activity to show no printers message
   if the user is searching and there are no matches and a searching for printers
   message if the user is not filtering the printers list.

2. Adding the fake PDF printer after the historical printers are loaded to avoid the case
   where we select the PDF printer and immediately after that the most used printer is
   selected resulting in an undesirable UI flicker.

3. Fixed a bug where if the most used printer which is initially considered unavailable
   is reported by the print service as available but the UI is not properly updated
   leaving the user in no way to click on the print button.

bug: 10983508

Change-Id: I60fdb7761332850fd5b9ffc0cb572a6213024dba

packages/PrintSpooler/res/values/styles.xml
packages/PrintSpooler/src/com/android/printspooler/FusedPrintersProvider.java
packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
packages/PrintSpooler/src/com/android/printspooler/SelectPrinterFragment.java

index f6db6be..d64380a 100644 (file)
@@ -30,7 +30,6 @@
     </style>
 
     <style name="PrintOptionEditTextStyle">
-         <item name="android:selectAllOnFocus">true</item>
          <item name="android:minHeight">?android:attr/listPreferredItemHeightSmall</item>
          <item name="android:singleLine">true</item>
          <item name="android:ellipsize">end</item>
index 65af830..8aa290c 100644 (file)
@@ -63,7 +63,6 @@ public class FusedPrintersProvider extends Loader<List<PrinterInfo>> {
     private static final boolean DEBUG = false;
 
     private static final double WEIGHT_DECAY_COEFFICIENT = 0.95f;
-
     private static final int MAX_HISTORY_LENGTH = 50;
 
     private static final int MAX_FAVORITE_PRINTER_COUNT = 4;
@@ -388,25 +387,25 @@ public class FusedPrintersProvider extends Loader<List<PrinterInfo>> {
                             + FusedPrintersProvider.this.hashCode());
                 }
 
-                // Ignore printer records whose target services are not installed.
+                // Ignore printer records whose target services are not enabled.
                 PrintManager printManager = (PrintManager) getContext()
                         .getSystemService(Context.PRINT_SERVICE);
                 List<PrintServiceInfo> services = printManager
-                        .getInstalledPrintServices();
+                        .getEnabledPrintServices();
 
-                Set<ComponentName> installedComponents = new ArraySet<ComponentName>();
+                Set<ComponentName> enabledComponents = new ArraySet<ComponentName>();
                 final int installedServiceCount = services.size();
                 for (int i = 0; i < installedServiceCount; i++) {
                     ServiceInfo serviceInfo = services.get(i).getResolveInfo().serviceInfo;
                     ComponentName componentName = new ComponentName(
                             serviceInfo.packageName, serviceInfo.name);
-                    installedComponents.add(componentName);
+                    enabledComponents.add(componentName);
                 }
 
                 final int printerCount = printers.size();
                 for (int i = printerCount - 1; i >= 0; i--) {
                     ComponentName printerServiceName = printers.get(i).getId().getServiceName();
-                    if (!installedComponents.contains(printerServiceName.getPackageName())) {
+                    if (!enabledComponents.contains(printerServiceName)) {
                         printers.remove(i);
                     }
                 }
index f1678ff..f50cdfd 100644 (file)
@@ -1214,7 +1214,7 @@ public class PrintJobConfigActivity extends Activity {
 
                                 if (capabilitiesChanged || statusChanged) {
                                     // If something changed during update...
-                                    if (updateUi()) {
+                                    if (updateUi() || !mController.hasPerformedLayout()) {
                                         // Update the document.
                                         mController.update();
                                     }
@@ -1269,10 +1269,6 @@ public class PrintJobConfigActivity extends Activity {
 
             showUi(UI_EDITING_PRINT_JOB, null);
             bindUi();
-
-            mCurrentPrinter = mDestinationSpinnerAdapter.mFakePdfPrinter;
-            updatePrintAttributes(mCurrentPrinter.getCapabilities());
-
             updateUi();
         }
 
@@ -2001,11 +1997,10 @@ public class PrintJobConfigActivity extends Activity {
                 implements LoaderManager.LoaderCallbacks<List<PrinterInfo>>{
             private final List<PrinterInfo> mPrinters = new ArrayList<PrinterInfo>();
 
-            private final PrinterInfo mFakePdfPrinter;
+            private PrinterInfo mFakePdfPrinter;
 
             public DestinationAdapter() {
                 getLoaderManager().initLoader(LOADER_ID_PRINTERS_LOADER, null, this);
-                mFakePdfPrinter = createFakePdfPrinter();
             }
 
             public int getPrinterIndex(PrinterId printerId) {
@@ -2039,7 +2034,9 @@ public class PrintJobConfigActivity extends Activity {
 
             @Override
             public int getCount() {
-                return Math.min(mPrinters.size() + 2, DEST_ADAPTER_MAX_ITEM_COUNT);
+                final int additionalItemCount = (mFakePdfPrinter != null) ? 2 : 1;
+                return Math.min(mPrinters.size() + additionalItemCount,
+                        DEST_ADAPTER_MAX_ITEM_COUNT);
             }
 
             @Override
@@ -2055,14 +2052,14 @@ public class PrintJobConfigActivity extends Activity {
             @Override
             public Object getItem(int position) {
                 if (mPrinters.isEmpty()) {
-                    if (position == 0) {
+                    if (position == 0 && mFakePdfPrinter != null) {
                         return mFakePdfPrinter;
                     }
                 } else {
                     if (position < 1) {
                         return mPrinters.get(position);
                     }
-                    if (position == 1) {
+                    if (position == 1 && mFakePdfPrinter != null) {
                         return mFakePdfPrinter;
                     }
                     if (position < getCount() - 1) {
@@ -2075,14 +2072,14 @@ public class PrintJobConfigActivity extends Activity {
             @Override
             public long getItemId(int position) {
                 if (mPrinters.isEmpty()) {
-                    if (position == 0) {
+                    if (position == 0 && mFakePdfPrinter != null) {
                         return DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF;
                     }
                     if (position == 1) {
                         return DEST_ADAPTER_ITEM_ID_ALL_PRINTERS;
                     }
                 } else {
-                    if (position == 1) {
+                    if (position == 1 && mFakePdfPrinter != null) {
                         return DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF;
                     }
                     if (position == getCount() - 1) {
@@ -2114,14 +2111,14 @@ public class PrintJobConfigActivity extends Activity {
                 Drawable icon = null;
 
                 if (mPrinters.isEmpty()) {
-                    if (position == 0) {
+                    if (position == 0 && mFakePdfPrinter != null) {
                         PrinterInfo printer = (PrinterInfo) getItem(position);
                         title = printer.getName();
                     } else if (position == 1) {
                         title = getString(R.string.all_printers);
                     }
                 } else {
-                    if (position == 1) {
+                    if (position == 1 && mFakePdfPrinter != null) {
                         PrinterInfo printer = (PrinterInfo) getItem(position);
                         title = printer.getName();
                     } else if (position == getCount() - 1) {
@@ -2174,6 +2171,16 @@ public class PrintJobConfigActivity extends Activity {
             @Override
             public void onLoadFinished(Loader<List<PrinterInfo>> loader,
                     List<PrinterInfo> printers) {
+                // If this is the first load, create the fake PDF printer.
+                // We do this to avoid flicker where the PDF printer is the
+                // only one and as soon as the loader loads the favorites
+                // it gets switched. Not a great user experience.
+                if (mFakePdfPrinter == null) {
+                    mCurrentPrinter = mFakePdfPrinter = createFakePdfPrinter();
+                    updatePrintAttributes(mCurrentPrinter.getCapabilities());
+                    updateUi();
+                }
+
                 // We rearrange the printers if the user selects a printer
                 // not shown in the initial short list. Therefore, we have
                 // to keep the printer order.
index 4ce7d05..62673b2 100644 (file)
@@ -99,19 +99,19 @@ public final class SelectPrinterFragment extends ListFragment {
         adapter.registerDataSetObserver(new DataSetObserver() {
             @Override
             public void onChanged() {
-                if (adapter.getCount() <= 0) {
+                if (!getActivity().isFinishing() && adapter.getCount() <= 0) {
                     updateEmptyView(adapter);
                 }
             }
 
             @Override
             public void onInvalidated() {
-                updateEmptyView(adapter);
+                if (!getActivity().isFinishing()) {
+                    updateEmptyView(adapter);
+                }
             }
         });
         setListAdapter(adapter);
-        View emptyView = getActivity().findViewById(R.id.empty_print_state);
-        getListView().setEmptyView(emptyView);
     }
 
     @Override
@@ -230,6 +230,10 @@ public final class SelectPrinterFragment extends ListFragment {
     }
 
     public void updateEmptyView(DestinationAdapter adapter) {
+        if (getListView().getEmptyView() == null) {
+            View emptyView = getActivity().findViewById(R.id.empty_print_state);
+            getListView().setEmptyView(emptyView);
+        }
         TextView titleView = (TextView) getActivity().findViewById(R.id.title);
         View progressBar = getActivity().findViewById(R.id.progress_bar);
         if (adapter.getUnfilteredCount() <= 0) {