2

My goal is to hide the default system context menu when selecting text within a web view and have my own custom menu that can trigger the original menu item actions.

To do that I have to be able to identify what each menu item is so I can hook it up to the relevant custom button. I have got this working for the menu items that have key shortcuts but not all do so I'm resorting to sniffing the item title (as the item ID's are not stable for example). The problem with this approach is it doesn't work across different languages. I found what I thought was the correct string resources (e.g. android.R.string.websearch) but when I test with Spanish the menu item title is "Búsqueda en la Web" and the string resource is "Buscar en la Web".

So I either need help finding the correct resource ids/strings for the items in that context menu, or need help finding a less hacky way to detect it. Here's my attempt...

for(int n=0;n < systemSelectionMenu.size();n++){
    MenuItem item = systemSelectionMenu.getItem(n);
    item.setVisible(false); // Hide menu item from system context menu
    switch(item.getAlphabeticShortcut()){
        case 'c': // Copy
            contextCopyActionId = item.getItemId();
            break;
        case 'x': // Cut
            contextCutActionId = item.getItemId();
            break;
        case 'v': // Paste
            contextPasteActionId = item.getItemId();
            break;
        case 'a': // Select all
            contextSelectAllActionId = item.getItemId();
            break;
        default:
            // Hack to determine which menu item does what...
            Resources res = Resources.getSystem();
            // Note some of these resource strings are internal so the only way to get them is through getIdentifier, this means they might change at some point
            // Note 2: item.getId() is not stable and cannot be used directly
            int webSearchId = res.getIdentifier("websearch", "string", "android"); // only way to get android.com.internal.R.string.websearch (android.R.string.websearch)
            String webSearchString = "";
            try {
                webSearchString = res.getString(webSearchId);
            }catch(NotFoundException e){}
            int shareId = res.getIdentifier("share", "string", "android"); // only way to get android.com.internal.R.string.websearch (android.R.string.websearch)
            String shareString = "";
            try {
                shareString = res.getString(shareId);
            }catch(NotFoundException e){}
            int autoFillId = res.getIdentifier("autofill", "string", "android"); // only way to get android.com.internal.R.string.websearch (android.R.string.websearch)
            String autoFillString = "";
            try {
                autoFillString = res.getString(autoFillId);
            }catch(NotFoundException e){}

            if(item.getTitle().equals("Web search") || item.getTitle().equals(webSearchString)) {
                contextWebSearchActionId = item.getItemId();
            }else if(item.getTitle().equals("Share") || item.getTitle().equals(shareString)) {
                contextShareActionId = item.getItemId();
            }else if(item.getTitle().equals("Autofill") || item.getTitle().equals(autoFillString)) {
                contextAutoFillActionId = item.getItemId();
            }
    }
}
MeatPopsicle
  • 535
  • 8
  • 21

0 Answers0