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.content.Context; 20 import android.print.PrintAttributes.MediaSize; 21 import android.util.ArrayMap; 22 23 import java.util.Comparator; 24 import java.util.Map; 25 26 /** 27 * Utility functions and classes for dealing with media sizes. 28 */ 29 public class MediaSizeUtils { 30 31 private static Map<MediaSize, String> sMediaSizeToStandardMap; 32 33 /** 34 * Gets the default media size for the current locale. 35 * 36 * @param context Context for accessing resources. 37 * @return The default media size. 38 */ 39 public static MediaSize getDefault(Context context) { 40 String mediaSizeId = context.getString(R.string.mediasize_default); 41 return MediaSize.getStandardMediaSizeById(mediaSizeId); 42 } 43 44 private static String getStandardForMediaSize(Context context, MediaSize mediaSize) { 45 if (sMediaSizeToStandardMap == null) { 46 sMediaSizeToStandardMap = new ArrayMap<MediaSize, String>(); 47 String[] mediaSizeToStandardMapValues = context.getResources() 48 .getStringArray(R.array.mediasize_to_standard_map); 49 final int mediaSizeToStandardCount = mediaSizeToStandardMapValues.length; 50 for (int i = 0; i < mediaSizeToStandardCount; i += 2) { 51 String mediaSizeId = mediaSizeToStandardMapValues[i]; 52 MediaSize key = MediaSize.getStandardMediaSizeById(mediaSizeId); 53 String value = mediaSizeToStandardMapValues[i + 1]; 54 sMediaSizeToStandardMap.put(key, value); 55 } 56 } 57 String standard = sMediaSizeToStandardMap.get(mediaSize); 58 return (standard != null) ? standard : context.getString( 59 R.string.mediasize_standard_iso); 60 } 61 62 /** 63 * Comparator for ordering standard media sizes. The ones for the current 64 * standard go to the top and the ones for the other standards follow grouped 65 * by standard. Media sizes of the same standard are ordered alphabetically. 66 */ 67 public static final class MediaSizeComparator implements Comparator<MediaSize> { 68 private final Context mContext; 69 70 public MediaSizeComparator(Context context) { 71 mContext = context; 72 } 73 74 @Override 75 public int compare(MediaSize lhs, MediaSize rhs) { 76 String currentStandard = mContext.getString(R.string.mediasize_standard); 77 String lhsStandard = getStandardForMediaSize(mContext, lhs); 78 String rhsStandard = getStandardForMediaSize(mContext, rhs); 79 80 // The current standard always wins. 81 if (lhsStandard.equals(currentStandard)) { 82 if (!rhsStandard.equals(currentStandard)) { 83 return -1; 84 } 85 } else if (rhsStandard.equals(currentStandard)) { 86 return 1; 87 } 88 89 if (!lhsStandard.equals(rhsStandard)) { 90 // Different standards - use the standard ordering. 91 return lhsStandard.compareTo(rhsStandard); 92 } else { 93 // Same standard - sort alphabetically by label. 94 return lhs.getLabel(mContext.getPackageManager()). 95 compareTo(rhs.getLabel(mContext.getPackageManager())); 96 } 97 } 98 } 99 } 100