1 /* 2 * Copyright (C) 2021 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.systemui.accessibility.floatingmenu; 18 19 import android.annotation.FloatRange; 20 import android.annotation.NonNull; 21 import android.text.TextUtils; 22 23 /** 24 * Stores information about the position, which includes percentage of X-axis of the screen, 25 * percentage of Y-axis of the screen. 26 */ 27 public class Position { 28 29 private static final char STRING_SEPARATOR = ','; 30 private static final TextUtils.SimpleStringSplitter sStringCommaSplitter = 31 new TextUtils.SimpleStringSplitter(STRING_SEPARATOR); 32 33 private float mPercentageX; 34 private float mPercentageY; 35 36 /** 37 * Creates a {@link Position} from a encoded string described in {@link #toString()}. 38 * 39 * @param positionString A string conform to the format described in {@link #toString()} 40 * @return A {@link Position} with the given value retrieved from {@code absolutePositionString} 41 * @throws IllegalArgumentException If {@code positionString} does not conform to the format 42 * described in {@link #toString()} 43 */ fromString(String positionString)44 public static Position fromString(String positionString) { 45 sStringCommaSplitter.setString(positionString); 46 if (sStringCommaSplitter.hasNext()) { 47 final float percentageX = Float.parseFloat(sStringCommaSplitter.next()); 48 final float percentageY = Float.parseFloat(sStringCommaSplitter.next()); 49 return new Position(percentageX, percentageY); 50 } 51 52 throw new IllegalArgumentException( 53 "Invalid Position string: " + positionString); 54 } 55 Position(float percentageX, float percentageY)56 Position(float percentageX, float percentageY) { 57 update(percentageX, percentageY); 58 } 59 60 @Override toString()61 public String toString() { 62 return mPercentageX + ", " + mPercentageY; 63 } 64 65 /** 66 * Updates the position with {@code percentagePosition}. 67 */ update(@onNull Position percentagePosition)68 public void update(@NonNull Position percentagePosition) { 69 update(percentagePosition.getPercentageX(), percentagePosition.getPercentageY()); 70 } 71 72 /** 73 * Updates the position with {@code percentageX} and {@code percentageY}. 74 * 75 * @param percentageX the new percentage of X-axis of the screen, from 0.0 to 1.0. 76 * @param percentageY the new percentage of Y-axis of the screen, from 0.0 to 1.0. 77 */ update(@loatRangefrom = 0.0, to = 1.0) float percentageX, @FloatRange(from = 0.0, to = 1.0) float percentageY)78 public void update(@FloatRange(from = 0.0, to = 1.0) float percentageX, 79 @FloatRange(from = 0.0, to = 1.0) float percentageY) { 80 mPercentageX = percentageX; 81 mPercentageY = percentageY; 82 } 83 getPercentageX()84 public float getPercentageX() { 85 return mPercentageX; 86 } 87 getPercentageY()88 public float getPercentageY() { 89 return mPercentageY; 90 } 91 } 92