List of usage examples for android.webkit JsResult cancel
public final void cancel()
From source file:org.apache.cordova.CordovaChromeClient.java
/** * Tell the client to display a confirm dialog to the user. * * @param view//from w w w.j a va2s. c o m * @param url * @param message * @param result */ @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder dlg = new AlertDialog.Builder(this.cordova.getActivity()); dlg.setMessage(message); dlg.setTitle("Confirm"); dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); dlg.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }); dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { //DO NOTHING public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { result.cancel(); return false; } else return true; } }); dlg.show(); return true; }
From source file:org.apache.cordova.CordovaChromeClient.java
/** * Tell the client to display a javascript alert dialog. * * @param view/*from w w w . j a v a2 s . com*/ * @param url * @param message * @param result */ @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder dlg = new AlertDialog.Builder(this.cordova.getActivity()); dlg.setMessage(message); dlg.setTitle("Alert"); //Don't let alerts break the back button dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); dlg.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }); dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { //DO NOTHING public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { result.confirm(); return false; } else return true; } }); dlg.show(); return true; }
From source file:org.apache.cordova.AndroidChromeClient.java
/** * Tell the client to display a confirm dialog to the user. * * @param view//from www . j av a2 s. c o m * @param url * @param message * @param result */ @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder dlg = new AlertDialog.Builder(this.cordova.getActivity()); dlg.setMessage(message); dlg.setTitle("Confirm"); dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); dlg.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }); dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { //DO NOTHING public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { result.cancel(); return false; } else return true; } }); dlg.create(); dlg.show(); return true; }
From source file:org.apache.cordova.AndroidChromeClient.java
/** * Tell the client to display a javascript alert dialog. * * @param view//from w w w . j a va2s . co m * @param url * @param message * @param result */ @Override public boolean onJsAlert(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder dlg = new AlertDialog.Builder(this.cordova.getActivity()); dlg.setMessage(message); dlg.setTitle("Alert"); //Don't let alerts break the back button dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); dlg.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }); dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { //DO NOTHING public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { result.confirm(); return false; } else return true; } }); dlg.create(); dlg.show(); return true; }
From source file:com.wpi.assistments.HomeTabbedActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_webview); final ProgressDialog pd = ProgressDialog.show(this, "", "Loading...", true); isTeacher = false;// ww w .j ava2 s . com toggle = true; logoutFlag = false; homeWebView = (WebView) findViewById(R.id.homeWebView); homeWebView.setWebViewClient(new WebViewClient() { public void onPageFinished(WebView view, String url) { Log.i("test", view.getUrl()); if (view.getUrl().equals("https://www.assistments.org/account/login") && !logoutFlag) { Intent intent = new Intent(getApplicationContext(), LoginActivity.class); intent.putExtra("showPopup", "true"); startActivity(intent); finish(); } else if (view.getUrl().equals("https://www.assistments.org/teacher")) { isTeacher = true; } else if (view.getUrl().equals("https://www.assistments.org/account/login") && logoutFlag) { Intent intent = new Intent(getApplicationContext(), LoginActivity.class); startActivity(intent); } if (!homeWebView.canGoBack()) { btnBack.setEnabled(false); } else { btnBack.setEnabled(true); } if (!homeWebView.canGoForward()) { btnForward.setEnabled(false); } else { btnForward.setEnabled(true); } if (pd.isShowing() && pd != null) { pd.dismiss(); } } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { pd.show(); view.loadUrl(url); return true; } }); homeWebView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { new AlertDialog.Builder(webviewContext).setTitle("ASSISTments").setMessage(message) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { logoutFlag = true; result.confirm(); } }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }).create().show(); return true; } }); if (savedInstanceState != null) { homeWebView.restoreState(savedInstanceState); } else { /** Scaling, replaced by overview mode and wide view port Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); if (size.x <= 780) { int scaleRate = (int) ((size.x) / 7.8); Log.i("test", Integer.toString(size.x)); homeWebView.setInitialScale(scaleRate); } */ Intent intent = getIntent(); if (intent.getStringExtra("username").equals("") && intent.getStringExtra("password").equals("")) { homeWebView.loadUrl("https://www.assistments.org/signup"); } else { String username = intent.getStringExtra("username"); String password = intent.getStringExtra("password"); String postData = "login=" + username + "&password=" + password + "&commit=Log in"; homeWebView.postUrl("https://www.assistments.org/account/login", EncodingUtils.getBytes(postData, "BASE64")); } } WebSettings settings = homeWebView.getSettings(); settings.setSaveFormData(false); settings.setSavePassword(false); settings.setJavaScriptEnabled(true); settings.setBuiltInZoomControls(true); settings.setDisplayZoomControls(false); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setSupportMultipleWindows(true); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setAppCacheEnabled(false); settings.setDomStorageEnabled(true); settings.setDatabaseEnabled(true); settings.setUseWideViewPort(false); settings.setLoadWithOverviewMode(true); homeWebView.clearCache(true); homeWebView.setPadding(0, 0, 0, 0); homeWebView.setInitialScale(getScale()); btnBack = (Button) findViewById(R.id.btnBack); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { homeWebView.goBack(); } }); btnRefresh = (Button) findViewById(R.id.btnRefresh); btnRefresh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //homeWebView.zoomOut(); homeWebView.scrollTo(1000, 0); Log.i("test", "111"); //homeWebView.reload(); } }); btnForward = (Button) findViewById(R.id.btnForward); btnForward.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { homeWebView.goForward(); } }); btnOffline = (Button) findViewById(R.id.btnOffline); btnOffline.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (toggle) { homeWebView.loadUrl( "https://www.assistments.org/assistments/student/index.html#offlineUserAssignmentList/"); btnOffline.setText("Home"); toggle = false; } else { if (isTeacher) { homeWebView.loadUrl("https://www.assistments.org/teacher"); } else { homeWebView.loadUrl("https://www.assistments.org/tutor"); } btnOffline.setText("Offline"); toggle = true; } } }); }
From source file:org.skt.runtime.RuntimeChromeClient.java
/** * Tell the client to display a confirm dialog to the user. * // w w w . j av a2 s .c om * @param view * @param url * @param message * @param result */ @Override public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { AlertDialog.Builder dlg = new AlertDialog.Builder(this.ctx); dlg.setMessage(message); dlg.setTitle("Confirm"); dlg.setCancelable(true); dlg.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.confirm(); } }); dlg.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { result.cancel(); } }); dlg.setOnCancelListener(new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { result.cancel(); } }); dlg.setOnKeyListener(new DialogInterface.OnKeyListener() { //DO NOTHING public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { result.cancel(); return false; } else return true; } }); dlg.create(); dlg.show(); return true; }
From source file:aarddict.android.ArticleViewActivity.java
@Override void initUI() {/* ww w .j ava2s.c om*/ this.scrollPositionsH = Collections.synchronizedMap(new HashMap<Article, ScrollXY>()); this.scrollPositionsV = Collections.synchronizedMap(new HashMap<Article, ScrollXY>()); loadAssets(); if (DeviceInfo.EINK_SCREEN) { useAnimation = false; setContentView(R.layout.eink_article_view); articleView = (ArticleView) findViewById(R.id.EinkArticleView); N2EpdController.n2MainActivity = this; EinkScreen.ResetController(2, articleView); // force full screen // refresh when changing // articles } // Setup animations only on non-eink screens else { useAnimation = true; fadeOutAnimation = new AlphaAnimation(1f, 0f); fadeOutAnimation.setDuration(600); fadeOutAnimation.setAnimationListener(new AnimationAdapter() { public void onAnimationEnd(Animation animation) { Button nextButton = (Button) findViewById(R.id.NextButton); nextButton.setVisibility(Button.GONE); } }); getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.article_view); articleView = (ArticleView) findViewById(R.id.ArticleView); } timer = new Timer(); backItems = Collections.synchronizedList(new LinkedList<HistoryItem>()); if (android.os.Build.VERSION.SDK_INT >= 11) { try { showFindDialogMethod = articleView.getClass().getMethod("showFindDialog", String.class, boolean.class); } catch (NoSuchMethodException e1) { Log.d(TAG, "showFindDialog method not found"); } } articleView.setOnScrollListener(new ArticleView.ScrollListener() { public void onScroll(int l, int t, int oldl, int oldt) { saveScrollPos(l, t); } }); articleView.getSettings().setJavaScriptEnabled(true); articleView.addJavascriptInterface(new SectionMatcher(), "matcher"); articleView.addJavascriptInterface(articleView, "scrollControl"); articleView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d(TAG + ".js", String.format("[%s]: %s", url, message)); result.cancel(); return true; } public void onProgressChanged(WebView view, int newProgress) { Log.d(TAG, "Progress: " + newProgress); setProgress(5000 + newProgress * 50); } }); articleView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { Log.d(TAG, "Page finished: " + url); currentTask = null; String section = null; if (url.contains("#")) { LookupWord lookupWord = LookupWord.splitWord(url); section = lookupWord.section; if (backItems.size() > 0) { HistoryItem currentHistoryItem = backItems.get(backItems.size() - 1); HistoryItem h = new HistoryItem(currentHistoryItem); h.article.section = section; backItems.add(h); } } else if (backItems.size() > 0) { Article current = backItems.get(backItems.size() - 1).article; section = current.section; } if (!restoreScrollPos()) { goToSection(section); } updateNextButtonVisibility(); } @Override public boolean shouldOverrideUrlLoading(WebView view, final String url) { Log.d(TAG, "URL clicked: " + url); String urlLower = url.toLowerCase(); if (urlLower.startsWith("http://") || urlLower.startsWith("https://") || urlLower.startsWith("ftp://") || urlLower.startsWith("sftp://") || urlLower.startsWith("mailto:")) { Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(browserIntent); } else { if (currentTask == null) { currentTask = new TimerTask() { public void run() { try { Article currentArticle = backItems.get(backItems.size() - 1).article; try { Iterator<Entry> currentIterator = dictionaryService.followLink(url, currentArticle.volumeId); List<Entry> result = new ArrayList<Entry>(); while (currentIterator.hasNext() && result.size() < 20) { result.add(currentIterator.next()); } showNext(new HistoryItem(result)); } catch (ArticleNotFound e) { showMessage(getString(R.string.msgArticleNotFound, e.word.toString())); } } catch (Exception e) { StringBuilder msgBuilder = new StringBuilder( "There was an error following link ").append("\"").append(url) .append("\""); if (e.getMessage() != null) { msgBuilder.append(": ").append(e.getMessage()); } final String msg = msgBuilder.toString(); Log.e(TAG, msg, e); showError(msg); } } }; try { timer.schedule(currentTask, 0); } catch (Exception e) { Log.d(TAG, "Failed to schedule task", e); } } } return true; } }); final Button nextButton = (Button) findViewById(R.id.NextButton); nextButton.getBackground().setAlpha(180); nextButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { if (nextButton.getVisibility() == View.VISIBLE) { updateNextButtonVisibility(); nextArticle(); updateNextButtonVisibility(); } } }); articleView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { updateNextButtonVisibility(); return false; } }); setProgressBarVisibility(true); }
From source file:com.pursuer.reader.easyrss.VerticalSingleItemView.java
@SuppressLint({ "NewApi", "SimpleDateFormat" }) public VerticalSingleItemView(final DataMgr dataMgr, final Context context, final String uid, final View menu, final VerticalItemViewCtrl itemViewCtrl) { this.dataMgr = dataMgr; this.context = context; this.item = dataMgr.getItemByUid(uid, ITEM_PROJECTION); this.fontSize = new SettingFontSize(dataMgr).getData(); this.theme = new SettingTheme(dataMgr).getData(); this.showTop = false; this.showBottom = false; this.menu = menu; this.itemViewCtrl = itemViewCtrl; this.imageClickTime = 0; final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.view = inflater.inflate(R.layout.single_item_view, null); // Disable hardware acceleration on Android 3.0-4.1 devices. if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR1 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) { view.setLayerType(View.LAYER_TYPE_SOFTWARE, null); }//from w w w.j a v a 2 s . com lastItemView = view.findViewById(R.id.LastItemView); nextItemView = view.findViewById(R.id.NextItemView); itemScrollView = (OverScrollView) view.findViewById(R.id.ItemScrollView); itemScrollView.setTopScrollView(lastItemView); itemScrollView.setBottomScrollView(nextItemView); itemScrollView.setOnScrollChangeListener(this); itemScrollView.setOnTouchListener(this); final View btnShowOriginal = view.findViewById(R.id.BtnShowOriginal); btnShowOriginal.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(final View view, final MotionEvent event) { boolean ret = false; final TextView txt = (TextView) view.findViewById(R.id.BtnText); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: txt.setTextColor(0xFF787878); ret = true; break; case MotionEvent.ACTION_MOVE: ret = true; break; case MotionEvent.ACTION_CANCEL: txt.setTextColor(0xBB787878); ret = false; break; case MotionEvent.ACTION_UP: txt.setTextColor(0xBB787878); final SettingBrowserChoice setting = new SettingBrowserChoice(dataMgr); if (setting.getData() == SettingBrowserChoice.BROWSER_CHOICE_EXTERNAL) { final Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse(item.getHref())); context.startActivity(intent); } else if (VerticalSingleItemView.this.listener != null) { VerticalSingleItemView.this.listener.showWebsitePage(item.getUid(), false); } break; default: } return ret; } }); final View btnShowMobilized = view.findViewById(R.id.BtnShowMobilized); btnShowMobilized.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(final View view, final MotionEvent event) { boolean ret = false; final TextView txt = (TextView) view.findViewById(R.id.BtnText); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: txt.setTextColor(0xFF787878); ret = true; break; case MotionEvent.ACTION_MOVE: ret = true; break; case MotionEvent.ACTION_CANCEL: txt.setTextColor(0xBB787878); ret = false; break; case MotionEvent.ACTION_UP: txt.setTextColor(0xBB787878); if (VerticalSingleItemView.this.listener != null) { VerticalSingleItemView.this.listener.showWebsitePage(item.getUid(), true); } ret = true; break; default: } return ret; } }); { final ListItemItem lastItem = itemViewCtrl.getLastItem(item.getUid()); final TextView txt = (TextView) lastItemView.findViewById(R.id.LastItemTitle); txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize); if (lastItem == null) { lastUid = null; menu.findViewById(R.id.BtnPrevious).setEnabled(false); final ImageView img = (ImageView) lastItemView.findViewById(R.id.LastItemArrow); img.setImageResource(R.drawable.no_more_circle); txt.setText(R.string.TxtNoPreviousItem); } else { lastUid = lastItem.getId(); final View btnLast = menu.findViewById(R.id.BtnPrevious); btnLast.setEnabled(true); btnLast.setOnClickListener(new OnClickListener() { @Override public void onClick(final View view) { if (listener != null) { listener.showLastItem(); } } }); txt.setText(lastItem.getTitle()); } } { final ListItemItem nextItem = itemViewCtrl.getNextItem(item.getUid()); final TextView txt = (TextView) nextItemView.findViewById(R.id.NextItemTitle); txt.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize); if (nextItem == null) { nextUid = null; menu.findViewById(R.id.BtnNext).setEnabled(false); final ImageView img = (ImageView) nextItemView.findViewById(R.id.NextItemArrow); img.setImageResource(R.drawable.no_more_circle); txt.setText(R.string.TxtNoNextItem); } else { nextUid = nextItem.getId(); final View btnNext = menu.findViewById(R.id.BtnNext); btnNext.setEnabled(true); btnNext.setOnClickListener(new OnClickListener() { @Override public void onClick(final View view) { if (listener != null) { listener.showNextItem(); } } }); txt.setText(nextItem.getTitle()); } } final TextView title = (TextView) view.findViewById(R.id.ItemTitle); title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize * 4 / 3); title.setText(item.getTitle()); final TextView info = (TextView) view.findViewById(R.id.ItemInfo); info.setTextSize(TypedValue.COMPLEX_UNIT_DIP, fontSize * 4 / 5); final StringBuilder infoText = new StringBuilder(); final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); sdf.setTimeZone(TimeZone.getDefault()); infoText.append(sdf.format(Utils.timestampToDate(item.getTimestamp()))); if (item.getAuthor() != null && item.getAuthor().length() > 0) { infoText.append(" | By "); infoText.append(item.getAuthor()); } if (item.getSourceTitle() != null && item.getSourceTitle().length() > 0) { infoText.append(" ("); infoText.append(item.getSourceTitle()); infoText.append(")"); } info.setText(infoText); webView = (WebView) view.findViewById(R.id.webView); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setBackgroundColor(context.getResources() .getColor(theme == SettingTheme.THEME_NORMAL ? R.color.NormalBackground : R.color.DarkBackground)); webView.setFocusable(false); webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) { if (VerticalSingleItemView.this.imageClickTime > System.currentTimeMillis() - 1000) { return true; } final Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setData(Uri.parse(url)); context.startActivity(intent); return true; } }); webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(final WebView view, final String url, final String message, final JsResult result) { if (VerticalSingleItemView.this.listener != null) { if (message.endsWith(".erss")) { VerticalSingleItemView.this.listener .onImageViewRequired(item.getStoragePath() + "/" + message); } else { VerticalSingleItemView.this.listener.onImageViewRequired(message); } } VerticalSingleItemView.this.imageClickTime = System.currentTimeMillis(); result.cancel(); return true; } }); updateButtonStar(); updateButtonSharing(); updateButtonOpenLink(); if (!item.getState().isRead()) { dataMgr.markItemAsReadWithTransactionByUid(uid); NetworkMgr.getInstance().startImmediateItemStateSyncing(); } }
From source file:net.wequick.small.webkit.WebView.java
private void initSettings() { WebSettings webSettings = this.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setUserAgentString(webSettings.getUserAgentString() + " Native"); this.addJavascriptInterface(new SmallJsBridge(), "_Small"); this.setWebChromeClient(new WebChromeClient() { @Override// w w w . jav a 2 s. c o m public void onReceivedTitle(android.webkit.WebView view, String title) { // Call if html title is set super.onReceivedTitle(view, title); mTitle = title; WebActivity activity = (WebActivity) WebViewPool.getContext(view); if (activity != null) { activity.setTitle(title); } // May receive head meta at the same time initMetas(); } @Override public boolean onJsAlert(android.webkit.WebView view, String url, String message, final android.webkit.JsResult result) { Context context = WebViewPool.getContext(view); if (context == null) return false; AlertDialog.Builder dlg = new AlertDialog.Builder(context); dlg.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mConfirmed = true; result.confirm(); } }); dlg.setMessage(message); AlertDialog alert = dlg.create(); alert.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (!mConfirmed) { result.cancel(); } } }); mConfirmed = false; alert.show(); return true; } @Override public boolean onJsConfirm(android.webkit.WebView view, String url, String message, final android.webkit.JsResult result) { Context context = WebViewPool.getContext(view); AlertDialog.Builder dlg = new AlertDialog.Builder(context); dlg.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mConfirmed = true; result.confirm(); } }); dlg.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { mConfirmed = true; result.cancel(); } }); dlg.setMessage(message); AlertDialog alert = dlg.create(); alert.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (!mConfirmed) { result.cancel(); } } }); mConfirmed = false; alert.show(); return true; } @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { String msg = consoleMessage.message(); if (msg == null) return false; Uri uri = Uri.parse(msg); if (uri != null && null != uri.getScheme() && uri.getScheme().equals(SMALL_SCHEME)) { String host = uri.getHost(); String ret = uri.getQueryParameter(SMALL_QUERY_KEY_RET); if (host.equals(SMALL_HOST_POP)) { WebActivity activity = (WebActivity) WebViewPool.getContext(WebView.this); activity.finish(ret); } else if (host.equals(SMALL_HOST_EXEC)) { if (mOnResultListener != null) { mOnResultListener.onResult(ret); } } return true; } Log.d(consoleMessage.sourceId(), "line" + consoleMessage.lineNumber() + ": " + consoleMessage.message()); return true; } @Override public void onCloseWindow(android.webkit.WebView window) { super.onCloseWindow(window); close(new OnResultListener() { @Override public void onResult(String ret) { if (ret.equals("false")) return; WebActivity activity = (WebActivity) WebViewPool.getContext(WebView.this); activity.finish(ret); } }); } }); this.setWebViewClient(new android.webkit.WebViewClient() { private final String ANCHOR_SCHEME = "anchor"; @Override public boolean shouldOverrideUrlLoading(android.webkit.WebView view, String url) { if (mLoadingUrl != null && mLoadingUrl.equals(url)) { // reload by window.location.reload or something return super.shouldOverrideUrlLoading(view, url); } Boolean hasStarted = mHasStartedUrl.get(url); if (hasStarted != null && hasStarted) { // location redirected before page finished return super.shouldOverrideUrlLoading(view, url); } HitTestResult hit = view.getHitTestResult(); if (hit != null) { Uri uri = Uri.parse(url); if (uri.getScheme().equals(ANCHOR_SCHEME)) { // Scroll to anchor int anchorY = Integer.parseInt(uri.getHost()); view.scrollTo(0, anchorY); } else { Small.openUri(uri, WebViewPool.getContext(view)); } return true; } return super.shouldOverrideUrlLoading(view, url); } @Override public void onPageStarted(android.webkit.WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); mHasStartedUrl.put(url, true); if (mLoadingUrl != null && mLoadingUrl.equals(url)) { // reload by window.location.reload or something mInjected = false; } if (sWebViewClient != null && url.equals(mLoadingUrl)) { sWebViewClient.onPageStarted(WebViewPool.getContext(view), (WebView) view, url, favicon); } } @Override public void onPageFinished(android.webkit.WebView view, String url) { super.onPageFinished(view, url); mHasStartedUrl.remove(url); HitTestResult hit = view.getHitTestResult(); if (hit != null && hit.getType() == HitTestResult.SRC_ANCHOR_TYPE) { // Triggered by user clicked Uri uri = Uri.parse(url); String anchor = uri.getFragment(); if (anchor != null) { // If is an anchor, calculate the content offset by DOM // and call native to adjust WebView's offset view.loadUrl(JS_PREFIX + "var y=document.body.scrollTop;" + "var e=document.getElementsByName('" + anchor + "')[0];" + "while(e){" + "y+=e.offsetTop-e.scrollTop+e.clientTop;e=e.offsetParent;}" + "location='" + ANCHOR_SCHEME + "://'+y;"); } } if (!mInjected) { // Re-inject Small Js loadJs(SMALL_INJECT_JS); initMetas(); mInjected = true; } if (sWebViewClient != null && url.equals(mLoadingUrl)) { sWebViewClient.onPageFinished(WebViewPool.getContext(view), (WebView) view, url); } } @Override public void onReceivedError(android.webkit.WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); Log.e("Web", "error: " + description); if (sWebViewClient != null && failingUrl.equals(mLoadingUrl)) { sWebViewClient.onReceivedError(WebViewPool.getContext(view), (WebView) view, errorCode, description, failingUrl); } } }); }
From source file:com.google.android.apps.paco.FeedbackActivity.java
private void setWebChromeClientThatHandlesAlertsAsDialogs() { webView.setWebChromeClient(new WebChromeClient() { @Override//from w w w . jav a 2 s . com public boolean onJsAlert(WebView view, String url, String message, JsResult result) { new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true) .setPositiveButton(R.string.ok, new Dialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }).create().show(); result.confirm(); return true; } public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) { if (url.contains("file:///android_asset/map.html")) { if (showDialog == false) { result.confirm(); return true; } else { new AlertDialog.Builder(view.getContext()).setMessage(message).setCancelable(true) .setPositiveButton(R.string.ok, new Dialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { showDialog = false; dialog.dismiss(); result.confirm(); } }).setNegativeButton(R.string.cancel_button, new Dialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); result.cancel(); } }).create().show(); return true; } } return super.onJsConfirm(view, url, message, result); } @Override public void onConsoleMessage(String message, int lineNumber, String sourceID) { Log.d(PacoConstants.TAG, message + " -- From line " + lineNumber + " of " + sourceID); } @Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) { Log.d(PacoConstants.TAG, "*" + consoleMessage.message() + " -- From line " + consoleMessage.lineNumber() + " of " + consoleMessage.sourceId()); return true; } }); }