Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  * Copyright (C) 2016 Mopria Alliance, Inc.
      4  *
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *      http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 package com.android.bips.jni;
     19 
     20 import android.annotation.SuppressLint;
     21 import android.content.Context;
     22 import android.print.PrintAttributes;
     23 
     24 import com.android.bips.R;
     25 
     26 import java.util.ArrayList;
     27 import java.util.Collection;
     28 import java.util.HashMap;
     29 import java.util.Map;
     30 
     31 /** Supported media sizes */
     32 public class MediaSizes {
     33     private static final String ISO_A4 = "iso_a4_210x297mm";
     34     private static final String LETTER = "na_letter_8.5x11in";
     35     private static final String LEGAL = "na_legal_8.5x14in";
     36     private static final String PHOTO_4x6in = "na_index-4x6_4x6in";
     37     private static final String PHOTO_5x7 = "na_5x7_5x7in";
     38     private static final String NA_LEDGER_11X17 = "na_ledger_11x17in";
     39     private static final String ISO_A3 = "iso_a3_297x420mm";
     40     private static final String ISO_A5 = "iso_a5_148x210mm";
     41     private static final String JPN_HAGAKI = "jpn_hagaki_100x148mm";
     42     private static final String OM_DSC_PHOTO = "om_dsc-photo_89x119mm";
     43     private static final String OM_CARD = "om_card_54x86mm";
     44     private static final String JIS_B4 = "jis_b4_257x364mm";
     45     private static final String JIS_B5 = "jis_b5_182x257mm";
     46     private static final String OE_PHOTO_L = "oe_photo-l_3.5x5in";
     47     private static final String NA_GOVT_LETTER = "na_govt-letter_8x10in";
     48 
     49     /** The backend string name for the default media size */
     50     static final String DEFAULT_MEDIA_NAME = ISO_A4;
     51 
     52     @SuppressLint("UseSparseArrays")
     53     private static final Map<Integer, String> sCodeToStringMap = new HashMap<>();
     54 
     55     /** The default string names for default media sizes */
     56     static final Collection<String> DEFAULT_MEDIA_NAMES = new ArrayList<>();
     57     private static MediaSizes sInstance;
     58 
     59     static {
     60         DEFAULT_MEDIA_NAMES.add(ISO_A4);
     61         DEFAULT_MEDIA_NAMES.add(LETTER);
     62         DEFAULT_MEDIA_NAMES.add(PHOTO_4x6in);
     63         DEFAULT_MEDIA_NAMES.add(PHOTO_5x7);
     64     }
     65 
     66     static {
     67         sCodeToStringMap.put(2, LETTER);
     68         sCodeToStringMap.put(3, LEGAL);
     69         sCodeToStringMap.put(7, NA_GOVT_LETTER);
     70         sCodeToStringMap.put(11, NA_LEDGER_11X17);
     71         sCodeToStringMap.put(25, ISO_A5);
     72         sCodeToStringMap.put(26, ISO_A4);
     73         sCodeToStringMap.put(27, ISO_A3);
     74         sCodeToStringMap.put(45, JIS_B5);
     75         sCodeToStringMap.put(46, JIS_B4);
     76         sCodeToStringMap.put(71, JPN_HAGAKI);
     77         sCodeToStringMap.put(74, PHOTO_4x6in);
     78         sCodeToStringMap.put(122, PHOTO_5x7);
     79         sCodeToStringMap.put(302, OM_DSC_PHOTO);
     80         sCodeToStringMap.put(303, OM_CARD);
     81         sCodeToStringMap.put(304, OE_PHOTO_L);
     82     }
     83 
     84     private final Map<String, PrintAttributes.MediaSize> mNameToSizeMap = new HashMap<>();
     85 
     86     private MediaSizes(Context context) {
     87         mNameToSizeMap.put(LETTER, PrintAttributes.MediaSize.NA_LETTER);
     88         mNameToSizeMap.put(LEGAL, PrintAttributes.MediaSize.NA_LEGAL);
     89         mNameToSizeMap.put(ISO_A3, PrintAttributes.MediaSize.ISO_A3);
     90         mNameToSizeMap.put(ISO_A4, PrintAttributes.MediaSize.ISO_A4);
     91         mNameToSizeMap.put(ISO_A5, PrintAttributes.MediaSize.ISO_A5);
     92         mNameToSizeMap.put(JPN_HAGAKI, PrintAttributes.MediaSize.JPN_HAGAKI);
     93         mNameToSizeMap.put(JIS_B4, PrintAttributes.MediaSize.JIS_B4);
     94         mNameToSizeMap.put(JIS_B5, PrintAttributes.MediaSize.JIS_B5);
     95         mNameToSizeMap.put(NA_LEDGER_11X17, PrintAttributes.MediaSize.NA_TABLOID);
     96 
     97         // Custom media sizes
     98         mNameToSizeMap.put(PHOTO_4x6in, new PrintAttributes.MediaSize(
     99                 PHOTO_4x6in,
    100                 context.getString(R.string.media_size_4x6in), 4000, 6000));
    101         mNameToSizeMap.put(NA_GOVT_LETTER, new PrintAttributes.MediaSize(
    102                 NA_GOVT_LETTER,
    103                 context.getString(R.string.media_size_8x10in), 8000, 10000));
    104         mNameToSizeMap.put(PHOTO_5x7, new PrintAttributes.MediaSize(
    105                 PHOTO_5x7,
    106                 context.getString(R.string.media_size_5x7in), 5000, 7000));
    107         mNameToSizeMap.put(OM_DSC_PHOTO, new PrintAttributes.MediaSize(
    108                 OM_DSC_PHOTO,
    109                 context.getString(R.string.media_size_89x119mm), 3504, 4685));
    110         mNameToSizeMap.put(OM_CARD, new PrintAttributes.MediaSize(
    111                 OM_CARD,
    112                 context.getString(R.string.media_size_54x86mm), 2126, 3386));
    113         mNameToSizeMap.put(OE_PHOTO_L, new PrintAttributes.MediaSize(
    114                 OE_PHOTO_L,
    115                 context.getString(R.string.media_size_l), 3500, 5000));
    116     }
    117 
    118     public static MediaSizes getInstance(Context context) {
    119         if (sInstance == null) {
    120             sInstance = new MediaSizes(context.getApplicationContext());
    121         }
    122         return sInstance;
    123     }
    124 
    125     /**
    126      * Return the backend code for a backend string name.
    127      */
    128     private static int toMediaCode(String name) {
    129         for (Map.Entry<Integer, String> entry : sCodeToStringMap.entrySet()) {
    130             if (entry.getValue().equals(name)) {
    131                 return entry.getKey();
    132             }
    133         }
    134         return 0;
    135     }
    136 
    137     /**
    138      * Return the backend string for a backend code name.
    139      */
    140     static String toMediaName(int code) {
    141         return sCodeToStringMap.get(code);
    142     }
    143 
    144     /**
    145      * Return the {@link PrintAttributes.MediaSize} object corresponding to the backend
    146      * string name.
    147      */
    148     PrintAttributes.MediaSize toMediaSize(String name) {
    149         return mNameToSizeMap.get(name);
    150     }
    151 
    152     /**
    153      * Return backend code for a {@link PrintAttributes.MediaSize}.
    154      */
    155     public int toMediaCode(PrintAttributes.MediaSize mediaSize) {
    156         for (Map.Entry<String, PrintAttributes.MediaSize> entry : mNameToSizeMap.entrySet()) {
    157             // Note: media size may be oriented and not match. So check by ID.
    158             if (entry.getValue().getId().equals(mediaSize.getId())) {
    159                 return toMediaCode(entry.getKey());
    160             }
    161         }
    162         return 0;
    163     }
    164 }