PrintAttributes.java revision b2420c917bf4092f44a2270d7e88ee2380f72080
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
17package android.print;
18
19import android.content.pm.PackageManager;
20import android.content.pm.PackageManager.NameNotFoundException;
21import android.content.res.Resources.NotFoundException;
22import android.os.Parcel;
23import android.os.Parcelable;
24import android.text.TextUtils;
25import android.util.ArrayMap;
26import android.util.Log;
27
28import com.android.internal.R;
29
30import java.util.Map;
31
32/**
33 * This class represents the attributes of a print job.
34 */
35public final class PrintAttributes implements Parcelable {
36    /** Color mode: Monochrome color scheme, for example one color is used. */
37    public static final int COLOR_MODE_MONOCHROME = 1 << 0;
38    /** Color mode: Color color scheme, for example many colors are used. */
39    public static final int COLOR_MODE_COLOR = 1 << 1;
40
41    private static final int VALID_COLOR_MODES =
42            COLOR_MODE_MONOCHROME | COLOR_MODE_COLOR;
43
44    private MediaSize mMediaSize;
45    private Resolution mResolution;
46    private Margins mMinMargins;
47
48    private int mColorMode;
49
50    PrintAttributes() {
51        /* hide constructor */
52    }
53
54    private PrintAttributes(Parcel parcel) {
55        mMediaSize = (parcel.readInt() ==  1) ? MediaSize.createFromParcel(parcel) : null;
56        mResolution = (parcel.readInt() ==  1) ? Resolution.createFromParcel(parcel) : null;
57        mMinMargins = (parcel.readInt() ==  1) ? Margins.createFromParcel(parcel) : null;
58        mColorMode = parcel.readInt();
59    }
60
61    /**
62     * Gets the media size.
63     *
64     * @return The media size or <code>null</code> if not set.
65     */
66    public MediaSize getMediaSize() {
67        return mMediaSize;
68    }
69
70    /**
71     * Sets the media size.
72     *
73     * @param The media size.
74     *
75     * @hide
76     */
77    public void setMediaSize(MediaSize mediaSize) {
78        mMediaSize = mediaSize;
79    }
80
81    /**
82     * Gets the resolution.
83     *
84     * @return The resolution or <code>null</code> if not set.
85     */
86    public Resolution getResolution() {
87        return mResolution;
88    }
89
90    /**
91     * Sets the resolution.
92     *
93     * @param The resolution.
94     *
95     * @hide
96     */
97    public void setResolution(Resolution resolution) {
98        mResolution = resolution;
99    }
100
101    /**
102     * Gets the minimal margins. If the content does not fit
103     * these margins it will be clipped.
104     *
105     * @return The margins or <code>null</code> if not set.
106     */
107    public Margins getMinMargins() {
108        return mMinMargins;
109    }
110
111    /**
112     * Sets the minimal margins. If the content does not fit
113     * these margins it will be clipped.
114     *
115     * @param The margins.
116     *
117     * @hide
118     */
119    public void setMinMargins(Margins margins) {
120        mMinMargins = margins;
121    }
122
123    /**
124     * Gets the color mode.
125     *
126     * @return The color mode or zero if not set.
127     *
128     * @see #COLOR_MODE_COLOR
129     * @see #COLOR_MODE_MONOCHROME
130     */
131    public int getColorMode() {
132        return mColorMode;
133    }
134
135    /**
136     * Sets the color mode.
137     *
138     * @param The color mode.
139     *
140     * @see #COLOR_MODE_MONOCHROME
141     * @see #COLOR_MODE_COLOR
142     *
143     * @hide
144     */
145    public void setColorMode(int colorMode) {
146        enforceValidColorMode(colorMode);
147        mColorMode = colorMode;
148    }
149
150    @Override
151    public void writeToParcel(Parcel parcel, int flags) {
152        if (mMediaSize != null) {
153            parcel.writeInt(1);
154            mMediaSize.writeToParcel(parcel);
155        } else {
156            parcel.writeInt(0);
157        }
158        if (mResolution != null) {
159            parcel.writeInt(1);
160            mResolution.writeToParcel(parcel);
161        } else {
162            parcel.writeInt(0);
163        }
164        if (mMinMargins != null) {
165            parcel.writeInt(1);
166            mMinMargins.writeToParcel(parcel);
167        } else {
168            parcel.writeInt(0);
169        }
170        parcel.writeInt(mColorMode);
171    }
172
173    @Override
174    public int describeContents() {
175        return 0;
176    }
177
178    @Override
179    public int hashCode() {
180        final int prime = 31;
181        int result = 1;
182        result = prime * result + mColorMode;
183        result = prime * result + ((mMinMargins == null) ? 0 : mMinMargins.hashCode());
184        result = prime * result + ((mMediaSize == null) ? 0 : mMediaSize.hashCode());
185        result = prime * result + ((mResolution == null) ? 0 : mResolution.hashCode());
186        return result;
187    }
188
189    @Override
190    public boolean equals(Object obj) {
191        if (this == obj) {
192            return true;
193        }
194        if (obj == null) {
195            return false;
196        }
197        if (getClass() != obj.getClass()) {
198            return false;
199        }
200        PrintAttributes other = (PrintAttributes) obj;
201        if (mColorMode != other.mColorMode) {
202            return false;
203        }
204        if (mMinMargins == null) {
205            if (other.mMinMargins != null) {
206                return false;
207            }
208        } else if (!mMinMargins.equals(other.mMinMargins)) {
209            return false;
210        }
211        if (mMediaSize == null) {
212            if (other.mMediaSize != null) {
213                return false;
214            }
215        } else if (!mMediaSize.equals(other.mMediaSize)) {
216            return false;
217        }
218        if (mResolution == null) {
219            if (other.mResolution != null) {
220                return false;
221            }
222        } else if (!mResolution.equals(other.mResolution)) {
223            return false;
224        }
225        return true;
226    }
227
228    @Override
229    public String toString() {
230        StringBuilder builder = new StringBuilder();
231        builder.append("PrintAttributes{");
232        builder.append("mediaSize: ").append(mMediaSize);
233        if (mMediaSize != null) {
234            builder.append(", orientation: ").append(mMediaSize.isPortrait()
235                    ? "portrait" : "landscape");
236        } else {
237            builder.append(", orientation: ").append("null");
238        }
239        builder.append(", resolution: ").append(mResolution);
240        builder.append(", minMargins: ").append(mMinMargins);
241        builder.append(", colorMode: ").append(colorModeToString(mColorMode));
242        builder.append("}");
243        return builder.toString();
244    }
245
246    /** @hide */
247    public void clear() {
248        mMediaSize = null;
249        mResolution = null;
250        mMinMargins = null;
251        mColorMode = 0;
252    }
253
254    /**
255     * @hide
256     */
257    public void copyFrom(PrintAttributes other) {
258        mMediaSize = other.mMediaSize;
259        mResolution = other.mResolution;
260        mMinMargins = other.mMinMargins;
261        mColorMode = other.mColorMode;
262    }
263
264    /**
265     * This class specifies a supported media size. Media size is the
266     * dimension of the media on which the content is printed. For
267     * example, the {@link #NA_LETTER} media size designates a page
268     * with size 8.5" x 11".
269     */
270    public static final class MediaSize {
271        private static final String LOG_TAG = "MediaSize";
272
273        private static final Map<String, MediaSize> sIdToMediaSizeMap =
274                new ArrayMap<String, MediaSize>();
275
276        /**
277         * Unknown media size in portrait mode.
278         * <p>
279         * <strong>Note: </strong>This is for specifying orientation without media
280         * size. You should not use the dimensions reported by this class.
281         * </p>
282         */
283        public static final MediaSize UNKNOWN_PORTRAIT =
284                new MediaSize("UNKNOWN_PORTRAIT", "android",
285                        R.string.mediasize_unknown_portrait, 1, Integer.MAX_VALUE);
286
287        /**
288         * Unknown media size in landscape mode.
289         * <p>
290         * <strong>Note: </strong>This is for specifying orientation without media
291         * size. You should not use the dimensions reported by this class.
292         * </p>
293         */
294        public static final MediaSize UNKNOWN_LANDSCAPE =
295                new MediaSize("UNKNOWN_LANDSCAPE", "android",
296                        R.string.mediasize_unknown_landscape, Integer.MAX_VALUE, 1);
297
298        // ISO sizes
299
300        /** ISO A0 media size: 841mm x 1189mm (33.11" x 46.81") */
301        public static final MediaSize ISO_A0 =
302                new MediaSize("ISO_A0", "android", R.string.mediasize_iso_a0, 33110, 46810);
303        /** ISO A1 media size: 594mm x 841mm (23.39" x 33.11") */
304        public static final MediaSize ISO_A1 =
305                new MediaSize("ISO_A1", "android", R.string.mediasize_iso_a1, 23390, 33110);
306        /** ISO A2 media size: 420mm x 594mm (16.54" x 23.39") */
307        public static final MediaSize ISO_A2 =
308                new MediaSize("ISO_A2", "android", R.string.mediasize_iso_a2, 16540, 23390);
309        /** ISO A3 media size: 297mm x 420mm (11.69" x 16.54") */
310        public static final MediaSize ISO_A3 =
311                new MediaSize("ISO_A3", "android", R.string.mediasize_iso_a3, 11690, 16540);
312        /** ISO A4 media size: 210mm x 297mm (8.27" x 11.69") */
313        public static final MediaSize ISO_A4 =
314                new MediaSize("ISO_A4", "android", R.string.mediasize_iso_a4, 8270, 11690);
315        /** ISO A5 media size: 148mm x 210mm (5.83" x 8.27") */
316        public static final MediaSize ISO_A5 =
317                new MediaSize("ISO_A5", "android", R.string.mediasize_iso_a5, 5830, 8270);
318        /** ISO A6 media size: 105mm x 148mm (4.13" x 5.83") */
319        public static final MediaSize ISO_A6 =
320                new MediaSize("ISO_A6", "android", R.string.mediasize_iso_a6, 4130, 5830);
321        /** ISO A7 media size: 74mm x 105mm (2.91" x 4.13") */
322        public static final MediaSize ISO_A7 =
323                new MediaSize("ISO_A7", "android", R.string.mediasize_iso_a7, 2910, 4130);
324        /** ISO A8 media size: 52mm x 74mm (2.05" x 2.91") */
325        public static final MediaSize ISO_A8 =
326                new MediaSize("ISO_A8", "android", R.string.mediasize_iso_a8, 2050, 2910);
327        /** ISO A9 media size: 37mm x 52mm (1.46" x 2.05") */
328        public static final MediaSize ISO_A9 =
329                new MediaSize("ISO_A9", "android", R.string.mediasize_iso_a9, 1460, 2050);
330        /** ISO A10 media size: 26mm x 37mm (1.02" x 1.46") */
331        public static final MediaSize ISO_A10 =
332                new MediaSize("ISO_A10", "android", R.string.mediasize_iso_a10, 1020, 1460);
333
334        /** ISO B0 media size: 1000mm x 1414mm (39.37" x 55.67") */
335        public static final MediaSize ISO_B0 =
336                new MediaSize("ISO_B0", "android", R.string.mediasize_iso_b0, 39370, 55670);
337        /** ISO B1 media size: 707mm x 1000mm (27.83" x 39.37") */
338        public static final MediaSize ISO_B1 =
339                new MediaSize("ISO_B1", "android", R.string.mediasize_iso_b1, 27830, 39370);
340        /** ISO B2 media size: 500mm x 707mm (19.69" x 27.83") */
341        public static final MediaSize ISO_B2 =
342                new MediaSize("ISO_B2", "android", R.string.mediasize_iso_b2, 19690, 27830);
343        /** ISO B3 media size: 353mm x 500mm (13.90" x 19.69") */
344        public static final MediaSize ISO_B3 =
345                new MediaSize("ISO_B3", "android", R.string.mediasize_iso_b3, 13900, 19690);
346        /** ISO B4 media size: 250mm x 353mm (9.84" x 13.90") */
347        public static final MediaSize ISO_B4 =
348                new MediaSize("ISO_B4", "android", R.string.mediasize_iso_b4, 9840, 13900);
349        /** ISO B5 media size: 176mm x 250mm (6.93" x 9.84") */
350        public static final MediaSize ISO_B5 =
351                new MediaSize("ISO_B5", "android", R.string.mediasize_iso_b5, 6930, 9840);
352        /** ISO B6 media size: 125mm x 176mm (4.92" x 6.93") */
353        public static final MediaSize ISO_B6 =
354                new MediaSize("ISO_B6", "android", R.string.mediasize_iso_b6, 4920, 6930);
355        /** ISO B7 media size: 88mm x 125mm (3.46" x 4.92") */
356        public static final MediaSize ISO_B7 =
357                new MediaSize("ISO_B7", "android", R.string.mediasize_iso_b7, 3460, 4920);
358        /** ISO B8 media size: 62mm x 88mm (2.44" x 3.46") */
359        public static final MediaSize ISO_B8 =
360                new MediaSize("ISO_B8", "android", R.string.mediasize_iso_b8, 2440, 3460);
361        /** ISO B9 media size: 44mm x 62mm (1.73" x 2.44") */
362        public static final MediaSize ISO_B9 =
363                new MediaSize("ISO_B9", "android", R.string.mediasize_iso_b9, 1730, 2440);
364        /** ISO B10 media size: 31mm x 44mm (1.22" x 1.73") */
365        public static final MediaSize ISO_B10 =
366                new MediaSize("ISO_B10", "android", R.string.mediasize_iso_b10, 1220, 1730);
367
368        /** ISO C0 media size: 917mm x 1297mm (36.10" x 51.06") */
369        public static final MediaSize ISO_C0 =
370                new MediaSize("ISO_C0", "android", R.string.mediasize_iso_c0, 36100, 51060);
371        /** ISO C1 media size: 648mm x 917mm (25.51" x 36.10") */
372        public static final MediaSize ISO_C1 =
373                new MediaSize("ISO_C1", "android", R.string.mediasize_iso_c1, 25510, 36100);
374        /** ISO C2 media size: 458mm x 648mm (18.03" x 25.51") */
375        public static final MediaSize ISO_C2 =
376                new MediaSize("ISO_C2", "android", R.string.mediasize_iso_c2, 18030, 25510);
377        /** ISO C3 media size: 324mm x 458mm (12.76" x 18.03") */
378        public static final MediaSize ISO_C3 =
379                new MediaSize("ISO_C3", "android", R.string.mediasize_iso_c3, 12760, 18030);
380        /** ISO C4 media size: 229mm x 324mm (9.02" x 12.76") */
381        public static final MediaSize ISO_C4 =
382                new MediaSize("ISO_C4", "android", R.string.mediasize_iso_c4, 9020, 12760);
383        /** ISO C5 media size: 162mm x 229mm (6.38" x 9.02") */
384        public static final MediaSize ISO_C5 =
385                new MediaSize("ISO_C5", "android", R.string.mediasize_iso_c5, 6380, 9020);
386        /** ISO C6 media size: 114mm x 162mm (4.49" x 6.38") */
387        public static final MediaSize ISO_C6 =
388                new MediaSize("ISO_C6", "android", R.string.mediasize_iso_c6, 4490, 6380);
389        /** ISO C7 media size: 81mm x 114mm (3.19" x 4.49") */
390        public static final MediaSize ISO_C7 =
391                new MediaSize("ISO_C7", "android", R.string.mediasize_iso_c7, 3190, 4490);
392        /** ISO C8 media size: 57mm x 81mm (2.24" x 3.19") */
393        public static final MediaSize ISO_C8 =
394                new MediaSize("ISO_C8", "android", R.string.mediasize_iso_c8, 2240, 3190);
395        /** ISO C9 media size: 40mm x 57mm (1.57" x 2.24") */
396        public static final MediaSize ISO_C9 =
397                new MediaSize("ISO_C9", "android", R.string.mediasize_iso_c9, 1570, 2240);
398        /** ISO C10 media size: 28mm x 40mm (1.10" x 1.57") */
399        public static final MediaSize ISO_C10 =
400                new MediaSize("ISO_C10", "android", R.string.mediasize_iso_c10, 1100, 1570);
401
402        // North America
403
404        /** North America Letter media size: 8.5" x 11" (279mm x 216mm) */
405        public static final MediaSize NA_LETTER =
406                new MediaSize("NA_LETTER", "android", R.string.mediasize_na_letter, 8500, 11000);
407        /** North America Government-Letter media size: 8.0" x 10.5" (203mm x 267mm) */
408        public static final MediaSize NA_GOVT_LETTER =
409                new MediaSize("NA_GOVT_LETTER", "android",
410                        R.string.mediasize_na_gvrnmt_letter, 8000, 10500);
411        /** North America Legal media size: 8.5" x 14" (216mm x 356mm) */
412        public static final MediaSize NA_LEGAL =
413                new MediaSize("NA_LEGAL", "android", R.string.mediasize_na_legal, 8500, 14000);
414        /** North America Junior Legal media size: 8.0" x 5.0" (203mm × 127mm) */
415        public static final MediaSize NA_JUNIOR_LEGAL =
416                new MediaSize("NA_JUNIOR_LEGAL", "android",
417                        R.string.mediasize_na_junior_legal, 8000, 5000);
418        /** North America Ledger media size: 17" x 11" (432mm × 279mm) */
419        public static final MediaSize NA_LEDGER =
420                new MediaSize("NA_LEDGER", "android", R.string.mediasize_na_ledger, 17000, 11000);
421        /** North America Tabloid media size: 11" x 17" (279mm × 432mm) */
422        public static final MediaSize NA_TABLOID =
423                new MediaSize("NA_TABLOID", "android",
424                        R.string.mediasize_na_tabloid, 11000, 17000);
425        /** North America Index Card 3x5 media size: 3" x 5" (76mm x 127mm) */
426        public static final MediaSize NA_INDEX_3X5 =
427                new MediaSize("NA_INDEX_3X5", "android",
428                        R.string.mediasize_na_index_3x5, 3000, 5000);
429        /** North America Index Card 4x6 media size: 4" x 6" (102mm x 152mm) */
430        public static final MediaSize NA_INDEX_4X6 =
431                new MediaSize("NA_INDEX_4X6", "android",
432                        R.string.mediasize_na_index_4x6, 4000, 6000);
433        /** North America Index Card 5x8 media size: 5" x 8" (127mm x 203mm) */
434        public static final MediaSize NA_INDEX_5X8 =
435                new MediaSize("NA_INDEX_5X8", "android",
436                        R.string.mediasize_na_index_5x8, 5000, 8000);
437        /** North America Monarch media size: 7.25" x 10.5" (184mm x 267mm) */
438        public static final MediaSize NA_MONARCH =
439                new MediaSize("NA_MONARCH", "android",
440                        R.string.mediasize_na_monarch, 7250, 10500);
441        /** North America Quarto media size: 8" x 10" (203mm x 254mm) */
442        public static final MediaSize NA_QUARTO =
443                new MediaSize("NA_QUARTO", "android",
444                        R.string.mediasize_na_quarto, 8000, 10000);
445        /** North America Foolscap media size: 8" x 13" (203mm x 330mm) */
446        public static final MediaSize NA_FOOLSCAP =
447                new MediaSize("NA_FOOLSCAP", "android",
448                        R.string.mediasize_na_foolscap, 8000, 13000);
449
450        // Chinese
451
452        /** Chinese ROC 8K media size: 270mm x 390mm (10.629" x 15.3543") */
453        public static final MediaSize ROC_8K =
454                new MediaSize("ROC_8K", "android",
455                        R.string.mediasize_chinese_roc_8k, 10629, 15354);
456        /** Chinese ROC 16K media size: 195mm x 270mm (7.677" x 10.629") */
457        public static final MediaSize ROC_16K =
458                new MediaSize("ROC_16K", "android",
459                        R.string.mediasize_chinese_roc_16k, 7677, 10629);
460
461        /** Chinese PRC 1 media size: 102mm x 165mm (4.015" x 6.496") */
462        public static final MediaSize PRC_1 =
463                new MediaSize("PRC_1", "android",
464                        R.string.mediasize_chinese_prc_1, 4015, 6496);
465        /** Chinese PRC 2 media size: 102mm x 176mm (4.015" x 6.929") */
466        public static final MediaSize PRC_2 =
467                new MediaSize("PRC_2", "android",
468                        R.string.mediasize_chinese_prc_2, 4015, 6929);
469        /** Chinese PRC 3 media size: 125mm x 176mm (4.921" x 6.929") */
470        public static final MediaSize PRC_3 =
471                new MediaSize("PRC_3", "android",
472                        R.string.mediasize_chinese_prc_3, 4921, 6929);
473        /** Chinese PRC 4 media size: 110mm x 208mm (4.330" x 8.189") */
474        public static final MediaSize PRC_4 =
475                new MediaSize("PRC_4", "android",
476                        R.string.mediasize_chinese_prc_4, 4330, 8189);
477        /** Chinese PRC 5 media size: 110mm x 220mm (4.330" x 8.661") */
478        public static final MediaSize PRC_5 =
479                new MediaSize("PRC_5", "android",
480                        R.string.mediasize_chinese_prc_5, 4330, 8661);
481        /** Chinese PRC 6 media size: 120mm x 320mm (4.724" x 12.599") */
482        public static final MediaSize PRC_6 =
483                new MediaSize("PRC_6", "android",
484                        R.string.mediasize_chinese_prc_6, 4724, 12599);
485        /** Chinese PRC 7 media size: 160mm x 230mm (6.299" x 9.055") */
486        public static final MediaSize PRC_7 =
487                new MediaSize("PRC_7", "android",
488                        R.string.mediasize_chinese_prc_7, 6299, 9055);
489        /** Chinese PRC 8 media size: 120mm x 309mm (4.724" x 12.165") */
490        public static final MediaSize PRC_8 =
491                new MediaSize("PRC_8", "android",
492                        R.string.mediasize_chinese_prc_8, 4724, 12165);
493        /** Chinese PRC 9 media size: 229mm x 324mm (9.016" x 12.756") */
494        public static final MediaSize PRC_9 =
495                new MediaSize("PRC_9", "android",
496                        R.string.mediasize_chinese_prc_9, 9016, 12756);
497        /** Chinese PRC 10 media size: 324mm x 458mm (12.756" x 18.032") */
498        public static final MediaSize PRC_10 =
499                new MediaSize("PRC_10", "android",
500                        R.string.mediasize_chinese_prc_10, 12756, 18032);
501
502        /** Chinese PRC 16k media size: 146mm x 215mm (5.749" x 8.465") */
503        public static final MediaSize PRC_16K =
504                new MediaSize("PRC_16K", "android",
505                        R.string.mediasize_chinese_prc_16k, 5749, 8465);
506        /** Chinese Pa Kai media size: 267mm x 389mm (10.512" x 15.315") */
507        public static final MediaSize OM_PA_KAI =
508                new MediaSize("OM_PA_KAI", "android",
509                        R.string.mediasize_chinese_om_pa_kai, 10512, 15315);
510        /** Chinese Dai Pa Kai media size: 275mm x 395mm (10.827" x 15.551") */
511        public static final MediaSize OM_DAI_PA_KAI =
512                new MediaSize("OM_DAI_PA_KAI", "android",
513                        R.string.mediasize_chinese_om_dai_pa_kai, 10827, 15551);
514        /** Chinese Jurro Ku Kai media size: 198mm x 275mm (7.796" x 10.827") */
515        public static final MediaSize OM_JUURO_KU_KAI =
516                new MediaSize("OM_JUURO_KU_KAI", "android",
517                        R.string.mediasize_chinese_om_jurro_ku_kai, 7796, 10827);
518
519        // Japanese
520
521        /** Japanese JIS B10 media size: 32mm x 45mm (1.259" x 1.772") */
522        public static final MediaSize JIS_B10 =
523                new MediaSize("JIS_B10", "android",
524                        R.string.mediasize_japanese_jis_b10, 1259, 1772);
525        /** Japanese JIS B9 media size: 45mm x 64mm (1.772" x 2.52") */
526        public static final MediaSize JIS_B9 =
527                new MediaSize("JIS_B9", "android",
528                        R.string.mediasize_japanese_jis_b9, 1772, 2520);
529        /** Japanese JIS B8 media size: 64mm x 91mm (2.52" x 3.583") */
530        public static final MediaSize JIS_B8 =
531                new MediaSize("JIS_B8", "android",
532                        R.string.mediasize_japanese_jis_b8, 2520, 3583);
533        /** Japanese JIS B7 media size: 91mm x 128mm (3.583" x 5.049") */
534        public static final MediaSize JIS_B7 =
535                new MediaSize("JIS_B7", "android",
536                        R.string.mediasize_japanese_jis_b7, 3583, 5049);
537        /** Japanese JIS B6 media size: 128mm x 182mm (5.049" x 7.165") */
538        public static final MediaSize JIS_B6 =
539                new MediaSize("JIS_B6", "android",
540                        R.string.mediasize_japanese_jis_b6, 5049, 7165);
541        /** Japanese JIS B5 media size: 182mm x 257mm (7.165" x 10.118") */
542        public static final MediaSize JIS_B5 =
543                new MediaSize("JIS_B5", "android",
544                        R.string.mediasize_japanese_jis_b5, 7165, 10118);
545        /** Japanese JIS B4 media size: 257mm x 364mm (10.118" x 14.331") */
546        public static final MediaSize JIS_B4 =
547                new MediaSize("JIS_B4", "android",
548                        R.string.mediasize_japanese_jis_b4, 10118, 14331);
549        /** Japanese JIS B3 media size: 364mm x 515mm (14.331" x 20.276") */
550        public static final MediaSize JIS_B3 =
551                new MediaSize("JIS_B3", "android",
552                        R.string.mediasize_japanese_jis_b3, 14331, 20276);
553        /** Japanese JIS B2 media size: 515mm x 728mm (20.276" x 28.661") */
554        public static final MediaSize JIS_B2 =
555                new MediaSize("JIS_B2", "android",
556                        R.string.mediasize_japanese_jis_b2, 20276, 28661);
557        /** Japanese JIS B1 media size: 728mm x 1030mm (28.661" x 40.551") */
558        public static final MediaSize JIS_B1 =
559                new MediaSize("JIS_B1", "android",
560                        R.string.mediasize_japanese_jis_b1, 28661, 40551);
561        /** Japanese JIS B0 media size: 1030mm x 1456mm (40.551" x 57.323") */
562        public static final MediaSize JIS_B0 =
563                new MediaSize("JIS_B0", "android",
564                        R.string.mediasize_japanese_jis_b0, 40551, 57323);
565
566        /** Japanese JIS Exec media size: 216mm x 330mm (8.504" x 12.992") */
567        public static final MediaSize JIS_EXEC =
568                new MediaSize("JIS_EXEC", "android",
569                        R.string.mediasize_japanese_jis_exec, 8504, 12992);
570
571        /** Japanese Chou4 media size: 90mm x 205mm (3.543" x 8.071") */
572        public static final MediaSize JPN_CHOU4 =
573                new MediaSize("JPN_CHOU4", "android",
574                        R.string.mediasize_japanese_chou4, 3543, 8071);
575        /** Japanese Chou3 media size: 120mm x 235mm (4.724" x 9.252") */
576        public static final MediaSize JPN_CHOU3 =
577                new MediaSize("JPN_CHOU3", "android",
578                        R.string.mediasize_japanese_chou3, 4724, 9252);
579        /** Japanese Chou2 media size: 111.1mm x 146mm (4.374" x 5.748") */
580        public static final MediaSize JPN_CHOU2 =
581                new MediaSize("JPN_CHOU2", "android",
582                        R.string.mediasize_japanese_chou2, 4374, 5748);
583
584        /** Japanese Hagaki media size: 100mm x 148mm (3.937" x 5.827") */
585        public static final MediaSize JPN_HAGAKI =
586                new MediaSize("JPN_HAGAKI", "android",
587                        R.string.mediasize_japanese_hagaki, 3937, 5827);
588        /** Japanese Oufuku media size: 148mm x 200mm (5.827" x 7.874") */
589        public static final MediaSize JPN_OUFUKU =
590                new MediaSize("JPN_OUFUKU", "android",
591                        R.string.mediasize_japanese_oufuku, 5827, 7874);
592
593        /** Japanese Kahu media size: 240mm x 322.1mm (9.449" x 12.681") */
594        public static final MediaSize JPN_KAHU =
595                new MediaSize("JPN_KAHU", "android",
596                        R.string.mediasize_japanese_kahu, 9449, 12681);
597        /** Japanese Kaku2 media size: 240mm x 332mm (9.449" x 13.071") */
598        public static final MediaSize JPN_KAKU2 =
599                new MediaSize("JPN_KAKU2", "android",
600                        R.string.mediasize_japanese_kaku2, 9449, 13071);
601
602        /** Japanese You4 media size: 105mm x 235mm (4.134" x 9.252") */
603        public static final MediaSize JPN_YOU4 =
604                new MediaSize("JPN_YOU4", "android",
605                        R.string.mediasize_japanese_you4, 4134, 9252);
606
607        private final String mId;
608        /**@hide */
609        public final String mLabel;
610        /**@hide */
611        public final String mPackageName;
612        /**@hide */
613        public final int mLabelResId;
614        private final int mWidthMils;
615        private final int mHeightMils;
616
617        /**
618         * Creates a new instance. This is the preferred constructor since
619         * it enables the media size label to be shown in a localized fashion
620         * on a locale change.
621         *
622         * @param id The unique media size id.
623         * @param packageName The name of the creating package.
624         * @param labelResId The resource if of a human readable label.
625         * @param widthMils The width in mils (thousands of an inch).
626         * @param heightMils The height in mils (thousands of an inch).
627         *
628         * @throws IllegalArgumentException If the id is empty.
629         * @throws IllegalArgumentException If the label is empty.
630         * @throws IllegalArgumentException If the widthMils is less than or equal to zero.
631         * @throws IllegalArgumentException If the heightMils is less than or equal to zero.
632         *
633         * @hide
634         */
635        public MediaSize(String id, String packageName, int labelResId,
636                int widthMils, int heightMils) {
637            if (TextUtils.isEmpty(id)) {
638                throw new IllegalArgumentException("id cannot be empty.");
639            }
640            if (TextUtils.isEmpty(packageName)) {
641                throw new IllegalArgumentException("packageName cannot be empty.");
642            }
643            if (labelResId <= 0) {
644                throw new IllegalArgumentException("labelResId must be greater than zero.");
645            }
646            if (widthMils <= 0) {
647                throw new IllegalArgumentException("widthMils "
648                        + "cannot be less than or equal to zero.");
649            }
650            if (heightMils <= 0) {
651                throw new IllegalArgumentException("heightMils "
652                       + "cannot be less than or euqual to zero.");
653            }
654            mPackageName = packageName;
655            mId = id;
656            mLabelResId = labelResId;
657            mWidthMils = widthMils;
658            mHeightMils = heightMils;
659            mLabel = null;
660
661            // Build this mapping only for predefined media sizes.
662            sIdToMediaSizeMap.put(mId, this);
663        }
664
665        /**
666         * Creates a new instance.
667         *
668         * @param id The unique media size id. It is unique amongst other media sizes
669         *        supported by the printer.
670         * @param label The <strong>internationalized</strong> human readable label.
671         * @param widthMils The width in mils (thousands of an inch).
672         * @param heightMils The height in mils (thousands of an inch).
673         *
674         * @throws IllegalArgumentException If the id is empty.
675         * @throws IllegalArgumentException If the label is empty.
676         * @throws IllegalArgumentException If the widthMils is less than or equal to zero.
677         * @throws IllegalArgumentException If the heightMils is less than or equal to zero.
678         */
679        public MediaSize(String id, String label, int widthMils, int heightMils) {
680            if (TextUtils.isEmpty(id)) {
681                throw new IllegalArgumentException("id cannot be empty.");
682            }
683            if (TextUtils.isEmpty(label)) {
684                throw new IllegalArgumentException("label cannot be empty.");
685            }
686            if (widthMils <= 0) {
687                throw new IllegalArgumentException("widthMils "
688                        + "cannot be less than or equal to zero.");
689            }
690            if (heightMils <= 0) {
691                throw new IllegalArgumentException("heightMils "
692                       + "cannot be less than or euqual to zero.");
693            }
694            mId = id;
695            mLabel = label;
696            mWidthMils = widthMils;
697            mHeightMils = heightMils;
698            mLabelResId = 0;
699            mPackageName = null;
700        }
701
702        /** @hide */
703        public MediaSize(String id, String label, String packageName,
704                int widthMils, int heightMils, int labelResId) {
705            mPackageName = packageName;
706            mId = id;
707            mLabelResId = labelResId;
708            mWidthMils = widthMils;
709            mHeightMils = heightMils;
710            mLabel = label;
711        }
712
713        /**
714         * Gets the unique media size id. It is unique amongst other media sizes
715         * supported by the printer.
716         * <p>
717         * This id is defined by the client that generated the media size
718         * instance and should not be interpreted by other parties.
719         * </p>
720         *
721         * @return The unique media size id.
722         */
723        public String getId() {
724            return mId;
725        }
726
727        /**
728         * Gets the human readable media size label.
729         *
730         * @param packageManager The package manager for loading the label.
731         * @return The human readable label.
732         */
733        public String getLabel(PackageManager packageManager) {
734            if (!TextUtils.isEmpty(mPackageName) && mLabelResId > 0) {
735                try {
736                    return packageManager.getResourcesForApplication(
737                            mPackageName).getString(mLabelResId);
738                } catch (NotFoundException nfe) {
739                    Log.w(LOG_TAG, "Could not load resouce" + mLabelResId
740                            + " from package " + mPackageName);
741                } catch (NameNotFoundException nnfee) {
742                    Log.w(LOG_TAG, "Could not load resouce" + mLabelResId
743                            + " from package " + mPackageName);
744                }
745            }
746            return mLabel;
747        }
748
749        /**
750         * Gets the media width in mils (thousands of an inch).
751         *
752         * @return The media width.
753         */
754        public int getWidthMils() {
755            return mWidthMils;
756        }
757
758        /**
759         * Gets the media height in mils (thousands of an inch).
760         *
761         * @return The media height.
762         */
763        public int getHeightMils() {
764            return mHeightMils;
765        }
766
767        /**
768         * Gets whether this media size is in portrait which is the
769         * height is greater or equal to the width.
770         *
771         * @return True if the media size is in portrait, false if
772         * it is in landscape.
773         */
774        public boolean isPortrait() {
775            return mHeightMils >= mWidthMils;
776        }
777
778        /**
779         * Returns a new media size in a portrait orientation
780         * which is the height is the greater dimension.
781         *
782         * @return New instance in landscape orientation.
783         */
784        public MediaSize asPortrait() {
785            return new MediaSize(mId, mLabel, mPackageName,
786                    Math.min(mWidthMils, mHeightMils),
787                    Math.max(mWidthMils, mHeightMils),
788                    mLabelResId);
789        }
790
791        /**
792         * Returns a new media size in a landscape orientation
793         * which is the height is the lesser dimension.
794         *
795         * @return New instance in landscape orientation.
796         */
797        public MediaSize asLandscape() {
798            return new MediaSize(mId, mLabel, mPackageName,
799                    Math.max(mWidthMils, mHeightMils),
800                    Math.min(mWidthMils, mHeightMils),
801                    mLabelResId);
802        }
803
804        void writeToParcel(Parcel parcel) {
805            parcel.writeString(mId);
806            parcel.writeString(mLabel);
807            parcel.writeString(mPackageName);
808            parcel.writeInt(mWidthMils);
809            parcel.writeInt(mHeightMils);
810            parcel.writeInt(mLabelResId);
811        }
812
813        static MediaSize createFromParcel(Parcel parcel) {
814            return new MediaSize(
815                    parcel.readString(),
816                    parcel.readString(),
817                    parcel.readString(),
818                    parcel.readInt(),
819                    parcel.readInt(),
820                    parcel.readInt());
821        }
822
823        @Override
824        public int hashCode() {
825            final int prime = 31;
826            int result = 1;
827            result = prime * result + mWidthMils;
828            result = prime * result + mHeightMils;
829            return result;
830        }
831
832        @Override
833        public boolean equals(Object obj) {
834            if (this == obj) {
835                return true;
836            }
837            if (obj == null) {
838                return false;
839            }
840            if (getClass() != obj.getClass()) {
841                return false;
842            }
843            MediaSize other = (MediaSize) obj;
844            if (mWidthMils != other.mWidthMils) {
845                return false;
846            }
847            if (mHeightMils != other.mHeightMils) {
848                return false;
849            }
850            return true;
851        }
852
853        @Override
854        public String toString() {
855            StringBuilder builder = new StringBuilder();
856            builder.append("MediaSize{");
857            builder.append("id: ").append(mId);
858            builder.append(", label: ").append(mLabel);
859            builder.append(", packageName: ").append(mPackageName);
860            builder.append(", heightMils: ").append(mHeightMils);
861            builder.append(", widthMils: ").append(mWidthMils);
862            builder.append(", labelResId: ").append(mLabelResId);
863            builder.append("}");
864            return builder.toString();
865        }
866
867        /**
868         * Gets a standard media size given its id.
869         *
870         * @param id The media size id.
871         * @return The media size for the given id or null.
872         *
873         * @hide
874         */
875        public static MediaSize getStandardMediaSizeById(String id) {
876            return sIdToMediaSizeMap.get(id);
877        }
878    }
879
880    /**
881     * This class specifies a supported resolution in DPI (dots per inch).
882     * Resolution defines how many points with different color can be placed
883     * on one inch in horizontal or vertical direction of the target media.
884     * For example, a printer with 600DIP can produce higher quality images
885     * the one with 300DPI resolution.
886     */
887    public static final class Resolution {
888        private final String mId;
889        private final String mLabel;
890        private final int mHorizontalDpi;
891        private final int mVerticalDpi;
892
893        /**
894         * Creates a new instance.
895         *
896         * @param id The unique resolution id. It is unique amongst other resolutions
897         *        supported by the printer.
898         * @param label The <strong>internationalized</strong> human readable label.
899         * @param horizontalDpi The horizontal resolution in DPI (dots per inch).
900         * @param verticalDpi The vertical resolution in DPI (dots per inch).
901         *
902         * @throws IllegalArgumentException If the id is empty.
903         * @throws IllegalArgumentException If the label is empty.
904         * @throws IllegalArgumentException If the horizontalDpi is less than or equal to zero.
905         * @throws IllegalArgumentException If the verticalDpi is less than or equal to zero.
906         */
907        public Resolution(String id, String label, int horizontalDpi, int verticalDpi) {
908            if (TextUtils.isEmpty(id)) {
909                throw new IllegalArgumentException("id cannot be empty.");
910            }
911            if (TextUtils.isEmpty(label)) {
912                throw new IllegalArgumentException("label cannot be empty.");
913            }
914            if (horizontalDpi <= 0) {
915                throw new IllegalArgumentException("horizontalDpi "
916                        + "cannot be less than or equal to zero.");
917            }
918            if (verticalDpi <= 0) {
919                throw new IllegalArgumentException("verticalDpi"
920                       + " cannot be less than or equal to zero.");
921            }
922            mId = id;
923            mLabel = label;
924            mHorizontalDpi = horizontalDpi;
925            mVerticalDpi = verticalDpi;
926        }
927
928        /**
929         * Gets the unique resolution id. It is unique amongst other resolutions
930         * supported by the printer.
931         * <p>
932         * This id is defined by the client that generated the resolution
933         * instance and should not be interpreted by other parties.
934         * </p>
935         *
936         * @return The unique resolution id.
937         */
938        public String getId() {
939            return mId;
940        }
941
942        /**
943         * Gets the resolution human readable label.
944         *
945         * @return The human readable label.
946         */
947        public String getLabel() {
948            return mLabel;
949        }
950
951        /**
952         * Gets the horizontal resolution in DPI (dots per inch).
953         *
954         * @return The horizontal resolution.
955         */
956        public int getHorizontalDpi() {
957            return mHorizontalDpi;
958        }
959
960        /**
961         * Gets the vertical resolution in DPI (dots per inch).
962         *
963         * @return The vertical resolution.
964         */
965        public int getVerticalDpi() {
966            return mVerticalDpi;
967        }
968
969        void writeToParcel(Parcel parcel) {
970            parcel.writeString(mId);
971            parcel.writeString(mLabel);
972            parcel.writeInt(mHorizontalDpi);
973            parcel.writeInt(mVerticalDpi);
974        }
975
976        static Resolution createFromParcel(Parcel parcel) {
977            return new Resolution(
978                    parcel.readString(),
979                    parcel.readString(),
980                    parcel.readInt(),
981                    parcel.readInt());
982        }
983
984        @Override
985        public int hashCode() {
986            final int prime = 31;
987            int result = 1;
988            result = prime * result + mHorizontalDpi;
989            result = prime * result + mVerticalDpi;
990            return result;
991        }
992
993        @Override
994        public boolean equals(Object obj) {
995            if (this == obj) {
996                return true;
997            }
998            if (obj == null) {
999                return false;
1000            }
1001            if (getClass() != obj.getClass()) {
1002                return false;
1003            }
1004            Resolution other = (Resolution) obj;
1005            if (mHorizontalDpi != other.mHorizontalDpi) {
1006                return false;
1007            }
1008            if (mVerticalDpi != other.mVerticalDpi) {
1009                return false;
1010            }
1011            return true;
1012        }
1013
1014        @Override
1015        public String toString() {
1016            StringBuilder builder = new StringBuilder();
1017            builder.append("Resolution{");
1018            builder.append("id: ").append(mId);
1019            builder.append(", label: ").append(mLabel);
1020            builder.append(", horizontalDpi: ").append(mHorizontalDpi);
1021            builder.append(", verticalDpi: ").append(mVerticalDpi);
1022            builder.append("}");
1023            return builder.toString();
1024        }
1025    }
1026
1027    /**
1028     * This class specifies content margins. Margins define the white space
1029     * around the content where the left margin defines the amount of white
1030     * space on the left of the content and so on.
1031     */
1032    public static final class Margins {
1033        public static final Margins NO_MARGINS = new Margins(0,  0,  0,  0);
1034
1035        private final int mLeftMils;
1036        private final int mTopMils;
1037        private final int mRightMils;
1038        private final int mBottomMils;
1039
1040        /**
1041         * Creates a new instance.
1042         *
1043         * @param leftMils The left margin in mils (thousands of an inch).
1044         * @param topMils The top margin in mils (thousands of an inch).
1045         * @param rightMils The right margin in mils (thousands of an inch).
1046         * @param bottomMils The bottom margin in mils (thousands of an inch).
1047         */
1048        public Margins(int leftMils, int topMils, int rightMils, int bottomMils) {
1049            mTopMils = topMils;
1050            mLeftMils = leftMils;
1051            mRightMils = rightMils;
1052            mBottomMils = bottomMils;
1053        }
1054
1055        /**
1056         * Gets the left margin in mils (thousands of an inch).
1057         *
1058         * @return The left margin.
1059         */
1060        public int getLeftMils() {
1061            return mLeftMils;
1062        }
1063
1064        /**
1065         * Gets the top margin in mils (thousands of an inch).
1066         *
1067         * @return The top margin.
1068         */
1069        public int getTopMils() {
1070            return mTopMils;
1071        }
1072
1073        /**
1074         * Gets the right margin in mils (thousands of an inch).
1075         *
1076         * @return The right margin.
1077         */
1078        public int getRightMils() {
1079            return mRightMils;
1080        }
1081
1082        /**
1083         * Gets the bottom margin in mils (thousands of an inch).
1084         *
1085         * @return The bottom margin.
1086         */
1087        public int getBottomMils() {
1088            return mBottomMils;
1089        }
1090
1091        void writeToParcel(Parcel parcel) {
1092            parcel.writeInt(mLeftMils);
1093            parcel.writeInt(mTopMils);
1094            parcel.writeInt(mRightMils);
1095            parcel.writeInt(mBottomMils);
1096        }
1097
1098        static Margins createFromParcel(Parcel parcel) {
1099            return new Margins(
1100                    parcel.readInt(),
1101                    parcel.readInt(),
1102                    parcel.readInt(),
1103                    parcel.readInt());
1104        }
1105
1106        @Override
1107        public int hashCode() {
1108            final int prime = 31;
1109            int result = 1;
1110            result = prime * result + mBottomMils;
1111            result = prime * result + mLeftMils;
1112            result = prime * result + mRightMils;
1113            result = prime * result + mTopMils;
1114            return result;
1115        }
1116
1117        @Override
1118        public boolean equals(Object obj) {
1119            if (this == obj) {
1120                return true;
1121            }
1122            if (obj == null) {
1123                return false;
1124            }
1125            if (getClass() != obj.getClass()) {
1126                return false;
1127            }
1128            Margins other = (Margins) obj;
1129            if (mBottomMils != other.mBottomMils) {
1130                return false;
1131            }
1132            if (mLeftMils != other.mLeftMils) {
1133                return false;
1134            }
1135            if (mRightMils != other.mRightMils) {
1136                return false;
1137            }
1138            if (mTopMils != other.mTopMils) {
1139                return false;
1140            }
1141            return true;
1142        }
1143
1144        @Override
1145        public String toString() {
1146            StringBuilder builder = new StringBuilder();
1147            builder.append("Margins{");
1148            builder.append("leftMils: ").append(mLeftMils);
1149            builder.append(", topMils: ").append(mTopMils);
1150            builder.append(", rightMils: ").append(mRightMils);
1151            builder.append(", bottomMils: ").append(mBottomMils);
1152            builder.append("}");
1153            return builder.toString();
1154        }
1155    }
1156
1157    static String colorModeToString(int colorMode) {
1158        switch (colorMode) {
1159            case COLOR_MODE_MONOCHROME: {
1160                return "COLOR_MODE_MONOCHROME";
1161            }
1162            case COLOR_MODE_COLOR: {
1163                return "COLOR_MODE_COLOR";
1164            }
1165            default:
1166                return "COLOR_MODE_UNKNOWN";
1167        }
1168    }
1169
1170    static void enforceValidColorMode(int colorMode) {
1171        if ((colorMode & VALID_COLOR_MODES) == 0 && Integer.bitCount(colorMode) == 1) {
1172            throw new IllegalArgumentException("invalid color mode: " + colorMode);
1173        }
1174    }
1175
1176    /**
1177     * Builder for creating {@link PrintAttributes}.
1178     */
1179    public static final class Builder {
1180        private final PrintAttributes mAttributes = new PrintAttributes();
1181
1182        /**
1183         * Sets the media size.
1184         *
1185         * @param mediaSize The media size.
1186         * @return This builder.
1187         */
1188        public Builder setMediaSize(MediaSize mediaSize) {
1189            mAttributes.setMediaSize(mediaSize);
1190            return this;
1191        }
1192
1193        /**
1194         * Sets the resolution.
1195         *
1196         * @param resolution The resolution.
1197         * @return This builder.
1198         */
1199        public Builder setResolution(Resolution resolution) {
1200            mAttributes.setResolution(resolution);
1201            return this;
1202        }
1203
1204        /**
1205         * Sets the minimal margins. If the content does not fit
1206         * these margins it will be clipped.
1207         *
1208         * @param margins The margins.
1209         * @return This builder.
1210         */
1211        public Builder setMinMargins(Margins margins) {
1212            mAttributes.setMinMargins(margins);
1213            return this;
1214        }
1215
1216        /**
1217         * Sets the color mode.
1218         *
1219         * @param colorMode A valid color mode or zero.
1220         * @return This builder.
1221         *
1222         * @see PrintAttributes#COLOR_MODE_MONOCHROME
1223         * @see PrintAttributes#COLOR_MODE_COLOR
1224         */
1225        public Builder setColorMode(int colorMode) {
1226            if (Integer.bitCount(colorMode) > 1) {
1227                throw new IllegalArgumentException("can specify at most one colorMode bit.");
1228            }
1229            mAttributes.setColorMode(colorMode);
1230            return this;
1231        }
1232
1233        /**
1234         * Creates a new {@link PrintAttributes} instance.
1235         *
1236         * @return The new instance.
1237         */
1238        public PrintAttributes build() {
1239            return mAttributes;
1240        }
1241    }
1242
1243    public static final Parcelable.Creator<PrintAttributes> CREATOR =
1244            new Creator<PrintAttributes>() {
1245        @Override
1246        public PrintAttributes createFromParcel(Parcel parcel) {
1247            return new PrintAttributes(parcel);
1248        }
1249
1250        @Override
1251        public PrintAttributes[] newArray(int size) {
1252            return new PrintAttributes[size];
1253        }
1254    };
1255}
1256