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

Commit 220cf991 authored by Mindy Pereira's avatar Mindy Pereira
Browse files

Add cut/copy/paste contextual menu.

Fixes bug:5290467 Contextual action list is not showing the list of items

Change-Id: I6407e2562613e24b64620bdb598589f196c32654
parent 6547c777
Loading
Loading
Loading
Loading
+103 −8
Original line number Original line Diff line number Diff line
@@ -19,11 +19,14 @@ package com.android.calculator2;
import android.content.ClipData;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Context;
import android.content.res.Resources;
import android.text.Editable;
import android.text.Editable;
import android.text.InputType;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ActionMode;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.MotionEvent;
@@ -32,6 +35,12 @@ import android.widget.Toast;


public class CalculatorEditText extends EditText {
public class CalculatorEditText extends EditText {


    private static final String LOG_TAG = "Calculator2";
    private static final int CUT = 0;
    private static final int COPY = 1;
    private static final int PASTE = 2;
    private String[] mMenuItemsStrings;

    public CalculatorEditText(Context context, AttributeSet attrs) {
    public CalculatorEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        super(context, attrs);
        setCustomSelectionActionModeCallback(new NoTextSelectionMode());
        setCustomSelectionActionModeCallback(new NoTextSelectionMode());
@@ -49,22 +58,108 @@ public class CalculatorEditText extends EditText {


    @Override
    @Override
    public boolean performLongClick() {
    public boolean performLongClick() {
        final Editable text = getText();
        showContextMenu();
        if (TextUtils.isEmpty(text)) {
        return true;
            return false;
    }
    }


    private class MenuHandler implements MenuItem.OnMenuItemClickListener {
        public boolean onMenuItemClick(MenuItem item) {
            return onTextContextMenuItem(item.getTitle());
        }
    }

    public boolean onTextContextMenuItem(CharSequence title) {
        boolean handled = false;
        if (TextUtils.equals(title, mMenuItemsStrings[CUT])) {
            cutContent();
            handled = true;
        } else if (TextUtils.equals(title,  mMenuItemsStrings[COPY])) {
            copyContent();
            copyContent();
        return true;
            handled = true;
        } else if (TextUtils.equals(title,  mMenuItemsStrings[PASTE])) {
            pasteContent();
            handled = true;
        }
        return handled;
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu) {
        MenuHandler handler = new MenuHandler();
        if (mMenuItemsStrings == null) {
            Resources resources = getResources();
            mMenuItemsStrings = new String[3];
            mMenuItemsStrings[CUT] = resources.getString(android.R.string.cut);
            mMenuItemsStrings[COPY] = resources.getString(android.R.string.copy);
            mMenuItemsStrings[PASTE] = resources.getString(android.R.string.paste);
        }
        for (int i = 0; i < mMenuItemsStrings.length; i++) {
            menu.add(Menu.NONE, i, i, mMenuItemsStrings[i]).setOnMenuItemClickListener(handler);
        }
        if (getText().length() == 0) {
            menu.getItem(CUT).setVisible(false);
            menu.getItem(COPY).setVisible(false);
        }
        ClipData primaryClip = getPrimaryClip();
        if (primaryClip == null || primaryClip.getItemCount() == 0
                || !canPaste(primaryClip.getItemAt(0).coerceToText(getContext()))) {
            menu.getItem(PASTE).setVisible(false);
        }
    }

    private void setPrimaryClip(ClipData clip) {
        ClipboardManager clipboard = (ClipboardManager) getContext().
                getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(clip);
    }
    }


    private void copyContent() {
    private void copyContent() {
        final Editable text = getText();
        final Editable text = getText();
        setSelection(0, text.length());
        int textLength = text.length();
        setSelection(0, textLength);
        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
                Context.CLIPBOARD_SERVICE);
                Context.CLIPBOARD_SERVICE);
        clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
        clipboard.setPrimaryClip(ClipData.newPlainText(null, text));
        Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show();
        Toast.makeText(getContext(), R.string.text_copied_toast, Toast.LENGTH_SHORT).show();
        setSelection(textLength);
    }

    private void cutContent() {
        final Editable text = getText();
        int textLength = text.length();
        setSelection(0, textLength);
        setPrimaryClip(ClipData.newPlainText(null, text));
        ((Editable) getText()).delete(0, textLength);
        setSelection(0);
    }

    private ClipData getPrimaryClip() {
        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(
                Context.CLIPBOARD_SERVICE);
        return clipboard.getPrimaryClip();
    }

    private void pasteContent() {
        ClipData clip = getPrimaryClip();
        if (clip != null) {
            for (int i = 0; i < clip.getItemCount(); i++) {
                CharSequence paste = clip.getItemAt(i).coerceToText(getContext());
                if (canPaste(paste)) {
                    ((Editable) getText()).insert(getSelectionEnd(), paste);
                }
            }
        }
    }

    private boolean canPaste(CharSequence paste) {
        boolean canPaste = true;
        try {
            Float.parseFloat(paste.toString());
        } catch (NumberFormatException e) {
            Log.e(LOG_TAG, "Error turning string to integer. Ignoring paste.", e);
            canPaste = false;
        }
        return canPaste;
    }
    }


    class NoTextSelectionMode implements ActionMode.Callback {
    class NoTextSelectionMode implements ActionMode.Callback {