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

Commit 7f0c5bd5 authored by Andrei Stingaceanu's avatar Andrei Stingaceanu
Browse files

Introduce "Share" option to the floating text selection toolbar.

Change-Id: I04b564224847eeb4e5f2a61f6a41f6046a1969a1
parent 17e11fad
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1722,6 +1722,7 @@ package android {
    field public static final int selectAll = 16908319; // 0x102001f
    field public static final int selectTextMode = 16908333; // 0x102002d
    field public static final int selectedIcon = 16908302; // 0x102000e
    field public static final int shareText = 16908343; // 0x1020037
    field public static final int startSelectingText = 16908328; // 0x1020028
    field public static final int statusBarBackground = 16908335; // 0x102002f
    field public static final int stopSelectingText = 16908329; // 0x1020029
+1 −0
Original line number Diff line number Diff line
@@ -1799,6 +1799,7 @@ package android {
    field public static final int selectAll = 16908319; // 0x102001f
    field public static final int selectTextMode = 16908333; // 0x102002d
    field public static final int selectedIcon = 16908302; // 0x102000e
    field public static final int shareText = 16908343; // 0x1020037
    field public static final int startSelectingText = 16908328; // 0x1020028
    field public static final int statusBarBackground = 16908335; // 0x102002f
    field public static final int stopSelectingText = 16908329; // 0x1020029
+6 −0
Original line number Diff line number Diff line
@@ -3055,6 +3055,12 @@ public class Editor {
                                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            }

            if (mTextView.canShare()) {
                menu.add(0, TextView.ID_SHARE, 0, com.android.internal.R.string.share).
                        setShowAsAction(
                                MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
            }

            menu.add(0, TextView.ID_SELECT_ALL, 0, com.android.internal.R.string.selectAll).
                    setIcon(styledAttributes.getResourceId(
                            R.styleable.SelectionModeDrawables_actionModeSelectAllDrawable, 0)).
+26 −5
Original line number Diff line number Diff line
@@ -8955,13 +8955,14 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
    static final int ID_CUT = android.R.id.cut;
    static final int ID_COPY = android.R.id.copy;
    static final int ID_PASTE = android.R.id.paste;
    static final int ID_SHARE = android.R.id.shareText;
    static final int ID_PASTE_AS_PLAIN_TEXT = android.R.id.pasteAsPlainText;
    static final int ID_REPLACE = android.R.id.replaceText;

    /**
     * Called when a context menu option for the text view is selected.  Currently
     * this will be one of {@link android.R.id#selectAll}, {@link android.R.id#cut},
     * {@link android.R.id#copy} or {@link android.R.id#paste}.
     * {@link android.R.id#copy}, {@link android.R.id#paste} or {@link android.R.id#shareText}.
     *
     * @return true if the context menu item action was performed.
     */
@@ -9014,6 +9015,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                setPrimaryClip(ClipData.newPlainText(null, getTransformedText(min, max)));
                stopSelectionActionMode();
                return true;

            case ID_SHARE:
                shareSelectedText();
                return true;
        }
        return false;
    }
@@ -9091,15 +9096,15 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
     * If provided, this ActionMode.Callback will be used to create the ActionMode when text
     * selection is initiated in this View.
     *
     * The standard implementation populates the menu with a subset of Select All, Cut, Copy and
     * Paste actions, depending on what this View supports.
     * The standard implementation populates the menu with a subset of Select All, Cut, Copy,
     * Paste and Share actions, depending on what this View supports.
     *
     * A custom implementation can add new entries in the default menu in its
     * {@link android.view.ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)} method. The
     * default actions can also be removed from the menu using
     * {@link android.view.Menu#removeItem(int)} and passing {@link android.R.id#selectAll},
     * {@link android.R.id#cut}, {@link android.R.id#copy} or {@link android.R.id#paste} ids as
     * parameters.
     * {@link android.R.id#cut}, {@link android.R.id#copy}, {@link android.R.id#paste} or
     * {@link android.R.id#shareText} ids as parameters.
     *
     * Returning false from
     * {@link android.view.ActionMode.Callback#onCreateActionMode(ActionMode, Menu)} will prevent
@@ -9168,6 +9173,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        return false;
    }

    boolean canShare() {
        return canCopy();
    }

    boolean canPaste() {
        return (mText instanceof Editable &&
                mEditor != null && mEditor.mKeyListener != null &&
@@ -9241,6 +9250,18 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        }
    }

    private void shareSelectedText() {
        String selectedText = getSelectedText();
        if (selectedText != null && !selectedText.isEmpty()) {
            Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.removeExtra(android.content.Intent.EXTRA_TEXT);
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, selectedText);
            getContext().startActivity(Intent.createChooser(sharingIntent, null));
            stopSelectionActionMode();
        }
    }

    private void setPrimaryClip(ClipData clip) {
        ClipboardManager clipboard = (ClipboardManager) getContext().
                getSystemService(Context.CLIPBOARD_SERVICE);
+1 −0
Original line number Diff line number Diff line
@@ -93,6 +93,7 @@
  <item type="id" name="undo" />
  <item type="id" name="redo" />
  <item type="id" name="replaceText" />
  <item type="id" name="shareText" />

  <!-- Accessibility action identifier for {@link android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction#ACTION_SHOW_ON_SCREEN}. -->
  <item type="id" name="accessibilityActionShowOnScreen" />
Loading