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