blob: 7a27be7417b9dd9f5bf316b0f7c30757da3ac351 [file] [log] [blame]
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.android_webview;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.content.R;
import org.chromium.content_public.browser.ActionModeCallbackHelper;
import org.chromium.content_public.browser.SelectionPopupController;
import org.chromium.content_public.browser.WebContents;
/**
* A class that handles selection action mode for Android WebView.
*/
public class AwActionModeCallback implements ActionMode.Callback {
private final Context mContext;
private final AwContents mAwContents;
private final ActionModeCallbackHelper mHelper;
private int mAllowedMenuItems;
public AwActionModeCallback(Context context, AwContents awContents, WebContents webContents) {
mContext = context;
mAwContents = awContents;
mHelper =
SelectionPopupController.fromWebContents(webContents).getActionModeCallbackHelper();
mHelper.setAllowedMenuItems(0); // No item is allowed by default for WebView.
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
int allowedItems = (getAllowedMenu(ActionModeCallbackHelper.MENU_ITEM_SHARE)
| getAllowedMenu(ActionModeCallbackHelper.MENU_ITEM_WEB_SEARCH)
| getAllowedMenu(ActionModeCallbackHelper.MENU_ITEM_PROCESS_TEXT));
if (allowedItems != mAllowedMenuItems) {
mHelper.setAllowedMenuItems(allowedItems);
mAllowedMenuItems = allowedItems;
}
mHelper.onCreateActionMode(mode, menu);
return true;
}
private int getAllowedMenu(int menuItem) {
boolean showItem = true;
if (menuItem == ActionModeCallbackHelper.MENU_ITEM_WEB_SEARCH) {
showItem = isWebSearchAvailable();
}
return showItem && mAwContents.isSelectActionModeAllowed(menuItem) ? menuItem : 0;
}
private boolean isWebSearchAvailable() {
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.EXTRA_NEW_SEARCH, true);
return mContext.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY).size() > 0;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return mHelper.onPrepareActionMode(mode, menu);
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (!mHelper.isActionModeValid()) return true;
int groupId = item.getGroupId();
if (groupId == R.id.select_action_menu_text_processing_menus) {
processText(item.getIntent());
// The ActionMode is not dismissed to match the behavior with
// TextView in Android M.
} else {
return mHelper.onActionItemClicked(mode, item);
}
return true;
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mHelper.onDestroyActionMode();
}
private void processText(Intent intent) {
RecordUserAction.record("MobileActionMode.ProcessTextIntent");
assert Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
String query = ActionModeCallbackHelper.sanitizeQuery(mHelper.getSelectedText(),
ActionModeCallbackHelper.MAX_SEARCH_QUERY_LENGTH);
if (TextUtils.isEmpty(query)) return;
intent.putExtra(Intent.EXTRA_PROCESS_TEXT, query);
try {
mAwContents.startProcessTextIntent(intent);
} catch (android.content.ActivityNotFoundException ex) {
// If no app handles it, do nothing.
}
}
}