Skip to content
This repository was archived by the owner on Feb 14, 2020. It is now read-only.

Commit 1014e09

Browse files
committed
Inflate dialog with layout resource so that theming is correct
1 parent d65e6c0 commit 1014e09

15 files changed

+1122
-2436
lines changed

library/src/main/java/com/parse/twitter/AsyncCallback.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
package com.parse.twitter;
1010

1111
public interface AsyncCallback {
12-
void onSuccess(Object result);
12+
void onSuccess(Object result);
1313

14-
void onCancel();
14+
void onCancel();
1515

16-
void onFailure(Throwable error);
16+
void onFailure(Throwable error);
1717
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/**
12+
* OAuth Flow exception
13+
*/
14+
public class OAuth1FlowException extends Exception {
15+
private static final long serialVersionUID = 4272662026279290823L;
16+
private final int errorCode;
17+
private final String description;
18+
private final String failingUrl;
19+
20+
public OAuth1FlowException(int errorCode, String description, String failingUrl) {
21+
super(String.format("OAuth Flow Error %d: Url: %s Description: %s", errorCode, failingUrl,
22+
description));
23+
this.errorCode = errorCode;
24+
this.description = description;
25+
this.failingUrl = failingUrl;
26+
}
27+
28+
public int getErrorCode() {
29+
return errorCode;
30+
}
31+
32+
public String getDescription() {
33+
return description;
34+
}
35+
36+
public String getFailingUrl() {
37+
return failingUrl;
38+
}
39+
}

0 commit comments

Comments
 (0)