This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
forked from venmo/app-switch-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VenmoWebViewActivity.java
94 lines (75 loc) · 3.07 KB
/
VenmoWebViewActivity.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
package com.venmo.demo; //Replace this with your package name
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.RelativeLayout;
public class VenmoWebViewActivity extends Activity {
Context mContext;
private WebView mVenmoWebView;
String mUrl;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.venmo_webview);
mUrl = getIntent().getExtras().getString("url");
mContext = this;
mVenmoWebView = (WebView)getLastNonConfigurationInstance();
if(mVenmoWebView != null) { //If screen was rotated, don't reload the whole webview
WebView myWebView = (WebView)findViewById(R.id.venmo_wv);
RelativeLayout parent = (RelativeLayout)myWebView.getParent();
parent.removeView(myWebView);
RelativeLayout parent2 = (RelativeLayout)mVenmoWebView.getParent();
parent2.removeView(mVenmoWebView);
parent.addView(mVenmoWebView);
}
else { //load the webview
mVenmoWebView = (WebView)findViewById(R.id.venmo_wv);
mVenmoWebView.addJavascriptInterface(new VenmoJavaScriptInterface(this), "VenmoAndroidSDK");
mVenmoWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mVenmoWebView.getSettings().setJavaScriptEnabled(true);
mVenmoWebView.getSettings().setUserAgentString("venmo-android-2.0");
mVenmoWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
mVenmoWebView.loadUrl(mUrl);
}
}
/*called right before screen rotates. We store the webview object so it can be recovered when the activity re-starts*/
@Override
public Object onRetainNonConfigurationInstance() {
return mVenmoWebView;
}
//This class handles what happens when the user has successfully paid OR if there's an error, and it's time to
//yield control back to your previous activity (the one that opened up this Venmo payment webview).
public class VenmoJavaScriptInterface
{
Context mContext;
Activity mActivity;
/** Instantiate the interface and set the context */
VenmoJavaScriptInterface(Context c) {
mContext = c;
mActivity = (Activity)c;
}
public void paymentSuccessful(String signed_request) {
Intent i = new Intent();
i.putExtra("signedrequest", signed_request);
mActivity.setResult(mActivity.RESULT_OK, i);
mActivity.finish();
}
public void error(String error_message) {
Intent i = new Intent();
i.putExtra("error_message", error_message);
mActivity.setResult(mActivity.RESULT_OK, i);
mActivity.finish();
}
public void cancel() {
Intent i = new Intent();
mActivity.setResult(mActivity.RESULT_CANCELED);
mActivity.finish();
}
}
}