aboutsummaryrefslogtreecommitdiff
path: root/WordPress/src/main/java/org/wordpress/android/widgets/MultiUsernameEditText.java
blob: c453fc77b61e95edbe9ace3ad0b587e11a6f0c0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.wordpress.android.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;


/**
 * Used to handle backspace in People Management username field
 */
public class MultiUsernameEditText extends WPEditText {

    private OnBackspacePressedListener mOnBackspacePressedListener;


    public MultiUsernameEditText(Context context) {
        super(context);
    }

    public MultiUsernameEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MultiUsernameEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setOnBackspacePressedListener(OnBackspacePressedListener onBackspacePressedListener) {
        this.mOnBackspacePressedListener = onBackspacePressedListener;
    }

    public interface OnBackspacePressedListener {
        boolean onBackspacePressed();
    }

    @Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
        //in this case it makes sense to not change EditText to fullscreen mode at landscape
        outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI;
        return new MultiUsernameEditTextInputConnection(this, false);
    }


    private class MultiUsernameEditTextInputConnection extends BaseInputConnection {

        public MultiUsernameEditTextInputConnection(View targetView, boolean fullEditor) {
            super(targetView, fullEditor);
        }

        @Override
        public boolean sendKeyEvent(KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
                if (mOnBackspacePressedListener != null) {
                    //if username was not deleted pass event to parent method and return the result
                    return !mOnBackspacePressedListener.onBackspacePressed() && super.sendKeyEvent(event);
                }
            }
            return super.sendKeyEvent(event);
        }

        @Override
        public boolean deleteSurroundingText(int beforeLength, int afterLength) {
            if (beforeLength == 1 && afterLength == 0) {
                return sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL))
                        && sendKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DEL));
            }

            return super.deleteSurroundingText(beforeLength, afterLength);
        }

    }

}