1 /* 2 * Copyright (C) 2019 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.car.setupwizardlib; 18 19 import com.android.car.setupwizardlib.InitialLockSetupConstants.ValidateLockFlags; 20 21 /** 22 * Provides helper methods for the usage of the InitialLockSetupService. 23 */ 24 public class InitialLockSetupHelper { 25 26 /** 27 * Checks the return flags from a valid lock check and returns true if the lock is valid. 28 */ isValidLockResultCode(@alidateLockFlags int flags)29 public static boolean isValidLockResultCode(@ValidateLockFlags int flags) { 30 return flags == 0; 31 } 32 33 /** 34 * Gets the byte representation of a pattern cell based on 0 indexed row and column. 35 * This should be a 3x3 pattern format. 36 */ getByteFromPatternCell(int row, int col)37 public static byte getByteFromPatternCell(int row, int col) { 38 return (byte) ((row * 3) + col + 1); 39 } 40 41 /** 42 * Returns the 0 indexed row of the pattern cell from a serialized byte pattern cell. 43 * This should be a 3x3 pattern format. 44 */ getPatternCellRowFromByte(byte cell)45 public static int getPatternCellRowFromByte(byte cell) { 46 return (byte) ((cell - 1) / 3); 47 } 48 49 /** 50 * Returns the 0 indexed column of the pattern cell from a serialized byte pattern cell. 51 */ getPatternCellColumnFromByte(byte cell)52 public static int getPatternCellColumnFromByte(byte cell) { 53 return (byte) ((cell - 1) % 3); 54 } 55 56 /** 57 * Converts a {@link CharSequence} into an array of bytes. This is for security reasons to avoid 58 * storing strings in memory. 59 */ charSequenceToByteArray(CharSequence chars)60 public static byte[] charSequenceToByteArray(CharSequence chars) { 61 if (chars == null) { 62 return null; 63 } 64 byte[] byteArray = new byte[chars.length()]; 65 for (int i = 0; i < chars.length(); i++) { 66 byteArray[i] = (byte) chars.charAt(i); 67 } 68 return byteArray; 69 } 70 71 /** 72 * Converts an array of bytes into a {@link CharSequence}. 73 */ byteArrayToCharSequence(byte[] input)74 public static CharSequence byteArrayToCharSequence(byte[] input) { 75 if (input == null) { 76 return null; 77 } 78 StringBuffer charSequence = new StringBuffer(); 79 for (int i = 0; i < input.length; i++) { 80 charSequence.append((char) input[i]); 81 } 82 return charSequence; 83 } 84 85 /** Return an ASCII-equivalent array of character digits for a numeric byte input. */ getNumericEquivalentByteArray(byte[] input)86 public static byte[] getNumericEquivalentByteArray(byte[] input) { 87 byte[] output = new byte[input.length]; 88 for (int i = 0; i < input.length; i++) { 89 output[i] = (byte) (input[i] + 48); 90 } 91 return output; 92 } 93 } 94