-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubLoginActivity.java
115 lines (92 loc) · 3.51 KB
/
GitHubLoginActivity.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
package com.company.myapp.lib.github;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.company.myapp.R;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import com.company.myapp.lib.github.GitHubAuth;
import android.net.Uri;
public class GitHubLoginActivity extends AppCompatActivity {
public static final String ACCESS_TOKEN_EXTRA = "GitHubAccessToken";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.github_login_webview_activity);
Toolbar mToolbar = (Toolbar) findViewById(R.id.github_login_toolbar);
setSupportActionBar(mToolbar);
setTitle("GitHub Login");
setupWebview();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
default:
finish();
return true;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.github_login_menu, menu);
return true;
}
private void setupWebview() {
WebView mWebView = (WebView) findViewById(R.id.github_login_webview);
mWebView.setWebViewClient(
new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith(GitHubAuth.REDIRECT_URL) && url.contains("code=")) {
String code = Uri.parse(url).getQueryParameter("code");
GitHubAuth.requestAccessToken(
code,
new Callback<String>() {
@Override
public void onResponse(Call<String> call, Response<String> response) {
JsonObject object =
new Gson().fromJson(response.body(), JsonElement.class).getAsJsonObject();
String token = object.get("access_token").getAsString();
Intent intent = new Intent();
intent.putExtra(ACCESS_TOKEN_EXTRA, token);
setResult(AppCompatActivity.RESULT_OK, intent);
finish();
}
@Override
public void onFailure(Call<String> call, Throwable t) {
setResult(AppCompatActivity.RESULT_CANCELED);
finish();
}
});
return true;
}
return false;
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(false);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setScrollbarFadingEnabled(false);
String url = "https://github.com/login/oauth/authorize" + "?client_id=" + GitHubAuth.CLIENT_ID;
mWebView.loadUrl(url);
}
}