|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-present, Parse, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +package com.parse.twitter; |
| 10 | + |
| 11 | +import android.annotation.SuppressLint; |
| 12 | +import android.content.Context; |
| 13 | +import android.content.DialogInterface; |
| 14 | +import android.content.Intent; |
| 15 | +import android.graphics.Bitmap; |
| 16 | +import android.net.Uri; |
| 17 | +import android.os.Bundle; |
| 18 | +import android.support.v7.app.AppCompatDialog; |
| 19 | +import android.view.View; |
| 20 | +import android.webkit.WebView; |
| 21 | +import android.webkit.WebViewClient; |
| 22 | +import android.widget.ProgressBar; |
| 23 | + |
| 24 | +/** |
| 25 | + * For internal use. |
| 26 | + */ |
| 27 | +class OAuth1FlowDialog extends AppCompatDialog { |
| 28 | + |
| 29 | + private final String callbackUrl; |
| 30 | + private final String requestUrl; |
| 31 | + private final String serviceUrlIdentifier; |
| 32 | + private final FlowResultHandler handler; |
| 33 | + |
| 34 | + private WebView webView; |
| 35 | + private ProgressBar progress; |
| 36 | + |
| 37 | + OAuth1FlowDialog(Context context, String requestUrl, String callbackUrl, String serviceUrlIdentifier, FlowResultHandler resultHandler) { |
| 38 | + super(context); |
| 39 | + this.requestUrl = requestUrl; |
| 40 | + this.callbackUrl = callbackUrl; |
| 41 | + this.serviceUrlIdentifier = serviceUrlIdentifier; |
| 42 | + this.handler = resultHandler; |
| 43 | + this.setOnCancelListener(new OnCancelListener() { |
| 44 | + @Override |
| 45 | + public void onCancel(DialogInterface dialog) { |
| 46 | + handler.onCancel(); |
| 47 | + } |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + @SuppressLint("SetJavaScriptEnabled") |
| 52 | + @Override |
| 53 | + protected void onCreate(Bundle savedInstanceState) { |
| 54 | + super.onCreate(savedInstanceState); |
| 55 | + setContentView(R.layout.parse_twitter_dialog_login); |
| 56 | + webView = findViewById(R.id.webView); |
| 57 | + progress = findViewById(R.id.progress); |
| 58 | + |
| 59 | + webView.setVerticalScrollBarEnabled(false); |
| 60 | + webView.setHorizontalScrollBarEnabled(false); |
| 61 | + webView.setWebViewClient(new OAuth1WebViewClient()); |
| 62 | + webView.getSettings().setJavaScriptEnabled(true); |
| 63 | + webView.setVisibility(View.INVISIBLE); |
| 64 | + webView.loadUrl(requestUrl); |
| 65 | + } |
| 66 | + |
| 67 | + public interface FlowResultHandler { |
| 68 | + /** |
| 69 | + * Called when the user cancels the dialog. |
| 70 | + */ |
| 71 | + void onCancel(); |
| 72 | + |
| 73 | + /** |
| 74 | + * Called when the dialog's web view receives an error. |
| 75 | + */ |
| 76 | + void onError(int errorCode, String description, String failingUrl); |
| 77 | + |
| 78 | + /** |
| 79 | + * Called when the dialog portion of the flow is complete. |
| 80 | + * |
| 81 | + * @param callbackUrl The final URL called back (including any query string appended |
| 82 | + * by the server). |
| 83 | + */ |
| 84 | + void onComplete(String callbackUrl); |
| 85 | + } |
| 86 | + |
| 87 | + private class OAuth1WebViewClient extends WebViewClient { |
| 88 | + @Override |
| 89 | + public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 90 | + if (url.startsWith(callbackUrl)) { |
| 91 | + OAuth1FlowDialog.this.dismiss(); |
| 92 | + handler.onComplete(url); |
| 93 | + return true; |
| 94 | + } else if (url.contains(serviceUrlIdentifier)) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + // launch non-service URLs in a full browser |
| 98 | + getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { |
| 104 | + super.onReceivedError(view, errorCode, description, failingUrl); |
| 105 | + OAuth1FlowDialog.this.dismiss(); |
| 106 | + handler.onError(errorCode, description, failingUrl); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 111 | + super.onPageStarted(view, url, favicon); |
| 112 | + progress.setVisibility(View.VISIBLE); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void onPageFinished(WebView view, String url) { |
| 117 | + super.onPageFinished(view, url); |
| 118 | + progress.setVisibility(View.GONE); |
| 119 | + webView.setVisibility(View.VISIBLE); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments