| 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 android.accessibilityservice; |
| 18 | |
| 19 | import android.annotation.FloatRange; |
| 20 | import android.annotation.IntDef; |
| 21 | import android.annotation.NonNull; |
| 22 | import android.os.Parcel; |
| 23 | import android.os.Parcelable; |
| 24 | |
| 25 | import java.lang.annotation.Retention; |
| 26 | import java.lang.annotation.RetentionPolicy; |
| 27 | |
| 28 | /** |
| 29 | * This class describes the magnification config for {@link AccessibilityService} to control the |
| 30 | * magnification. |
| 31 | * |
| 32 | * <p> |
| 33 | * When the magnification config uses {@link #MAGNIFICATION_MODE_DEFAULT}, |
| 34 | * {@link AccessibilityService} will be able to control the activated magnifier on the display. |
| 35 | * If there is no magnifier activated, it controls the last-activated magnification mode. |
| 36 | * If there is no magnifier activated before, it controls full-screen magnifier by default. |
| 37 | * </p> |
| 38 | * |
| 39 | * <p> |
| 40 | * When the magnification config uses {@link #MAGNIFICATION_MODE_FULLSCREEN}. |
| 41 | * {@link AccessibilityService} will be able to control full-screen magnifier on the display. |
| 42 | * </p> |
| 43 | * |
| 44 | * <p> |
| 45 | * When the magnification config uses {@link #MAGNIFICATION_MODE_WINDOW} and the platform |
| 46 | * supports {@link android.content.pm.PackageManager#FEATURE_WINDOW_MAGNIFICATION} feature. |
| 47 | * {@link AccessibilityService} will be able to control window magnifier on the display. |
| 48 | * </p> |
| 49 | * |
| 50 | * <p> |
| 51 | * If the other magnification configs, scale centerX and centerY, are not set by the |
| 52 | * {@link Builder}, the configs should be current values or default values. And the center |
| 53 | * position ordinarily is the center of the screen. |
| 54 | * </p> |
| 55 | */ |
| 56 | public final class MagnificationConfig implements Parcelable { |
| 57 | |
| 58 | /** The controlling magnification mode. It controls the activated magnifier. */ |
| 59 | public static final int MAGNIFICATION_MODE_DEFAULT = 0; |
| 60 | /** The controlling magnification mode. It controls full-screen magnifier. */ |
| 61 | public static final int MAGNIFICATION_MODE_FULLSCREEN = 1; |
| 62 | /** |
| 63 | * The controlling magnification mode. It is valid if the platform supports |
| 64 | * {@link android.content.pm.PackageManager#FEATURE_WINDOW_MAGNIFICATION} feature. |
| 65 | */ |
| 66 | public static final int MAGNIFICATION_MODE_WINDOW = 2; |
| 67 | |
| 68 | /** @hide */ |
| 69 | @IntDef(prefix = {"MAGNIFICATION_MODE"}, value = { |
| 70 | MAGNIFICATION_MODE_DEFAULT, |
| 71 | MAGNIFICATION_MODE_FULLSCREEN, |
| 72 | MAGNIFICATION_MODE_WINDOW, |
| 73 | }) |
| 74 | @Retention(RetentionPolicy.SOURCE) |
| 75 | @interface MagnificationMode { |
| 76 | } |
| 77 | |
| 78 | private int mMode = MAGNIFICATION_MODE_DEFAULT; |
| 79 | private float mScale = Float.NaN; |
| 80 | private float mCenterX = Float.NaN; |
| 81 | private float mCenterY = Float.NaN; |
| 82 | |
| 83 | private MagnificationConfig() { |
| 84 | /* do nothing */ |
| 85 | } |
| 86 | |
| 87 | private MagnificationConfig(@NonNull Parcel parcel) { |
| 88 | mMode = parcel.readInt(); |
| 89 | mScale = parcel.readFloat(); |
| 90 | mCenterX = parcel.readFloat(); |
| 91 | mCenterY = parcel.readFloat(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns the magnification mode that is the current activated mode or the controlling mode of |
| 96 | * the config. |
| 97 | * |
| 98 | * @return The magnification mode |
| 99 | */ |
| 100 | public int getMode() { |
| 101 | return mMode; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Returns the magnification scale of the controlling magnifier |
| 106 | * |
| 107 | * @return the scale If the controlling magnifier is not activated, it returns 1 by default |
| 108 | */ |
| 109 | public float getScale() { |
| 110 | return mScale; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Returns the screen-relative X coordinate of the center of the magnification viewport. |
| 115 | * |
| 116 | * @return the X coordinate. If the controlling magnifier is {@link #MAGNIFICATION_MODE_WINDOW} |
| 117 | * but not enabled, it returns {@link Float#NaN}. If the controlling magnifier is {@link |
| 118 | * #MAGNIFICATION_MODE_FULLSCREEN} but not enabled, it returns 0 |
| 119 | */ |
| 120 | public float getCenterX() { |
| 121 | return mCenterX; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns the screen-relative Y coordinate of the center of the magnification viewport. |
| 126 | * |
| 127 | * @return the Y coordinate If the controlling magnifier is {@link #MAGNIFICATION_MODE_WINDOW} |
| 128 | * but not enabled, it returns {@link Float#NaN}. If the controlling magnifier is {@link |
| 129 | * #MAGNIFICATION_MODE_FULLSCREEN} but not enabled, it returns 0 |
| 130 | */ |
| 131 | public float getCenterY() { |
| 132 | return mCenterY; |
| 133 | } |
| 134 | |
| 135 | @NonNull |
| 136 | @Override |
| 137 | public String toString() { |
| 138 | StringBuilder stringBuilder = new StringBuilder("MagnificationConfig["); |
| 139 | stringBuilder.append("mode: ").append(getMode()); |
| 140 | stringBuilder.append(", "); |
| 141 | stringBuilder.append("scale: ").append(getScale()); |
| 142 | stringBuilder.append(", "); |
| 143 | stringBuilder.append("centerX: ").append(getCenterX()); |
| 144 | stringBuilder.append(", "); |
| 145 | stringBuilder.append("centerY: ").append(getCenterY()); |
| 146 | stringBuilder.append("] "); |
| 147 | return stringBuilder.toString(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int describeContents() { |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void writeToParcel(@NonNull Parcel parcel, int flags) { |
| 157 | parcel.writeInt(mMode); |
| 158 | parcel.writeFloat(mScale); |
| 159 | parcel.writeFloat(mCenterX); |
| 160 | parcel.writeFloat(mCenterY); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Builder for creating {@link MagnificationConfig} objects. |
| 165 | */ |
| 166 | public static final class Builder { |
| 167 | |
| 168 | private int mMode = MAGNIFICATION_MODE_DEFAULT; |
| 169 | private float mScale = Float.NaN; |
| 170 | private float mCenterX = Float.NaN; |
| 171 | private float mCenterY = Float.NaN; |
| 172 | |
| 173 | /** |
| 174 | * Creates a new Builder. |
| 175 | */ |
| 176 | public Builder() { |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets the magnification mode. |
| 181 | * |
| 182 | * @param mode The magnification mode |
| 183 | * @return This builder |
| 184 | */ |
| 185 | @NonNull |
| 186 | public MagnificationConfig.Builder setMode(@MagnificationMode int mode) { |
| 187 | mMode = mode; |
| 188 | return this; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Sets the magnification scale. |
| 193 | * |
| 194 | * @param scale The magnification scale, in the range [1, 8] |
| 195 | * @return This builder |
| 196 | */ |
| 197 | @NonNull |
| 198 | public MagnificationConfig.Builder setScale(@FloatRange(from = 1f, to = 8f) float scale) { |
| 199 | mScale = scale; |
| 200 | return this; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Sets the X coordinate of the center of the magnification viewport. |
| 205 | * The controlling magnifier will apply the given position. |
| 206 | * |
| 207 | * @param centerX the screen-relative X coordinate around which to |
| 208 | * center and scale that is in the range [0, screenWidth], |
| 209 | * or {@link Float#NaN} to leave unchanged |
| 210 | * @return This builder |
| 211 | */ |
| 212 | @NonNull |
| 213 | public MagnificationConfig.Builder setCenterX(float centerX) { |
| 214 | mCenterX = centerX; |
| 215 | return this; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Sets the Y coordinate of the center of the magnification viewport. |
| 220 | * The controlling magnifier will apply the given position. |
| 221 | * |
| 222 | * @param centerY the screen-relative Y coordinate around which to |
| 223 | * center and scale that is in the range [0, screenHeight], |
| 224 | * or {@link Float#NaN} to leave unchanged |
| 225 | * @return This builder |
| 226 | */ |
| 227 | @NonNull |
| 228 | public MagnificationConfig.Builder setCenterY(float centerY) { |
| 229 | mCenterY = centerY; |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Builds and returns a {@link MagnificationConfig} |
| 235 | */ |
| 236 | @NonNull |
| 237 | public MagnificationConfig build() { |
| 238 | MagnificationConfig magnificationConfig = new MagnificationConfig(); |
| 239 | magnificationConfig.mMode = mMode; |
| 240 | magnificationConfig.mScale = mScale; |
| 241 | magnificationConfig.mCenterX = mCenterX; |
| 242 | magnificationConfig.mCenterY = mCenterY; |
| 243 | return magnificationConfig; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @see Parcelable.Creator |
| 249 | */ |
| 250 | public static final @NonNull Parcelable.Creator<MagnificationConfig> CREATOR = |
| 251 | new Parcelable.Creator<MagnificationConfig>() { |
| 252 | public MagnificationConfig createFromParcel(Parcel parcel) { |
| 253 | return new MagnificationConfig(parcel); |
| 254 | } |
| 255 | |
| 256 | public MagnificationConfig[] newArray(int size) { |
| 257 | return new MagnificationConfig[size]; |
| 258 | } |
| 259 | }; |
| 260 | } |