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