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
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166

content / public / android / java / src / org / chromium / content_public / browser / ActionModeCallbackHelper.java [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content_public.browser;

import android.content.Intent;
import android.graphics.Rect;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;

import androidx.annotation.Nullable;

import org.chromium.content.browser.selection.SelectionPopupControllerImpl;

/**
 * Helper class for {@link WebActionMode} encapsulating
 * {@link android.view.ActionMode}. Exposes the functionality of the class
 * for embedder to provide with the callback instance that interacts with it.
 */
public abstract class ActionModeCallbackHelper {
    private static final String TAG = "ActionModeHelper";

    /** Google search doesn't support requests slightly larger than this. */
    public static final int MAX_SEARCH_QUERY_LENGTH = 1000;

    public static final int MENU_ITEM_SHARE = WebSettings.MENU_ITEM_SHARE;
    public static final int MENU_ITEM_WEB_SEARCH = WebSettings.MENU_ITEM_WEB_SEARCH;
    public static final int MENU_ITEM_PROCESS_TEXT = WebSettings.MENU_ITEM_PROCESS_TEXT;

    public static final EmptyActionCallback EMPTY_CALLBACK = new EmptyActionCallback();

    /**
     * Trim a given string query to be processed safely.
     *
     * @param query a raw query to sanitize.
     * @param maxLength maximum length to which the query will be truncated.
     */
    public static String sanitizeQuery(String query, int maxLength) {
        return SelectionPopupControllerImpl.sanitizeQuery(query, maxLength);
    }

    /** Empty {@link ActionMode.Callback} that does nothing. Used for {@link #EMPTY_CALLBACK}. */
    private static class EmptyActionCallback extends ActionModeCallback {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {}

        @Override
        public void onGetContentRect(ActionMode mode, View view, Rect outRect) {}

        @Override
        public boolean onDropdownItemClicked(
                int groupId,
                int id,
                @Nullable Intent intent,
                @Nullable View.OnClickListener clickListener) {
            return false;
        }
    }
    ;

    /**
     * @return {@code true} if selection action mode is started and in proper working state. if
     *     null, it was not started or is in finished, destroyed state.
     */
    public abstract boolean isActionModeValid();

    /**
     * @see ActionMode#finish()
     */
    public abstract void finishActionMode();

    /** Dismisses the menu. No matter which type (i.e. ActionMode, Dropdown) is showing. */
    public abstract void dismissMenu();

    /**
     * @return The selected text (empty if no text is selected).
     */
    public abstract String getSelectedText();

    /**
     * @return {@link RenderFrameHost} object only available during page selection,
     *      if there is a valid ActionMode available.
     */
    @Nullable
    public abstract RenderFrameHost getRenderFrameHost();

    /**
     * Called when the processed text is replied from an activity that supports
     * Intent.ACTION_PROCESS_TEXT.
     * @param resultCode the code that indicates if the activity successfully processed the text
     * @param data the reply that contains the processed text.
     */
    public abstract void onReceivedProcessTextResult(int resultCode, Intent data);

    /**
     * Set the action mode menu items allowed on the content.
     * @param allowedMenuItems bit field of item-flag mapping.
     */
    public abstract void setAllowedMenuItems(int allowedMenuItems);

    /**
     * If the passed in mode and menu matches one of the MENU_ITEM_* items, return it.
     * Otherwise, return 0. Only call from inside the implementation of
     * ActionMode.Callback#onActionItemClicked.
     */
    public abstract int getAllowedMenuItemIfAny(ActionMode mode, MenuItem item);

    /**
     * Returns the {@link WebSettings} menu item that maps to the menu item properties
     * passed in. Otherwise, returns 0.
     * @param groupId the group id of the menu item.
     * @param id the id of the menu item.
     */
    public abstract int getAllowedMenuItemIfAny(int groupId, int id);

    /**
     * @see {@link ActionMode.Callback#onCreateActionMode(ActionMode, Menu)}
     */
    public abstract void onCreateActionMode(ActionMode mode, Menu menu);

    /**
     * @see {@link ActionMode.Callback#onPrepareActionMode(ActionMode, Menu)}
     */
    public abstract boolean onPrepareActionMode(ActionMode mode, Menu menu);

    /**
     * @see {@link ActionMode.Callback#onActionItemClicked(ActionMode, MenuItem)}
     */
    public abstract boolean onActionItemClicked(ActionMode mode, MenuItem item);

    /** Callback for when a drop-down menu item is clicked. */
    public abstract boolean onDropdownItemClicked(
            int groupId,
            int id,
            @Nullable Intent intent,
            @Nullable View.OnClickListener clickListener);

    /**
     * @see {@link ActionMode.Callback#onDestroyActionMode(ActionMode)}
     */
    public abstract void onDestroyActionMode();

    /**
     * @see {@link ActionMode.Callback2#onDestroyActionMode(ActionMode)}
     */
    public abstract void onGetContentRect(ActionMode mode, View view, Rect outRect);
}