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