summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/accessibility/floatingmenu/Position.java
blob: 7b7eda84df4c4b87430c873e2031c1b89aca934c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.accessibility.floatingmenu;

import android.annotation.FloatRange;
import android.text.TextUtils;

/**
 * Stores information about the position, which includes percentage of X-axis of the screen,
 * percentage of Y-axis of the screen.
 */
public class Position {

    private static final char STRING_SEPARATOR = ',';
    private static final TextUtils.SimpleStringSplitter sStringCommaSplitter =
            new TextUtils.SimpleStringSplitter(STRING_SEPARATOR);

    private float mPercentageX;
    private float mPercentageY;

    /**
     * Creates a {@link Position} from a encoded string described in {@link #toString()}.
     *
     * @param positionString A string conform to the format described in {@link #toString()}
     * @return A {@link Position} with the given value retrieved from {@code absolutePositionString}
     * @throws IllegalArgumentException If {@code positionString} does not conform to the format
     *                                  described in {@link #toString()}
     */
    public static Position fromString(String positionString) {
        sStringCommaSplitter.setString(positionString);
        if (sStringCommaSplitter.hasNext()) {
            final float percentageX = Float.parseFloat(sStringCommaSplitter.next());
            final float percentageY = Float.parseFloat(sStringCommaSplitter.next());
            return new Position(percentageX, percentageY);
        }

        throw new IllegalArgumentException(
                "Invalid Position string: " + positionString);
    }

    Position(float percentageX, float percentageY) {
        update(percentageX, percentageY);
    }

    @Override
    public String toString() {
        return mPercentageX + ", " + mPercentageY;
    }

    /**
     * Updates the position with {@code percentageX} and {@code percentageY}.
     *
     * @param percentageX the new percentage of X-axis of the screen, from 0.0 to 1.0.
     * @param percentageY the new percentage of Y-axis of the screen, from 0.0 to 1.0.
     */
    public void update(@FloatRange(from = 0.0, to = 1.0) float percentageX,
            @FloatRange(from = 0.0, to = 1.0) float percentageY) {
        mPercentageX = percentageX;
        mPercentageY = percentageY;
    }

    public float getPercentageX() {
        return mPercentageX;
    }

    public float getPercentageY() {
        return mPercentageY;
    }
}