18097973089420749dcd1ab4974a629c2466b31ccAngus Kong/*
28097973089420749dcd1ab4974a629c2466b31ccAngus Kong * Copyright (C) 2014 The Android Open Source Project
38097973089420749dcd1ab4974a629c2466b31ccAngus Kong *
48097973089420749dcd1ab4974a629c2466b31ccAngus Kong * Licensed under the Apache License, Version 2.0 (the "License");
58097973089420749dcd1ab4974a629c2466b31ccAngus Kong * you may not use this file except in compliance with the License.
68097973089420749dcd1ab4974a629c2466b31ccAngus Kong * You may obtain a copy of the License at
78097973089420749dcd1ab4974a629c2466b31ccAngus Kong *
88097973089420749dcd1ab4974a629c2466b31ccAngus Kong *      http://www.apache.org/licenses/LICENSE-2.0
98097973089420749dcd1ab4974a629c2466b31ccAngus Kong *
108097973089420749dcd1ab4974a629c2466b31ccAngus Kong * Unless required by applicable law or agreed to in writing, software
118097973089420749dcd1ab4974a629c2466b31ccAngus Kong * distributed under the License is distributed on an "AS IS" BASIS,
128097973089420749dcd1ab4974a629c2466b31ccAngus Kong * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138097973089420749dcd1ab4974a629c2466b31ccAngus Kong * See the License for the specific language governing permissions and
148097973089420749dcd1ab4974a629c2466b31ccAngus Kong * limitations under the License.
158097973089420749dcd1ab4974a629c2466b31ccAngus Kong */
168097973089420749dcd1ab4974a629c2466b31ccAngus Kong
178097973089420749dcd1ab4974a629c2466b31ccAngus Kongpackage com.android.ex.camera2.portability;
188097973089420749dcd1ab4974a629c2466b31ccAngus Kong
198097973089420749dcd1ab4974a629c2466b31ccAngus Kongimport android.graphics.Point;
208097973089420749dcd1ab4974a629c2466b31ccAngus Kongimport android.hardware.Camera;
218097973089420749dcd1ab4974a629c2466b31ccAngus Kongimport android.text.TextUtils;
228097973089420749dcd1ab4974a629c2466b31ccAngus Kong
238097973089420749dcd1ab4974a629c2466b31ccAngus Kongimport java.util.ArrayList;
248097973089420749dcd1ab4974a629c2466b31ccAngus Kongimport java.util.List;
258097973089420749dcd1ab4974a629c2466b31ccAngus Kong
268097973089420749dcd1ab4974a629c2466b31ccAngus Kong/**
278097973089420749dcd1ab4974a629c2466b31ccAngus Kong * An immutable simple size container.
288097973089420749dcd1ab4974a629c2466b31ccAngus Kong */
298097973089420749dcd1ab4974a629c2466b31ccAngus Kongpublic class Size {
308097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public static final String DELIMITER = ",";
318097973089420749dcd1ab4974a629c2466b31ccAngus Kong
328097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
338097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * An helper method to build a list of this class from a list of
348097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * {@link android.hardware.Camera.Size}.
358097973089420749dcd1ab4974a629c2466b31ccAngus Kong     *
368097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @param cameraSizes Source.
378097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @return The built list.
388097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
398097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public static List<Size> buildListFromCameraSizes(List<Camera.Size> cameraSizes) {
408097973089420749dcd1ab4974a629c2466b31ccAngus Kong        ArrayList<Size> list = new ArrayList<Size>(cameraSizes.size());
418097973089420749dcd1ab4974a629c2466b31ccAngus Kong        for (Camera.Size cameraSize : cameraSizes) {
428097973089420749dcd1ab4974a629c2466b31ccAngus Kong            list.add(new Size(cameraSize));
438097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
448097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return list;
458097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
468097973089420749dcd1ab4974a629c2466b31ccAngus Kong
478097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
48a0842b40441db5332a5290f941021636b1182761Sol Boucher     * A helper method to build a list of this class from a list of {@link android.util.Size}.
49a0842b40441db5332a5290f941021636b1182761Sol Boucher     *
50a0842b40441db5332a5290f941021636b1182761Sol Boucher     * @param cameraSizes Source.
51a0842b40441db5332a5290f941021636b1182761Sol Boucher     * @return The built list.
52a0842b40441db5332a5290f941021636b1182761Sol Boucher     */
53a0842b40441db5332a5290f941021636b1182761Sol Boucher    public static List<Size> buildListFromAndroidSizes(List<android.util.Size> androidSizes) {
54a0842b40441db5332a5290f941021636b1182761Sol Boucher        ArrayList<Size> list = new ArrayList<Size>(androidSizes.size());
55a0842b40441db5332a5290f941021636b1182761Sol Boucher        for (android.util.Size androidSize : androidSizes) {
56a0842b40441db5332a5290f941021636b1182761Sol Boucher            list.add(new Size(androidSize));
57a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
58a0842b40441db5332a5290f941021636b1182761Sol Boucher        return list;
59a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
60a0842b40441db5332a5290f941021636b1182761Sol Boucher
61a0842b40441db5332a5290f941021636b1182761Sol Boucher    /**
628097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * Encode List of this class as comma-separated list of integers.
638097973089420749dcd1ab4974a629c2466b31ccAngus Kong     *
648097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @param sizes List of this class to encode.
658097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @return encoded string.
668097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
678097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public static String listToString(List<Size> sizes) {
688097973089420749dcd1ab4974a629c2466b31ccAngus Kong        ArrayList<Integer> flatSizes = new ArrayList<>();
698097973089420749dcd1ab4974a629c2466b31ccAngus Kong        for (Size s : sizes) {
708097973089420749dcd1ab4974a629c2466b31ccAngus Kong            flatSizes.add(s.width());
718097973089420749dcd1ab4974a629c2466b31ccAngus Kong            flatSizes.add(s.height());
728097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
738097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return TextUtils.join(DELIMITER, flatSizes);
748097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
758097973089420749dcd1ab4974a629c2466b31ccAngus Kong
768097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
778097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * Decode comma-separated even-length list of integers into a List of this class.
788097973089420749dcd1ab4974a629c2466b31ccAngus Kong     *
798097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @param encodedSizes encoded string.
808097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @return List of this class.
818097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
828097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public static List<Size> stringToList(String encodedSizes) {
838097973089420749dcd1ab4974a629c2466b31ccAngus Kong        String[] flatSizes = TextUtils.split(encodedSizes, DELIMITER);
848097973089420749dcd1ab4974a629c2466b31ccAngus Kong        ArrayList<Size> list = new ArrayList<>();
858097973089420749dcd1ab4974a629c2466b31ccAngus Kong        for (int i = 0; i < flatSizes.length; i += 2) {
868097973089420749dcd1ab4974a629c2466b31ccAngus Kong            int width = Integer.parseInt(flatSizes[i]);
878097973089420749dcd1ab4974a629c2466b31ccAngus Kong            int height = Integer.parseInt(flatSizes[i + 1]);
888097973089420749dcd1ab4974a629c2466b31ccAngus Kong            list.add(new Size(width,height));
898097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
908097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return list;
918097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
928097973089420749dcd1ab4974a629c2466b31ccAngus Kong
938097973089420749dcd1ab4974a629c2466b31ccAngus Kong    private final Point val;
948097973089420749dcd1ab4974a629c2466b31ccAngus Kong
958097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
968097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * Constructor.
978097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
988097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public Size(int width, int height) {
998097973089420749dcd1ab4974a629c2466b31ccAngus Kong        val = new Point(width, height);
1008097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1018097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1028097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
1038097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * Copy constructor.
1048097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
1058097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public Size(Size other) {
1068097973089420749dcd1ab4974a629c2466b31ccAngus Kong        if (other == null) {
1078097973089420749dcd1ab4974a629c2466b31ccAngus Kong            val = new Point(0, 0);
1088097973089420749dcd1ab4974a629c2466b31ccAngus Kong        } else {
1098097973089420749dcd1ab4974a629c2466b31ccAngus Kong            val = new Point(other.width(), other.height());
1108097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
1118097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1128097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1138097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
1148097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * Constructor from a source {@link android.hardware.Camera.Size}.
1158097973089420749dcd1ab4974a629c2466b31ccAngus Kong     *
1168097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @param other The source size.
1178097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
1188097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public Size(Camera.Size other) {
1198097973089420749dcd1ab4974a629c2466b31ccAngus Kong        if (other == null) {
1208097973089420749dcd1ab4974a629c2466b31ccAngus Kong            val = new Point(0, 0);
1218097973089420749dcd1ab4974a629c2466b31ccAngus Kong        } else {
1228097973089420749dcd1ab4974a629c2466b31ccAngus Kong            val = new Point(other.width, other.height);
1238097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
1248097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1258097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1268097973089420749dcd1ab4974a629c2466b31ccAngus Kong    /**
127a0842b40441db5332a5290f941021636b1182761Sol Boucher     * Constructor from a source {@link android.util.Size}.
128a0842b40441db5332a5290f941021636b1182761Sol Boucher     *
129a0842b40441db5332a5290f941021636b1182761Sol Boucher     * @param other The source size.
130a0842b40441db5332a5290f941021636b1182761Sol Boucher     */
131a0842b40441db5332a5290f941021636b1182761Sol Boucher    public Size(android.util.Size other) {
132a0842b40441db5332a5290f941021636b1182761Sol Boucher        if (other == null) {
133a0842b40441db5332a5290f941021636b1182761Sol Boucher            val = new Point(0, 0);
134a0842b40441db5332a5290f941021636b1182761Sol Boucher        } else {
135a0842b40441db5332a5290f941021636b1182761Sol Boucher            val = new Point(other.getWidth(), other.getHeight());
136a0842b40441db5332a5290f941021636b1182761Sol Boucher        }
137a0842b40441db5332a5290f941021636b1182761Sol Boucher    }
138a0842b40441db5332a5290f941021636b1182761Sol Boucher
139a0842b40441db5332a5290f941021636b1182761Sol Boucher    /**
1408097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * Constructor from a source {@link android.graphics.Point}.
1418097973089420749dcd1ab4974a629c2466b31ccAngus Kong     *
1428097973089420749dcd1ab4974a629c2466b31ccAngus Kong     * @param p The source size.
1438097973089420749dcd1ab4974a629c2466b31ccAngus Kong     */
1448097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public Size(Point p) {
1458097973089420749dcd1ab4974a629c2466b31ccAngus Kong        if (p == null) {
1468097973089420749dcd1ab4974a629c2466b31ccAngus Kong            val = new Point(0, 0);
1478097973089420749dcd1ab4974a629c2466b31ccAngus Kong        } else {
1488097973089420749dcd1ab4974a629c2466b31ccAngus Kong            val = new Point(p);
1498097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
1508097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1518097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1528097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public int width() {
1538097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return val.x;
1548097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1558097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1568097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public int height() {
1578097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return val.y;
1588097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1598097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1608097973089420749dcd1ab4974a629c2466b31ccAngus Kong    @Override
1618097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public boolean equals(Object o) {
1628097973089420749dcd1ab4974a629c2466b31ccAngus Kong        if (o instanceof Size) {
1638097973089420749dcd1ab4974a629c2466b31ccAngus Kong            Size other = (Size) o;
1648097973089420749dcd1ab4974a629c2466b31ccAngus Kong            return val.equals(other.val);
1658097973089420749dcd1ab4974a629c2466b31ccAngus Kong        }
1668097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return false;
1678097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1688097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1698097973089420749dcd1ab4974a629c2466b31ccAngus Kong    @Override
1708097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public int hashCode() {
1718097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return val.hashCode();
1728097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1738097973089420749dcd1ab4974a629c2466b31ccAngus Kong
1748097973089420749dcd1ab4974a629c2466b31ccAngus Kong    @Override
1758097973089420749dcd1ab4974a629c2466b31ccAngus Kong    public String toString() {
1768097973089420749dcd1ab4974a629c2466b31ccAngus Kong        return "Size: (" + this.width() + " x " + this.height() + ")";
1778097973089420749dcd1ab4974a629c2466b31ccAngus Kong    }
1788097973089420749dcd1ab4974a629c2466b31ccAngus Kong}
179