| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package androidx.window.common; |
| 18 |
|
| 19 |
import static androidx.window.util.ExtensionHelper.isZero; |
| 20 |
|
| 21 |
import android.annotation.IntDef; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.graphics.Rect; |
| 24 |
import android.util.Log; |
| 25 |
|
| 26 |
import androidx.annotation.NonNull; |
| 27 |
|
| 28 |
import java.lang.annotation.Retention; |
| 29 |
import java.lang.annotation.RetentionPolicy; |
| 30 |
import java.util.ArrayList; |
| 31 |
import java.util.List; |
| 32 |
import java.util.Objects; |
| 33 |
import java.util.regex.Matcher; |
| 34 |
import java.util.regex.Pattern; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public final class CommonFoldingFeature { |
| 42 |
|
| 43 |
private static final boolean DEBUG = false; |
| 44 |
|
| 45 |
public static final String TAG = CommonFoldingFeature.class.getSimpleName(); |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static final int COMMON_TYPE_FOLD = 1; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static final int COMMON_TYPE_HINGE = 2; |
| 57 |
|
| 58 |
@IntDef({COMMON_TYPE_FOLD, COMMON_TYPE_HINGE}) |
| 59 |
@Retention(RetentionPolicy.SOURCE) |
| 60 |
public @interface Type { |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static final int COMMON_STATE_UNKNOWN = -1; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static final int COMMON_STATE_FLAT = 3; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static final int COMMON_STATE_HALF_OPENED = 2; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
@IntDef({COMMON_STATE_UNKNOWN, COMMON_STATE_FLAT, COMMON_STATE_HALF_OPENED}) |
| 84 |
@Retention(RetentionPolicy.SOURCE) |
| 85 |
public @interface State { |
| 86 |
} |
| 87 |
|
| 88 |
private static final Pattern FEATURE_PATTERN = |
| 89 |
Pattern.compile("([a-z]+)-\\[(\\d+),(\\d+),(\\d+),(\\d+)]-?(flat|half-opened)?"); |
| 90 |
|
| 91 |
private static final String FEATURE_TYPE_FOLD = "fold"; |
| 92 |
private static final String FEATURE_TYPE_HINGE = "hinge"; |
| 93 |
|
| 94 |
private static final String PATTERN_STATE_FLAT = "flat"; |
| 95 |
private static final String PATTERN_STATE_HALF_OPENED = "half-opened"; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static List<CommonFoldingFeature> parseListFromString(@NonNull String value, |
| 109 |
@State int hingeState) { |
| 110 |
List<CommonFoldingFeature> features = new ArrayList<>(); |
| 111 |
String[] featureStrings = value.split(";"); |
| 112 |
for (String featureString : featureStrings) { |
| 113 |
CommonFoldingFeature feature; |
| 114 |
try { |
| 115 |
feature = CommonFoldingFeature.parseFromString(featureString, hingeState); |
| 116 |
} catch (IllegalArgumentException e) { |
| 117 |
if (DEBUG) { |
| 118 |
Log.w(TAG, "Failed to parse display feature: " + featureString, e); |
| 119 |
} |
| 120 |
continue; |
| 121 |
} |
| 122 |
features.add(feature); |
| 123 |
} |
| 124 |
return features; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
@NonNull |
| 138 |
private static CommonFoldingFeature parseFromString(@NonNull String string, |
| 139 |
@State int hingeState) { |
| 140 |
Matcher featureMatcher = FEATURE_PATTERN.matcher(string); |
| 141 |
if (!featureMatcher.matches()) { |
| 142 |
throw new IllegalArgumentException("Malformed feature description format: " + string); |
| 143 |
} |
| 144 |
try { |
| 145 |
String featureType = featureMatcher.group(1); |
| 146 |
featureType = featureType == null ? "" : featureType; |
| 147 |
int type; |
| 148 |
switch (featureType) { |
| 149 |
case FEATURE_TYPE_FOLD: |
| 150 |
type = COMMON_TYPE_FOLD; |
| 151 |
break; |
| 152 |
case FEATURE_TYPE_HINGE: |
| 153 |
type = COMMON_TYPE_HINGE; |
| 154 |
break; |
| 155 |
default: { |
| 156 |
throw new IllegalArgumentException("Malformed feature type: " + featureType); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
int left = Integer.parseInt(featureMatcher.group(2)); |
| 161 |
int top = Integer.parseInt(featureMatcher.group(3)); |
| 162 |
int right = Integer.parseInt(featureMatcher.group(4)); |
| 163 |
int bottom = Integer.parseInt(featureMatcher.group(5)); |
| 164 |
Rect featureRect = new Rect(left, top, right, bottom); |
| 165 |
if (isZero(featureRect)) { |
| 166 |
throw new IllegalArgumentException("Feature has empty bounds: " + string); |
| 167 |
} |
| 168 |
String stateString = featureMatcher.group(6); |
| 169 |
stateString = stateString == null ? "" : stateString; |
| 170 |
final int state; |
| 171 |
switch (stateString) { |
| 172 |
case PATTERN_STATE_FLAT: |
| 173 |
state = COMMON_STATE_FLAT; |
| 174 |
break; |
| 175 |
case PATTERN_STATE_HALF_OPENED: |
| 176 |
state = COMMON_STATE_HALF_OPENED; |
| 177 |
break; |
| 178 |
default: |
| 179 |
state = hingeState; |
| 180 |
break; |
| 181 |
} |
| 182 |
return new CommonFoldingFeature(type, state, featureRect); |
| 183 |
} catch (NumberFormatException e) { |
| 184 |
throw new IllegalArgumentException("Malformed feature description: " + string, e); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
private final int mType; |
| 189 |
@Nullable |
| 190 |
private final int mState; |
| 191 |
@NonNull |
| 192 |
private final Rect mRect; |
| 193 |
|
| 194 |
CommonFoldingFeature(int type, int state, @NonNull Rect rect) { |
| 195 |
assertValidState(state); |
| 196 |
this.mType = type; |
| 197 |
this.mState = state; |
| 198 |
if (rect.width() == 0 && rect.height() == 0) { |
| 199 |
throw new IllegalArgumentException( |
| 200 |
"Display feature rectangle cannot have zero width and height simultaneously."); |
| 201 |
} |
| 202 |
this.mRect = new Rect(rect); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
@Type |
| 207 |
public int getType() { |
| 208 |
return mType; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
@State |
| 213 |
public int getState() { |
| 214 |
return mState; |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
@NonNull |
| 219 |
public Rect getRect() { |
| 220 |
return new Rect(mRect); |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
public boolean equals(Object o) { |
| 225 |
if (this == o) return true; |
| 226 |
if (o == null || getClass() != o.getClass()) return false; |
| 227 |
CommonFoldingFeature that = (CommonFoldingFeature) o; |
| 228 |
return mType == that.mType |
| 229 |
&& Objects.equals(mState, that.mState) |
| 230 |
&& mRect.equals(that.mRect); |
| 231 |
} |
| 232 |
|
| 233 |
@Override |
| 234 |
public int hashCode() { |
| 235 |
return Objects.hash(mType, mState, mRect); |
| 236 |
} |
| 237 |
|
| 238 |
private static void assertValidState(@Nullable Integer state) { |
| 239 |
if (state != null && state != COMMON_STATE_FLAT |
| 240 |
&& state != COMMON_STATE_HALF_OPENED && state != COMMON_STATE_UNKNOWN) { |
| 241 |
throw new IllegalArgumentException("Invalid state: " + state |
| 242 |
+ "must be either COMMON_STATE_FLAT or COMMON_STATE_HALF_OPENED"); |
| 243 |
} |
| 244 |
} |
| 245 |
} |