Merge \"Cache resources needed to sort media sizes.\" into nyc-dev
am: 142fc425c4
Change-Id: I368ffa5269029801ed191351fb1fff9bde72778a
This commit is contained in:
@@ -614,6 +614,9 @@ public class PrintActivity extends Activity implements RemotePrintDocument.Updat
|
|||||||
@Override
|
@Override
|
||||||
public void onConfigurationChanged(Configuration newConfig) {
|
public void onConfigurationChanged(Configuration newConfig) {
|
||||||
super.onConfigurationChanged(newConfig);
|
super.onConfigurationChanged(newConfig);
|
||||||
|
|
||||||
|
mMediaSizeComparator.onConfigurationChanged(newConfig);
|
||||||
|
|
||||||
if (mPrintPreviewController != null) {
|
if (mPrintPreviewController != null) {
|
||||||
mPrintPreviewController.onOrientationChanged();
|
mPrintPreviewController.onOrientationChanged();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,13 +16,16 @@
|
|||||||
|
|
||||||
package com.android.printspooler.util;
|
package com.android.printspooler.util;
|
||||||
|
|
||||||
|
import android.annotation.NonNull;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.content.pm.ActivityInfo;
|
||||||
|
import android.content.res.Configuration;
|
||||||
import android.print.PrintAttributes.MediaSize;
|
import android.print.PrintAttributes.MediaSize;
|
||||||
import android.util.ArrayMap;
|
|
||||||
|
|
||||||
import com.android.printspooler.R;
|
import com.android.printspooler.R;
|
||||||
|
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -30,7 +33,10 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
public final class MediaSizeUtils {
|
public final class MediaSizeUtils {
|
||||||
|
|
||||||
private static Map<MediaSize, String> sMediaSizeToStandardMap;
|
private static Map<MediaSize, Integer> sMediaSizeToStandardMap;
|
||||||
|
|
||||||
|
/** The media size standard for all media sizes no standard is defined for */
|
||||||
|
private static int sMediaSizeStandardIso;
|
||||||
|
|
||||||
private MediaSizeUtils() {
|
private MediaSizeUtils() {
|
||||||
/* do nothing - hide constructor */
|
/* do nothing - hide constructor */
|
||||||
@@ -47,22 +53,32 @@ public final class MediaSizeUtils {
|
|||||||
return MediaSize.getStandardMediaSizeById(mediaSizeId);
|
return MediaSize.getStandardMediaSizeById(mediaSizeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String getStandardForMediaSize(Context context, MediaSize mediaSize) {
|
/**
|
||||||
|
* Get the standard the {@link MediaSize} belongs to.
|
||||||
|
*
|
||||||
|
* @param context The context of the caller
|
||||||
|
* @param mediaSize The {@link MediaSize} to be resolved
|
||||||
|
*
|
||||||
|
* @return The standard the {@link MediaSize} belongs to
|
||||||
|
*/
|
||||||
|
private static int getStandardForMediaSize(Context context, MediaSize mediaSize) {
|
||||||
if (sMediaSizeToStandardMap == null) {
|
if (sMediaSizeToStandardMap == null) {
|
||||||
sMediaSizeToStandardMap = new ArrayMap<MediaSize, String>();
|
sMediaSizeStandardIso = Integer.parseInt(context.getString(
|
||||||
|
R.string.mediasize_standard_iso));
|
||||||
|
|
||||||
|
sMediaSizeToStandardMap = new HashMap<>();
|
||||||
String[] mediaSizeToStandardMapValues = context.getResources()
|
String[] mediaSizeToStandardMapValues = context.getResources()
|
||||||
.getStringArray(R.array.mediasize_to_standard_map);
|
.getStringArray(R.array.mediasize_to_standard_map);
|
||||||
final int mediaSizeToStandardCount = mediaSizeToStandardMapValues.length;
|
final int mediaSizeToStandardCount = mediaSizeToStandardMapValues.length;
|
||||||
for (int i = 0; i < mediaSizeToStandardCount; i += 2) {
|
for (int i = 0; i < mediaSizeToStandardCount; i += 2) {
|
||||||
String mediaSizeId = mediaSizeToStandardMapValues[i];
|
String mediaSizeId = mediaSizeToStandardMapValues[i];
|
||||||
MediaSize key = MediaSize.getStandardMediaSizeById(mediaSizeId);
|
MediaSize key = MediaSize.getStandardMediaSizeById(mediaSizeId);
|
||||||
String value = mediaSizeToStandardMapValues[i + 1];
|
int value = Integer.parseInt(mediaSizeToStandardMapValues[i + 1]);
|
||||||
sMediaSizeToStandardMap.put(key, value);
|
sMediaSizeToStandardMap.put(key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
String standard = sMediaSizeToStandardMap.get(mediaSize);
|
Integer standard = sMediaSizeToStandardMap.get(mediaSize);
|
||||||
return (standard != null) ? standard : context.getString(
|
return (standard != null) ? standard : sMediaSizeStandardIso;
|
||||||
R.string.mediasize_standard_iso);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -73,32 +89,76 @@ public final class MediaSizeUtils {
|
|||||||
public static final class MediaSizeComparator implements Comparator<MediaSize> {
|
public static final class MediaSizeComparator implements Comparator<MediaSize> {
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
|
||||||
|
/** Current configuration */
|
||||||
|
private Configuration mCurrentConfig;
|
||||||
|
|
||||||
|
/** The standard to use for the current locale */
|
||||||
|
private int mCurrentStandard;
|
||||||
|
|
||||||
|
/** Mapping from media size to label */
|
||||||
|
private final @NonNull Map<MediaSize, String> mMediaSizeToLabel;
|
||||||
|
|
||||||
public MediaSizeComparator(Context context) {
|
public MediaSizeComparator(Context context) {
|
||||||
mContext = context;
|
mContext = context;
|
||||||
|
mMediaSizeToLabel = new HashMap<>();
|
||||||
|
mCurrentStandard = Integer.parseInt(mContext.getString(R.string.mediasize_standard));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle a configuration change by reloading all resources.
|
||||||
|
*
|
||||||
|
* @param newConfig The new configuration that will be applied.
|
||||||
|
*/
|
||||||
|
public void onConfigurationChanged(@NonNull Configuration newConfig) {
|
||||||
|
if (mCurrentConfig == null ||
|
||||||
|
(newConfig.diff(mCurrentConfig) & ActivityInfo.CONFIG_LOCALE) != 0) {
|
||||||
|
mCurrentStandard = Integer
|
||||||
|
.parseInt(mContext.getString(R.string.mediasize_standard));
|
||||||
|
mMediaSizeToLabel.clear();
|
||||||
|
|
||||||
|
mCurrentConfig = newConfig;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the label for a {@link MediaSize}.
|
||||||
|
*
|
||||||
|
* @param context The context the label should be loaded for
|
||||||
|
* @param mediaSize The {@link MediaSize} to resolve
|
||||||
|
*
|
||||||
|
* @return The label for the media size
|
||||||
|
*/
|
||||||
|
public @NonNull String getLabel(@NonNull Context context, @NonNull MediaSize mediaSize) {
|
||||||
|
String label = mMediaSizeToLabel.get(mediaSize);
|
||||||
|
|
||||||
|
if (label == null) {
|
||||||
|
label = mediaSize.getLabel(context.getPackageManager());
|
||||||
|
mMediaSizeToLabel.put(mediaSize, label);
|
||||||
|
}
|
||||||
|
|
||||||
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(MediaSize lhs, MediaSize rhs) {
|
public int compare(MediaSize lhs, MediaSize rhs) {
|
||||||
String currentStandard = mContext.getString(R.string.mediasize_standard);
|
int lhsStandard = getStandardForMediaSize(mContext, lhs);
|
||||||
String lhsStandard = getStandardForMediaSize(mContext, lhs);
|
int rhsStandard = getStandardForMediaSize(mContext, rhs);
|
||||||
String rhsStandard = getStandardForMediaSize(mContext, rhs);
|
|
||||||
|
|
||||||
// The current standard always wins.
|
// The current standard always wins.
|
||||||
if (lhsStandard.equals(currentStandard)) {
|
if (lhsStandard == mCurrentStandard) {
|
||||||
if (!rhsStandard.equals(currentStandard)) {
|
if (rhsStandard != mCurrentStandard) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else if (rhsStandard.equals(currentStandard)) {
|
} else if (rhsStandard == mCurrentStandard) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!lhsStandard.equals(rhsStandard)) {
|
if (lhsStandard != rhsStandard) {
|
||||||
// Different standards - use the standard ordering.
|
// Different standards - use the standard ordering.
|
||||||
return lhsStandard.compareTo(rhsStandard);
|
return Integer.valueOf(lhsStandard).compareTo(rhsStandard);
|
||||||
} else {
|
} else {
|
||||||
// Same standard - sort alphabetically by label.
|
// Same standard - sort alphabetically by label.
|
||||||
return lhs.getLabel(mContext.getPackageManager()).
|
return getLabel(mContext, lhs).compareTo(getLabel(mContext, rhs));
|
||||||
compareTo(rhs.getLabel(mContext.getPackageManager()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user