Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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; |
| 18 | |
| 19 | import android.app.Activity; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 20 | import android.app.Dialog; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 21 | import android.app.LoaderManager; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 22 | import android.content.ActivityNotFoundException; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.Loader; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 28 | import android.content.pm.PackageInfo; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager.NameNotFoundException; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
| 31 | import android.content.pm.ServiceInfo; |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 32 | import android.database.DataSetObserver; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 33 | import android.graphics.Rect; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 34 | import android.graphics.drawable.Drawable; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
| 36 | import android.os.AsyncTask; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 37 | import android.os.Bundle; |
| 38 | import android.os.Handler; |
| 39 | import android.os.IBinder; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 40 | import android.os.IBinder.DeathRecipient; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 41 | import android.os.Looper; |
| 42 | import android.os.Message; |
| 43 | import android.os.RemoteException; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 44 | import android.print.ILayoutResultCallback; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 45 | import android.print.IPrintDocumentAdapter; |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 46 | import android.print.IPrintDocumentAdapterObserver; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 47 | import android.print.IWriteResultCallback; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 48 | import android.print.PageRange; |
| 49 | import android.print.PrintAttributes; |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 50 | import android.print.PrintAttributes.Margins; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 51 | import android.print.PrintAttributes.MediaSize; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 52 | import android.print.PrintAttributes.Resolution; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 53 | import android.print.PrintDocumentAdapter; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 54 | import android.print.PrintDocumentInfo; |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 55 | import android.print.PrintJobId; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 56 | import android.print.PrintJobInfo; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 57 | import android.print.PrintManager; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 58 | import android.print.PrinterCapabilitiesInfo; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 59 | import android.print.PrinterId; |
| 60 | import android.print.PrinterInfo; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 61 | import android.printservice.PrintService; |
| 62 | import android.printservice.PrintServiceInfo; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 63 | import android.provider.DocumentsContract; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 64 | import android.text.Editable; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 65 | import android.text.TextUtils; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 66 | import android.text.TextUtils.SimpleStringSplitter; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 67 | import android.text.TextWatcher; |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 68 | import android.util.ArrayMap; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 69 | import android.util.AttributeSet; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 70 | import android.util.Log; |
Svetoslav | 3aa2e2b | 2013-10-10 16:46:06 -0700 | [diff] [blame] | 71 | import android.view.Gravity; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 72 | import android.view.KeyEvent; |
| 73 | import android.view.MotionEvent; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 74 | import android.view.View; |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 75 | import android.view.View.MeasureSpec; |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 76 | import android.view.View.OnAttachStateChangeListener; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 77 | import android.view.View.OnClickListener; |
Svetoslav | 89ed9fc | 2013-10-11 17:00:45 -0700 | [diff] [blame] | 78 | import android.view.View.OnFocusChangeListener; |
Svetoslav Ganov | 695c7fa | 2013-08-07 19:29:42 -0700 | [diff] [blame] | 79 | import android.view.ViewConfiguration; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 80 | import android.view.ViewGroup; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 81 | import android.view.ViewGroup.LayoutParams; |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 82 | import android.view.ViewPropertyAnimator; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 83 | import android.view.inputmethod.InputMethodManager; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 84 | import android.widget.AdapterView; |
| 85 | import android.widget.AdapterView.OnItemSelectedListener; |
| 86 | import android.widget.ArrayAdapter; |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 87 | import android.widget.BaseAdapter; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 88 | import android.widget.Button; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 89 | import android.widget.EditText; |
Svetoslav | 3aa2e2b | 2013-10-10 16:46:06 -0700 | [diff] [blame] | 90 | import android.widget.FrameLayout; |
Svetoslav | c335eb4 | 2013-09-26 15:55:47 -0700 | [diff] [blame] | 91 | import android.widget.ImageView; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 92 | import android.widget.Spinner; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 93 | import android.widget.TextView; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 94 | |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 95 | import com.android.printspooler.MediaSizeUtils.MediaSizeComparator; |
| 96 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 97 | import libcore.io.IoUtils; |
| 98 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 99 | import java.io.File; |
| 100 | import java.io.FileInputStream; |
| 101 | import java.io.FileNotFoundException; |
| 102 | import java.io.IOException; |
| 103 | import java.io.InputStream; |
| 104 | import java.io.OutputStream; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 105 | import java.lang.ref.WeakReference; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 106 | import java.util.ArrayList; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 107 | import java.util.Arrays; |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 108 | import java.util.Collections; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 109 | import java.util.Comparator; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 110 | import java.util.List; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 111 | import java.util.concurrent.atomic.AtomicInteger; |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 112 | import java.util.regex.Matcher; |
| 113 | import java.util.regex.Pattern; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 114 | |
| 115 | /** |
| 116 | * Activity for configuring a print job. |
| 117 | */ |
| 118 | public class PrintJobConfigActivity extends Activity { |
| 119 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 120 | private static final String LOG_TAG = "PrintJobConfigActivity"; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 121 | |
Svetoslav | 5559c36 | 2013-09-20 11:58:55 -0700 | [diff] [blame] | 122 | private static final boolean DEBUG = false; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 123 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 124 | public static final String INTENT_EXTRA_PRINTER_ID = "INTENT_EXTRA_PRINTER_ID"; |
| 125 | |
| 126 | private static final int LOADER_ID_PRINTERS_LOADER = 1; |
| 127 | |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 128 | private static final int ORIENTATION_PORTRAIT = 0; |
| 129 | private static final int ORIENTATION_LANDSCAPE = 1; |
| 130 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 131 | private static final int DEST_ADAPTER_MAX_ITEM_COUNT = 9; |
| 132 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 133 | private static final int DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF = Integer.MAX_VALUE; |
| 134 | private static final int DEST_ADAPTER_ITEM_ID_ALL_PRINTERS = Integer.MAX_VALUE - 1; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 135 | |
| 136 | private static final int ACTIVITY_REQUEST_CREATE_FILE = 1; |
| 137 | private static final int ACTIVITY_REQUEST_SELECT_PRINTER = 2; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 138 | private static final int ACTIVITY_POPULATE_ADVANCED_PRINT_OPTIONS = 3; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 139 | |
| 140 | private static final int CONTROLLER_STATE_FINISHED = 1; |
| 141 | private static final int CONTROLLER_STATE_FAILED = 2; |
| 142 | private static final int CONTROLLER_STATE_CANCELLED = 3; |
| 143 | private static final int CONTROLLER_STATE_INITIALIZED = 4; |
| 144 | private static final int CONTROLLER_STATE_STARTED = 5; |
| 145 | private static final int CONTROLLER_STATE_LAYOUT_STARTED = 6; |
| 146 | private static final int CONTROLLER_STATE_LAYOUT_COMPLETED = 7; |
| 147 | private static final int CONTROLLER_STATE_WRITE_STARTED = 8; |
| 148 | private static final int CONTROLLER_STATE_WRITE_COMPLETED = 9; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 149 | |
| 150 | private static final int EDITOR_STATE_INITIALIZED = 1; |
| 151 | private static final int EDITOR_STATE_CONFIRMED_PRINT = 2; |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 152 | private static final int EDITOR_STATE_CANCELLED = 3; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 153 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 154 | private static final int MIN_COPIES = 1; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 155 | private static final String MIN_COPIES_STRING = String.valueOf(MIN_COPIES); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 156 | |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 157 | private static final Pattern PATTERN_DIGITS = Pattern.compile("[\\d]+"); |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 158 | |
| 159 | private static final Pattern PATTERN_ESCAPE_SPECIAL_CHARS = Pattern.compile( |
| 160 | "(?=[]\\[+&|!(){}^\"~*?:\\\\])"); |
| 161 | |
| 162 | private static final Pattern PATTERN_PAGE_RANGE = Pattern.compile( |
Svetoslav Ganov | ab051ba | 2013-09-26 00:34:54 -0700 | [diff] [blame] | 163 | "[\\s]*[0-9]*[\\s]*[\\-]?[\\s]*[0-9]*[\\s]*?(([,])" |
| 164 | + "[\\s]*[0-9]*[\\s]*[\\-]?[\\s]*[0-9]*[\\s]*|[\\s]*)+"); |
Svetoslav | 597945f | 2013-07-17 18:37:36 -0700 | [diff] [blame] | 165 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 166 | public static final PageRange[] ALL_PAGES_ARRAY = new PageRange[] {PageRange.ALL_PAGES}; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 167 | |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 168 | private final PrintAttributes mOldPrintAttributes = new PrintAttributes.Builder().build(); |
| 169 | private final PrintAttributes mCurrPrintAttributes = new PrintAttributes.Builder().build(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 170 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 171 | private final DeathRecipient mDeathRecipient = new DeathRecipient() { |
| 172 | @Override |
| 173 | public void binderDied() { |
| 174 | finish(); |
| 175 | } |
| 176 | }; |
| 177 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 178 | private Editor mEditor; |
| 179 | private Document mDocument; |
| 180 | private PrintController mController; |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 181 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 182 | private PrintJobId mPrintJobId; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 183 | |
| 184 | private IBinder mIPrintDocumentAdapter; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 185 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 186 | private Dialog mGeneratingPrintJobDialog; |
| 187 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 188 | private PrintSpoolerProvider mSpoolerProvider; |
| 189 | |
| 190 | private String mCallingPackageName; |
| 191 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 192 | @Override |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 193 | protected void onCreate(Bundle bundle) { |
| 194 | super.onCreate(bundle); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 195 | |
Svetoslav Ganov | 56ddf1f | 2013-10-05 19:55:49 -0700 | [diff] [blame] | 196 | setTitle(R.string.print_dialog); |
| 197 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 198 | Bundle extras = getIntent().getExtras(); |
| 199 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 200 | PrintJobInfo printJob = extras.getParcelable(PrintManager.EXTRA_PRINT_JOB); |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 201 | if (printJob == null) { |
| 202 | throw new IllegalArgumentException("printJob cannot be null"); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 205 | mPrintJobId = printJob.getId(); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 206 | mIPrintDocumentAdapter = extras.getBinder(PrintManager.EXTRA_PRINT_DOCUMENT_ADAPTER); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 207 | if (mIPrintDocumentAdapter == null) { |
| 208 | throw new IllegalArgumentException("PrintDocumentAdapter cannot be null"); |
| 209 | } |
| 210 | |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 211 | try { |
| 212 | IPrintDocumentAdapter.Stub.asInterface(mIPrintDocumentAdapter) |
| 213 | .setObserver(new PrintDocumentAdapterObserver(this)); |
| 214 | } catch (RemoteException re) { |
| 215 | finish(); |
| 216 | return; |
| 217 | } |
| 218 | |
Svetoslav | 2fbd2a7 | 2013-09-16 17:53:51 -0700 | [diff] [blame] | 219 | PrintAttributes attributes = printJob.getAttributes(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 220 | if (attributes != null) { |
| 221 | mCurrPrintAttributes.copyFrom(attributes); |
| 222 | } |
| 223 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 224 | mCallingPackageName = extras.getString(DocumentsContract.EXTRA_PACKAGE_NAME); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 225 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 226 | setContentView(R.layout.print_job_config_activity_container); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 227 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 228 | try { |
| 229 | mIPrintDocumentAdapter.linkToDeath(mDeathRecipient, 0); |
| 230 | } catch (RemoteException re) { |
| 231 | finish(); |
| 232 | return; |
| 233 | } |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 234 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 235 | mDocument = new Document(); |
| 236 | mEditor = new Editor(); |
| 237 | |
| 238 | mSpoolerProvider = new PrintSpoolerProvider(this, |
| 239 | new Runnable() { |
| 240 | @Override |
| 241 | public void run() { |
| 242 | // We got the spooler so unleash the UI. |
| 243 | mController = new PrintController(new RemotePrintDocumentAdapter( |
| 244 | IPrintDocumentAdapter.Stub.asInterface(mIPrintDocumentAdapter), |
| 245 | mSpoolerProvider.getSpooler().generateFileForPrintJob(mPrintJobId))); |
| 246 | mController.initialize(); |
| 247 | |
| 248 | mEditor.initialize(); |
| 249 | mEditor.postCreate(); |
| 250 | } |
| 251 | }); |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | @Override |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 255 | public void onResume() { |
| 256 | super.onResume(); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 257 | if (mSpoolerProvider.getSpooler() != null) { |
| 258 | mEditor.refreshCurrentPrinter(); |
| 259 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | @Override |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 263 | public void onPause() { |
| 264 | if (isFinishing()) { |
| 265 | if (mController != null && mController.hasStarted()) { |
| 266 | mController.finish(); |
| 267 | } |
| 268 | if (mEditor != null && mEditor.isPrintConfirmed() |
| 269 | && mController != null && mController.isFinished()) { |
| 270 | mSpoolerProvider.getSpooler().setPrintJobState(mPrintJobId, |
| 271 | PrintJobInfo.STATE_QUEUED, null); |
| 272 | } else { |
| 273 | mSpoolerProvider.getSpooler().setPrintJobState(mPrintJobId, |
| 274 | PrintJobInfo.STATE_CANCELED, null); |
| 275 | } |
| 276 | if (mGeneratingPrintJobDialog != null) { |
| 277 | mGeneratingPrintJobDialog.dismiss(); |
| 278 | mGeneratingPrintJobDialog = null; |
| 279 | } |
| 280 | mIPrintDocumentAdapter.unlinkToDeath(mDeathRecipient, 0); |
| 281 | mSpoolerProvider.destroy(); |
| 282 | } |
| 283 | super.onPause(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 286 | public boolean onTouchEvent(MotionEvent event) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 287 | if (mController != null && mEditor != null && |
| 288 | !mEditor.isPrintConfirmed() && mEditor.shouldCloseOnTouch(event)) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 289 | if (!mController.isWorking()) { |
| 290 | PrintJobConfigActivity.this.finish(); |
| 291 | } |
| 292 | mEditor.cancel(); |
| 293 | return true; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 294 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 295 | return super.onTouchEvent(event); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 298 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 299 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 300 | event.startTracking(); |
| 301 | } |
| 302 | return super.onKeyDown(keyCode, event); |
| 303 | } |
| 304 | |
| 305 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 306 | if (mController != null && mEditor != null) { |
| 307 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 308 | if (mEditor.isShwoingGeneratingPrintJobUi()) { |
| 309 | return true; |
| 310 | } |
| 311 | if (event.isTracking() && !event.isCanceled()) { |
| 312 | if (!mController.isWorking()) { |
| 313 | PrintJobConfigActivity.this.finish(); |
| 314 | } |
| 315 | } |
| 316 | mEditor.cancel(); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 317 | return true; |
| 318 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 319 | } |
| 320 | return super.onKeyUp(keyCode, event); |
| 321 | } |
| 322 | |
| 323 | private boolean printAttributesChanged() { |
| 324 | return !mOldPrintAttributes.equals(mCurrPrintAttributes); |
| 325 | } |
| 326 | |
| 327 | private class PrintController { |
| 328 | private final AtomicInteger mRequestCounter = new AtomicInteger(); |
| 329 | |
| 330 | private final RemotePrintDocumentAdapter mRemotePrintAdapter; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 331 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 332 | private final Bundle mMetadata; |
| 333 | |
| 334 | private final ControllerHandler mHandler; |
| 335 | |
| 336 | private final LayoutResultCallback mLayoutResultCallback; |
| 337 | |
| 338 | private final WriteResultCallback mWriteResultCallback; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 339 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 340 | private int mControllerState = CONTROLLER_STATE_INITIALIZED; |
| 341 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 342 | private boolean mHasStarted; |
| 343 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 344 | private PageRange[] mRequestedPages; |
| 345 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 346 | public PrintController(RemotePrintDocumentAdapter adapter) { |
| 347 | mRemotePrintAdapter = adapter; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 348 | mMetadata = new Bundle(); |
| 349 | mHandler = new ControllerHandler(getMainLooper()); |
| 350 | mLayoutResultCallback = new LayoutResultCallback(mHandler); |
| 351 | mWriteResultCallback = new WriteResultCallback(mHandler); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 354 | public void initialize() { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 355 | mHasStarted = false; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 356 | mControllerState = CONTROLLER_STATE_INITIALIZED; |
| 357 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 358 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 359 | public void cancel() { |
Svetoslav | d270cb9 | 2013-10-31 14:27:16 -0700 | [diff] [blame] | 360 | if (isWorking()) { |
| 361 | mRemotePrintAdapter.cancel(); |
| 362 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 363 | mControllerState = CONTROLLER_STATE_CANCELLED; |
| 364 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 365 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 366 | public boolean isCancelled() { |
| 367 | return (mControllerState == CONTROLLER_STATE_CANCELLED); |
| 368 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 369 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 370 | public boolean isFinished() { |
| 371 | return (mControllerState == CONTROLLER_STATE_FINISHED); |
| 372 | } |
| 373 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 374 | public boolean hasStarted() { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 375 | return mHasStarted; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | public boolean hasPerformedLayout() { |
| 379 | return mControllerState >= CONTROLLER_STATE_LAYOUT_COMPLETED; |
| 380 | } |
| 381 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 382 | public boolean isPerformingLayout() { |
| 383 | return mControllerState == CONTROLLER_STATE_LAYOUT_STARTED; |
| 384 | } |
| 385 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 386 | public boolean isWorking() { |
| 387 | return mControllerState == CONTROLLER_STATE_LAYOUT_STARTED |
| 388 | || mControllerState == CONTROLLER_STATE_WRITE_STARTED; |
| 389 | } |
| 390 | |
| 391 | public void start() { |
| 392 | mControllerState = CONTROLLER_STATE_STARTED; |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 393 | mHasStarted = true; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 394 | mRemotePrintAdapter.start(); |
| 395 | } |
| 396 | |
| 397 | public void update() { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 398 | if (!mController.hasStarted()) { |
| 399 | mController.start(); |
| 400 | } |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 401 | |
| 402 | // If the print attributes are the same and we are performing |
| 403 | // a layout, then we have to wait for it to completed which will |
| 404 | // trigger writing of the necessary pages. |
| 405 | final boolean printAttributesChanged = printAttributesChanged(); |
| 406 | if (!printAttributesChanged && isPerformingLayout()) { |
| 407 | return; |
| 408 | } |
| 409 | |
Svetoslav | 5559c36 | 2013-09-20 11:58:55 -0700 | [diff] [blame] | 410 | // If print is confirmed we always do a layout since the previous |
| 411 | // ones were for preview and this one is for printing. |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 412 | if (!printAttributesChanged && !mEditor.isPrintConfirmed()) { |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 413 | if (mDocument.info == null) { |
| 414 | // We are waiting for the result of a layout, so do nothing. |
| 415 | return; |
| 416 | } |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 417 | // If the attributes didn't change and we have done a layout, then |
| 418 | // we do not do a layout but may have to ask the app to write some |
| 419 | // pages. Hence, pretend layout completed and nothing changed, so |
| 420 | // we handle writing as usual. |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 421 | handleOnLayoutFinished(mDocument.info, false, mRequestCounter.get()); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 422 | } else { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 423 | mSpoolerProvider.getSpooler().setPrintJobAttributesNoPersistence( |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 424 | mPrintJobId, mCurrPrintAttributes); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 425 | |
Svetoslav | 6811f4e | 2013-09-18 15:58:28 -0700 | [diff] [blame] | 426 | mMetadata.putBoolean(PrintDocumentAdapter.EXTRA_PRINT_PREVIEW, |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 427 | !mEditor.isPrintConfirmed()); |
| 428 | |
| 429 | mControllerState = CONTROLLER_STATE_LAYOUT_STARTED; |
| 430 | |
| 431 | mRemotePrintAdapter.layout(mOldPrintAttributes, mCurrPrintAttributes, |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 432 | mLayoutResultCallback, mMetadata, mRequestCounter.incrementAndGet()); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 433 | |
| 434 | mOldPrintAttributes.copyFrom(mCurrPrintAttributes); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | public void finish() { |
| 439 | mControllerState = CONTROLLER_STATE_FINISHED; |
| 440 | mRemotePrintAdapter.finish(); |
| 441 | } |
| 442 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 443 | private void handleOnLayoutFinished(PrintDocumentInfo info, |
| 444 | boolean layoutChanged, int sequence) { |
| 445 | if (mRequestCounter.get() != sequence) { |
| 446 | return; |
| 447 | } |
| 448 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 449 | if (isCancelled()) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 450 | mEditor.updateUi(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 451 | if (mEditor.isDone()) { |
| 452 | PrintJobConfigActivity.this.finish(); |
| 453 | } |
| 454 | return; |
| 455 | } |
| 456 | |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 457 | final int oldControllerState = mControllerState; |
| 458 | |
| 459 | if (mControllerState == CONTROLLER_STATE_LAYOUT_STARTED) { |
| 460 | mControllerState = CONTROLLER_STATE_LAYOUT_COMPLETED; |
| 461 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 462 | |
Svetoslav | 5559c36 | 2013-09-20 11:58:55 -0700 | [diff] [blame] | 463 | // For layout purposes we care only whether the type or the page |
| 464 | // count changed. We still do not have the size since we did not |
| 465 | // call write. We use "layoutChanged" set by the application to |
| 466 | // know whether something else changed about the document. |
| 467 | final boolean infoChanged = !equalsIgnoreSize(info, mDocument.info); |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 468 | // If the info changed, we update the document and the print job. |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 469 | if (infoChanged) { |
| 470 | mDocument.info = info; |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 471 | // Set the info. |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 472 | mSpoolerProvider.getSpooler().setPrintJobPrintDocumentInfoNoPersistence( |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 473 | mPrintJobId, info); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // If the document info or the layout changed, then |
| 477 | // drop the pages since we have to fetch them again. |
| 478 | if (infoChanged || layoutChanged) { |
| 479 | mDocument.pages = null; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 480 | mSpoolerProvider.getSpooler().setPrintJobPagesNoPersistence( |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 481 | mPrintJobId, null); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 484 | PageRange[] oldRequestedPages = mRequestedPages; |
| 485 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 486 | // No pages means that the user selected an invalid range while we |
| 487 | // were doing a layout or the layout returned a document info for |
| 488 | // which the selected range is invalid. In such a case we do not |
| 489 | // write anything and wait for the user to fix the range which will |
| 490 | // trigger an update. |
| 491 | mRequestedPages = mEditor.getRequestedPages(); |
Svetoslav | c348402 | 2013-09-16 19:03:39 -0700 | [diff] [blame] | 492 | if (mRequestedPages == null || mRequestedPages.length == 0) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 493 | mEditor.updateUi(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 494 | if (mEditor.isDone()) { |
| 495 | PrintJobConfigActivity.this.finish(); |
| 496 | } |
| 497 | return; |
Svetoslav | c348402 | 2013-09-16 19:03:39 -0700 | [diff] [blame] | 498 | } else { |
| 499 | // If print is not confirmed we just ask for the first of the |
| 500 | // selected pages to emulate a behavior that shows preview |
| 501 | // increasing the chances that apps will implement the APIs |
| 502 | // correctly. |
| 503 | if (!mEditor.isPrintConfirmed()) { |
| 504 | if (ALL_PAGES_ARRAY.equals(mRequestedPages)) { |
| 505 | mRequestedPages = new PageRange[] {new PageRange(0, 0)}; |
| 506 | } else { |
| 507 | final int firstPage = mRequestedPages[0].getStart(); |
| 508 | mRequestedPages = new PageRange[] {new PageRange(firstPage, firstPage)}; |
| 509 | } |
| 510 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 511 | } |
| 512 | |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 513 | // If the info and the layout did not change... |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 514 | if (!infoChanged && !layoutChanged |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 515 | // and we have the requested pages ... |
| 516 | && (PageRangeUtils.contains(mDocument.pages, mRequestedPages)) |
| 517 | // ...or the requested pages are being written... |
| 518 | || (oldControllerState == CONTROLLER_STATE_WRITE_STARTED |
| 519 | && oldRequestedPages != null |
| 520 | && PageRangeUtils.contains(oldRequestedPages, mRequestedPages))) { |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 521 | // Nothing interesting changed and we have all requested pages. |
| 522 | // Then update the print jobs's pages as we will not do a write |
| 523 | // and we usually update the pages in the write complete callback. |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 524 | if (mDocument.pages != null) { |
| 525 | // Update the print job's pages given we have them. |
| 526 | updatePrintJobPages(mDocument.pages, mRequestedPages); |
| 527 | } |
Svetoslav | ebec468 | 2013-10-08 17:17:08 -0700 | [diff] [blame] | 528 | mEditor.updateUi(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 529 | if (mEditor.isDone()) { |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 530 | requestCreatePdfFileOrFinish(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 531 | } |
| 532 | return; |
| 533 | } |
| 534 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 535 | mEditor.updateUi(); |
| 536 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 537 | // Request a write of the pages of interest. |
| 538 | mControllerState = CONTROLLER_STATE_WRITE_STARTED; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 539 | mRemotePrintAdapter.write(mRequestedPages, mWriteResultCallback, |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 540 | mRequestCounter.incrementAndGet()); |
| 541 | } |
| 542 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 543 | private void handleOnLayoutFailed(final CharSequence error, int sequence) { |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 544 | if (mRequestCounter.get() != sequence) { |
| 545 | return; |
| 546 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 547 | mControllerState = CONTROLLER_STATE_FAILED; |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 548 | mEditor.showUi(Editor.UI_ERROR, new Runnable() { |
| 549 | @Override |
| 550 | public void run() { |
| 551 | if (!TextUtils.isEmpty(error)) { |
| 552 | TextView messageView = (TextView) findViewById(R.id.message); |
| 553 | messageView.setText(error); |
| 554 | } |
| 555 | } |
| 556 | }); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 559 | private void handleOnWriteFinished(PageRange[] pages, int sequence) { |
| 560 | if (mRequestCounter.get() != sequence) { |
| 561 | return; |
| 562 | } |
| 563 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 564 | if (isCancelled()) { |
| 565 | if (mEditor.isDone()) { |
| 566 | PrintJobConfigActivity.this.finish(); |
| 567 | } |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | mControllerState = CONTROLLER_STATE_WRITE_COMPLETED; |
| 572 | |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 573 | // Update the document size. |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 574 | File file = mSpoolerProvider.getSpooler() |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 575 | .generateFileForPrintJob(mPrintJobId); |
| 576 | mDocument.info.setDataSize(file.length()); |
| 577 | |
Svetoslav | 5559c36 | 2013-09-20 11:58:55 -0700 | [diff] [blame] | 578 | // Update the print job with the updated info. |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 579 | mSpoolerProvider.getSpooler().setPrintJobPrintDocumentInfoNoPersistence( |
Svetoslav | 5559c36 | 2013-09-20 11:58:55 -0700 | [diff] [blame] | 580 | mPrintJobId, mDocument.info); |
| 581 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 582 | // Update which pages we have fetched. |
| 583 | mDocument.pages = PageRangeUtils.normalize(pages); |
| 584 | |
| 585 | if (DEBUG) { |
| 586 | Log.i(LOG_TAG, "Requested: " + Arrays.toString(mRequestedPages) |
| 587 | + " and got: " + Arrays.toString(mDocument.pages)); |
| 588 | } |
| 589 | |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 590 | updatePrintJobPages(mDocument.pages, mRequestedPages); |
| 591 | |
| 592 | if (mEditor.isDone()) { |
| 593 | requestCreatePdfFileOrFinish(); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | private void updatePrintJobPages(PageRange[] writtenPages, PageRange[] requestedPages) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 598 | // Adjust the print job pages based on what was requested and written. |
| 599 | // The cases are ordered in the most expected to the least expected. |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 600 | if (Arrays.equals(writtenPages, requestedPages)) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 601 | // We got a document with exactly the pages we wanted. Hence, |
| 602 | // the printer has to print all pages in the data. |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 603 | mSpoolerProvider.getSpooler().setPrintJobPagesNoPersistence(mPrintJobId, |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 604 | ALL_PAGES_ARRAY); |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 605 | } else if (Arrays.equals(writtenPages, ALL_PAGES_ARRAY)) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 606 | // We requested specific pages but got all of them. Hence, |
| 607 | // the printer has to print only the requested pages. |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 608 | mSpoolerProvider.getSpooler().setPrintJobPagesNoPersistence(mPrintJobId, |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 609 | requestedPages); |
| 610 | } else if (PageRangeUtils.contains(writtenPages, requestedPages)) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 611 | // We requested specific pages and got more but not all pages. |
| 612 | // Hence, we have to offset appropriately the printed pages to |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 613 | // be based off the start of the written ones instead of zero. |
| 614 | // The written pages are always non-null and not empty. |
| 615 | final int offset = -writtenPages[0].getStart(); |
| 616 | PageRange[] offsetPages = Arrays.copyOf(requestedPages, requestedPages.length); |
| 617 | PageRangeUtils.offset(offsetPages, offset); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 618 | mSpoolerProvider.getSpooler().setPrintJobPagesNoPersistence(mPrintJobId, |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 619 | offsetPages); |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 620 | } else if (Arrays.equals(requestedPages, ALL_PAGES_ARRAY) |
| 621 | && writtenPages.length == 1 && writtenPages[0].getStart() == 0 |
| 622 | && writtenPages[0].getEnd() == mDocument.info.getPageCount() - 1) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 623 | // We requested all pages via the special constant and got all |
| 624 | // of them as an explicit enumeration. Hence, the printer has |
| 625 | // to print only the requested pages. |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 626 | mSpoolerProvider.getSpooler().setPrintJobPagesNoPersistence(mPrintJobId, |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 627 | writtenPages); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 628 | } else { |
| 629 | // We did not get the pages we requested, then the application |
| 630 | // misbehaves, so we fail quickly. |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 631 | mControllerState = CONTROLLER_STATE_FAILED; |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 632 | Log.e(LOG_TAG, "Received invalid pages from the app: requested=" |
| 633 | + Arrays.toString(requestedPages) + " written=" |
| 634 | + Arrays.toString(writtenPages)); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 635 | mEditor.showUi(Editor.UI_ERROR, null); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 636 | } |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | private void requestCreatePdfFileOrFinish() { |
| 640 | if (mEditor.isPrintingToPdf()) { |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 641 | Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT); |
| 642 | intent.setType("application/pdf"); |
Svetoslav Ganov | 4d4c66d | 2013-10-24 18:04:39 -0700 | [diff] [blame] | 643 | intent.putExtra(Intent.EXTRA_TITLE, mDocument.info.getName()); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 644 | intent.putExtra(DocumentsContract.EXTRA_PACKAGE_NAME, mCallingPackageName); |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 645 | startActivityForResult(intent, ACTIVITY_REQUEST_CREATE_FILE); |
| 646 | } else { |
| 647 | PrintJobConfigActivity.this.finish(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 651 | private void handleOnWriteFailed(final CharSequence error, int sequence) { |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 652 | if (mRequestCounter.get() != sequence) { |
| 653 | return; |
| 654 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 655 | mControllerState = CONTROLLER_STATE_FAILED; |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 656 | mEditor.showUi(Editor.UI_ERROR, new Runnable() { |
| 657 | @Override |
| 658 | public void run() { |
| 659 | if (!TextUtils.isEmpty(error)) { |
| 660 | TextView messageView = (TextView) findViewById(R.id.message); |
| 661 | messageView.setText(error); |
| 662 | } |
| 663 | } |
| 664 | }); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 665 | } |
| 666 | |
Svetoslav | 5559c36 | 2013-09-20 11:58:55 -0700 | [diff] [blame] | 667 | private boolean equalsIgnoreSize(PrintDocumentInfo lhs, PrintDocumentInfo rhs) { |
| 668 | if (lhs == rhs) { |
| 669 | return true; |
| 670 | } |
| 671 | if (lhs == null) { |
| 672 | if (rhs != null) { |
| 673 | return false; |
| 674 | } |
| 675 | } else { |
| 676 | if (rhs == null) { |
| 677 | return false; |
| 678 | } |
| 679 | if (lhs.getContentType() != rhs.getContentType() |
| 680 | || lhs.getPageCount() != rhs.getPageCount()) { |
| 681 | return false; |
| 682 | } |
| 683 | } |
| 684 | return true; |
| 685 | } |
| 686 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 687 | private final class ControllerHandler extends Handler { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 688 | public static final int MSG_ON_LAYOUT_FINISHED = 1; |
| 689 | public static final int MSG_ON_LAYOUT_FAILED = 2; |
| 690 | public static final int MSG_ON_WRITE_FINISHED = 3; |
| 691 | public static final int MSG_ON_WRITE_FAILED = 4; |
| 692 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 693 | public ControllerHandler(Looper looper) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 694 | super(looper, null, false); |
| 695 | } |
| 696 | |
| 697 | @Override |
| 698 | public void handleMessage(Message message) { |
| 699 | switch (message.what) { |
| 700 | case MSG_ON_LAYOUT_FINISHED: { |
| 701 | PrintDocumentInfo info = (PrintDocumentInfo) message.obj; |
| 702 | final boolean changed = (message.arg1 == 1); |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 703 | final int sequence = message.arg2; |
| 704 | handleOnLayoutFinished(info, changed, sequence); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 705 | } break; |
| 706 | |
| 707 | case MSG_ON_LAYOUT_FAILED: { |
| 708 | CharSequence error = (CharSequence) message.obj; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 709 | final int sequence = message.arg1; |
| 710 | handleOnLayoutFailed(error, sequence); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 711 | } break; |
| 712 | |
| 713 | case MSG_ON_WRITE_FINISHED: { |
| 714 | PageRange[] pages = (PageRange[]) message.obj; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 715 | final int sequence = message.arg1; |
| 716 | handleOnWriteFinished(pages, sequence); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 717 | } break; |
| 718 | |
| 719 | case MSG_ON_WRITE_FAILED: { |
| 720 | CharSequence error = (CharSequence) message.obj; |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 721 | final int sequence = message.arg1; |
| 722 | handleOnWriteFailed(error, sequence); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 723 | } break; |
| 724 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 725 | } |
| 726 | } |
| 727 | } |
| 728 | |
Svetoslav Ganov | 14db965 | 2013-08-06 14:40:46 -0700 | [diff] [blame] | 729 | private static final class LayoutResultCallback extends ILayoutResultCallback.Stub { |
| 730 | private final WeakReference<PrintController.ControllerHandler> mWeakHandler; |
| 731 | |
| 732 | public LayoutResultCallback(PrintController.ControllerHandler handler) { |
| 733 | mWeakHandler = new WeakReference<PrintController.ControllerHandler>(handler); |
| 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public void onLayoutFinished(PrintDocumentInfo info, boolean changed, int sequence) { |
| 738 | Handler handler = mWeakHandler.get(); |
| 739 | if (handler != null) { |
| 740 | handler.obtainMessage(PrintController.ControllerHandler.MSG_ON_LAYOUT_FINISHED, |
| 741 | changed ? 1 : 0, sequence, info).sendToTarget(); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | @Override |
| 746 | public void onLayoutFailed(CharSequence error, int sequence) { |
| 747 | Handler handler = mWeakHandler.get(); |
| 748 | if (handler != null) { |
| 749 | handler.obtainMessage(PrintController.ControllerHandler.MSG_ON_LAYOUT_FAILED, |
| 750 | sequence, 0, error).sendToTarget(); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | private static final class WriteResultCallback extends IWriteResultCallback.Stub { |
| 756 | private final WeakReference<PrintController.ControllerHandler> mWeakHandler; |
| 757 | |
| 758 | public WriteResultCallback(PrintController.ControllerHandler handler) { |
| 759 | mWeakHandler = new WeakReference<PrintController.ControllerHandler>(handler); |
| 760 | } |
| 761 | |
| 762 | @Override |
| 763 | public void onWriteFinished(PageRange[] pages, int sequence) { |
| 764 | Handler handler = mWeakHandler.get(); |
| 765 | if (handler != null) { |
| 766 | handler.obtainMessage(PrintController.ControllerHandler.MSG_ON_WRITE_FINISHED, |
| 767 | sequence, 0, pages).sendToTarget(); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | @Override |
| 772 | public void onWriteFailed(CharSequence error, int sequence) { |
| 773 | Handler handler = mWeakHandler.get(); |
| 774 | if (handler != null) { |
| 775 | handler.obtainMessage(PrintController.ControllerHandler.MSG_ON_WRITE_FAILED, |
| 776 | sequence, 0, error).sendToTarget(); |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 781 | @Override |
| 782 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 783 | switch (requestCode) { |
| 784 | case ACTIVITY_REQUEST_CREATE_FILE: { |
| 785 | if (data != null) { |
| 786 | Uri uri = data.getData(); |
| 787 | writePrintJobDataAndFinish(uri); |
| 788 | } else { |
| 789 | mEditor.showUi(Editor.UI_EDITING_PRINT_JOB, |
| 790 | new Runnable() { |
| 791 | @Override |
| 792 | public void run() { |
| 793 | mEditor.initialize(); |
| 794 | mEditor.bindUi(); |
Svetoslav Ganov | 7ff610e | 2013-10-12 17:23:59 -0700 | [diff] [blame] | 795 | mEditor.reselectCurrentPrinter(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 796 | mEditor.updateUi(); |
| 797 | } |
| 798 | }); |
| 799 | } |
| 800 | } break; |
| 801 | |
| 802 | case ACTIVITY_REQUEST_SELECT_PRINTER: { |
| 803 | if (resultCode == RESULT_OK) { |
| 804 | PrinterId printerId = (PrinterId) data.getParcelableExtra( |
| 805 | INTENT_EXTRA_PRINTER_ID); |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 806 | if (printerId != null) { |
| 807 | mEditor.ensurePrinterSelected(printerId); |
| 808 | break; |
| 809 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 810 | } |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 811 | mEditor.ensureCurrentPrinterSelected(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 812 | } break; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 813 | |
| 814 | case ACTIVITY_POPULATE_ADVANCED_PRINT_OPTIONS: { |
| 815 | if (resultCode == RESULT_OK) { |
| 816 | PrintJobInfo printJobInfo = (PrintJobInfo) data.getParcelableExtra( |
| 817 | PrintService.EXTRA_PRINT_JOB_INFO); |
| 818 | if (printJobInfo != null) { |
| 819 | mEditor.updateFromAdvancedOptions(printJobInfo); |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | mEditor.cancel(); |
| 824 | PrintJobConfigActivity.this.finish(); |
| 825 | } break; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | private void writePrintJobDataAndFinish(final Uri uri) { |
| 830 | new AsyncTask<Void, Void, Void>() { |
| 831 | @Override |
| 832 | protected Void doInBackground(Void... params) { |
| 833 | InputStream in = null; |
| 834 | OutputStream out = null; |
| 835 | try { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 836 | PrintJobInfo printJob = mSpoolerProvider.getSpooler() |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 837 | .getPrintJobInfo(mPrintJobId, PrintManager.APP_ID_ANY); |
| 838 | if (printJob == null) { |
| 839 | return null; |
| 840 | } |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 841 | File file = mSpoolerProvider.getSpooler() |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 842 | .generateFileForPrintJob(mPrintJobId); |
| 843 | in = new FileInputStream(file); |
| 844 | out = getContentResolver().openOutputStream(uri); |
| 845 | final byte[] buffer = new byte[8192]; |
| 846 | while (true) { |
| 847 | final int readByteCount = in.read(buffer); |
| 848 | if (readByteCount < 0) { |
| 849 | break; |
| 850 | } |
| 851 | out.write(buffer, 0, readByteCount); |
| 852 | } |
| 853 | } catch (FileNotFoundException fnfe) { |
| 854 | Log.e(LOG_TAG, "Error writing print job data!", fnfe); |
| 855 | } catch (IOException ioe) { |
| 856 | Log.e(LOG_TAG, "Error writing print job data!", ioe); |
| 857 | } finally { |
| 858 | IoUtils.closeQuietly(in); |
| 859 | IoUtils.closeQuietly(out); |
| 860 | } |
| 861 | return null; |
| 862 | } |
| 863 | |
| 864 | @Override |
| 865 | public void onPostExecute(Void result) { |
| 866 | mEditor.cancel(); |
| 867 | PrintJobConfigActivity.this.finish(); |
| 868 | } |
| 869 | }.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, (Void[]) null); |
| 870 | } |
| 871 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 872 | private final class Editor { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 873 | private static final int UI_NONE = 0; |
| 874 | private static final int UI_EDITING_PRINT_JOB = 1; |
| 875 | private static final int UI_GENERATING_PRINT_JOB = 2; |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 876 | private static final int UI_ERROR = 3; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 877 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 878 | private EditText mCopiesEditText; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 879 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 880 | private TextView mRangeOptionsTitle; |
| 881 | private TextView mPageRangeTitle; |
| 882 | private EditText mPageRangeEditText; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 883 | |
| 884 | private Spinner mDestinationSpinner; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 885 | private DestinationAdapter mDestinationSpinnerAdapter; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 886 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 887 | private Spinner mMediaSizeSpinner; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 888 | private ArrayAdapter<SpinnerItem<MediaSize>> mMediaSizeSpinnerAdapter; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 889 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 890 | private Spinner mColorModeSpinner; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 891 | private ArrayAdapter<SpinnerItem<Integer>> mColorModeSpinnerAdapter; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 892 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 893 | private Spinner mOrientationSpinner; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 894 | private ArrayAdapter<SpinnerItem<Integer>> mOrientationSpinnerAdapter; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 895 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 896 | private Spinner mRangeOptionsSpinner; |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 897 | private ArrayAdapter<SpinnerItem<Integer>> mRangeOptionsSpinnerAdapter; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 898 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 899 | private SimpleStringSplitter mStringCommaSplitter = |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 900 | new SimpleStringSplitter(','); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 901 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 902 | private View mContentContainer; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 903 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 904 | private View mAdvancedPrintOptionsContainer; |
| 905 | |
| 906 | private Button mAdvancedOptionsButton; |
| 907 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 908 | private Button mPrintButton; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 909 | |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 910 | private PrinterId mNextPrinterId; |
| 911 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 912 | private PrinterInfo mCurrentPrinter; |
| 913 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 914 | private MediaSizeComparator mMediaSizeComparator; |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 915 | |
Svetoslav | 89ed9fc | 2013-10-11 17:00:45 -0700 | [diff] [blame] | 916 | private final OnFocusChangeListener mFocusListener = new OnFocusChangeListener() { |
| 917 | @Override |
| 918 | public void onFocusChange(View view, boolean hasFocus) { |
| 919 | EditText editText = (EditText) view; |
| 920 | if (!TextUtils.isEmpty(editText.getText())) { |
| 921 | editText.setSelection(editText.getText().length()); |
| 922 | } |
| 923 | } |
| 924 | }; |
| 925 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 926 | private final OnItemSelectedListener mOnItemSelectedListener = |
| 927 | new AdapterView.OnItemSelectedListener() { |
| 928 | @Override |
| 929 | public void onItemSelected(AdapterView<?> spinner, View view, int position, long id) { |
| 930 | if (spinner == mDestinationSpinner) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 931 | if (mIgnoreNextDestinationChange) { |
| 932 | mIgnoreNextDestinationChange = false; |
| 933 | return; |
| 934 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 935 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 936 | if (position == AdapterView.INVALID_POSITION) { |
| 937 | updateUi(); |
| 938 | return; |
| 939 | } |
| 940 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 941 | if (id == DEST_ADAPTER_ITEM_ID_ALL_PRINTERS) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 942 | startSelectPrinterActivity(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 943 | return; |
| 944 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 945 | |
Svetoslav Ganov | 6be4c764 | 2013-09-26 21:12:47 -0700 | [diff] [blame] | 946 | mCapabilitiesTimeout.remove(); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 947 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 948 | mCurrentPrinter = (PrinterInfo) mDestinationSpinnerAdapter |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 949 | .getItem(position); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 950 | |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 951 | mSpoolerProvider.getSpooler().setPrintJobPrinterNoPersistence( |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 952 | mPrintJobId, mCurrentPrinter); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 953 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 954 | if (mCurrentPrinter.getStatus() == PrinterInfo.STATUS_UNAVAILABLE) { |
Svetoslav | 1c664b6 | 2013-11-04 12:30:08 -0800 | [diff] [blame] | 955 | mCapabilitiesTimeout.post(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 956 | updateUi(); |
| 957 | return; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 958 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 959 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 960 | PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities(); |
| 961 | if (capabilities == null) { |
Svetoslav Ganov | 6be4c764 | 2013-09-26 21:12:47 -0700 | [diff] [blame] | 962 | mCapabilitiesTimeout.post(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 963 | updateUi(); |
| 964 | refreshCurrentPrinter(); |
| 965 | } else { |
| 966 | updatePrintAttributes(capabilities); |
| 967 | updateUi(); |
| 968 | mController.update(); |
Svetoslav Ganov | 307a0d4 | 2013-09-23 15:51:03 -0700 | [diff] [blame] | 969 | refreshCurrentPrinter(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 970 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 971 | } else if (spinner == mMediaSizeSpinner) { |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 972 | if (mIgnoreNextMediaSizeChange) { |
| 973 | mIgnoreNextMediaSizeChange = false; |
| 974 | return; |
| 975 | } |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 976 | if (mOldMediaSizeSelectionIndex |
| 977 | == mMediaSizeSpinner.getSelectedItemPosition()) { |
| 978 | mOldMediaSizeSelectionIndex = AdapterView.INVALID_POSITION; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 979 | return; |
| 980 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 981 | SpinnerItem<MediaSize> mediaItem = mMediaSizeSpinnerAdapter.getItem(position); |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 982 | if (mOrientationSpinner.getSelectedItemPosition() == 0) { |
| 983 | mCurrPrintAttributes.setMediaSize(mediaItem.value.asPortrait()); |
| 984 | } else { |
| 985 | mCurrPrintAttributes.setMediaSize(mediaItem.value.asLandscape()); |
| 986 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 987 | if (!hasErrors()) { |
| 988 | mController.update(); |
| 989 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 990 | } else if (spinner == mColorModeSpinner) { |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 991 | if (mIgnoreNextColorChange) { |
| 992 | mIgnoreNextColorChange = false; |
| 993 | return; |
| 994 | } |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 995 | if (mOldColorModeSelectionIndex |
| 996 | == mColorModeSpinner.getSelectedItemPosition()) { |
| 997 | mOldColorModeSelectionIndex = AdapterView.INVALID_POSITION; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 998 | return; |
| 999 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1000 | SpinnerItem<Integer> colorModeItem = |
| 1001 | mColorModeSpinnerAdapter.getItem(position); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1002 | mCurrPrintAttributes.setColorMode(colorModeItem.value); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1003 | if (!hasErrors()) { |
| 1004 | mController.update(); |
| 1005 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1006 | } else if (spinner == mOrientationSpinner) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1007 | if (mIgnoreNextOrientationChange) { |
| 1008 | mIgnoreNextOrientationChange = false; |
| 1009 | return; |
| 1010 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1011 | SpinnerItem<Integer> orientationItem = |
| 1012 | mOrientationSpinnerAdapter.getItem(position); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1013 | setCurrentPrintAttributesOrientation(orientationItem.value); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1014 | if (!hasErrors()) { |
| 1015 | mController.update(); |
| 1016 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1017 | } else if (spinner == mRangeOptionsSpinner) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1018 | if (mIgnoreNextRangeOptionChange) { |
| 1019 | mIgnoreNextRangeOptionChange = false; |
| 1020 | return; |
| 1021 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1022 | updateUi(); |
| 1023 | if (!hasErrors()) { |
| 1024 | mController.update(); |
| 1025 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | @Override |
| 1030 | public void onNothingSelected(AdapterView<?> parent) { |
| 1031 | /* do nothing*/ |
| 1032 | } |
| 1033 | }; |
| 1034 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1035 | private void setCurrentPrintAttributesOrientation(int orientation) { |
| 1036 | MediaSize mediaSize = mCurrPrintAttributes.getMediaSize(); |
| 1037 | if (orientation == ORIENTATION_PORTRAIT) { |
| 1038 | if (!mediaSize.isPortrait()) { |
| 1039 | // Rotate the media size. |
| 1040 | mCurrPrintAttributes.setMediaSize(mediaSize.asPortrait()); |
| 1041 | |
| 1042 | // Rotate the resolution. |
| 1043 | Resolution oldResolution = mCurrPrintAttributes.getResolution(); |
| 1044 | Resolution newResolution = new Resolution( |
| 1045 | oldResolution.getId(), |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1046 | oldResolution.getLabel(), |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1047 | oldResolution.getVerticalDpi(), |
| 1048 | oldResolution.getHorizontalDpi()); |
| 1049 | mCurrPrintAttributes.setResolution(newResolution); |
| 1050 | |
| 1051 | // Rotate the physical margins. |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1052 | Margins oldMinMargins = mCurrPrintAttributes.getMinMargins(); |
| 1053 | Margins newMinMargins = new Margins( |
| 1054 | oldMinMargins.getBottomMils(), |
| 1055 | oldMinMargins.getLeftMils(), |
| 1056 | oldMinMargins.getTopMils(), |
| 1057 | oldMinMargins.getRightMils()); |
| 1058 | mCurrPrintAttributes.setMinMargins(newMinMargins); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1059 | } |
| 1060 | } else { |
| 1061 | if (mediaSize.isPortrait()) { |
| 1062 | // Rotate the media size. |
| 1063 | mCurrPrintAttributes.setMediaSize(mediaSize.asLandscape()); |
| 1064 | |
| 1065 | // Rotate the resolution. |
| 1066 | Resolution oldResolution = mCurrPrintAttributes.getResolution(); |
| 1067 | Resolution newResolution = new Resolution( |
| 1068 | oldResolution.getId(), |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1069 | oldResolution.getLabel(), |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1070 | oldResolution.getVerticalDpi(), |
| 1071 | oldResolution.getHorizontalDpi()); |
| 1072 | mCurrPrintAttributes.setResolution(newResolution); |
| 1073 | |
| 1074 | // Rotate the physical margins. |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1075 | Margins oldMinMargins = mCurrPrintAttributes.getMinMargins(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1076 | Margins newMargins = new Margins( |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1077 | oldMinMargins.getTopMils(), |
| 1078 | oldMinMargins.getRightMils(), |
| 1079 | oldMinMargins.getBottomMils(), |
| 1080 | oldMinMargins.getLeftMils()); |
| 1081 | mCurrPrintAttributes.setMinMargins(newMargins); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | private void updatePrintAttributes(PrinterCapabilitiesInfo capabilities) { |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1087 | PrintAttributes defaults = capabilities.getDefaults(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1088 | |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 1089 | // Sort the media sizes based on the current locale. |
| 1090 | List<MediaSize> sortedMediaSizes = new ArrayList<MediaSize>( |
| 1091 | capabilities.getMediaSizes()); |
| 1092 | Collections.sort(sortedMediaSizes, mMediaSizeComparator); |
| 1093 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1094 | // Media size. |
| 1095 | MediaSize currMediaSize = mCurrPrintAttributes.getMediaSize(); |
| 1096 | if (currMediaSize == null) { |
| 1097 | mCurrPrintAttributes.setMediaSize(defaults.getMediaSize()); |
| 1098 | } else { |
| 1099 | MediaSize currMediaSizePortrait = currMediaSize.asPortrait(); |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 1100 | final int mediaSizeCount = sortedMediaSizes.size(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1101 | for (int i = 0; i < mediaSizeCount; i++) { |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 1102 | MediaSize mediaSize = sortedMediaSizes.get(i); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1103 | if (currMediaSizePortrait.equals(mediaSize.asPortrait())) { |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 1104 | mCurrPrintAttributes.setMediaSize(currMediaSize); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1105 | break; |
| 1106 | } |
| 1107 | } |
| 1108 | } |
| 1109 | |
| 1110 | // Color mode. |
| 1111 | final int colorMode = mCurrPrintAttributes.getColorMode(); |
| 1112 | if ((capabilities.getColorModes() & colorMode) == 0) { |
| 1113 | mCurrPrintAttributes.setColorMode(colorMode); |
| 1114 | } |
| 1115 | |
| 1116 | // Resolution |
| 1117 | Resolution resolution = mCurrPrintAttributes.getResolution(); |
| 1118 | if (resolution == null || !capabilities.getResolutions().contains(resolution)) { |
| 1119 | mCurrPrintAttributes.setResolution(defaults.getResolution()); |
| 1120 | } |
| 1121 | |
| 1122 | // Margins. |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1123 | Margins margins = mCurrPrintAttributes.getMinMargins(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1124 | if (margins == null) { |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1125 | mCurrPrintAttributes.setMinMargins(defaults.getMinMargins()); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1126 | } else { |
| 1127 | Margins minMargins = capabilities.getMinMargins(); |
| 1128 | if (margins.getLeftMils() < minMargins.getLeftMils() |
| 1129 | || margins.getTopMils() < minMargins.getTopMils() |
| 1130 | || margins.getRightMils() > minMargins.getRightMils() |
| 1131 | || margins.getBottomMils() > minMargins.getBottomMils()) { |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 1132 | mCurrPrintAttributes.setMinMargins(defaults.getMinMargins()); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1137 | private final TextWatcher mCopiesTextWatcher = new TextWatcher() { |
| 1138 | @Override |
| 1139 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 1140 | /* do nothing */ |
| 1141 | } |
| 1142 | |
| 1143 | @Override |
| 1144 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 1145 | /* do nothing */ |
| 1146 | } |
| 1147 | |
| 1148 | @Override |
| 1149 | public void afterTextChanged(Editable editable) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1150 | if (mIgnoreNextCopiesChange) { |
| 1151 | mIgnoreNextCopiesChange = false; |
| 1152 | return; |
| 1153 | } |
| 1154 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1155 | final boolean hadErrors = hasErrors(); |
| 1156 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1157 | if (editable.length() == 0) { |
| 1158 | mCopiesEditText.setError(""); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1159 | updateUi(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1160 | return; |
| 1161 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1162 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1163 | int copies = 0; |
| 1164 | try { |
| 1165 | copies = Integer.parseInt(editable.toString()); |
| 1166 | } catch (NumberFormatException nfe) { |
| 1167 | /* ignore */ |
| 1168 | } |
| 1169 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1170 | if (copies < MIN_COPIES) { |
| 1171 | mCopiesEditText.setError(""); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1172 | updateUi(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1173 | return; |
| 1174 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1175 | |
| 1176 | mCopiesEditText.setError(null); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1177 | mSpoolerProvider.getSpooler().setPrintJobCopiesNoPersistence( |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1178 | mPrintJobId, copies); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1179 | updateUi(); |
| 1180 | |
| 1181 | if (hadErrors && !hasErrors() && printAttributesChanged()) { |
| 1182 | mController.update(); |
| 1183 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1184 | } |
| 1185 | }; |
| 1186 | |
| 1187 | private final TextWatcher mRangeTextWatcher = new TextWatcher() { |
| 1188 | @Override |
| 1189 | public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 1190 | /* do nothing */ |
| 1191 | } |
| 1192 | |
| 1193 | @Override |
| 1194 | public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 1195 | /* do nothing */ |
| 1196 | } |
| 1197 | |
| 1198 | @Override |
| 1199 | public void afterTextChanged(Editable editable) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1200 | if (mIgnoreNextRangeChange) { |
| 1201 | mIgnoreNextRangeChange = false; |
| 1202 | return; |
| 1203 | } |
| 1204 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1205 | final boolean hadErrors = hasErrors(); |
| 1206 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1207 | String text = editable.toString(); |
| 1208 | |
| 1209 | if (TextUtils.isEmpty(text)) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1210 | mPageRangeEditText.setError(""); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1211 | updateUi(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1212 | return; |
| 1213 | } |
| 1214 | |
| 1215 | String escapedText = PATTERN_ESCAPE_SPECIAL_CHARS.matcher(text).replaceAll("////"); |
| 1216 | if (!PATTERN_PAGE_RANGE.matcher(escapedText).matches()) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1217 | mPageRangeEditText.setError(""); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1218 | updateUi(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1219 | return; |
| 1220 | } |
| 1221 | |
Svetoslav Ganov | ab051ba | 2013-09-26 00:34:54 -0700 | [diff] [blame] | 1222 | // The range |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1223 | Matcher matcher = PATTERN_DIGITS.matcher(text); |
| 1224 | while (matcher.find()) { |
Svetoslav Ganov | ab051ba | 2013-09-26 00:34:54 -0700 | [diff] [blame] | 1225 | String numericString = text.substring(matcher.start(), matcher.end()).trim(); |
| 1226 | if (TextUtils.isEmpty(numericString)) { |
| 1227 | continue; |
| 1228 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1229 | final int pageIndex = Integer.parseInt(numericString); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1230 | if (pageIndex < 1 || pageIndex > mDocument.info.getPageCount()) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1231 | mPageRangeEditText.setError(""); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1232 | updateUi(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1233 | return; |
| 1234 | } |
| 1235 | } |
| 1236 | |
Svetoslav Ganov | b0e68fd | 2013-09-21 12:01:54 -0700 | [diff] [blame] | 1237 | // We intentionally do not catch the case of the from page being |
| 1238 | // greater than the to page. When computing the requested pages |
| 1239 | // we just swap them if necessary. |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1240 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1241 | // Keep the print job up to date with the selected pages if we |
| 1242 | // know how many pages are there in the document. |
| 1243 | PageRange[] requestedPages = getRequestedPages(); |
| 1244 | if (requestedPages != null && requestedPages.length > 0 |
| 1245 | && requestedPages[requestedPages.length - 1].getEnd() |
| 1246 | < mDocument.info.getPageCount()) { |
| 1247 | mSpoolerProvider.getSpooler().setPrintJobPagesNoPersistence( |
| 1248 | mPrintJobId, requestedPages); |
| 1249 | } |
| 1250 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1251 | mPageRangeEditText.setError(null); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1252 | mPrintButton.setEnabled(true); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1253 | updateUi(); |
| 1254 | |
| 1255 | if (hadErrors && !hasErrors() && printAttributesChanged()) { |
| 1256 | updateUi(); |
| 1257 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1258 | } |
| 1259 | }; |
| 1260 | |
Svetoslav Ganov | 6be4c764 | 2013-09-26 21:12:47 -0700 | [diff] [blame] | 1261 | private final WaitForPrinterCapabilitiesTimeout mCapabilitiesTimeout = |
| 1262 | new WaitForPrinterCapabilitiesTimeout(); |
| 1263 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1264 | private int mEditorState; |
| 1265 | |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1266 | private boolean mIgnoreNextDestinationChange; |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1267 | private int mOldMediaSizeSelectionIndex; |
| 1268 | private int mOldColorModeSelectionIndex; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1269 | private boolean mIgnoreNextOrientationChange; |
| 1270 | private boolean mIgnoreNextRangeOptionChange; |
| 1271 | private boolean mIgnoreNextCopiesChange; |
| 1272 | private boolean mIgnoreNextRangeChange; |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1273 | private boolean mIgnoreNextMediaSizeChange; |
| 1274 | private boolean mIgnoreNextColorChange; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1275 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1276 | private int mCurrentUi = UI_NONE; |
| 1277 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1278 | private boolean mFavoritePrinterSelected; |
| 1279 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1280 | public Editor() { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1281 | showUi(UI_EDITING_PRINT_JOB, null); |
| 1282 | } |
| 1283 | |
| 1284 | public void postCreate() { |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1285 | // Destination. |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 1286 | mMediaSizeComparator = new MediaSizeComparator(PrintJobConfigActivity.this); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1287 | mDestinationSpinnerAdapter = new DestinationAdapter(); |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 1288 | mDestinationSpinnerAdapter.registerDataSetObserver(new DataSetObserver() { |
| 1289 | @Override |
| 1290 | public void onChanged() { |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 1291 | // Initially, we have only safe to PDF as a printer but after some |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1292 | // printers are loaded we want to select the user's favorite one |
| 1293 | // which is the first. |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 1294 | if (!mFavoritePrinterSelected && mDestinationSpinnerAdapter.getCount() > 1) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1295 | mFavoritePrinterSelected = true; |
| 1296 | mDestinationSpinner.setSelection(0); |
Svetoslav Ganov | 69b91e0 | 2013-10-07 22:17:30 -0700 | [diff] [blame] | 1297 | // Workaround again the weird spinner behavior to notify for selection |
| 1298 | // change on the next layout pass as the current printer is used below. |
| 1299 | mCurrentPrinter = (PrinterInfo) mDestinationSpinnerAdapter.getItem(0); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 1302 | // If there is a next printer to select and we succeed selecting |
| 1303 | // it - done. Let the selection handling code make everything right. |
| 1304 | if (mNextPrinterId != null && selectPrinter(mNextPrinterId)) { |
| 1305 | mNextPrinterId = null; |
| 1306 | return; |
| 1307 | } |
| 1308 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1309 | // If the current printer properties changed, we update the UI. |
| 1310 | if (mCurrentPrinter != null) { |
| 1311 | final int printerCount = mDestinationSpinnerAdapter.getCount(); |
| 1312 | for (int i = 0; i < printerCount; i++) { |
| 1313 | Object item = mDestinationSpinnerAdapter.getItem(i); |
| 1314 | // Some items are not printers |
| 1315 | if (item instanceof PrinterInfo) { |
| 1316 | PrinterInfo printer = (PrinterInfo) item; |
| 1317 | if (!printer.getId().equals(mCurrentPrinter.getId())) { |
| 1318 | continue; |
| 1319 | } |
| 1320 | |
Svetoslav | 9d843e0 | 2013-10-14 13:15:31 -0700 | [diff] [blame] | 1321 | // If nothing changed - done. |
| 1322 | if (mCurrentPrinter.equals(printer)) { |
| 1323 | return; |
| 1324 | } |
| 1325 | |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 1326 | // If the current printer became available and has no |
| 1327 | // capabilities, we refresh it. |
| 1328 | if (mCurrentPrinter.getStatus() == PrinterInfo.STATUS_UNAVAILABLE |
| 1329 | && printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE |
Svetoslav | 9d843e0 | 2013-10-14 13:15:31 -0700 | [diff] [blame] | 1330 | && printer.getCapabilities() == null) { |
| 1331 | if (!mCapabilitiesTimeout.isPosted()) { |
| 1332 | mCapabilitiesTimeout.post(); |
| 1333 | } |
| 1334 | mCurrentPrinter.copyFrom(printer); |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 1335 | refreshCurrentPrinter(); |
| 1336 | return; |
| 1337 | } |
| 1338 | |
Svetoslav | 02dded1 | 2013-10-11 13:56:08 -0700 | [diff] [blame] | 1339 | // If the current printer became unavailable or its |
| 1340 | // capabilities go away, we update the UI and add a |
| 1341 | // timeout to declare the printer as unavailable. |
| 1342 | if ((mCurrentPrinter.getStatus() != PrinterInfo.STATUS_UNAVAILABLE |
| 1343 | && printer.getStatus() == PrinterInfo.STATUS_UNAVAILABLE) |
| 1344 | || (mCurrentPrinter.getCapabilities() != null |
| 1345 | && printer.getCapabilities() == null)) { |
| 1346 | if (!mCapabilitiesTimeout.isPosted()) { |
| 1347 | mCapabilitiesTimeout.post(); |
Svetoslav | 02dded1 | 2013-10-11 13:56:08 -0700 | [diff] [blame] | 1348 | } |
Svetoslav | 9d843e0 | 2013-10-14 13:15:31 -0700 | [diff] [blame] | 1349 | mCurrentPrinter.copyFrom(printer); |
| 1350 | updateUi(); |
| 1351 | return; |
Svetoslav | 02dded1 | 2013-10-11 13:56:08 -0700 | [diff] [blame] | 1352 | } |
| 1353 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1354 | // We just refreshed the current printer. |
| 1355 | if (printer.getCapabilities() != null |
Svetoslav Ganov | 6be4c764 | 2013-09-26 21:12:47 -0700 | [diff] [blame] | 1356 | && mCapabilitiesTimeout.isPosted()) { |
| 1357 | mCapabilitiesTimeout.remove(); |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1358 | updatePrintAttributes(printer.getCapabilities()); |
| 1359 | updateUi(); |
| 1360 | mController.update(); |
| 1361 | } |
| 1362 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1363 | // Update the UI if capabilities changed. |
| 1364 | boolean capabilitiesChanged = false; |
| 1365 | |
| 1366 | if (mCurrentPrinter.getCapabilities() == null) { |
| 1367 | if (printer.getCapabilities() != null) { |
Svetoslav | 2c1b177 | 2013-12-20 17:10:25 -0800 | [diff] [blame] | 1368 | updatePrintAttributes(printer.getCapabilities()); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1369 | capabilitiesChanged = true; |
| 1370 | } |
| 1371 | } else if (!mCurrentPrinter.getCapabilities().equals( |
| 1372 | printer.getCapabilities())) { |
| 1373 | capabilitiesChanged = true; |
| 1374 | } |
| 1375 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1376 | // Update the UI if the status changed. |
| 1377 | final boolean statusChanged = mCurrentPrinter.getStatus() |
| 1378 | != printer.getStatus(); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1379 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1380 | // Update the printer with the latest info. |
| 1381 | if (!mCurrentPrinter.equals(printer)) { |
| 1382 | mCurrentPrinter.copyFrom(printer); |
| 1383 | } |
| 1384 | |
| 1385 | if (capabilitiesChanged || statusChanged) { |
| 1386 | // If something changed during update... |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 1387 | if (updateUi() || !mController.hasPerformedLayout()) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1388 | // Update the document. |
| 1389 | mController.update(); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | break; |
| 1394 | } |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 1395 | } |
| 1396 | } |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | @Override |
| 1400 | public void onInvalidated() { |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 1401 | /* do nothing - we always have one fake PDF printer */ |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 1402 | } |
| 1403 | }); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1404 | |
| 1405 | // Media size. |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1406 | mMediaSizeSpinnerAdapter = new ArrayAdapter<SpinnerItem<MediaSize>>( |
| 1407 | PrintJobConfigActivity.this, |
| 1408 | R.layout.spinner_dropdown_item, R.id.title); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1409 | |
| 1410 | // Color mode. |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1411 | mColorModeSpinnerAdapter = new ArrayAdapter<SpinnerItem<Integer>>( |
| 1412 | PrintJobConfigActivity.this, |
| 1413 | R.layout.spinner_dropdown_item, R.id.title); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1414 | |
| 1415 | // Orientation |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1416 | mOrientationSpinnerAdapter = new ArrayAdapter<SpinnerItem<Integer>>( |
| 1417 | PrintJobConfigActivity.this, |
| 1418 | R.layout.spinner_dropdown_item, R.id.title); |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 1419 | String[] orientationLabels = getResources().getStringArray( |
| 1420 | R.array.orientation_labels); |
| 1421 | mOrientationSpinnerAdapter.add(new SpinnerItem<Integer>( |
| 1422 | ORIENTATION_PORTRAIT, orientationLabels[0])); |
| 1423 | mOrientationSpinnerAdapter.add(new SpinnerItem<Integer>( |
| 1424 | ORIENTATION_LANDSCAPE, orientationLabels[1])); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1425 | |
| 1426 | // Range options |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1427 | mRangeOptionsSpinnerAdapter = new ArrayAdapter<SpinnerItem<Integer>>( |
| 1428 | PrintJobConfigActivity.this, |
| 1429 | R.layout.spinner_dropdown_item, R.id.title); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1430 | final int[] rangeOptionsValues = getResources().getIntArray( |
| 1431 | R.array.page_options_values); |
| 1432 | String[] rangeOptionsLabels = getResources().getStringArray( |
| 1433 | R.array.page_options_labels); |
| 1434 | final int rangeOptionsCount = rangeOptionsLabels.length; |
| 1435 | for (int i = 0; i < rangeOptionsCount; i++) { |
| 1436 | mRangeOptionsSpinnerAdapter.add(new SpinnerItem<Integer>( |
| 1437 | rangeOptionsValues[i], rangeOptionsLabels[i])); |
| 1438 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1439 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1440 | showUi(UI_EDITING_PRINT_JOB, null); |
| 1441 | bindUi(); |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 1442 | updateUi(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 1443 | } |
| 1444 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1445 | public void reselectCurrentPrinter() { |
| 1446 | if (mCurrentPrinter != null) { |
| 1447 | // TODO: While the data did not change and we set the adapter |
| 1448 | // to a newly inflated spinner, the latter does not show the |
| 1449 | // current item unless we poke the adapter. This requires more |
| 1450 | // investigation. Maybe an optimization in AdapterView does not |
| 1451 | // call into the adapter if the view is not visible which is the |
| 1452 | // case when we set the adapter. |
| 1453 | mDestinationSpinnerAdapter.notifyDataSetChanged(); |
| 1454 | final int position = mDestinationSpinnerAdapter.getPrinterIndex( |
| 1455 | mCurrentPrinter.getId()); |
| 1456 | mDestinationSpinner.setSelection(position); |
| 1457 | } |
| 1458 | } |
| 1459 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1460 | public void refreshCurrentPrinter() { |
| 1461 | PrinterInfo printer = (PrinterInfo) mDestinationSpinner.getSelectedItem(); |
| 1462 | if (printer != null) { |
| 1463 | FusedPrintersProvider printersLoader = (FusedPrintersProvider) |
| 1464 | (Loader<?>) getLoaderManager().getLoader( |
| 1465 | LOADER_ID_PRINTERS_LOADER); |
| 1466 | if (printersLoader != null) { |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 1467 | printersLoader.setTrackedPrinter(printer.getId()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 1472 | public void addCurrentPrinterToHistory() { |
| 1473 | PrinterInfo printer = (PrinterInfo) mDestinationSpinner.getSelectedItem(); |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 1474 | PrinterId fakePdfPritnerId = mDestinationSpinnerAdapter.mFakePdfPrinter.getId(); |
| 1475 | if (printer != null && !printer.getId().equals(fakePdfPritnerId)) { |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 1476 | FusedPrintersProvider printersLoader = (FusedPrintersProvider) |
| 1477 | (Loader<?>) getLoaderManager().getLoader( |
| 1478 | LOADER_ID_PRINTERS_LOADER); |
| 1479 | if (printersLoader != null) { |
| 1480 | printersLoader.addHistoricalPrinter(printer); |
| 1481 | } |
| 1482 | } |
| 1483 | } |
| 1484 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1485 | public void updateFromAdvancedOptions(PrintJobInfo printJobInfo) { |
| 1486 | boolean updateContent = false; |
| 1487 | |
| 1488 | // Copies. |
| 1489 | mCopiesEditText.setText(String.valueOf(printJobInfo.getCopies())); |
| 1490 | |
| 1491 | // Media size and orientation |
| 1492 | PrintAttributes attributes = printJobInfo.getAttributes(); |
| 1493 | if (!mCurrPrintAttributes.getMediaSize().equals(attributes.getMediaSize())) { |
| 1494 | final int mediaSizeCount = mMediaSizeSpinnerAdapter.getCount(); |
| 1495 | for (int i = 0; i < mediaSizeCount; i++) { |
| 1496 | MediaSize mediaSize = mMediaSizeSpinnerAdapter.getItem(i).value; |
| 1497 | if (mediaSize.asPortrait().equals(attributes.getMediaSize().asPortrait())) { |
| 1498 | updateContent = true; |
| 1499 | mCurrPrintAttributes.setMediaSize(attributes.getMediaSize()); |
| 1500 | mMediaSizeSpinner.setSelection(i); |
| 1501 | mIgnoreNextMediaSizeChange = true; |
| 1502 | if (attributes.getMediaSize().isPortrait()) { |
| 1503 | mOrientationSpinner.setSelection(0); |
| 1504 | mIgnoreNextOrientationChange = true; |
| 1505 | } else { |
| 1506 | mOrientationSpinner.setSelection(1); |
| 1507 | mIgnoreNextOrientationChange = true; |
| 1508 | } |
| 1509 | break; |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
| 1514 | // Color mode. |
| 1515 | final int colorMode = attributes.getColorMode(); |
| 1516 | if (mCurrPrintAttributes.getColorMode() != colorMode) { |
| 1517 | if (colorMode == PrintAttributes.COLOR_MODE_MONOCHROME) { |
| 1518 | updateContent = true; |
| 1519 | mColorModeSpinner.setSelection(0); |
| 1520 | mIgnoreNextColorChange = true; |
| 1521 | mCurrPrintAttributes.setColorMode(attributes.getColorMode()); |
| 1522 | } else if (colorMode == PrintAttributes.COLOR_MODE_COLOR) { |
| 1523 | updateContent = true; |
| 1524 | mColorModeSpinner.setSelection(1); |
| 1525 | mIgnoreNextColorChange = true; |
| 1526 | mCurrPrintAttributes.setColorMode(attributes.getColorMode()); |
| 1527 | } |
| 1528 | } |
| 1529 | |
| 1530 | // Range. |
| 1531 | PageRange[] pageRanges = printJobInfo.getPages(); |
| 1532 | if (pageRanges != null && pageRanges.length > 0) { |
| 1533 | pageRanges = PageRangeUtils.normalize(pageRanges); |
| 1534 | final int pageRangeCount = pageRanges.length; |
| 1535 | if (pageRangeCount == 1 && pageRanges[0] == PageRange.ALL_PAGES) { |
| 1536 | mRangeOptionsSpinner.setSelection(0); |
| 1537 | } else { |
| 1538 | final int pageCount = mDocument.info.getPageCount(); |
| 1539 | if (pageRanges[0].getStart() >= 0 |
| 1540 | && pageRanges[pageRanges.length - 1].getEnd() < pageCount) { |
| 1541 | mRangeOptionsSpinner.setSelection(1); |
| 1542 | StringBuilder builder = new StringBuilder(); |
| 1543 | for (int i = 0; i < pageRangeCount; i++) { |
| 1544 | if (builder.length() > 0) { |
| 1545 | builder.append(','); |
| 1546 | } |
| 1547 | PageRange pageRange = pageRanges[i]; |
Svetoslav Ganov | 1a85d9f | 2013-11-15 21:42:12 -0800 | [diff] [blame] | 1548 | final int shownStartPage = pageRange.getStart() + 1; |
| 1549 | final int shownEndPage = pageRange.getEnd() + 1; |
| 1550 | builder.append(shownStartPage); |
| 1551 | if (shownStartPage != shownEndPage) { |
| 1552 | builder.append('-'); |
| 1553 | builder.append(shownEndPage); |
| 1554 | } |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1555 | } |
| 1556 | mPageRangeEditText.setText(builder.toString()); |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | // Update the advanced options. |
| 1562 | mSpoolerProvider.getSpooler().setPrintJobAdvancedOptionsNoPersistence( |
| 1563 | mPrintJobId, printJobInfo.getAdvancedOptions()); |
| 1564 | |
| 1565 | // Update the content if needed. |
| 1566 | if (updateContent) { |
| 1567 | mController.update(); |
| 1568 | } |
| 1569 | } |
| 1570 | |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 1571 | public void ensurePrinterSelected(PrinterId printerId) { |
| 1572 | // If the printer is not present maybe the loader is not |
| 1573 | // updated yet. In this case make a note and as soon as |
| 1574 | // the printer appears will will select it. |
| 1575 | if (!selectPrinter(printerId)) { |
| 1576 | mNextPrinterId = printerId; |
| 1577 | } |
| 1578 | } |
| 1579 | |
| 1580 | public boolean selectPrinter(PrinterId printerId) { |
| 1581 | mDestinationSpinnerAdapter.ensurePrinterInVisibleAdapterPosition(printerId); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1582 | final int position = mDestinationSpinnerAdapter.getPrinterIndex(printerId); |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 1583 | if (position != AdapterView.INVALID_POSITION |
| 1584 | && position != mDestinationSpinner.getSelectedItemPosition()) { |
| 1585 | Object item = mDestinationSpinnerAdapter.getItem(position); |
| 1586 | mCurrentPrinter = (PrinterInfo) item; |
| 1587 | mDestinationSpinner.setSelection(position); |
| 1588 | return true; |
| 1589 | } |
| 1590 | return false; |
| 1591 | } |
| 1592 | |
| 1593 | public void ensureCurrentPrinterSelected() { |
| 1594 | if (mCurrentPrinter != null) { |
| 1595 | selectPrinter(mCurrentPrinter.getId()); |
| 1596 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1597 | } |
| 1598 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1599 | public boolean isPrintingToPdf() { |
| 1600 | return mDestinationSpinner.getSelectedItem() |
| 1601 | == mDestinationSpinnerAdapter.mFakePdfPrinter; |
| 1602 | } |
| 1603 | |
Svetoslav Ganov | 695c7fa | 2013-08-07 19:29:42 -0700 | [diff] [blame] | 1604 | public boolean shouldCloseOnTouch(MotionEvent event) { |
| 1605 | if (event.getAction() != MotionEvent.ACTION_DOWN) { |
| 1606 | return false; |
| 1607 | } |
| 1608 | |
| 1609 | final int[] locationInWindow = new int[2]; |
| 1610 | mContentContainer.getLocationInWindow(locationInWindow); |
| 1611 | |
| 1612 | final int windowTouchSlop = ViewConfiguration.get(PrintJobConfigActivity.this) |
| 1613 | .getScaledWindowTouchSlop(); |
| 1614 | final int eventX = (int) event.getX(); |
| 1615 | final int eventY = (int) event.getY(); |
| 1616 | final int lenientWindowLeft = locationInWindow[0] - windowTouchSlop; |
| 1617 | final int lenientWindowRight = lenientWindowLeft + mContentContainer.getWidth() |
| 1618 | + windowTouchSlop; |
| 1619 | final int lenientWindowTop = locationInWindow[1] - windowTouchSlop; |
| 1620 | final int lenientWindowBottom = lenientWindowTop + mContentContainer.getHeight() |
| 1621 | + windowTouchSlop; |
| 1622 | |
| 1623 | if (eventX < lenientWindowLeft || eventX > lenientWindowRight |
| 1624 | || eventY < lenientWindowTop || eventY > lenientWindowBottom) { |
| 1625 | return true; |
| 1626 | } |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1630 | public boolean isShwoingGeneratingPrintJobUi() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1631 | return (mCurrentUi == UI_GENERATING_PRINT_JOB); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1634 | public void showUi(int ui, final Runnable postSwitchCallback) { |
| 1635 | if (ui == UI_NONE) { |
| 1636 | throw new IllegalStateException("cannot remove the ui"); |
| 1637 | } |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1638 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1639 | if (mCurrentUi == ui) { |
| 1640 | return; |
| 1641 | } |
| 1642 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1643 | final int oldUi = mCurrentUi; |
| 1644 | mCurrentUi = ui; |
| 1645 | |
| 1646 | switch (oldUi) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1647 | case UI_NONE: { |
| 1648 | switch (ui) { |
| 1649 | case UI_EDITING_PRINT_JOB: { |
| 1650 | doUiSwitch(R.layout.print_job_config_activity_content_editing); |
| 1651 | registerPrintButtonClickListener(); |
| 1652 | if (postSwitchCallback != null) { |
| 1653 | postSwitchCallback.run(); |
| 1654 | } |
| 1655 | } break; |
| 1656 | |
| 1657 | case UI_GENERATING_PRINT_JOB: { |
| 1658 | doUiSwitch(R.layout.print_job_config_activity_content_generating); |
| 1659 | registerCancelButtonClickListener(); |
| 1660 | if (postSwitchCallback != null) { |
| 1661 | postSwitchCallback.run(); |
| 1662 | } |
| 1663 | } break; |
| 1664 | } |
| 1665 | } break; |
| 1666 | |
| 1667 | case UI_EDITING_PRINT_JOB: { |
| 1668 | switch (ui) { |
| 1669 | case UI_GENERATING_PRINT_JOB: { |
| 1670 | animateUiSwitch(R.layout.print_job_config_activity_content_generating, |
| 1671 | new Runnable() { |
| 1672 | @Override |
| 1673 | public void run() { |
| 1674 | registerCancelButtonClickListener(); |
| 1675 | if (postSwitchCallback != null) { |
| 1676 | postSwitchCallback.run(); |
| 1677 | } |
| 1678 | } |
Svetoslav | 3aa2e2b | 2013-10-10 16:46:06 -0700 | [diff] [blame] | 1679 | }, |
| 1680 | new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 1681 | ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1682 | } break; |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1683 | |
| 1684 | case UI_ERROR: { |
| 1685 | animateUiSwitch(R.layout.print_job_config_activity_content_error, |
| 1686 | new Runnable() { |
| 1687 | @Override |
| 1688 | public void run() { |
| 1689 | registerOkButtonClickListener(); |
| 1690 | if (postSwitchCallback != null) { |
| 1691 | postSwitchCallback.run(); |
| 1692 | } |
| 1693 | } |
| 1694 | }, |
| 1695 | new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 1696 | ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); |
| 1697 | } break; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1698 | } |
| 1699 | } break; |
| 1700 | |
| 1701 | case UI_GENERATING_PRINT_JOB: { |
| 1702 | switch (ui) { |
| 1703 | case UI_EDITING_PRINT_JOB: { |
| 1704 | animateUiSwitch(R.layout.print_job_config_activity_content_editing, |
| 1705 | new Runnable() { |
| 1706 | @Override |
| 1707 | public void run() { |
| 1708 | registerPrintButtonClickListener(); |
| 1709 | if (postSwitchCallback != null) { |
| 1710 | postSwitchCallback.run(); |
| 1711 | } |
| 1712 | } |
Svetoslav | 3aa2e2b | 2013-10-10 16:46:06 -0700 | [diff] [blame] | 1713 | }, |
| 1714 | new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, |
| 1715 | ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER)); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1716 | } break; |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1717 | |
| 1718 | case UI_ERROR: { |
| 1719 | animateUiSwitch(R.layout.print_job_config_activity_content_error, |
| 1720 | new Runnable() { |
| 1721 | @Override |
| 1722 | public void run() { |
| 1723 | registerOkButtonClickListener(); |
| 1724 | if (postSwitchCallback != null) { |
| 1725 | postSwitchCallback.run(); |
| 1726 | } |
| 1727 | } |
| 1728 | }, |
| 1729 | new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, |
| 1730 | ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); |
| 1731 | } break; |
| 1732 | } |
| 1733 | } break; |
| 1734 | |
| 1735 | case UI_ERROR: { |
| 1736 | switch (ui) { |
| 1737 | case UI_EDITING_PRINT_JOB: { |
| 1738 | animateUiSwitch(R.layout.print_job_config_activity_content_editing, |
| 1739 | new Runnable() { |
| 1740 | @Override |
| 1741 | public void run() { |
| 1742 | registerPrintButtonClickListener(); |
| 1743 | if (postSwitchCallback != null) { |
| 1744 | postSwitchCallback.run(); |
| 1745 | } |
| 1746 | } |
| 1747 | }, |
| 1748 | new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, |
| 1749 | ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER)); |
| 1750 | } break; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1751 | } |
| 1752 | } break; |
| 1753 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1754 | } |
| 1755 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1756 | private void registerAdvancedPrintOptionsButtonClickListener() { |
| 1757 | Button advancedOptionsButton = (Button) findViewById(R.id.advanced_settings_button); |
| 1758 | advancedOptionsButton.setOnClickListener(new OnClickListener() { |
| 1759 | @Override |
| 1760 | public void onClick(View v) { |
| 1761 | ComponentName serviceName = mCurrentPrinter.getId().getServiceName(); |
| 1762 | String activityName = getAdvancedOptionsActivityName(serviceName); |
| 1763 | if (TextUtils.isEmpty(activityName)) { |
| 1764 | return; |
| 1765 | } |
| 1766 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 1767 | intent.setComponent(new ComponentName(serviceName.getPackageName(), |
| 1768 | activityName)); |
| 1769 | |
| 1770 | List<ResolveInfo> resolvedActivities = getPackageManager() |
| 1771 | .queryIntentActivities(intent, 0); |
| 1772 | if (resolvedActivities.isEmpty()) { |
| 1773 | return; |
| 1774 | } |
| 1775 | // The activity is a component name, therefore it is one or none. |
| 1776 | if (resolvedActivities.get(0).activityInfo.exported) { |
| 1777 | PrintJobInfo printJobInfo = mSpoolerProvider.getSpooler().getPrintJobInfo( |
| 1778 | mPrintJobId, PrintManager.APP_ID_ANY); |
| 1779 | intent.putExtra(PrintService.EXTRA_PRINT_JOB_INFO, printJobInfo); |
| 1780 | // TODO: Make this an API for the next release. |
| 1781 | intent.putExtra("android.intent.extra.print.EXTRA_PRINTER_INFO", |
| 1782 | mCurrentPrinter); |
| 1783 | try { |
| 1784 | startActivityForResult(intent, |
| 1785 | ACTIVITY_POPULATE_ADVANCED_PRINT_OPTIONS); |
| 1786 | } catch (ActivityNotFoundException anfe) { |
| 1787 | Log.e(LOG_TAG, "Error starting activity for intent: " + intent, anfe); |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | }); |
| 1792 | } |
| 1793 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1794 | private void registerPrintButtonClickListener() { |
| 1795 | Button printButton = (Button) findViewById(R.id.print_button); |
| 1796 | printButton.setOnClickListener(new OnClickListener() { |
| 1797 | @Override |
| 1798 | public void onClick(View v) { |
| 1799 | PrinterInfo printer = (PrinterInfo) mDestinationSpinner.getSelectedItem(); |
| 1800 | if (printer != null) { |
| 1801 | mEditor.confirmPrint(); |
| 1802 | mController.update(); |
| 1803 | if (!printer.equals(mDestinationSpinnerAdapter.mFakePdfPrinter)) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1804 | mEditor.refreshCurrentPrinter(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1805 | } |
| 1806 | } else { |
| 1807 | mEditor.cancel(); |
| 1808 | PrintJobConfigActivity.this.finish(); |
| 1809 | } |
| 1810 | } |
| 1811 | }); |
| 1812 | } |
| 1813 | |
| 1814 | private void registerCancelButtonClickListener() { |
| 1815 | Button cancelButton = (Button) findViewById(R.id.cancel_button); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1816 | cancelButton.setOnClickListener(new OnClickListener() { |
| 1817 | @Override |
| 1818 | public void onClick(View v) { |
| 1819 | if (!mController.isWorking()) { |
| 1820 | PrintJobConfigActivity.this.finish(); |
| 1821 | } |
| 1822 | mEditor.cancel(); |
| 1823 | } |
| 1824 | }); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1825 | } |
| 1826 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1827 | private void registerOkButtonClickListener() { |
| 1828 | Button okButton = (Button) findViewById(R.id.ok_button); |
| 1829 | okButton.setOnClickListener(new OnClickListener() { |
| 1830 | @Override |
| 1831 | public void onClick(View v) { |
| 1832 | mEditor.showUi(Editor.UI_EDITING_PRINT_JOB, new Runnable() { |
| 1833 | @Override |
| 1834 | public void run() { |
| 1835 | // Start over with a clean slate. |
| 1836 | mOldPrintAttributes.clear(); |
| 1837 | mController.initialize(); |
| 1838 | mEditor.initialize(); |
| 1839 | mEditor.bindUi(); |
| 1840 | mEditor.reselectCurrentPrinter(); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 1841 | if (!mController.hasPerformedLayout()) { |
| 1842 | mController.update(); |
| 1843 | } |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1844 | } |
| 1845 | }); |
| 1846 | } |
| 1847 | }); |
| 1848 | } |
| 1849 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1850 | private void doUiSwitch(int showLayoutId) { |
| 1851 | ViewGroup contentContainer = (ViewGroup) findViewById(R.id.content_container); |
| 1852 | contentContainer.removeAllViews(); |
| 1853 | getLayoutInflater().inflate(showLayoutId, contentContainer, true); |
| 1854 | } |
| 1855 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1856 | private void animateUiSwitch(int showLayoutId, final Runnable beforeShowNewUiAction, |
Svetoslav | 3aa2e2b | 2013-10-10 16:46:06 -0700 | [diff] [blame] | 1857 | final LayoutParams containerParams) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1858 | // Find everything we will shuffle around. |
| 1859 | final ViewGroup contentContainer = (ViewGroup) findViewById(R.id.content_container); |
| 1860 | final View hidingView = contentContainer.getChildAt(0); |
| 1861 | final View showingView = getLayoutInflater().inflate(showLayoutId, |
| 1862 | null, false); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1863 | |
| 1864 | // First animation - fade out the old content. |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 1865 | AutoCancellingAnimator.animate(hidingView).alpha(0.0f) |
| 1866 | .withLayer().withEndAction(new Runnable() { |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1867 | @Override |
| 1868 | public void run() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1869 | hidingView.setVisibility(View.INVISIBLE); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1870 | |
| 1871 | // Prepare the new content with correct size and alpha. |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1872 | showingView.setMinimumWidth(contentContainer.getWidth()); |
| 1873 | showingView.setAlpha(0.0f); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1874 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1875 | // Compute how to much shrink /stretch the content. |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1876 | final int widthSpec = MeasureSpec.makeMeasureSpec( |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1877 | contentContainer.getWidth(), MeasureSpec.UNSPECIFIED); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1878 | final int heightSpec = MeasureSpec.makeMeasureSpec( |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1879 | contentContainer.getHeight(), MeasureSpec.UNSPECIFIED); |
| 1880 | showingView.measure(widthSpec, heightSpec); |
| 1881 | final float scaleY = (float) showingView.getMeasuredHeight() |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1882 | / (float) contentContainer.getHeight(); |
| 1883 | |
| 1884 | // Second animation - resize the container. |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1885 | AutoCancellingAnimator.animate(contentContainer).scaleY(scaleY) |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 1886 | .withEndAction(new Runnable() { |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1887 | @Override |
| 1888 | public void run() { |
| 1889 | // Swap the old and the new content. |
| 1890 | contentContainer.removeAllViews(); |
| 1891 | contentContainer.setScaleY(1.0f); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1892 | contentContainer.addView(showingView); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1893 | |
Svetoslav | 3aa2e2b | 2013-10-10 16:46:06 -0700 | [diff] [blame] | 1894 | contentContainer.setLayoutParams(containerParams); |
| 1895 | |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1896 | beforeShowNewUiAction.run(); |
| 1897 | |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1898 | // Third animation - show the new content. |
Svetoslav Ganov | caff388 | 2013-10-09 10:53:26 -0700 | [diff] [blame] | 1899 | AutoCancellingAnimator.animate(showingView).alpha(1.0f); |
Svetoslav Ganov | 32c5eb3 | 2013-08-06 23:49:25 -0700 | [diff] [blame] | 1900 | } |
| 1901 | }); |
| 1902 | } |
| 1903 | }); |
| 1904 | } |
| 1905 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1906 | public void initialize() { |
| 1907 | mEditorState = EDITOR_STATE_INITIALIZED; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | public boolean isCancelled() { |
| 1911 | return mEditorState == EDITOR_STATE_CANCELLED; |
| 1912 | } |
| 1913 | |
| 1914 | public void cancel() { |
| 1915 | mEditorState = EDITOR_STATE_CANCELLED; |
| 1916 | mController.cancel(); |
| 1917 | updateUi(); |
| 1918 | } |
| 1919 | |
| 1920 | public boolean isDone() { |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 1921 | return isPrintConfirmed() || isCancelled(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
| 1924 | public boolean isPrintConfirmed() { |
| 1925 | return mEditorState == EDITOR_STATE_CONFIRMED_PRINT; |
| 1926 | } |
| 1927 | |
| 1928 | public void confirmPrint() { |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 1929 | addCurrentPrinterToHistory(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1930 | mEditorState = EDITOR_STATE_CONFIRMED_PRINT; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1931 | showUi(UI_GENERATING_PRINT_JOB, null); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1932 | } |
| 1933 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1934 | public PageRange[] getRequestedPages() { |
| 1935 | if (hasErrors()) { |
| 1936 | return null; |
| 1937 | } |
| 1938 | if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) { |
| 1939 | List<PageRange> pageRanges = new ArrayList<PageRange>(); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 1940 | mStringCommaSplitter.setString(mPageRangeEditText.getText().toString()); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1941 | |
| 1942 | while (mStringCommaSplitter.hasNext()) { |
| 1943 | String range = mStringCommaSplitter.next().trim(); |
Svetoslav Ganov | ab051ba | 2013-09-26 00:34:54 -0700 | [diff] [blame] | 1944 | if (TextUtils.isEmpty(range)) { |
| 1945 | continue; |
| 1946 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1947 | final int dashIndex = range.indexOf('-'); |
| 1948 | final int fromIndex; |
| 1949 | final int toIndex; |
| 1950 | |
| 1951 | if (dashIndex > 0) { |
Svetoslav Ganov | ab051ba | 2013-09-26 00:34:54 -0700 | [diff] [blame] | 1952 | fromIndex = Integer.parseInt(range.substring(0, dashIndex).trim()) - 1; |
Svetoslav | c348402 | 2013-09-16 19:03:39 -0700 | [diff] [blame] | 1953 | // It is possible that the dash is at the end since the input |
| 1954 | // verification can has to allow the user to keep entering if |
| 1955 | // this would lead to a valid input. So we handle this. |
| 1956 | toIndex = (dashIndex < range.length() - 1) |
| 1957 | ? Integer.parseInt(range.substring(dashIndex + 1, |
Svetoslav Ganov | ab051ba | 2013-09-26 00:34:54 -0700 | [diff] [blame] | 1958 | range.length()).trim()) - 1 : fromIndex; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1959 | } else { |
Svetoslav Ganov | d26d489 | 2013-08-28 14:37:54 -0700 | [diff] [blame] | 1960 | fromIndex = toIndex = Integer.parseInt(range) - 1; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1961 | } |
| 1962 | |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 1963 | PageRange pageRange = new PageRange(Math.min(fromIndex, toIndex), |
| 1964 | Math.max(fromIndex, toIndex)); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 1965 | pageRanges.add(pageRange); |
| 1966 | } |
| 1967 | |
| 1968 | PageRange[] pageRangesArray = new PageRange[pageRanges.size()]; |
| 1969 | pageRanges.toArray(pageRangesArray); |
| 1970 | |
| 1971 | return PageRangeUtils.normalize(pageRangesArray); |
| 1972 | } |
| 1973 | |
| 1974 | return ALL_PAGES_ARRAY; |
| 1975 | } |
| 1976 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1977 | private void bindUi() { |
| 1978 | if (mCurrentUi != UI_EDITING_PRINT_JOB) { |
| 1979 | return; |
| 1980 | } |
| 1981 | |
| 1982 | // Content container |
| 1983 | mContentContainer = findViewById(R.id.content_container); |
| 1984 | |
| 1985 | // Copies |
| 1986 | mCopiesEditText = (EditText) findViewById(R.id.copies_edittext); |
Svetoslav | 89ed9fc | 2013-10-11 17:00:45 -0700 | [diff] [blame] | 1987 | mCopiesEditText.setOnFocusChangeListener(mFocusListener); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1988 | mCopiesEditText.setText(MIN_COPIES_STRING); |
Svetoslav | 89ed9fc | 2013-10-11 17:00:45 -0700 | [diff] [blame] | 1989 | mCopiesEditText.setSelection(mCopiesEditText.getText().length()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1990 | mCopiesEditText.addTextChangedListener(mCopiesTextWatcher); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1991 | if (!TextUtils.equals(mCopiesEditText.getText(), MIN_COPIES_STRING)) { |
| 1992 | mIgnoreNextCopiesChange = true; |
| 1993 | } |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 1994 | mSpoolerProvider.getSpooler().setPrintJobCopiesNoPersistence( |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 1995 | mPrintJobId, MIN_COPIES); |
| 1996 | |
| 1997 | // Destination. |
| 1998 | mDestinationSpinner = (Spinner) findViewById(R.id.destination_spinner); |
Svetoslav | 39e71de | 2013-10-02 19:30:32 -0700 | [diff] [blame] | 1999 | mDestinationSpinner.setDropDownWidth(ViewGroup.LayoutParams.MATCH_PARENT); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2000 | mDestinationSpinner.setAdapter(mDestinationSpinnerAdapter); |
| 2001 | mDestinationSpinner.setOnItemSelectedListener(mOnItemSelectedListener); |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 2002 | if (mDestinationSpinnerAdapter.getCount() > 0) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2003 | mIgnoreNextDestinationChange = true; |
| 2004 | } |
| 2005 | |
| 2006 | // Media size. |
| 2007 | mMediaSizeSpinner = (Spinner) findViewById(R.id.paper_size_spinner); |
| 2008 | mMediaSizeSpinner.setAdapter(mMediaSizeSpinnerAdapter); |
| 2009 | mMediaSizeSpinner.setOnItemSelectedListener(mOnItemSelectedListener); |
| 2010 | if (mMediaSizeSpinnerAdapter.getCount() > 0) { |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2011 | mOldMediaSizeSelectionIndex = 0; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | // Color mode. |
| 2015 | mColorModeSpinner = (Spinner) findViewById(R.id.color_spinner); |
| 2016 | mColorModeSpinner.setAdapter(mColorModeSpinnerAdapter); |
| 2017 | mColorModeSpinner.setOnItemSelectedListener(mOnItemSelectedListener); |
| 2018 | if (mColorModeSpinnerAdapter.getCount() > 0) { |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2019 | mOldColorModeSelectionIndex = 0; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | // Orientation |
| 2023 | mOrientationSpinner = (Spinner) findViewById(R.id.orientation_spinner); |
| 2024 | mOrientationSpinner.setAdapter(mOrientationSpinnerAdapter); |
| 2025 | mOrientationSpinner.setOnItemSelectedListener(mOnItemSelectedListener); |
| 2026 | if (mOrientationSpinnerAdapter.getCount() > 0) { |
| 2027 | mIgnoreNextOrientationChange = true; |
| 2028 | } |
| 2029 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2030 | // Range options |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2031 | mRangeOptionsTitle = (TextView) findViewById(R.id.range_options_title); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2032 | mRangeOptionsSpinner = (Spinner) findViewById(R.id.range_options_spinner); |
| 2033 | mRangeOptionsSpinner.setAdapter(mRangeOptionsSpinnerAdapter); |
| 2034 | mRangeOptionsSpinner.setOnItemSelectedListener(mOnItemSelectedListener); |
| 2035 | if (mRangeOptionsSpinnerAdapter.getCount() > 0) { |
| 2036 | mIgnoreNextRangeOptionChange = true; |
| 2037 | } |
| 2038 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2039 | // Page range |
| 2040 | mPageRangeTitle = (TextView) findViewById(R.id.page_range_title); |
| 2041 | mPageRangeEditText = (EditText) findViewById(R.id.page_range_edittext); |
Svetoslav | 89ed9fc | 2013-10-11 17:00:45 -0700 | [diff] [blame] | 2042 | mPageRangeEditText.setOnFocusChangeListener(mFocusListener); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2043 | mPageRangeEditText.addTextChangedListener(mRangeTextWatcher); |
| 2044 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 2045 | // Advanced options button. |
| 2046 | mAdvancedPrintOptionsContainer = findViewById(R.id.advanced_settings_container); |
| 2047 | mAdvancedOptionsButton = (Button) findViewById(R.id.advanced_settings_button); |
| 2048 | registerAdvancedPrintOptionsButtonClickListener(); |
| 2049 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2050 | // Print button |
| 2051 | mPrintButton = (Button) findViewById(R.id.print_button); |
| 2052 | registerPrintButtonClickListener(); |
| 2053 | } |
| 2054 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2055 | public boolean updateUi() { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2056 | if (mCurrentUi != UI_EDITING_PRINT_JOB) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2057 | return false; |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2058 | } |
Svetoslav | a36285f | 2013-09-05 11:27:45 -0700 | [diff] [blame] | 2059 | if (isPrintConfirmed() || isCancelled()) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2060 | mDestinationSpinner.setEnabled(false); |
| 2061 | mCopiesEditText.setEnabled(false); |
| 2062 | mMediaSizeSpinner.setEnabled(false); |
| 2063 | mColorModeSpinner.setEnabled(false); |
| 2064 | mOrientationSpinner.setEnabled(false); |
| 2065 | mRangeOptionsSpinner.setEnabled(false); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2066 | mPageRangeEditText.setEnabled(false); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2067 | mPrintButton.setEnabled(false); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 2068 | mAdvancedOptionsButton.setEnabled(false); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2069 | return false; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2072 | // If a printer with capabilities is selected, then we enabled all options. |
| 2073 | boolean allOptionsEnabled = false; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2074 | final int selectedIndex = mDestinationSpinner.getSelectedItemPosition(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2075 | if (selectedIndex >= 0) { |
| 2076 | Object item = mDestinationSpinnerAdapter.getItem(selectedIndex); |
| 2077 | if (item instanceof PrinterInfo) { |
| 2078 | PrinterInfo printer = (PrinterInfo) item; |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2079 | if (printer.getCapabilities() != null |
| 2080 | && printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2081 | allOptionsEnabled = true; |
| 2082 | } |
| 2083 | } |
| 2084 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2085 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2086 | if (!allOptionsEnabled) { |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2087 | mCopiesEditText.setEnabled(false); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2088 | mMediaSizeSpinner.setEnabled(false); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2089 | mColorModeSpinner.setEnabled(false); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2090 | mOrientationSpinner.setEnabled(false); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2091 | mRangeOptionsSpinner.setEnabled(false); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2092 | mPageRangeEditText.setEnabled(false); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2093 | mPrintButton.setEnabled(false); |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 2094 | mAdvancedOptionsButton.setEnabled(false); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2095 | return false; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2096 | } else { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2097 | boolean someAttributeSelectionChanged = false; |
| 2098 | |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2099 | PrinterInfo printer = (PrinterInfo) mDestinationSpinner.getSelectedItem(); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 2100 | PrinterCapabilitiesInfo capabilities = printer.getCapabilities(); |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 2101 | PrintAttributes defaultAttributes = printer.getCapabilities().getDefaults(); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2102 | |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2103 | // Media size. |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2104 | // Sort the media sizes based on the current locale. |
| 2105 | List<MediaSize> mediaSizes = new ArrayList<MediaSize>(capabilities.getMediaSizes()); |
| 2106 | Collections.sort(mediaSizes, mMediaSizeComparator); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2107 | |
| 2108 | // If the media sizes changed, we update the adapter and the spinner. |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2109 | boolean mediaSizesChanged = false; |
| 2110 | final int mediaSizeCount = mediaSizes.size(); |
| 2111 | if (mediaSizeCount != mMediaSizeSpinnerAdapter.getCount()) { |
| 2112 | mediaSizesChanged = true; |
| 2113 | } else { |
| 2114 | for (int i = 0; i < mediaSizeCount; i++) { |
| 2115 | if (!mediaSizes.get(i).equals(mMediaSizeSpinnerAdapter.getItem(i).value)) { |
| 2116 | mediaSizesChanged = true; |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | } |
| 2121 | if (mediaSizesChanged) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2122 | // Remember the old media size to try selecting it again. |
| 2123 | int oldMediaSizeNewIndex = AdapterView.INVALID_POSITION; |
| 2124 | MediaSize oldMediaSize = mCurrPrintAttributes.getMediaSize(); |
| 2125 | |
| 2126 | // Rebuild the adapter data. |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2127 | mMediaSizeSpinnerAdapter.clear(); |
| 2128 | for (int i = 0; i < mediaSizeCount; i++) { |
| 2129 | MediaSize mediaSize = mediaSizes.get(i); |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2130 | if (mediaSize.asPortrait().equals(oldMediaSize.asPortrait())) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2131 | // Update the index of the old selection. |
| 2132 | oldMediaSizeNewIndex = i; |
| 2133 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2134 | mMediaSizeSpinnerAdapter.add(new SpinnerItem<MediaSize>( |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 2135 | mediaSize, mediaSize.getLabel(getPackageManager()))); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2136 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2137 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2138 | mMediaSizeSpinner.setEnabled(true); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2139 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2140 | if (oldMediaSizeNewIndex != AdapterView.INVALID_POSITION) { |
| 2141 | // Select the old media size - nothing really changed. |
| 2142 | setMediaSizeSpinnerSelectionNoCallback(oldMediaSizeNewIndex); |
| 2143 | } else { |
| 2144 | // Select the first or the default and mark if selection changed. |
| 2145 | final int mediaSizeIndex = Math.max(mediaSizes.indexOf( |
| 2146 | defaultAttributes.getMediaSize()), 0); |
| 2147 | setMediaSizeSpinnerSelectionNoCallback(mediaSizeIndex); |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2148 | if (oldMediaSize.isPortrait()) { |
| 2149 | mCurrPrintAttributes.setMediaSize(mMediaSizeSpinnerAdapter |
| 2150 | .getItem(mediaSizeIndex).value.asPortrait()); |
| 2151 | } else { |
| 2152 | mCurrPrintAttributes.setMediaSize(mMediaSizeSpinnerAdapter |
| 2153 | .getItem(mediaSizeIndex).value.asLandscape()); |
| 2154 | } |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2155 | someAttributeSelectionChanged = true; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2156 | } |
| 2157 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2158 | mMediaSizeSpinner.setEnabled(true); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2159 | |
| 2160 | // Color mode. |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 2161 | final int colorModes = capabilities.getColorModes(); |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2162 | |
| 2163 | // If the color modes changed, we update the adapter and the spinner. |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2164 | boolean colorModesChanged = false; |
| 2165 | if (Integer.bitCount(colorModes) != mColorModeSpinnerAdapter.getCount()) { |
| 2166 | colorModesChanged = true; |
| 2167 | } else { |
| 2168 | int remainingColorModes = colorModes; |
Svetoslav Ganov | 55b5f80 | 2013-08-05 11:26:16 -0700 | [diff] [blame] | 2169 | int adapterIndex = 0; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2170 | while (remainingColorModes != 0) { |
| 2171 | final int colorBitOffset = Integer.numberOfTrailingZeros( |
| 2172 | remainingColorModes); |
| 2173 | final int colorMode = 1 << colorBitOffset; |
| 2174 | remainingColorModes &= ~colorMode; |
Svetoslav Ganov | 55b5f80 | 2013-08-05 11:26:16 -0700 | [diff] [blame] | 2175 | if (colorMode != mColorModeSpinnerAdapter.getItem(adapterIndex).value) { |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2176 | colorModesChanged = true; |
| 2177 | break; |
| 2178 | } |
Svetoslav Ganov | 55b5f80 | 2013-08-05 11:26:16 -0700 | [diff] [blame] | 2179 | adapterIndex++; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2180 | } |
| 2181 | } |
| 2182 | if (colorModesChanged) { |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2183 | // Remember the old color mode to try selecting it again. |
| 2184 | int oldColorModeNewIndex = AdapterView.INVALID_POSITION; |
| 2185 | final int oldColorMode = mCurrPrintAttributes.getColorMode(); |
| 2186 | |
| 2187 | // Rebuild the adapter data. |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2188 | mColorModeSpinnerAdapter.clear(); |
| 2189 | String[] colorModeLabels = getResources().getStringArray( |
| 2190 | R.array.color_mode_labels); |
| 2191 | int remainingColorModes = colorModes; |
| 2192 | while (remainingColorModes != 0) { |
| 2193 | final int colorBitOffset = Integer.numberOfTrailingZeros( |
| 2194 | remainingColorModes); |
| 2195 | final int colorMode = 1 << colorBitOffset; |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2196 | if (colorMode == oldColorMode) { |
| 2197 | // Update the index of the old selection. |
| 2198 | oldColorModeNewIndex = colorBitOffset; |
| 2199 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2200 | remainingColorModes &= ~colorMode; |
| 2201 | mColorModeSpinnerAdapter.add(new SpinnerItem<Integer>(colorMode, |
| 2202 | colorModeLabels[colorBitOffset])); |
| 2203 | } |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2204 | mColorModeSpinner.setEnabled(true); |
| 2205 | if (oldColorModeNewIndex != AdapterView.INVALID_POSITION) { |
| 2206 | // Select the old color mode - nothing really changed. |
| 2207 | setColorModeSpinnerSelectionNoCallback(oldColorModeNewIndex); |
Svetoslav Ganov | 55b5f80 | 2013-08-05 11:26:16 -0700 | [diff] [blame] | 2208 | } else { |
Svetoslav | b0d97b8 | 2014-01-02 11:23:44 -0800 | [diff] [blame] | 2209 | final int selectedColorMode = colorModes & defaultAttributes.getColorMode(); |
| 2210 | final int itemCount = mColorModeSpinnerAdapter.getCount(); |
| 2211 | for (int i = 0; i < itemCount; i++) { |
| 2212 | SpinnerItem<Integer> item = mColorModeSpinnerAdapter.getItem(i); |
| 2213 | if (selectedColorMode == item.value) { |
| 2214 | setColorModeSpinnerSelectionNoCallback(i); |
| 2215 | mCurrPrintAttributes.setColorMode(selectedColorMode); |
| 2216 | someAttributeSelectionChanged = true; |
| 2217 | } |
| 2218 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2219 | } |
| 2220 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2221 | mColorModeSpinner.setEnabled(true); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2222 | |
Svetoslav | 773f54d | 2013-09-03 14:01:43 -0700 | [diff] [blame] | 2223 | // Orientation |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2224 | MediaSize mediaSize = mCurrPrintAttributes.getMediaSize(); |
| 2225 | if (mediaSize.isPortrait() |
| 2226 | && mOrientationSpinner.getSelectedItemPosition() != 0) { |
| 2227 | mIgnoreNextOrientationChange = true; |
| 2228 | mOrientationSpinner.setSelection(0); |
| 2229 | } else if (!mediaSize.isPortrait() |
| 2230 | && mOrientationSpinner.getSelectedItemPosition() != 1) { |
| 2231 | mIgnoreNextOrientationChange = true; |
| 2232 | mOrientationSpinner.setSelection(1); |
| 2233 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2234 | mOrientationSpinner.setEnabled(true); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2235 | |
| 2236 | // Range options |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2237 | PrintDocumentInfo info = mDocument.info; |
Svetoslav Ganov | b0e68fd | 2013-09-21 12:01:54 -0700 | [diff] [blame] | 2238 | if (info != null && info.getPageCount() > 0) { |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2239 | if (info.getPageCount() == 1) { |
| 2240 | mRangeOptionsSpinner.setEnabled(false); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2241 | } else { |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2242 | mRangeOptionsSpinner.setEnabled(true); |
| 2243 | if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) { |
| 2244 | if (!mPageRangeEditText.isEnabled()) { |
| 2245 | mPageRangeEditText.setEnabled(true); |
| 2246 | mPageRangeEditText.setVisibility(View.VISIBLE); |
| 2247 | mPageRangeTitle.setVisibility(View.VISIBLE); |
| 2248 | mPageRangeEditText.requestFocus(); |
| 2249 | InputMethodManager imm = (InputMethodManager) |
| 2250 | getSystemService(INPUT_METHOD_SERVICE); |
| 2251 | imm.showSoftInput(mPageRangeEditText, 0); |
| 2252 | } |
| 2253 | } else { |
| 2254 | mPageRangeEditText.setEnabled(false); |
| 2255 | mPageRangeEditText.setVisibility(View.INVISIBLE); |
| 2256 | mPageRangeTitle.setVisibility(View.INVISIBLE); |
| 2257 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2258 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2259 | final int pageCount = mDocument.info.getPageCount(); |
Svetoslav | 5ab717f | 2013-10-04 17:27:33 -0700 | [diff] [blame] | 2260 | String title = (pageCount != PrintDocumentInfo.PAGE_COUNT_UNKNOWN) |
| 2261 | ? getString(R.string.label_pages, String.valueOf(pageCount)) |
| 2262 | : getString(R.string.page_count_unknown); |
| 2263 | mRangeOptionsTitle.setText(title); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2264 | } else { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 2265 | if (mRangeOptionsSpinner.getSelectedItemPosition() != 0) { |
| 2266 | mIgnoreNextRangeOptionChange = true; |
| 2267 | mRangeOptionsSpinner.setSelection(0); |
| 2268 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2269 | mRangeOptionsSpinner.setEnabled(false); |
Svetoslav | 5ab717f | 2013-10-04 17:27:33 -0700 | [diff] [blame] | 2270 | mRangeOptionsTitle.setText(getString(R.string.page_count_unknown)); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2271 | mPageRangeEditText.setEnabled(false); |
| 2272 | mPageRangeEditText.setVisibility(View.INVISIBLE); |
| 2273 | mPageRangeTitle.setVisibility(View.INVISIBLE); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2274 | } |
| 2275 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 2276 | // Advanced print options |
| 2277 | ComponentName serviceName = mCurrentPrinter.getId().getServiceName(); |
| 2278 | if (!TextUtils.isEmpty(getAdvancedOptionsActivityName(serviceName))) { |
| 2279 | mAdvancedPrintOptionsContainer.setVisibility(View.VISIBLE); |
| 2280 | mAdvancedOptionsButton.setEnabled(true); |
| 2281 | } else { |
| 2282 | mAdvancedPrintOptionsContainer.setVisibility(View.GONE); |
| 2283 | mAdvancedOptionsButton.setEnabled(false); |
| 2284 | } |
| 2285 | |
| 2286 | // Print |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2287 | if (mDestinationSpinner.getSelectedItemId() |
| 2288 | != DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF) { |
| 2289 | String newText = getString(R.string.print_button); |
| 2290 | if (!TextUtils.equals(newText, mPrintButton.getText())) { |
| 2291 | mPrintButton.setText(R.string.print_button); |
| 2292 | } |
| 2293 | } else { |
| 2294 | String newText = getString(R.string.save_button); |
| 2295 | if (!TextUtils.equals(newText, mPrintButton.getText())) { |
| 2296 | mPrintButton.setText(R.string.save_button); |
| 2297 | } |
| 2298 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2299 | if ((mRangeOptionsSpinner.getSelectedItemPosition() == 1 |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2300 | && (TextUtils.isEmpty(mPageRangeEditText.getText()) || hasErrors())) |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2301 | || (mRangeOptionsSpinner.getSelectedItemPosition() == 0 |
| 2302 | && (!mController.hasPerformedLayout() || hasErrors()))) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2303 | mPrintButton.setEnabled(false); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2304 | } else { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2305 | mPrintButton.setEnabled(true); |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2306 | } |
| 2307 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2308 | // Copies |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2309 | if (mDestinationSpinner.getSelectedItemId() |
| 2310 | != DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF) { |
| 2311 | mCopiesEditText.setEnabled(true); |
| 2312 | } else { |
| 2313 | mCopiesEditText.setEnabled(false); |
| 2314 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2315 | if (mCopiesEditText.getError() == null |
| 2316 | && TextUtils.isEmpty(mCopiesEditText.getText())) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 2317 | mIgnoreNextCopiesChange = true; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2318 | mCopiesEditText.setText(String.valueOf(MIN_COPIES)); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2319 | mCopiesEditText.requestFocus(); |
| 2320 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2321 | |
| 2322 | return someAttributeSelectionChanged; |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2323 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Svetoslav | b4fda13 | 2013-10-25 18:57:43 -0700 | [diff] [blame] | 2326 | private String getAdvancedOptionsActivityName(ComponentName serviceName) { |
| 2327 | PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); |
| 2328 | List<PrintServiceInfo> printServices = printManager.getEnabledPrintServices(); |
| 2329 | final int printServiceCount = printServices.size(); |
| 2330 | for (int i = 0; i < printServiceCount; i ++) { |
| 2331 | PrintServiceInfo printServiceInfo = printServices.get(i); |
| 2332 | ServiceInfo serviceInfo = printServiceInfo.getResolveInfo().serviceInfo; |
| 2333 | if (serviceInfo.name.equals(serviceName.getClassName()) |
| 2334 | && serviceInfo.packageName.equals(serviceName.getPackageName())) { |
| 2335 | return printServiceInfo.getAdvancedOptionsActivityName(); |
| 2336 | } |
| 2337 | } |
| 2338 | return null; |
| 2339 | } |
| 2340 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2341 | private void setMediaSizeSpinnerSelectionNoCallback(int position) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2342 | if (mMediaSizeSpinner.getSelectedItemPosition() != position) { |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2343 | mOldMediaSizeSelectionIndex = position; |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2344 | mMediaSizeSpinner.setSelection(position); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2345 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2346 | } |
| 2347 | |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2348 | private void setColorModeSpinnerSelectionNoCallback(int position) { |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2349 | if (mColorModeSpinner.getSelectedItemPosition() != position) { |
Svetoslav | cc65b0c | 2013-09-10 21:08:32 -0700 | [diff] [blame] | 2350 | mOldColorModeSelectionIndex = position; |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2351 | mColorModeSpinner.setSelection(position); |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2352 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | private void startSelectPrinterActivity() { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2356 | Intent intent = new Intent(PrintJobConfigActivity.this, |
| 2357 | SelectPrinterActivity.class); |
| 2358 | startActivityForResult(intent, ACTIVITY_REQUEST_SELECT_PRINTER); |
| 2359 | } |
| 2360 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2361 | private boolean hasErrors() { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2362 | if (mCopiesEditText.getError() != null) { |
| 2363 | return true; |
| 2364 | } |
| 2365 | return mPageRangeEditText.getVisibility() == View.VISIBLE |
| 2366 | && mPageRangeEditText.getError() != null; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2367 | } |
| 2368 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2369 | private final class SpinnerItem<T> { |
| 2370 | final T value; |
| 2371 | CharSequence label; |
| 2372 | |
| 2373 | public SpinnerItem(T value, CharSequence label) { |
| 2374 | this.value = value; |
| 2375 | this.label = label; |
| 2376 | } |
| 2377 | |
| 2378 | public String toString() { |
| 2379 | return label.toString(); |
| 2380 | } |
| 2381 | } |
| 2382 | |
Svetoslav Ganov | 6be4c764 | 2013-09-26 21:12:47 -0700 | [diff] [blame] | 2383 | private final class WaitForPrinterCapabilitiesTimeout implements Runnable { |
| 2384 | private static final long GET_CAPABILITIES_TIMEOUT_MILLIS = 10000; // 10sec |
| 2385 | |
| 2386 | private boolean mIsPosted; |
| 2387 | |
| 2388 | public void post() { |
| 2389 | if (!mIsPosted) { |
| 2390 | mDestinationSpinner.postDelayed(this, |
| 2391 | GET_CAPABILITIES_TIMEOUT_MILLIS); |
| 2392 | mIsPosted = true; |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | public void remove() { |
| 2397 | if (mIsPosted) { |
| 2398 | mIsPosted = false; |
| 2399 | mDestinationSpinner.removeCallbacks(this); |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | public boolean isPosted() { |
| 2404 | return mIsPosted; |
| 2405 | } |
| 2406 | |
| 2407 | @Override |
| 2408 | public void run() { |
| 2409 | mIsPosted = false; |
| 2410 | if (mDestinationSpinner.getSelectedItemPosition() >= 0) { |
| 2411 | View itemView = mDestinationSpinner.getSelectedView(); |
Svetoslav | 2fa010c | 2013-10-14 17:31:26 -0700 | [diff] [blame] | 2412 | TextView titleView = (TextView) itemView.findViewById(R.id.subtitle); |
| 2413 | try { |
| 2414 | PackageInfo packageInfo = getPackageManager().getPackageInfo( |
| 2415 | mCurrentPrinter.getId().getServiceName().getPackageName(), 0); |
| 2416 | CharSequence service = packageInfo.applicationInfo.loadLabel( |
| 2417 | getPackageManager()); |
| 2418 | String subtitle = getString(R.string.printer_unavailable, service.toString()); |
| 2419 | titleView.setText(subtitle); |
| 2420 | } catch (NameNotFoundException nnfe) { |
| 2421 | /* ignore */ |
| 2422 | } |
Svetoslav Ganov | 6be4c764 | 2013-09-26 21:12:47 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } |
| 2425 | } |
| 2426 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2427 | private final class DestinationAdapter extends BaseAdapter |
| 2428 | implements LoaderManager.LoaderCallbacks<List<PrinterInfo>>{ |
| 2429 | private final List<PrinterInfo> mPrinters = new ArrayList<PrinterInfo>(); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2430 | |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2431 | private PrinterInfo mFakePdfPrinter; |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2432 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2433 | public DestinationAdapter() { |
| 2434 | getLoaderManager().initLoader(LOADER_ID_PRINTERS_LOADER, null, this); |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2437 | public int getPrinterIndex(PrinterId printerId) { |
| 2438 | for (int i = 0; i < getCount(); i++) { |
| 2439 | PrinterInfo printer = (PrinterInfo) getItem(i); |
| 2440 | if (printer != null && printer.getId().equals(printerId)) { |
| 2441 | return i; |
| 2442 | } |
| 2443 | } |
| 2444 | return AdapterView.INVALID_POSITION; |
| 2445 | } |
| 2446 | |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 2447 | public void ensurePrinterInVisibleAdapterPosition(PrinterId printerId) { |
| 2448 | final int printerCount = mPrinters.size(); |
| 2449 | for (int i = 0; i < printerCount; i++) { |
| 2450 | PrinterInfo printer = (PrinterInfo) mPrinters.get(i); |
| 2451 | if (printer.getId().equals(printerId)) { |
| 2452 | // If already in the list - do nothing. |
| 2453 | if (i < getCount() - 2) { |
| 2454 | return; |
| 2455 | } |
| 2456 | // Else replace the last one (two items are not printers). |
| 2457 | final int lastPrinterIndex = getCount() - 3; |
| 2458 | mPrinters.set(i, mPrinters.get(lastPrinterIndex)); |
| 2459 | mPrinters.set(lastPrinterIndex, printer); |
| 2460 | notifyDataSetChanged(); |
| 2461 | return; |
| 2462 | } |
| 2463 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2464 | } |
| 2465 | |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2466 | @Override |
| 2467 | public int getCount() { |
Svetoslav | 54adee8 | 2013-10-11 11:28:30 -0700 | [diff] [blame] | 2468 | if (mFakePdfPrinter == null) { |
| 2469 | return 0; |
| 2470 | } |
| 2471 | return Math.min(mPrinters.size() + 2, DEST_ADAPTER_MAX_ITEM_COUNT); |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2472 | } |
| 2473 | |
| 2474 | @Override |
Svetoslav | 2a708617 | 2013-09-24 15:14:41 -0700 | [diff] [blame] | 2475 | public boolean isEnabled(int position) { |
| 2476 | Object item = getItem(position); |
| 2477 | if (item instanceof PrinterInfo) { |
| 2478 | PrinterInfo printer = (PrinterInfo) item; |
| 2479 | return printer.getStatus() != PrinterInfo.STATUS_UNAVAILABLE; |
| 2480 | } |
| 2481 | return true; |
| 2482 | } |
| 2483 | |
| 2484 | @Override |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2485 | public Object getItem(int position) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2486 | if (mPrinters.isEmpty()) { |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2487 | if (position == 0 && mFakePdfPrinter != null) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2488 | return mFakePdfPrinter; |
| 2489 | } |
| 2490 | } else { |
| 2491 | if (position < 1) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2492 | return mPrinters.get(position); |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2493 | } |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2494 | if (position == 1 && mFakePdfPrinter != null) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2495 | return mFakePdfPrinter; |
| 2496 | } |
| 2497 | if (position < getCount() - 1) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2498 | return mPrinters.get(position - 1); |
| 2499 | } |
| 2500 | } |
| 2501 | return null; |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | @Override |
| 2505 | public long getItemId(int position) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2506 | if (mPrinters.isEmpty()) { |
Svetoslav | 54adee8 | 2013-10-11 11:28:30 -0700 | [diff] [blame] | 2507 | if (mFakePdfPrinter != null) { |
| 2508 | if (position == 0) { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 2509 | return DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF; |
Svetoslav | 54adee8 | 2013-10-11 11:28:30 -0700 | [diff] [blame] | 2510 | } else if (position == 1) { |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 2511 | return DEST_ADAPTER_ITEM_ID_ALL_PRINTERS; |
| 2512 | } |
Svetoslav Ganov | 9186d0c | 2013-09-03 00:11:58 -0700 | [diff] [blame] | 2513 | } |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2514 | } else { |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2515 | if (position == 1 && mFakePdfPrinter != null) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2516 | return DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF; |
| 2517 | } |
| 2518 | if (position == getCount() - 1) { |
| 2519 | return DEST_ADAPTER_ITEM_ID_ALL_PRINTERS; |
| 2520 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2521 | } |
Svetoslav | 66160bb | 2013-08-12 22:36:50 -0700 | [diff] [blame] | 2522 | return position; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | @Override |
| 2526 | public View getDropDownView(int position, View convertView, |
| 2527 | ViewGroup parent) { |
Svetoslav | 2a708617 | 2013-09-24 15:14:41 -0700 | [diff] [blame] | 2528 | View view = getView(position, convertView, parent); |
| 2529 | view.setEnabled(isEnabled(position)); |
| 2530 | return view; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | @Override |
| 2534 | public View getView(int position, View convertView, ViewGroup parent) { |
| 2535 | if (convertView == null) { |
| 2536 | convertView = getLayoutInflater().inflate( |
Svetoslav | c335eb4 | 2013-09-26 15:55:47 -0700 | [diff] [blame] | 2537 | R.layout.printer_dropdown_item, parent, false); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2538 | } |
| 2539 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2540 | CharSequence title = null; |
| 2541 | CharSequence subtitle = null; |
Svetoslav | c335eb4 | 2013-09-26 15:55:47 -0700 | [diff] [blame] | 2542 | Drawable icon = null; |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2543 | |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2544 | if (mPrinters.isEmpty()) { |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2545 | if (position == 0 && mFakePdfPrinter != null) { |
Svetoslav Ganov | 44720af | 2013-08-20 16:32:53 -0700 | [diff] [blame] | 2546 | PrinterInfo printer = (PrinterInfo) getItem(position); |
| 2547 | title = printer.getName(); |
| 2548 | } else if (position == 1) { |
| 2549 | title = getString(R.string.all_printers); |
| 2550 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2551 | } else { |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2552 | if (position == 1 && mFakePdfPrinter != null) { |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2553 | PrinterInfo printer = (PrinterInfo) getItem(position); |
| 2554 | title = printer.getName(); |
| 2555 | } else if (position == getCount() - 1) { |
| 2556 | title = getString(R.string.all_printers); |
| 2557 | } else { |
| 2558 | PrinterInfo printer = (PrinterInfo) getItem(position); |
| 2559 | title = printer.getName(); |
| 2560 | try { |
| 2561 | PackageInfo packageInfo = getPackageManager().getPackageInfo( |
| 2562 | printer.getId().getServiceName().getPackageName(), 0); |
| 2563 | subtitle = packageInfo.applicationInfo.loadLabel(getPackageManager()); |
Svetoslav | c335eb4 | 2013-09-26 15:55:47 -0700 | [diff] [blame] | 2564 | icon = packageInfo.applicationInfo.loadIcon(getPackageManager()); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2565 | } catch (NameNotFoundException nnfe) { |
| 2566 | /* ignore */ |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | TextView titleView = (TextView) convertView.findViewById(R.id.title); |
| 2572 | titleView.setText(title); |
| 2573 | |
| 2574 | TextView subtitleView = (TextView) convertView.findViewById(R.id.subtitle); |
| 2575 | if (!TextUtils.isEmpty(subtitle)) { |
| 2576 | subtitleView.setText(subtitle); |
| 2577 | subtitleView.setVisibility(View.VISIBLE); |
| 2578 | } else { |
| 2579 | subtitleView.setText(null); |
| 2580 | subtitleView.setVisibility(View.GONE); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Svetoslav | c335eb4 | 2013-09-26 15:55:47 -0700 | [diff] [blame] | 2583 | ImageView iconView = (ImageView) convertView.findViewById(R.id.icon); |
| 2584 | if (icon != null) { |
| 2585 | iconView.setImageDrawable(icon); |
| 2586 | iconView.setVisibility(View.VISIBLE); |
| 2587 | } else { |
Svetoslav | b196106 | 2013-10-16 16:41:30 -0700 | [diff] [blame] | 2588 | iconView.setVisibility(View.INVISIBLE); |
Svetoslav | c335eb4 | 2013-09-26 15:55:47 -0700 | [diff] [blame] | 2589 | } |
| 2590 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2591 | return convertView; |
| 2592 | } |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2593 | |
| 2594 | @Override |
| 2595 | public Loader<List<PrinterInfo>> onCreateLoader(int id, Bundle args) { |
| 2596 | if (id == LOADER_ID_PRINTERS_LOADER) { |
| 2597 | return new FusedPrintersProvider(PrintJobConfigActivity.this); |
| 2598 | } |
| 2599 | return null; |
| 2600 | } |
| 2601 | |
| 2602 | @Override |
| 2603 | public void onLoadFinished(Loader<List<PrinterInfo>> loader, |
| 2604 | List<PrinterInfo> printers) { |
Svetoslav Ganov | 24c686b | 2013-10-05 18:52:06 -0700 | [diff] [blame] | 2605 | // If this is the first load, create the fake PDF printer. |
| 2606 | // We do this to avoid flicker where the PDF printer is the |
| 2607 | // only one and as soon as the loader loads the favorites |
| 2608 | // it gets switched. Not a great user experience. |
| 2609 | if (mFakePdfPrinter == null) { |
| 2610 | mCurrentPrinter = mFakePdfPrinter = createFakePdfPrinter(); |
| 2611 | updatePrintAttributes(mCurrentPrinter.getCapabilities()); |
| 2612 | updateUi(); |
| 2613 | } |
| 2614 | |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 2615 | // We rearrange the printers if the user selects a printer |
| 2616 | // not shown in the initial short list. Therefore, we have |
| 2617 | // to keep the printer order. |
| 2618 | |
| 2619 | // No old printers - do not bother keeping their position. |
| 2620 | if (mPrinters.isEmpty()) { |
| 2621 | mPrinters.addAll(printers); |
| 2622 | mEditor.ensureCurrentPrinterSelected(); |
| 2623 | notifyDataSetChanged(); |
| 2624 | return; |
| 2625 | } |
| 2626 | |
| 2627 | // Add the new printers to a map. |
| 2628 | ArrayMap<PrinterId, PrinterInfo> newPrintersMap = |
| 2629 | new ArrayMap<PrinterId, PrinterInfo>(); |
| 2630 | final int printerCount = printers.size(); |
| 2631 | for (int i = 0; i < printerCount; i++) { |
| 2632 | PrinterInfo printer = printers.get(i); |
| 2633 | newPrintersMap.put(printer.getId(), printer); |
| 2634 | } |
| 2635 | |
| 2636 | List<PrinterInfo> newPrinters = new ArrayList<PrinterInfo>(); |
| 2637 | |
| 2638 | // Update printers we already have. |
| 2639 | final int oldPrinterCount = mPrinters.size(); |
| 2640 | for (int i = 0; i < oldPrinterCount; i++) { |
| 2641 | PrinterId oldPrinterId = mPrinters.get(i).getId(); |
| 2642 | PrinterInfo updatedPrinter = newPrintersMap.remove(oldPrinterId); |
| 2643 | if (updatedPrinter != null) { |
| 2644 | newPrinters.add(updatedPrinter); |
| 2645 | } |
| 2646 | } |
| 2647 | |
| 2648 | // Add the rest of the new printers, i.e. what is left. |
| 2649 | newPrinters.addAll(newPrintersMap.values()); |
| 2650 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2651 | mPrinters.clear(); |
Svetoslav | 4e4874b | 2013-10-01 17:53:17 -0700 | [diff] [blame] | 2652 | mPrinters.addAll(newPrinters); |
| 2653 | |
| 2654 | mEditor.ensureCurrentPrinterSelected(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2655 | notifyDataSetChanged(); |
| 2656 | } |
| 2657 | |
| 2658 | @Override |
| 2659 | public void onLoaderReset(Loader<List<PrinterInfo>> loader) { |
| 2660 | mPrinters.clear(); |
| 2661 | notifyDataSetInvalidated(); |
| 2662 | } |
| 2663 | |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2664 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2665 | private PrinterInfo createFakePdfPrinter() { |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2666 | MediaSize defaultMediaSize = MediaSizeUtils.getDefault(PrintJobConfigActivity.this); |
Svetoslav Ganov | 0ce3e87 | 2013-09-26 08:45:22 -0700 | [diff] [blame] | 2667 | |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2668 | PrinterId printerId = new PrinterId(getComponentName(), "PDF printer"); |
| 2669 | |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2670 | PrinterCapabilitiesInfo.Builder builder = |
| 2671 | new PrinterCapabilitiesInfo.Builder(printerId); |
| 2672 | |
| 2673 | String[] mediaSizeIds = getResources().getStringArray( |
| 2674 | R.array.pdf_printer_media_sizes); |
| 2675 | final int mediaSizeIdCount = mediaSizeIds.length; |
| 2676 | for (int i = 0; i < mediaSizeIdCount; i++) { |
| 2677 | String id = mediaSizeIds[i]; |
| 2678 | MediaSize mediaSize = MediaSize.getStandardMediaSizeById(id); |
| 2679 | builder.addMediaSize(mediaSize, mediaSize.equals(defaultMediaSize)); |
| 2680 | } |
| 2681 | |
| 2682 | builder.addResolution(new Resolution("PDF resolution", "PDF resolution", |
| 2683 | 300, 300), true); |
| 2684 | builder.setColorModes(PrintAttributes.COLOR_MODE_COLOR |
| 2685 | | PrintAttributes.COLOR_MODE_MONOCHROME, |
| 2686 | PrintAttributes.COLOR_MODE_COLOR); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2687 | |
| 2688 | return new PrinterInfo.Builder(printerId, getString(R.string.save_as_pdf), |
Svetoslav Ganov | aec1417 | 2013-08-27 00:30:54 -0700 | [diff] [blame] | 2689 | PrinterInfo.STATUS_IDLE) |
Svetoslav Ganov | 7be27ac | 2013-09-30 09:04:50 -0700 | [diff] [blame] | 2690 | .setCapabilities(builder.build()) |
Svetoslav | 651dd4e | 2013-09-12 14:37:47 -0700 | [diff] [blame] | 2691 | .build(); |
Svetoslav | 269403b | 2013-08-14 17:31:04 -0700 | [diff] [blame] | 2692 | } |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2693 | } |
| 2694 | } |
| 2695 | |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2696 | /** |
| 2697 | * An instance of this class class is intended to be the first focusable |
| 2698 | * in a layout to which the system automatically gives focus. It performs |
| 2699 | * some voodoo to avoid the first tap on it to start an edit mode, rather |
| 2700 | * to bring up the IME, i.e. to get the behavior as if the view was not |
| 2701 | * focused. |
| 2702 | */ |
| 2703 | public static final class CustomEditText extends EditText { |
| 2704 | private boolean mClickedBeforeFocus; |
| 2705 | private CharSequence mError; |
| 2706 | |
| 2707 | public CustomEditText(Context context, AttributeSet attrs) { |
| 2708 | super(context, attrs); |
| 2709 | } |
| 2710 | |
| 2711 | @Override |
| 2712 | public boolean performClick() { |
| 2713 | super.performClick(); |
| 2714 | if (isFocused() && !mClickedBeforeFocus) { |
| 2715 | clearFocus(); |
| 2716 | requestFocus(); |
| 2717 | } |
| 2718 | mClickedBeforeFocus = true; |
| 2719 | return true; |
| 2720 | } |
| 2721 | |
| 2722 | @Override |
| 2723 | public CharSequence getError() { |
| 2724 | return mError; |
| 2725 | } |
| 2726 | |
| 2727 | @Override |
| 2728 | public void setError(CharSequence error, Drawable icon) { |
| 2729 | setCompoundDrawables(null, null, icon, null); |
| 2730 | mError = error; |
| 2731 | } |
| 2732 | |
| 2733 | protected void onFocusChanged(boolean gainFocus, int direction, |
| 2734 | Rect previouslyFocusedRect) { |
| 2735 | if (!gainFocus) { |
| 2736 | mClickedBeforeFocus = false; |
| 2737 | } |
| 2738 | super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | private static final class Document { |
| 2743 | public PrintDocumentInfo info; |
| 2744 | public PageRange[] pages; |
| 2745 | } |
| 2746 | |
| 2747 | private static final class PageRangeUtils { |
| 2748 | |
| 2749 | private static final Comparator<PageRange> sComparator = new Comparator<PageRange>() { |
| 2750 | @Override |
| 2751 | public int compare(PageRange lhs, PageRange rhs) { |
| 2752 | return lhs.getStart() - rhs.getStart(); |
| 2753 | } |
| 2754 | }; |
| 2755 | |
| 2756 | private PageRangeUtils() { |
| 2757 | throw new UnsupportedOperationException(); |
| 2758 | } |
| 2759 | |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2760 | public static boolean contains(PageRange[] ourRanges, PageRange[] otherRanges) { |
| 2761 | if (ourRanges == null || otherRanges == null) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2762 | return false; |
| 2763 | } |
| 2764 | |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2765 | if (ourRanges.length == 1 |
| 2766 | && PageRange.ALL_PAGES.equals(ourRanges[0])) { |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2767 | return true; |
| 2768 | } |
| 2769 | |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2770 | ourRanges = normalize(ourRanges); |
| 2771 | otherRanges = normalize(otherRanges); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2772 | |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2773 | // Note that the code below relies on the ranges being normalized |
| 2774 | // which is they contain monotonically increasing non-intersecting |
| 2775 | // subranges whose start is less that or equal to the end. |
| 2776 | int otherRangeIdx = 0; |
| 2777 | final int ourRangeCount = ourRanges.length; |
| 2778 | final int otherRangeCount = otherRanges.length; |
| 2779 | for (int ourRangeIdx = 0; ourRangeIdx < ourRangeCount; ourRangeIdx++) { |
| 2780 | PageRange ourRange = ourRanges[ourRangeIdx]; |
| 2781 | for (; otherRangeIdx < otherRangeCount; otherRangeIdx++) { |
| 2782 | PageRange otherRange = otherRanges[otherRangeIdx]; |
| 2783 | if (otherRange.getStart() > ourRange.getEnd()) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2784 | break; |
| 2785 | } |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2786 | if (otherRange.getStart() < ourRange.getStart() |
| 2787 | || otherRange.getEnd() > ourRange.getEnd()) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2788 | return false; |
| 2789 | } |
| 2790 | } |
| 2791 | } |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2792 | if (otherRangeIdx < otherRangeCount) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2793 | return false; |
| 2794 | } |
| 2795 | return true; |
| 2796 | } |
| 2797 | |
| 2798 | public static PageRange[] normalize(PageRange[] pageRanges) { |
| 2799 | if (pageRanges == null) { |
| 2800 | return null; |
| 2801 | } |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2802 | final int oldRangeCount = pageRanges.length; |
| 2803 | if (oldRangeCount <= 1) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2804 | return pageRanges; |
| 2805 | } |
| 2806 | Arrays.sort(pageRanges, sComparator); |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2807 | int newRangeCount = 1; |
| 2808 | for (int i = 0; i < oldRangeCount - 1; i++) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2809 | newRangeCount++; |
| 2810 | PageRange currentRange = pageRanges[i]; |
| 2811 | PageRange nextRange = pageRanges[i + 1]; |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2812 | if (currentRange.getEnd() + 1 >= nextRange.getStart()) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2813 | newRangeCount--; |
| 2814 | pageRanges[i] = null; |
| 2815 | pageRanges[i + 1] = new PageRange(currentRange.getStart(), |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2816 | Math.max(currentRange.getEnd(), nextRange.getEnd())); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2817 | } |
| 2818 | } |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2819 | if (newRangeCount == oldRangeCount) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2820 | return pageRanges; |
| 2821 | } |
Svetoslav | f0c48a7 | 2013-09-18 14:09:47 -0700 | [diff] [blame] | 2822 | return Arrays.copyOfRange(pageRanges, oldRangeCount - newRangeCount, |
| 2823 | oldRangeCount); |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2824 | } |
| 2825 | |
Svetoslav | a4f6409 | 2013-09-20 18:57:21 -0700 | [diff] [blame] | 2826 | public static void offset(PageRange[] pageRanges, int offset) { |
Svetoslav Ganov | 85b1f88 | 2013-07-24 17:00:06 -0700 | [diff] [blame] | 2827 | if (offset == 0) { |
| 2828 | return; |
| 2829 | } |
| 2830 | final int pageRangeCount = pageRanges.length; |
| 2831 | for (int i = 0; i < pageRangeCount; i++) { |
| 2832 | final int start = pageRanges[i].getStart() + offset; |
| 2833 | final int end = pageRanges[i].getEnd() + offset; |
| 2834 | pageRanges[i] = new PageRange(start, end); |
| 2835 | } |
Svetoslav Ganov | 0d1daa5 | 2013-07-23 13:29:31 -0700 | [diff] [blame] | 2836 | } |
| 2837 | } |
Svetoslav Ganov | 860f8a6 | 2013-09-14 00:59:03 -0700 | [diff] [blame] | 2838 | |
| 2839 | private static final class AutoCancellingAnimator |
| 2840 | implements OnAttachStateChangeListener, Runnable { |
| 2841 | |
| 2842 | private ViewPropertyAnimator mAnimator; |
| 2843 | |
| 2844 | private boolean mCancelled; |
| 2845 | private Runnable mEndCallback; |
| 2846 | |
| 2847 | public static AutoCancellingAnimator animate(View view) { |
| 2848 | ViewPropertyAnimator animator = view.animate(); |
| 2849 | AutoCancellingAnimator cancellingWrapper = |
| 2850 | new AutoCancellingAnimator(animator); |
| 2851 | view.addOnAttachStateChangeListener(cancellingWrapper); |
| 2852 | return cancellingWrapper; |
| 2853 | } |
| 2854 | |
| 2855 | private AutoCancellingAnimator(ViewPropertyAnimator animator) { |
| 2856 | mAnimator = animator; |
| 2857 | } |
| 2858 | |
| 2859 | public AutoCancellingAnimator alpha(float alpha) { |
| 2860 | mAnimator = mAnimator.alpha(alpha); |
| 2861 | return this; |
| 2862 | } |
| 2863 | |
| 2864 | public void cancel() { |
| 2865 | mAnimator.cancel(); |
| 2866 | } |
| 2867 | |
| 2868 | public AutoCancellingAnimator withLayer() { |
| 2869 | mAnimator = mAnimator.withLayer(); |
| 2870 | return this; |
| 2871 | } |
| 2872 | |
| 2873 | public AutoCancellingAnimator withEndAction(Runnable callback) { |
| 2874 | mEndCallback = callback; |
| 2875 | mAnimator = mAnimator.withEndAction(this); |
| 2876 | return this; |
| 2877 | } |
| 2878 | |
| 2879 | public AutoCancellingAnimator scaleY(float scale) { |
| 2880 | mAnimator = mAnimator.scaleY(scale); |
| 2881 | return this; |
| 2882 | } |
| 2883 | |
| 2884 | @Override |
| 2885 | public void onViewAttachedToWindow(View v) { |
| 2886 | /* do nothing */ |
| 2887 | } |
| 2888 | |
| 2889 | @Override |
| 2890 | public void onViewDetachedFromWindow(View v) { |
| 2891 | cancel(); |
| 2892 | } |
| 2893 | |
| 2894 | @Override |
| 2895 | public void run() { |
| 2896 | if (!mCancelled) { |
| 2897 | mEndCallback.run(); |
| 2898 | } |
| 2899 | } |
| 2900 | } |
Svetoslav | 7bfbbcb | 2013-10-10 13:36:23 -0700 | [diff] [blame] | 2901 | |
| 2902 | private static final class PrintSpoolerProvider implements ServiceConnection { |
| 2903 | private final Context mContext; |
| 2904 | private final Runnable mCallback; |
| 2905 | |
| 2906 | private PrintSpoolerService mSpooler; |
| 2907 | |
| 2908 | public PrintSpoolerProvider(Context context, Runnable callback) { |
| 2909 | mContext = context; |
| 2910 | mCallback = callback; |
| 2911 | Intent intent = new Intent(mContext, PrintSpoolerService.class); |
| 2912 | mContext.bindService(intent, this, 0); |
| 2913 | } |
| 2914 | |
| 2915 | public PrintSpoolerService getSpooler() { |
| 2916 | return mSpooler; |
| 2917 | } |
| 2918 | |
| 2919 | public void destroy() { |
| 2920 | if (mSpooler != null) { |
| 2921 | mContext.unbindService(this); |
| 2922 | } |
| 2923 | } |
| 2924 | |
| 2925 | @Override |
| 2926 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 2927 | mSpooler = ((PrintSpoolerService.PrintSpooler) service).getService(); |
| 2928 | if (mSpooler != null) { |
| 2929 | mCallback.run(); |
| 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | @Override |
| 2934 | public void onServiceDisconnected(ComponentName name) { |
| 2935 | /* do noting - we are in the same process */ |
| 2936 | } |
| 2937 | } |
Svetoslav Ganov | 858a185 | 2013-10-17 22:20:40 -0700 | [diff] [blame] | 2938 | |
| 2939 | private static final class PrintDocumentAdapterObserver |
| 2940 | extends IPrintDocumentAdapterObserver.Stub { |
| 2941 | private final WeakReference<PrintJobConfigActivity> mWeakActvity; |
| 2942 | |
| 2943 | public PrintDocumentAdapterObserver(PrintJobConfigActivity activity) { |
| 2944 | mWeakActvity = new WeakReference<PrintJobConfigActivity>(activity); |
| 2945 | } |
| 2946 | |
| 2947 | @Override |
| 2948 | public void onDestroy() { |
| 2949 | final PrintJobConfigActivity activity = mWeakActvity.get(); |
| 2950 | if (activity != null) { |
| 2951 | activity.mController.mHandler.post(new Runnable() { |
| 2952 | @Override |
| 2953 | public void run() { |
| 2954 | if (activity.mController != null) { |
| 2955 | activity.mController.cancel(); |
| 2956 | } |
| 2957 | if (activity.mEditor != null) { |
| 2958 | activity.mEditor.cancel(); |
| 2959 | } |
| 2960 | activity.finish(); |
| 2961 | } |
| 2962 | }); |
| 2963 | } |
| 2964 | } |
| 2965 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 2966 | } |