-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathOAuthManagerDialogFragment.java
341 lines (279 loc) · 11.3 KB
/
OAuthManagerDialogFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package io.fullstack.oauth;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.facebook.react.bridge.ReactContext;
import com.github.scribejava.core.model.OAuth1AccessToken;
import java.lang.reflect.Method;
import java.util.Set;
import im.delight.android.webview.AdvancedWebView;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class OAuthManagerDialogFragment extends DialogFragment implements AdvancedWebView.Listener {
private static final int WEBVIEW_TAG = 100001;
private static final int WIDGET_TAG = 100002;
private static final String TAG = "OauthFragment";
private OAuthManagerFragmentController mController;
private ReactContext mReactContext;
private AdvancedWebView mWebView;
private ProgressBar mProgressBar;
public static final OAuthManagerDialogFragment newInstance(
final ReactContext reactContext,
OAuthManagerFragmentController controller
) {
Bundle args = new Bundle();
OAuthManagerDialogFragment frag =
new OAuthManagerDialogFragment(reactContext, controller);
return frag;
}
public OAuthManagerDialogFragment(
final ReactContext reactContext,
OAuthManagerFragmentController controller
) {
this.mController = controller;
this.mReactContext = reactContext;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
if (dialog != null) {
dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final Context context = mReactContext;
LayoutParams rootViewLayoutParams = this.getFullscreenLayoutParams(context);
RelativeLayout rootView = new RelativeLayout(context);
mProgressBar = new ProgressBar(context);
RelativeLayout.LayoutParams progressParams = new RelativeLayout.LayoutParams(convertDpToPixel(50f,context),convertDpToPixel(50f,context));
progressParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mProgressBar.setLayoutParams(progressParams);
mProgressBar.setIndeterminate(true);
getDialog().setCanceledOnTouchOutside(true);
rootView.setLayoutParams(rootViewLayoutParams);
// mWebView = (AdvancedWebView) rootView.findViewById(R.id.webview);
Log.d(TAG, "Creating webview");
mWebView = new AdvancedWebView(context);
// mWebView.setId(WEBVIEW_TAG);
mWebView.setListener(this, this);
mWebView.setVisibility(View.VISIBLE);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setUserAgentString("Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
LayoutParams layoutParams = this.getFullscreenLayoutParams(context);
//new LayoutParams(
// LayoutParams.FILL_PARENT,
// DIALOG_HEIGHT
// );
// mWebView.setLayoutParams(layoutParams);
rootView.addView(mWebView, layoutParams);
rootView.addView(mProgressBar,progressParams);
// LinearLayout pframe = new LinearLayout(context);
// pframe.setId(WIDGET_TAG);
// pframe.setOrientation(LinearLayout.VERTICAL);
// pframe.setVisibility(View.GONE);
// pframe.setGravity(Gravity.CENTER);
// pframe.setLayoutParams(layoutParams);
// rootView.addView(pframe, layoutParams);
this.setupWebView(mWebView);
mController.getRequestTokenUrlAndLoad(mWebView);
Log.d(TAG, "Loading view...");
return rootView;
}
private LayoutParams getFullscreenLayoutParams(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
// DisplayMetrics metrics = context.getResources().getDisplayMetrics();
Display display = wm.getDefaultDisplay();
int realWidth;
int realHeight;
if (Build.VERSION.SDK_INT >= 17){
//new pleasant way to get real metrics
DisplayMetrics realMetrics = new DisplayMetrics();
display.getRealMetrics(realMetrics);
realWidth = realMetrics.widthPixels;
realHeight = realMetrics.heightPixels;
} else if (Build.VERSION.SDK_INT >= 14) {
//reflection for this weird in-between time
try {
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
realWidth = (Integer) mGetRawW.invoke(display);
realHeight = (Integer) mGetRawH.invoke(display);
} catch (Exception e) {
//this may not be 100% accurate, but it's all we've got
realWidth = display.getWidth();
realHeight = display.getHeight();
Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
}
} else {
//This should be close, as lower API devices should not have window navigation bars
realWidth = display.getWidth();
realHeight = display.getHeight();
}
return new LayoutParams(realWidth, realHeight-convertDpToPixel(50f,context));
}
private void setupWebView(AdvancedWebView webView) {
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return interceptUrl(view, url, true);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view, int code, String desc, String failingUrl) {
Log.i(TAG, "onReceivedError: " + failingUrl);
super.onReceivedError(view, code, desc, failingUrl);
onError(desc);
}
private boolean interceptUrl(WebView view, String url, boolean loadUrl) {
Log.i(TAG, "interceptUrl called with url: " + url);
// url would be http://localhost/twitter?denied=xxx when it's canceled
Pattern p = Pattern.compile("\\S*denied\\S*");
Matcher m = p.matcher(url);
if(m.matches()){
Log.i(TAG, "authentication is canceled");
return false;
}
if (isCallbackUri(url, mController.getCallbackUrl())) {
mController.getAccessToken(mWebView, url);
return true;
}
if (loadUrl) {
view.loadUrl(url);
}
return false;
}
});
}
public void setComplete(final OAuth1AccessToken accessToken) {
Log.d(TAG, "Completed: " + accessToken);
}
// @Override
// public void onDismiss(final DialogInterface dialog) {
// super.onDismiss(dialog);
// Log.d(TAG, "Dismissing dialog");
// }
// @Override
// void onCancel(DialogInterface dialog) {
// Log.d(TAG, "onCancel called for dialog");
// onError("Cancelled");
// }
@SuppressLint("NewApi")
@Override
public void onResume() {
super.onResume();
mWebView.onResume();
Log.d(TAG, "onResume called");
}
@SuppressLint("NewApi")
@Override
public void onPause() {
Log.d(TAG, "onPause called");
mWebView.onPause();
super.onPause();
}
@Override
public void onDestroy() {
mWebView.onDestroy();
this.mController = null;
// ...
super.onDestroy();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mWebView.onActivityResult(requestCode, resultCode, intent);
Log.d(TAG, "onActivityResult: " + requestCode);
// ...
}
@Override
public void onPageStarted(String url, Bitmap favicon) {
Log.d(TAG, "onPageStarted " + url);
}
@Override
public void onPageFinished(String url) {
Log.d(TAG, "onPageFinished: " + url);
// mController.onComplete(url);
}
@Override
public void onPageError(int errorCode, String description, String failingUrl) {
Log.e(TAG, "onPageError: " + failingUrl);
mController.onError(errorCode, description, failingUrl);
}
@Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) { }
@Override
public void onExternalPageRequest(String url) {
Log.d(TAG, "onExternalPageRequest: " + url);
}
private void onError(String msg) {
Log.e(TAG, "Error: " + msg);
}
static boolean isCallbackUri(String uri, String callbackUrl) {
Uri u = null;
Uri r = null;
try {
u = Uri.parse(uri);
r = Uri.parse(callbackUrl);
} catch (NullPointerException e) {
return false;
}
if (u == null || r == null) return false;
boolean rOpaque = r.isOpaque();
boolean uOpaque = u.isOpaque();
if (uOpaque != rOpaque) return false;
if (rOpaque) return TextUtils.equals(uri, callbackUrl);
if (!TextUtils.equals(r.getScheme(), u.getScheme())) return false;
if (u.getPort() != r.getPort()) return false;
if (!TextUtils.isEmpty(r.getPath()) && !TextUtils.equals(r.getPath(), u.getPath())) return false;
Set<String> paramKeys = r.getQueryParameterNames();
for (String key : paramKeys) {
if (!TextUtils.equals(r.getQueryParameter(key), u.getQueryParameter(key))) return false;
}
String frag = r.getFragment();
if (!TextUtils.isEmpty(frag) && !TextUtils.equals(frag, u.getFragment())) return false;
return true;
}
public static int convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return (int)px;
}
}