1 /*
2  * Copyright (C) 2019 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.bluetooth.avrcpcontroller;
18 
19 import android.util.Log;
20 
21 import java.util.HashSet;
22 
23 /**
24  * Represents the set of possible transformations available for a variant of an image to get the
25  * image to a particular pixel size.
26  *
27  * <p>The transformations supported by BIP v1.2.1 include: - Stretch - Fill - Crop
28  *
29  * <p>Example in an image properties/format: <variant encoding=“GIF” pixel=“80*60-640*480”
30  * transformation="stretch fill"/> <variant encoding=“GIF” pixel=“80*60-640*480”
31  * transformation="fill"/> <variant encoding=“GIF” pixel=“80*60-640*480” transformation="stretch
32  * fill crop"/>
33  *
34  * <p>Example in an image descriptor: <image-descriptor version=“1.0”> <image encoding=“JPEG”
35  * pixel=“1280*960” size=“500000” transformation="stretch"/> </image-descriptor>
36  */
37 public class BipTransformation {
38     private static final String TAG = "avrcpcontroller.BipTransformation";
39 
40     public static final int UNKNOWN = -1;
41     public static final int STRETCH = 0;
42     public static final int FILL = 1;
43     public static final int CROP = 2;
44 
45     public final HashSet<Integer> mSupportedTransformations = new HashSet<Integer>(3);
46 
47     /** Create an empty set of BIP Transformations */
BipTransformation()48     public BipTransformation() {}
49 
50     /** Create a set of BIP Transformations from an attribute value from an Image Format string */
BipTransformation(String transformations)51     public BipTransformation(String transformations) {
52         if (transformations == null) return;
53 
54         transformations = transformations.trim().toLowerCase();
55         String[] tokens = transformations.split(" ");
56         for (String token : tokens) {
57             switch (token) {
58                 case "stretch":
59                     addTransformation(STRETCH);
60                     break;
61                 case "fill":
62                     addTransformation(FILL);
63                     break;
64                 case "crop":
65                     addTransformation(CROP);
66                     break;
67                 default:
68                     Log.e(TAG, "Found unknown transformation '" + token + "'");
69                     break;
70             }
71         }
72     }
73 
74     /** Create a set of BIP Transformations from a single supported transformation */
BipTransformation(int transformation)75     public BipTransformation(int transformation) {
76         addTransformation(transformation);
77     }
78 
79     /** Create a set of BIP Transformations from a set of supported transformations */
BipTransformation(int[] transformations)80     public BipTransformation(int[] transformations) {
81         for (int transformation : transformations) {
82             addTransformation(transformation);
83         }
84     }
85 
86     /**
87      * Add a supported Transformation
88      *
89      * @param transformation - The transformation you with to support
90      */
addTransformation(int transformation)91     public void addTransformation(int transformation) {
92         if (!isValid(transformation)) {
93             throw new IllegalArgumentException(
94                     "Invalid transformation ID '" + transformation + "'");
95         }
96         mSupportedTransformations.add(transformation);
97     }
98 
99     /**
100      * Remove a supported Transformation
101      *
102      * @param transformation - The transformation you with to remove support for
103      */
removeTransformation(int transformation)104     public void removeTransformation(int transformation) {
105         if (!isValid(transformation)) {
106             throw new IllegalArgumentException(
107                     "Invalid transformation ID '" + transformation + "'");
108         }
109         mSupportedTransformations.remove(transformation);
110     }
111 
112     /**
113      * Determine if a given transformations is valid
114      *
115      * @param transformation The integer encoding ID of the transformation. Should be one of the
116      *     BipTransformation.* constants, but doesn't *have* to be
117      * @return True if the transformation constant is valid, False otherwise
118      */
isValid(int transformation)119     private boolean isValid(int transformation) {
120         return transformation >= STRETCH && transformation <= CROP;
121     }
122 
123     /**
124      * Determine if this set of transformations supports a desired transformation
125      *
126      * @param transformation The ID of the desired transformation, STRETCH, FILL, or CROP
127      * @return True if this set supports the transformation, False otherwise
128      */
isSupported(int transformation)129     public boolean isSupported(int transformation) {
130         return mSupportedTransformations.contains(transformation);
131     }
132 
133     /**
134      * Determine if this object supports any transformations at all
135      *
136      * @return True if any valid transformations are supported, False otherwise
137      */
supportsAny()138     public boolean supportsAny() {
139         return !mSupportedTransformations.isEmpty();
140     }
141 
142     @Override
equals(Object o)143     public boolean equals(Object o) {
144         if (o == this) return true;
145         if (o == null && !supportsAny()) return true;
146         if (!(o instanceof BipTransformation)) return false;
147 
148         BipTransformation t = (BipTransformation) o;
149         return mSupportedTransformations.equals(t.mSupportedTransformations);
150     }
151 
152     @Override
toString()153     public String toString() {
154         if (!supportsAny()) return null;
155         String transformations = "";
156         if (isSupported(STRETCH)) {
157             transformations += "stretch ";
158         }
159         if (isSupported(FILL)) {
160             transformations += "fill ";
161         }
162         if (isSupported(CROP)) {
163             transformations += "crop ";
164         }
165         return transformations.trim();
166     }
167 }
168