1 /* 2 * Copyright (C) 2020 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.inputmethodservice.cts.ime; 18 19 import android.graphics.Bitmap; 20 21 import androidx.annotation.NonNull; 22 import androidx.annotation.Size; 23 24 /** 25 * A utility class to put a unique image on the software keyboard so that whether the software 26 * keyboard is visible to the user or not can be determined from the screenshot. 27 */ 28 public enum Watermark { 29 30 /** 31 * A unique (64x64) image for the test IME 1. 32 */ 33 IME1(new long[] { 34 0b0000000000000000000000000000000000000000000000000000000000000000L, 35 0b0000000000000000000000000000000000000000000000000000000000000000L, 36 0b0000000000000000000000000000000000000000000000000000000000000000L, 37 0b0001111111111111111111111111111111111111111111111111111111111000L, 38 0b0001111111111111111111111111111111111111111111111111111111111000L, 39 0b0001110010110100010000111100001000100001111000010001011010011000L, 40 0b0001100011111011001110101101100000001101101011100110111110011000L, 41 0b0001101101001011101111000011110111011110000111101110100101111000L, 42 0b0001111100000100110001010010011111110010010100011001000001111000L, 43 0b0001110010110100010000111100001000100001111000010001011010011000L, 44 0b0001110011111011001110101101100000001101101011100110111110011000L, 45 0b0001110101001011101111000011110111011110000111101110100101011000L, 46 0b0001100100000100110001010010011111110010010100011001000001011000L, 47 0b0001101110110100010000111100001000100001111000010001011011111000L, 48 0b0001111111111011001110101101100000001101101011100110111111111000L, 49 0b0001110011001011101111000011110111011110000111101110100110011000L, 50 0b0001100001000100110001010010011111110010010100011001000100011000L, 51 0b0001101101010100010000111100001000100001111000010001010101111000L, 52 0b0001111110011011001110101101100000001101101011100110110011111000L, 53 0b0001110010111011101111000011110111011110000111101110111010011000L, 54 0b0001100001111100110001010010011111110010010100011001111100011000L, 55 0b0001101101001000010000111100001000100001111000010000100101111000L, 56 0b0001111110000101001110101101100000001101101011100101000011111000L, 57 0b0001110010110100101111000011110111011110000111101001011010011000L, 58 0b0001100001111011110001010010011111110010010100011110111100011000L, 59 0b0001101101001011100000111100001000100001111000001110100101111000L, 60 0b0001111110000100010110101101100000001101101011010001000011111000L, 61 0b0001110010110100010011000011110111011110000110010001011010011000L, 62 0b0001100001111011101111010010011111110010010111101110111100011000L, 63 0b0001101101001011101111011100001000100001110111101110100101111000L, 64 0b0001111110000100010001011101100000001101110100010001000011111000L, 65 0b0001110010110100010000111101110111011101111000010001011010011000L, 66 0b0001100001111011101110100101100010001101001011101110111100011000L, 67 0b0001101101001011101111000011110111011110000111101110100101111000L, 68 0b0001111110000100010001011010011101110010110100010001000011111000L, 69 0b0001110010110100010000111100001000100001111000010001011010011000L, 70 0b0001100001111011101110100101100010001101001011101110111100011000L, 71 0b0001101101001011101111000011110111011110000111101110100101111000L, 72 0b0001111110000100010001011010011101110010110100010001000011111000L, 73 0b0001110010110100010000111100001000100001111000010001011010011000L, 74 0b0001100001111011101110100101100010001101001011101110111100011000L, 75 0b0001101101001011101111000011110111011110000111101110100101111000L, 76 0b0001111110000100010001011010011101110010110100010001000011111000L, 77 0b0001110010110100010000111100001000100001111000010001011010011000L, 78 0b0001100001111011101110100101100010001101001011101110111100011000L, 79 0b0001101101001011101111000011110111011110000111101110100101111000L, 80 0b0001111110000100010001011010011101110010110100010001000011111000L, 81 0b0001110010110100010000111100001000100001111000010001011010011000L, 82 0b0001100001111011101110100101100010001101001011101110111100011000L, 83 0b0001101101001011101111000011110111011110000111101110100101111000L, 84 0b0001111110000100010001011010011101110010110100010001000011111000L, 85 0b0001110010110100010000111100001000100001111000010001011010011000L, 86 0b0001100001111011101110100101100010001101001011101110111100011000L, 87 0b0001101101001011101111000011110111011110000111101110100101111000L, 88 0b0001111110000100010001011010011101110010110100010001000011111000L, 89 0b0001110010110100010000111100001000100001111000010001011010011000L, 90 0b0001100001111011101110100101100010001101001011101110111100011000L, 91 0b0001101101001011101111000011110111011110000111101110100101111000L, 92 0b0001111110000100010001011010011101110010110100010001000011111000L, 93 0b0001111111111111111111111111111111111111111111111111111111111000L, 94 0b0001111111111111111111111111111111111111111111111111111111111000L, 95 0b0000000000000000000000000000000000000000000000000000000000000000L, 96 0b0000000000000000000000000000000000000000000000000000000000000000L, 97 0b0000000000000000000000000000000000000000000000000000000000000000L, 98 }), 99 100 /** 101 * A unique (64x64) image for the test IME 2. 102 */ 103 IME2(new long[] { 104 0b0000000000000000000000000000000000000000000000000000000000000000L, 105 0b0000000000000000000000000000000000000000000000000000000000000000L, 106 0b0000000000000000000000000000000000000000000000000000000000000000L, 107 0b0001111111111111111111111111111111111111111111111111111111111000L, 108 0b0001111111111111111111111111111111111111111111111111111111111000L, 109 0b0001101101001011101111000011110111011110000111101110100101111000L, 110 0b0001111100000100110001010010011111110010010100011001000001111000L, 111 0b0001110010110100010000111100001000100001111000010001011010011000L, 112 0b0001100011111011001110101101100000001101101011100110111110011000L, 113 0b0001101101001011101111000011110111011110000111101110100101111000L, 114 0b0001101100000100110001010010011111110010010100011001000001111000L, 115 0b0001101010110100010000111100001000100001111000010001011010111000L, 116 0b0001111011111011001110101101100000001101101011100110111110111000L, 117 0b0001110001001011101111000011110111011110000111101110100100011000L, 118 0b0001100000000100110001010010011111110010010100011001000000011000L, 119 0b0001101100110100010000111100001000100001111000010001011001111000L, 120 0b0001111110111011001110101101100000001101101011100110111011111000L, 121 0b0001110010101011101111000011110111011110000111101110101010011000L, 122 0b0001100001100100110001010010011111110010010100011001001100011000L, 123 0b0001101101000100010000111100001000100001111000010001000101111000L, 124 0b0001111110000011001110101101100000001101101011100110000011111000L, 125 0b0001110010110111101111000011110111011110000111101111011010011000L, 126 0b0001100001111010110001010010011111110010010100011010111100011000L, 127 0b0001101101001011010000111100001000100001111000010110100101111000L, 128 0b0001111110000100001110101101100000001101101011100001000011111000L, 129 0b0001110010110100011111000011110111011110000111110001011010011000L, 130 0b0001100001111011101001010010011111110010010100101110111100011000L, 131 0b0001101101001011101100111100001000100001111001101110100101111000L, 132 0b0001111110000100010000101101100000001101101000010001000011111000L, 133 0b0001110010110100010000100011110111011110001000010001011010011000L, 134 0b0001100001111011101110100010011111110010001011101110111100011000L, 135 0b0001101101001011101111000010001000100010000111101110100101111000L, 136 0b0001111110000100010001011010011101110010110100010001000011111000L, 137 0b0001110010110100010000111100001000100001111000010001011010011000L, 138 0b0001100001111011101110100101100010001101001011101110111100011000L, 139 0b0001101101001011101111000011110111011110000111101110100101111000L, 140 0b0001111110000100010001011010011101110010110100010001000011111000L, 141 0b0001110010110100010000111100001000100001111000010001011010011000L, 142 0b0001100001111011101110100101100010001101001011101110111100011000L, 143 0b0001101101001011101111000011110111011110000111101110100101111000L, 144 0b0001111110000100010001011010011101110010110100010001000011111000L, 145 0b0001110010110100010000111100001000100001111000010001011010011000L, 146 0b0001100001111011101110100101100010001101001011101110111100011000L, 147 0b0001101101001011101111000011110111011110000111101110100101111000L, 148 0b0001111110000100010001011010011101110010110100010001000011111000L, 149 0b0001110010110100010000111100001000100001111000010001011010011000L, 150 0b0001100001111011101110100101100010001101001011101110111100011000L, 151 0b0001101101001011101111000011110111011110000111101110100101111000L, 152 0b0001111110000100010001011010011101110010110100010001000011111000L, 153 0b0001110010110100010000111100001000100001111000010001011010011000L, 154 0b0001100001111011101110100101100010001101001011101110111100011000L, 155 0b0001101101001011101111000011110111011110000111101110100101111000L, 156 0b0001111110000100010001011010011101110010110100010001000011111000L, 157 0b0001110010110100010000111100001000100001111000010001011010011000L, 158 0b0001100001111011101110100101100010001101001011101110111100011000L, 159 0b0001101101001011101111000011110111011110000111101110100101111000L, 160 0b0001111110000100010001011010011101110010110100010001000011111000L, 161 0b0001110010110100010000111100001000100001111000010001011010011000L, 162 0b0001100001111011101110100101100010001101001011101110111100011000L, 163 0b0001111111111111111111111111111111111111111111111111111111111000L, 164 0b0001111111111111111111111111111111111111111111111111111111111000L, 165 0b0000000000000000000000000000000000000000000000000000000000000000L, 166 0b0000000000000000000000000000000000000000000000000000000000000000L, 167 0b0000000000000000000000000000000000000000000000000000000000000000L, 168 }); 169 170 private final BitmapImage mImage; 171 Watermark(@onNull @izevalue = 64) long[] bitImage)172 Watermark(@NonNull @Size(value = 64) long[] bitImage) { 173 final int size = 64; 174 if (bitImage.length != size) { 175 throw new UnsupportedOperationException("bitImage.length=" + bitImage.length 176 + " must be " + size); 177 } 178 final long mask = 0b1000000000000000000000000000000000000000000000000000000000000000L; 179 final int[] pixels = new int[size * size]; 180 for (int y = 0; y < size; ++y) { 181 for (int x = 0; x < size; ++x) { 182 pixels[y * size + x] = 183 ((bitImage[y] << x) & mask) == mask ? 0xffffffff : 0xff000000; 184 } 185 } 186 mImage = new BitmapImage(pixels, size, size); 187 } 188 189 /** 190 * @return {@link Bitmap} object of the unique image. 191 */ toBitmap()192 public Bitmap toBitmap() { 193 return mImage.toBitmap(); 194 } 195 196 /** 197 * Check the unique image can be found in the specified {@link Bitmap}. 198 * 199 * @param bitmap {@link Bitmap} to be checked. 200 * @return {@code true} if the corresponding unique image is found in the {@code bitmap}. 201 */ isContainedIn(@onNull Bitmap bitmap)202 public boolean isContainedIn(@NonNull Bitmap bitmap) { 203 final BitmapImage targetImage = BitmapImage.createFromBitmap(bitmap); 204 // Search from the bottom line with an assumption that the IME is shown at the bottom. 205 for (int offsetY = targetImage.getHeight() - 1; offsetY >= 0; --offsetY) { 206 for (int offsetX = 0; offsetX < targetImage.getWidth(); ++offsetX) { 207 if (mImage.robustMatch(targetImage, offsetX, offsetY)) { 208 return true; 209 } 210 } 211 } 212 return false; 213 } 214 } 215