Skip to content

Commit 135bb8d

Browse files
committed
Added slack and fixed response
1 parent 8800ca0 commit 135bb8d

File tree

5 files changed

+329
-124
lines changed

5 files changed

+329
-124
lines changed

android/src/main/java/io/fullstack/oauth/OAuthManagerDialogFragment.java

Lines changed: 78 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.annotation.SuppressLint;
1212
import android.widget.LinearLayout;
1313
import android.view.Gravity;
14+
import android.os.Build;
1415

1516
import android.app.DialogFragment;
1617
import android.content.DialogInterface;
@@ -25,6 +26,9 @@
2526
import android.view.ViewGroup.LayoutParams;
2627
import android.content.Context;
2728
import android.util.DisplayMetrics;
29+
import android.view.Display;
30+
import java.lang.reflect.Method;
31+
import android.view.WindowManager;
2832

2933
import com.github.scribejava.core.model.OAuth1AccessToken;
3034
import com.github.scribejava.core.model.OAuth1RequestToken;
@@ -33,6 +37,7 @@
3337
import android.os.Bundle;
3438
import android.app.Fragment;
3539
import java.io.IOException;
40+
import com.facebook.react.bridge.ReactContext;
3641

3742
public class OAuthManagerDialogFragment extends DialogFragment implements AdvancedWebView.Listener {
3843

@@ -42,52 +47,72 @@ public class OAuthManagerDialogFragment extends DialogFragment implements Advanc
4247
private static final String TAG = "OAuthManagerDialogFragment";
4348
private OAuthManagerFragmentController mController;
4449

50+
private ReactContext mReactContext;
4551
private AdvancedWebView mWebView;
4652

4753
public static final OAuthManagerDialogFragment newInstance(
54+
final ReactContext reactContext,
4855
OAuthManagerFragmentController controller
4956
) {
5057
Bundle args = new Bundle();
5158
OAuthManagerDialogFragment frag =
52-
new OAuthManagerDialogFragment(controller);
59+
new OAuthManagerDialogFragment(reactContext, controller);
5360

5461
return frag;
5562
}
5663

5764
public OAuthManagerDialogFragment(
65+
final ReactContext reactContext,
5866
OAuthManagerFragmentController controller
5967
) {
6068
this.mController = controller;
69+
this.mReactContext = reactContext;
6170
}
6271

6372
@Override
6473
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
6574
// View rootView = inflater.inflate(R.id.primary, container, false);
66-
final Context context = inflater.getContext();
75+
// final Context context = inflater.getContext();
6776
// DisplayMetrics metrics = context.getResources().getDisplayMetrics();
68-
// final int DIALOG_HEIGHT = (int) Math.min(0.8f * metrics.heightPixels, 1024);
77+
// final int DIALOG_HEIGHT = (int) Math.min(0.99f * metrics.heightPixels, 1024);
78+
79+
// LayoutParams rootViewLayoutParams = new LayoutParams(
80+
// LayoutParams.FILL_PARENT,
81+
// LayoutParams.FILL_PARENT
82+
// );
83+
final Context context = mReactContext;
84+
LayoutParams rootViewLayoutParams = this.getFullscreenLayoutParams(context);
6985

7086
FrameLayout rootView = new FrameLayout(context);
7187
getDialog().setCanceledOnTouchOutside(true);
72-
rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
88+
rootView.setLayoutParams(rootViewLayoutParams);
7389

7490
// mWebView = (AdvancedWebView) rootView.findViewById(R.id.webview);
7591
Log.d(TAG, "Creating webview");
7692
mWebView = new AdvancedWebView(context);
7793
mWebView.setId(WEBVIEW_TAG);
7894
mWebView.setListener(this, this);
7995
mWebView.setVisibility(View.VISIBLE);
96+
mWebView.getSettings().setJavaScriptEnabled(true);
97+
mWebView.getSettings().setDomStorageEnabled(true);
98+
99+
LayoutParams layoutParams = this.getFullscreenLayoutParams(context);
100+
//new LayoutParams(
101+
// LayoutParams.FILL_PARENT,
102+
// DIALOG_HEIGHT
103+
// );
104+
// mWebView.setLayoutParams(layoutParams);
80105

81-
rootView.addView(mWebView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
106+
rootView.addView(mWebView, layoutParams);
82107

83-
LinearLayout pframe = new LinearLayout(context);
84-
pframe.setId(WIDGET_TAG);
85-
pframe.setOrientation(LinearLayout.VERTICAL);
86-
pframe.setVisibility(View.GONE);
87-
pframe.setGravity(Gravity.CENTER);
108+
// LinearLayout pframe = new LinearLayout(context);
109+
// pframe.setId(WIDGET_TAG);
110+
// pframe.setOrientation(LinearLayout.VERTICAL);
111+
// pframe.setVisibility(View.GONE);
112+
// pframe.setGravity(Gravity.CENTER);
113+
// pframe.setLayoutParams(layoutParams);
88114

89-
rootView.addView(pframe,
90-
new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
115+
// rootView.addView(pframe, layoutParams);
91116

92117
this.setupWebView(mWebView);
93118
mController.getRequestTokenUrlAndLoad(mWebView);
@@ -96,12 +121,49 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
96121
return rootView;
97122
}
98123

124+
private LayoutParams getFullscreenLayoutParams(Context context) {
125+
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
126+
// DisplayMetrics metrics = context.getResources().getDisplayMetrics();
127+
Display display = wm.getDefaultDisplay();
128+
int realWidth;
129+
int realHeight;
130+
131+
if (Build.VERSION.SDK_INT >= 17){
132+
//new pleasant way to get real metrics
133+
DisplayMetrics realMetrics = new DisplayMetrics();
134+
display.getRealMetrics(realMetrics);
135+
realWidth = realMetrics.widthPixels;
136+
realHeight = realMetrics.heightPixels;
137+
138+
} else if (Build.VERSION.SDK_INT >= 14) {
139+
//reflection for this weird in-between time
140+
try {
141+
Method mGetRawH = Display.class.getMethod("getRawHeight");
142+
Method mGetRawW = Display.class.getMethod("getRawWidth");
143+
realWidth = (Integer) mGetRawW.invoke(display);
144+
realHeight = (Integer) mGetRawH.invoke(display);
145+
} catch (Exception e) {
146+
//this may not be 100% accurate, but it's all we've got
147+
realWidth = display.getWidth();
148+
realHeight = display.getHeight();
149+
Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
150+
}
151+
152+
} else {
153+
//This should be close, as lower API devices should not have window navigation bars
154+
realWidth = display.getWidth();
155+
realHeight = display.getHeight();
156+
}
157+
158+
return new LayoutParams(realWidth, realHeight);
159+
}
160+
161+
99162
private void setupWebView(AdvancedWebView webView) {
100163
webView.setWebViewClient(new WebViewClient() {
101164
@Override
102165
public boolean shouldOverrideUrlLoading(WebView view, String url) {
103-
interceptUrl(view, url, true);
104-
return true;
166+
return interceptUrl(view, url, true);
105167
}
106168

107169
@Override
@@ -112,6 +174,7 @@ public void onReceivedError(WebView view, int code, String desc, String failingU
112174
}
113175

114176
private boolean interceptUrl(WebView view, String url, boolean loadUrl) {
177+
Log.i(TAG, "interceptUrl called with url: " + url);
115178
if (isCallbackUri(url, mController.getCallbackUrl())) {
116179
mController.getAccessToken(mWebView, url);
117180

@@ -192,6 +255,7 @@ public void onPageStarted(String url, Bitmap favicon) {
192255
@Override
193256
public void onPageFinished(String url) {
194257
Log.d(TAG, "onPageFinished: " + url);
258+
// mController.onComplete(url);
195259
}
196260

197261
@Override

android/src/main/java/io/fullstack/oauth/OAuthManagerFragmentController.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.text.TextUtils;
99
import im.delight.android.webview.AdvancedWebView;
1010

11+
import com.facebook.react.bridge.ReactContext;
1112
import android.net.Uri;
1213
import android.util.Log;
1314
import android.view.View;
@@ -40,12 +41,15 @@ public class OAuthManagerFragmentController {
4041
private final android.app.FragmentManager fragmentManager;
4142
private final Handler uiHandler;
4243

44+
private ReactContext context;
45+
private String providerName;
4346
private String authVersion;
4447
private OAuth10aService oauth10aService;
4548
private OAuth20Service oauth20Service;
4649
private String callbackUrl;
4750
private OAuth1RequestToken oauth1RequestToken;
4851
private HashMap<String,Object> mCfg;
52+
private AdvancedWebView mWebView;
4953

5054
private Runnable onAccessToken;
5155
private OAuthManagerOnAccessTokenListener mListener;
@@ -55,6 +59,7 @@ private void runOnMainThread(Runnable runnable) {
5559
}
5660

5761
public OAuthManagerFragmentController(
62+
final ReactContext mReactContext,
5863
android.app.FragmentManager fragmentManager,
5964
final String providerName,
6065
OAuth10aService oauthService,
@@ -63,12 +68,15 @@ public OAuthManagerFragmentController(
6368
this.uiHandler = new Handler(Looper.getMainLooper());
6469
this.fragmentManager = fragmentManager;
6570

71+
this.context = mReactContext;
72+
this.providerName = providerName;
6673
this.authVersion = "1.0";
6774
this.oauth10aService = oauthService;
6875
this.callbackUrl = callbackUrl;
6976
}
7077

7178
public OAuthManagerFragmentController(
79+
final ReactContext mReactContext,
7280
android.app.FragmentManager fragmentManager,
7381
final String providerName,
7482
OAuth20Service oauthService,
@@ -77,6 +85,8 @@ public OAuthManagerFragmentController(
7785
this.uiHandler = new Handler(Looper.getMainLooper());
7886
this.fragmentManager = fragmentManager;
7987

88+
this.context = mReactContext;
89+
this.providerName = providerName;
8090
this.authVersion = "2.0";
8191
this.oauth20Service = oauthService;
8292
this.callbackUrl = callbackUrl;
@@ -107,7 +117,7 @@ public void run() {
107117

108118
Log.d(TAG, "Creating new Fragment");
109119
OAuthManagerDialogFragment frag =
110-
OAuthManagerDialogFragment.newInstance(OAuthManagerFragmentController.this);
120+
OAuthManagerDialogFragment.newInstance(context, OAuthManagerFragmentController.this);
111121

112122
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
113123
ft.add(frag, TAG);
@@ -141,22 +151,34 @@ public void loaded10aAccessToken(final OAuth1AccessToken accessToken) {
141151
Log.d(TAG, "Loaded access token in OAuthManagerFragmentController");
142152
Log.d(TAG, "AccessToken: " + accessToken + " (raw: " + accessToken.getRawResponse() + ")");
143153

154+
mWebView = null;
144155
this.dismissDialog();
145156
mListener.onOAuth1AccessToken(accessToken);
146157
}
147158

148159
public void loaded20AccessToken(final OAuth2AccessToken accessToken) {
160+
mWebView = null;
149161
this.dismissDialog();
150162
mListener.onOAuth2AccessToken(accessToken);
151163
}
152164

165+
public void onComplete(String url) {
166+
Log.d(TAG, "onComplete called in fragment controller " + url);
167+
// if (mWebView != null) {
168+
// this.getAccessToken(mWebView, url);
169+
// } else {
170+
// this.dismissDialog();
171+
// }
172+
}
173+
153174
public void onError(int errorCode, String description, String failingUrl) {
154175
Log.e(TAG, "Error in OAuthManagerFragmentController: " + description);
155176
this.dismissDialog();
156177
mListener.onRequestTokenError(new Exception(description));
157178
}
158179

159180
public void getRequestTokenUrlAndLoad(AdvancedWebView webView) {
181+
mWebView = webView;
160182
LoadRequestTokenTask task = new LoadRequestTokenTask(this, webView);
161183
task.execute();
162184
}
@@ -174,9 +196,15 @@ public void getAccessToken(
174196
task.execute();
175197
} else if (authVersion.equals("2.0")) {
176198
String code = responseUri.getQueryParameter("code");
177-
Load2AccessTokenTask task = new Load2AccessTokenTask(
178-
this, webView, code);
179-
task.execute();
199+
Log.d(TAG, "Called getAccessToken with code: " + code + " at " + url);
200+
if (code != null) {
201+
Load2AccessTokenTask task = new Load2AccessTokenTask(
202+
this, webView, code);
203+
task.execute();
204+
} else {
205+
this.dismissDialog();
206+
mListener.onRequestTokenError(new Exception("No token found"));
207+
}
180208
}
181209
}
182210

0 commit comments

Comments
 (0)