summaryrefslogtreecommitdiffstats
path: root/packages/PrintSpooler/src/com/android/printspooler/util/MediaSizeUtils.java
blob: 912ee1d094540d5af6618362a11eb38d055e0cea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.printspooler.util;

import android.content.Context;
import android.print.PrintAttributes.MediaSize;
import android.util.ArrayMap;

import com.android.printspooler.R;

import java.util.Comparator;
import java.util.Map;

/**
 * Utility functions and classes for dealing with media sizes.
 */
public final class MediaSizeUtils {

    private static Map<MediaSize, String> sMediaSizeToStandardMap;

    private MediaSizeUtils() {
        /* do nothing - hide constructor */
    }

    /**
     * Gets the default media size for the current locale.
     *
     * @param context Context for accessing resources.
     * @return The default media size.
     */
    public static MediaSize getDefault(Context context) {
        String mediaSizeId = context.getString(R.string.mediasize_default);
        return MediaSize.getStandardMediaSizeById(mediaSizeId);
    }

    private static String getStandardForMediaSize(Context context, MediaSize mediaSize) {
        if (sMediaSizeToStandardMap == null) {
            sMediaSizeToStandardMap = new ArrayMap<MediaSize, String>();
            String[] mediaSizeToStandardMapValues = context.getResources()
                    .getStringArray(R.array.mediasize_to_standard_map);
            final int mediaSizeToStandardCount = mediaSizeToStandardMapValues.length;
            for (int i = 0; i < mediaSizeToStandardCount; i += 2) {
                String mediaSizeId = mediaSizeToStandardMapValues[i];
                MediaSize key = MediaSize.getStandardMediaSizeById(mediaSizeId);
                String value = mediaSizeToStandardMapValues[i + 1];
                sMediaSizeToStandardMap.put(key, value);
            }
        }
        String standard = sMediaSizeToStandardMap.get(mediaSize);
        return (standard != null) ? standard : context.getString(
                R.string.mediasize_standard_iso);
    }

    /**
     * Comparator for ordering standard media sizes. The ones for the current
     * standard go to the top and the ones for the other standards follow grouped
     * by standard. Media sizes of the same standard are ordered alphabetically.
     */
    public static final class MediaSizeComparator implements Comparator<MediaSize> {
        private final Context mContext;

        public MediaSizeComparator(Context context) {
            mContext = context;
        }

        @Override
        public int compare(MediaSize lhs, MediaSize rhs) {
            String currentStandard = mContext.getString(R.string.mediasize_standard);
            String lhsStandard = getStandardForMediaSize(mContext, lhs);
            String rhsStandard = getStandardForMediaSize(mContext, rhs);

            // The current standard always wins.
            if (lhsStandard.equals(currentStandard)) {
                if (!rhsStandard.equals(currentStandard)) {
                    return -1;
                }
            } else if (rhsStandard.equals(currentStandard)) {
                return 1;
            }

            if (!lhsStandard.equals(rhsStandard)) {
                // Different standards - use the standard ordering.
                return lhsStandard.compareTo(rhsStandard);
            } else {
                // Same standard - sort alphabetically by label.
                return lhs.getLabel(mContext.getPackageManager()).
                        compareTo(rhs.getLabel(mContext.getPackageManager()));
            }
        }
    }
}