mKeys = new ArrayList<>();
+
+ /**
+ * Edge flags for this row of keys. Possible values that can be assigned are
+ * {@link Keyboard#EDGE_TOP EDGE_TOP} and {@link Keyboard#EDGE_BOTTOM EDGE_BOTTOM}
+ */
+ public int rowEdgeFlags;
+
+ /**
+ * The keyboard mode for this row
+ */
+ public int mode;
+
+ private Keyboard parent;
+
+ public Row(Keyboard parent) {
+ this.parent = parent;
+ }
+
+ public Row(Resources res, Keyboard parent, XmlResourceParser parser) {
+ this.parent = parent;
+ TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser),
+ R.styleable.Keyboard);
+ defaultWidth = getDimensionOrFraction(a, R.styleable.Keyboard_keyWidth,
+ parent.mDisplayWidth, parent.mDefaultWidth);
+ defaultHeight = getDimensionOrFraction(a, R.styleable.Keyboard_keyHeight,
+ parent.mDisplayHeight, parent.mDefaultHeight);
+ defaultHorizontalGap = getDimensionOrFraction(a, R.styleable.Keyboard_horizontalGap,
+ parent.mDisplayWidth, parent.mDefaultHorizontalGap);
+ verticalGap = getDimensionOrFraction(a, R.styleable.Keyboard_verticalGap,
+ parent.mDisplayHeight, parent.mDefaultVerticalGap);
+ a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard_Row);
+ rowEdgeFlags = a.getInt(R.styleable.Keyboard_Row_rowEdgeFlags, 0);
+ mode = a.getResourceId(R.styleable.Keyboard_Row_keyboardMode, 0);
+ a.recycle();
+ }
+ }
+
+ /**
+ * Class for describing the position and characteristics of a single key in the keyboard.
+ *
+ * ref R.styleable#Keyboard_keyWidth
+ * ref R.styleable#Keyboard_keyHeight
+ * ref R.styleable#Keyboard_horizontalGap
+ * ref R.styleable#Keyboard_Key_codes
+ * ref R.styleable#Keyboard_Key_keyIcon
+ * ref R.styleable#Keyboard_Key_keyLabel
+ * ref R.styleable#Keyboard_Key_iconPreview
+ * ref R.styleable#Keyboard_Key_isSticky
+ * ref R.styleable#Keyboard_Key_isRepeatable
+ * ref R.styleable#Keyboard_Key_isModifier
+ * ref R.styleable#Keyboard_Key_popupKeyboard
+ * ref R.styleable#Keyboard_Key_popupCharacters
+ * ref R.styleable#Keyboard_Key_keyOutputText
+ * ref R.styleable#Keyboard_Key_keyEdgeFlags
+ */
+ public static class Key {
+ /**
+ * All the key codes (unicode or custom code) that this key could generate, zero'th
+ * being the most important.
+ */
+ public int[] codes;
+
+ /**
+ * Label to display
+ */
+ public CharSequence label;
+
+ /**
+ * Icon to display instead of a label. Icon takes precedence over a label
+ */
+ public Drawable icon;
+ /**
+ * Preview version of the icon, for the preview popup
+ */
+ public Drawable iconPreview;
+ /**
+ * Width of the key, not including the gap
+ */
+ public int width;
+ /**
+ * Height of the key, not including the gap
+ */
+ public int height;
+ /**
+ * The horizontal gap before this key
+ */
+ public int gap;
+ /**
+ * Whether this key is sticky, i.e., a toggle key
+ */
+ public boolean sticky;
+ /**
+ * X coordinate of the key in the keyboard layout
+ */
+ public int x;
+ /**
+ * Y coordinate of the key in the keyboard layout
+ */
+ public int y;
+ /**
+ * The current pressed state of this key
+ */
+ public boolean pressed;
+ /**
+ * If this is a sticky key, is it on?
+ */
+ public boolean on;
+ /**
+ * Text to output when pressed. This can be multiple characters, like ".com"
+ */
+ public CharSequence text;
+ /**
+ * Popup characters
+ */
+ public CharSequence popupCharacters;
+
+ /**
+ * Flags that specify the anchoring to edges of the keyboard for detecting touch events
+ * that are just out of the boundary of the key. This is a bit mask of
+ * {@link Keyboard#EDGE_LEFT}, {@link Keyboard#EDGE_RIGHT}, {@link Keyboard#EDGE_TOP} and
+ * {@link Keyboard#EDGE_BOTTOM}.
+ */
+ public int edgeFlags;
+ /**
+ * Whether this is a modifier key, such as Shift or Alt
+ */
+ public boolean modifier;
+ /**
+ * The keyboard that this key belongs to
+ */
+ private Keyboard keyboard;
+ /**
+ * If this key pops up a mini keyboard, this is the resource id for the XML layout for that
+ * keyboard.
+ */
+ public int popupResId;
+ /**
+ * Whether this key repeats itself when held down
+ */
+ public boolean repeatable;
+
+
+ private final static int[] KEY_STATE_NORMAL_ON = {
+ android.R.attr.state_checkable,
+ android.R.attr.state_checked
+ };
+
+ private final static int[] KEY_STATE_PRESSED_ON = {
+ android.R.attr.state_pressed,
+ android.R.attr.state_checkable,
+ android.R.attr.state_checked
+ };
+
+ private final static int[] KEY_STATE_NORMAL_OFF = {
+ android.R.attr.state_checkable
+ };
+
+ private final static int[] KEY_STATE_PRESSED_OFF = {
+ android.R.attr.state_pressed,
+ android.R.attr.state_checkable
+ };
+
+ private final static int[] KEY_STATE_NORMAL = {
+ };
+
+ private final static int[] KEY_STATE_PRESSED = {
+ android.R.attr.state_pressed
+ };
+
+ /**
+ * Create an empty key with no attributes.
+ */
+ public Key(Row parent) {
+ keyboard = parent.parent;
+ height = parent.defaultHeight;
+ width = parent.defaultWidth;
+ gap = parent.defaultHorizontalGap;
+ edgeFlags = parent.rowEdgeFlags;
+ }
+
+ /**
+ * Create a key with the given top-left coordinate and extract its attributes from
+ * the XML parser.
+ *
+ * @param res resources associated with the caller's context
+ * @param parent the row that this key belongs to. The row must already be attached to
+ * a {@link Keyboard}.
+ * @param x the x coordinate of the top-left
+ * @param y the y coordinate of the top-left
+ * @param parser the XML parser containing the attributes for this key
+ */
+ public Key(Resources res, Row parent, int x, int y, XmlResourceParser parser) {
+ this(parent);
+
+ this.x = x;
+ this.y = y;
+
+ TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard);
+
+ width = getDimensionOrFraction(a, R.styleable.Keyboard_keyWidth,
+ keyboard.mDisplayWidth, parent.defaultWidth);
+ height = getDimensionOrFraction(a, R.styleable.Keyboard_keyHeight,
+ keyboard.mDisplayHeight, parent.defaultHeight);
+ gap = getDimensionOrFraction(a, R.styleable.Keyboard_horizontalGap,
+ keyboard.mDisplayWidth, parent.defaultHorizontalGap);
+ a.recycle();
+ a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard_Key);
+ this.x += gap;
+ TypedValue codesValue = new TypedValue();
+ a.getValue(R.styleable.Keyboard_Key_codes, codesValue);
+ if (codesValue.type == TypedValue.TYPE_INT_DEC
+ || codesValue.type == TypedValue.TYPE_INT_HEX) {
+ codes = new int[]{codesValue.data};
+ } else if (codesValue.type == TypedValue.TYPE_STRING) {
+ codes = parseCSV(codesValue.string.toString());
+ }
+
+ iconPreview = a.getDrawable(R.styleable.Keyboard_Key_iconPreview);
+ if (iconPreview != null) {
+ iconPreview.setBounds(0, 0, iconPreview.getIntrinsicWidth(),
+ iconPreview.getIntrinsicHeight());
+ }
+ popupCharacters = a.getText(R.styleable.Keyboard_Key_popupCharacters);
+ popupResId = a.getResourceId(R.styleable.Keyboard_Key_popupKeyboard, 0);
+ repeatable = a.getBoolean(R.styleable.Keyboard_Key_isRepeatable, false);
+ modifier = a.getBoolean(R.styleable.Keyboard_Key_isModifier, false);
+ sticky = a.getBoolean(R.styleable.Keyboard_Key_isSticky, false);
+ edgeFlags = a.getInt(R.styleable.Keyboard_Key_keyEdgeFlags, 0);
+ edgeFlags |= parent.rowEdgeFlags;
+
+ icon = a.getDrawable(R.styleable.Keyboard_Key_keyIcon);
+ if (icon != null) {
+ icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight());
+ }
+ label = a.getText(R.styleable.Keyboard_Key_keyLabel);
+ text = a.getText(R.styleable.Keyboard_Key_keyOutputText);
+
+ if (codes == null && !TextUtils.isEmpty(label)) {
+ codes = new int[]{label.charAt(0)};
+ }
+ a.recycle();
+ }
+
+ /**
+ * Informs the key that it has been pressed, in case it needs to change its appearance or
+ * state.
+ *
+ * @see #onReleased(boolean)
+ */
+ public void onPressed() {
+ pressed = !pressed;
+ }
+
+ /**
+ * Changes the pressed state of the key.
+ *
+ * Toggled state of the key will be flipped when all the following conditions are
+ * fulfilled:
+ *
+ *
+ * - This is a sticky key, that is, {@link #sticky} is {@code true}.
+ *
- The parameter {@code inside} is {@code true}.
+ *
- {@link android.os.Build.VERSION#SDK_INT} is greater than
+ * {@link android.os.Build.VERSION_CODES#LOLLIPOP_MR1}.
+ *
+ *
+ * @param inside whether the finger was released inside the key. Works only on Android M and
+ * later. See the method document for details.
+ * @see #onPressed()
+ */
+ public void onReleased(boolean inside) {
+ pressed = !pressed;
+ if (sticky && inside) {
+ on = !on;
+ }
+ }
+
+ int[] parseCSV(String value) {
+ int count = 0;
+ int lastIndex = 0;
+ if (value.length() > 0) {
+ count++;
+ while ((lastIndex = value.indexOf(",", lastIndex + 1)) > 0) {
+ count++;
+ }
+ }
+ int[] values = new int[count];
+ count = 0;
+ StringTokenizer st = new StringTokenizer(value, ",");
+ while (st.hasMoreTokens()) {
+ try {
+ values[count++] = Integer.parseInt(st.nextToken());
+ } catch (NumberFormatException nfe) {
+ Log.e(TAG, "Error parsing keycodes " + value);
+ }
+ }
+ return values;
+ }
+
+ /**
+ * Detects if a point falls inside this key.
+ *
+ * @param x the x-coordinate of the point
+ * @param y the y-coordinate of the point
+ * @return whether or not the point falls inside the key. If the key is attached to an edge,
+ * it will assume that all points between the key and the edge are considered to be inside
+ * the key.
+ */
+ public boolean isInside(int x, int y) {
+ boolean leftEdge = (edgeFlags & EDGE_LEFT) > 0;
+ boolean rightEdge = (edgeFlags & EDGE_RIGHT) > 0;
+ boolean topEdge = (edgeFlags & EDGE_TOP) > 0;
+ boolean bottomEdge = (edgeFlags & EDGE_BOTTOM) > 0;
+ return (x >= this.x || (leftEdge && x <= this.x + this.width))
+ && (x < this.x + this.width || (rightEdge && x >= this.x))
+ && (y >= this.y || (topEdge && y <= this.y + this.height))
+ && (y < this.y + this.height || (bottomEdge && y >= this.y));
+ }
+
+ /**
+ * Returns the square of the distance between the center of the key and the given point.
+ *
+ * @param x the x-coordinate of the point
+ * @param y the y-coordinate of the point
+ * @return the square of the distance of the point from the center of the key
+ */
+ public int squaredDistanceFrom(int x, int y) {
+ int xDist = this.x + width / 2 - x;
+ int yDist = this.y + height / 2 - y;
+ return xDist * xDist + yDist * yDist;
+ }
+
+ /**
+ * Returns the drawable state for the key, based on the current state and type of the key.
+ *
+ * @return the drawable state of the key.
+ * @see android.graphics.drawable.StateListDrawable#setState(int[])
+ */
+ public int[] getCurrentDrawableState() {
+ int[] states = KEY_STATE_NORMAL;
+
+ if (on) {
+ if (pressed) {
+ states = KEY_STATE_PRESSED_ON;
+ } else {
+ states = KEY_STATE_NORMAL_ON;
+ }
+ } else {
+ if (sticky) {
+ if (pressed) {
+ states = KEY_STATE_PRESSED_OFF;
+ } else {
+ states = KEY_STATE_NORMAL_OFF;
+ }
+ } else {
+ if (pressed) {
+ states = KEY_STATE_PRESSED;
+ }
+ }
+ }
+ return states;
+ }
+ }
+
+ /**
+ * Creates a keyboard from the given xml key layout file.
+ *
+ * @param context the application or service context
+ * @param xmlLayoutResId the resource file that contains the keyboard layout and keys.
+ */
+ public Keyboard(Context context, int xmlLayoutResId) {
+ this(context, xmlLayoutResId, 0);
+ }
+
+ /**
+ * Creates a keyboard from the given xml key layout file. Weeds out rows
+ * that have a keyboard mode defined but don't match the specified mode.
+ *
+ * @param context the application or service context
+ * @param xmlLayoutResId the resource file that contains the keyboard layout and keys.
+ * @param modeId keyboard mode identifier
+ * @param width sets width of keyboard
+ * @param height sets height of keyboard
+ */
+ public Keyboard(Context context, int xmlLayoutResId, int modeId, int width, int height) {
+ mDisplayWidth = width;
+ mDisplayHeight = height;
+
+ mDefaultHorizontalGap = 0;
+ mDefaultWidth = mDisplayWidth / 10;
+ mDefaultVerticalGap = 0;
+ mDefaultHeight = mDefaultWidth;
+ mKeys = new ArrayList<>();
+ mModifierKeys = new ArrayList<>();
+ mKeyboardMode = modeId;
+ loadKeyboard(context, context.getResources().getXml(xmlLayoutResId));
+ }
+
+ /**
+ * Creates a keyboard from the given xml key layout file. Weeds out rows
+ * that have a keyboard mode defined but don't match the specified mode.
+ *
+ * @param context the application or service context
+ * @param xmlLayoutResId the resource file that contains the keyboard layout and keys.
+ * @param modeId keyboard mode identifier
+ */
+ public Keyboard(Context context, int xmlLayoutResId, int modeId) {
+ DisplayMetrics dm = context.getResources().getDisplayMetrics();
+ mDisplayWidth = dm.widthPixels;
+ mDisplayHeight = dm.heightPixels;
+ //Log.v(TAG, "keyboard's display metrics:" + dm);
+
+ mDefaultHorizontalGap = 0;
+ mDefaultWidth = mDisplayWidth / 10;
+ mDefaultVerticalGap = 0;
+ mDefaultHeight = mDefaultWidth;
+ mKeys = new ArrayList<>();
+ mModifierKeys = new ArrayList<>();
+ mKeyboardMode = modeId;
+ loadKeyboard(context, context.getResources().getXml(xmlLayoutResId));
+ }
+
+ /**
+ * Creates a blank keyboard from the given resource file and populates it with the specified
+ * characters in left-to-right, top-to-bottom fashion, using the specified number of columns.
+ *
+ * If the specified number of columns is -1, then the keyboard will fit as many keys as
+ * possible in each row.
+ *
+ * @param context the application or service context
+ * @param layoutTemplateResId the layout template file, containing no keys.
+ * @param characters the list of characters to display on the keyboard. One key will be created
+ * for each character.
+ * @param columns the number of columns of keys to display. If this number is greater than the
+ * number of keys that can fit in a row, it will be ignored. If this number is -1, the
+ * keyboard will fit as many keys as possible in each row.
+ */
+ public Keyboard(Context context, int layoutTemplateResId,
+ CharSequence characters, int columns, int horizontalPadding) {
+ this(context, layoutTemplateResId);
+ int x = 0;
+ int y = 0;
+ int column = 0;
+ mTotalWidth = 0;
+
+ Row row = new Row(this);
+ row.defaultHeight = mDefaultHeight;
+ row.defaultWidth = mDefaultWidth;
+ row.defaultHorizontalGap = mDefaultHorizontalGap;
+ row.verticalGap = mDefaultVerticalGap;
+ row.rowEdgeFlags = EDGE_TOP | EDGE_BOTTOM;
+ final int maxColumns = columns == -1 ? Integer.MAX_VALUE : columns;
+ for (int i = 0; i < characters.length(); i++) {
+ char c = characters.charAt(i);
+ if (column >= maxColumns
+ || x + mDefaultWidth + horizontalPadding > mDisplayWidth) {
+ x = 0;
+ y += mDefaultVerticalGap + mDefaultHeight;
+ column = 0;
+ }
+ final Key key = new Key(row);
+ key.x = x;
+ key.y = y;
+ key.label = String.valueOf(c);
+ key.codes = new int[]{c};
+ column++;
+ x += key.width + key.gap;
+ mKeys.add(key);
+ row.mKeys.add(key);
+ if (x > mTotalWidth) {
+ mTotalWidth = x;
+ }
+ }
+ mTotalHeight = y + mDefaultHeight;
+ rows.add(row);
+ }
+
+ final void resize(int newWidth, int newHeight) {
+ int numRows = rows.size();
+ for (int rowIndex = 0; rowIndex < numRows; ++rowIndex) {
+ Row row = rows.get(rowIndex);
+ int numKeys = row.mKeys.size();
+ int totalGap = 0;
+ int totalWidth = 0;
+ for (int keyIndex = 0; keyIndex < numKeys; ++keyIndex) {
+ Key key = row.mKeys.get(keyIndex);
+ if (keyIndex > 0) {
+ totalGap += key.gap;
+ }
+ totalWidth += key.width;
+ }
+ if (totalGap + totalWidth > newWidth) {
+ int x = 0;
+ float scaleFactor = (float) (newWidth - totalGap) / totalWidth;
+ for (int keyIndex = 0; keyIndex < numKeys; ++keyIndex) {
+ Key key = row.mKeys.get(keyIndex);
+ key.width *= scaleFactor;
+ key.x = x;
+ x += key.width + key.gap;
+ }
+ }
+ }
+ mTotalWidth = newWidth;
+ }
+
+ public List getKeys() {
+ return mKeys;
+ }
+
+ /**
+ * Returns the total height of the keyboard
+ *
+ * @return the total height of the keyboard
+ */
+ public int getHeight() {
+ return mTotalHeight;
+ }
+
+ public int getMinWidth() {
+ return mTotalWidth;
+ }
+
+ public boolean setShifted(boolean shiftState) {
+ for (Key shiftKey : mShiftKeys) {
+ if (shiftKey != null) {
+ shiftKey.on = shiftState;
+ }
+ }
+ if (mShifted != shiftState) {
+ mShifted = shiftState;
+ return true;
+ }
+ return false;
+ }
+
+ public boolean isShifted() {
+ return mShifted;
+ }
+
+ private void computeNearestNeighbors() {
+ // Round-up so we don't have any pixels outside the grid
+ mCellWidth = (getMinWidth() + GRID_WIDTH - 1) / GRID_WIDTH;
+ mCellHeight = (getHeight() + GRID_HEIGHT - 1) / GRID_HEIGHT;
+ mGridNeighbors = new int[GRID_SIZE][];
+ int[] indices = new int[mKeys.size()];
+ final int gridWidth = GRID_WIDTH * mCellWidth;
+ final int gridHeight = GRID_HEIGHT * mCellHeight;
+ for (int x = 0; x < gridWidth; x += mCellWidth) {
+ for (int y = 0; y < gridHeight; y += mCellHeight) {
+ int count = 0;
+ for (int i = 0; i < mKeys.size(); i++) {
+ final Key key = mKeys.get(i);
+ if (key.squaredDistanceFrom(x, y) < mProximityThreshold ||
+ key.squaredDistanceFrom(x + mCellWidth - 1, y) < mProximityThreshold ||
+ key.squaredDistanceFrom(x + mCellWidth - 1, y + mCellHeight - 1)
+ < mProximityThreshold ||
+ key.squaredDistanceFrom(x, y + mCellHeight - 1) < mProximityThreshold) {
+ indices[count++] = i;
+ }
+ }
+ int[] cell = new int[count];
+ System.arraycopy(indices, 0, cell, 0, count);
+ mGridNeighbors[(y / mCellHeight) * GRID_WIDTH + (x / mCellWidth)] = cell;
+ }
+ }
+ }
+
+ /**
+ * Returns the indices of the keys that are closest to the given point.
+ *
+ * @param x the x-coordinate of the point
+ * @param y the y-coordinate of the point
+ * @return the array of integer indices for the nearest keys to the given point. If the given
+ * point is out of range, then an array of size zero is returned.
+ */
+ public int[] getNearestKeys(int x, int y) {
+ if (mGridNeighbors == null) computeNearestNeighbors();
+ if (x >= 0 && x < getMinWidth() && y >= 0 && y < getHeight()) {
+ int index = (y / mCellHeight) * GRID_WIDTH + (x / mCellWidth);
+ if (index < GRID_SIZE) {
+ return mGridNeighbors[index];
+ }
+ }
+ return new int[0];
+ }
+
+ protected Row createRowFromXml(Resources res, XmlResourceParser parser) {
+ return new Row(res, this, parser);
+ }
+
+ protected Key createKeyFromXml(Resources res, Row parent, int x, int y,
+ XmlResourceParser parser) {
+ return new Key(res, parent, x, y, parser);
+ }
+
+ private void loadKeyboard(Context context, XmlResourceParser parser) {
+ boolean inKey = false;
+ boolean inRow = false;
+ int x = 0;
+ int y = 0;
+ Key key = null;
+ Row currentRow = null;
+ Resources res = context.getResources();
+ boolean skipRow;
+
+ try {
+ int event;
+ while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
+ if (event == XmlResourceParser.START_TAG) {
+ String tag = parser.getName();
+ if (TAG_ROW.equals(tag)) {
+ inRow = true;
+ x = 0;
+ currentRow = createRowFromXml(res, parser);
+ rows.add(currentRow);
+ skipRow = currentRow.mode != 0 && currentRow.mode != mKeyboardMode;
+ if (skipRow) {
+ skipToEndOfRow(parser);
+ inRow = false;
+ }
+ } else if (TAG_KEY.equals(tag)) {
+ inKey = true;
+ key = createKeyFromXml(res, currentRow, x, y, parser);
+ mKeys.add(key);
+ if (key.codes[0] == KEYCODE_SHIFT) {
+ // Find available shift key slot and put this shift key in it
+ for (int i = 0; i < mShiftKeys.length; i++) {
+ if (mShiftKeys[i] == null) {
+ mShiftKeys[i] = key;
+ mShiftKeyIndices[i] = mKeys.size() - 1;
+ break;
+ }
+ }
+ mModifierKeys.add(key);
+ } else if (key.codes[0] == KEYCODE_ALT) {
+ mModifierKeys.add(key);
+ }
+ currentRow.mKeys.add(key);
+ } else if (TAG_KEYBOARD.equals(tag)) {
+ parseKeyboardAttributes(res, parser);
+ }
+ } else if (event == XmlResourceParser.END_TAG) {
+ if (inKey) {
+ inKey = false;
+ x += key.gap + key.width;
+ if (x > mTotalWidth) {
+ mTotalWidth = x;
+ }
+ } else if (inRow) {
+ inRow = false;
+ y += currentRow.verticalGap;
+ y += currentRow.defaultHeight;
+ }
+ }
+ }
+ } catch (Exception e) {
+ Log.e(TAG, "Parse error:" + e);
+ e.printStackTrace();
+ }
+ mTotalHeight = y - mDefaultVerticalGap;
+ }
+
+ private void skipToEndOfRow(XmlResourceParser parser)
+ throws XmlPullParserException, IOException {
+ int event;
+ while ((event = parser.next()) != XmlResourceParser.END_DOCUMENT) {
+ if (event == XmlResourceParser.END_TAG
+ && parser.getName().equals(TAG_ROW)) {
+ break;
+ }
+ }
+ }
+
+ private void parseKeyboardAttributes(Resources res, XmlResourceParser parser) {
+ TypedArray a = res.obtainAttributes(Xml.asAttributeSet(parser), R.styleable.Keyboard);
+
+ mDefaultWidth = getDimensionOrFraction(a, R.styleable.Keyboard_keyWidth,
+ mDisplayWidth, mDisplayWidth / 10);
+ mDefaultHeight = getDimensionOrFraction(a, R.styleable.Keyboard_keyHeight,
+ mDisplayHeight, 50);
+ mDefaultHorizontalGap = getDimensionOrFraction(a, R.styleable.Keyboard_horizontalGap,
+ mDisplayWidth, 0);
+ mDefaultVerticalGap = getDimensionOrFraction(a, R.styleable.Keyboard_verticalGap,
+ mDisplayHeight, 0);
+ mProximityThreshold = (int) (mDefaultWidth * SEARCH_DISTANCE);
+ mProximityThreshold = mProximityThreshold * mProximityThreshold; // Square it for comparison
+ a.recycle();
+ }
+
+ static int getDimensionOrFraction(TypedArray a, int index, int base, int defValue) {
+ TypedValue value = a.peekValue(index);
+ if (value == null) return defValue;
+ if (value.type == TypedValue.TYPE_DIMENSION) {
+ return a.getDimensionPixelOffset(index, defValue);
+ } else if (value.type == TypedValue.TYPE_FRACTION) {
+ // Round it to avoid values like 47.9999 from getting truncated
+ return Math.round(a.getFraction(index, base, base, defValue));
+ }
+ return defValue;
+ }
+}
diff --git a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardView.java b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardView.java
new file mode 100644
index 000000000..79c35f8e3
--- /dev/null
+++ b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/KeyboardView.java
@@ -0,0 +1,1513 @@
+/*
+ * Copyright (C) 2008-2009 Google Inc. 2022 J-Jamet
+ *
+ * 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.kunzisoft.keepass.magikeyboard;
+
+import static com.kunzisoft.keepass.magikeyboard.MagikeyboardService.KEY_BACK_KEYBOARD;
+import static com.kunzisoft.keepass.magikeyboard.MagikeyboardService.KEY_CHANGE_KEYBOARD;
+
+import android.content.Context;
+import android.content.res.TypedArray;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Paint;
+import android.graphics.Paint.Align;
+import android.graphics.PorterDuff;
+import android.graphics.Rect;
+import android.graphics.Typeface;
+import android.graphics.drawable.Drawable;
+import android.os.Build;
+import android.os.Handler;
+import android.os.Looper;
+import android.os.Message;
+import android.util.AttributeSet;
+import android.util.TypedValue;
+import android.view.GestureDetector;
+import android.view.Gravity;
+import android.view.LayoutInflater;
+import android.view.MotionEvent;
+import android.view.View;
+import android.view.ViewConfiguration;
+import android.view.ViewGroup.LayoutParams;
+import android.widget.PopupWindow;
+import android.widget.TextView;
+
+import androidx.annotation.RequiresApi;
+
+import com.kunzisoft.keepass.R;
+import com.kunzisoft.keepass.magikeyboard.Keyboard.Key;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * A view that renders a virtual {@link Keyboard}. It handles rendering of keys and
+ * detecting key presses and touch movements.
+ *
+ * ref R.styleable#KeyboardView_keyBackground
+ * ref R.styleable#KeyboardView_keyPreviewLayout
+ * ref R.styleable#KeyboardView_keyPreviewOffset
+ * ref R.styleable#KeyboardView_keyPreviewHeight
+ * ref R.styleable#KeyboardView_labelTextSize
+ * ref R.styleable#KeyboardView_keyTextSize
+ * ref R.styleable#KeyboardView_keyTextColor
+ * ref R.styleable#KeyboardView_verticalCorrection
+ * ref R.styleable#KeyboardView_popupLayout
+ */
+public class KeyboardView extends View implements View.OnClickListener {
+
+ /**
+ * Listener for virtual keyboard events.
+ */
+ public interface OnKeyboardActionListener {
+
+ /**
+ * Called when the user presses a key. This is sent before the {@link #onKey} is called.
+ * For keys that repeat, this is only called once.
+ *
+ * @param primaryCode the unicode of the key being pressed. If the touch is not on a valid
+ * key, the value will be zero.
+ */
+ void onPress(int primaryCode);
+
+ /**
+ * Called when the user releases a key. This is sent after the {@link #onKey} is called.
+ * For keys that repeat, this is only called once.
+ *
+ * @param primaryCode the code of the key that was released
+ */
+ void onRelease(int primaryCode);
+
+ /**
+ * Send a key press to the listener.
+ *
+ * @param primaryCode this is the key that was pressed
+ * @param keyCodes the codes for all the possible alternative keys
+ * with the primary code being the first. If the primary key code is
+ * a single character such as an alphabet or number or symbol, the alternatives
+ * will include other characters that may be on the same key or adjacent keys.
+ * These codes are useful to correct for accidental presses of a key adjacent to
+ * the intended key.
+ */
+ void onKey(int primaryCode, int[] keyCodes);
+
+ /**
+ * Sends a sequence of characters to the listener.
+ *
+ * @param text the sequence of characters to be displayed.
+ */
+ void onText(CharSequence text);
+
+ /**
+ * Called when the user quickly moves the finger from right to left.
+ */
+ void swipeLeft();
+
+ /**
+ * Called when the user quickly moves the finger from left to right.
+ */
+ void swipeRight();
+
+ /**
+ * Called when the user quickly moves the finger from up to down.
+ */
+ void swipeDown();
+
+ /**
+ * Called when the user quickly moves the finger from down to up.
+ */
+ void swipeUp();
+ }
+
+ private static final boolean DEBUG = false;
+ private static final int NOT_A_KEY = -1;
+ private static final int[] KEY_DELETE = {Keyboard.KEYCODE_DELETE};
+ private static final int[] LONG_PRESSABLE_STATE_SET = {R.attr.state_long_pressable};
+
+ private Keyboard mKeyboard;
+ private int mCurrentKeyIndex = NOT_A_KEY;
+ private int mLabelTextSize;
+ private int mKeyTextSize;
+ private int mKeyTextColor;
+ private float mShadowRadius;
+ private int mShadowColor;
+ private float mBackgroundDimAmount;
+
+ private TextView mPreviewText;
+ private PopupWindow mPreviewPopup;
+ private int mPreviewTextSizeLarge;
+ private int mPreviewOffset;
+ private int mPreviewHeight;
+ // Working variable
+ private final int[] mCoordinates = new int[2];
+
+ private PopupWindow mPopupKeyboard;
+ private View mMiniKeyboardContainer;
+ private KeyboardView mMiniKeyboard;
+ private boolean mMiniKeyboardOnScreen;
+ private View mPopupParent;
+ private int mMiniKeyboardOffsetX;
+ private int mMiniKeyboardOffsetY;
+ private Map mMiniKeyboardCache;
+ private Key[] mKeys;
+
+ /**
+ * Listener for {@link OnKeyboardActionListener}.
+ */
+ private OnKeyboardActionListener mKeyboardActionListener;
+
+ private static final int MSG_SHOW_PREVIEW = 1;
+ private static final int MSG_REMOVE_PREVIEW = 2;
+ private static final int MSG_REPEAT = 3;
+ private static final int MSG_LONGPRESS = 4;
+
+ private static final int DELAY_BEFORE_PREVIEW = 0;
+ private static final int DELAY_AFTER_PREVIEW = 70;
+ private static final int DEBOUNCE_TIME = 70;
+
+ private int mVerticalCorrection;
+ private int mProximityThreshold;
+
+ private boolean mPreviewCentered = false;
+ private boolean mShowPreview = true;
+ private boolean mShowTouchPoints = true;
+ private int mPopupPreviewX;
+ private int mPopupPreviewY;
+
+ private int mLastX;
+ private int mLastY;
+ private int mStartX;
+ private int mStartY;
+
+ private boolean mProximityCorrectOn;
+
+ private Paint mPaint;
+ private Rect mPadding;
+
+ private long mDownTime;
+ private long mLastMoveTime;
+ private int mLastKey;
+ private int mLastCodeX;
+ private int mLastCodeY;
+ private int mCurrentKey = NOT_A_KEY;
+ private int mDownKey = NOT_A_KEY;
+ private long mLastKeyTime;
+ private long mCurrentKeyTime;
+ private int[] mKeyIndices = new int[12];
+ private GestureDetector mGestureDetector;
+ private int mPopupX;
+ private int mPopupY;
+ private int mRepeatKeyIndex = NOT_A_KEY;
+ private int mPopupLayout;
+ private boolean mAbortKey;
+ private Key mInvalidatedKey;
+ private Rect mClipRegion = new Rect(0, 0, 0, 0);
+ private boolean mPossiblePoly;
+ private SwipeTracker mSwipeTracker = new SwipeTracker();
+ private int mSwipeThreshold;
+ private boolean mDisambiguateSwipe;
+
+ // Variables for dealing with multiple pointers
+ private int mOldPointerCount = 1;
+ private float mOldPointerX;
+ private float mOldPointerY;
+
+ private Drawable mKeyBackground;
+
+ private static final int REPEAT_INTERVAL = 50; // ~20 keys per second
+ private static final int REPEAT_START_DELAY = 400;
+ private static final int LONGPRESS_TIMEOUT = ViewConfiguration.getLongPressTimeout();
+
+ private static int MAX_NEARBY_KEYS = 12;
+ private int[] mDistances = new int[MAX_NEARBY_KEYS];
+
+ // For multi-tap
+ private int mLastSentIndex;
+ private int mTapCount;
+ private long mLastTapTime;
+ private boolean mInMultiTap;
+ private static final int MULTITAP_INTERVAL = 800; // milliseconds
+ private StringBuilder mPreviewLabel = new StringBuilder(1);
+
+ /**
+ * Whether the keyboard bitmap needs to be redrawn before it's blitted.
+ **/
+ private boolean mDrawPending;
+ /**
+ * The dirty region in the keyboard bitmap
+ */
+ private Rect mDirtyRect = new Rect();
+ /**
+ * The keyboard bitmap for faster updates
+ */
+ private Bitmap mBuffer;
+ /**
+ * Notes if the keyboard just changed, so that we could possibly reallocate the mBuffer.
+ */
+ private boolean mKeyboardChanged;
+ /**
+ * The canvas for the above mutable keyboard bitmap
+ */
+ private Canvas mCanvas;
+
+ Handler mHandler;
+
+ public KeyboardView(Context context, AttributeSet attrs) {
+ this(context, attrs, R.attr.keyboardViewStyle);
+ }
+
+ public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ init(context, attrs, defStyleAttr, 0);
+ }
+
+ @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
+ public KeyboardView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+ super(context, attrs, defStyleAttr, defStyleRes);
+ init(context, attrs, defStyleAttr, defStyleRes);
+ }
+
+ public void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
+ TypedArray a = context.obtainStyledAttributes(
+ attrs, R.styleable.KeyboardView, defStyleAttr, defStyleRes);
+
+ LayoutInflater inflate =
+ (LayoutInflater) context
+ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+
+ int previewLayout = 0;
+ int keyTextSize = 0;
+
+ int n = a.getIndexCount();
+
+ for (int i = 0; i < n; i++) {
+ int attr = a.getIndex(i);
+
+ if (attr == R.styleable.KeyboardView_keyBackground) {
+ mKeyBackground = a.getDrawable(attr);
+ } else if (attr == R.styleable.KeyboardView_verticalCorrection) {
+ mVerticalCorrection = a.getDimensionPixelOffset(attr, 0);
+ } else if (attr == R.styleable.KeyboardView_keyPreviewLayout) {
+ previewLayout = a.getResourceId(attr, 0);
+ } else if (attr == R.styleable.KeyboardView_keyPreviewOffset) {
+ mPreviewOffset = a.getDimensionPixelOffset(attr, 0);
+ } else if (attr == R.styleable.KeyboardView_keyPreviewHeight) {
+ mPreviewHeight = a.getDimensionPixelSize(attr, 80);
+ } else if (attr == R.styleable.KeyboardView_keyTextSize) {
+ mKeyTextSize = a.getDimensionPixelSize(attr, 18);
+ } else if (attr == R.styleable.KeyboardView_keyTextColor) {
+ mKeyTextColor = a.getColor(attr, 0xFF000000);
+ } else if (attr == R.styleable.KeyboardView_labelTextSize) {
+ mLabelTextSize = a.getDimensionPixelSize(attr, 14);
+ } else if (attr == R.styleable.KeyboardView_popupLayout) {
+ mPopupLayout = a.getResourceId(attr, 0);
+ } else if (attr == R.styleable.KeyboardView_shadowColor) {
+ mShadowColor = a.getColor(attr, 0);
+ } else if (attr == R.styleable.KeyboardView_shadowRadius) {
+ mShadowRadius = a.getFloat(attr, 0f);
+ }
+ }
+
+ mBackgroundDimAmount = 0.6f;
+
+ mPreviewPopup = new PopupWindow(context);
+ if (previewLayout != 0) {
+ mPreviewText = (TextView) inflate.inflate(previewLayout, null);
+ mPreviewTextSizeLarge = (int) mPreviewText.getTextSize();
+ mPreviewPopup.setContentView(mPreviewText);
+ mPreviewPopup.setBackgroundDrawable(null);
+ } else {
+ mShowPreview = false;
+ }
+
+ mPreviewPopup.setTouchable(false);
+
+ mPopupKeyboard = new PopupWindow(context);
+ mPopupKeyboard.setBackgroundDrawable(null);
+
+ mPopupParent = this;
+
+ mPaint = new Paint();
+ mPaint.setAntiAlias(true);
+ mPaint.setTextSize(keyTextSize);
+ mPaint.setTextAlign(Align.CENTER);
+ mPaint.setAlpha(255);
+
+ mPadding = new Rect(0, 0, 0, 0);
+ mMiniKeyboardCache = new HashMap();
+ mKeyBackground.getPadding(mPadding);
+
+ mSwipeThreshold = (int) (500 * getResources().getDisplayMetrics().density);
+ mDisambiguateSwipe = true;
+
+ resetMultiTap();
+ }
+
+ @Override
+ protected void onAttachedToWindow() {
+ super.onAttachedToWindow();
+ initGestureDetector();
+ if (mHandler == null) {
+ mHandler = new Handler(Looper.getMainLooper()) {
+ @Override
+ public void handleMessage(Message msg) {
+ switch (msg.what) {
+ case MSG_SHOW_PREVIEW:
+ showKey(msg.arg1);
+ break;
+ case MSG_REMOVE_PREVIEW:
+ mPreviewText.setVisibility(INVISIBLE);
+ break;
+ case MSG_REPEAT:
+ if (repeatKey()) {
+ Message repeat = Message.obtain(this, MSG_REPEAT);
+ sendMessageDelayed(repeat, REPEAT_INTERVAL);
+ }
+ break;
+ case MSG_LONGPRESS:
+ openPopupIfRequired((MotionEvent) msg.obj);
+ break;
+ }
+ }
+ };
+ }
+ }
+
+ private void initGestureDetector() {
+ if (mGestureDetector == null) {
+ mGestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
+ @Override
+ public boolean onFling(MotionEvent me1, MotionEvent me2,
+ float velocityX, float velocityY) {
+ if (mPossiblePoly) return false;
+ final float absX = Math.abs(velocityX);
+ final float absY = Math.abs(velocityY);
+ float deltaX = me2.getX() - me1.getX();
+ float deltaY = me2.getY() - me1.getY();
+ int travelX = getWidth() / 2; // Half the keyboard width
+ int travelY = getHeight() / 2; // Half the keyboard height
+ mSwipeTracker.computeCurrentVelocity(1000);
+ final float endingVelocityX = mSwipeTracker.getXVelocity();
+ final float endingVelocityY = mSwipeTracker.getYVelocity();
+ boolean sendDownKey = false;
+ if (velocityX > mSwipeThreshold && absY < absX && deltaX > travelX) {
+ if (mDisambiguateSwipe && endingVelocityX < velocityX / 4) {
+ sendDownKey = true;
+ } else {
+ swipeRight();
+ return true;
+ }
+ } else if (velocityX < -mSwipeThreshold && absY < absX && deltaX < -travelX) {
+ if (mDisambiguateSwipe && endingVelocityX > velocityX / 4) {
+ sendDownKey = true;
+ } else {
+ swipeLeft();
+ return true;
+ }
+ } else if (velocityY < -mSwipeThreshold && absX < absY && deltaY < -travelY) {
+ if (mDisambiguateSwipe && endingVelocityY > velocityY / 4) {
+ sendDownKey = true;
+ } else {
+ swipeUp();
+ return true;
+ }
+ } else if (velocityY > mSwipeThreshold && absX < absY / 2 && deltaY > travelY) {
+ if (mDisambiguateSwipe && endingVelocityY < velocityY / 4) {
+ sendDownKey = true;
+ } else {
+ swipeDown();
+ return true;
+ }
+ }
+
+ if (sendDownKey) {
+ detectAndSendKey(mDownKey, mStartX, mStartY, me1.getEventTime());
+ }
+ return false;
+ }
+ });
+
+ mGestureDetector.setIsLongpressEnabled(false);
+ }
+ }
+
+ public void setOnKeyboardActionListener(OnKeyboardActionListener listener) {
+ mKeyboardActionListener = listener;
+ }
+
+ /**
+ * Returns the {@link OnKeyboardActionListener} object.
+ *
+ * @return the listener attached to this keyboard
+ */
+ protected OnKeyboardActionListener getOnKeyboardActionListener() {
+ return mKeyboardActionListener;
+ }
+
+ /**
+ * Attaches a keyboard to this view. The keyboard can be switched at any time and the
+ * view will re-layout itself to accommodate the keyboard.
+ *
+ * @param keyboard the keyboard to display in this view
+ * @see Keyboard
+ * @see #getKeyboard()
+ */
+ public void setKeyboard(Keyboard keyboard) {
+ if (mKeyboard != null) {
+ showPreview(NOT_A_KEY);
+ }
+ // Remove any pending messages
+ removeMessages();
+ mKeyboard = keyboard;
+ List keys = mKeyboard.getKeys();
+ mKeys = keys.toArray(new Key[keys.size()]);
+ requestLayout();
+ // Hint to reallocate the buffer if the size changed
+ mKeyboardChanged = true;
+ invalidateAllKeys();
+ computeProximityThreshold(keyboard);
+ mMiniKeyboardCache.clear(); // Not really necessary to do every time, but will free up views
+ // Switching to a different keyboard should abort any pending keys so that the key up
+ // doesn't get delivered to the old or new keyboard
+ mAbortKey = true; // Until the next ACTION_DOWN
+ }
+
+ /**
+ * Returns the current keyboard being displayed by this view.
+ *
+ * @return the currently attached keyboard
+ * @see #setKeyboard(Keyboard)
+ */
+ public Keyboard getKeyboard() {
+ return mKeyboard;
+ }
+
+ /**
+ * Sets the state of the shift key of the keyboard, if any.
+ *
+ * @param shifted whether or not to enable the state of the shift key
+ * @return true if the shift key state changed, false if there was no change
+ * @see KeyboardView#isShifted()
+ */
+ public boolean setShifted(boolean shifted) {
+ if (mKeyboard != null) {
+ if (mKeyboard.setShifted(shifted)) {
+ // The whole keyboard probably needs to be redrawn
+ invalidateAllKeys();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns the state of the shift key of the keyboard, if any.
+ *
+ * @return true if the shift is in a pressed state, false otherwise. If there is
+ * no shift key on the keyboard or there is no keyboard attached, it returns false.
+ * @see KeyboardView#setShifted(boolean)
+ */
+ public boolean isShifted() {
+ if (mKeyboard != null) {
+ return mKeyboard.isShifted();
+ }
+ return false;
+ }
+
+ /**
+ * Enables or disables the key feedback popup. This is a popup that shows a magnified
+ * version of the depressed key. By default the preview is enabled.
+ *
+ * @param previewEnabled whether or not to enable the key feedback popup
+ * @see #isPreviewEnabled()
+ */
+ public void setPreviewEnabled(boolean previewEnabled) {
+ mShowPreview = previewEnabled;
+ }
+
+ /**
+ * Returns the enabled state of the key feedback popup.
+ *
+ * @return whether or not the key feedback popup is enabled
+ * @see #setPreviewEnabled(boolean)
+ */
+ public boolean isPreviewEnabled() {
+ return mShowPreview;
+ }
+
+ public void setPopupParent(View v) {
+ mPopupParent = v;
+ }
+
+ public void setPopupOffset(int x, int y) {
+ mMiniKeyboardOffsetX = x;
+ mMiniKeyboardOffsetY = y;
+ if (mPreviewPopup.isShowing()) {
+ mPreviewPopup.dismiss();
+ }
+ }
+
+ /**
+ * When enabled, calls to {@link OnKeyboardActionListener#onKey} will include key
+ * codes for adjacent keys. When disabled, only the primary key code will be
+ * reported.
+ *
+ * @param enabled whether or not the proximity correction is enabled
+ */
+ public void setProximityCorrectionEnabled(boolean enabled) {
+ mProximityCorrectOn = enabled;
+ }
+
+ /**
+ * Returns true if proximity correction is enabled.
+ */
+ public boolean isProximityCorrectionEnabled() {
+ return mProximityCorrectOn;
+ }
+
+ /**
+ * Popup keyboard close button clicked.
+ *
+ * @hide
+ */
+ public void onClick(View v) {
+ dismissPopupKeyboard();
+ }
+
+ private CharSequence adjustCase(CharSequence label) {
+ if (mKeyboard.isShifted() && label != null && label.length() < 3
+ && Character.isLowerCase(label.charAt(0))) {
+ label = label.toString().toUpperCase();
+ }
+ return label;
+ }
+
+ @Override
+ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
+ // Round up a little
+ int paddingLeft = getPaddingLeft();
+ int paddingRight = getPaddingRight();
+ int paddingTop = getPaddingTop();
+ int paddingBottom = getPaddingBottom();
+ if (mKeyboard == null) {
+ setMeasuredDimension(paddingLeft + paddingRight, paddingTop + paddingBottom);
+ } else {
+ int width = mKeyboard.getMinWidth() + paddingLeft + paddingRight;
+ if (MeasureSpec.getSize(widthMeasureSpec) < width + 10) {
+ width = MeasureSpec.getSize(widthMeasureSpec);
+ }
+ setMeasuredDimension(width, mKeyboard.getHeight() + paddingTop + paddingBottom);
+ }
+ }
+
+ /**
+ * Compute the average distance between adjacent keys (horizontally and vertically)
+ * and square it to get the proximity threshold. We use a square here and in computing
+ * the touch distance from a key's center to avoid taking a square root.
+ *
+ * @param keyboard
+ */
+ private void computeProximityThreshold(Keyboard keyboard) {
+ if (keyboard == null) return;
+ final Key[] keys = mKeys;
+ if (keys == null) return;
+ int length = keys.length;
+ int dimensionSum = 0;
+ for (int i = 0; i < length; i++) {
+ Key key = keys[i];
+ dimensionSum += Math.min(key.width, key.height) + key.gap;
+ }
+ if (dimensionSum < 0 || length == 0) return;
+ mProximityThreshold = (int) (dimensionSum * 1.4f / length);
+ mProximityThreshold *= mProximityThreshold; // Square it
+ }
+
+ @Override
+ public void onSizeChanged(int w, int h, int oldw, int oldh) {
+ super.onSizeChanged(w, h, oldw, oldh);
+ if (mKeyboard != null) {
+ mKeyboard.resize(w, h);
+ }
+ // Release the buffer, if any and it will be reallocated on the next draw
+ mBuffer = null;
+ }
+
+ @Override
+ public void onDraw(Canvas canvas) {
+ super.onDraw(canvas);
+ if (mDrawPending || mBuffer == null || mKeyboardChanged) {
+ onBufferDraw();
+ }
+ canvas.drawBitmap(mBuffer, 0, 0, null);
+ }
+
+ private void onBufferDraw() {
+ if (mBuffer == null || mKeyboardChanged) {
+ if (mBuffer == null || mKeyboardChanged &&
+ (mBuffer.getWidth() != getWidth() || mBuffer.getHeight() != getHeight())) {
+ // Make sure our bitmap is at least 1x1
+ final int width = Math.max(1, getWidth());
+ final int height = Math.max(1, getHeight());
+ mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
+ mCanvas = new Canvas(mBuffer);
+ }
+ invalidateAllKeys();
+ mKeyboardChanged = false;
+ }
+
+ if (mKeyboard == null) return;
+
+ mCanvas.save();
+ final Canvas canvas = mCanvas;
+ canvas.clipRect(mDirtyRect);
+
+ final Paint paint = mPaint;
+ final Drawable keyBackground = mKeyBackground;
+ final Rect clipRegion = mClipRegion;
+ final Rect padding = mPadding;
+ final int kbdPaddingLeft = getPaddingLeft();
+ final int kbdPaddingTop = getPaddingTop();
+ final Key[] keys = mKeys;
+ final Key invalidKey = mInvalidatedKey;
+
+ paint.setColor(mKeyTextColor);
+ boolean drawSingleKey = false;
+ if (invalidKey != null && canvas.getClipBounds(clipRegion)) {
+ // Is clipRegion completely contained within the invalidated key?
+ if (invalidKey.x + kbdPaddingLeft - 1 <= clipRegion.left &&
+ invalidKey.y + kbdPaddingTop - 1 <= clipRegion.top &&
+ invalidKey.x + invalidKey.width + kbdPaddingLeft + 1 >= clipRegion.right &&
+ invalidKey.y + invalidKey.height + kbdPaddingTop + 1 >= clipRegion.bottom) {
+ drawSingleKey = true;
+ }
+ }
+ canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
+ final int keyCount = keys.length;
+ for (int i = 0; i < keyCount; i++) {
+ final Key key = keys[i];
+ if (drawSingleKey && invalidKey != key) {
+ continue;
+ }
+ int[] drawableState = key.getCurrentDrawableState();
+ keyBackground.setState(drawableState);
+
+ // Switch the character to uppercase if shift is pressed
+ String label = key.label == null ? null : adjustCase(key.label).toString();
+
+ final Rect bounds = keyBackground.getBounds();
+ if (key.width != bounds.right ||
+ key.height != bounds.bottom) {
+ keyBackground.setBounds(0, 0, key.width, key.height);
+ }
+ canvas.translate(key.x + kbdPaddingLeft, key.y + kbdPaddingTop);
+ keyBackground.draw(canvas);
+
+ if (label != null) {
+ // For characters, use large font. For labels like "Done", use small font.
+ if (label.length() > 1 && key.codes.length < 2) {
+ paint.setTextSize(mLabelTextSize);
+ paint.setTypeface(Typeface.DEFAULT_BOLD);
+ } else {
+ paint.setTextSize(mKeyTextSize);
+ paint.setTypeface(Typeface.DEFAULT);
+ }
+ // Draw a drop shadow for the text
+ paint.setShadowLayer(mShadowRadius, 0, 0, mShadowColor);
+ // Draw the text
+ canvas.drawText(label,
+ (key.width - padding.left - padding.right) / 2
+ + padding.left,
+ (key.height - padding.top - padding.bottom) / 2
+ + (paint.getTextSize() - paint.descent()) / 2 + padding.top,
+ paint);
+ // Turn off drop shadow
+ paint.setShadowLayer(0, 0, 0, 0);
+ } else if (key.icon != null) {
+ final int drawableX = (key.width - padding.left - padding.right
+ - key.icon.getIntrinsicWidth()) / 2 + padding.left;
+ final int drawableY = (key.height - padding.top - padding.bottom
+ - key.icon.getIntrinsicHeight()) / 2 + padding.top;
+ canvas.translate(drawableX, drawableY);
+ key.icon.setBounds(0, 0,
+ key.icon.getIntrinsicWidth(), key.icon.getIntrinsicHeight());
+ key.icon.draw(canvas);
+ canvas.translate(-drawableX, -drawableY);
+ }
+ canvas.translate(-key.x - kbdPaddingLeft, -key.y - kbdPaddingTop);
+ }
+ mInvalidatedKey = null;
+ // Overlay a dark rectangle to dim the keyboard
+ if (mMiniKeyboardOnScreen) {
+ paint.setColor((int) (mBackgroundDimAmount * 0xFF) << 24);
+ canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
+ }
+
+ if (DEBUG && mShowTouchPoints) {
+ paint.setAlpha(128);
+ paint.setColor(0xFFFF0000);
+ canvas.drawCircle(mStartX, mStartY, 3, paint);
+ canvas.drawLine(mStartX, mStartY, mLastX, mLastY, paint);
+ paint.setColor(0xFF0000FF);
+ canvas.drawCircle(mLastX, mLastY, 3, paint);
+ paint.setColor(0xFF00FF00);
+ canvas.drawCircle((mStartX + mLastX) / 2, (mStartY + mLastY) / 2, 2, paint);
+ }
+ mCanvas.restore();
+ mDrawPending = false;
+ mDirtyRect.setEmpty();
+ }
+
+ private int getKeyIndices(int x, int y, int[] allKeys) {
+ final Key[] keys = mKeys;
+ int primaryIndex = NOT_A_KEY;
+ int closestKey = NOT_A_KEY;
+ int closestKeyDist = mProximityThreshold + 1;
+ java.util.Arrays.fill(mDistances, Integer.MAX_VALUE);
+ int[] nearestKeyIndices = mKeyboard.getNearestKeys(x, y);
+ final int keyCount = nearestKeyIndices.length;
+ for (int i = 0; i < keyCount; i++) {
+ final Key key = keys[nearestKeyIndices[i]];
+ int dist = 0;
+ boolean isInside = key.isInside(x, y);
+ if (isInside) {
+ primaryIndex = nearestKeyIndices[i];
+ }
+
+ if (((mProximityCorrectOn
+ && (dist = key.squaredDistanceFrom(x, y)) < mProximityThreshold)
+ || isInside)
+ && key.codes[0] > 32) {
+ // Find insertion point
+ final int nCodes = key.codes.length;
+ if (dist < closestKeyDist) {
+ closestKeyDist = dist;
+ closestKey = nearestKeyIndices[i];
+ }
+
+ if (allKeys == null) continue;
+
+ for (int j = 0; j < mDistances.length; j++) {
+ if (mDistances[j] > dist) {
+ // Make space for nCodes codes
+ System.arraycopy(mDistances, j, mDistances, j + nCodes,
+ mDistances.length - j - nCodes);
+ System.arraycopy(allKeys, j, allKeys, j + nCodes,
+ allKeys.length - j - nCodes);
+ for (int c = 0; c < nCodes; c++) {
+ allKeys[j + c] = key.codes[c];
+ mDistances[j + c] = dist;
+ }
+ break;
+ }
+ }
+ }
+ }
+ if (primaryIndex == NOT_A_KEY) {
+ primaryIndex = closestKey;
+ }
+ return primaryIndex;
+ }
+
+ private void detectAndSendKey(int index, int x, int y, long eventTime) {
+ if (index != NOT_A_KEY && index < mKeys.length) {
+ final Key key = mKeys[index];
+ if (key.text != null) {
+ mKeyboardActionListener.onText(key.text);
+ mKeyboardActionListener.onRelease(NOT_A_KEY);
+ } else {
+ int code = key.codes[0];
+ //TextEntryState.keyPressedAt(key, x, y);
+ int[] codes = new int[MAX_NEARBY_KEYS];
+ Arrays.fill(codes, NOT_A_KEY);
+ getKeyIndices(x, y, codes);
+ // Multi-tap
+ if (mInMultiTap) {
+ if (mTapCount != -1) {
+ mKeyboardActionListener.onKey(Keyboard.KEYCODE_DELETE, KEY_DELETE);
+ } else {
+ mTapCount = 0;
+ }
+ code = key.codes[mTapCount];
+ }
+ mKeyboardActionListener.onKey(code, codes);
+ mKeyboardActionListener.onRelease(code);
+ }
+ mLastSentIndex = index;
+ mLastTapTime = eventTime;
+ }
+ }
+
+ /**
+ * Handle multi-tap keys by producing the key label for the current multi-tap state.
+ */
+ private CharSequence getPreviewText(Key key) {
+ if (mInMultiTap) {
+ // Multi-tap
+ mPreviewLabel.setLength(0);
+ mPreviewLabel.append((char) key.codes[mTapCount < 0 ? 0 : mTapCount]);
+ return adjustCase(mPreviewLabel);
+ } else {
+ return adjustCase(key.label);
+ }
+ }
+
+ private void showPreview(int keyIndex) {
+ int oldKeyIndex = mCurrentKeyIndex;
+ final PopupWindow previewPopup = mPreviewPopup;
+
+ mCurrentKeyIndex = keyIndex;
+ // Release the old key and press the new key
+ final Key[] keys = mKeys;
+ if (oldKeyIndex != mCurrentKeyIndex) {
+ if (oldKeyIndex != NOT_A_KEY && keys.length > oldKeyIndex) {
+ Key oldKey = keys[oldKeyIndex];
+ oldKey.onReleased(mCurrentKeyIndex == NOT_A_KEY);
+ invalidateKey(oldKeyIndex);
+ }
+ if (mCurrentKeyIndex != NOT_A_KEY && keys.length > mCurrentKeyIndex) {
+ Key newKey = keys[mCurrentKeyIndex];
+ newKey.onPressed();
+ invalidateKey(mCurrentKeyIndex);
+ }
+ }
+ // If key changed and preview is on ...
+ if (oldKeyIndex != mCurrentKeyIndex && mShowPreview) {
+ mHandler.removeMessages(MSG_SHOW_PREVIEW);
+ if (previewPopup.isShowing()) {
+ if (keyIndex == NOT_A_KEY) {
+ mHandler.sendMessageDelayed(mHandler
+ .obtainMessage(MSG_REMOVE_PREVIEW),
+ DELAY_AFTER_PREVIEW);
+ }
+ }
+ if (keyIndex != NOT_A_KEY) {
+ if (previewPopup.isShowing() && mPreviewText.getVisibility() == VISIBLE) {
+ // Show right away, if it's already visible and finger is moving around
+ showKey(keyIndex);
+ } else {
+ mHandler.sendMessageDelayed(
+ mHandler.obtainMessage(MSG_SHOW_PREVIEW, keyIndex, 0),
+ DELAY_BEFORE_PREVIEW);
+ }
+ }
+ }
+ }
+
+ private void showKey(final int keyIndex) {
+ final PopupWindow previewPopup = mPreviewPopup;
+ final Key[] keys = mKeys;
+ if (keyIndex < 0 || keyIndex >= mKeys.length) return;
+ Key key = keys[keyIndex];
+ if (key.icon != null) {
+ mPreviewText.setCompoundDrawables(null, null, null,
+ key.iconPreview != null ? key.iconPreview : key.icon);
+ mPreviewText.setText(null);
+ } else {
+ mPreviewText.setCompoundDrawables(null, null, null, null);
+ mPreviewText.setText(getPreviewText(key));
+ if (key.label.length() > 1 && key.codes.length < 2) {
+ mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mKeyTextSize);
+ mPreviewText.setTypeface(Typeface.DEFAULT_BOLD);
+ } else {
+ mPreviewText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mPreviewTextSizeLarge);
+ mPreviewText.setTypeface(Typeface.DEFAULT);
+ }
+ }
+ mPreviewText.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
+ MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
+ int popupWidth = Math.max(mPreviewText.getMeasuredWidth(), key.width
+ + mPreviewText.getPaddingLeft() + mPreviewText.getPaddingRight());
+ final int popupHeight = mPreviewHeight;
+ LayoutParams lp = mPreviewText.getLayoutParams();
+ if (lp != null) {
+ lp.width = popupWidth;
+ lp.height = popupHeight;
+ }
+ if (!mPreviewCentered) {
+ mPopupPreviewX = key.x - mPreviewText.getPaddingLeft() + getPaddingLeft();
+ mPopupPreviewY = key.y - popupHeight + mPreviewOffset;
+ } else {
+ mPopupPreviewX = 160 - mPreviewText.getMeasuredWidth() / 2;
+ mPopupPreviewY = -mPreviewText.getMeasuredHeight();
+ }
+ mHandler.removeMessages(MSG_REMOVE_PREVIEW);
+ getLocationInWindow(mCoordinates);
+ mCoordinates[0] += mMiniKeyboardOffsetX; // Offset may be zero
+ mCoordinates[1] += mMiniKeyboardOffsetY; // Offset may be zero
+
+ // Set the preview background state
+ mPreviewText.getBackground().setState(
+ key.popupResId != 0 ? LONG_PRESSABLE_STATE_SET : EMPTY_STATE_SET);
+ mPopupPreviewX += mCoordinates[0];
+ mPopupPreviewY += mCoordinates[1];
+
+ // If the popup cannot be shown above the key, put it on the side
+ getLocationOnScreen(mCoordinates);
+ if (mPopupPreviewY + mCoordinates[1] < 0) {
+ // If the key you're pressing is on the left side of the keyboard, show the popup on
+ // the right, offset by enough to see at least one key to the left/right.
+ if (key.x + key.width <= getWidth() / 2) {
+ mPopupPreviewX += (int) (key.width * 2.5);
+ } else {
+ mPopupPreviewX -= (int) (key.width * 2.5);
+ }
+ mPopupPreviewY += popupHeight;
+ }
+
+ if (previewPopup.isShowing()) {
+ previewPopup.update(mPopupPreviewX, mPopupPreviewY,
+ popupWidth, popupHeight);
+ } else {
+ previewPopup.setWidth(popupWidth);
+ previewPopup.setHeight(popupHeight);
+ previewPopup.showAtLocation(mPopupParent, Gravity.NO_GRAVITY,
+ mPopupPreviewX, mPopupPreviewY);
+ }
+ mPreviewText.setVisibility(VISIBLE);
+ }
+
+ /**
+ * Requests a redraw of the entire keyboard. Calling {@link #invalidate} is not sufficient
+ * because the keyboard renders the keys to an off-screen buffer and an invalidate() only
+ * draws the cached buffer.
+ *
+ * @see #invalidateKey(int)
+ */
+ public void invalidateAllKeys() {
+ mDirtyRect.union(0, 0, getWidth(), getHeight());
+ mDrawPending = true;
+ invalidate();
+ }
+
+ /**
+ * Invalidates a key so that it will be redrawn on the next repaint. Use this method if only
+ * one key is changing it's content. Any changes that affect the position or size of the key
+ * may not be honored.
+ *
+ * @param keyIndex the index of the key in the attached {@link Keyboard}.
+ * @see #invalidateAllKeys
+ */
+ public void invalidateKey(int keyIndex) {
+ if (mKeys == null) return;
+ if (keyIndex < 0 || keyIndex >= mKeys.length) {
+ return;
+ }
+ int paddingLeft = getPaddingLeft();
+ int paddingTop = getPaddingTop();
+ final Key key = mKeys[keyIndex];
+ mInvalidatedKey = key;
+ mDirtyRect.union(key.x + paddingLeft, key.y + paddingTop,
+ key.x + key.width + paddingLeft, key.y + key.height + paddingTop);
+ onBufferDraw();
+ invalidate();
+ }
+
+ private boolean openPopupIfRequired(MotionEvent me) {
+ // Check if we have a popup layout specified first.
+ if (mPopupLayout == 0) {
+ return false;
+ }
+ if (mCurrentKey < 0 || mCurrentKey >= mKeys.length) {
+ return false;
+ }
+
+ Key popupKey = mKeys[mCurrentKey];
+ boolean result = onLongPress(popupKey);
+ if (result) {
+ mAbortKey = true;
+ showPreview(NOT_A_KEY);
+ }
+ return result;
+ }
+
+ /**
+ * Called when a key is long pressed. By default this will open any popup keyboard associated
+ * with this key through the attributes popupLayout and popupCharacters.
+ *
+ * @param popupKey the key that was long pressed
+ * @return true if the long press is handled, false otherwise. Subclasses should call the
+ * method on the base class if the subclass doesn't wish to handle the call.
+ */
+ protected boolean onLongPress(Key popupKey) {
+ if (popupKey.codes[0] == KEY_BACK_KEYBOARD) {
+ mKeyboardActionListener.onKey(KEY_CHANGE_KEYBOARD, popupKey.codes);
+ return true;
+ } else {
+ int popupKeyboardId = popupKey.popupResId;
+ if (popupKeyboardId != 0) {
+ mMiniKeyboardContainer = mMiniKeyboardCache.get(popupKey);
+ if (mMiniKeyboardContainer == null) {
+ LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(
+ Context.LAYOUT_INFLATER_SERVICE);
+ mMiniKeyboardContainer = inflater.inflate(mPopupLayout, null);
+ mMiniKeyboard = mMiniKeyboardContainer.findViewById(R.id.magikeyboard_view);
+ View closeButton = mMiniKeyboardContainer.findViewById(R.id.keyboard_popup_close);
+ if (closeButton != null) closeButton.setOnClickListener(this);
+ mMiniKeyboard.setOnKeyboardActionListener(new OnKeyboardActionListener() {
+ public void onKey(int primaryCode, int[] keyCodes) {
+ mKeyboardActionListener.onKey(primaryCode, keyCodes);
+ dismissPopupKeyboard();
+ }
+
+ public void onText(CharSequence text) {
+ mKeyboardActionListener.onText(text);
+ dismissPopupKeyboard();
+ }
+
+ public void swipeLeft() {
+ }
+
+ public void swipeRight() {
+ }
+
+ public void swipeUp() {
+ }
+
+ public void swipeDown() {
+ }
+
+ public void onPress(int primaryCode) {
+ mKeyboardActionListener.onPress(primaryCode);
+ }
+
+ public void onRelease(int primaryCode) {
+ mKeyboardActionListener.onRelease(primaryCode);
+ }
+ });
+ Keyboard keyboard;
+ if (popupKey.popupCharacters != null) {
+ keyboard = new Keyboard(getContext(), popupKeyboardId,
+ popupKey.popupCharacters, -1, getPaddingLeft() + getPaddingRight());
+ } else {
+ keyboard = new Keyboard(getContext(), popupKeyboardId);
+ }
+ mMiniKeyboard.setKeyboard(keyboard);
+ mMiniKeyboard.setPopupParent(this);
+ mMiniKeyboardContainer.measure(
+ MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.AT_MOST),
+ MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.AT_MOST));
+
+ mMiniKeyboardCache.put(popupKey, mMiniKeyboardContainer);
+ } else {
+ mMiniKeyboard = mMiniKeyboardContainer.findViewById(R.id.magikeyboard_view);
+ }
+ getLocationInWindow(mCoordinates);
+ mPopupX = popupKey.x + getPaddingLeft();
+ mPopupY = popupKey.y + getPaddingTop();
+ mPopupX = mPopupX + popupKey.width - mMiniKeyboardContainer.getMeasuredWidth();
+ mPopupY = mPopupY - mMiniKeyboardContainer.getMeasuredHeight();
+ final int x = mPopupX + mMiniKeyboardContainer.getPaddingRight() + mCoordinates[0];
+ final int y = mPopupY + mMiniKeyboardContainer.getPaddingBottom() + mCoordinates[1];
+ mMiniKeyboard.setPopupOffset(x < 0 ? 0 : x, y);
+ mMiniKeyboard.setShifted(isShifted());
+ mPopupKeyboard.setContentView(mMiniKeyboardContainer);
+ mPopupKeyboard.setWidth(mMiniKeyboardContainer.getMeasuredWidth());
+ mPopupKeyboard.setHeight(mMiniKeyboardContainer.getMeasuredHeight());
+ mPopupKeyboard.showAtLocation(this, Gravity.NO_GRAVITY, x, y);
+ mMiniKeyboardOnScreen = true;
+ invalidateAllKeys();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean onTouchEvent(MotionEvent me) {
+ // Convert multi-pointer up/down events to single up/down events to
+ // deal with the typical multi-pointer behavior of two-thumb typing
+ final int pointerCount = me.getPointerCount();
+ final int action = me.getAction();
+ boolean result = false;
+ final long now = me.getEventTime();
+
+ if (pointerCount != mOldPointerCount) {
+ if (pointerCount == 1) {
+ // Send a down event for the latest pointer
+ MotionEvent down = MotionEvent.obtain(now, now, MotionEvent.ACTION_DOWN,
+ me.getX(), me.getY(), me.getMetaState());
+ result = onModifiedTouchEvent(down, false);
+ down.recycle();
+ // If it's an up action, then deliver the up as well.
+ if (action == MotionEvent.ACTION_UP) {
+ result = onModifiedTouchEvent(me, true);
+ }
+ } else {
+ // Send an up event for the last pointer
+ MotionEvent up = MotionEvent.obtain(now, now, MotionEvent.ACTION_UP,
+ mOldPointerX, mOldPointerY, me.getMetaState());
+ result = onModifiedTouchEvent(up, true);
+ up.recycle();
+ }
+ } else {
+ if (pointerCount == 1) {
+ result = onModifiedTouchEvent(me, false);
+ mOldPointerX = me.getX();
+ mOldPointerY = me.getY();
+ } else {
+ // Don't do anything when 2 pointers are down and moving.
+ result = true;
+ }
+ }
+ mOldPointerCount = pointerCount;
+
+ return result;
+ }
+
+ private boolean onModifiedTouchEvent(MotionEvent me, boolean possiblePoly) {
+ int touchX = (int) me.getX() - getPaddingLeft();
+ int touchY = (int) me.getY() - getPaddingTop();
+ if (touchY >= -mVerticalCorrection)
+ touchY += mVerticalCorrection;
+ final int action = me.getAction();
+ final long eventTime = me.getEventTime();
+ int keyIndex = getKeyIndices(touchX, touchY, null);
+ mPossiblePoly = possiblePoly;
+
+ // Track the last few movements to look for spurious swipes.
+ if (action == MotionEvent.ACTION_DOWN) mSwipeTracker.clear();
+ mSwipeTracker.addMovement(me);
+
+ // Ignore all motion events until a DOWN.
+ if (mAbortKey
+ && action != MotionEvent.ACTION_DOWN && action != MotionEvent.ACTION_CANCEL) {
+ return true;
+ }
+
+ if (mGestureDetector.onTouchEvent(me)) {
+ showPreview(NOT_A_KEY);
+ mHandler.removeMessages(MSG_REPEAT);
+ mHandler.removeMessages(MSG_LONGPRESS);
+ return true;
+ }
+
+ // Needs to be called after the gesture detector gets a turn, as it may have
+ // displayed the mini keyboard
+ if (mMiniKeyboardOnScreen && action != MotionEvent.ACTION_CANCEL) {
+ return true;
+ }
+
+ switch (action) {
+ case MotionEvent.ACTION_DOWN:
+ mAbortKey = false;
+ mStartX = touchX;
+ mStartY = touchY;
+ mLastCodeX = touchX;
+ mLastCodeY = touchY;
+ mLastKeyTime = 0;
+ mCurrentKeyTime = 0;
+ mLastKey = NOT_A_KEY;
+ mCurrentKey = keyIndex;
+ mDownKey = keyIndex;
+ mDownTime = me.getEventTime();
+ mLastMoveTime = mDownTime;
+ checkMultiTap(eventTime, keyIndex);
+ mKeyboardActionListener.onPress(keyIndex != NOT_A_KEY ?
+ mKeys[keyIndex].codes[0] : 0);
+ if (mCurrentKey >= 0 && mKeys[mCurrentKey].repeatable) {
+ mRepeatKeyIndex = mCurrentKey;
+ Message msg = mHandler.obtainMessage(MSG_REPEAT);
+ mHandler.sendMessageDelayed(msg, REPEAT_START_DELAY);
+ repeatKey();
+ // Delivering the key could have caused an abort
+ if (mAbortKey) {
+ mRepeatKeyIndex = NOT_A_KEY;
+ break;
+ }
+ }
+ if (mCurrentKey != NOT_A_KEY) {
+ Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me);
+ mHandler.sendMessageDelayed(msg, LONGPRESS_TIMEOUT);
+ }
+ showPreview(keyIndex);
+ break;
+
+ case MotionEvent.ACTION_MOVE:
+ boolean continueLongPress = false;
+ if (keyIndex != NOT_A_KEY) {
+ if (mCurrentKey == NOT_A_KEY) {
+ mCurrentKey = keyIndex;
+ mCurrentKeyTime = eventTime - mDownTime;
+ } else {
+ if (keyIndex == mCurrentKey) {
+ mCurrentKeyTime += eventTime - mLastMoveTime;
+ continueLongPress = true;
+ } else if (mRepeatKeyIndex == NOT_A_KEY) {
+ resetMultiTap();
+ mLastKey = mCurrentKey;
+ mLastCodeX = mLastX;
+ mLastCodeY = mLastY;
+ mLastKeyTime =
+ mCurrentKeyTime + eventTime - mLastMoveTime;
+ mCurrentKey = keyIndex;
+ mCurrentKeyTime = 0;
+ }
+ }
+ }
+ if (!continueLongPress) {
+ // Cancel old longpress
+ mHandler.removeMessages(MSG_LONGPRESS);
+ // Start new longpress if key has changed
+ if (keyIndex != NOT_A_KEY) {
+ Message msg = mHandler.obtainMessage(MSG_LONGPRESS, me);
+ mHandler.sendMessageDelayed(msg, LONGPRESS_TIMEOUT);
+ }
+ }
+ showPreview(mCurrentKey);
+ mLastMoveTime = eventTime;
+ break;
+
+ case MotionEvent.ACTION_UP:
+ removeMessages();
+ if (keyIndex == mCurrentKey) {
+ mCurrentKeyTime += eventTime - mLastMoveTime;
+ } else {
+ resetMultiTap();
+ mLastKey = mCurrentKey;
+ mLastKeyTime = mCurrentKeyTime + eventTime - mLastMoveTime;
+ mCurrentKey = keyIndex;
+ mCurrentKeyTime = 0;
+ }
+ if (mCurrentKeyTime < mLastKeyTime && mCurrentKeyTime < DEBOUNCE_TIME
+ && mLastKey != NOT_A_KEY) {
+ mCurrentKey = mLastKey;
+ touchX = mLastCodeX;
+ touchY = mLastCodeY;
+ }
+ showPreview(NOT_A_KEY);
+ Arrays.fill(mKeyIndices, NOT_A_KEY);
+ // If we're not on a repeating key (which sends on a DOWN event)
+ if (mRepeatKeyIndex == NOT_A_KEY && !mMiniKeyboardOnScreen && !mAbortKey) {
+ detectAndSendKey(mCurrentKey, touchX, touchY, eventTime);
+ }
+ invalidateKey(keyIndex);
+ mRepeatKeyIndex = NOT_A_KEY;
+ break;
+ case MotionEvent.ACTION_CANCEL:
+ removeMessages();
+ dismissPopupKeyboard();
+ mAbortKey = true;
+ showPreview(NOT_A_KEY);
+ invalidateKey(mCurrentKey);
+ break;
+ }
+ mLastX = touchX;
+ mLastY = touchY;
+ return true;
+ }
+
+ private boolean repeatKey() {
+ Key key = mKeys[mRepeatKeyIndex];
+ detectAndSendKey(mCurrentKey, key.x, key.y, mLastTapTime);
+ return true;
+ }
+
+ protected void swipeRight() {
+ mKeyboardActionListener.swipeRight();
+ }
+
+ protected void swipeLeft() {
+ mKeyboardActionListener.swipeLeft();
+ }
+
+ protected void swipeUp() {
+ mKeyboardActionListener.swipeUp();
+ }
+
+ protected void swipeDown() {
+ mKeyboardActionListener.swipeDown();
+ }
+
+ public void closing() {
+ if (mPreviewPopup.isShowing()) {
+ mPreviewPopup.dismiss();
+ }
+ removeMessages();
+
+ dismissPopupKeyboard();
+ mBuffer = null;
+ mCanvas = null;
+ mMiniKeyboardCache.clear();
+ }
+
+ private void removeMessages() {
+ if (mHandler != null) {
+ mHandler.removeMessages(MSG_REPEAT);
+ mHandler.removeMessages(MSG_LONGPRESS);
+ mHandler.removeMessages(MSG_SHOW_PREVIEW);
+ }
+ }
+
+ @Override
+ public void onDetachedFromWindow() {
+ super.onDetachedFromWindow();
+ closing();
+ }
+
+ private void dismissPopupKeyboard() {
+ if (mPopupKeyboard.isShowing()) {
+ mPopupKeyboard.dismiss();
+ mMiniKeyboardOnScreen = false;
+ invalidateAllKeys();
+ }
+ }
+
+ public boolean handleBack() {
+ if (mPopupKeyboard.isShowing()) {
+ dismissPopupKeyboard();
+ return true;
+ }
+ return false;
+ }
+
+ private void resetMultiTap() {
+ mLastSentIndex = NOT_A_KEY;
+ mTapCount = 0;
+ mLastTapTime = -1;
+ mInMultiTap = false;
+ }
+
+ private void checkMultiTap(long eventTime, int keyIndex) {
+ if (keyIndex == NOT_A_KEY) return;
+ Key key = mKeys[keyIndex];
+ if (key.codes.length > 1) {
+ mInMultiTap = true;
+ if (eventTime < mLastTapTime + MULTITAP_INTERVAL
+ && keyIndex == mLastSentIndex) {
+ mTapCount = (mTapCount + 1) % key.codes.length;
+ return;
+ } else {
+ mTapCount = -1;
+ return;
+ }
+ }
+ if (eventTime > mLastTapTime + MULTITAP_INTERVAL || keyIndex != mLastSentIndex) {
+ resetMultiTap();
+ }
+ }
+
+ private static class SwipeTracker {
+
+ static final int NUM_PAST = 4;
+ static final int LONGEST_PAST_TIME = 200;
+
+ final float mPastX[] = new float[NUM_PAST];
+ final float mPastY[] = new float[NUM_PAST];
+ final long mPastTime[] = new long[NUM_PAST];
+
+ float mYVelocity;
+ float mXVelocity;
+
+ public void clear() {
+ mPastTime[0] = 0;
+ }
+
+ public void addMovement(MotionEvent ev) {
+ long time = ev.getEventTime();
+ final int N = ev.getHistorySize();
+ for (int i = 0; i < N; i++) {
+ addPoint(ev.getHistoricalX(i), ev.getHistoricalY(i),
+ ev.getHistoricalEventTime(i));
+ }
+ addPoint(ev.getX(), ev.getY(), time);
+ }
+
+ private void addPoint(float x, float y, long time) {
+ int drop = -1;
+ int i;
+ final long[] pastTime = mPastTime;
+ for (i = 0; i < NUM_PAST; i++) {
+ if (pastTime[i] == 0) {
+ break;
+ } else if (pastTime[i] < time - LONGEST_PAST_TIME) {
+ drop = i;
+ }
+ }
+ if (i == NUM_PAST && drop < 0) {
+ drop = 0;
+ }
+ if (drop == i) drop--;
+ final float[] pastX = mPastX;
+ final float[] pastY = mPastY;
+ if (drop >= 0) {
+ final int start = drop + 1;
+ final int count = NUM_PAST - drop - 1;
+ System.arraycopy(pastX, start, pastX, 0, count);
+ System.arraycopy(pastY, start, pastY, 0, count);
+ System.arraycopy(pastTime, start, pastTime, 0, count);
+ i -= (drop + 1);
+ }
+ pastX[i] = x;
+ pastY[i] = y;
+ pastTime[i] = time;
+ i++;
+ if (i < NUM_PAST) {
+ pastTime[i] = 0;
+ }
+ }
+
+ public void computeCurrentVelocity(int units) {
+ computeCurrentVelocity(units, Float.MAX_VALUE);
+ }
+
+ public void computeCurrentVelocity(int units, float maxVelocity) {
+ final float[] pastX = mPastX;
+ final float[] pastY = mPastY;
+ final long[] pastTime = mPastTime;
+
+ final float oldestX = pastX[0];
+ final float oldestY = pastY[0];
+ final long oldestTime = pastTime[0];
+ float accumX = 0;
+ float accumY = 0;
+ int N = 0;
+ while (N < NUM_PAST) {
+ if (pastTime[N] == 0) {
+ break;
+ }
+ N++;
+ }
+
+ for (int i = 1; i < N; i++) {
+ final int dur = (int) (pastTime[i] - oldestTime);
+ if (dur == 0) continue;
+ float dist = pastX[i] - oldestX;
+ float vel = (dist / dur) * units; // pixels/frame.
+ if (accumX == 0) accumX = vel;
+ else accumX = (accumX + vel) * .5f;
+
+ dist = pastY[i] - oldestY;
+ vel = (dist / dur) * units; // pixels/frame.
+ if (accumY == 0) accumY = vel;
+ else accumY = (accumY + vel) * .5f;
+ }
+ mXVelocity = accumX < 0.0f ? Math.max(accumX, -maxVelocity)
+ : Math.min(accumX, maxVelocity);
+ mYVelocity = accumY < 0.0f ? Math.max(accumY, -maxVelocity)
+ : Math.min(accumY, maxVelocity);
+ }
+
+ public float getXVelocity() {
+ return mXVelocity;
+ }
+
+ public float getYVelocity() {
+ return mYVelocity;
+ }
+ }
+}
diff --git a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikeyboardService.kt b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikeyboardService.kt
index b8b5854a1..2472ab69f 100644
--- a/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikeyboardService.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/magikeyboard/MagikeyboardService.kt
@@ -17,15 +17,12 @@
* along with KeePassDX. If not, see .
*
*/
-@file:Suppress("DEPRECATION")
package com.kunzisoft.keepass.magikeyboard
import android.content.Context
import android.content.Intent
import android.inputmethodservice.InputMethodService
-import android.inputmethodservice.Keyboard
-import android.inputmethodservice.KeyboardView
import android.media.AudioManager
import android.os.Build
import android.util.Log
@@ -99,7 +96,6 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL
popupCustomKeys = PopupWindow(context).apply {
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
- softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
inputMethodMode = PopupWindow.INPUT_METHOD_NEEDED
contentView = popupFieldsView
}
@@ -130,8 +126,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL
removeEntryInfo()
}
assignKeyboardView()
- keyboardView?.setOnKeyboardActionListener(this)
- keyboardView?.isPreviewEnabled = false
+ keyboardView?.onKeyboardActionListener = this
return rootKeyboardView
}
@@ -206,6 +201,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
switchToPreviousInputMethod()
} else {
+ @Suppress("DEPRECATION")
window.window?.let { window ->
imeManager?.switchToLastInputMethod(window.attributes.token)
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt
index 4a60e870b..12d1b4b0a 100644
--- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt
@@ -38,6 +38,8 @@ class EntryInfo : NodeInfo {
var url: String = ""
var notes: String = ""
var tags: Tags = Tags()
+ var backgroundColor: Int? = null
+ var foregroundColor: Int? = null
var customFields: MutableList = mutableListOf()
var attachments: MutableList = mutableListOf()
var otpModel: OtpModel? = null
@@ -52,6 +54,10 @@ class EntryInfo : NodeInfo {
url = parcel.readString() ?: url
notes = parcel.readString() ?: notes
tags = parcel.readParcelable(Tags::class.java.classLoader) ?: tags
+ val readBgColor = parcel.readInt()
+ backgroundColor = if (readBgColor == -1) null else readBgColor
+ val readFgColor = parcel.readInt()
+ foregroundColor = if (readFgColor == -1) null else readFgColor
parcel.readList(customFields, Field::class.java.classLoader)
parcel.readList(attachments, Attachment::class.java.classLoader)
otpModel = parcel.readParcelable(OtpModel::class.java.classLoader) ?: otpModel
@@ -70,6 +76,8 @@ class EntryInfo : NodeInfo {
parcel.writeString(url)
parcel.writeString(notes)
parcel.writeParcelable(tags, flags)
+ parcel.writeInt(backgroundColor ?: -1)
+ parcel.writeInt(foregroundColor ?: -1)
parcel.writeList(customFields)
parcel.writeList(attachments)
parcel.writeParcelable(otpModel, flags)
@@ -197,6 +205,8 @@ class EntryInfo : NodeInfo {
if (url != other.url) return false
if (notes != other.notes) return false
if (tags != other.tags) return false
+ if (backgroundColor != other.backgroundColor) return false
+ if (foregroundColor != other.foregroundColor) return false
if (customFields != other.customFields) return false
if (attachments != other.attachments) return false
if (otpModel != other.otpModel) return false
@@ -213,6 +223,8 @@ class EntryInfo : NodeInfo {
result = 31 * result + url.hashCode()
result = 31 * result + notes.hashCode()
result = 31 * result + tags.hashCode()
+ result = 31 * result + backgroundColor.hashCode()
+ result = 31 * result + foregroundColor.hashCode()
result = 31 * result + customFields.hashCode()
result = 31 * result + attachments.hashCode()
result = 31 * result + (otpModel?.hashCode() ?: 0)
diff --git a/app/src/main/java/com/kunzisoft/keepass/model/GroupInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/GroupInfo.kt
index f66072fc2..96f491462 100644
--- a/app/src/main/java/com/kunzisoft/keepass/model/GroupInfo.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/model/GroupInfo.kt
@@ -1,12 +1,15 @@
package com.kunzisoft.keepass.model
import android.os.Parcel
+import android.os.ParcelUuid
import android.os.Parcelable
import com.kunzisoft.keepass.database.element.icon.IconImageStandard
import com.kunzisoft.keepass.database.element.icon.IconImageStandard.Companion.FOLDER_ID
+import java.util.*
class GroupInfo : NodeInfo {
+ var id: UUID? = null
var notes: String? = null
init {
@@ -16,11 +19,14 @@ class GroupInfo : NodeInfo {
constructor(): super()
constructor(parcel: Parcel): super(parcel) {
+ id = parcel.readParcelable(ParcelUuid::class.java.classLoader)?.uuid ?: id
notes = parcel.readString()
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
super.writeToParcel(parcel, flags)
+ val uuid = if (id != null) ParcelUuid(id) else null
+ parcel.writeParcelable(uuid, flags)
parcel.writeString(notes)
}
@@ -29,6 +35,7 @@ class GroupInfo : NodeInfo {
if (other !is GroupInfo) return false
if (!super.equals(other)) return false
+ if (id != other.id) return false
if (notes != other.notes) return false
return true
@@ -36,6 +43,7 @@ class GroupInfo : NodeInfo {
override fun hashCode(): Int {
var result = super.hashCode()
+ result = 31 * result + (id?.hashCode() ?: 0)
result = 31 * result + (notes?.hashCode() ?: 0)
return result
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/model/NodeInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/NodeInfo.kt
index aad2fd752..95de2b790 100644
--- a/app/src/main/java/com/kunzisoft/keepass/model/NodeInfo.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/model/NodeInfo.kt
@@ -4,6 +4,8 @@ import android.os.Parcel
import android.os.Parcelable
import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.icon.IconImage
+import com.kunzisoft.keepass.utils.UuidUtil
+import java.util.*
open class NodeInfo() : Parcelable {
diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt
index d07ace49f..eef173567 100644
--- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt
@@ -234,7 +234,7 @@ data class OtpElement(var otpModel: OtpModel = OtpModel()) {
fun replaceBase32Chars(parameter: String): String {
// Add padding '=' at end if not Base32 length
- var parameterNewSize = parameter.toUpperCase(Locale.ENGLISH).removeSpaceChars()
+ var parameterNewSize = parameter.uppercase(Locale.ENGLISH).removeSpaceChars()
while (parameterNewSize.length % 8 != 0) {
parameterNewSize += '='
}
@@ -264,7 +264,7 @@ enum class OtpTokenType {
companion object {
fun getFromString(tokenType: String): OtpTokenType {
- return when (tokenType.toLowerCase(Locale.ENGLISH)) {
+ return when (tokenType.lowercase(Locale.ENGLISH)) {
"s", "steam" -> STEAM
"hotp" -> RFC4226
else -> RFC6238
diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt
index a4cd13279..ba8090cbd 100644
--- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt
@@ -143,7 +143,7 @@ object OtpEntryFields {
if (otpPlainText != null && otpPlainText.isNotEmpty() && isOTPUri(otpPlainText)) {
val uri = Uri.parse(otpPlainText.removeSpaceChars())
- if (uri.scheme == null || OTP_SCHEME != uri.scheme!!.toLowerCase(Locale.ENGLISH)) {
+ if (uri.scheme == null || OTP_SCHEME != uri.scheme!!.lowercase(Locale.ENGLISH)) {
Log.e(TAG, "Invalid or missing scheme in uri")
return false
}
@@ -309,7 +309,7 @@ object OtpEntryFields {
}
if (algorithmField != null) {
otpElement.algorithm =
- when (algorithmField.toUpperCase(Locale.ENGLISH)) {
+ when (algorithmField.uppercase(Locale.ENGLISH)) {
TIMEOTP_ALGORITHM_SHA1_VALUE -> HashAlgorithm.SHA1
TIMEOTP_ALGORITHM_SHA256_VALUE -> HashAlgorithm.SHA256
TIMEOTP_ALGORITHM_SHA512_VALUE -> HashAlgorithm.SHA512
@@ -417,7 +417,7 @@ object OtpEntryFields {
val output = HashMap()
for (element in elements) {
val pair = element.split("=".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
- output[pair[0].toLowerCase(Locale.ENGLISH)] = pair[1]
+ output[pair[0].lowercase(Locale.ENGLISH)] = pair[1]
}
return output
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/services/AdvancedUnlockNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/AdvancedUnlockNotificationService.kt
index 9f63b9ce2..bf6ac0f0f 100644
--- a/app/src/main/java/com/kunzisoft/keepass/services/AdvancedUnlockNotificationService.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/services/AdvancedUnlockNotificationService.kt
@@ -1,9 +1,11 @@
package com.kunzisoft.keepass.services
+import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.*
import android.net.Uri
import android.os.Binder
+import android.os.Build
import android.os.IBinder
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.app.database.CipherDatabaseEntity
@@ -50,11 +52,20 @@ class AdvancedUnlockNotificationService : NotificationService() {
mTempCipherDao = ArrayList()
}
+ // It's simpler to use pendingIntent to perform REMOVE_ADVANCED_UNLOCK_KEY_ACTION
+ // because can be directly broadcast to another module or app
+ @SuppressLint("LaunchActivityFromNotification")
override fun onBind(intent: Intent): IBinder {
super.onBind(intent)
val pendingDeleteIntent = PendingIntent.getBroadcast(this,
- 4577, Intent(REMOVE_ADVANCED_UNLOCK_KEY_ACTION), 0)
+ 4577,
+ Intent(REMOVE_ADVANCED_UNLOCK_KEY_ACTION),
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE
+ } else {
+ 0
+ })
val biometricUnlockEnabled = PreferencesUtil.isBiometricUnlockEnable(this)
val notificationBuilder = buildNewNotification().apply {
setSmallIcon(if (biometricUnlockEnabled) {
diff --git a/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt
index 2e48f8be6..853079985 100644
--- a/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/services/AttachmentFileNotificationService.kt
@@ -24,6 +24,7 @@ import android.content.ContentResolver
import android.content.Intent
import android.net.Uri
import android.os.Binder
+import android.os.Build
import android.os.IBinder
import android.util.Log
import com.kunzisoft.keepass.R
@@ -188,20 +189,30 @@ class AttachmentFileNotificationService: LockNotificationService() {
private fun newNotification(attachmentNotification: AttachmentNotification) {
val pendingContentIntent = PendingIntent.getActivity(this,
- 0,
- Intent().apply {
- action = Intent.ACTION_VIEW
- setDataAndType(attachmentNotification.uri,
- contentResolver.getType(attachmentNotification.uri))
- addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
- }, PendingIntent.FLAG_CANCEL_CURRENT)
+ 0,
+ Intent().apply {
+ action = Intent.ACTION_VIEW
+ setDataAndType(attachmentNotification.uri,
+ contentResolver.getType(attachmentNotification.uri))
+ addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ }, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
+ } else {
+ PendingIntent.FLAG_CANCEL_CURRENT
+ }
+ )
val pendingDeleteIntent = PendingIntent.getService(this,
0,
Intent(this, AttachmentFileNotificationService::class.java).apply {
// No action to delete the service
putExtra(FILE_URI_KEY, attachmentNotification.uri)
- }, PendingIntent.FLAG_CANCEL_CURRENT)
+ }, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
+ } else {
+ PendingIntent.FLAG_CANCEL_CURRENT
+ }
+ )
val fileName = UriUtil.getFileData(this, attachmentNotification.uri)?.name
?: attachmentNotification.uri.path
diff --git a/app/src/main/java/com/kunzisoft/keepass/services/ClipboardEntryNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/ClipboardEntryNotificationService.kt
index f31f0472e..9372ab85b 100644
--- a/app/src/main/java/com/kunzisoft/keepass/services/ClipboardEntryNotificationService.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/services/ClipboardEntryNotificationService.kt
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.services
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
+import android.os.Build
import android.util.Log
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.model.EntryInfo
@@ -112,7 +113,13 @@ class ClipboardEntryNotificationService : LockNotificationService() {
putParcelableArrayListExtra(EXTRA_CLIPBOARD_FIELDS, fieldsToAdd)
}
return PendingIntent.getService(
- this, 0, copyIntent, PendingIntent.FLAG_UPDATE_CURRENT)
+ this, 0, copyIntent,
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+ } else {
+ PendingIntent.FLAG_UPDATE_CURRENT
+ }
+ )
}
private fun newNotification(title: String?, fieldsToAdd: ArrayList) {
@@ -162,7 +169,13 @@ class ClipboardEntryNotificationService : LockNotificationService() {
val cleanIntent = Intent(this, ClipboardEntryNotificationService::class.java)
cleanIntent.action = ACTION_CLEAN_CLIPBOARD
val cleanPendingIntent = PendingIntent.getService(
- this, 0, cleanIntent, PendingIntent.FLAG_UPDATE_CURRENT)
+ this, 0, cleanIntent,
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+ } else {
+ PendingIntent.FLAG_UPDATE_CURRENT
+ }
+ )
builder.setDeleteIntent(cleanPendingIntent)
//Get settings
diff --git a/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt
index 1898d56f4..81e41cc27 100644
--- a/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/services/DatabaseTaskNotificationService.kt
@@ -24,6 +24,7 @@ import android.content.Intent
import android.net.Uri
import android.os.*
import android.util.Log
+import androidx.media.app.NotificationCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.GroupActivity
import com.kunzisoft.keepass.app.database.CipherDatabaseEntity
@@ -225,6 +226,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
val actionRunnable: ActionRunnable? = when (intentAction) {
ACTION_DATABASE_CREATE_TASK -> buildDatabaseCreateActionTask(intent, database)
ACTION_DATABASE_LOAD_TASK -> buildDatabaseLoadActionTask(intent, database)
+ ACTION_DATABASE_MERGE_TASK -> buildDatabaseMergeActionTask(database)
ACTION_DATABASE_RELOAD_TASK -> buildDatabaseReloadActionTask(database)
ACTION_DATABASE_ASSIGN_PASSWORD_TASK -> buildDatabaseAssignPasswordActionTask(intent, database)
ACTION_DATABASE_CREATE_GROUP_TASK -> buildDatabaseCreateGroupActionTask(intent, database)
@@ -286,8 +288,12 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
} finally {
// Save the database info before performing action
- if (intentAction == ACTION_DATABASE_LOAD_TASK) {
- saveDatabaseInfo()
+ when (intentAction) {
+ ACTION_DATABASE_LOAD_TASK,
+ ACTION_DATABASE_MERGE_TASK,
+ ACTION_DATABASE_RELOAD_TASK -> {
+ saveDatabaseInfo()
+ }
}
val save = !database.isReadOnly
&& (intentAction == ACTION_DATABASE_SAVE
@@ -330,6 +336,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
return when (intentAction) {
ACTION_DATABASE_LOAD_TASK,
+ ACTION_DATABASE_MERGE_TASK,
ACTION_DATABASE_RELOAD_TASK,
null -> {
START_STICKY
@@ -366,6 +373,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
when (intentAction) {
ACTION_DATABASE_CREATE_TASK -> R.string.creating_database
ACTION_DATABASE_LOAD_TASK,
+ ACTION_DATABASE_MERGE_TASK,
ACTION_DATABASE_RELOAD_TASK -> R.string.loading_database
ACTION_DATABASE_SAVE -> R.string.saving_database
else -> {
@@ -377,6 +385,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
mMessageId = when (intentAction) {
ACTION_DATABASE_LOAD_TASK,
+ ACTION_DATABASE_MERGE_TASK,
ACTION_DATABASE_RELOAD_TASK -> null
else -> null
}
@@ -384,6 +393,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
mWarningId =
if (!saveAction
|| intentAction == ACTION_DATABASE_LOAD_TASK
+ || intentAction == ACTION_DATABASE_MERGE_TASK
|| intentAction == ACTION_DATABASE_RELOAD_TASK)
null
else
@@ -407,11 +417,21 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
this,
0,
Intent(this, GroupActivity::class.java),
- PendingIntent.FLAG_UPDATE_CURRENT
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+ } else {
+ PendingIntent.FLAG_UPDATE_CURRENT
+ }
)
val pendingDeleteIntent = PendingIntent.getBroadcast(
this,
- 4576, Intent(LOCK_ACTION), 0
+ 4576,
+ Intent(LOCK_ACTION),
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE
+ } else {
+ 0
+ }
)
// Add actions in notifications
notificationBuilder.apply {
@@ -420,9 +440,16 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
// Unfortunately swipe is disabled in lollipop+
setDeleteIntent(pendingDeleteIntent)
addAction(
- R.drawable.ic_lock_white_24dp, getString(R.string.lock),
+ R.drawable.ic_lock_database_white_32dp, getString(R.string.lock),
pendingDeleteIntent
)
+ // Won't work with Xiaomi and Kitkat
+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.R) {
+ setStyle(
+ NotificationCompat.MediaStyle()
+ .setShowActionsInCompactView(0)
+ )
+ }
}
}
}
@@ -579,6 +606,17 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
}
}
+ private fun buildDatabaseMergeActionTask(database: Database): ActionRunnable {
+ return MergeDatabaseRunnable(
+ this,
+ database,
+ this
+ ) { result ->
+ // No need to add each info to reload database
+ result.data = Bundle()
+ }
+ }
+
private fun buildDatabaseReloadActionTask(database: Database): ActionRunnable {
return ReloadDatabaseRunnable(
this,
@@ -889,6 +927,7 @@ open class DatabaseTaskNotificationService : LockNotificationService(), Progress
const val ACTION_DATABASE_CREATE_TASK = "ACTION_DATABASE_CREATE_TASK"
const val ACTION_DATABASE_LOAD_TASK = "ACTION_DATABASE_LOAD_TASK"
+ const val ACTION_DATABASE_MERGE_TASK = "ACTION_DATABASE_MERGE_TASK"
const val ACTION_DATABASE_RELOAD_TASK = "ACTION_DATABASE_RELOAD_TASK"
const val ACTION_DATABASE_ASSIGN_PASSWORD_TASK = "ACTION_DATABASE_ASSIGN_PASSWORD_TASK"
const val ACTION_DATABASE_CREATE_GROUP_TASK = "ACTION_DATABASE_CREATE_GROUP_TASK"
diff --git a/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt b/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt
index eb6776449..031a4c1ed 100644
--- a/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/services/KeyboardEntryNotificationService.kt
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.services
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
+import android.os.Build
import android.util.Log
import android.widget.Toast
import androidx.preference.PreferenceManager
@@ -93,7 +94,13 @@ class KeyboardEntryNotificationService : LockNotificationService() {
val deleteIntent = Intent(this, KeyboardEntryNotificationService::class.java).apply {
action = ACTION_CLEAN_KEYBOARD_ENTRY
}
- pendingDeleteIntent = PendingIntent.getService(this, 0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)
+ pendingDeleteIntent = PendingIntent.getService(this, 0, deleteIntent,
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
+ } else {
+ PendingIntent.FLAG_UPDATE_CURRENT
+ }
+ )
val builder = buildNewNotification()
.setSmallIcon(R.drawable.notification_ic_keyboard_key_24dp)
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/AutofillSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/AutofillSettingsFragment.kt
index f3462752b..35dab43ae 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/AutofillSettingsFragment.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/AutofillSettingsFragment.kt
@@ -57,6 +57,7 @@ class AutofillSettingsFragment : PreferenceFragmentCompat() {
}
if (dialogFragment != null) {
+ @Suppress("DEPRECATION")
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(parentFragmentManager, TAG_AUTOFILL_PREF_FRAGMENT)
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/MagikeyboardSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/MagikeyboardSettingsFragment.kt
index 3d7e6c9e6..c1af92d98 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/MagikeyboardSettingsFragment.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/MagikeyboardSettingsFragment.kt
@@ -48,6 +48,7 @@ class MagikeyboardSettingsFragment : PreferenceFragmentCompat() {
}
if (dialogFragment != null) {
+ @Suppress("DEPRECATION")
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(parentFragmentManager, TAG_PREF_FRAGMENT)
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt
index de6f36f05..10bbb3c8a 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/NestedAppSettingsFragment.kt
@@ -40,7 +40,6 @@ import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.ProFeatureDialogFragment
import com.kunzisoft.keepass.activities.dialogs.UnavailableFeatureDialogFragment
import com.kunzisoft.keepass.activities.stylish.Stylish
-import com.kunzisoft.keepass.app.database.CipherDatabaseAction
import com.kunzisoft.keepass.app.database.FileDatabaseHistoryAction
import com.kunzisoft.keepass.biometric.AdvancedUnlockManager
import com.kunzisoft.keepass.education.Education
@@ -157,7 +156,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
val intent = Intent(Settings.ACTION_REQUEST_SET_AUTOFILL_SERVICE)
intent.data = Uri.parse("package:com.kunzisoft.keepass.autofill.KeeAutofillService")
Log.d(javaClass.name, "Autofill enable service: intent=$intent")
- startActivityForResult(intent, REQUEST_CODE_AUTOFILL)
+ startActivity(intent)
} else {
Log.d(javaClass.name, "Autofill service already enabled.")
}
@@ -366,26 +365,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
) { _, _ ->
validate?.invoke()
deleteKeysAlertDialog?.setOnDismissListener(null)
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- AdvancedUnlockManager.deleteEntryKeyInKeystoreForBiometric(
- activity,
- object : AdvancedUnlockManager.AdvancedUnlockErrorCallback {
- fun showException(e: Exception) {
- Toast.makeText(context,
- getString(R.string.advanced_unlock_scanning_error, e.localizedMessage),
- Toast.LENGTH_SHORT).show()
- }
-
- override fun onInvalidKeyException(e: Exception) {
- showException(e)
- }
-
- override fun onGenericException(e: Exception) {
- showException(e)
- }
- })
- }
- CipherDatabaseAction.getInstance(activity.applicationContext).deleteAll()
+ AdvancedUnlockManager.deleteAllEntryKeysInKeystoreForBiometric(activity)
}
.setNegativeButton(resources.getString(android.R.string.cancel)
) { _, _ ->}
@@ -472,7 +452,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
getString(R.string.show_uuid_key),
getString(R.string.enable_education_screens_key),
getString(R.string.reset_education_screens_key) -> {
- DATABASE_APPEARANCE_PREFERENCE_CHANGED = true
+ DATABASE_PREFERENCE_CHANGED = true
}
}
return super.onPreferenceTreeClick(preference)
@@ -494,6 +474,7 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
if (dialogFragment != null) {
+ @Suppress("DEPRECATION")
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(parentFragmentManager, TAG_PREF_FRAGMENT)
}
@@ -533,9 +514,8 @@ class NestedAppSettingsFragment : NestedSettingsFragment() {
}
companion object {
- private const val REQUEST_CODE_AUTOFILL = 5201
private const val TAG_PREF_FRAGMENT = "TAG_PREF_FRAGMENT"
- var DATABASE_APPEARANCE_PREFERENCE_CHANGED = false
+ var DATABASE_PREFERENCE_CHANGED = false
}
}
\ No newline at end of file
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/NestedDatabaseSettingsFragment.kt b/app/src/main/java/com/kunzisoft/keepass/settings/NestedDatabaseSettingsFragment.kt
index a276c0c3d..c55a727f3 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/NestedDatabaseSettingsFragment.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/NestedDatabaseSettingsFragment.kt
@@ -30,8 +30,8 @@ import androidx.preference.PreferenceCategory
import androidx.preference.SwitchPreference
import com.kunzisoft.androidclearchroma.ChromaUtil
import com.kunzisoft.keepass.R
-import com.kunzisoft.keepass.activities.legacy.DatabaseRetrieval
import com.kunzisoft.keepass.activities.dialogs.AssignMasterKeyDialogFragment
+import com.kunzisoft.keepass.activities.legacy.DatabaseRetrieval
import com.kunzisoft.keepass.activities.legacy.resetAppTimeoutWhenViewTouchedOrFocused
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
@@ -51,13 +51,14 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
private val mDatabaseViewModel: DatabaseViewModel by activityViewModels()
private var mDatabase: Database? = null
private var mDatabaseReadOnly: Boolean = false
+ private var mMergeDataAllowed: Boolean = false
private var mDatabaseAutoSaveEnabled: Boolean = true
private var mScreen: Screen? = null
private var dbNamePref: InputTextPreference? = null
private var dbDescriptionPref: InputTextPreference? = null
- private var dbDefaultUsername: InputTextPreference? = null
+ private var dbDefaultUsernamePref: InputTextPreference? = null
private var dbCustomColorPref: DialogColorPreference? = null
private var dbDataCompressionPref: Preference? = null
private var recycleBinGroupPref: DialogListExplanationPreference? = null
@@ -115,6 +116,10 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
mDatabaseViewModel.saveDatabase(save)
}
+ private fun mergeDatabase() {
+ mDatabaseViewModel.mergeDatabase(false)
+ }
+
private fun reloadDatabase() {
mDatabaseViewModel.reloadDatabase(false)
}
@@ -122,6 +127,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
override fun onDatabaseRetrieved(database: Database?) {
mDatabase = database
mDatabaseReadOnly = database?.isReadOnly == true
+ mMergeDataAllowed = database?.isMergeDataAllowed() == true
mDatabase?.let {
if (it.loaded) {
@@ -164,29 +170,20 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
}
// Database default username
- dbDefaultUsername = findPreference(getString(R.string.database_default_username_key))
- if (database.allowDefaultUsername) {
- dbDefaultUsername?.summary = database.defaultUsername
- } else {
- dbDefaultUsername?.isEnabled = false
- // TODO dbGeneralPrefCategory?.removePreference(dbDefaultUsername)
- }
+ dbDefaultUsernamePref = findPreference(getString(R.string.database_default_username_key))
+ dbDefaultUsernamePref?.summary = database.defaultUsername
// Database custom color
dbCustomColorPref = findPreference(getString(R.string.database_custom_color_key))
- if (database.allowCustomColor) {
- dbCustomColorPref?.apply {
- try {
- color = Color.parseColor(database.customColor)
- summary = database.customColor
- } catch (e: Exception) {
- color = DialogColorPreference.DISABLE_COLOR
- summary = ""
- }
+ dbCustomColorPref?.apply {
+ val customColor = database.customColor
+ if (customColor != null) {
+ color = customColor
+ summary = ChromaUtil.getFormattedColorString(customColor, false)
+ } else{
+ color = DialogColorPreference.DISABLE_COLOR
+ summary = ""
}
- } else {
- dbCustomColorPref?.isEnabled = false
- // TODO dbGeneralPrefCategory?.removePreference(dbCustomColorPref)
}
// Version
@@ -348,12 +345,13 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
}
}
- private val colorSelectedListener: ((Boolean, Int)-> Unit) = { enable, color ->
- dbCustomColorPref?.summary = ChromaUtil.getFormattedColorString(color, false)
- if (enable) {
+ private val colorSelectedListener: ((Int?)-> Unit) = { color ->
+ if (color != null) {
dbCustomColorPref?.color = color
+ dbCustomColorPref?.summary = ChromaUtil.getFormattedColorString(color, false)
} else {
dbCustomColorPref?.color = DialogColorPreference.DISABLE_COLOR
+ dbCustomColorPref?.summary = ""
}
}
@@ -416,7 +414,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
mDatabase?.defaultUsername = oldDefaultUsername
oldDefaultUsername
}
- dbDefaultUsername?.summary = defaultUsernameToShow
+ dbDefaultUsernamePref?.summary = defaultUsernameToShow
}
DatabaseTaskNotificationService.ACTION_DATABASE_UPDATE_COLOR_TASK -> {
val oldColor = data.getString(DatabaseTaskNotificationService.OLD_ELEMENT_KEY)!!
@@ -426,7 +424,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
if (result.isSuccess) {
newColor
} else {
- mDatabase?.customColor = oldColor
+ mDatabase?.customColor = Color.parseColor(oldColor)
oldColor
}
dbCustomColorPref?.summary = defaultColorToShow
@@ -632,6 +630,7 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
}
if (dialogFragment != null && !mDatabaseReadOnly) {
+ @Suppress("DEPRECATION")
dialogFragment.setTargetFragment(this, 0)
dialogFragment.show(parentFragmentManager, TAG_PREF_FRAGMENT)
}
@@ -655,6 +654,10 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
inflater.inflate(R.menu.database, menu)
if (mDatabaseReadOnly) {
menu.findItem(R.id.menu_save_database)?.isVisible = false
+ menu.findItem(R.id.menu_merge_database)?.isVisible = false
+ }
+ if (!mMergeDataAllowed) {
+ menu.findItem(R.id.menu_merge_database)?.isVisible = false
}
}
@@ -664,6 +667,10 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
saveDatabase(!mDatabaseReadOnly)
true
}
+ R.id.menu_merge_database -> {
+ mergeDatabase()
+ return true
+ }
R.id.menu_reload_database -> {
reloadDatabase()
return true
@@ -680,6 +687,27 @@ class NestedDatabaseSettingsFragment : NestedSettingsFragment(), DatabaseRetriev
}
}
+ override fun onPreferenceTreeClick(preference: Preference?): Boolean {
+ // To reload group when database settings are modified
+ when (preference?.key) {
+ getString(R.string.database_name_key),
+ getString(R.string.database_description_key),
+ getString(R.string.database_default_username_key),
+ getString(R.string.database_custom_color_key),
+ getString(R.string.database_data_compression_key),
+ getString(R.string.database_data_remove_unlinked_attachments_key),
+ getString(R.string.recycle_bin_enable_key),
+ getString(R.string.recycle_bin_group_key),
+ getString(R.string.templates_group_enable_key),
+ getString(R.string.templates_group_uuid_key),
+ getString(R.string.max_history_items_key),
+ getString(R.string.max_history_size_key) -> {
+ NestedAppSettingsFragment.DATABASE_PREFERENCE_CHANGED = true
+ }
+ }
+ return super.onPreferenceTreeClick(preference)
+ }
+
companion object {
private const val TAG_PREF_FRAGMENT = "TAG_PREF_FRAGMENT"
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt
index c2059d234..36efc093e 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/PreferencesUtil.kt
@@ -278,6 +278,12 @@ object PreferencesUtil {
context.resources.getBoolean(R.bool.enable_auto_save_database_default))
}
+ fun isKeepScreenOnEnabled(context: Context): Boolean {
+ val prefs = PreferenceManager.getDefaultSharedPreferences(context)
+ return prefs.getBoolean(context.getString(R.string.enable_keep_screen_on_key),
+ context.resources.getBoolean(R.bool.enable_keep_screen_on_default))
+ }
+
fun isAdvancedUnlockEnable(context: Context): Boolean {
return isBiometricUnlockEnable(context) || isDeviceCredentialUnlockEnable(context)
}
@@ -595,6 +601,7 @@ object PreferencesUtil {
context.getString(R.string.delete_entered_password_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.enable_read_only_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.enable_auto_save_database_key) -> editor.putBoolean(name, value.toBoolean())
+ context.getString(R.string.enable_keep_screen_on_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.omit_backup_search_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.auto_focus_search_key) -> editor.putBoolean(name, value.toBoolean())
context.getString(R.string.subdomain_search_key) -> editor.putBoolean(name, value.toBoolean())
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt b/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt
index 2baa56008..04f19014c 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/SettingsActivity.kt
@@ -30,6 +30,7 @@ import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.fragment.app.Fragment
+import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.dialogs.AssignMasterKeyDialogFragment
import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper
@@ -49,11 +50,10 @@ open class SettingsActivity
private var backupManager: BackupManager? = null
private var mExternalFileHelper: ExternalFileHelper? = null
- private var appPropertiesFileCreationRequestCode: Int? = null
private var coordinatorLayout: CoordinatorLayout? = null
private var toolbar: Toolbar? = null
- private var lockView: View? = null
+ private var lockView: FloatingActionButton? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -64,6 +64,41 @@ open class SettingsActivity
toolbar = findViewById(R.id.toolbar)
mExternalFileHelper = ExternalFileHelper(this)
+ mExternalFileHelper?.buildOpenDocument { selectedFileUri ->
+ // Import app properties result
+ try {
+ selectedFileUri?.let { uri ->
+ val appProperties = Properties()
+ contentResolver?.openInputStream(uri)?.use { inputStream ->
+ appProperties.load(inputStream)
+ }
+ PreferencesUtil.setAppProperties(this, appProperties)
+
+ // Restart the current activity
+ reloadActivity()
+ Toast.makeText(this, R.string.success_import_app_properties, Toast.LENGTH_LONG).show()
+ }
+ } catch (e: Exception) {
+ Toast.makeText(this, R.string.error_import_app_properties, Toast.LENGTH_LONG).show()
+ Log.e(TAG, "Unable to import app properties", e)
+ }
+ }
+ mExternalFileHelper?.buildCreateDocument { createdFileUri ->
+ // Export app properties result
+ try {
+ createdFileUri?.let { uri ->
+ contentResolver?.openOutputStream(uri)?.use { outputStream ->
+ PreferencesUtil
+ .getAppProperties(this)
+ .store(outputStream, getString(R.string.description_app_properties))
+ }
+ Toast.makeText(this, R.string.success_export_app_properties, Toast.LENGTH_LONG).show()
+ }
+ } catch (e: Exception) {
+ Toast.makeText(this, R.string.error_export_app_properties, Toast.LENGTH_LONG).show()
+ Log.e(DatabaseLockActivity.TAG, "Unable to export app properties", e)
+ }
+ }
if (savedInstanceState?.getString(TITLE_KEY).isNullOrEmpty())
toolbar?.setTitle(R.string.settings)
@@ -78,11 +113,12 @@ open class SettingsActivity
}
if (savedInstanceState == null) {
+ lockView?.visibility = View.GONE
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, retrieveMainFragment())
.commit()
} else {
- lockView?.visibility = if (savedInstanceState.getBoolean(SHOW_LOCK)) View.VISIBLE else View.GONE
+ if (savedInstanceState.getBoolean(SHOW_LOCK)) lockView?.show() else lockView?.hide()
}
backupManager = BackupManager(this)
@@ -153,14 +189,14 @@ open class SettingsActivity
NestedSettingsFragment.Screen.DATABASE,
NestedSettingsFragment.Screen.DATABASE_MASTER_KEY,
NestedSettingsFragment.Screen.DATABASE_SECURITY -> {
- lockView?.visibility = View.VISIBLE
+ lockView?.show()
}
else -> {
- lockView?.visibility = View.GONE
+ lockView?.hide()
}
}
} else {
- lockView?.visibility = View.GONE
+ lockView?.hide()
}
}
@@ -217,54 +253,10 @@ open class SettingsActivity
}
fun exportAppProperties() {
- appPropertiesFileCreationRequestCode = mExternalFileHelper?.createDocument(getString(R.string.app_properties_file_name,
+ mExternalFileHelper?.createDocument(getString(R.string.app_properties_file_name,
DateTime.now().toLocalDateTime().toString("yyyy-MM-dd'_'HH-mm")))
}
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
-
- // Import app properties result
- try {
- mExternalFileHelper?.onOpenDocumentResult(requestCode, resultCode, data) { selectedFileUri ->
- selectedFileUri?.let { uri ->
- val appProperties = Properties()
- contentResolver?.openInputStream(uri)?.use { inputStream ->
- appProperties.load(inputStream)
- }
- PreferencesUtil.setAppProperties(this, appProperties)
-
- // Restart the current activity
- reloadActivity()
- Toast.makeText(this, R.string.success_import_app_properties, Toast.LENGTH_LONG).show()
- }
- }
- } catch (e: Exception) {
- Toast.makeText(this, R.string.error_import_app_properties, Toast.LENGTH_LONG).show()
- Log.e(TAG, "Unable to import app properties", e)
- }
-
- // Export app properties result
- try {
- if (requestCode == appPropertiesFileCreationRequestCode) {
- mExternalFileHelper?.onCreateDocumentResult(requestCode, resultCode, data) { createdFileUri ->
- createdFileUri?.let { uri ->
- contentResolver?.openOutputStream(uri)?.use { outputStream ->
- PreferencesUtil
- .getAppProperties(this)
- .store(outputStream, getString(R.string.description_app_properties))
- }
- Toast.makeText(this, R.string.success_export_app_properties, Toast.LENGTH_LONG).show()
- }
- }
- appPropertiesFileCreationRequestCode = null
- }
- } catch (e: Exception) {
- Toast.makeText(this, R.string.error_export_app_properties, Toast.LENGTH_LONG).show()
- Log.e(DatabaseLockActivity.TAG, "Unable to export app properties", e)
- }
- }
-
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preference/DialogColorPreference.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preference/DialogColorPreference.kt
index 0a4b3ebcc..68ff04335 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/preference/DialogColorPreference.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/preference/DialogColorPreference.kt
@@ -41,7 +41,7 @@ class DialogColorPreference @JvmOverloads constructor(context: Context,
}
override fun getDialogLayoutResource(): Int {
- return R.layout.pref_dialog_input_color
+ return R.layout.fragment_color_picker
}
companion object {
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt
index 8eaeb9c0a..0a1c7ff56 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseColorPreferenceDialogFragmentCompat.kt
@@ -30,27 +30,57 @@ import android.view.Window
import android.widget.CompoundButton
import androidx.annotation.ColorInt
import androidx.appcompat.app.AlertDialog
-import com.kunzisoft.androidclearchroma.ChromaUtil
import com.kunzisoft.androidclearchroma.IndicatorMode
import com.kunzisoft.androidclearchroma.colormode.ColorMode
import com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment
import com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment.*
+import com.kunzisoft.androidclearchroma.view.ChromaColorView
import com.kunzisoft.keepass.R
+import com.kunzisoft.keepass.activities.dialogs.ColorPickerDialogFragment
import com.kunzisoft.keepass.database.element.Database
class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialogFragmentCompat() {
private lateinit var rootView: View
private lateinit var enableSwitchView: CompoundButton
- private var chromaColorFragment: ChromaColorFragment? = null
+ private lateinit var chromaColorView: ChromaColorView
- var onColorSelectedListener: ((enable: Boolean, color: Int) -> Unit)? = null
+ var onColorSelectedListener: ((color: Int?) -> Unit)? = null
+
+ private var mDefaultColor = Color.WHITE
+ private var mActivated = false
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val alertDialogBuilder = AlertDialog.Builder(requireActivity())
- rootView = requireActivity().layoutInflater.inflate(R.layout.pref_dialog_input_color, null)
+ rootView = requireActivity().layoutInflater.inflate(R.layout.fragment_color_picker, null)
enableSwitchView = rootView.findViewById(R.id.switch_element)
+ chromaColorView = rootView.findViewById(R.id.chroma_color_view)
+
+ if (savedInstanceState != null) {
+ if (savedInstanceState.containsKey(ARG_INITIAL_COLOR)) {
+ mDefaultColor = savedInstanceState.getInt(ARG_INITIAL_COLOR)
+ }
+ if (savedInstanceState.containsKey(ARG_ACTIVATED)) {
+ mActivated = savedInstanceState.getBoolean(ARG_ACTIVATED)
+ }
+ } else {
+ arguments?.apply {
+ if (containsKey(ARG_INITIAL_COLOR)) {
+ mDefaultColor = getInt(ARG_INITIAL_COLOR)
+ }
+ if (containsKey(ARG_ACTIVATED)) {
+ mActivated = getBoolean(ARG_ACTIVATED)
+ }
+ }
+ }
+ enableSwitchView.isChecked = mActivated
+ chromaColorView.currentColor = mDefaultColor
+
+ chromaColorView.setOnColorChangedListener {
+ if (!enableSwitchView.isChecked)
+ enableSwitchView.isChecked = true
+ }
alertDialogBuilder.setPositiveButton(android.R.string.ok) { _, _ ->
onDialogClosed(true)
@@ -68,8 +98,6 @@ class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialog
// request a window without the title
dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
- dialog.setOnShowListener { measureLayout(it as Dialog) }
-
return dialog
}
@@ -77,73 +105,48 @@ class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialog
super.onDatabaseRetrieved(database)
database?.let {
- val initColor = try {
+ var initColor = it.customColor
+ if (initColor != null) {
enableSwitchView.isChecked = true
- Color.parseColor(it.customColor)
- } catch (e: Exception) {
+ } else {
enableSwitchView.isChecked = false
- DEFAULT_COLOR
+ initColor = DEFAULT_COLOR
}
+ chromaColorView.currentColor = initColor
arguments?.putInt(ARG_INITIAL_COLOR, initColor)
}
-
- val fragmentManager = childFragmentManager
- chromaColorFragment = fragmentManager.findFragmentByTag(TAG_FRAGMENT_COLORS) as ChromaColorFragment?
-
- if (chromaColorFragment == null) {
- chromaColorFragment = newInstance(arguments)
- fragmentManager.beginTransaction().apply {
- add(com.kunzisoft.androidclearchroma.R.id.color_dialog_container, chromaColorFragment!!, TAG_FRAGMENT_COLORS)
- commit()
- }
- }
}
override fun onDialogClosed(database: Database?, positiveResult: Boolean) {
super.onDialogClosed(database, positiveResult)
if (positiveResult) {
- val customColorEnable = enableSwitchView.isChecked
- chromaColorFragment?.currentColor?.let { currentColor ->
- onColorSelectedListener?.invoke(customColorEnable, currentColor)
- database?.let {
- val newColor = if (customColorEnable) {
- ChromaUtil.getFormattedColorString(currentColor, false)
- } else {
- ""
- }
- val oldColor = database.customColor
- database.customColor = newColor
- saveColor(oldColor, newColor)
- }
+ val newColor: Int? = if (enableSwitchView.isChecked)
+ chromaColorView.currentColor
+ else
+ null
+ onColorSelectedListener?.invoke(newColor)
+ database?.let {
+ val oldColor = database.customColor
+ database.customColor = newColor
+ saveColor(oldColor, newColor)
}
}
}
- /**
- * Set new dimensions to dialog
- * @param ad dialog
- */
- private fun measureLayout(ad: Dialog) {
- val typedValue = TypedValue()
- resources.getValue(com.kunzisoft.androidclearchroma.R.dimen.chroma_dialog_height_multiplier, typedValue, true)
- val heightMultiplier = typedValue.float
- val height = (ad.context.resources.displayMetrics.heightPixels * heightMultiplier).toInt()
-
- resources.getValue(com.kunzisoft.androidclearchroma.R.dimen.chroma_dialog_width_multiplier, typedValue, true)
- val widthMultiplier = typedValue.float
- val width = (ad.context.resources.displayMetrics.widthPixels * widthMultiplier).toInt()
-
- ad.window?.setLayout(width, height)
- }
-
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
return rootView
}
- companion object {
- private const val TAG_FRAGMENT_COLORS = "TAG_FRAGMENT_COLORS"
+ override fun onSaveInstanceState(outState: Bundle) {
+ super.onSaveInstanceState(outState)
+ outState.putInt(ARG_INITIAL_COLOR, chromaColorView.currentColor)
+ outState.putBoolean(ARG_ACTIVATED, mActivated)
+ }
+ companion object {
+ private const val ARG_INITIAL_COLOR = "ARG_INITIAL_COLOR"
+ private const val ARG_ACTIVATED = "ARG_ACTIVATED"
@ColorInt
const val DEFAULT_COLOR: Int = Color.WHITE
@@ -151,9 +154,7 @@ class DatabaseColorPreferenceDialogFragmentCompat : DatabaseSavePreferenceDialog
val fragment = DatabaseColorPreferenceDialogFragmentCompat()
val bundle = Bundle(1)
bundle.putString(ARG_KEY, key)
- bundle.putInt(ARG_INITIAL_COLOR, Color.BLACK)
- bundle.putInt(ARG_COLOR_MODE, ColorMode.RGB.ordinal)
- bundle.putInt(ARG_INDICATOR_MODE, IndicatorMode.HEX.ordinal)
+ bundle.putInt(ARG_INITIAL_COLOR, DEFAULT_COLOR)
fragment.arguments = bundle
return fragment
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt
index b10afb610..06a7bae25 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DatabaseSavePreferenceDialogFragmentCompat.kt
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.activityViewModels
+import com.kunzisoft.androidclearchroma.ChromaUtil
import com.kunzisoft.keepass.activities.legacy.DatabaseRetrieval
import com.kunzisoft.keepass.database.crypto.EncryptionAlgorithm
import com.kunzisoft.keepass.database.crypto.kdf.KdfEngine
@@ -76,9 +77,17 @@ abstract class DatabaseSavePreferenceDialogFragmentCompat
// To inherit to save element in database
}
- protected fun saveColor(oldColor: String,
- newColor: String) {
- mDatabaseViewModel.saveColor(oldColor, newColor, mDatabaseAutoSaveEnable)
+ protected fun saveColor(oldColor: Int?,
+ newColor: Int?) {
+ val oldColorString = if (oldColor != null)
+ ChromaUtil.getFormattedColorString(oldColor, false)
+ else
+ ""
+ val newColorString = if (newColor != null)
+ ChromaUtil.getFormattedColorString(newColor, false)
+ else
+ ""
+ mDatabaseViewModel.saveColor(oldColorString, newColorString, mDatabaseAutoSaveEnable)
}
protected fun saveCompression(oldCompression: CompressionAlgorithm,
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DurationDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DurationDialogFragmentCompat.kt
index ca29c48b2..c7eb377aa 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DurationDialogFragmentCompat.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/DurationDialogFragmentCompat.kt
@@ -19,7 +19,13 @@
*/
package com.kunzisoft.keepass.settings.preferencedialogfragment
+import android.app.AlarmManager
+import android.content.Context
+import android.content.Intent
+import android.os.Build
import android.os.Bundle
+import android.provider.Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM
+import android.util.Log
import android.view.View
import android.widget.NumberPicker
import com.kunzisoft.keepass.R
@@ -62,6 +68,30 @@ class DurationDialogFragmentCompat : InputPreferenceDialogFragmentCompat() {
}
}
+ override fun onResume() {
+ super.onResume()
+
+ (context?.applicationContext?.getSystemService(Context.ALARM_SERVICE) as AlarmManager?)?.let { alarmManager ->
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
+ && !alarmManager.canScheduleExactAlarms()) {
+ setExplanationText(R.string.warning_exact_alarm)
+ setExplanationButton(R.string.permission) {
+ // Open the exact alarm permission screen
+ try {
+ startActivity(Intent().apply {
+ action = ACTION_REQUEST_SCHEDULE_EXACT_ALARM
+ })
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to open exact alarm permission screen", e)
+ }
+ }
+ } else {
+ explanationText = ""
+ setExplanationButton("") {}
+ }
+ }
+ }
+
private fun durationToDaysHoursMinutesSeconds(duration: Long) {
if (duration < 0) {
mEnabled = false
@@ -164,6 +194,7 @@ class DurationDialogFragmentCompat : InputPreferenceDialogFragmentCompat() {
}
companion object {
+ private const val TAG = "DurationDialogFrgCmpt"
private const val ENABLE_KEY = "ENABLE_KEY"
private const val DAYS_KEY = "DAYS_KEY"
private const val HOURS_KEY = "HOURS_KEY"
diff --git a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/InputPreferenceDialogFragmentCompat.kt b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/InputPreferenceDialogFragmentCompat.kt
index 49a3bc247..d88fa8f7f 100644
--- a/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/InputPreferenceDialogFragmentCompat.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/settings/preferencedialogfragment/InputPreferenceDialogFragmentCompat.kt
@@ -22,6 +22,7 @@ package com.kunzisoft.keepass.settings.preferencedialogfragment
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
+import android.widget.Button
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.TextView
@@ -35,6 +36,7 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
private var inputTextView: EditText? = null
private var textUnitView: TextView? = null
private var textExplanationView: TextView? = null
+ private var explanationButton: Button? = null
private var switchElementView: CompoundButton? = null
private var mOnInputTextEditorActionListener: TextView.OnEditorActionListener? = null
@@ -100,6 +102,27 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
explanationText = getString(explanationTextId)
}
+ val explanationButtonText: String?
+ get() = explanationButton?.text?.toString() ?: ""
+
+ fun setExplanationButton(explanationButtonText: String?, clickListener: View.OnClickListener) {
+ explanationButton?.apply {
+ if (explanationButtonText != null && explanationButtonText.isNotEmpty()) {
+ text = explanationButtonText
+ visibility = View.VISIBLE
+ setOnClickListener(clickListener)
+ } else {
+ text = ""
+ visibility = View.GONE
+ setOnClickListener(null)
+ }
+ }
+ }
+
+ fun setExplanationButton(@StringRes explanationButtonTextId: Int, clickListener: View.OnClickListener) {
+ setExplanationButton(getString(explanationButtonTextId), clickListener)
+ }
+
override fun onBindDialogView(view: View) {
super.onBindDialogView(view)
@@ -128,6 +151,8 @@ abstract class InputPreferenceDialogFragmentCompat : PreferenceDialogFragmentCom
textUnitView?.visibility = View.GONE
textExplanationView = view.findViewById(R.id.explanation_text)
textExplanationView?.visibility = View.GONE
+ explanationButton = view.findViewById(R.id.explanation_button)
+ explanationButton?.visibility = View.GONE
switchElementView = view.findViewById(R.id.switch_element)
switchElementView?.visibility = View.GONE
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/tasks/ActionRunnable.kt b/app/src/main/java/com/kunzisoft/keepass/tasks/ActionRunnable.kt
index 3554c69e8..07a7c476d 100644
--- a/app/src/main/java/com/kunzisoft/keepass/tasks/ActionRunnable.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/tasks/ActionRunnable.kt
@@ -31,9 +31,13 @@ abstract class ActionRunnable: Runnable {
var result: Result = Result()
override fun run() {
- onStartRun()
- onActionRun()
- onFinishRun()
+ try {
+ onStartRun()
+ onActionRun()
+ onFinishRun()
+ } catch (runException: Exception) {
+ setError(runException)
+ }
}
abstract fun onStartRun()
diff --git a/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt b/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt
index f8a8514aa..aee7c370c 100644
--- a/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/timeout/TimeoutHelper.kt
@@ -43,9 +43,14 @@ object TimeoutHelper {
private fun getLockPendingIntent(context: Context): PendingIntent {
return PendingIntent.getBroadcast(context.applicationContext,
- REQUEST_ID,
- Intent(LOCK_ACTION),
- PendingIntent.FLAG_CANCEL_CURRENT)
+ REQUEST_ID,
+ Intent(LOCK_ACTION),
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_CANCEL_CURRENT
+ } else {
+ PendingIntent.FLAG_CANCEL_CURRENT
+ }
+ )
}
/**
@@ -61,9 +66,26 @@ object TimeoutHelper {
val triggerTime = System.currentTimeMillis() + timeout
Log.d(TAG, "TimeoutHelper start")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager.setExact(AlarmManager.RTC, triggerTime, getLockPendingIntent(context))
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
+ && !alarmManager.canScheduleExactAlarms()) {
+ alarmManager.set(
+ AlarmManager.RTC,
+ triggerTime,
+ getLockPendingIntent(context)
+ )
+ } else {
+ alarmManager.setExact(
+ AlarmManager.RTC,
+ triggerTime,
+ getLockPendingIntent(context)
+ )
+ }
} else {
- alarmManager.set(AlarmManager.RTC, triggerTime, getLockPendingIntent(context))
+ alarmManager.set(
+ AlarmManager.RTC,
+ triggerTime,
+ getLockPendingIntent(context)
+ )
}
}
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt b/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt
index cca7a5762..78e8c5edb 100644
--- a/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/utils/BroadcastAction.kt
@@ -60,18 +60,41 @@ class LockReceiver(var lockAction: () -> Unit) : BroadcastReceiver() {
Intent.ACTION_SCREEN_OFF -> {
if (PreferencesUtil.isLockDatabaseWhenScreenShutOffEnable(context)) {
mLockPendingIntent = PendingIntent.getBroadcast(context,
- 4575,
- Intent(intent).apply {
- action = LOCK_ACTION
- },
- 0)
+ 4575,
+ Intent(intent).apply {
+ action = LOCK_ACTION
+ },
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
+ PendingIntent.FLAG_IMMUTABLE
+ } else {
+ 0
+ }
+ )
// Launch the effective action after a small time
val first: Long = System.currentTimeMillis() + context.getString(R.string.timeout_screen_off).toLong()
- val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager?
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
- alarmManager?.setExact(AlarmManager.RTC_WAKEUP, first, mLockPendingIntent)
- } else {
- alarmManager?.set(AlarmManager.RTC_WAKEUP, first, mLockPendingIntent)
+ (context.getSystemService(ALARM_SERVICE) as AlarmManager?)?.let { alarmManager ->
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S
+ && !alarmManager.canScheduleExactAlarms()) {
+ alarmManager.set(
+ AlarmManager.RTC_WAKEUP,
+ first,
+ mLockPendingIntent
+ )
+ } else {
+ alarmManager.setExact(
+ AlarmManager.RTC_WAKEUP,
+ first,
+ mLockPendingIntent
+ )
+ }
+ } else {
+ alarmManager.set(
+ AlarmManager.RTC_WAKEUP,
+ first,
+ mLockPendingIntent
+ )
+ }
}
} else {
cancelLockPendingIntent(context)
diff --git a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt
index dada5bb1d..71e50ff3e 100644
--- a/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/utils/UriUtil.kt
@@ -86,7 +86,7 @@ object UriUtil {
private fun isFileScheme(fileUri: Uri): Boolean {
val scheme = fileUri.scheme
- if (scheme == null || scheme.isEmpty() || scheme.toLowerCase(Locale.ENGLISH) == "file") {
+ if (scheme == null || scheme.isEmpty() || scheme.lowercase(Locale.ENGLISH) == "file") {
return true
}
return false
@@ -94,7 +94,7 @@ object UriUtil {
private fun isContentScheme(fileUri: Uri): Boolean {
val scheme = fileUri.scheme
- if (scheme != null && scheme.toLowerCase(Locale.ENGLISH) == "content") {
+ if (scheme != null && scheme.lowercase(Locale.ENGLISH) == "content") {
return true
}
return false
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/AdvancedUnlockInfoView.kt b/app/src/main/java/com/kunzisoft/keepass/view/AdvancedUnlockInfoView.kt
index fa492037f..26af9c974 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/AdvancedUnlockInfoView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/AdvancedUnlockInfoView.kt
@@ -104,10 +104,12 @@ class AdvancedUnlockInfoView @JvmOverloads constructor(context: Context,
return unlockMessageTextView?.text?.toString() ?: ""
}
set(value) {
- if (value == null || value.isEmpty())
+ if (value == null || value.isEmpty()) {
unlockMessageTextView?.visibility = GONE
- else
+ } else {
unlockMessageTextView?.visibility = VISIBLE
+ stopIconViewAnimation()
+ }
unlockMessageTextView?.text = value ?: ""
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/KeyFileSelectionView.kt b/app/src/main/java/com/kunzisoft/keepass/view/KeyFileSelectionView.kt
index 3bf85978a..182623ac7 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/KeyFileSelectionView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/KeyFileSelectionView.kt
@@ -2,6 +2,7 @@ package com.kunzisoft.keepass.view
import android.content.Context
import android.net.Uri
+import android.os.Parcelable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.TextView
@@ -9,6 +10,9 @@ import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.material.textfield.TextInputLayout
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.utils.UriUtil
+import android.os.Parcel
+import android.os.Parcelable.Creator
+
class KeyFileSelectionView @JvmOverloads constructor(context: Context,
attrs: AttributeSet? = null,
@@ -54,4 +58,45 @@ class KeyFileSelectionView @JvmOverloads constructor(context: Context,
UriUtil.getFileData(context, value)?.name ?: value.path
} ?: ""
}
+
+ override fun onSaveInstanceState(): Parcelable {
+ val superState = super.onSaveInstanceState()
+ val saveState = SavedState(superState)
+ saveState.mUri = this.mUri
+ return saveState
+ }
+
+ override fun onRestoreInstanceState(state: Parcelable?) {
+ if (state !is SavedState) {
+ super.onRestoreInstanceState(state)
+ return
+ }
+ super.onRestoreInstanceState(state.superState)
+ this.mUri = state.mUri
+ }
+
+ internal class SavedState : BaseSavedState {
+ var mUri: Uri? = null
+
+ constructor(superState: Parcelable?) : super(superState) {}
+
+ private constructor(parcel: Parcel) : super(parcel) {
+ mUri = parcel.readParcelable(Uri::class.java.classLoader)
+ }
+
+ override fun writeToParcel(out: Parcel, flags: Int) {
+ super.writeToParcel(out, flags)
+ out.writeParcelable(mUri, flags)
+ }
+
+ companion object CREATOR : Creator {
+ override fun createFromParcel(parcel: Parcel): SavedState {
+ return SavedState(parcel)
+ }
+
+ override fun newArray(size: Int): Array {
+ return arrayOfNulls(size)
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/MagikeyboardView.kt b/app/src/main/java/com/kunzisoft/keepass/view/MagikeyboardView.kt
deleted file mode 100644
index 0c7771004..000000000
--- a/app/src/main/java/com/kunzisoft/keepass/view/MagikeyboardView.kt
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Copyright 2019 Jeremy Jamet / Kunzisoft.
- *
- * This file is part of KeePassDX.
- *
- * KeePassDX is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * KeePassDX is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with KeePassDX. If not, see .
- *
- */
-@file:Suppress("DEPRECATION")
-
-package com.kunzisoft.keepass.view
-
-import android.content.Context
-import android.inputmethodservice.Keyboard
-import android.inputmethodservice.KeyboardView
-import android.os.Build
-import androidx.annotation.RequiresApi
-import android.util.AttributeSet
-
-import com.kunzisoft.keepass.magikeyboard.MagikeyboardService.Companion.KEY_BACK_KEYBOARD
-import com.kunzisoft.keepass.magikeyboard.MagikeyboardService.Companion.KEY_CHANGE_KEYBOARD
-
-class MagikeyboardView : KeyboardView {
-
- constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
-
- constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
-
- @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
- constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes)
-
- override fun onLongPress(key: Keyboard.Key): Boolean {
- return if (key.codes[0] == KEY_BACK_KEYBOARD) {
- onKeyboardActionListener.onKey(KEY_CHANGE_KEYBOARD, IntArray(0))
- true
- } else {
- //Log.d("LatinKeyboardView", "KEY: " + key.codes[0]);
- super.onLongPress(key)
- }
- }
-}
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt
index ce970154d..5f158f8b8 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateAbstractView.kt
@@ -46,6 +46,10 @@ abstract class TemplateAbstractView<
protected var headerContainerView: ViewGroup
protected var entryIconView: ImageView
+ protected var backgroundColorView: View
+ protected var foregroundColorView: View
+ protected var backgroundColorButton: ImageView
+ protected var foregroundColorButton: ImageView
private var titleContainerView: ViewGroup
protected var templateContainerView: ViewGroup
private var customFieldsContainerView: SectionView
@@ -57,6 +61,10 @@ abstract class TemplateAbstractView<
headerContainerView = findViewById(R.id.template_header_container)
entryIconView = findViewById(R.id.template_icon_button)
+ backgroundColorView = findViewById(R.id.template_background_color)
+ foregroundColorView = findViewById(R.id.template_foreground_color)
+ backgroundColorButton = findViewById(R.id.template_background_color_button)
+ foregroundColorButton = findViewById(R.id.template_foreground_color_button)
titleContainerView = findViewById(R.id.template_title_container)
templateContainerView = findViewById(R.id.template_fields_container)
// To fix card view margin below Marshmallow
@@ -85,8 +93,10 @@ abstract class TemplateAbstractView<
fun setTemplate(template: Template?) {
if (mTemplate != template) {
mTemplate = template
+ applyTemplateParametersToEntry()
if (mEntryInfo != null) {
- populateEntryInfoWithViews(true)
+ populateEntryInfoWithViews(templateFieldNotEmpty = true,
+ retrieveDefaultValues = false)
}
buildTemplateAndPopulateInfo()
clearFocus()
@@ -95,6 +105,16 @@ abstract class TemplateAbstractView<
}
}
+ private fun applyTemplateParametersToEntry() {
+ // Change the entry icon by the template icon
+ mTemplate?.icon?.let { templateIcon ->
+ mEntryInfo?.icon = templateIcon
+ }
+ // Change the entry color by the template color
+ mEntryInfo?.backgroundColor = mTemplate?.backgroundColor
+ mEntryInfo?.foregroundColor = mTemplate?.foregroundColor
+ }
+
private fun buildTemplate() {
// Retrieve preferences
mHideProtectedValue = PreferencesUtil.hideProtectedValue(context)
@@ -203,9 +223,7 @@ abstract class TemplateAbstractView<
setNumberLines(20)
},
TemplateAttributeAction.CUSTOM_EDITION
- ).apply {
- default = field.protectedValue.stringValue
- }
+ )
return buildViewForTemplateField(customFieldTemplateAttribute, field, FIELD_CUSTOM_TAG)
}
@@ -390,50 +408,74 @@ abstract class TemplateAbstractView<
return emptyList()
}
- protected open fun populateEntryInfoWithViews(templateFieldNotEmpty: Boolean) {
+ protected open fun populateEntryInfoWithViews(templateFieldNotEmpty: Boolean,
+ retrieveDefaultValues: Boolean) {
if (mEntryInfo == null)
mEntryInfo = EntryInfo()
- // Icon already populate
-
- val titleView: TEntryFieldView? = findViewWithTag(FIELD_TITLE_TAG)
- titleView?.value?.let {
- mEntryInfo?.title = it
+ try {
+ val titleView: TEntryFieldView? = findViewWithTag(FIELD_TITLE_TAG)
+ titleView?.value?.let {
+ mEntryInfo?.title = it
+ }
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to populate title view", e)
}
- val userNameView: TEntryFieldView? = findViewWithTag(FIELD_USERNAME_TAG)
- userNameView?.value?.let {
- mEntryInfo?.username = it
+ try {
+ val userNameView: TEntryFieldView? = findViewWithTag(FIELD_USERNAME_TAG)
+ userNameView?.value?.let {
+ mEntryInfo?.username = it
+ }
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to populate username view", e)
}
- val passwordView: TEntryFieldView? = findViewWithTag(FIELD_PASSWORD_TAG)
- passwordView?.value?.let {
- mEntryInfo?.password = it
+ try {
+ val passwordView: TEntryFieldView? = findViewWithTag(FIELD_PASSWORD_TAG)
+ passwordView?.value?.let {
+ mEntryInfo?.password = it
+ }
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to populate password view", e)
}
- val urlView: TEntryFieldView? = findViewWithTag(FIELD_URL_TAG)
- urlView?.value?.let {
- mEntryInfo?.url = it
+ try {
+ val urlView: TEntryFieldView? = findViewWithTag(FIELD_URL_TAG)
+ urlView?.value?.let {
+ mEntryInfo?.url = it
+ }
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to populate url view", e)
}
- val expirationView: TDateTimeView? = findViewWithTag(FIELD_EXPIRES_TAG)
- expirationView?.activation?.let {
- mEntryInfo?.expires = it
- }
- expirationView?.dateTime?.let {
- mEntryInfo?.expiryTime = it
+ try {
+ val expirationView: TDateTimeView? = findViewWithTag(FIELD_EXPIRES_TAG)
+ expirationView?.activation?.let {
+ mEntryInfo?.expires = it
+ }
+ expirationView?.dateTime?.let {
+ mEntryInfo?.expiryTime = it
+ }
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to populate expiration view", e)
}
- val notesView: TEntryFieldView? = findViewWithTag(FIELD_NOTES_TAG)
- notesView?.value?.let {
- mEntryInfo?.notes = it
+ try {
+ val notesView: TEntryFieldView? = findViewWithTag(FIELD_NOTES_TAG)
+ notesView?.value?.let {
+ mEntryInfo?.notes = it
+ }
+ } catch (e: Exception) {
+ Log.e(TAG, "Unable to populate notes view", e)
}
- retrieveCustomFieldsFromView(templateFieldNotEmpty)
+ retrieveCustomFieldsFromView(templateFieldNotEmpty, retrieveDefaultValues)
}
fun getEntryInfo(): EntryInfo {
- populateEntryInfoWithViews(true)
+ populateEntryInfoWithViews(templateFieldNotEmpty = true,
+ retrieveDefaultValues = true)
return mEntryInfo ?: EntryInfo()
}
@@ -479,23 +521,31 @@ abstract class TemplateAbstractView<
return mViewFields.indexOfFirst { it.field.name.equals(name, true) }
}
- private fun retrieveCustomFieldsFromView(templateFieldNotEmpty: Boolean = false) {
+ private fun retrieveCustomFieldsFromView(templateFieldNotEmpty: Boolean = false,
+ retrieveDefaultValues: Boolean = false) {
mEntryInfo?.customFields = mViewFields.mapNotNull {
- getCustomField(it.field.name, templateFieldNotEmpty)
+ getCustomField(it.field.name, templateFieldNotEmpty, retrieveDefaultValues)
}.toMutableList()
}
protected fun getCustomField(fieldName: String): Field {
- return getCustomField(fieldName, false)
- ?: Field(fieldName, ProtectedString(false))
+ return getCustomField(fieldName,
+ templateFieldNotEmpty = false,
+ retrieveDefaultValues = false
+ ) ?: Field(fieldName, ProtectedString(false))
}
- private fun getCustomField(fieldName: String, templateFieldNotEmpty: Boolean): Field? {
+ private fun getCustomField(fieldName: String,
+ templateFieldNotEmpty: Boolean,
+ retrieveDefaultValues: Boolean): Field? {
getViewFieldByName(fieldName)?.let { fieldId ->
- val editView: View? = fieldId.view
+ val editView: View = fieldId.view
if (editView is GenericFieldView) {
// Do not return field with a default value
- val defaultViewValue = if (editView.value == editView.default) "" else editView.value
+ val defaultViewValue =
+ if (retrieveDefaultValues || editView.value != editView.default) {
+ editView.value
+ } else ""
if (!templateFieldNotEmpty
|| (editView.tag == FIELD_CUSTOM_TAG && defaultViewValue.isNotEmpty())) {
return Field(
@@ -641,7 +691,8 @@ abstract class TemplateAbstractView<
override fun onSaveInstanceState(): Parcelable {
val superSave = super.onSaveInstanceState()
val saveState = SavedState(superSave)
- populateEntryInfoWithViews(false)
+ populateEntryInfoWithViews(templateFieldNotEmpty = false,
+ retrieveDefaultValues = false)
saveState.template = this.mTemplate
saveState.entryInfo = this.mEntryInfo
onSaveEntryInstanceState(saveState)
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt
index 956621a09..bc42ae0e3 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/TemplateEditView.kt
@@ -5,13 +5,17 @@ import android.os.Build
import android.util.AttributeSet
import android.view.View
import androidx.annotation.IdRes
+import androidx.core.graphics.BlendModeColorFilterCompat
+import androidx.core.graphics.BlendModeCompat
import androidx.core.view.isVisible
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.Field
import com.kunzisoft.keepass.database.element.icon.IconImage
import com.kunzisoft.keepass.database.element.security.ProtectedString
-import com.kunzisoft.keepass.database.element.template.*
+import com.kunzisoft.keepass.database.element.template.TemplateAttribute
+import com.kunzisoft.keepass.database.element.template.TemplateAttributeAction
+import com.kunzisoft.keepass.database.element.template.TemplateField
import com.kunzisoft.keepass.otp.OtpEntryFields
import org.joda.time.DateTime
@@ -51,7 +55,53 @@ class TemplateEditView @JvmOverloads constructor(context: Context,
fun setIcon(iconImage: IconImage) {
mEntryInfo?.icon = iconImage
- populateIconMethod?.invoke(entryIconView, iconImage)
+ refreshIcon()
+ }
+
+ fun setOnBackgroundColorClickListener(onClickListener: OnClickListener) {
+ backgroundColorButton.setOnClickListener(onClickListener)
+ }
+
+ fun getBackgroundColor(): Int? {
+ return mEntryInfo?.backgroundColor
+ }
+
+ fun setBackgroundColor(color: Int?) {
+ applyBackgroundColor(color)
+ mEntryInfo?.backgroundColor = color
+ }
+
+ private fun applyBackgroundColor(color: Int?) {
+ if (color != null) {
+ backgroundColorView.background.colorFilter = BlendModeColorFilterCompat
+ .createBlendModeColorFilterCompat(color, BlendModeCompat.SRC_ATOP)
+ backgroundColorView.visibility = View.VISIBLE
+ } else {
+ backgroundColorView.visibility = View.GONE
+ }
+ }
+
+ fun setOnForegroundColorClickListener(onClickListener: OnClickListener) {
+ foregroundColorButton.setOnClickListener(onClickListener)
+ }
+
+ fun getForegroundColor(): Int? {
+ return mEntryInfo?.foregroundColor
+ }
+
+ fun setForegroundColor(color: Int?) {
+ applyForegroundColor(color)
+ mEntryInfo?.foregroundColor = color
+ }
+
+ private fun applyForegroundColor(color: Int?) {
+ if (color != null) {
+ foregroundColorView.background.colorFilter = BlendModeColorFilterCompat
+ .createBlendModeColorFilterCompat(color, BlendModeCompat.SRC_ATOP)
+ foregroundColorView.visibility = View.VISIBLE
+ } else {
+ foregroundColorView.visibility = View.GONE
+ }
}
override fun preProcessTemplate() {
@@ -64,6 +114,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context,
TextEditFieldView(it).apply {
// hiddenProtectedValue (mHideProtectedValue) don't work with TextInputLayout
setProtection(field.protectedValue.isProtected)
+ default = templateAttribute.default
setMaxChars(templateAttribute.options.getNumberChars())
setMaxLines(templateAttribute.options.getNumberLines())
setActionClick(templateAttribute, field, this)
@@ -79,7 +130,7 @@ class TemplateEditView @JvmOverloads constructor(context: Context,
return context?.let {
TextSelectFieldView(it).apply {
setItems(templateAttribute.options.getListItems())
- default = field.protectedValue.stringValue
+ default = templateAttribute.default
setActionClick(templateAttribute, field, this)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NO
@@ -195,11 +246,14 @@ class TemplateEditView @JvmOverloads constructor(context: Context,
override fun populateViewsWithEntryInfo(showEmptyFields: Boolean): List {
refreshIcon()
+ applyBackgroundColor(mEntryInfo?.backgroundColor)
+ applyForegroundColor(mEntryInfo?.foregroundColor)
return super.populateViewsWithEntryInfo(showEmptyFields)
}
- override fun populateEntryInfoWithViews(templateFieldNotEmpty: Boolean) {
- super.populateEntryInfoWithViews(templateFieldNotEmpty)
+ override fun populateEntryInfoWithViews(templateFieldNotEmpty: Boolean,
+ retrieveDefaultValues: Boolean) {
+ super.populateEntryInfoWithViews(templateFieldNotEmpty, retrieveDefaultValues)
mEntryInfo?.otpModel = OtpEntryFields.parseFields { key ->
getCustomField(key).protectedValue.toString()
}?.otpModel
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TextFieldView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TextFieldView.kt
index f754107cf..af400b70d 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/TextFieldView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/TextFieldView.kt
@@ -73,7 +73,7 @@ class TextFieldView @JvmOverloads constructor(context: Context,
}
private val valueView = AppCompatTextView(context).apply {
setTextAppearance(context,
- R.style.KeepassDXStyle_TextAppearance_TextEntryItem)
+ R.style.KeepassDXStyle_TextAppearance_TextNode)
layoutParams = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT).also {
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt b/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt
index e420dcc9e..781c5dda6 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/TextSelectFieldView.kt
@@ -194,6 +194,7 @@ class TextSelectFieldView @JvmOverloads constructor(context: Context,
get() = valueSpinnerAdapter.getItem(mDefaultPosition)
set(value) {
mDefaultPosition = valueSpinnerAdapter.getPosition(value)
+ valueSpinnerAdapter.notifyDataSetChanged()
}
override fun setOnActionClickListener(onActionClickListener: OnClickListener?,
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/ToolbarAction.kt b/app/src/main/java/com/kunzisoft/keepass/view/ToolbarAction.kt
index 7c103a828..852bd9aa3 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/ToolbarAction.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/ToolbarAction.kt
@@ -80,7 +80,8 @@ class ToolbarAction @JvmOverloads constructor(context: Context,
mActionModeCallback = null
}
- fun invalidateMenu() {
+ override fun invalidateMenu() {
+ super.invalidateMenu()
open()
mActionModeCallback?.onPrepareActionMode(actionMode, menu)
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt b/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt
index b433aacfc..119ec2732 100644
--- a/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/view/ViewUtil.kt
@@ -23,9 +23,7 @@ import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ValueAnimator
import android.content.Context
-import android.graphics.Color
-import android.graphics.Paint
-import android.graphics.Typeface
+import android.graphics.*
import android.text.Selection
import android.text.Spannable
import android.text.SpannableString
@@ -37,6 +35,7 @@ import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.TextView
import android.widget.Toast
+import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
@@ -44,6 +43,16 @@ import com.google.android.material.snackbar.Snackbar
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.settings.PreferencesUtil
import com.kunzisoft.keepass.tasks.ActionRunnable
+import androidx.appcompat.view.menu.ActionMenuItemView
+
+import android.widget.ImageView
+import androidx.appcompat.widget.ActionMenuView
+
+import androidx.core.graphics.drawable.DrawableCompat
+
+import android.graphics.drawable.Drawable
+import com.google.android.material.appbar.CollapsingToolbarLayout
+
/**
* Replace font by monospace, must be called after setText()
@@ -207,4 +216,50 @@ fun CoordinatorLayout.showActionErrorIfNeeded(result: ActionRunnable.Result) {
Snackbar.make(this, message, Snackbar.LENGTH_LONG).asError().show()
}
}
+}
+
+fun Toolbar.changeControlColor(color: Int) {
+ val colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
+ for (i in 0 until childCount) {
+ val view: View = getChildAt(i)
+ // Change the color of back button (or open drawer button).
+ if (view is ImageView) {
+ //Action Bar back button
+ view.drawable.colorFilter = colorFilter
+ }
+ if (view is ActionMenuView) {
+ view.post {
+ for (j in 0 until view.childCount) {
+ // Change the color of any ActionMenuViews - icons that
+ // are not back button, nor text, nor overflow menu icon.
+ val innerView: View = view.getChildAt(j)
+ if (innerView is ActionMenuItemView) {
+ innerView.compoundDrawables.forEach { drawable ->
+ //Important to set the color filter in separate thread,
+ //by adding it to the message queue
+ //Won't work otherwise.
+ drawable?.colorFilter = colorFilter
+ }
+ }
+ }
+ }
+ }
+ }
+ // Change the color of title and subtitle.
+ setTitleTextColor(color)
+ setSubtitleTextColor(color)
+ // Change the color of the Overflow Menu icon.
+ var drawable: Drawable? = overflowIcon
+ if (drawable != null) {
+ drawable = DrawableCompat.wrap(drawable)
+ DrawableCompat.setTint(drawable.mutate(), color)
+ overflowIcon = drawable
+ }
+ invalidate()
+}
+
+fun CollapsingToolbarLayout.changeTitleColor(color: Int) {
+ setCollapsedTitleTextColor(color)
+ setExpandedTitleColor(color)
+ invalidate()
}
\ No newline at end of file
diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/AdvancedUnlockViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/AdvancedUnlockViewModel.kt
new file mode 100644
index 000000000..1c5c0a140
--- /dev/null
+++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/AdvancedUnlockViewModel.kt
@@ -0,0 +1,32 @@
+package com.kunzisoft.keepass.viewmodels
+
+import android.net.Uri
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.ViewModel
+
+class AdvancedUnlockViewModel : ViewModel() {
+
+ var allowAutoOpenBiometricPrompt : Boolean = true
+ var deviceCredentialAuthSucceeded: Boolean? = null
+
+ val onInitAdvancedUnlockModeRequested : LiveData get() = _onInitAdvancedUnlockModeRequested
+ private val _onInitAdvancedUnlockModeRequested = SingleLiveEvent()
+
+ val onUnlockAvailabilityCheckRequested : LiveData get() = _onUnlockAvailabilityCheckRequested
+ private val _onUnlockAvailabilityCheckRequested = SingleLiveEvent()
+
+ val onDatabaseFileLoaded : LiveData get() = _onDatabaseFileLoaded
+ private val _onDatabaseFileLoaded = SingleLiveEvent()
+
+ fun initAdvancedUnlockMode() {
+ _onInitAdvancedUnlockModeRequested.call()
+ }
+
+ fun checkUnlockAvailability() {
+ _onUnlockAvailabilityCheckRequested.call()
+ }
+
+ fun databaseFileLoaded(databaseUri: Uri?) {
+ _onDatabaseFileLoaded.value = databaseUri
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/ColorPickerViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/ColorPickerViewModel.kt
new file mode 100644
index 000000000..282e3f425
--- /dev/null
+++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/ColorPickerViewModel.kt
@@ -0,0 +1,15 @@
+package com.kunzisoft.keepass.viewmodels
+
+import androidx.lifecycle.LiveData
+import androidx.lifecycle.MutableLiveData
+import androidx.lifecycle.ViewModel
+
+class ColorPickerViewModel: ViewModel() {
+
+ val colorPicked : LiveData get() = _colorPicked
+ private val _colorPicked = MutableLiveData()
+
+ fun pickColor(color: Int?) {
+ _colorPicked.value = color
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/DatabaseViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/DatabaseViewModel.kt
index 9e3dafcaa..8608b144a 100644
--- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/DatabaseViewModel.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/DatabaseViewModel.kt
@@ -21,6 +21,9 @@ class DatabaseViewModel: ViewModel() {
val saveDatabase : LiveData get() = _saveDatabase
private val _saveDatabase = SingleLiveEvent()
+ val mergeDatabase : LiveData get() = _mergeDatabase
+ private val _mergeDatabase = SingleLiveEvent()
+
val reloadDatabase : LiveData get() = _reloadDatabase
private val _reloadDatabase = SingleLiveEvent()
@@ -84,6 +87,10 @@ class DatabaseViewModel: ViewModel() {
_saveDatabase.value = save
}
+ fun mergeDatabase(fixDuplicateUuid: Boolean) {
+ _mergeDatabase.value = fixDuplicateUuid
+ }
+
fun reloadDatabase(fixDuplicateUuid: Boolean) {
_reloadDatabase.value = fixDuplicateUuid
}
diff --git a/app/src/main/java/com/kunzisoft/keepass/viewmodels/NodeEditViewModel.kt b/app/src/main/java/com/kunzisoft/keepass/viewmodels/NodeEditViewModel.kt
index 80d9ea79a..4c55af246 100644
--- a/app/src/main/java/com/kunzisoft/keepass/viewmodels/NodeEditViewModel.kt
+++ b/app/src/main/java/com/kunzisoft/keepass/viewmodels/NodeEditViewModel.kt
@@ -14,6 +14,14 @@ abstract class NodeEditViewModel : ViewModel() {
val onIconSelected : LiveData get() = _onIconSelected
private val _onIconSelected = SingleLiveEvent()
+ private var mColorRequest: ColorRequest = ColorRequest.BACKGROUND
+ val requestColorSelection : LiveData get() = _requestColorSelection
+ private val _requestColorSelection = SingleLiveEvent()
+ val onBackgroundColorSelected : LiveData get() = _onBackgroundColorSelected
+ private val _onBackgroundColorSelected = SingleLiveEvent()
+ val onForegroundColorSelected : LiveData get() = _onForegroundColorSelected
+ private val _onForegroundColorSelected = SingleLiveEvent()
+
val requestDateTimeSelection : LiveData get() = _requestDateTimeSelection
private val _requestDateTimeSelection = SingleLiveEvent()
val onDateSelected : LiveData get() = _onDateSelected
@@ -29,6 +37,23 @@ abstract class NodeEditViewModel : ViewModel() {
_onIconSelected.value = iconImage
}
+ fun requestBackgroundColorSelection(initialColor: Int?) {
+ mColorRequest = ColorRequest.BACKGROUND
+ _requestColorSelection.value = initialColor
+ }
+
+ fun requestForegroundColorSelection(initialColor: Int?) {
+ mColorRequest = ColorRequest.FOREGROUND
+ _requestColorSelection.value = initialColor
+ }
+
+ fun selectColor(color: Int?) {
+ when (mColorRequest) {
+ ColorRequest.BACKGROUND -> _onBackgroundColorSelected.value = color
+ ColorRequest.FOREGROUND -> _onForegroundColorSelected.value = color
+ }
+ }
+
fun requestDateTimeSelection(dateInstant: DateInstant) {
_requestDateTimeSelection.value = dateInstant
}
@@ -40,4 +65,8 @@ abstract class NodeEditViewModel : ViewModel() {
fun selectTime(hours: Int, minutes: Int) {
_onTimeSelected.value = DataTime(hours, minutes)
}
+
+ private enum class ColorRequest {
+ BACKGROUND, FOREGROUND
+ }
}
\ No newline at end of file
diff --git a/app/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixListData.kt b/app/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixListData.kt
index 0439b304d..be2387e0a 100644
--- a/app/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixListData.kt
+++ b/app/src/main/java/mozilla/components/lib/publicsuffixlist/PublicSuffixListData.kt
@@ -144,7 +144,7 @@ internal class PublicSuffixListData(
}
companion object {
- val WILDCARD_LABEL = byteArrayOf('*'.toByte())
+ val WILDCARD_LABEL = byteArrayOf('*'.code.toByte())
val PREVAILING_RULE = listOf("*")
val EMPTY_RULE = listOf()
const val EXCEPTION_MARKER = '!'
diff --git a/app/src/main/java/mozilla/components/lib/publicsuffixlist/ext/ByteArray.kt b/app/src/main/java/mozilla/components/lib/publicsuffixlist/ext/ByteArray.kt
index 7316e4053..c0a215ebe 100644
--- a/app/src/main/java/mozilla/components/lib/publicsuffixlist/ext/ByteArray.kt
+++ b/app/src/main/java/mozilla/components/lib/publicsuffixlist/ext/ByteArray.kt
@@ -36,7 +36,7 @@ internal fun ByteArray.binarySearch(labels: List, labelIndex: Int): S
while (true) {
val byte0 = if (expectDot) {
expectDot = false
- '.'.toByte()
+ '.'.code.toByte()
} else {
labels[currentLabelIndex][currentLabelByteIndex] and BITMASK
}
@@ -103,7 +103,7 @@ internal fun ByteArray.binarySearch(labels: List