Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 153611de authored by Roman Birg's avatar Roman Birg Committed by Gerrit Code Review
Browse files

LockPatternUtils: add a static way to retreive pattern from string



The non-static method assumed that the pattern would be the same size as
the currently set length, but when we are setting up a new pattern, it
may have a different length.

Also add a utility method to compare two patterns.

Ref: CYNGNOS-661

Change-Id: I73d0e8d016c5c93ba64ab697579ac39d8b78716a
Signed-off-by: default avatarRoman Birg <roman@cyngn.com>
parent 13f8e029
Loading
Loading
Loading
Loading
+36 −3
Original line number Diff line number Diff line
@@ -1086,16 +1086,29 @@ public class LockPatternUtils {
     * @return The pattern.
     */
    public List<LockPatternView.Cell> stringToPattern(String string) {
        return stringToPattern(string, getLockPatternSize());
    }

    /**
     * Deserialize a pattern.
     * @return The pattern.
     */
    public static List<LockPatternView.Cell> stringToPattern(String string, byte size) {
        List<LockPatternView.Cell> result = Lists.newArrayList();

        final byte size = getLockPatternSize();
        LockPatternView.Cell.updateSize(size);
        LockPatternView.Cell[][] tmp = new LockPatternView.Cell[size][size];
        for(int i = 0; i < size; i++) {
            for(int j = 0; j < size; j++) {
                tmp[i][j] = new LockPatternView.Cell(i, j, size);
            }
        }

        final byte[] bytes = string.getBytes();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            result.add(LockPatternView.Cell.of(b / size, b % size, size));
            result.add(LockPatternView.Cell.of(tmp, b / size, b % size, size));
        }

        return result;
    }

@@ -1128,6 +1141,26 @@ public class LockPatternUtils {
        return new String(res);
    }

    /**
     * Check whether two patterns match
     */
    public static boolean patternMatches(List<LockPatternView.Cell> p1,
                                         List<LockPatternView.Cell> p2) {
        if (p1 == null || p2 == null) {
            return false;
        }
        if (p1.size() != p2.size()) {
            return false;
        }
        byte size = (byte) p1.size();
        for(int i = 0; i < size; i++) {
            if (!p1.get(i).equals(p2.get(i))) {
                return false;
            }
        }
        return true;
    }

    /*
     * Generate an SHA-1 hash for the pattern. Not the most secure, but it is
     * at least a second level of protection. First level is that the file
+18 −5
Original line number Diff line number Diff line
@@ -149,7 +149,7 @@ public class LockPatternView extends View {
         * @param row The row of the cell.
         * @param column The column of the cell.
         */
        private Cell(int row, int column, byte size) {
        /* package */ Cell(int row, int column, byte size) {
            checkRange(row, column, size);
            this.row = row;
            this.column = column;
@@ -168,8 +168,12 @@ public class LockPatternView extends View {
         * @param column The column of the cell.
         */
        public static synchronized Cell of(int row, int column, byte size) {
            return of(sCells, row, column, size);
        }

        public static Cell of(Cell[][] cells, int row, int column, byte size) {
            checkRange(row, column, size);
            return sCells[row][column];
            return cells[row][column];
        }

        public static void updateSize(byte size) {
@@ -190,6 +194,16 @@ public class LockPatternView extends View {
            }
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof Cell) {
                final Cell other = (Cell) o;
                return other.getColumn() == getColumn()
                        && other.getRow() == getRow();
            }
            return false;
        }

        public String toString() {
            return "(row=" + row + ",clmn=" + column + ")";
        }
@@ -1108,9 +1122,6 @@ public class LockPatternView extends View {
    protected void onRestoreInstanceState(Parcelable state) {
        final SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        setPattern(
                DisplayMode.Correct,
                mLockPatternUtils.stringToPattern(ss.getSerializedPattern()));
        mPatternDisplayMode = DisplayMode.values()[ss.getDisplayMode()];
        mPatternSize = ss.getPatternSize();
        mInputEnabled = ss.isInputEnabled();
@@ -1118,6 +1129,8 @@ public class LockPatternView extends View {
        mEnableHapticFeedback = ss.isTactileFeedbackEnabled();
        mVisibleDots = ss.isVisibleDots();
        mShowErrorPath = ss.isShowErrorPath();
        setPattern(DisplayMode.Correct,
                LockPatternUtils.stringToPattern(ss.getSerializedPattern(), mPatternSize));
    }

    /**