读书人

Browser 长按以后的处理

发布时间: 2012-09-27 11:11:17 作者: rapoo

Browser 长按之后的处理

@Override    public void onCreateContextMenu(ContextMenu menu, View v,            ContextMenuInfo menuInfo) {        WebView webview = (WebView) v;        WebView.HitTestResult result = webview.getHitTestResult();        if (result == null) {            return;        }        int type = result.getType();        if (type == WebView.HitTestResult.UNKNOWN_TYPE) {            Log.w(LOGTAG,                    "We should not show context menu when nothing is touched");            return;        }        if (type == WebView.HitTestResult.EDIT_TEXT_TYPE) {            // let TextView handles context menu            return;        }        // Note, http://b/issue?id=1106666 is requesting that        // an inflated menu can be used again. This is not available        // yet, so inflate each time (yuk!)        MenuInflater inflater = getMenuInflater();        inflater.inflate(R.menu.browsercontext, menu);        // Show the correct menu group        String extra = result.getExtra();        menu.setGroupVisible(R.id.PHONE_MENU,                type == WebView.HitTestResult.PHONE_TYPE);        menu.setGroupVisible(R.id.EMAIL_MENU,                type == WebView.HitTestResult.EMAIL_TYPE);        menu.setGroupVisible(R.id.GEO_MENU,                type == WebView.HitTestResult.GEO_TYPE);        menu.setGroupVisible(R.id.IMAGE_MENU,                type == WebView.HitTestResult.IMAGE_TYPE                || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);        menu.setGroupVisible(R.id.ANCHOR_MENU,                type == WebView.HitTestResult.SRC_ANCHOR_TYPE                || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE);        // Setup custom handling depending on the type        switch (type) {            case WebView.HitTestResult.PHONE_TYPE:                menu.setHeaderTitle(Uri.decode(extra));                menu.findItem(R.id.dial_context_menu_id).setIntent(                        new Intent(Intent.ACTION_VIEW, Uri                                .parse(WebView.SCHEME_TEL + extra)));                Intent addIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);                addIntent.putExtra(Insert.PHONE, Uri.decode(extra));                addIntent.setType(Contacts.People.CONTENT_ITEM_TYPE);                menu.findItem(R.id.add_contact_context_menu_id).setIntent(                        addIntent);                menu.findItem(R.id.copy_phone_context_menu_id).setOnMenuItemClickListener(                        new Copy(extra));                break;            case WebView.HitTestResult.EMAIL_TYPE:                menu.setHeaderTitle(extra);                menu.findItem(R.id.email_context_menu_id).setIntent(                        new Intent(Intent.ACTION_VIEW, Uri                                .parse(WebView.SCHEME_MAILTO + extra)));                menu.findItem(R.id.copy_mail_context_menu_id).setOnMenuItemClickListener(                        new Copy(extra));                break;            case WebView.HitTestResult.GEO_TYPE:                menu.setHeaderTitle(extra);                menu.findItem(R.id.map_context_menu_id).setIntent(                        new Intent(Intent.ACTION_VIEW, Uri                                .parse(WebView.SCHEME_GEO                                        + URLEncoder.encode(extra))));                menu.findItem(R.id.copy_geo_context_menu_id).setOnMenuItemClickListener(                        new Copy(extra));                break;            case WebView.HitTestResult.SRC_ANCHOR_TYPE:            case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:                TextView titleView = (TextView) LayoutInflater.from(this)                        .inflate(android.R.layout.browser_link_context_header,                        null);                titleView.setText(extra);                menu.setHeaderView(titleView);                // decide whether to show the open link in new tab option                menu.findItem(R.id.open_newtab_context_menu_id).setVisible(                        mTabControl.getTabCount() < TabControl.MAX_TABS);                PackageManager pm = getPackageManager();                Intent send = new Intent(Intent.ACTION_SEND);                send.setType("text/plain");                ResolveInfo ri = pm.resolveActivity(send, PackageManager.MATCH_DEFAULT_ONLY);                menu.findItem(R.id.share_link_context_menu_id).setVisible(ri != null);                if (type == WebView.HitTestResult.SRC_ANCHOR_TYPE) {                    break;                }                // otherwise fall through to handle image part            case WebView.HitTestResult.IMAGE_TYPE:                if (type == WebView.HitTestResult.IMAGE_TYPE) {                    menu.setHeaderTitle(extra);                }                menu.findItem(R.id.view_image_context_menu_id).setIntent(                        new Intent(Intent.ACTION_VIEW, Uri.parse(extra)));                menu.findItem(R.id.download_context_menu_id).                        setOnMenuItemClickListener(new Download(extra));                break;            default:                Log.w(LOGTAG, "We should not get here.");                break;        }    }

读书人网 >移动开发

热点推荐