-
-
Notifications
You must be signed in to change notification settings - Fork 508
Expand file tree
/
Copy pathOAuthLoginActivity.java
More file actions
178 lines (147 loc) · 4.94 KB
/
OAuthLoginActivity.java
File metadata and controls
178 lines (147 loc) · 4.94 KB
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
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package org.quantumbadger.redreader.activities;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.RedReader;
import org.quantumbadger.redreader.common.PrefsUtility;
import org.quantumbadger.redreader.common.TorCommon;
import org.quantumbadger.redreader.reddit.api.RedditOAuth;
import java.util.Objects;
import info.guardianproject.netcipher.webkit.WebkitProxy;
public class OAuthLoginActivity extends ViewsBaseActivity {
private static final String OAUTH_HOST = "rr_oauth_redir";
private static final String REDREADER_SCHEME = "redreader";
private static final String HTTP_SCHEME = "http";
private WebView mWebView;
@Override
protected void onDestroy() {
super.onDestroy();
final CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookies(null);
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(final Bundle savedInstanceState) {
PrefsUtility.applyTheme(this);
super.onCreate(savedInstanceState);
mWebView = new WebView(this);
if (TorCommon.isTorEnabled()) {
try {
final boolean result = WebkitProxy.setProxy(
RedReader.class.getCanonicalName(),
getApplicationContext(),
mWebView,
"127.0.0.1",
8118);
if (!result) {
BugReportActivity.handleGlobalError(
this,
getResources().getString(R.string.error_tor_setting_failed));
}
} catch (final Exception e) {
BugReportActivity.handleGlobalError(this, e);
}
}
final WebSettings settings = mWebView.getSettings();
settings.setBuiltInZoomControls(false);
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(false);
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setDomStorageEnabled(true);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
settings.setSaveFormData(false);
}
settings.setDatabaseEnabled(false);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
settings.setDisplayZoomControls(false);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onConsoleMessage(final ConsoleMessage consoleMessage) {
return true;
}
});
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(
final WebView view,
final WebResourceRequest request) {
final Uri url = request.getUrl();
if (Objects.equals(url.getHost(), OAUTH_HOST) &&
(Objects.equals(url.getScheme(), REDREADER_SCHEME) ||
Objects.equals(url.getScheme(), HTTP_SCHEME))) {
final Intent intent = new Intent();
intent.setData(url);
setResult(RESULT_OK, intent);
finish();
} else {
setTitle(url.getHost());
return false;
}
return true;
}
});
setBaseActivityListing(mWebView);
mWebView.loadUrl(RedditOAuth.getPromptUri().toString());
}
@Override
protected void onPause() {
super.onPause();
if (mWebView != null) {
mWebView.onPause();
mWebView.pauseTimers();
}
}
@Override
protected void onResume() {
super.onResume();
if (mWebView != null) {
mWebView.resumeTimers();
mWebView.onResume();
}
}
public static class ResultContract extends ActivityResultContract<Void, Uri> {
@NonNull
@Override
public Intent createIntent(@NonNull final Context context, final Void unused) {
return new Intent(context, OAuthLoginActivity.class);
}
@Override
public Uri parseResult(final int resultCode, @Nullable final Intent intent) {
if (resultCode == RESULT_OK && intent != null) {
return intent.getData();
}
return null;
}
}
}