Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.printspooler.ui; |
| 18 | |
| 19 | import android.app.Activity; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.app.Dialog; |
| 22 | import android.app.DialogFragment; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 23 | import android.app.Fragment; |
| 24 | import android.app.FragmentTransaction; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 25 | import android.app.LoaderManager; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 26 | import android.content.ActivityNotFoundException; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 29 | import android.content.DialogInterface; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 30 | import android.content.Intent; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 31 | import android.content.Loader; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 32 | import android.content.ServiceConnection; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 33 | import android.content.SharedPreferences; |
| 34 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 35 | import android.content.pm.PackageManager; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 36 | import android.content.pm.PackageManager.NameNotFoundException; |
| 37 | import android.content.pm.ResolveInfo; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 38 | import android.content.res.Configuration; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 39 | import android.database.DataSetObserver; |
| 40 | import android.graphics.drawable.Drawable; |
| 41 | import android.net.Uri; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 42 | import android.os.AsyncTask; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 43 | import android.os.Bundle; |
| 44 | import android.os.Handler; |
| 45 | import android.os.IBinder; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 46 | import android.os.ParcelFileDescriptor; |
| 47 | import android.os.RemoteException; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 48 | import android.print.IPrintDocumentAdapter; |
| 49 | import android.print.PageRange; |
| 50 | import android.print.PrintAttributes; |
| 51 | import android.print.PrintAttributes.MediaSize; |
| 52 | import android.print.PrintAttributes.Resolution; |
| 53 | import android.print.PrintDocumentInfo; |
| 54 | import android.print.PrintJobInfo; |
| 55 | import android.print.PrintManager; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 56 | import android.print.PrintServicesLoader; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 57 | import android.print.PrinterCapabilitiesInfo; |
| 58 | import android.print.PrinterId; |
| 59 | import android.print.PrinterInfo; |
| 60 | import android.printservice.PrintService; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 61 | import android.printservice.PrintServiceInfo; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 62 | import android.provider.DocumentsContract; |
| 63 | import android.text.Editable; |
| 64 | import android.text.TextUtils; |
| 65 | import android.text.TextUtils.SimpleStringSplitter; |
| 66 | import android.text.TextWatcher; |
| 67 | import android.util.ArrayMap; |
Philip P. Moltmann | 4959caf | 2016-01-21 14:30:56 -0800 | [diff] [blame] | 68 | import android.util.ArraySet; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 69 | import android.util.Log; |
Philip P. Moltmann | 443075a | 2016-01-26 13:04:21 -0800 | [diff] [blame] | 70 | import android.util.TypedValue; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 71 | import android.view.KeyEvent; |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 72 | import android.view.MotionEvent; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 73 | import android.view.View; |
| 74 | import android.view.View.OnClickListener; |
| 75 | import android.view.View.OnFocusChangeListener; |
| 76 | import android.view.ViewGroup; |
| 77 | import android.view.inputmethod.InputMethodManager; |
| 78 | import android.widget.AdapterView; |
| 79 | import android.widget.AdapterView.OnItemSelectedListener; |
| 80 | import android.widget.ArrayAdapter; |
| 81 | import android.widget.BaseAdapter; |
| 82 | import android.widget.Button; |
| 83 | import android.widget.EditText; |
| 84 | import android.widget.ImageView; |
| 85 | import android.widget.Spinner; |
| 86 | import android.widget.TextView; |
| 87 | |
Chris Wren | dcc34fd | 2015-07-30 14:27:02 -0400 | [diff] [blame] | 88 | import com.android.internal.logging.MetricsLogger; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 89 | import com.android.printspooler.R; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 90 | import com.android.printspooler.model.MutexFileProvider; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 91 | import com.android.printspooler.model.PrintSpoolerProvider; |
| 92 | import com.android.printspooler.model.PrintSpoolerService; |
| 93 | import com.android.printspooler.model.RemotePrintDocument; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 94 | import com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 95 | import com.android.printspooler.renderer.IPdfEditor; |
| 96 | import com.android.printspooler.renderer.PdfManipulationService; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 97 | import com.android.printspooler.util.ApprovedPrintServices; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 98 | import com.android.printspooler.util.MediaSizeUtils; |
| 99 | import com.android.printspooler.util.MediaSizeUtils.MediaSizeComparator; |
| 100 | import com.android.printspooler.util.PageRangeUtils; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 101 | import com.android.printspooler.widget.PrintContentView; |
| 102 | import com.android.printspooler.widget.PrintContentView.OptionsStateChangeListener; |
| 103 | import com.android.printspooler.widget.PrintContentView.OptionsStateController; |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 104 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 105 | import libcore.io.IoUtils; |
| 106 | import libcore.io.Streams; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 107 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 108 | import java.io.File; |
| 109 | import java.io.FileInputStream; |
| 110 | import java.io.FileOutputStream; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 111 | import java.io.IOException; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 112 | import java.io.InputStream; |
| 113 | import java.io.OutputStream; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 114 | import java.util.ArrayList; |
| 115 | import java.util.Arrays; |
| 116 | import java.util.Collection; |
| 117 | import java.util.Collections; |
| 118 | import java.util.List; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 119 | import java.util.Objects; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 120 | import java.util.regex.Matcher; |
| 121 | import java.util.regex.Pattern; |
| 122 | |
| 123 | public class PrintActivity extends Activity implements RemotePrintDocument.UpdateResultCallbacks, |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 124 | PrintErrorFragment.OnActionListener, PageAdapter.ContentCallbacks, |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 125 | OptionsStateChangeListener, OptionsStateController, |
| 126 | LoaderManager.LoaderCallbacks<List<PrintServiceInfo>> { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 127 | private static final String LOG_TAG = "PrintActivity"; |
| 128 | |
Svetoslav | f8ffa56 | 2014-07-23 18:22:03 -0700 | [diff] [blame] | 129 | private static final boolean DEBUG = false; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 130 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 131 | private static final String FRAGMENT_TAG = "FRAGMENT_TAG"; |
| 132 | |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 133 | private static final String HAS_PRINTED_PREF = "has_printed"; |
| 134 | |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 135 | private static final int LOADER_ID_ENABLED_PRINT_SERVICES = 1; |
| 136 | private static final int LOADER_ID_PRINT_REGISTRY = 2; |
| 137 | private static final int LOADER_ID_PRINT_REGISTRY_INT = 3; |
| 138 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 139 | private static final int ORIENTATION_PORTRAIT = 0; |
| 140 | private static final int ORIENTATION_LANDSCAPE = 1; |
| 141 | |
| 142 | private static final int ACTIVITY_REQUEST_CREATE_FILE = 1; |
| 143 | private static final int ACTIVITY_REQUEST_SELECT_PRINTER = 2; |
| 144 | private static final int ACTIVITY_REQUEST_POPULATE_ADVANCED_PRINT_OPTIONS = 3; |
| 145 | |
| 146 | private static final int DEST_ADAPTER_MAX_ITEM_COUNT = 9; |
| 147 | |
| 148 | private static final int DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF = Integer.MAX_VALUE; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 149 | private static final int DEST_ADAPTER_ITEM_ID_MORE = Integer.MAX_VALUE - 1; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 150 | |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 151 | private static final int STATE_INITIALIZING = 0; |
| 152 | private static final int STATE_CONFIGURING = 1; |
| 153 | private static final int STATE_PRINT_CONFIRMED = 2; |
| 154 | private static final int STATE_PRINT_CANCELED = 3; |
| 155 | private static final int STATE_UPDATE_FAILED = 4; |
| 156 | private static final int STATE_CREATE_FILE_FAILED = 5; |
| 157 | private static final int STATE_PRINTER_UNAVAILABLE = 6; |
| 158 | private static final int STATE_UPDATE_SLOW = 7; |
| 159 | private static final int STATE_PRINT_COMPLETED = 8; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 160 | |
| 161 | private static final int UI_STATE_PREVIEW = 0; |
| 162 | private static final int UI_STATE_ERROR = 1; |
| 163 | private static final int UI_STATE_PROGRESS = 2; |
| 164 | |
| 165 | private static final int MIN_COPIES = 1; |
| 166 | private static final String MIN_COPIES_STRING = String.valueOf(MIN_COPIES); |
| 167 | |
| 168 | private static final Pattern PATTERN_DIGITS = Pattern.compile("[\\d]+"); |
| 169 | |
| 170 | private static final Pattern PATTERN_ESCAPE_SPECIAL_CHARS = Pattern.compile( |
| 171 | "(?=[]\\[+&|!(){}^\"~*?:\\\\])"); |
| 172 | |
| 173 | private static final Pattern PATTERN_PAGE_RANGE = Pattern.compile( |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 174 | "[\\s]*[0-9]+[\\-]?[\\s]*[0-9]*[\\s]*?(([,])" |
| 175 | + "[\\s]*[0-9]+[\\s]*[\\-]?[\\s]*[0-9]*[\\s]*|[\\s]*)+"); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 176 | |
Philip P. Moltmann | c2f913d | 2016-02-01 12:03:48 -0800 | [diff] [blame] | 177 | private boolean mIsOptionsUiBound = false; |
| 178 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 179 | private final PrinterAvailabilityDetector mPrinterAvailabilityDetector = |
| 180 | new PrinterAvailabilityDetector(); |
| 181 | |
| 182 | private final SimpleStringSplitter mStringCommaSplitter = new SimpleStringSplitter(','); |
| 183 | |
| 184 | private final OnFocusChangeListener mSelectAllOnFocusListener = new SelectAllOnFocusListener(); |
| 185 | |
| 186 | private PrintSpoolerProvider mSpoolerProvider; |
| 187 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 188 | private PrintPreviewController mPrintPreviewController; |
| 189 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 190 | private PrintJobInfo mPrintJob; |
| 191 | private RemotePrintDocument mPrintedDocument; |
| 192 | private PrinterRegistry mPrinterRegistry; |
| 193 | |
| 194 | private EditText mCopiesEditText; |
| 195 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 196 | private TextView mPageRangeTitle; |
| 197 | private EditText mPageRangeEditText; |
| 198 | |
| 199 | private Spinner mDestinationSpinner; |
| 200 | private DestinationAdapter mDestinationSpinnerAdapter; |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 201 | private boolean mShowDestinationPrompt; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 202 | |
| 203 | private Spinner mMediaSizeSpinner; |
| 204 | private ArrayAdapter<SpinnerItem<MediaSize>> mMediaSizeSpinnerAdapter; |
| 205 | |
| 206 | private Spinner mColorModeSpinner; |
| 207 | private ArrayAdapter<SpinnerItem<Integer>> mColorModeSpinnerAdapter; |
| 208 | |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 209 | private Spinner mDuplexModeSpinner; |
| 210 | private ArrayAdapter<SpinnerItem<Integer>> mDuplexModeSpinnerAdapter; |
| 211 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 212 | private Spinner mOrientationSpinner; |
| 213 | private ArrayAdapter<SpinnerItem<Integer>> mOrientationSpinnerAdapter; |
| 214 | |
| 215 | private Spinner mRangeOptionsSpinner; |
| 216 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 217 | private PrintContentView mOptionsContent; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 218 | |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 219 | private View mSummaryContainer; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 220 | private TextView mSummaryCopies; |
| 221 | private TextView mSummaryPaperSize; |
| 222 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 223 | private Button mMoreOptionsButton; |
| 224 | |
| 225 | private ImageView mPrintButton; |
| 226 | |
| 227 | private ProgressMessageController mProgressMessageController; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 228 | private MutexFileProvider mFileProvider; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 229 | |
| 230 | private MediaSizeComparator mMediaSizeComparator; |
| 231 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 232 | private PrinterInfo mCurrentPrinter; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 233 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 234 | private PageRange[] mSelectedPages; |
| 235 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 236 | private String mCallingPackageName; |
| 237 | |
Svetoslav | 73764e3 | 2014-07-15 15:56:46 -0700 | [diff] [blame] | 238 | private int mCurrentPageCount; |
| 239 | |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 240 | private int mState = STATE_INITIALIZING; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 241 | |
| 242 | private int mUiState = UI_STATE_PREVIEW; |
| 243 | |
Philip P. Moltmann | 1bb7f36 | 2016-02-26 14:21:20 -0800 | [diff] [blame] | 244 | /** Observer for changes to the printers */ |
| 245 | private PrintersObserver mPrintersObserver; |
| 246 | |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 247 | /** Advances options activity name for current printer */ |
| 248 | private ComponentName mAdvancedPrintOptionsActivity; |
| 249 | |
| 250 | /** Whether at least one print services is enabled or not */ |
| 251 | private boolean mArePrintServicesEnabled; |
| 252 | |
Philip P. Moltmann | b170c08 | 2016-03-21 12:48:58 -0700 | [diff] [blame] | 253 | /** Is doFinish() already in progress */ |
| 254 | private boolean mIsFinishing; |
| 255 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 256 | @Override |
| 257 | public void onCreate(Bundle savedInstanceState) { |
| 258 | super.onCreate(savedInstanceState); |
| 259 | |
| 260 | Bundle extras = getIntent().getExtras(); |
| 261 | |
| 262 | mPrintJob = extras.getParcelable(PrintManager.EXTRA_PRINT_JOB); |
| 263 | if (mPrintJob == null) { |
| 264 | throw new IllegalArgumentException(PrintManager.EXTRA_PRINT_JOB |
| 265 | + " cannot be null"); |
| 266 | } |
Philip P. Moltmann | b4efdb4 | 2015-11-10 14:58:44 -0800 | [diff] [blame] | 267 | if (mPrintJob.getAttributes() == null) { |
| 268 | mPrintJob.setAttributes(new PrintAttributes.Builder().build()); |
| 269 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 270 | |
| 271 | final IBinder adapter = extras.getBinder(PrintManager.EXTRA_PRINT_DOCUMENT_ADAPTER); |
| 272 | if (adapter == null) { |
| 273 | throw new IllegalArgumentException(PrintManager.EXTRA_PRINT_DOCUMENT_ADAPTER |
| 274 | + " cannot be null"); |
| 275 | } |
| 276 | |
| 277 | mCallingPackageName = extras.getString(DocumentsContract.EXTRA_PACKAGE_NAME); |
| 278 | |
| 279 | // This will take just a few milliseconds, so just wait to |
| 280 | // bind to the local service before showing the UI. |
| 281 | mSpoolerProvider = new PrintSpoolerProvider(this, |
| 282 | new Runnable() { |
| 283 | @Override |
| 284 | public void run() { |
Philip P. Moltmann | cf5b777 | 2016-02-01 16:58:57 -0800 | [diff] [blame] | 285 | if (isFinishing()) { |
| 286 | // onPause might have not been able to cancel the job, see PrintActivity#onPause |
| 287 | // To be sure, cancel the job again. Double canceling does no harm. |
| 288 | mSpoolerProvider.getSpooler().setPrintJobState(mPrintJob.getId(), |
| 289 | PrintJobInfo.STATE_CANCELED, null); |
| 290 | } else { |
| 291 | onConnectedToPrintSpooler(adapter); |
| 292 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 293 | } |
| 294 | }); |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 295 | |
| 296 | getLoaderManager().initLoader(LOADER_ID_ENABLED_PRINT_SERVICES, null, this); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 299 | private void onConnectedToPrintSpooler(final IBinder documentAdapter) { |
| 300 | // Now that we are bound to the print spooler service, |
| 301 | // create the printer registry and wait for it to get |
| 302 | // the first batch of results which will be delivered |
| 303 | // after reading historical data. This should be pretty |
| 304 | // fast, so just wait before showing the UI. |
| 305 | mPrinterRegistry = new PrinterRegistry(PrintActivity.this, |
| 306 | new Runnable() { |
| 307 | @Override |
| 308 | public void run() { |
| 309 | onPrinterRegistryReady(documentAdapter); |
| 310 | } |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 311 | }, LOADER_ID_PRINT_REGISTRY, LOADER_ID_PRINT_REGISTRY_INT); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | private void onPrinterRegistryReady(IBinder documentAdapter) { |
| 315 | // Now that we are bound to the local print spooler service |
| 316 | // and the printer registry loaded the historical printers |
| 317 | // we can show the UI without flickering. |
| 318 | setTitle(R.string.print_dialog); |
| 319 | setContentView(R.layout.print_activity); |
| 320 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 321 | try { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 322 | mFileProvider = new MutexFileProvider( |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 323 | PrintSpoolerService.generateFileForPrintJob( |
| 324 | PrintActivity.this, mPrintJob.getId())); |
| 325 | } catch (IOException ioe) { |
| 326 | // At this point we cannot recover, so just take it down. |
| 327 | throw new IllegalStateException("Cannot create print job file", ioe); |
| 328 | } |
| 329 | |
| 330 | mPrintPreviewController = new PrintPreviewController(PrintActivity.this, |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 331 | mFileProvider); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 332 | mPrintedDocument = new RemotePrintDocument(PrintActivity.this, |
| 333 | IPrintDocumentAdapter.Stub.asInterface(documentAdapter), |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 334 | mFileProvider, new RemotePrintDocument.RemoteAdapterDeathObserver() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 335 | @Override |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 336 | public void onDied() { |
Svetoslav | 05e041b | 2014-10-14 14:14:49 -0700 | [diff] [blame] | 337 | // If we are finishing or we are in a state that we do not need any |
| 338 | // data from the printing app, then no need to finish. |
| 339 | if (isFinishing() || (isFinalState(mState) && !mPrintedDocument.isUpdating())) { |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 340 | return; |
| 341 | } |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 342 | setState(STATE_PRINT_CANCELED); |
Philip P. Moltmann | 645a3e1 | 2016-02-25 11:20:41 -0800 | [diff] [blame] | 343 | mPrintedDocument.cancel(true); |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 344 | doFinish(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 345 | } |
| 346 | }, PrintActivity.this); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 347 | mProgressMessageController = new ProgressMessageController( |
| 348 | PrintActivity.this); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 349 | mMediaSizeComparator = new MediaSizeComparator(PrintActivity.this); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 350 | mDestinationSpinnerAdapter = new DestinationAdapter(); |
| 351 | |
| 352 | bindUi(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 353 | updateOptionsUi(); |
| 354 | |
| 355 | // Now show the updated UI to avoid flicker. |
| 356 | mOptionsContent.setVisibility(View.VISIBLE); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 357 | mSelectedPages = computeSelectedPages(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 358 | mPrintedDocument.start(); |
| 359 | |
| 360 | ensurePreviewUiShown(); |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 361 | |
| 362 | setState(STATE_CONFIGURING); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 363 | } |
| 364 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 365 | @Override |
Philip P. Moltmann | e2b95e4 | 2015-11-20 11:53:12 -0800 | [diff] [blame] | 366 | public void onStart() { |
| 367 | super.onStart(); |
Philip P. Moltmann | 51dbc8e | 2016-02-01 13:56:45 -0800 | [diff] [blame] | 368 | if (mPrinterRegistry != null && mCurrentPrinter != null) { |
Svetoslav | d724a40 | 2014-09-16 11:53:15 -0700 | [diff] [blame] | 369 | mPrinterRegistry.setTrackedPrinter(mCurrentPrinter.getId()); |
| 370 | } |
Chris Wren | dcc34fd | 2015-07-30 14:27:02 -0400 | [diff] [blame] | 371 | MetricsLogger.count(this, "print_preview", 1); |
Svetoslav | d724a40 | 2014-09-16 11:53:15 -0700 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | @Override |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 375 | public void onPause() { |
Svetoslav | 3ef8e20 | 2014-09-10 14:35:58 -0700 | [diff] [blame] | 376 | PrintSpoolerService spooler = mSpoolerProvider.getSpooler(); |
| 377 | |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 378 | if (mState == STATE_INITIALIZING) { |
Svetoslav | 3ef8e20 | 2014-09-10 14:35:58 -0700 | [diff] [blame] | 379 | if (isFinishing()) { |
Philip P. Moltmann | cf5b777 | 2016-02-01 16:58:57 -0800 | [diff] [blame] | 380 | if (spooler != null) { |
| 381 | spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_CANCELED, null); |
| 382 | } |
Svetoslav | 3ef8e20 | 2014-09-10 14:35:58 -0700 | [diff] [blame] | 383 | } |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 384 | super.onPause(); |
| 385 | return; |
| 386 | } |
| 387 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 388 | if (isFinishing()) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 389 | spooler.updatePrintJobUserConfigurableOptionsNoPersistence(mPrintJob); |
| 390 | |
| 391 | switch (mState) { |
Svetoslav | b59555c | 2014-07-24 10:13:00 -0700 | [diff] [blame] | 392 | case STATE_PRINT_COMPLETED: { |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 393 | if (mCurrentPrinter == mDestinationSpinnerAdapter.getPdfPrinter()) { |
| 394 | spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_COMPLETED, |
| 395 | null); |
| 396 | } else { |
| 397 | spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_QUEUED, |
| 398 | null); |
| 399 | } |
Svetoslav | b59555c | 2014-07-24 10:13:00 -0700 | [diff] [blame] | 400 | } break; |
| 401 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 402 | case STATE_CREATE_FILE_FAILED: { |
| 403 | spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_FAILED, |
| 404 | getString(R.string.print_write_error_message)); |
| 405 | } break; |
| 406 | |
| 407 | default: { |
| 408 | spooler.setPrintJobState(mPrintJob.getId(), PrintJobInfo.STATE_CANCELED, null); |
| 409 | } break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 410 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Philip P. Moltmann | e2b95e4 | 2015-11-20 11:53:12 -0800 | [diff] [blame] | 413 | super.onPause(); |
| 414 | } |
| 415 | |
| 416 | @Override |
| 417 | protected void onStop() { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 418 | mPrinterAvailabilityDetector.cancel(); |
Philip P. Moltmann | 51dbc8e | 2016-02-01 13:56:45 -0800 | [diff] [blame] | 419 | |
| 420 | if (mPrinterRegistry != null) { |
| 421 | mPrinterRegistry.setTrackedPrinter(null); |
| 422 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 423 | |
Philip P. Moltmann | e2b95e4 | 2015-11-20 11:53:12 -0800 | [diff] [blame] | 424 | super.onStop(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 429 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 430 | event.startTracking(); |
| 431 | return true; |
| 432 | } |
| 433 | return super.onKeyDown(keyCode, event); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 438 | if (mState == STATE_INITIALIZING) { |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 439 | doFinish(); |
| 440 | return true; |
| 441 | } |
| 442 | |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 443 | if (mState == STATE_PRINT_CANCELED || mState == STATE_PRINT_CONFIRMED |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 444 | || mState == STATE_PRINT_COMPLETED) { |
Svetoslav | 3ef8e20 | 2014-09-10 14:35:58 -0700 | [diff] [blame] | 445 | return true; |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 448 | if (keyCode == KeyEvent.KEYCODE_BACK |
| 449 | && event.isTracking() && !event.isCanceled()) { |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 450 | if (mPrintPreviewController != null && mPrintPreviewController.isOptionsOpened() |
Svetoslav | 15cbc8a | 2014-07-11 09:45:07 -0700 | [diff] [blame] | 451 | && !hasErrors()) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 452 | mPrintPreviewController.closeOptions(); |
| 453 | } else { |
| 454 | cancelPrint(); |
| 455 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 456 | return true; |
| 457 | } |
| 458 | return super.onKeyUp(keyCode, event); |
| 459 | } |
| 460 | |
| 461 | @Override |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 462 | public void onRequestContentUpdate() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 463 | if (canUpdateDocument()) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 464 | updateDocument(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
| 468 | @Override |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 469 | public void onMalformedPdfFile() { |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 470 | onPrintDocumentError("Cannot print a malformed PDF file"); |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void onSecurePdfFile() { |
| 475 | onPrintDocumentError("Cannot print a password protected PDF file"); |
| 476 | } |
| 477 | |
| 478 | private void onPrintDocumentError(String message) { |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 479 | setState(mProgressMessageController.cancel()); |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 480 | ensureErrorUiShown(null, PrintErrorFragment.ACTION_RETRY); |
| 481 | |
| 482 | setState(STATE_UPDATE_FAILED); |
| 483 | |
| 484 | updateOptionsUi(); |
Svet Ganov | fce84f0 | 2014-10-31 16:56:52 -0700 | [diff] [blame] | 485 | |
| 486 | mPrintedDocument.kill(message); |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | @Override |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 490 | public void onActionPerformed() { |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 491 | if (mState == STATE_UPDATE_FAILED |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 492 | && canUpdateDocument() && updateDocument(true)) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 493 | ensurePreviewUiShown(); |
| 494 | setState(STATE_CONFIGURING); |
| 495 | updateOptionsUi(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 499 | @Override |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 500 | public void onUpdateCanceled() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 501 | if (DEBUG) { |
| 502 | Log.i(LOG_TAG, "onUpdateCanceled()"); |
| 503 | } |
| 504 | |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 505 | setState(mProgressMessageController.cancel()); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 506 | ensurePreviewUiShown(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 507 | |
| 508 | switch (mState) { |
| 509 | case STATE_PRINT_CONFIRMED: { |
| 510 | requestCreatePdfFileOrFinish(); |
| 511 | } break; |
| 512 | |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 513 | case STATE_CREATE_FILE_FAILED: |
| 514 | case STATE_PRINT_COMPLETED: |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 515 | case STATE_PRINT_CANCELED: { |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 516 | doFinish(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 517 | } break; |
| 518 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | @Override |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 522 | public void onUpdateCompleted(RemotePrintDocumentInfo document) { |
| 523 | if (DEBUG) { |
| 524 | Log.i(LOG_TAG, "onUpdateCompleted()"); |
| 525 | } |
| 526 | |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 527 | setState(mProgressMessageController.cancel()); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 528 | ensurePreviewUiShown(); |
| 529 | |
| 530 | // Update the print job with the info for the written document. The page |
| 531 | // count we get from the remote document is the pages in the document from |
| 532 | // the app perspective but the print job should contain the page count from |
| 533 | // print service perspective which is the pages in the written PDF not the |
| 534 | // pages in the printed document. |
| 535 | PrintDocumentInfo info = document.info; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 536 | if (info != null) { |
| 537 | final int pageCount = PageRangeUtils.getNormalizedPageCount(document.writtenPages, |
| 538 | getAdjustedPageCount(info)); |
| 539 | PrintDocumentInfo adjustedInfo = new PrintDocumentInfo.Builder(info.getName()) |
| 540 | .setContentType(info.getContentType()) |
| 541 | .setPageCount(pageCount) |
| 542 | .build(); |
| 543 | mPrintJob.setDocumentInfo(adjustedInfo); |
| 544 | mPrintJob.setPages(document.printedPages); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 545 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 546 | |
| 547 | switch (mState) { |
| 548 | case STATE_PRINT_CONFIRMED: { |
| 549 | requestCreatePdfFileOrFinish(); |
| 550 | } break; |
| 551 | |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 552 | case STATE_CREATE_FILE_FAILED: |
| 553 | case STATE_PRINT_COMPLETED: |
Svet Ganov | eaaf051 | 2014-11-26 04:09:27 -0800 | [diff] [blame] | 554 | case STATE_PRINT_CANCELED: { |
| 555 | updateOptionsUi(); |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 556 | |
| 557 | doFinish(); |
Svet Ganov | eaaf051 | 2014-11-26 04:09:27 -0800 | [diff] [blame] | 558 | } break; |
| 559 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 560 | default: { |
| 561 | updatePrintPreviewController(document.changed); |
| 562 | |
| 563 | setState(STATE_CONFIGURING); |
| 564 | updateOptionsUi(); |
| 565 | } break; |
| 566 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | @Override |
| 570 | public void onUpdateFailed(CharSequence error) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 571 | if (DEBUG) { |
| 572 | Log.i(LOG_TAG, "onUpdateFailed()"); |
| 573 | } |
| 574 | |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 575 | setState(mProgressMessageController.cancel()); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 576 | ensureErrorUiShown(error, PrintErrorFragment.ACTION_RETRY); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 577 | |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 578 | if (mState == STATE_CREATE_FILE_FAILED |
| 579 | || mState == STATE_PRINT_COMPLETED |
| 580 | || mState == STATE_PRINT_CANCELED) { |
| 581 | doFinish(); |
| 582 | } |
| 583 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 584 | setState(STATE_UPDATE_FAILED); |
| 585 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 586 | updateOptionsUi(); |
| 587 | } |
| 588 | |
| 589 | @Override |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 590 | public void onOptionsOpened() { |
| 591 | updateSelectedPagesFromPreview(); |
| 592 | } |
| 593 | |
| 594 | @Override |
| 595 | public void onOptionsClosed() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 596 | // Make sure the IME is not on the way of preview as |
| 597 | // the user may have used it to type copies or range. |
Yohei Yukawa | 777ef95 | 2015-11-25 20:32:24 -0800 | [diff] [blame] | 598 | InputMethodManager imm = getSystemService(InputMethodManager.class); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 599 | imm.hideSoftInputFromWindow(mDestinationSpinner.getWindowToken(), 0); |
| 600 | } |
| 601 | |
| 602 | private void updatePrintPreviewController(boolean contentUpdated) { |
| 603 | // If we have not heard from the application, do nothing. |
| 604 | RemotePrintDocumentInfo documentInfo = mPrintedDocument.getDocumentInfo(); |
| 605 | if (!documentInfo.laidout) { |
| 606 | return; |
| 607 | } |
| 608 | |
| 609 | // Update the preview controller. |
| 610 | mPrintPreviewController.onContentUpdated(contentUpdated, |
| 611 | getAdjustedPageCount(documentInfo.info), |
| 612 | mPrintedDocument.getDocumentInfo().writtenPages, |
| 613 | mSelectedPages, mPrintJob.getAttributes().getMediaSize(), |
| 614 | mPrintJob.getAttributes().getMinMargins()); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | @Override |
| 619 | public boolean canOpenOptions() { |
| 620 | return true; |
| 621 | } |
| 622 | |
| 623 | @Override |
| 624 | public boolean canCloseOptions() { |
| 625 | return !hasErrors(); |
| 626 | } |
| 627 | |
| 628 | @Override |
| 629 | public void onConfigurationChanged(Configuration newConfig) { |
| 630 | super.onConfigurationChanged(newConfig); |
Svet Ganov | f6cd14d | 2014-11-20 07:43:30 -0800 | [diff] [blame] | 631 | if (mPrintPreviewController != null) { |
| 632 | mPrintPreviewController.onOrientationChanged(); |
| 633 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 637 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 638 | switch (requestCode) { |
| 639 | case ACTIVITY_REQUEST_CREATE_FILE: { |
| 640 | onStartCreateDocumentActivityResult(resultCode, data); |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 641 | } break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 642 | |
| 643 | case ACTIVITY_REQUEST_SELECT_PRINTER: { |
| 644 | onSelectPrinterActivityResult(resultCode, data); |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 645 | } break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 646 | |
| 647 | case ACTIVITY_REQUEST_POPULATE_ADVANCED_PRINT_OPTIONS: { |
| 648 | onAdvancedPrintOptionsActivityResult(resultCode, data); |
Svetoslav | 5ef522b | 2014-07-23 20:15:09 -0700 | [diff] [blame] | 649 | } break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | private void startCreateDocumentActivity() { |
Svetoslav | e1dcb39 | 2014-09-26 19:49:14 -0700 | [diff] [blame] | 654 | if (!isResumed()) { |
| 655 | return; |
| 656 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 657 | PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info; |
| 658 | if (info == null) { |
| 659 | return; |
| 660 | } |
| 661 | Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
| 662 | intent.setType("application/pdf"); |
| 663 | intent.putExtra(Intent.EXTRA_TITLE, info.getName()); |
| 664 | intent.putExtra(DocumentsContract.EXTRA_PACKAGE_NAME, mCallingPackageName); |
| 665 | startActivityForResult(intent, ACTIVITY_REQUEST_CREATE_FILE); |
| 666 | } |
| 667 | |
| 668 | private void onStartCreateDocumentActivityResult(int resultCode, Intent data) { |
| 669 | if (resultCode == RESULT_OK && data != null) { |
Svetoslav | b59555c | 2014-07-24 10:13:00 -0700 | [diff] [blame] | 670 | updateOptionsUi(); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 671 | final Uri uri = data.getData(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 672 | // Calling finish here does not invoke lifecycle callbacks but we |
| 673 | // update the print job in onPause if finishing, hence post a message. |
| 674 | mDestinationSpinner.post(new Runnable() { |
| 675 | @Override |
| 676 | public void run() { |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 677 | transformDocumentAndFinish(uri); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 678 | } |
| 679 | }); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 680 | } else if (resultCode == RESULT_CANCELED) { |
Svetoslav | b75632c | 2014-09-17 18:38:27 -0700 | [diff] [blame] | 681 | mState = STATE_CONFIGURING; |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 682 | |
| 683 | // The previous update might have been canceled |
| 684 | updateDocument(false); |
| 685 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 686 | updateOptionsUi(); |
| 687 | } else { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 688 | setState(STATE_CREATE_FILE_FAILED); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 689 | updateOptionsUi(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 690 | // Calling finish here does not invoke lifecycle callbacks but we |
| 691 | // update the print job in onPause if finishing, hence post a message. |
| 692 | mDestinationSpinner.post(new Runnable() { |
| 693 | @Override |
| 694 | public void run() { |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 695 | doFinish(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 696 | } |
| 697 | }); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
| 701 | private void startSelectPrinterActivity() { |
| 702 | Intent intent = new Intent(this, SelectPrinterActivity.class); |
| 703 | startActivityForResult(intent, ACTIVITY_REQUEST_SELECT_PRINTER); |
| 704 | } |
| 705 | |
| 706 | private void onSelectPrinterActivityResult(int resultCode, Intent data) { |
| 707 | if (resultCode == RESULT_OK && data != null) { |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 708 | PrinterInfo printerInfo = data.getParcelableExtra( |
| 709 | SelectPrinterActivity.INTENT_EXTRA_PRINTER); |
| 710 | if (printerInfo != null) { |
| 711 | mCurrentPrinter = printerInfo; |
| 712 | mDestinationSpinnerAdapter.ensurePrinterInVisibleAdapterPosition(printerInfo); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
Masaaki Iwaguchi | 86d13f3 | 2015-04-03 16:11:39 +0900 | [diff] [blame] | 716 | if (mCurrentPrinter != null) { |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 717 | // Trigger PrintersObserver.onChanged() to adjust selection back to current printer |
| 718 | mDestinationSpinnerAdapter.notifyDataSetChanged(); |
Masaaki Iwaguchi | 86d13f3 | 2015-04-03 16:11:39 +0900 | [diff] [blame] | 719 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | private void startAdvancedPrintOptionsActivity(PrinterInfo printer) { |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 723 | if (mAdvancedPrintOptionsActivity == null) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 724 | return; |
| 725 | } |
| 726 | |
| 727 | Intent intent = new Intent(Intent.ACTION_MAIN); |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 728 | intent.setComponent(mAdvancedPrintOptionsActivity); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 729 | |
| 730 | List<ResolveInfo> resolvedActivities = getPackageManager() |
| 731 | .queryIntentActivities(intent, 0); |
| 732 | if (resolvedActivities.isEmpty()) { |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | // The activity is a component name, therefore it is one or none. |
| 737 | if (resolvedActivities.get(0).activityInfo.exported) { |
Philip P. Moltmann | d365f69 | 2016-02-29 13:06:14 -0800 | [diff] [blame] | 738 | PrintJobInfo.Builder printJobBuilder = new PrintJobInfo.Builder(mPrintJob); |
| 739 | printJobBuilder.setPages(mSelectedPages); |
| 740 | |
| 741 | intent.putExtra(PrintService.EXTRA_PRINT_JOB_INFO, printJobBuilder.build()); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 742 | intent.putExtra(PrintService.EXTRA_PRINTER_INFO, printer); |
Svet Ganov | f12b393 | 2015-07-25 12:10:54 -0700 | [diff] [blame] | 743 | intent.putExtra(PrintService.EXTRA_PRINT_DOCUMENT_INFO, |
Svet Ganov | 5772b5c | 2015-06-11 02:46:45 -0700 | [diff] [blame] | 744 | mPrintedDocument.getDocumentInfo().info); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 745 | |
| 746 | // This is external activity and may not be there. |
| 747 | try { |
| 748 | startActivityForResult(intent, ACTIVITY_REQUEST_POPULATE_ADVANCED_PRINT_OPTIONS); |
| 749 | } catch (ActivityNotFoundException anfe) { |
| 750 | Log.e(LOG_TAG, "Error starting activity for intent: " + intent, anfe); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | private void onAdvancedPrintOptionsActivityResult(int resultCode, Intent data) { |
| 756 | if (resultCode != RESULT_OK || data == null) { |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | PrintJobInfo printJobInfo = data.getParcelableExtra(PrintService.EXTRA_PRINT_JOB_INFO); |
| 761 | |
| 762 | if (printJobInfo == null) { |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | // Take the advanced options without interpretation. |
| 767 | mPrintJob.setAdvancedOptions(printJobInfo.getAdvancedOptions()); |
| 768 | |
Philip P. Moltmann | d365f69 | 2016-02-29 13:06:14 -0800 | [diff] [blame] | 769 | if (printJobInfo.getCopies() < 1) { |
| 770 | Log.w(LOG_TAG, "Cannot apply return value from advanced options activity. Copies " + |
| 771 | "must be 1 or more. Actual value is: " + printJobInfo.getCopies() + ". " + |
| 772 | "Ignoring."); |
| 773 | } else { |
| 774 | mCopiesEditText.setText(String.valueOf(printJobInfo.getCopies())); |
| 775 | mPrintJob.setCopies(printJobInfo.getCopies()); |
| 776 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 777 | |
| 778 | PrintAttributes currAttributes = mPrintJob.getAttributes(); |
| 779 | PrintAttributes newAttributes = printJobInfo.getAttributes(); |
| 780 | |
Svet Ganov | 2eb7fad | 2014-10-01 17:49:16 -0700 | [diff] [blame] | 781 | if (newAttributes != null) { |
| 782 | // Take the media size only if the current printer supports is. |
| 783 | MediaSize oldMediaSize = currAttributes.getMediaSize(); |
| 784 | MediaSize newMediaSize = newAttributes.getMediaSize(); |
Philip P. Moltmann | d365f69 | 2016-02-29 13:06:14 -0800 | [diff] [blame] | 785 | if (newMediaSize != null && !oldMediaSize.equals(newMediaSize)) { |
Svet Ganov | 2eb7fad | 2014-10-01 17:49:16 -0700 | [diff] [blame] | 786 | final int mediaSizeCount = mMediaSizeSpinnerAdapter.getCount(); |
| 787 | MediaSize newMediaSizePortrait = newAttributes.getMediaSize().asPortrait(); |
| 788 | for (int i = 0; i < mediaSizeCount; i++) { |
| 789 | MediaSize supportedSizePortrait = mMediaSizeSpinnerAdapter.getItem(i) |
| 790 | .value.asPortrait(); |
| 791 | if (supportedSizePortrait.equals(newMediaSizePortrait)) { |
| 792 | currAttributes.setMediaSize(newMediaSize); |
| 793 | mMediaSizeSpinner.setSelection(i); |
| 794 | if (currAttributes.getMediaSize().isPortrait()) { |
| 795 | if (mOrientationSpinner.getSelectedItemPosition() != 0) { |
| 796 | mOrientationSpinner.setSelection(0); |
| 797 | } |
| 798 | } else { |
| 799 | if (mOrientationSpinner.getSelectedItemPosition() != 1) { |
| 800 | mOrientationSpinner.setSelection(1); |
| 801 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 802 | } |
Svet Ganov | 2eb7fad | 2014-10-01 17:49:16 -0700 | [diff] [blame] | 803 | break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 804 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 807 | |
Svet Ganov | 2eb7fad | 2014-10-01 17:49:16 -0700 | [diff] [blame] | 808 | // Take the resolution only if the current printer supports is. |
| 809 | Resolution oldResolution = currAttributes.getResolution(); |
| 810 | Resolution newResolution = newAttributes.getResolution(); |
| 811 | if (!oldResolution.equals(newResolution)) { |
| 812 | PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities(); |
| 813 | if (capabilities != null) { |
| 814 | List<Resolution> resolutions = capabilities.getResolutions(); |
| 815 | final int resolutionCount = resolutions.size(); |
| 816 | for (int i = 0; i < resolutionCount; i++) { |
| 817 | Resolution resolution = resolutions.get(i); |
| 818 | if (resolution.equals(newResolution)) { |
| 819 | currAttributes.setResolution(resolution); |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | // Take the color mode only if the current printer supports it. |
| 827 | final int currColorMode = currAttributes.getColorMode(); |
| 828 | final int newColorMode = newAttributes.getColorMode(); |
| 829 | if (currColorMode != newColorMode) { |
| 830 | final int colorModeCount = mColorModeSpinner.getCount(); |
| 831 | for (int i = 0; i < colorModeCount; i++) { |
| 832 | final int supportedColorMode = mColorModeSpinnerAdapter.getItem(i).value; |
| 833 | if (supportedColorMode == newColorMode) { |
| 834 | currAttributes.setColorMode(newColorMode); |
| 835 | mColorModeSpinner.setSelection(i); |
| 836 | break; |
| 837 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 838 | } |
| 839 | } |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 840 | |
| 841 | // Take the duplex mode only if the current printer supports it. |
| 842 | final int currDuplexMode = currAttributes.getDuplexMode(); |
| 843 | final int newDuplexMode = newAttributes.getDuplexMode(); |
| 844 | if (currDuplexMode != newDuplexMode) { |
| 845 | final int duplexModeCount = mDuplexModeSpinner.getCount(); |
| 846 | for (int i = 0; i < duplexModeCount; i++) { |
| 847 | final int supportedDuplexMode = mDuplexModeSpinnerAdapter.getItem(i).value; |
| 848 | if (supportedDuplexMode == newDuplexMode) { |
| 849 | currAttributes.setDuplexMode(newDuplexMode); |
| 850 | mDuplexModeSpinner.setSelection(i); |
| 851 | break; |
| 852 | } |
| 853 | } |
| 854 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Svetoslav | 528424c | 2014-09-26 19:11:29 -0700 | [diff] [blame] | 857 | // Handle selected page changes making sure they are in the doc. |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 858 | PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info; |
| 859 | final int pageCount = (info != null) ? getAdjustedPageCount(info) : 0; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 860 | PageRange[] pageRanges = printJobInfo.getPages(); |
Svetoslav | 528424c | 2014-09-26 19:11:29 -0700 | [diff] [blame] | 861 | if (pageRanges != null && pageCount > 0) { |
| 862 | pageRanges = PageRangeUtils.normalize(pageRanges); |
| 863 | |
| 864 | List<PageRange> validatedList = new ArrayList<>(); |
| 865 | final int rangeCount = pageRanges.length; |
| 866 | for (int i = 0; i < rangeCount; i++) { |
| 867 | PageRange pageRange = pageRanges[i]; |
| 868 | if (pageRange.getEnd() >= pageCount) { |
| 869 | final int rangeStart = pageRange.getStart(); |
| 870 | final int rangeEnd = pageCount - 1; |
| 871 | if (rangeStart <= rangeEnd) { |
| 872 | pageRange = new PageRange(rangeStart, rangeEnd); |
| 873 | validatedList.add(pageRange); |
| 874 | } |
| 875 | break; |
| 876 | } |
| 877 | validatedList.add(pageRange); |
| 878 | } |
| 879 | |
| 880 | if (!validatedList.isEmpty()) { |
| 881 | PageRange[] validatedArray = new PageRange[validatedList.size()]; |
| 882 | validatedList.toArray(validatedArray); |
| 883 | updateSelectedPages(validatedArray, pageCount); |
| 884 | } |
| 885 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 886 | |
| 887 | // Update the content if needed. |
| 888 | if (canUpdateDocument()) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 889 | updateDocument(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 893 | private void setState(int state) { |
| 894 | if (isFinalState(mState)) { |
| 895 | if (isFinalState(state)) { |
| 896 | mState = state; |
| 897 | } |
| 898 | } else { |
| 899 | mState = state; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | private static boolean isFinalState(int state) { |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 904 | return state == STATE_PRINT_CANCELED |
| 905 | || state == STATE_PRINT_COMPLETED |
| 906 | || state == STATE_CREATE_FILE_FAILED; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | private void updateSelectedPagesFromPreview() { |
| 910 | PageRange[] selectedPages = mPrintPreviewController.getSelectedPages(); |
| 911 | if (!Arrays.equals(mSelectedPages, selectedPages)) { |
| 912 | updateSelectedPages(selectedPages, |
| 913 | getAdjustedPageCount(mPrintedDocument.getDocumentInfo().info)); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | private void updateSelectedPages(PageRange[] selectedPages, int pageInDocumentCount) { |
| 918 | if (selectedPages == null || selectedPages.length <= 0) { |
| 919 | return; |
| 920 | } |
| 921 | |
| 922 | selectedPages = PageRangeUtils.normalize(selectedPages); |
| 923 | |
| 924 | // Handle the case where all pages are specified explicitly |
| 925 | // instead of the *all pages* constant. |
| 926 | if (PageRangeUtils.isAllPages(selectedPages, pageInDocumentCount)) { |
| 927 | selectedPages = new PageRange[] {PageRange.ALL_PAGES}; |
| 928 | } |
| 929 | |
| 930 | if (Arrays.equals(mSelectedPages, selectedPages)) { |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | mSelectedPages = selectedPages; |
| 935 | mPrintJob.setPages(selectedPages); |
| 936 | |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 937 | if (Arrays.equals(selectedPages, PageRange.ALL_PAGES_ARRAY)) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 938 | if (mRangeOptionsSpinner.getSelectedItemPosition() != 0) { |
| 939 | mRangeOptionsSpinner.setSelection(0); |
| 940 | mPageRangeEditText.setText(""); |
| 941 | } |
| 942 | } else if (selectedPages[0].getStart() >= 0 |
| 943 | && selectedPages[selectedPages.length - 1].getEnd() < pageInDocumentCount) { |
| 944 | if (mRangeOptionsSpinner.getSelectedItemPosition() != 1) { |
| 945 | mRangeOptionsSpinner.setSelection(1); |
| 946 | } |
| 947 | |
| 948 | StringBuilder builder = new StringBuilder(); |
| 949 | final int pageRangeCount = selectedPages.length; |
| 950 | for (int i = 0; i < pageRangeCount; i++) { |
| 951 | if (builder.length() > 0) { |
| 952 | builder.append(','); |
| 953 | } |
| 954 | |
| 955 | final int shownStartPage; |
| 956 | final int shownEndPage; |
| 957 | PageRange pageRange = selectedPages[i]; |
| 958 | if (pageRange.equals(PageRange.ALL_PAGES)) { |
| 959 | shownStartPage = 1; |
| 960 | shownEndPage = pageInDocumentCount; |
| 961 | } else { |
| 962 | shownStartPage = pageRange.getStart() + 1; |
| 963 | shownEndPage = pageRange.getEnd() + 1; |
| 964 | } |
| 965 | |
| 966 | builder.append(shownStartPage); |
| 967 | |
| 968 | if (shownStartPage != shownEndPage) { |
| 969 | builder.append('-'); |
| 970 | builder.append(shownEndPage); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | mPageRangeEditText.setText(builder.toString()); |
| 975 | } |
| 976 | } |
| 977 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 978 | private void ensureProgressUiShown() { |
Svetoslav | 23d3361 | 2014-09-16 10:50:52 -0700 | [diff] [blame] | 979 | if (isFinishing()) { |
| 980 | return; |
| 981 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 982 | if (mUiState != UI_STATE_PROGRESS) { |
| 983 | mUiState = UI_STATE_PROGRESS; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 984 | mPrintPreviewController.setUiShown(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 985 | Fragment fragment = PrintProgressFragment.newInstance(); |
| 986 | showFragment(fragment); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | private void ensurePreviewUiShown() { |
Svetoslav | 23d3361 | 2014-09-16 10:50:52 -0700 | [diff] [blame] | 991 | if (isFinishing()) { |
| 992 | return; |
| 993 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 994 | if (mUiState != UI_STATE_PREVIEW) { |
| 995 | mUiState = UI_STATE_PREVIEW; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 996 | mPrintPreviewController.setUiShown(true); |
| 997 | showFragment(null); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 998 | } |
| 999 | } |
| 1000 | |
| 1001 | private void ensureErrorUiShown(CharSequence message, int action) { |
Svetoslav | 23d3361 | 2014-09-16 10:50:52 -0700 | [diff] [blame] | 1002 | if (isFinishing()) { |
| 1003 | return; |
| 1004 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1005 | if (mUiState != UI_STATE_ERROR) { |
| 1006 | mUiState = UI_STATE_ERROR; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1007 | mPrintPreviewController.setUiShown(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1008 | Fragment fragment = PrintErrorFragment.newInstance(message, action); |
| 1009 | showFragment(fragment); |
| 1010 | } |
| 1011 | } |
| 1012 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1013 | private void showFragment(Fragment newFragment) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1014 | FragmentTransaction transaction = getFragmentManager().beginTransaction(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1015 | Fragment oldFragment = getFragmentManager().findFragmentByTag(FRAGMENT_TAG); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1016 | if (oldFragment != null) { |
| 1017 | transaction.remove(oldFragment); |
| 1018 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1019 | if (newFragment != null) { |
| 1020 | transaction.add(R.id.embedded_content_container, newFragment, FRAGMENT_TAG); |
| 1021 | } |
Svetoslav | d25ddc3 | 2015-11-24 12:54:18 -0800 | [diff] [blame] | 1022 | transaction.commitAllowingStateLoss(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1023 | getFragmentManager().executePendingTransactions(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | private void requestCreatePdfFileOrFinish() { |
Philip P. Moltmann | 645a3e1 | 2016-02-25 11:20:41 -0800 | [diff] [blame] | 1027 | mPrintedDocument.cancel(false); |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 1028 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1029 | if (mCurrentPrinter == mDestinationSpinnerAdapter.getPdfPrinter()) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1030 | startCreateDocumentActivity(); |
| 1031 | } else { |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 1032 | transformDocumentAndFinish(null); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1033 | } |
| 1034 | } |
| 1035 | |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1036 | /** |
| 1037 | * Clear the selected page range and update the preview if needed. |
| 1038 | */ |
| 1039 | private void clearPageRanges() { |
| 1040 | mRangeOptionsSpinner.setSelection(0); |
| 1041 | mPageRangeEditText.setError(null); |
| 1042 | mPageRangeEditText.setText(""); |
| 1043 | mSelectedPages = PageRange.ALL_PAGES_ARRAY; |
| 1044 | |
| 1045 | if (!Arrays.equals(mSelectedPages, mPrintPreviewController.getSelectedPages())) { |
| 1046 | updatePrintPreviewController(false); |
| 1047 | } |
| 1048 | } |
| 1049 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1050 | private void updatePrintAttributesFromCapabilities(PrinterCapabilitiesInfo capabilities) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1051 | boolean clearRanges = false; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1052 | PrintAttributes defaults = capabilities.getDefaults(); |
| 1053 | |
| 1054 | // Sort the media sizes based on the current locale. |
| 1055 | List<MediaSize> sortedMediaSizes = new ArrayList<>(capabilities.getMediaSizes()); |
| 1056 | Collections.sort(sortedMediaSizes, mMediaSizeComparator); |
| 1057 | |
| 1058 | PrintAttributes attributes = mPrintJob.getAttributes(); |
| 1059 | |
| 1060 | // Media size. |
| 1061 | MediaSize currMediaSize = attributes.getMediaSize(); |
| 1062 | if (currMediaSize == null) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1063 | clearRanges = true; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1064 | attributes.setMediaSize(defaults.getMediaSize()); |
| 1065 | } else { |
Philip P. Moltmann | dc5765e | 2016-01-22 09:39:35 -0800 | [diff] [blame] | 1066 | MediaSize newMediaSize = null; |
| 1067 | boolean isPortrait = currMediaSize.isPortrait(); |
| 1068 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1069 | // Try to find the current media size in the capabilities as |
| 1070 | // it may be in a different orientation. |
| 1071 | MediaSize currMediaSizePortrait = currMediaSize.asPortrait(); |
| 1072 | final int mediaSizeCount = sortedMediaSizes.size(); |
| 1073 | for (int i = 0; i < mediaSizeCount; i++) { |
| 1074 | MediaSize mediaSize = sortedMediaSizes.get(i); |
| 1075 | if (currMediaSizePortrait.equals(mediaSize.asPortrait())) { |
Philip P. Moltmann | dc5765e | 2016-01-22 09:39:35 -0800 | [diff] [blame] | 1076 | newMediaSize = mediaSize; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1077 | break; |
| 1078 | } |
| 1079 | } |
| 1080 | // If we did not find the current media size fall back to default. |
Philip P. Moltmann | dc5765e | 2016-01-22 09:39:35 -0800 | [diff] [blame] | 1081 | if (newMediaSize == null) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1082 | clearRanges = true; |
Philip P. Moltmann | dc5765e | 2016-01-22 09:39:35 -0800 | [diff] [blame] | 1083 | newMediaSize = defaults.getMediaSize(); |
| 1084 | } |
| 1085 | |
| 1086 | if (newMediaSize != null) { |
| 1087 | if (isPortrait) { |
| 1088 | attributes.setMediaSize(newMediaSize.asPortrait()); |
| 1089 | } else { |
| 1090 | attributes.setMediaSize(newMediaSize.asLandscape()); |
| 1091 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | // Color mode. |
| 1096 | final int colorMode = attributes.getColorMode(); |
| 1097 | if ((capabilities.getColorModes() & colorMode) == 0) { |
| 1098 | attributes.setColorMode(defaults.getColorMode()); |
| 1099 | } |
| 1100 | |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1101 | // Duplex mode. |
| 1102 | final int duplexMode = attributes.getDuplexMode(); |
| 1103 | if ((capabilities.getDuplexModes() & duplexMode) == 0) { |
| 1104 | attributes.setDuplexMode(defaults.getDuplexMode()); |
| 1105 | } |
| 1106 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1107 | // Resolution |
| 1108 | Resolution resolution = attributes.getResolution(); |
| 1109 | if (resolution == null || !capabilities.getResolutions().contains(resolution)) { |
| 1110 | attributes.setResolution(defaults.getResolution()); |
| 1111 | } |
| 1112 | |
| 1113 | // Margins. |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1114 | if (!Objects.equals(attributes.getMinMargins(), defaults.getMinMargins())) { |
| 1115 | clearRanges = true; |
| 1116 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1117 | attributes.setMinMargins(defaults.getMinMargins()); |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1118 | |
| 1119 | if (clearRanges) { |
| 1120 | clearPageRanges(); |
| 1121 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 1124 | private boolean updateDocument(boolean clearLastError) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1125 | if (!clearLastError && mPrintedDocument.hasUpdateError()) { |
| 1126 | return false; |
| 1127 | } |
| 1128 | |
| 1129 | if (clearLastError && mPrintedDocument.hasUpdateError()) { |
| 1130 | mPrintedDocument.clearUpdateError(); |
| 1131 | } |
| 1132 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 1133 | final boolean preview = mState != STATE_PRINT_CONFIRMED; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1134 | final PageRange[] pages; |
| 1135 | if (preview) { |
| 1136 | pages = mPrintPreviewController.getRequestedPages(); |
| 1137 | } else { |
| 1138 | pages = mPrintPreviewController.getSelectedPages(); |
| 1139 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1140 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1141 | final boolean willUpdate = mPrintedDocument.update(mPrintJob.getAttributes(), |
| 1142 | pages, preview); |
| 1143 | |
Svetoslav | 6552bf3 | 2014-09-03 21:15:55 -0700 | [diff] [blame] | 1144 | if (willUpdate && !mPrintedDocument.hasLaidOutPages()) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1145 | // When the update is done we update the print preview. |
| 1146 | mProgressMessageController.post(); |
| 1147 | return true; |
Svetoslav | 7fd5ada | 2014-09-16 14:41:17 -0700 | [diff] [blame] | 1148 | } else if (!willUpdate) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1149 | // Update preview. |
| 1150 | updatePrintPreviewController(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | return false; |
| 1154 | } |
| 1155 | |
| 1156 | private void addCurrentPrinterToHistory() { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1157 | if (mCurrentPrinter != null) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1158 | PrinterId fakePdfPrinterId = mDestinationSpinnerAdapter.getPdfPrinter().getId(); |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1159 | if (!mCurrentPrinter.getId().equals(fakePdfPrinterId)) { |
| 1160 | mPrinterRegistry.addHistoricalPrinter(mCurrentPrinter); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1165 | private void cancelPrint() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1166 | setState(STATE_PRINT_CANCELED); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1167 | updateOptionsUi(); |
Philip P. Moltmann | 645a3e1 | 2016-02-25 11:20:41 -0800 | [diff] [blame] | 1168 | mPrintedDocument.cancel(true); |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 1169 | doFinish(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Philip P. Moltmann | c309d71 | 2016-03-31 13:49:38 -0700 | [diff] [blame] | 1172 | /** |
| 1173 | * Update the selected pages from the text field. |
| 1174 | */ |
| 1175 | private void updateSelectedPagesFromTextField() { |
| 1176 | PageRange[] selectedPages = computeSelectedPages(); |
| 1177 | if (!Arrays.equals(mSelectedPages, selectedPages)) { |
| 1178 | mSelectedPages = selectedPages; |
| 1179 | // Update preview. |
| 1180 | updatePrintPreviewController(false); |
| 1181 | } |
| 1182 | } |
| 1183 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1184 | private void confirmPrint() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1185 | setState(STATE_PRINT_CONFIRMED); |
| 1186 | |
Chris Wren | dcc34fd | 2015-07-30 14:27:02 -0400 | [diff] [blame] | 1187 | MetricsLogger.count(this, "print_confirmed", 1); |
| 1188 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1189 | updateOptionsUi(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1190 | addCurrentPrinterToHistory(); |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1191 | setUserPrinted(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1192 | |
Philip P. Moltmann | c309d71 | 2016-03-31 13:49:38 -0700 | [diff] [blame] | 1193 | // updateSelectedPagesFromTextField migth update the preview, hence apply the preview first |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1194 | updateSelectedPagesFromPreview(); |
Philip P. Moltmann | c309d71 | 2016-03-31 13:49:38 -0700 | [diff] [blame] | 1195 | updateSelectedPagesFromTextField(); |
| 1196 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1197 | mPrintPreviewController.closeOptions(); |
| 1198 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1199 | if (canUpdateDocument()) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 1200 | updateDocument(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1201 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1202 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1203 | if (!mPrintedDocument.isUpdating()) { |
| 1204 | requestCreatePdfFileOrFinish(); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | private void bindUi() { |
| 1209 | // Summary |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 1210 | mSummaryContainer = findViewById(R.id.summary_content); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1211 | mSummaryCopies = (TextView) findViewById(R.id.copies_count_summary); |
| 1212 | mSummaryPaperSize = (TextView) findViewById(R.id.paper_size_summary); |
| 1213 | |
| 1214 | // Options container |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1215 | mOptionsContent = (PrintContentView) findViewById(R.id.options_content); |
| 1216 | mOptionsContent.setOptionsStateChangeListener(this); |
| 1217 | mOptionsContent.setOpenOptionsController(this); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1218 | |
| 1219 | OnItemSelectedListener itemSelectedListener = new MyOnItemSelectedListener(); |
| 1220 | OnClickListener clickListener = new MyClickListener(); |
| 1221 | |
| 1222 | // Copies |
| 1223 | mCopiesEditText = (EditText) findViewById(R.id.copies_edittext); |
| 1224 | mCopiesEditText.setOnFocusChangeListener(mSelectAllOnFocusListener); |
| 1225 | mCopiesEditText.setText(MIN_COPIES_STRING); |
| 1226 | mCopiesEditText.setSelection(mCopiesEditText.getText().length()); |
| 1227 | mCopiesEditText.addTextChangedListener(new EditTextWatcher()); |
| 1228 | |
| 1229 | // Destination. |
Philip P. Moltmann | 1bb7f36 | 2016-02-26 14:21:20 -0800 | [diff] [blame] | 1230 | mPrintersObserver = new PrintersObserver(); |
| 1231 | mDestinationSpinnerAdapter.registerDataSetObserver(mPrintersObserver); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1232 | mDestinationSpinner = (Spinner) findViewById(R.id.destination_spinner); |
| 1233 | mDestinationSpinner.setAdapter(mDestinationSpinnerAdapter); |
| 1234 | mDestinationSpinner.setOnItemSelectedListener(itemSelectedListener); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1235 | |
| 1236 | // Media size. |
| 1237 | mMediaSizeSpinnerAdapter = new ArrayAdapter<>( |
Svetoslav | c404cac | 2014-08-27 18:37:16 -0700 | [diff] [blame] | 1238 | this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1239 | mMediaSizeSpinner = (Spinner) findViewById(R.id.paper_size_spinner); |
| 1240 | mMediaSizeSpinner.setAdapter(mMediaSizeSpinnerAdapter); |
| 1241 | mMediaSizeSpinner.setOnItemSelectedListener(itemSelectedListener); |
| 1242 | |
| 1243 | // Color mode. |
| 1244 | mColorModeSpinnerAdapter = new ArrayAdapter<>( |
Svetoslav | c404cac | 2014-08-27 18:37:16 -0700 | [diff] [blame] | 1245 | this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1246 | mColorModeSpinner = (Spinner) findViewById(R.id.color_spinner); |
| 1247 | mColorModeSpinner.setAdapter(mColorModeSpinnerAdapter); |
| 1248 | mColorModeSpinner.setOnItemSelectedListener(itemSelectedListener); |
| 1249 | |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1250 | // Duplex mode. |
| 1251 | mDuplexModeSpinnerAdapter = new ArrayAdapter<>( |
| 1252 | this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1); |
| 1253 | mDuplexModeSpinner = (Spinner) findViewById(R.id.duplex_spinner); |
| 1254 | mDuplexModeSpinner.setAdapter(mDuplexModeSpinnerAdapter); |
| 1255 | mDuplexModeSpinner.setOnItemSelectedListener(itemSelectedListener); |
| 1256 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1257 | // Orientation |
| 1258 | mOrientationSpinnerAdapter = new ArrayAdapter<>( |
Svetoslav | c404cac | 2014-08-27 18:37:16 -0700 | [diff] [blame] | 1259 | this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1260 | String[] orientationLabels = getResources().getStringArray( |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1261 | R.array.orientation_labels); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1262 | mOrientationSpinnerAdapter.add(new SpinnerItem<>( |
| 1263 | ORIENTATION_PORTRAIT, orientationLabels[0])); |
| 1264 | mOrientationSpinnerAdapter.add(new SpinnerItem<>( |
| 1265 | ORIENTATION_LANDSCAPE, orientationLabels[1])); |
| 1266 | mOrientationSpinner = (Spinner) findViewById(R.id.orientation_spinner); |
| 1267 | mOrientationSpinner.setAdapter(mOrientationSpinnerAdapter); |
| 1268 | mOrientationSpinner.setOnItemSelectedListener(itemSelectedListener); |
| 1269 | |
| 1270 | // Range options |
Svetoslav | c404cac | 2014-08-27 18:37:16 -0700 | [diff] [blame] | 1271 | ArrayAdapter<SpinnerItem<Integer>> rangeOptionsSpinnerAdapter = new ArrayAdapter<>( |
| 1272 | this, android.R.layout.simple_spinner_dropdown_item, android.R.id.text1); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1273 | mRangeOptionsSpinner = (Spinner) findViewById(R.id.range_options_spinner); |
| 1274 | mRangeOptionsSpinner.setAdapter(rangeOptionsSpinnerAdapter); |
| 1275 | mRangeOptionsSpinner.setOnItemSelectedListener(itemSelectedListener); |
Svetoslav | 73764e3 | 2014-07-15 15:56:46 -0700 | [diff] [blame] | 1276 | updatePageRangeOptions(PrintDocumentInfo.PAGE_COUNT_UNKNOWN); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1277 | |
| 1278 | // Page range |
| 1279 | mPageRangeTitle = (TextView) findViewById(R.id.page_range_title); |
| 1280 | mPageRangeEditText = (EditText) findViewById(R.id.page_range_edittext); |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1281 | mPageRangeEditText.setVisibility(View.INVISIBLE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1282 | mPageRangeEditText.setOnFocusChangeListener(mSelectAllOnFocusListener); |
| 1283 | mPageRangeEditText.addTextChangedListener(new RangeTextWatcher()); |
| 1284 | |
| 1285 | // Advanced options button. |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1286 | mMoreOptionsButton = (Button) findViewById(R.id.more_options_button); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1287 | mMoreOptionsButton.setOnClickListener(clickListener); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1288 | |
| 1289 | // Print button |
| 1290 | mPrintButton = (ImageView) findViewById(R.id.print_button); |
| 1291 | mPrintButton.setOnClickListener(clickListener); |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1292 | |
Philip P. Moltmann | c2f913d | 2016-02-01 12:03:48 -0800 | [diff] [blame] | 1293 | // The UI is now initialized |
| 1294 | mIsOptionsUiBound = true; |
| 1295 | |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1296 | // Special prompt instead of destination spinner for the first time the user printed |
| 1297 | if (!hasUserEverPrinted()) { |
| 1298 | mShowDestinationPrompt = true; |
| 1299 | |
| 1300 | mSummaryCopies.setEnabled(false); |
| 1301 | mSummaryPaperSize.setEnabled(false); |
| 1302 | |
| 1303 | mDestinationSpinner.setOnTouchListener(new View.OnTouchListener() { |
| 1304 | @Override |
| 1305 | public boolean onTouch(View v, MotionEvent event) { |
| 1306 | mShowDestinationPrompt = false; |
| 1307 | mSummaryCopies.setEnabled(true); |
| 1308 | mSummaryPaperSize.setEnabled(true); |
| 1309 | updateOptionsUi(); |
| 1310 | |
| 1311 | mDestinationSpinner.setOnTouchListener(null); |
| 1312 | mDestinationSpinnerAdapter.notifyDataSetChanged(); |
| 1313 | |
| 1314 | return false; |
| 1315 | } |
| 1316 | }); |
| 1317 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 1320 | @Override |
| 1321 | public Loader<List<PrintServiceInfo>> onCreateLoader(int id, Bundle args) { |
| 1322 | return new PrintServicesLoader((PrintManager) getSystemService(Context.PRINT_SERVICE), this, |
| 1323 | PrintManager.ENABLED_SERVICES); |
| 1324 | } |
| 1325 | |
| 1326 | @Override |
| 1327 | public void onLoadFinished(Loader<List<PrintServiceInfo>> loader, |
| 1328 | List<PrintServiceInfo> services) { |
| 1329 | ComponentName newAdvancedPrintOptionsActivity = null; |
| 1330 | if (mCurrentPrinter != null && services != null) { |
| 1331 | final int numServices = services.size(); |
| 1332 | for (int i = 0; i < numServices; i++) { |
| 1333 | PrintServiceInfo service = services.get(i); |
| 1334 | |
| 1335 | if (service.getComponentName().equals(mCurrentPrinter.getId().getServiceName())) { |
| 1336 | String advancedOptionsActivityName = service.getAdvancedOptionsActivityName(); |
| 1337 | |
| 1338 | if (!TextUtils.isEmpty(advancedOptionsActivityName)) { |
| 1339 | newAdvancedPrintOptionsActivity = new ComponentName( |
| 1340 | service.getComponentName().getPackageName(), |
| 1341 | advancedOptionsActivityName); |
| 1342 | |
| 1343 | break; |
| 1344 | } |
| 1345 | } |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | if (!Objects.equals(newAdvancedPrintOptionsActivity, mAdvancedPrintOptionsActivity)) { |
| 1350 | mAdvancedPrintOptionsActivity = newAdvancedPrintOptionsActivity; |
| 1351 | updateOptionsUi(); |
| 1352 | } |
| 1353 | |
| 1354 | boolean newArePrintServicesEnabled = services != null && !services.isEmpty(); |
| 1355 | if (mArePrintServicesEnabled != newArePrintServicesEnabled) { |
| 1356 | mArePrintServicesEnabled = newArePrintServicesEnabled; |
| 1357 | |
| 1358 | // Reload mDestinationSpinnerAdapter as mArePrintServicesEnabled changed and the adapter |
| 1359 | // reads that in DestinationAdapter#getMoreItemTitle |
| 1360 | if (mDestinationSpinnerAdapter != null) { |
| 1361 | mDestinationSpinnerAdapter.notifyDataSetChanged(); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | @Override |
| 1367 | public void onLoaderReset(Loader<List<PrintServiceInfo>> loader) { |
| 1368 | if (!isFinishing()) { |
| 1369 | onLoadFinished(loader, null); |
| 1370 | } |
| 1371 | } |
| 1372 | |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 1373 | /** |
| 1374 | * A dialog that asks the user to approve a {@link PrintService}. This dialog is automatically |
| 1375 | * dismissed if the same {@link PrintService} gets approved by another |
| 1376 | * {@link PrintServiceApprovalDialog}. |
| 1377 | */ |
| 1378 | private static final class PrintServiceApprovalDialog extends DialogFragment |
| 1379 | implements OnSharedPreferenceChangeListener { |
| 1380 | private static final String PRINTSERVICE_KEY = "PRINTSERVICE"; |
| 1381 | private ApprovedPrintServices mApprovedServices; |
| 1382 | |
| 1383 | /** |
| 1384 | * Create a new {@link PrintServiceApprovalDialog} that ask the user to approve a |
| 1385 | * {@link PrintService}. |
| 1386 | * |
| 1387 | * @param printService The {@link ComponentName} of the service to approve |
| 1388 | * @return A new {@link PrintServiceApprovalDialog} that might approve the service |
| 1389 | */ |
| 1390 | static PrintServiceApprovalDialog newInstance(ComponentName printService) { |
| 1391 | PrintServiceApprovalDialog dialog = new PrintServiceApprovalDialog(); |
| 1392 | |
| 1393 | Bundle args = new Bundle(); |
| 1394 | args.putParcelable(PRINTSERVICE_KEY, printService); |
| 1395 | dialog.setArguments(args); |
| 1396 | |
| 1397 | return dialog; |
| 1398 | } |
| 1399 | |
| 1400 | @Override |
| 1401 | public void onStop() { |
| 1402 | super.onStop(); |
| 1403 | |
| 1404 | mApprovedServices.unregisterChangeListener(this); |
| 1405 | } |
| 1406 | |
| 1407 | @Override |
| 1408 | public void onStart() { |
| 1409 | super.onStart(); |
| 1410 | |
| 1411 | ComponentName printService = getArguments().getParcelable(PRINTSERVICE_KEY); |
| 1412 | synchronized (ApprovedPrintServices.sLock) { |
| 1413 | if (mApprovedServices.isApprovedService(printService)) { |
| 1414 | dismiss(); |
| 1415 | } else { |
| 1416 | mApprovedServices.registerChangeListenerLocked(this); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |
| 1421 | @Override |
| 1422 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 1423 | super.onCreateDialog(savedInstanceState); |
| 1424 | |
| 1425 | mApprovedServices = new ApprovedPrintServices(getActivity()); |
| 1426 | |
| 1427 | PackageManager packageManager = getActivity().getPackageManager(); |
| 1428 | CharSequence serviceLabel; |
| 1429 | try { |
| 1430 | ComponentName printService = getArguments().getParcelable(PRINTSERVICE_KEY); |
| 1431 | |
| 1432 | serviceLabel = packageManager.getApplicationInfo(printService.getPackageName(), 0) |
| 1433 | .loadLabel(packageManager); |
| 1434 | } catch (NameNotFoundException e) { |
| 1435 | serviceLabel = null; |
| 1436 | } |
| 1437 | |
| 1438 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
| 1439 | builder.setTitle(getString(R.string.print_service_security_warning_title, |
| 1440 | serviceLabel)) |
| 1441 | .setMessage(getString(R.string.print_service_security_warning_summary, |
| 1442 | serviceLabel)) |
| 1443 | .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { |
| 1444 | @Override |
| 1445 | public void onClick(DialogInterface dialog, int id) { |
| 1446 | ComponentName printService = |
| 1447 | getArguments().getParcelable(PRINTSERVICE_KEY); |
| 1448 | // Prevent onSharedPreferenceChanged from getting triggered |
| 1449 | mApprovedServices |
| 1450 | .unregisterChangeListener(PrintServiceApprovalDialog.this); |
| 1451 | |
| 1452 | mApprovedServices.addApprovedService(printService); |
| 1453 | ((PrintActivity) getActivity()).confirmPrint(); |
| 1454 | } |
| 1455 | }) |
| 1456 | .setNegativeButton(android.R.string.cancel, null); |
| 1457 | |
| 1458 | return builder.create(); |
| 1459 | } |
| 1460 | |
| 1461 | @Override |
| 1462 | public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { |
| 1463 | ComponentName printService = getArguments().getParcelable(PRINTSERVICE_KEY); |
| 1464 | |
| 1465 | synchronized (ApprovedPrintServices.sLock) { |
| 1466 | if (mApprovedServices.isApprovedService(printService)) { |
| 1467 | dismiss(); |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1473 | private final class MyClickListener implements OnClickListener { |
| 1474 | @Override |
| 1475 | public void onClick(View view) { |
| 1476 | if (view == mPrintButton) { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1477 | if (mCurrentPrinter != null) { |
Philip P. Moltmann | 853a6f5 | 2015-11-03 10:38:56 -0800 | [diff] [blame] | 1478 | if (mDestinationSpinnerAdapter.getPdfPrinter() == mCurrentPrinter) { |
| 1479 | confirmPrint(); |
| 1480 | } else { |
| 1481 | ApprovedPrintServices approvedServices = |
| 1482 | new ApprovedPrintServices(PrintActivity.this); |
| 1483 | |
| 1484 | ComponentName printService = mCurrentPrinter.getId().getServiceName(); |
| 1485 | if (approvedServices.isApprovedService(printService)) { |
| 1486 | confirmPrint(); |
| 1487 | } else { |
| 1488 | PrintServiceApprovalDialog.newInstance(printService) |
| 1489 | .show(getFragmentManager(), "approve"); |
| 1490 | } |
| 1491 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1492 | } else { |
| 1493 | cancelPrint(); |
| 1494 | } |
| 1495 | } else if (view == mMoreOptionsButton) { |
Philip P. Moltmann | c309d71 | 2016-03-31 13:49:38 -0700 | [diff] [blame] | 1496 | // The selected pages is only applied once the user leaves the text field. A click |
| 1497 | // on this button, does not count as leaving. |
| 1498 | updateSelectedPagesFromTextField(); |
| 1499 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1500 | if (mCurrentPrinter != null) { |
| 1501 | startAdvancedPrintOptionsActivity(mCurrentPrinter); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1502 | } |
| 1503 | } |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | private static boolean canPrint(PrinterInfo printer) { |
| 1508 | return printer.getCapabilities() != null |
| 1509 | && printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE; |
| 1510 | } |
| 1511 | |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1512 | /** |
| 1513 | * Disable all options UI elements, beside the {@link #mDestinationSpinner} |
| 1514 | */ |
| 1515 | private void disableOptionsUi() { |
| 1516 | mCopiesEditText.setEnabled(false); |
| 1517 | mCopiesEditText.setFocusable(false); |
| 1518 | mMediaSizeSpinner.setEnabled(false); |
| 1519 | mColorModeSpinner.setEnabled(false); |
| 1520 | mDuplexModeSpinner.setEnabled(false); |
| 1521 | mOrientationSpinner.setEnabled(false); |
| 1522 | mRangeOptionsSpinner.setEnabled(false); |
| 1523 | mPageRangeEditText.setEnabled(false); |
| 1524 | mPrintButton.setVisibility(View.GONE); |
| 1525 | mMoreOptionsButton.setEnabled(false); |
| 1526 | } |
| 1527 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1528 | void updateOptionsUi() { |
Philip P. Moltmann | c2f913d | 2016-02-01 12:03:48 -0800 | [diff] [blame] | 1529 | if (!mIsOptionsUiBound) { |
| 1530 | return; |
| 1531 | } |
| 1532 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1533 | // Always update the summary. |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 1534 | updateSummary(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1535 | |
| 1536 | if (mState == STATE_PRINT_CONFIRMED |
Svetoslav | b59555c | 2014-07-24 10:13:00 -0700 | [diff] [blame] | 1537 | || mState == STATE_PRINT_COMPLETED |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1538 | || mState == STATE_PRINT_CANCELED |
| 1539 | || mState == STATE_UPDATE_FAILED |
| 1540 | || mState == STATE_CREATE_FILE_FAILED |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1541 | || mState == STATE_PRINTER_UNAVAILABLE |
| 1542 | || mState == STATE_UPDATE_SLOW) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1543 | if (mState != STATE_PRINTER_UNAVAILABLE) { |
| 1544 | mDestinationSpinner.setEnabled(false); |
| 1545 | } |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1546 | disableOptionsUi(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1547 | return; |
| 1548 | } |
| 1549 | |
| 1550 | // If no current printer, or it has no capabilities, or it is not |
| 1551 | // available, we disable all print options except the destination. |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1552 | if (mCurrentPrinter == null || !canPrint(mCurrentPrinter)) { |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1553 | disableOptionsUi(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1554 | return; |
| 1555 | } |
| 1556 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1557 | PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1558 | PrintAttributes defaultAttributes = capabilities.getDefaults(); |
| 1559 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1560 | // Destination. |
| 1561 | mDestinationSpinner.setEnabled(true); |
| 1562 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1563 | // Media size. |
| 1564 | mMediaSizeSpinner.setEnabled(true); |
| 1565 | |
| 1566 | List<MediaSize> mediaSizes = new ArrayList<>(capabilities.getMediaSizes()); |
| 1567 | // Sort the media sizes based on the current locale. |
| 1568 | Collections.sort(mediaSizes, mMediaSizeComparator); |
| 1569 | |
| 1570 | PrintAttributes attributes = mPrintJob.getAttributes(); |
| 1571 | |
| 1572 | // If the media sizes changed, we update the adapter and the spinner. |
| 1573 | boolean mediaSizesChanged = false; |
| 1574 | final int mediaSizeCount = mediaSizes.size(); |
| 1575 | if (mediaSizeCount != mMediaSizeSpinnerAdapter.getCount()) { |
| 1576 | mediaSizesChanged = true; |
| 1577 | } else { |
| 1578 | for (int i = 0; i < mediaSizeCount; i++) { |
| 1579 | if (!mediaSizes.get(i).equals(mMediaSizeSpinnerAdapter.getItem(i).value)) { |
| 1580 | mediaSizesChanged = true; |
| 1581 | break; |
| 1582 | } |
| 1583 | } |
| 1584 | } |
| 1585 | if (mediaSizesChanged) { |
| 1586 | // Remember the old media size to try selecting it again. |
| 1587 | int oldMediaSizeNewIndex = AdapterView.INVALID_POSITION; |
| 1588 | MediaSize oldMediaSize = attributes.getMediaSize(); |
| 1589 | |
| 1590 | // Rebuild the adapter data. |
| 1591 | mMediaSizeSpinnerAdapter.clear(); |
| 1592 | for (int i = 0; i < mediaSizeCount; i++) { |
| 1593 | MediaSize mediaSize = mediaSizes.get(i); |
| 1594 | if (oldMediaSize != null |
| 1595 | && mediaSize.asPortrait().equals(oldMediaSize.asPortrait())) { |
| 1596 | // Update the index of the old selection. |
| 1597 | oldMediaSizeNewIndex = i; |
| 1598 | } |
| 1599 | mMediaSizeSpinnerAdapter.add(new SpinnerItem<>( |
| 1600 | mediaSize, mediaSize.getLabel(getPackageManager()))); |
| 1601 | } |
| 1602 | |
| 1603 | if (oldMediaSizeNewIndex != AdapterView.INVALID_POSITION) { |
| 1604 | // Select the old media size - nothing really changed. |
| 1605 | if (mMediaSizeSpinner.getSelectedItemPosition() != oldMediaSizeNewIndex) { |
| 1606 | mMediaSizeSpinner.setSelection(oldMediaSizeNewIndex); |
| 1607 | } |
| 1608 | } else { |
| 1609 | // Select the first or the default. |
| 1610 | final int mediaSizeIndex = Math.max(mediaSizes.indexOf( |
| 1611 | defaultAttributes.getMediaSize()), 0); |
| 1612 | if (mMediaSizeSpinner.getSelectedItemPosition() != mediaSizeIndex) { |
| 1613 | mMediaSizeSpinner.setSelection(mediaSizeIndex); |
| 1614 | } |
| 1615 | // Respect the orientation of the old selection. |
| 1616 | if (oldMediaSize != null) { |
| 1617 | if (oldMediaSize.isPortrait()) { |
| 1618 | attributes.setMediaSize(mMediaSizeSpinnerAdapter |
| 1619 | .getItem(mediaSizeIndex).value.asPortrait()); |
| 1620 | } else { |
| 1621 | attributes.setMediaSize(mMediaSizeSpinnerAdapter |
| 1622 | .getItem(mediaSizeIndex).value.asLandscape()); |
| 1623 | } |
| 1624 | } |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // Color mode. |
| 1629 | mColorModeSpinner.setEnabled(true); |
| 1630 | final int colorModes = capabilities.getColorModes(); |
| 1631 | |
| 1632 | // If the color modes changed, we update the adapter and the spinner. |
| 1633 | boolean colorModesChanged = false; |
| 1634 | if (Integer.bitCount(colorModes) != mColorModeSpinnerAdapter.getCount()) { |
| 1635 | colorModesChanged = true; |
| 1636 | } else { |
| 1637 | int remainingColorModes = colorModes; |
| 1638 | int adapterIndex = 0; |
| 1639 | while (remainingColorModes != 0) { |
| 1640 | final int colorBitOffset = Integer.numberOfTrailingZeros(remainingColorModes); |
| 1641 | final int colorMode = 1 << colorBitOffset; |
| 1642 | remainingColorModes &= ~colorMode; |
| 1643 | if (colorMode != mColorModeSpinnerAdapter.getItem(adapterIndex).value) { |
| 1644 | colorModesChanged = true; |
| 1645 | break; |
| 1646 | } |
| 1647 | adapterIndex++; |
| 1648 | } |
| 1649 | } |
| 1650 | if (colorModesChanged) { |
| 1651 | // Remember the old color mode to try selecting it again. |
| 1652 | int oldColorModeNewIndex = AdapterView.INVALID_POSITION; |
| 1653 | final int oldColorMode = attributes.getColorMode(); |
| 1654 | |
| 1655 | // Rebuild the adapter data. |
| 1656 | mColorModeSpinnerAdapter.clear(); |
| 1657 | String[] colorModeLabels = getResources().getStringArray(R.array.color_mode_labels); |
| 1658 | int remainingColorModes = colorModes; |
| 1659 | while (remainingColorModes != 0) { |
| 1660 | final int colorBitOffset = Integer.numberOfTrailingZeros(remainingColorModes); |
| 1661 | final int colorMode = 1 << colorBitOffset; |
| 1662 | if (colorMode == oldColorMode) { |
| 1663 | // Update the index of the old selection. |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1664 | oldColorModeNewIndex = mColorModeSpinnerAdapter.getCount(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1665 | } |
| 1666 | remainingColorModes &= ~colorMode; |
| 1667 | mColorModeSpinnerAdapter.add(new SpinnerItem<>(colorMode, |
| 1668 | colorModeLabels[colorBitOffset])); |
| 1669 | } |
| 1670 | if (oldColorModeNewIndex != AdapterView.INVALID_POSITION) { |
| 1671 | // Select the old color mode - nothing really changed. |
| 1672 | if (mColorModeSpinner.getSelectedItemPosition() != oldColorModeNewIndex) { |
| 1673 | mColorModeSpinner.setSelection(oldColorModeNewIndex); |
| 1674 | } |
| 1675 | } else { |
| 1676 | // Select the default. |
| 1677 | final int selectedColorMode = colorModes & defaultAttributes.getColorMode(); |
| 1678 | final int itemCount = mColorModeSpinnerAdapter.getCount(); |
| 1679 | for (int i = 0; i < itemCount; i++) { |
| 1680 | SpinnerItem<Integer> item = mColorModeSpinnerAdapter.getItem(i); |
| 1681 | if (selectedColorMode == item.value) { |
| 1682 | if (mColorModeSpinner.getSelectedItemPosition() != i) { |
| 1683 | mColorModeSpinner.setSelection(i); |
| 1684 | } |
| 1685 | attributes.setColorMode(selectedColorMode); |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1686 | break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1687 | } |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 1692 | // Duplex mode. |
| 1693 | mDuplexModeSpinner.setEnabled(true); |
| 1694 | final int duplexModes = capabilities.getDuplexModes(); |
| 1695 | |
| 1696 | // If the duplex modes changed, we update the adapter and the spinner. |
| 1697 | // Note that we use bit count +1 to account for the no duplex option. |
| 1698 | boolean duplexModesChanged = false; |
| 1699 | if (Integer.bitCount(duplexModes) != mDuplexModeSpinnerAdapter.getCount()) { |
| 1700 | duplexModesChanged = true; |
| 1701 | } else { |
| 1702 | int remainingDuplexModes = duplexModes; |
| 1703 | int adapterIndex = 0; |
| 1704 | while (remainingDuplexModes != 0) { |
| 1705 | final int duplexBitOffset = Integer.numberOfTrailingZeros(remainingDuplexModes); |
| 1706 | final int duplexMode = 1 << duplexBitOffset; |
| 1707 | remainingDuplexModes &= ~duplexMode; |
| 1708 | if (duplexMode != mDuplexModeSpinnerAdapter.getItem(adapterIndex).value) { |
| 1709 | duplexModesChanged = true; |
| 1710 | break; |
| 1711 | } |
| 1712 | adapterIndex++; |
| 1713 | } |
| 1714 | } |
| 1715 | if (duplexModesChanged) { |
| 1716 | // Remember the old duplex mode to try selecting it again. Also the fallback |
| 1717 | // is no duplexing which is always the first item in the dropdown. |
| 1718 | int oldDuplexModeNewIndex = AdapterView.INVALID_POSITION; |
| 1719 | final int oldDuplexMode = attributes.getDuplexMode(); |
| 1720 | |
| 1721 | // Rebuild the adapter data. |
| 1722 | mDuplexModeSpinnerAdapter.clear(); |
| 1723 | String[] duplexModeLabels = getResources().getStringArray(R.array.duplex_mode_labels); |
| 1724 | int remainingDuplexModes = duplexModes; |
| 1725 | while (remainingDuplexModes != 0) { |
| 1726 | final int duplexBitOffset = Integer.numberOfTrailingZeros(remainingDuplexModes); |
| 1727 | final int duplexMode = 1 << duplexBitOffset; |
| 1728 | if (duplexMode == oldDuplexMode) { |
| 1729 | // Update the index of the old selection. |
| 1730 | oldDuplexModeNewIndex = mDuplexModeSpinnerAdapter.getCount(); |
| 1731 | } |
| 1732 | remainingDuplexModes &= ~duplexMode; |
| 1733 | mDuplexModeSpinnerAdapter.add(new SpinnerItem<>(duplexMode, |
| 1734 | duplexModeLabels[duplexBitOffset])); |
| 1735 | } |
| 1736 | |
| 1737 | if (oldDuplexModeNewIndex != AdapterView.INVALID_POSITION) { |
| 1738 | // Select the old duplex mode - nothing really changed. |
| 1739 | if (mDuplexModeSpinner.getSelectedItemPosition() != oldDuplexModeNewIndex) { |
| 1740 | mDuplexModeSpinner.setSelection(oldDuplexModeNewIndex); |
| 1741 | } |
| 1742 | } else { |
| 1743 | // Select the default. |
| 1744 | final int selectedDuplexMode = defaultAttributes.getDuplexMode(); |
| 1745 | final int itemCount = mDuplexModeSpinnerAdapter.getCount(); |
| 1746 | for (int i = 0; i < itemCount; i++) { |
| 1747 | SpinnerItem<Integer> item = mDuplexModeSpinnerAdapter.getItem(i); |
| 1748 | if (selectedDuplexMode == item.value) { |
| 1749 | if (mDuplexModeSpinner.getSelectedItemPosition() != i) { |
| 1750 | mDuplexModeSpinner.setSelection(i); |
| 1751 | } |
| 1752 | attributes.setDuplexMode(selectedDuplexMode); |
| 1753 | break; |
| 1754 | } |
| 1755 | } |
| 1756 | } |
| 1757 | } |
| 1758 | |
| 1759 | mDuplexModeSpinner.setEnabled(mDuplexModeSpinnerAdapter.getCount() > 1); |
| 1760 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1761 | // Orientation |
| 1762 | mOrientationSpinner.setEnabled(true); |
| 1763 | MediaSize mediaSize = attributes.getMediaSize(); |
| 1764 | if (mediaSize != null) { |
| 1765 | if (mediaSize.isPortrait() |
| 1766 | && mOrientationSpinner.getSelectedItemPosition() != 0) { |
| 1767 | mOrientationSpinner.setSelection(0); |
| 1768 | } else if (!mediaSize.isPortrait() |
| 1769 | && mOrientationSpinner.getSelectedItemPosition() != 1) { |
| 1770 | mOrientationSpinner.setSelection(1); |
| 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | // Range options |
| 1775 | PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1776 | final int pageCount = getAdjustedPageCount(info); |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1777 | if (pageCount > 0) { |
| 1778 | if (info != null) { |
| 1779 | if (pageCount == 1) { |
| 1780 | mRangeOptionsSpinner.setEnabled(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1781 | } else { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1782 | mRangeOptionsSpinner.setEnabled(true); |
| 1783 | if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) { |
| 1784 | if (!mPageRangeEditText.isEnabled()) { |
| 1785 | mPageRangeEditText.setEnabled(true); |
| 1786 | mPageRangeEditText.setVisibility(View.VISIBLE); |
| 1787 | mPageRangeTitle.setVisibility(View.VISIBLE); |
| 1788 | mPageRangeEditText.requestFocus(); |
| 1789 | InputMethodManager imm = (InputMethodManager) |
| 1790 | getSystemService(Context.INPUT_METHOD_SERVICE); |
| 1791 | imm.showSoftInput(mPageRangeEditText, 0); |
| 1792 | } |
| 1793 | } else { |
| 1794 | mPageRangeEditText.setEnabled(false); |
| 1795 | mPageRangeEditText.setVisibility(View.INVISIBLE); |
| 1796 | mPageRangeTitle.setVisibility(View.INVISIBLE); |
| 1797 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1798 | } |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1799 | } else { |
| 1800 | if (mRangeOptionsSpinner.getSelectedItemPosition() != 0) { |
| 1801 | mRangeOptionsSpinner.setSelection(0); |
| 1802 | mPageRangeEditText.setText(""); |
| 1803 | } |
| 1804 | mRangeOptionsSpinner.setEnabled(false); |
| 1805 | mPageRangeEditText.setEnabled(false); |
| 1806 | mPageRangeEditText.setVisibility(View.INVISIBLE); |
| 1807 | mPageRangeTitle.setVisibility(View.INVISIBLE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1808 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1809 | } |
| 1810 | |
Svetoslav | 73764e3 | 2014-07-15 15:56:46 -0700 | [diff] [blame] | 1811 | final int newPageCount = getAdjustedPageCount(info); |
| 1812 | if (newPageCount != mCurrentPageCount) { |
| 1813 | mCurrentPageCount = newPageCount; |
| 1814 | updatePageRangeOptions(newPageCount); |
| 1815 | } |
| 1816 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1817 | // Advanced print options |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 1818 | if (mAdvancedPrintOptionsActivity != null) { |
Svetoslav | 3c23824 | 2014-08-19 13:44:29 -0700 | [diff] [blame] | 1819 | mMoreOptionsButton.setVisibility(View.VISIBLE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1820 | mMoreOptionsButton.setEnabled(true); |
| 1821 | } else { |
Svetoslav | 3c23824 | 2014-08-19 13:44:29 -0700 | [diff] [blame] | 1822 | mMoreOptionsButton.setVisibility(View.GONE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1823 | mMoreOptionsButton.setEnabled(false); |
| 1824 | } |
| 1825 | |
| 1826 | // Print |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1827 | if (mDestinationSpinnerAdapter.getPdfPrinter() != mCurrentPrinter) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1828 | mPrintButton.setImageResource(com.android.internal.R.drawable.ic_print); |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 1829 | mPrintButton.setContentDescription(getString(R.string.print_button)); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1830 | } else { |
Svetoslav | f8ffa56 | 2014-07-23 18:22:03 -0700 | [diff] [blame] | 1831 | mPrintButton.setImageResource(R.drawable.ic_menu_savetopdf); |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 1832 | mPrintButton.setContentDescription(getString(R.string.savetopdf_button)); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1833 | } |
Svetoslav | e1dcb39 | 2014-09-26 19:49:14 -0700 | [diff] [blame] | 1834 | if (!mPrintedDocument.getDocumentInfo().laidout |
| 1835 | ||(mRangeOptionsSpinner.getSelectedItemPosition() == 1 |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1836 | && (TextUtils.isEmpty(mPageRangeEditText.getText()) || hasErrors())) |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1837 | || (mRangeOptionsSpinner.getSelectedItemPosition() == 0 |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1838 | && (mPrintedDocument.getDocumentInfo() == null || hasErrors()))) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1839 | mPrintButton.setVisibility(View.GONE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1840 | } else { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1841 | mPrintButton.setVisibility(View.VISIBLE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | // Copies |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1845 | if (mDestinationSpinnerAdapter.getPdfPrinter() != mCurrentPrinter) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1846 | mCopiesEditText.setEnabled(true); |
Svetoslav | c404cac | 2014-08-27 18:37:16 -0700 | [diff] [blame] | 1847 | mCopiesEditText.setFocusableInTouchMode(true); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1848 | } else { |
Svet Ganov | 45e50e9 | 2014-10-23 12:39:08 -0700 | [diff] [blame] | 1849 | CharSequence text = mCopiesEditText.getText(); |
| 1850 | if (TextUtils.isEmpty(text) || !MIN_COPIES_STRING.equals(text.toString())) { |
| 1851 | mCopiesEditText.setText(MIN_COPIES_STRING); |
| 1852 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1853 | mCopiesEditText.setEnabled(false); |
Svetoslav | c404cac | 2014-08-27 18:37:16 -0700 | [diff] [blame] | 1854 | mCopiesEditText.setFocusable(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1855 | } |
| 1856 | if (mCopiesEditText.getError() == null |
| 1857 | && TextUtils.isEmpty(mCopiesEditText.getText())) { |
Svet Ganov | 45e50e9 | 2014-10-23 12:39:08 -0700 | [diff] [blame] | 1858 | mCopiesEditText.setText(MIN_COPIES_STRING); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1859 | mCopiesEditText.requestFocus(); |
| 1860 | } |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 1861 | |
| 1862 | if (mShowDestinationPrompt) { |
| 1863 | disableOptionsUi(); |
| 1864 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 1867 | private void updateSummary() { |
Philip P. Moltmann | c2f913d | 2016-02-01 12:03:48 -0800 | [diff] [blame] | 1868 | if (!mIsOptionsUiBound) { |
| 1869 | return; |
| 1870 | } |
| 1871 | |
Svetoslav | e652b02 | 2014-09-09 22:11:10 -0700 | [diff] [blame] | 1872 | CharSequence copiesText = null; |
| 1873 | CharSequence mediaSizeText = null; |
| 1874 | |
| 1875 | if (!TextUtils.isEmpty(mCopiesEditText.getText())) { |
| 1876 | copiesText = mCopiesEditText.getText(); |
| 1877 | mSummaryCopies.setText(copiesText); |
| 1878 | } |
| 1879 | |
| 1880 | final int selectedMediaIndex = mMediaSizeSpinner.getSelectedItemPosition(); |
| 1881 | if (selectedMediaIndex >= 0) { |
| 1882 | SpinnerItem<MediaSize> mediaItem = mMediaSizeSpinnerAdapter.getItem(selectedMediaIndex); |
| 1883 | mediaSizeText = mediaItem.label; |
| 1884 | mSummaryPaperSize.setText(mediaSizeText); |
| 1885 | } |
| 1886 | |
| 1887 | if (!TextUtils.isEmpty(copiesText) && !TextUtils.isEmpty(mediaSizeText)) { |
| 1888 | String summaryText = getString(R.string.summary_template, copiesText, mediaSizeText); |
| 1889 | mSummaryContainer.setContentDescription(summaryText); |
| 1890 | } |
| 1891 | } |
| 1892 | |
Svetoslav | 73764e3 | 2014-07-15 15:56:46 -0700 | [diff] [blame] | 1893 | private void updatePageRangeOptions(int pageCount) { |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 1894 | @SuppressWarnings("unchecked") |
Svetoslav | 73764e3 | 2014-07-15 15:56:46 -0700 | [diff] [blame] | 1895 | ArrayAdapter<SpinnerItem<Integer>> rangeOptionsSpinnerAdapter = |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 1896 | (ArrayAdapter<SpinnerItem<Integer>>) mRangeOptionsSpinner.getAdapter(); |
Svetoslav | 73764e3 | 2014-07-15 15:56:46 -0700 | [diff] [blame] | 1897 | rangeOptionsSpinnerAdapter.clear(); |
| 1898 | |
| 1899 | final int[] rangeOptionsValues = getResources().getIntArray( |
| 1900 | R.array.page_options_values); |
| 1901 | |
| 1902 | String pageCountLabel = (pageCount > 0) ? String.valueOf(pageCount) : ""; |
| 1903 | String[] rangeOptionsLabels = new String[] { |
| 1904 | getString(R.string.template_all_pages, pageCountLabel), |
| 1905 | getString(R.string.template_page_range, pageCountLabel) |
| 1906 | }; |
| 1907 | |
| 1908 | final int rangeOptionsCount = rangeOptionsLabels.length; |
| 1909 | for (int i = 0; i < rangeOptionsCount; i++) { |
| 1910 | rangeOptionsSpinnerAdapter.add(new SpinnerItem<>( |
| 1911 | rangeOptionsValues[i], rangeOptionsLabels[i])); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1915 | private PageRange[] computeSelectedPages() { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1916 | if (hasErrors()) { |
| 1917 | return null; |
| 1918 | } |
| 1919 | |
| 1920 | if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) { |
| 1921 | List<PageRange> pageRanges = new ArrayList<>(); |
| 1922 | mStringCommaSplitter.setString(mPageRangeEditText.getText().toString()); |
| 1923 | |
| 1924 | while (mStringCommaSplitter.hasNext()) { |
| 1925 | String range = mStringCommaSplitter.next().trim(); |
| 1926 | if (TextUtils.isEmpty(range)) { |
| 1927 | continue; |
| 1928 | } |
| 1929 | final int dashIndex = range.indexOf('-'); |
| 1930 | final int fromIndex; |
| 1931 | final int toIndex; |
| 1932 | |
| 1933 | if (dashIndex > 0) { |
| 1934 | fromIndex = Integer.parseInt(range.substring(0, dashIndex).trim()) - 1; |
| 1935 | // It is possible that the dash is at the end since the input |
| 1936 | // verification can has to allow the user to keep entering if |
| 1937 | // this would lead to a valid input. So we handle this. |
| 1938 | if (dashIndex < range.length() - 1) { |
| 1939 | String fromString = range.substring(dashIndex + 1, range.length()).trim(); |
| 1940 | toIndex = Integer.parseInt(fromString) - 1; |
| 1941 | } else { |
| 1942 | toIndex = fromIndex; |
| 1943 | } |
| 1944 | } else { |
| 1945 | fromIndex = toIndex = Integer.parseInt(range) - 1; |
| 1946 | } |
| 1947 | |
| 1948 | PageRange pageRange = new PageRange(Math.min(fromIndex, toIndex), |
| 1949 | Math.max(fromIndex, toIndex)); |
| 1950 | pageRanges.add(pageRange); |
| 1951 | } |
| 1952 | |
| 1953 | PageRange[] pageRangesArray = new PageRange[pageRanges.size()]; |
| 1954 | pageRanges.toArray(pageRangesArray); |
| 1955 | |
| 1956 | return PageRangeUtils.normalize(pageRangesArray); |
| 1957 | } |
| 1958 | |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 1959 | return PageRange.ALL_PAGES_ARRAY; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1960 | } |
| 1961 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1962 | private int getAdjustedPageCount(PrintDocumentInfo info) { |
| 1963 | if (info != null) { |
| 1964 | final int pageCount = info.getPageCount(); |
| 1965 | if (pageCount != PrintDocumentInfo.PAGE_COUNT_UNKNOWN) { |
| 1966 | return pageCount; |
| 1967 | } |
| 1968 | } |
| 1969 | // If the app does not tell us how many pages are in the |
| 1970 | // doc we ask for all pages and use the document page count. |
| 1971 | return mPrintPreviewController.getFilePageCount(); |
| 1972 | } |
| 1973 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1974 | private boolean hasErrors() { |
| 1975 | return (mCopiesEditText.getError() != null) |
| 1976 | || (mPageRangeEditText.getVisibility() == View.VISIBLE |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1977 | && mPageRangeEditText.getError() != null); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | public void onPrinterAvailable(PrinterInfo printer) { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1981 | if (mCurrentPrinter.equals(printer)) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1982 | setState(STATE_CONFIGURING); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1983 | if (canUpdateDocument()) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 1984 | updateDocument(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1985 | } |
| 1986 | ensurePreviewUiShown(); |
| 1987 | updateOptionsUi(); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | public void onPrinterUnavailable(PrinterInfo printer) { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 1992 | if (mCurrentPrinter.getId().equals(printer.getId())) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 1993 | setState(STATE_PRINTER_UNAVAILABLE); |
Philip P. Moltmann | 645a3e1 | 2016-02-25 11:20:41 -0800 | [diff] [blame] | 1994 | mPrintedDocument.cancel(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 1995 | ensureErrorUiShown(getString(R.string.print_error_printer_unavailable), |
| 1996 | PrintErrorFragment.ACTION_NONE); |
| 1997 | updateOptionsUi(); |
| 1998 | } |
| 1999 | } |
| 2000 | |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2001 | private boolean canUpdateDocument() { |
| 2002 | if (mPrintedDocument.isDestroyed()) { |
| 2003 | return false; |
| 2004 | } |
| 2005 | |
| 2006 | if (hasErrors()) { |
| 2007 | return false; |
| 2008 | } |
| 2009 | |
| 2010 | PrintAttributes attributes = mPrintJob.getAttributes(); |
| 2011 | |
| 2012 | final int colorMode = attributes.getColorMode(); |
| 2013 | if (colorMode != PrintAttributes.COLOR_MODE_COLOR |
| 2014 | && colorMode != PrintAttributes.COLOR_MODE_MONOCHROME) { |
| 2015 | return false; |
| 2016 | } |
| 2017 | if (attributes.getMediaSize() == null) { |
| 2018 | return false; |
| 2019 | } |
| 2020 | if (attributes.getMinMargins() == null) { |
| 2021 | return false; |
| 2022 | } |
| 2023 | if (attributes.getResolution() == null) { |
| 2024 | return false; |
| 2025 | } |
| 2026 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2027 | if (mCurrentPrinter == null) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2028 | return false; |
| 2029 | } |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2030 | PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2031 | if (capabilities == null) { |
| 2032 | return false; |
| 2033 | } |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2034 | if (mCurrentPrinter.getStatus() == PrinterInfo.STATUS_UNAVAILABLE) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2035 | return false; |
| 2036 | } |
| 2037 | |
| 2038 | return true; |
| 2039 | } |
| 2040 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2041 | private void transformDocumentAndFinish(final Uri writeToUri) { |
| 2042 | // If saving to PDF, apply the attibutes as we are acting as a print service. |
| 2043 | PrintAttributes attributes = mDestinationSpinnerAdapter.getPdfPrinter() == mCurrentPrinter |
| 2044 | ? mPrintJob.getAttributes() : null; |
| 2045 | new DocumentTransformer(this, mPrintJob, mFileProvider, attributes, new Runnable() { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2046 | @Override |
| 2047 | public void run() { |
| 2048 | if (writeToUri != null) { |
| 2049 | mPrintedDocument.writeContent(getContentResolver(), writeToUri); |
| 2050 | } |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 2051 | setState(STATE_PRINT_COMPLETED); |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 2052 | doFinish(); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2053 | } |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2054 | }).transform(); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2055 | } |
| 2056 | |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 2057 | private void doFinish() { |
Philip P. Moltmann | 32e0755 | 2016-03-07 10:31:49 -0800 | [diff] [blame] | 2058 | if (mPrintedDocument != null && mPrintedDocument.isUpdating()) { |
Philip P. Moltmann | cc3fa0d | 2016-02-03 11:03:16 -0800 | [diff] [blame] | 2059 | // The printedDocument will call doFinish() when the current command finishes |
| 2060 | return; |
| 2061 | } |
| 2062 | |
Philip P. Moltmann | b170c08 | 2016-03-21 12:48:58 -0700 | [diff] [blame] | 2063 | if (mIsFinishing) { |
Philip P. Moltmann | 0ad7fc5 | 2016-03-07 11:36:06 -0800 | [diff] [blame] | 2064 | return; |
| 2065 | } |
| 2066 | |
Philip P. Moltmann | b170c08 | 2016-03-21 12:48:58 -0700 | [diff] [blame] | 2067 | mIsFinishing = true; |
Philip P. Moltmann | 0ad7fc5 | 2016-03-07 11:36:06 -0800 | [diff] [blame] | 2068 | |
Philip P. Moltmann | 51dbc8e | 2016-02-01 13:56:45 -0800 | [diff] [blame] | 2069 | if (mPrinterRegistry != null) { |
| 2070 | mPrinterRegistry.setTrackedPrinter(null); |
| 2071 | } |
| 2072 | |
Philip P. Moltmann | 1bb7f36 | 2016-02-26 14:21:20 -0800 | [diff] [blame] | 2073 | if (mPrintersObserver != null) { |
| 2074 | mDestinationSpinnerAdapter.unregisterDataSetObserver(mPrintersObserver); |
| 2075 | } |
| 2076 | |
Philip P. Moltmann | 940fa80 | 2016-03-23 16:56:24 -0700 | [diff] [blame] | 2077 | if (mSpoolerProvider != null) { |
| 2078 | mSpoolerProvider.destroy(); |
| 2079 | } |
| 2080 | |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2081 | setState(mProgressMessageController.cancel()); |
| 2082 | |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 2083 | if (mState != STATE_INITIALIZING) { |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 2084 | mPrintedDocument.finish(); |
| 2085 | mPrintedDocument.destroy(); |
Svet Ganov | c80814e | 2014-11-24 02:01:37 -0800 | [diff] [blame] | 2086 | mPrintPreviewController.destroy(new Runnable() { |
| 2087 | @Override |
| 2088 | public void run() { |
| 2089 | finish(); |
| 2090 | } |
| 2091 | }); |
| 2092 | } else { |
| 2093 | finish(); |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 2094 | } |
Svetoslav | e17123d | 2014-09-11 12:39:05 -0700 | [diff] [blame] | 2095 | } |
| 2096 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2097 | private final class SpinnerItem<T> { |
| 2098 | final T value; |
| 2099 | final CharSequence label; |
| 2100 | |
| 2101 | public SpinnerItem(T value, CharSequence label) { |
| 2102 | this.value = value; |
| 2103 | this.label = label; |
| 2104 | } |
| 2105 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 2106 | @Override |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2107 | public String toString() { |
| 2108 | return label.toString(); |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | private final class PrinterAvailabilityDetector implements Runnable { |
| 2113 | private static final long UNAVAILABLE_TIMEOUT_MILLIS = 10000; // 10sec |
| 2114 | |
| 2115 | private boolean mPosted; |
| 2116 | |
| 2117 | private boolean mPrinterUnavailable; |
| 2118 | |
| 2119 | private PrinterInfo mPrinter; |
| 2120 | |
| 2121 | public void updatePrinter(PrinterInfo printer) { |
| 2122 | if (printer.equals(mDestinationSpinnerAdapter.getPdfPrinter())) { |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | final boolean available = printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE |
| 2127 | && printer.getCapabilities() != null; |
| 2128 | final boolean notifyIfAvailable; |
| 2129 | |
| 2130 | if (mPrinter == null || !mPrinter.getId().equals(printer.getId())) { |
| 2131 | notifyIfAvailable = true; |
| 2132 | unpostIfNeeded(); |
| 2133 | mPrinterUnavailable = false; |
| 2134 | mPrinter = new PrinterInfo.Builder(printer).build(); |
| 2135 | } else { |
| 2136 | notifyIfAvailable = |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2137 | (mPrinter.getStatus() == PrinterInfo.STATUS_UNAVAILABLE |
| 2138 | && printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE) |
| 2139 | || (mPrinter.getCapabilities() == null |
| 2140 | && printer.getCapabilities() != null); |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 2141 | mPrinter = printer; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | if (available) { |
| 2145 | unpostIfNeeded(); |
| 2146 | mPrinterUnavailable = false; |
| 2147 | if (notifyIfAvailable) { |
| 2148 | onPrinterAvailable(mPrinter); |
| 2149 | } |
| 2150 | } else { |
| 2151 | if (!mPrinterUnavailable) { |
| 2152 | postIfNeeded(); |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | |
| 2157 | public void cancel() { |
| 2158 | unpostIfNeeded(); |
| 2159 | mPrinterUnavailable = false; |
| 2160 | } |
| 2161 | |
| 2162 | private void postIfNeeded() { |
| 2163 | if (!mPosted) { |
| 2164 | mPosted = true; |
| 2165 | mDestinationSpinner.postDelayed(this, UNAVAILABLE_TIMEOUT_MILLIS); |
| 2166 | } |
| 2167 | } |
| 2168 | |
| 2169 | private void unpostIfNeeded() { |
| 2170 | if (mPosted) { |
| 2171 | mPosted = false; |
| 2172 | mDestinationSpinner.removeCallbacks(this); |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | @Override |
| 2177 | public void run() { |
| 2178 | mPosted = false; |
| 2179 | mPrinterUnavailable = true; |
| 2180 | onPrinterUnavailable(mPrinter); |
| 2181 | } |
| 2182 | } |
| 2183 | |
| 2184 | private static final class PrinterHolder { |
| 2185 | PrinterInfo printer; |
| 2186 | boolean removed; |
| 2187 | |
| 2188 | public PrinterHolder(PrinterInfo printer) { |
| 2189 | this.printer = printer; |
| 2190 | } |
| 2191 | } |
| 2192 | |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 2193 | |
| 2194 | /** |
| 2195 | * Check if the user has ever printed a document |
| 2196 | * |
| 2197 | * @return true iff the user has ever printed a document |
| 2198 | */ |
| 2199 | private boolean hasUserEverPrinted() { |
| 2200 | SharedPreferences preferences = getSharedPreferences(HAS_PRINTED_PREF, MODE_PRIVATE); |
| 2201 | |
| 2202 | return preferences.getBoolean(HAS_PRINTED_PREF, false); |
| 2203 | } |
| 2204 | |
| 2205 | /** |
| 2206 | * Remember that the user printed a document |
| 2207 | */ |
| 2208 | private void setUserPrinted() { |
| 2209 | SharedPreferences preferences = getSharedPreferences(HAS_PRINTED_PREF, MODE_PRIVATE); |
| 2210 | |
| 2211 | if (!preferences.getBoolean(HAS_PRINTED_PREF, false)) { |
| 2212 | SharedPreferences.Editor edit = preferences.edit(); |
| 2213 | |
| 2214 | edit.putBoolean(HAS_PRINTED_PREF, true); |
| 2215 | edit.apply(); |
| 2216 | } |
| 2217 | } |
| 2218 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2219 | private final class DestinationAdapter extends BaseAdapter |
| 2220 | implements PrinterRegistry.OnPrintersChangeListener { |
| 2221 | private final List<PrinterHolder> mPrinterHolders = new ArrayList<>(); |
| 2222 | |
| 2223 | private final PrinterHolder mFakePdfPrinterHolder; |
| 2224 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2225 | private boolean mHistoricalPrintersLoaded; |
| 2226 | |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 2227 | /** |
| 2228 | * Has the {@link #mDestinationSpinner} ever used a view from printer_dropdown_prompt |
| 2229 | */ |
| 2230 | private boolean hadPromptView; |
| 2231 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2232 | public DestinationAdapter() { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2233 | mHistoricalPrintersLoaded = mPrinterRegistry.areHistoricalPrintersLoaded(); |
| 2234 | if (mHistoricalPrintersLoaded) { |
| 2235 | addPrinters(mPrinterHolders, mPrinterRegistry.getPrinters()); |
| 2236 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2237 | mPrinterRegistry.setOnPrintersChangeListener(this); |
| 2238 | mFakePdfPrinterHolder = new PrinterHolder(createFakePdfPrinter()); |
| 2239 | } |
| 2240 | |
| 2241 | public PrinterInfo getPdfPrinter() { |
| 2242 | return mFakePdfPrinterHolder.printer; |
| 2243 | } |
| 2244 | |
| 2245 | public int getPrinterIndex(PrinterId printerId) { |
| 2246 | for (int i = 0; i < getCount(); i++) { |
| 2247 | PrinterHolder printerHolder = (PrinterHolder) getItem(i); |
| 2248 | if (printerHolder != null && !printerHolder.removed |
| 2249 | && printerHolder.printer.getId().equals(printerId)) { |
| 2250 | return i; |
| 2251 | } |
| 2252 | } |
| 2253 | return AdapterView.INVALID_POSITION; |
| 2254 | } |
| 2255 | |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2256 | public void ensurePrinterInVisibleAdapterPosition(PrinterInfo printer) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2257 | final int printerCount = mPrinterHolders.size(); |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2258 | boolean isKnownPrinter = false; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2259 | for (int i = 0; i < printerCount; i++) { |
| 2260 | PrinterHolder printerHolder = mPrinterHolders.get(i); |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2261 | |
| 2262 | if (printerHolder.printer.getId().equals(printer.getId())) { |
| 2263 | isKnownPrinter = true; |
| 2264 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2265 | // If already in the list - do nothing. |
| 2266 | if (i < getCount() - 2) { |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2267 | break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2268 | } |
| 2269 | // Else replace the last one (two items are not printers). |
| 2270 | final int lastPrinterIndex = getCount() - 3; |
| 2271 | mPrinterHolders.set(i, mPrinterHolders.get(lastPrinterIndex)); |
| 2272 | mPrinterHolders.set(lastPrinterIndex, printerHolder); |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2273 | break; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2274 | } |
| 2275 | } |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2276 | |
| 2277 | if (!isKnownPrinter) { |
| 2278 | PrinterHolder printerHolder = new PrinterHolder(printer); |
| 2279 | printerHolder.removed = true; |
| 2280 | |
| 2281 | mPrinterHolders.add(Math.max(0, getCount() - 3), printerHolder); |
| 2282 | } |
| 2283 | |
| 2284 | // Force reload to adjust selection in PrintersObserver.onChanged() |
| 2285 | notifyDataSetChanged(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2286 | } |
| 2287 | |
| 2288 | @Override |
| 2289 | public int getCount() { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2290 | if (mHistoricalPrintersLoaded) { |
| 2291 | return Math.min(mPrinterHolders.size() + 2, DEST_ADAPTER_MAX_ITEM_COUNT); |
| 2292 | } |
| 2293 | return 0; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2294 | } |
| 2295 | |
| 2296 | @Override |
| 2297 | public boolean isEnabled(int position) { |
| 2298 | Object item = getItem(position); |
| 2299 | if (item instanceof PrinterHolder) { |
| 2300 | PrinterHolder printerHolder = (PrinterHolder) item; |
| 2301 | return !printerHolder.removed |
| 2302 | && printerHolder.printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE; |
| 2303 | } |
| 2304 | return true; |
| 2305 | } |
| 2306 | |
| 2307 | @Override |
| 2308 | public Object getItem(int position) { |
| 2309 | if (mPrinterHolders.isEmpty()) { |
| 2310 | if (position == 0) { |
| 2311 | return mFakePdfPrinterHolder; |
| 2312 | } |
| 2313 | } else { |
| 2314 | if (position < 1) { |
| 2315 | return mPrinterHolders.get(position); |
| 2316 | } |
| 2317 | if (position == 1) { |
| 2318 | return mFakePdfPrinterHolder; |
| 2319 | } |
| 2320 | if (position < getCount() - 1) { |
| 2321 | return mPrinterHolders.get(position - 1); |
| 2322 | } |
| 2323 | } |
| 2324 | return null; |
| 2325 | } |
| 2326 | |
| 2327 | @Override |
| 2328 | public long getItemId(int position) { |
| 2329 | if (mPrinterHolders.isEmpty()) { |
| 2330 | if (position == 0) { |
| 2331 | return DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF; |
| 2332 | } else if (position == 1) { |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2333 | return DEST_ADAPTER_ITEM_ID_MORE; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2334 | } |
| 2335 | } else { |
| 2336 | if (position == 1) { |
| 2337 | return DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF; |
| 2338 | } |
| 2339 | if (position == getCount() - 1) { |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2340 | return DEST_ADAPTER_ITEM_ID_MORE; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | return position; |
| 2344 | } |
| 2345 | |
| 2346 | @Override |
| 2347 | public View getDropDownView(int position, View convertView, ViewGroup parent) { |
| 2348 | View view = getView(position, convertView, parent); |
| 2349 | view.setEnabled(isEnabled(position)); |
| 2350 | return view; |
| 2351 | } |
| 2352 | |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2353 | private String getMoreItemTitle() { |
| 2354 | if (mArePrintServicesEnabled) { |
| 2355 | return getString(R.string.all_printers); |
| 2356 | } else { |
| 2357 | return getString(R.string.print_add_printer); |
| 2358 | } |
| 2359 | } |
| 2360 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2361 | @Override |
| 2362 | public View getView(int position, View convertView, ViewGroup parent) { |
Philip P. Moltmann | 5e54896 | 2015-11-13 15:33:40 -0800 | [diff] [blame] | 2363 | if (mShowDestinationPrompt) { |
| 2364 | if (convertView == null) { |
| 2365 | convertView = getLayoutInflater().inflate( |
| 2366 | R.layout.printer_dropdown_prompt, parent, false); |
| 2367 | hadPromptView = true; |
| 2368 | } |
| 2369 | |
| 2370 | return convertView; |
| 2371 | } else { |
| 2372 | // We don't know if we got an recyled printer_dropdown_prompt, hence do not use it |
| 2373 | if (hadPromptView || convertView == null) { |
| 2374 | convertView = getLayoutInflater().inflate( |
| 2375 | R.layout.printer_dropdown_item, parent, false); |
| 2376 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
| 2379 | CharSequence title = null; |
| 2380 | CharSequence subtitle = null; |
| 2381 | Drawable icon = null; |
| 2382 | |
| 2383 | if (mPrinterHolders.isEmpty()) { |
| 2384 | if (position == 0 && getPdfPrinter() != null) { |
| 2385 | PrinterHolder printerHolder = (PrinterHolder) getItem(position); |
| 2386 | title = printerHolder.printer.getName(); |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 2387 | icon = getResources().getDrawable(R.drawable.ic_menu_savetopdf, null); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2388 | } else if (position == 1) { |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2389 | title = getMoreItemTitle(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2390 | } |
| 2391 | } else { |
| 2392 | if (position == 1 && getPdfPrinter() != null) { |
| 2393 | PrinterHolder printerHolder = (PrinterHolder) getItem(position); |
| 2394 | title = printerHolder.printer.getName(); |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 2395 | icon = getResources().getDrawable(R.drawable.ic_menu_savetopdf, null); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2396 | } else if (position == getCount() - 1) { |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2397 | title = getMoreItemTitle(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2398 | } else { |
| 2399 | PrinterHolder printerHolder = (PrinterHolder) getItem(position); |
Philip P. Moltmann | bb9f686 | 2015-12-01 14:44:24 -0800 | [diff] [blame] | 2400 | PrinterInfo printInfo = printerHolder.printer; |
| 2401 | |
| 2402 | title = printInfo.getName(); |
| 2403 | icon = printInfo.loadIcon(PrintActivity.this); |
| 2404 | subtitle = printInfo.getDescription(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2405 | } |
| 2406 | } |
| 2407 | |
| 2408 | TextView titleView = (TextView) convertView.findViewById(R.id.title); |
| 2409 | titleView.setText(title); |
| 2410 | |
| 2411 | TextView subtitleView = (TextView) convertView.findViewById(R.id.subtitle); |
| 2412 | if (!TextUtils.isEmpty(subtitle)) { |
| 2413 | subtitleView.setText(subtitle); |
| 2414 | subtitleView.setVisibility(View.VISIBLE); |
| 2415 | } else { |
| 2416 | subtitleView.setText(null); |
| 2417 | subtitleView.setVisibility(View.GONE); |
| 2418 | } |
| 2419 | |
| 2420 | ImageView iconView = (ImageView) convertView.findViewById(R.id.icon); |
| 2421 | if (icon != null) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2422 | iconView.setVisibility(View.VISIBLE); |
Philip P. Moltmann | 443075a | 2016-01-26 13:04:21 -0800 | [diff] [blame] | 2423 | if (!isEnabled(position)) { |
| 2424 | icon.mutate(); |
| 2425 | |
| 2426 | TypedValue value = new TypedValue(); |
| 2427 | getTheme().resolveAttribute(android.R.attr.disabledAlpha, value, true); |
| 2428 | icon.setAlpha((int)(value.getFloat() * 255)); |
| 2429 | } |
| 2430 | iconView.setImageDrawable(icon); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2431 | } else { |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2432 | iconView.setVisibility(View.INVISIBLE); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | return convertView; |
| 2436 | } |
| 2437 | |
| 2438 | @Override |
| 2439 | public void onPrintersChanged(List<PrinterInfo> printers) { |
| 2440 | // We rearrange the printers if the user selects a printer |
| 2441 | // not shown in the initial short list. Therefore, we have |
| 2442 | // to keep the printer order. |
| 2443 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2444 | // Check if historical printers are loaded as this adapter is open |
| 2445 | // for busyness only if they are. This member is updated here and |
| 2446 | // when the adapter is created because the historical printers may |
| 2447 | // be loaded before or after the adapter is created. |
| 2448 | mHistoricalPrintersLoaded = mPrinterRegistry.areHistoricalPrintersLoaded(); |
| 2449 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2450 | // No old printers - do not bother keeping their position. |
| 2451 | if (mPrinterHolders.isEmpty()) { |
| 2452 | addPrinters(mPrinterHolders, printers); |
| 2453 | notifyDataSetChanged(); |
| 2454 | return; |
| 2455 | } |
| 2456 | |
| 2457 | // Add the new printers to a map. |
| 2458 | ArrayMap<PrinterId, PrinterInfo> newPrintersMap = new ArrayMap<>(); |
| 2459 | final int printerCount = printers.size(); |
| 2460 | for (int i = 0; i < printerCount; i++) { |
| 2461 | PrinterInfo printer = printers.get(i); |
| 2462 | newPrintersMap.put(printer.getId(), printer); |
| 2463 | } |
| 2464 | |
| 2465 | List<PrinterHolder> newPrinterHolders = new ArrayList<>(); |
| 2466 | |
| 2467 | // Update printers we already have which are either updated or removed. |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2468 | // We do not remove the currently selected printer. |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2469 | final int oldPrinterCount = mPrinterHolders.size(); |
| 2470 | for (int i = 0; i < oldPrinterCount; i++) { |
| 2471 | PrinterHolder printerHolder = mPrinterHolders.get(i); |
| 2472 | PrinterId oldPrinterId = printerHolder.printer.getId(); |
| 2473 | PrinterInfo updatedPrinter = newPrintersMap.remove(oldPrinterId); |
| 2474 | if (updatedPrinter != null) { |
| 2475 | printerHolder.printer = updatedPrinter; |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2476 | printerHolder.removed = false; |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2477 | newPrinterHolders.add(printerHolder); |
| 2478 | } else if (mCurrentPrinter != null && mCurrentPrinter.getId().equals(oldPrinterId)){ |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2479 | printerHolder.removed = true; |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2480 | newPrinterHolders.add(printerHolder); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2481 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | // Add the rest of the new printers, i.e. what is left. |
| 2485 | addPrinters(newPrinterHolders, newPrintersMap.values()); |
| 2486 | |
| 2487 | mPrinterHolders.clear(); |
| 2488 | mPrinterHolders.addAll(newPrinterHolders); |
| 2489 | |
| 2490 | notifyDataSetChanged(); |
| 2491 | } |
| 2492 | |
| 2493 | @Override |
| 2494 | public void onPrintersInvalid() { |
| 2495 | mPrinterHolders.clear(); |
| 2496 | notifyDataSetInvalidated(); |
| 2497 | } |
| 2498 | |
| 2499 | public PrinterHolder getPrinterHolder(PrinterId printerId) { |
| 2500 | final int itemCount = getCount(); |
| 2501 | for (int i = 0; i < itemCount; i++) { |
| 2502 | Object item = getItem(i); |
| 2503 | if (item instanceof PrinterHolder) { |
| 2504 | PrinterHolder printerHolder = (PrinterHolder) item; |
| 2505 | if (printerId.equals(printerHolder.printer.getId())) { |
| 2506 | return printerHolder; |
| 2507 | } |
| 2508 | } |
| 2509 | } |
| 2510 | return null; |
| 2511 | } |
| 2512 | |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2513 | /** |
| 2514 | * Remove a printer from the holders if it is marked as removed. |
| 2515 | * |
| 2516 | * @param printerId the id of the printer to remove. |
| 2517 | * |
| 2518 | * @return true iff the printer was removed. |
| 2519 | */ |
| 2520 | public boolean pruneRemovedPrinter(PrinterId printerId) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2521 | final int holderCounts = mPrinterHolders.size(); |
| 2522 | for (int i = holderCounts - 1; i >= 0; i--) { |
| 2523 | PrinterHolder printerHolder = mPrinterHolders.get(i); |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2524 | |
| 2525 | if (printerHolder.printer.getId().equals(printerId) && printerHolder.removed) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2526 | mPrinterHolders.remove(i); |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2527 | return true; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2528 | } |
| 2529 | } |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2530 | |
| 2531 | return false; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | private void addPrinters(List<PrinterHolder> list, Collection<PrinterInfo> printers) { |
| 2535 | for (PrinterInfo printer : printers) { |
| 2536 | PrinterHolder printerHolder = new PrinterHolder(printer); |
| 2537 | list.add(printerHolder); |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | private PrinterInfo createFakePdfPrinter() { |
Philip P. Moltmann | 4959caf | 2016-01-21 14:30:56 -0800 | [diff] [blame] | 2542 | ArraySet<MediaSize> allMediaSizes = MediaSize.getAllPredefinedSizes(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2543 | MediaSize defaultMediaSize = MediaSizeUtils.getDefault(PrintActivity.this); |
| 2544 | |
| 2545 | PrinterId printerId = new PrinterId(getComponentName(), "PDF printer"); |
| 2546 | |
| 2547 | PrinterCapabilitiesInfo.Builder builder = |
| 2548 | new PrinterCapabilitiesInfo.Builder(printerId); |
| 2549 | |
Philip P. Moltmann | 4959caf | 2016-01-21 14:30:56 -0800 | [diff] [blame] | 2550 | final int mediaSizeCount = allMediaSizes.size(); |
| 2551 | for (int i = 0; i < mediaSizeCount; i++) { |
| 2552 | MediaSize mediaSize = allMediaSizes.valueAt(i); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2553 | builder.addMediaSize(mediaSize, mediaSize.equals(defaultMediaSize)); |
| 2554 | } |
| 2555 | |
| 2556 | builder.addResolution(new Resolution("PDF resolution", "PDF resolution", 300, 300), |
| 2557 | true); |
| 2558 | builder.setColorModes(PrintAttributes.COLOR_MODE_COLOR |
| 2559 | | PrintAttributes.COLOR_MODE_MONOCHROME, PrintAttributes.COLOR_MODE_COLOR); |
| 2560 | |
| 2561 | return new PrinterInfo.Builder(printerId, getString(R.string.save_as_pdf), |
| 2562 | PrinterInfo.STATUS_IDLE).setCapabilities(builder.build()).build(); |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | private final class PrintersObserver extends DataSetObserver { |
| 2567 | @Override |
| 2568 | public void onChanged() { |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2569 | PrinterInfo oldPrinterState = mCurrentPrinter; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2570 | if (oldPrinterState == null) { |
| 2571 | return; |
| 2572 | } |
| 2573 | |
| 2574 | PrinterHolder printerHolder = mDestinationSpinnerAdapter.getPrinterHolder( |
| 2575 | oldPrinterState.getId()); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2576 | PrinterInfo newPrinterState = printerHolder.printer; |
| 2577 | |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2578 | if (printerHolder.removed) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2579 | onPrinterUnavailable(newPrinterState); |
| 2580 | } |
| 2581 | |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2582 | if (mDestinationSpinner.getSelectedItem() != printerHolder) { |
| 2583 | mDestinationSpinner.setSelection( |
| 2584 | mDestinationSpinnerAdapter.getPrinterIndex(newPrinterState.getId())); |
| 2585 | } |
| 2586 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2587 | if (oldPrinterState.equals(newPrinterState)) { |
| 2588 | return; |
| 2589 | } |
| 2590 | |
| 2591 | PrinterCapabilitiesInfo oldCapab = oldPrinterState.getCapabilities(); |
| 2592 | PrinterCapabilitiesInfo newCapab = newPrinterState.getCapabilities(); |
| 2593 | |
Philip P. Moltmann | 1bb7f36 | 2016-02-26 14:21:20 -0800 | [diff] [blame] | 2594 | final boolean hadCabab = oldCapab != null; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2595 | final boolean hasCapab = newCapab != null; |
| 2596 | final boolean gotCapab = oldCapab == null && newCapab != null; |
| 2597 | final boolean lostCapab = oldCapab != null && newCapab == null; |
| 2598 | final boolean capabChanged = capabilitiesChanged(oldCapab, newCapab); |
| 2599 | |
| 2600 | final int oldStatus = oldPrinterState.getStatus(); |
| 2601 | final int newStatus = newPrinterState.getStatus(); |
| 2602 | |
| 2603 | final boolean isActive = newStatus != PrinterInfo.STATUS_UNAVAILABLE; |
| 2604 | final boolean becameActive = (oldStatus == PrinterInfo.STATUS_UNAVAILABLE |
| 2605 | && oldStatus != newStatus); |
| 2606 | final boolean becameInactive = (newStatus == PrinterInfo.STATUS_UNAVAILABLE |
| 2607 | && oldStatus != newStatus); |
| 2608 | |
| 2609 | mPrinterAvailabilityDetector.updatePrinter(newPrinterState); |
| 2610 | |
Philip P. Moltmann | c2ad226 | 2016-01-13 09:17:15 -0800 | [diff] [blame] | 2611 | mCurrentPrinter = newPrinterState; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2612 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2613 | final boolean updateNeeded = ((capabChanged && hasCapab && isActive) |
| 2614 | || (becameActive && hasCapab) || (isActive && gotCapab)); |
| 2615 | |
Philip P. Moltmann | 1bb7f36 | 2016-02-26 14:21:20 -0800 | [diff] [blame] | 2616 | if (capabChanged && hasCapab) { |
| 2617 | updatePrintAttributesFromCapabilities(newCapab); |
| 2618 | } |
| 2619 | |
| 2620 | if (updateNeeded) { |
| 2621 | updatePrintPreviewController(false); |
| 2622 | } |
| 2623 | |
| 2624 | if ((isActive && gotCapab) || (becameActive && hasCapab)) { |
| 2625 | onPrinterAvailable(newPrinterState); |
| 2626 | } else if ((becameInactive && hadCabab) || (isActive && lostCapab)) { |
| 2627 | onPrinterUnavailable(newPrinterState); |
| 2628 | } |
| 2629 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2630 | if (updateNeeded && canUpdateDocument()) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2631 | updateDocument(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2632 | } |
| 2633 | |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2634 | // Force a reload of the enabled print services to update mAdvancedPrintOptionsActivity |
| 2635 | // in onLoadFinished(); |
| 2636 | getLoaderManager().getLoader(LOADER_ID_ENABLED_PRINT_SERVICES).forceLoad(); |
| 2637 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2638 | updateOptionsUi(); |
Philip P. Moltmann | 17332cb | 2016-01-29 15:17:00 -0800 | [diff] [blame] | 2639 | updateSummary(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | private boolean capabilitiesChanged(PrinterCapabilitiesInfo oldCapabilities, |
| 2643 | PrinterCapabilitiesInfo newCapabilities) { |
| 2644 | if (oldCapabilities == null) { |
| 2645 | if (newCapabilities != null) { |
| 2646 | return true; |
| 2647 | } |
| 2648 | } else if (!oldCapabilities.equals(newCapabilities)) { |
| 2649 | return true; |
| 2650 | } |
| 2651 | return false; |
| 2652 | } |
| 2653 | } |
| 2654 | |
| 2655 | private final class MyOnItemSelectedListener implements AdapterView.OnItemSelectedListener { |
| 2656 | @Override |
| 2657 | public void onItemSelected(AdapterView<?> spinner, View view, int position, long id) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2658 | boolean clearRanges = false; |
| 2659 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2660 | if (spinner == mDestinationSpinner) { |
| 2661 | if (position == AdapterView.INVALID_POSITION) { |
| 2662 | return; |
| 2663 | } |
| 2664 | |
Philip P. Moltmann | 66c9659 | 2016-02-24 11:32:43 -0800 | [diff] [blame] | 2665 | if (id == DEST_ADAPTER_ITEM_ID_MORE) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2666 | startSelectPrinterActivity(); |
| 2667 | return; |
| 2668 | } |
| 2669 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2670 | PrinterHolder currentItem = (PrinterHolder) mDestinationSpinner.getSelectedItem(); |
| 2671 | PrinterInfo currentPrinter = (currentItem != null) ? currentItem.printer : null; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2672 | |
| 2673 | // Why on earth item selected is called if no selection changed. |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2674 | if (mCurrentPrinter == currentPrinter) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2675 | return; |
| 2676 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2677 | |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2678 | PrinterId oldId = null; |
| 2679 | if (mCurrentPrinter != null) { |
| 2680 | oldId = mCurrentPrinter.getId(); |
| 2681 | } |
| 2682 | |
Svet Ganov | 48fec5c | 2014-07-14 00:14:07 -0700 | [diff] [blame] | 2683 | mCurrentPrinter = currentPrinter; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2684 | |
Philip P. Moltmann | 63ce0b7 | 2016-03-08 11:16:56 -0800 | [diff] [blame] | 2685 | if (oldId != null) { |
| 2686 | boolean printerRemoved = mDestinationSpinnerAdapter.pruneRemovedPrinter(oldId); |
| 2687 | |
| 2688 | if (printerRemoved) { |
| 2689 | // Trigger PrinterObserver.onChanged to adjust selection. This will call |
| 2690 | // this function again. |
| 2691 | mDestinationSpinnerAdapter.notifyDataSetChanged(); |
| 2692 | return; |
| 2693 | } |
| 2694 | } |
| 2695 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2696 | PrinterHolder printerHolder = mDestinationSpinnerAdapter.getPrinterHolder( |
| 2697 | currentPrinter.getId()); |
| 2698 | if (!printerHolder.removed) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2699 | setState(STATE_CONFIGURING); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2700 | ensurePreviewUiShown(); |
| 2701 | } |
| 2702 | |
| 2703 | mPrintJob.setPrinterId(currentPrinter.getId()); |
| 2704 | mPrintJob.setPrinterName(currentPrinter.getName()); |
| 2705 | |
| 2706 | mPrinterRegistry.setTrackedPrinter(currentPrinter.getId()); |
| 2707 | |
| 2708 | PrinterCapabilitiesInfo capabilities = currentPrinter.getCapabilities(); |
| 2709 | if (capabilities != null) { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2710 | updatePrintAttributesFromCapabilities(capabilities); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2711 | } |
| 2712 | |
| 2713 | mPrinterAvailabilityDetector.updatePrinter(currentPrinter); |
Philip P. Moltmann | ba245f9 | 2016-03-07 13:44:59 -0800 | [diff] [blame] | 2714 | |
| 2715 | // Force a reload of the enabled print services to update |
| 2716 | // mAdvancedPrintOptionsActivity in onLoadFinished(); |
| 2717 | getLoaderManager().getLoader(LOADER_ID_ENABLED_PRINT_SERVICES).forceLoad(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2718 | } else if (spinner == mMediaSizeSpinner) { |
| 2719 | SpinnerItem<MediaSize> mediaItem = mMediaSizeSpinnerAdapter.getItem(position); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2720 | PrintAttributes attributes = mPrintJob.getAttributes(); |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2721 | |
| 2722 | MediaSize newMediaSize; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2723 | if (mOrientationSpinner.getSelectedItemPosition() == 0) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2724 | newMediaSize = mediaItem.value.asPortrait(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2725 | } else { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2726 | newMediaSize = mediaItem.value.asLandscape(); |
| 2727 | } |
| 2728 | |
| 2729 | if (newMediaSize != attributes.getMediaSize()) { |
| 2730 | clearRanges = true; |
| 2731 | attributes.setMediaSize(newMediaSize); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2732 | } |
| 2733 | } else if (spinner == mColorModeSpinner) { |
| 2734 | SpinnerItem<Integer> colorModeItem = mColorModeSpinnerAdapter.getItem(position); |
| 2735 | mPrintJob.getAttributes().setColorMode(colorModeItem.value); |
Svetoslav | 948c9a6 | 2015-02-02 19:47:04 -0800 | [diff] [blame] | 2736 | } else if (spinner == mDuplexModeSpinner) { |
| 2737 | SpinnerItem<Integer> duplexModeItem = mDuplexModeSpinnerAdapter.getItem(position); |
| 2738 | mPrintJob.getAttributes().setDuplexMode(duplexModeItem.value); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2739 | } else if (spinner == mOrientationSpinner) { |
| 2740 | SpinnerItem<Integer> orientationItem = mOrientationSpinnerAdapter.getItem(position); |
| 2741 | PrintAttributes attributes = mPrintJob.getAttributes(); |
Svetoslav | e3bbb3d | 2014-06-12 10:43:20 -0700 | [diff] [blame] | 2742 | if (mMediaSizeSpinner.getSelectedItem() != null) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2743 | boolean isPortrait = attributes.isPortrait(); |
| 2744 | |
| 2745 | if (isPortrait != (orientationItem.value == ORIENTATION_PORTRAIT)) { |
| 2746 | clearRanges = true; |
| 2747 | if (orientationItem.value == ORIENTATION_PORTRAIT) { |
| 2748 | attributes.copyFrom(attributes.asPortrait()); |
| 2749 | } else { |
| 2750 | attributes.copyFrom(attributes.asLandscape()); |
| 2751 | } |
Svetoslav | e3bbb3d | 2014-06-12 10:43:20 -0700 | [diff] [blame] | 2752 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2753 | } |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2754 | } else if (spinner == mRangeOptionsSpinner) { |
| 2755 | if (mRangeOptionsSpinner.getSelectedItemPosition() == 0) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2756 | clearRanges = true; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2757 | mPageRangeEditText.setText(""); |
| 2758 | } else if (TextUtils.isEmpty(mPageRangeEditText.getText())) { |
| 2759 | mPageRangeEditText.setError(""); |
| 2760 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2763 | if (clearRanges) { |
| 2764 | clearPageRanges(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | updateOptionsUi(); |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2768 | |
| 2769 | if (canUpdateDocument()) { |
| 2770 | updateDocument(false); |
| 2771 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2772 | } |
| 2773 | |
| 2774 | @Override |
| 2775 | public void onNothingSelected(AdapterView<?> parent) { |
| 2776 | /* do nothing*/ |
| 2777 | } |
| 2778 | } |
| 2779 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2780 | private final class SelectAllOnFocusListener implements OnFocusChangeListener { |
| 2781 | @Override |
| 2782 | public void onFocusChange(View view, boolean hasFocus) { |
| 2783 | EditText editText = (EditText) view; |
| 2784 | if (!TextUtils.isEmpty(editText.getText())) { |
| 2785 | editText.setSelection(editText.getText().length()); |
| 2786 | } |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2787 | |
| 2788 | if (view == mPageRangeEditText && !hasFocus) { |
Philip P. Moltmann | c309d71 | 2016-03-31 13:49:38 -0700 | [diff] [blame] | 2789 | updateSelectedPagesFromTextField(); |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2790 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2791 | } |
| 2792 | } |
| 2793 | |
| 2794 | private final class RangeTextWatcher implements TextWatcher { |
| 2795 | @Override |
| 2796 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 2797 | /* do nothing */ |
| 2798 | } |
| 2799 | |
| 2800 | @Override |
| 2801 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 2802 | /* do nothing */ |
| 2803 | } |
| 2804 | |
| 2805 | @Override |
| 2806 | public void afterTextChanged(Editable editable) { |
| 2807 | final boolean hadErrors = hasErrors(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2808 | String text = editable.toString(); |
| 2809 | |
| 2810 | if (TextUtils.isEmpty(text)) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2811 | if (mPageRangeEditText.getError() == null) { |
| 2812 | mPageRangeEditText.setError(""); |
| 2813 | updateOptionsUi(); |
| 2814 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2815 | return; |
| 2816 | } |
| 2817 | |
| 2818 | String escapedText = PATTERN_ESCAPE_SPECIAL_CHARS.matcher(text).replaceAll("////"); |
| 2819 | if (!PATTERN_PAGE_RANGE.matcher(escapedText).matches()) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2820 | if (mPageRangeEditText.getError() == null) { |
| 2821 | mPageRangeEditText.setError(""); |
| 2822 | updateOptionsUi(); |
| 2823 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2828 | final int pageCount = (info != null) ? getAdjustedPageCount(info) : 0; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2829 | |
| 2830 | // The range |
| 2831 | Matcher matcher = PATTERN_DIGITS.matcher(text); |
| 2832 | while (matcher.find()) { |
| 2833 | String numericString = text.substring(matcher.start(), matcher.end()).trim(); |
| 2834 | if (TextUtils.isEmpty(numericString)) { |
| 2835 | continue; |
| 2836 | } |
| 2837 | final int pageIndex = Integer.parseInt(numericString); |
| 2838 | if (pageIndex < 1 || pageIndex > pageCount) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2839 | if (mPageRangeEditText.getError() == null) { |
| 2840 | mPageRangeEditText.setError(""); |
| 2841 | updateOptionsUi(); |
| 2842 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2843 | return; |
| 2844 | } |
| 2845 | } |
| 2846 | |
| 2847 | // We intentionally do not catch the case of the from page being |
| 2848 | // greater than the to page. When computing the requested pages |
| 2849 | // we just swap them if necessary. |
| 2850 | |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2851 | if (mPageRangeEditText.getError() != null) { |
| 2852 | mPageRangeEditText.setError(null); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2853 | updateOptionsUi(); |
| 2854 | } |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2855 | |
| 2856 | if (hadErrors && canUpdateDocument()) { |
| 2857 | updateDocument(false); |
| 2858 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | |
| 2862 | private final class EditTextWatcher implements TextWatcher { |
| 2863 | @Override |
| 2864 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 2865 | /* do nothing */ |
| 2866 | } |
| 2867 | |
| 2868 | @Override |
| 2869 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 2870 | /* do nothing */ |
| 2871 | } |
| 2872 | |
| 2873 | @Override |
| 2874 | public void afterTextChanged(Editable editable) { |
| 2875 | final boolean hadErrors = hasErrors(); |
| 2876 | |
| 2877 | if (editable.length() == 0) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2878 | if (mCopiesEditText.getError() == null) { |
| 2879 | mCopiesEditText.setError(""); |
| 2880 | updateOptionsUi(); |
| 2881 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2882 | return; |
| 2883 | } |
| 2884 | |
| 2885 | int copies = 0; |
| 2886 | try { |
| 2887 | copies = Integer.parseInt(editable.toString()); |
| 2888 | } catch (NumberFormatException nfe) { |
| 2889 | /* ignore */ |
| 2890 | } |
| 2891 | |
| 2892 | if (copies < MIN_COPIES) { |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2893 | if (mCopiesEditText.getError() == null) { |
| 2894 | mCopiesEditText.setError(""); |
| 2895 | updateOptionsUi(); |
| 2896 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2897 | return; |
| 2898 | } |
| 2899 | |
| 2900 | mPrintJob.setCopies(copies); |
| 2901 | |
Philip P. Moltmann | 4ef83c46 | 2016-03-24 15:27:45 -0700 | [diff] [blame] | 2902 | if (mCopiesEditText.getError() != null) { |
| 2903 | mCopiesEditText.setError(null); |
| 2904 | updateOptionsUi(); |
| 2905 | } |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2906 | |
| 2907 | if (hadErrors && canUpdateDocument()) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2908 | updateDocument(false); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2909 | } |
| 2910 | } |
| 2911 | } |
| 2912 | |
| 2913 | private final class ProgressMessageController implements Runnable { |
| 2914 | private static final long PROGRESS_TIMEOUT_MILLIS = 1000; |
| 2915 | |
| 2916 | private final Handler mHandler; |
| 2917 | |
| 2918 | private boolean mPosted; |
| 2919 | |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2920 | /** State before run was executed */ |
| 2921 | private int mPreviousState = -1; |
| 2922 | |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2923 | public ProgressMessageController(Context context) { |
| 2924 | mHandler = new Handler(context.getMainLooper(), null, false); |
| 2925 | } |
| 2926 | |
| 2927 | public void post() { |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2928 | if (mState == STATE_UPDATE_SLOW) { |
| 2929 | setState(STATE_UPDATE_SLOW); |
| 2930 | ensureProgressUiShown(); |
| 2931 | updateOptionsUi(); |
| 2932 | |
| 2933 | return; |
| 2934 | } else if (mPosted) { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2935 | return; |
| 2936 | } |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2937 | mPreviousState = -1; |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2938 | mPosted = true; |
| 2939 | mHandler.postDelayed(this, PROGRESS_TIMEOUT_MILLIS); |
| 2940 | } |
| 2941 | |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2942 | private int getStateAfterCancel() { |
| 2943 | if (mPreviousState == -1) { |
| 2944 | return mState; |
| 2945 | } else { |
| 2946 | return mPreviousState; |
| 2947 | } |
| 2948 | } |
| 2949 | |
| 2950 | public int cancel() { |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2951 | if (!mPosted) { |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2952 | return getStateAfterCancel(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2953 | } |
| 2954 | mPosted = false; |
| 2955 | mHandler.removeCallbacks(this); |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2956 | |
| 2957 | return getStateAfterCancel(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2958 | } |
| 2959 | |
| 2960 | @Override |
| 2961 | public void run() { |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2962 | mPosted = false; |
Philip P. Moltmann | 7b92d3c | 2016-03-31 14:18:47 -0700 | [diff] [blame] | 2963 | mPreviousState = mState; |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2964 | setState(STATE_UPDATE_SLOW); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2965 | ensureProgressUiShown(); |
Svet Ganov | 525a66b | 2014-06-14 22:29:00 -0700 | [diff] [blame] | 2966 | updateOptionsUi(); |
Svetoslav | a798c0a | 2014-05-15 10:47:19 -0700 | [diff] [blame] | 2967 | } |
| 2968 | } |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2969 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2970 | private static final class DocumentTransformer implements ServiceConnection { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2971 | private static final String TEMP_FILE_PREFIX = "print_job"; |
| 2972 | private static final String TEMP_FILE_EXTENSION = ".pdf"; |
| 2973 | |
| 2974 | private final Context mContext; |
| 2975 | |
| 2976 | private final MutexFileProvider mFileProvider; |
| 2977 | |
| 2978 | private final PrintJobInfo mPrintJob; |
| 2979 | |
| 2980 | private final PageRange[] mPagesToShred; |
| 2981 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2982 | private final PrintAttributes mAttributesToApply; |
| 2983 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2984 | private final Runnable mCallback; |
| 2985 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2986 | public DocumentTransformer(Context context, PrintJobInfo printJob, |
| 2987 | MutexFileProvider fileProvider, PrintAttributes attributes, |
| 2988 | Runnable callback) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2989 | mContext = context; |
| 2990 | mPrintJob = printJob; |
| 2991 | mFileProvider = fileProvider; |
| 2992 | mCallback = callback; |
| 2993 | mPagesToShred = computePagesToShred(mPrintJob); |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2994 | mAttributesToApply = attributes; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2995 | } |
| 2996 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2997 | public void transform() { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 2998 | // If we have only the pages we want, done. |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 2999 | if (mPagesToShred.length <= 0 && mAttributesToApply == null) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3000 | mCallback.run(); |
| 3001 | return; |
| 3002 | } |
| 3003 | |
| 3004 | // Bind to the manipulation service and the work |
| 3005 | // will be performed upon connection to the service. |
| 3006 | Intent intent = new Intent(PdfManipulationService.ACTION_GET_EDITOR); |
| 3007 | intent.setClass(mContext, PdfManipulationService.class); |
| 3008 | mContext.bindService(intent, this, Context.BIND_AUTO_CREATE); |
| 3009 | } |
| 3010 | |
| 3011 | @Override |
| 3012 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 3013 | final IPdfEditor editor = IPdfEditor.Stub.asInterface(service); |
| 3014 | new AsyncTask<Void, Void, Void>() { |
| 3015 | @Override |
| 3016 | protected Void doInBackground(Void... params) { |
Svetoslav | fb3532ee | 2014-09-15 18:28:51 -0700 | [diff] [blame] | 3017 | // It's OK to access the data members as they are |
| 3018 | // final and this code is the last one to touch |
| 3019 | // them as shredding is the very last step, so the |
| 3020 | // UI is not interactive at this point. |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 3021 | doTransform(editor); |
Svetoslav | fb3532ee | 2014-09-15 18:28:51 -0700 | [diff] [blame] | 3022 | updatePrintJob(); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3023 | return null; |
| 3024 | } |
Svetoslav | fb3532ee | 2014-09-15 18:28:51 -0700 | [diff] [blame] | 3025 | |
| 3026 | @Override |
| 3027 | protected void onPostExecute(Void aVoid) { |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 3028 | mContext.unbindService(DocumentTransformer.this); |
Svetoslav | fb3532ee | 2014-09-15 18:28:51 -0700 | [diff] [blame] | 3029 | mCallback.run(); |
| 3030 | } |
Svetoslav | b75632c | 2014-09-17 18:38:27 -0700 | [diff] [blame] | 3031 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | @Override |
| 3035 | public void onServiceDisconnected(ComponentName name) { |
| 3036 | /* do nothing */ |
| 3037 | } |
| 3038 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 3039 | private void doTransform(IPdfEditor editor) { |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3040 | File tempFile = null; |
| 3041 | ParcelFileDescriptor src = null; |
| 3042 | ParcelFileDescriptor dst = null; |
| 3043 | InputStream in = null; |
| 3044 | OutputStream out = null; |
| 3045 | try { |
| 3046 | File jobFile = mFileProvider.acquireFile(null); |
| 3047 | src = ParcelFileDescriptor.open(jobFile, ParcelFileDescriptor.MODE_READ_WRITE); |
| 3048 | |
| 3049 | // Open the document. |
| 3050 | editor.openDocument(src); |
| 3051 | |
| 3052 | // We passed the fd over IPC, close this one. |
| 3053 | src.close(); |
| 3054 | |
| 3055 | // Drop the pages. |
| 3056 | editor.removePages(mPagesToShred); |
| 3057 | |
Svetoslav | bec22be | 2014-09-25 13:03:20 -0700 | [diff] [blame] | 3058 | // Apply print attributes if needed. |
| 3059 | if (mAttributesToApply != null) { |
| 3060 | editor.applyPrintAttributes(mAttributesToApply); |
| 3061 | } |
| 3062 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3063 | // Write the modified PDF to a temp file. |
| 3064 | tempFile = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_EXTENSION, |
| 3065 | mContext.getCacheDir()); |
| 3066 | dst = ParcelFileDescriptor.open(tempFile, ParcelFileDescriptor.MODE_READ_WRITE); |
| 3067 | editor.write(dst); |
| 3068 | dst.close(); |
| 3069 | |
| 3070 | // Close the document. |
| 3071 | editor.closeDocument(); |
| 3072 | |
| 3073 | // Copy the temp file over the print job file. |
| 3074 | jobFile.delete(); |
| 3075 | in = new FileInputStream(tempFile); |
| 3076 | out = new FileOutputStream(jobFile); |
| 3077 | Streams.copy(in, out); |
| 3078 | } catch (IOException|RemoteException e) { |
| 3079 | Log.e(LOG_TAG, "Error dropping pages", e); |
| 3080 | } finally { |
| 3081 | IoUtils.closeQuietly(src); |
| 3082 | IoUtils.closeQuietly(dst); |
| 3083 | IoUtils.closeQuietly(in); |
| 3084 | IoUtils.closeQuietly(out); |
| 3085 | if (tempFile != null) { |
| 3086 | tempFile.delete(); |
| 3087 | } |
Svetoslav | 5668348 | 2014-09-23 16:22:42 -0700 | [diff] [blame] | 3088 | mFileProvider.releaseFile(); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | private void updatePrintJob() { |
| 3093 | // Update the print job pages. |
| 3094 | final int newPageCount = PageRangeUtils.getNormalizedPageCount( |
| 3095 | mPrintJob.getPages(), 0); |
| 3096 | mPrintJob.setPages(new PageRange[]{PageRange.ALL_PAGES}); |
| 3097 | |
| 3098 | // Update the print job document info. |
| 3099 | PrintDocumentInfo oldDocInfo = mPrintJob.getDocumentInfo(); |
| 3100 | PrintDocumentInfo newDocInfo = new PrintDocumentInfo |
| 3101 | .Builder(oldDocInfo.getName()) |
| 3102 | .setContentType(oldDocInfo.getContentType()) |
| 3103 | .setPageCount(newPageCount) |
| 3104 | .build(); |
| 3105 | mPrintJob.setDocumentInfo(newDocInfo); |
| 3106 | } |
| 3107 | |
| 3108 | private static PageRange[] computePagesToShred(PrintJobInfo printJob) { |
| 3109 | List<PageRange> rangesToShred = new ArrayList<>(); |
| 3110 | PageRange previousRange = null; |
| 3111 | |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3112 | PageRange[] printedPages = printJob.getPages(); |
| 3113 | final int rangeCount = printedPages.length; |
| 3114 | for (int i = 0; i < rangeCount; i++) { |
Philip P. Moltmann | 9e4bbc6 | 2016-03-31 15:39:16 -0700 | [diff] [blame] | 3115 | PageRange range = printedPages[i]; |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3116 | |
| 3117 | if (previousRange == null) { |
| 3118 | final int startPageIdx = 0; |
| 3119 | final int endPageIdx = range.getStart() - 1; |
| 3120 | if (startPageIdx <= endPageIdx) { |
| 3121 | PageRange removedRange = new PageRange(startPageIdx, endPageIdx); |
| 3122 | rangesToShred.add(removedRange); |
| 3123 | } |
| 3124 | } else { |
| 3125 | final int startPageIdx = previousRange.getEnd() + 1; |
| 3126 | final int endPageIdx = range.getStart() - 1; |
| 3127 | if (startPageIdx <= endPageIdx) { |
| 3128 | PageRange removedRange = new PageRange(startPageIdx, endPageIdx); |
| 3129 | rangesToShred.add(removedRange); |
| 3130 | } |
| 3131 | } |
| 3132 | |
| 3133 | if (i == rangeCount - 1) { |
Philip P. Moltmann | 9e4bbc6 | 2016-03-31 15:39:16 -0700 | [diff] [blame] | 3134 | if (range.getEnd() != Integer.MAX_VALUE) { |
| 3135 | rangesToShred.add(new PageRange(range.getEnd() + 1, Integer.MAX_VALUE)); |
Svetoslav | 62ce332 | 2014-09-04 21:17:17 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | previousRange = range; |
| 3140 | } |
| 3141 | |
| 3142 | PageRange[] result = new PageRange[rangesToShred.size()]; |
| 3143 | rangesToShred.toArray(result); |
| 3144 | return result; |
| 3145 | } |
| 3146 | } |
Svet Ganov | 561b893 | 2014-09-02 21:51:45 +0000 | [diff] [blame] | 3147 | } |