2022-01-28 18:27:16 +00:00
|
|
|
package com.fox2code.mmm.androidacy;
|
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Intent;
|
2022-01-30 21:14:36 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2022-02-06 17:06:26 +00:00
|
|
|
import android.graphics.Bitmap;
|
2022-01-28 18:27:16 +00:00
|
|
|
import android.net.Uri;
|
2022-01-30 15:54:26 +00:00
|
|
|
import android.os.Build;
|
2022-01-28 18:27:16 +00:00
|
|
|
import android.os.Bundle;
|
2022-02-22 00:45:56 +00:00
|
|
|
import android.util.Log;
|
2022-01-30 21:14:36 +00:00
|
|
|
import android.webkit.ValueCallback;
|
|
|
|
import android.webkit.WebChromeClient;
|
2022-01-28 18:27:16 +00:00
|
|
|
import android.webkit.WebResourceRequest;
|
|
|
|
import android.webkit.WebSettings;
|
|
|
|
import android.webkit.WebView;
|
2022-02-06 17:06:26 +00:00
|
|
|
import android.widget.Toast;
|
2022-01-28 18:27:16 +00:00
|
|
|
|
2022-01-30 22:32:10 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2022-01-28 18:27:16 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2022-02-06 17:06:26 +00:00
|
|
|
import androidx.webkit.WebResourceErrorCompat;
|
2022-01-30 22:32:10 +00:00
|
|
|
import androidx.webkit.WebSettingsCompat;
|
|
|
|
import androidx.webkit.WebViewClientCompat;
|
|
|
|
import androidx.webkit.WebViewFeature;
|
2022-01-28 18:27:16 +00:00
|
|
|
|
2022-01-28 22:12:32 +00:00
|
|
|
import com.fox2code.mmm.BuildConfig;
|
2022-01-30 15:54:26 +00:00
|
|
|
import com.fox2code.mmm.Constants;
|
2022-01-28 21:47:41 +00:00
|
|
|
import com.fox2code.mmm.MainApplication;
|
2022-01-28 18:27:16 +00:00
|
|
|
import com.fox2code.mmm.R;
|
2022-03-26 19:39:47 +00:00
|
|
|
import com.fox2code.mmm.XHooks;
|
2022-01-28 18:27:16 +00:00
|
|
|
import com.fox2code.mmm.compat.CompatActivity;
|
|
|
|
import com.fox2code.mmm.utils.Http;
|
|
|
|
import com.fox2code.mmm.utils.IntentHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Per Androidacy repo implementation agreement, no request of this WebView shall be modified.
|
|
|
|
*/
|
2022-01-28 21:47:41 +00:00
|
|
|
public class AndroidacyActivity extends CompatActivity {
|
2022-02-22 00:45:56 +00:00
|
|
|
private static final String TAG = "AndroidacyActivity";
|
2022-02-05 17:07:36 +00:00
|
|
|
private static final String REFERRER = "utm_source=FoxMMM&utm_medium=app";
|
|
|
|
|
2022-01-28 22:12:32 +00:00
|
|
|
static {
|
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
|
WebView.setWebContentsDebuggingEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-30 22:32:10 +00:00
|
|
|
WebView webView;
|
2022-02-22 00:45:56 +00:00
|
|
|
AndroidacyWebAPI androidacyWebAPI;
|
2022-01-30 22:32:10 +00:00
|
|
|
boolean backOnResume;
|
2022-01-28 18:27:16 +00:00
|
|
|
|
|
|
|
@Override
|
2022-01-28 21:47:41 +00:00
|
|
|
@SuppressLint({"SetJavaScriptEnabled", "JavascriptInterface"})
|
2022-01-28 18:27:16 +00:00
|
|
|
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
Intent intent = this.getIntent();
|
|
|
|
Uri uri;
|
2022-01-28 21:47:41 +00:00
|
|
|
if (!MainApplication.checkSecret(intent) ||
|
2022-02-22 00:45:56 +00:00
|
|
|
(uri = intent.getData()) == null) {
|
|
|
|
Log.w(TAG, "Impersonation detected");
|
2022-01-28 18:27:16 +00:00
|
|
|
this.forceBackPressed();
|
|
|
|
return;
|
|
|
|
}
|
2022-02-05 17:07:36 +00:00
|
|
|
String url = uri.toString();
|
2022-02-22 00:45:56 +00:00
|
|
|
if (!AndroidacyUtil.isAndroidacyLink(url, uri)) {
|
|
|
|
Log.w(TAG, "Calling non androidacy link in secure WebView: " + url);
|
2022-02-05 17:07:36 +00:00
|
|
|
this.forceBackPressed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!url.endsWith(REFERRER) && (url.startsWith("https://www.androidacy.com/") ||
|
2022-02-05 21:10:33 +00:00
|
|
|
url.startsWith("https://api.androidacy.com/magisk/"))) {
|
2022-02-05 17:07:36 +00:00
|
|
|
if (url.lastIndexOf('/') < url.lastIndexOf('?')) {
|
|
|
|
url = url + '&' + REFERRER;
|
|
|
|
} else {
|
|
|
|
url = url + '?' + REFERRER;
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 15:54:26 +00:00
|
|
|
boolean allowInstall = intent.getBooleanExtra(
|
|
|
|
Constants.EXTRA_ANDROIDACY_ALLOW_INSTALL, false);
|
2022-01-30 21:14:36 +00:00
|
|
|
String title = intent.getStringExtra(Constants.EXTRA_ANDROIDACY_ACTIONBAR_TITLE);
|
|
|
|
String config = intent.getStringExtra(Constants.EXTRA_ANDROIDACY_ACTIONBAR_CONFIG);
|
2022-03-25 01:00:12 +00:00
|
|
|
int compatLevel = intent.getIntExtra(Constants.EXTRA_ANDROIDACY_COMPAT_LEVEL, 0);
|
2022-01-28 18:27:16 +00:00
|
|
|
this.setContentView(R.layout.webview);
|
2022-02-23 20:30:51 +00:00
|
|
|
setActionBarBackground(null);
|
|
|
|
this.setDisplayHomeAsUpEnabled(true);
|
|
|
|
if (title == null || title.isEmpty()) {
|
|
|
|
this.setTitle("Androidacy");
|
2022-03-02 21:20:41 +00:00
|
|
|
} else {
|
|
|
|
this.setTitle(title);
|
2022-02-23 20:30:51 +00:00
|
|
|
}
|
2022-01-30 22:32:10 +00:00
|
|
|
if (allowInstall || title == null || title.isEmpty()) {
|
2022-01-30 21:14:36 +00:00
|
|
|
this.hideActionBar();
|
|
|
|
} else { // Only used for note section
|
|
|
|
if (config != null && !config.isEmpty()) {
|
|
|
|
String configPkg = IntentHelper.getPackageOfConfig(config);
|
|
|
|
try {
|
2022-03-26 19:39:47 +00:00
|
|
|
XHooks.checkConfigTargetExists(this, configPkg, config);
|
2022-01-30 21:14:36 +00:00
|
|
|
this.setActionBarExtraMenuButton(R.drawable.ic_baseline_app_settings_alt_24,
|
|
|
|
menu -> {
|
|
|
|
IntentHelper.openConfig(this, config);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
} catch (PackageManager.NameNotFoundException ignored) {}
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 18:27:16 +00:00
|
|
|
this.webView = this.findViewById(R.id.webView);
|
|
|
|
WebSettings webSettings = this.webView.getSettings();
|
|
|
|
webSettings.setUserAgentString(Http.getAndroidacyUA());
|
|
|
|
webSettings.setDomStorageEnabled(true);
|
|
|
|
webSettings.setJavaScriptEnabled(true);
|
2022-02-24 19:27:10 +00:00
|
|
|
webSettings.setAllowFileAccess(false);
|
2022-01-30 15:54:26 +00:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Make website follow app theme
|
|
|
|
webSettings.setForceDark(MainApplication.getINSTANCE().isLightTheme() ?
|
|
|
|
WebSettings.FORCE_DARK_OFF : WebSettings.FORCE_DARK_ON);
|
2022-01-30 22:32:10 +00:00
|
|
|
} else if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) {
|
|
|
|
WebSettingsCompat.setForceDark(webSettings, MainApplication.getINSTANCE().isLightTheme() ?
|
|
|
|
WebSettingsCompat.FORCE_DARK_OFF : WebSettingsCompat.FORCE_DARK_ON);
|
2022-01-30 15:54:26 +00:00
|
|
|
}
|
2022-01-30 22:32:10 +00:00
|
|
|
this.webView.setWebViewClient(new WebViewClientCompat() {
|
2022-02-06 17:06:26 +00:00
|
|
|
private String pageUrl;
|
|
|
|
|
2022-01-28 18:27:16 +00:00
|
|
|
@Override
|
2022-01-30 22:32:10 +00:00
|
|
|
public boolean shouldOverrideUrlLoading(
|
|
|
|
@NonNull WebView view, @NonNull WebResourceRequest request) {
|
2022-02-22 00:45:56 +00:00
|
|
|
// Don't open non Androidacy urls inside WebView
|
2022-03-03 12:59:39 +00:00
|
|
|
if (request.isForMainFrame() &&
|
|
|
|
!AndroidacyUtil.isAndroidacyLink(request.getUrl())) {
|
|
|
|
IntentHelper.openUri(view.getContext(), request.getUrl().toString());
|
2022-01-28 18:27:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-02-06 17:06:26 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageStarted(WebView view, String url, Bitmap favicon) {
|
|
|
|
this.pageUrl = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onReceivedError(String url,int errorCode) {
|
|
|
|
if ((url.startsWith("https://api.androidacy.com/magisk/") ||
|
|
|
|
url.equals(pageUrl)) && (errorCode == 419 || errorCode == 429)) {
|
|
|
|
Toast.makeText(AndroidacyActivity.this,
|
|
|
|
"Too many requests!", Toast.LENGTH_LONG).show();
|
|
|
|
AndroidacyActivity.this.runOnUiThread(AndroidacyActivity.this::onBackPressed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
|
|
|
|
this.onReceivedError(failingUrl, errorCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceivedError(@NonNull WebView view, @NonNull WebResourceRequest request,
|
|
|
|
@NonNull WebResourceErrorCompat error) {
|
|
|
|
if (WebViewFeature.isFeatureSupported(WebViewFeature.WEB_RESOURCE_ERROR_GET_CODE)) {
|
|
|
|
this.onReceivedError(request.getUrl().toString(), error.getErrorCode());
|
|
|
|
}
|
|
|
|
}
|
2022-01-28 18:27:16 +00:00
|
|
|
});
|
2022-01-30 21:14:36 +00:00
|
|
|
this.webView.setWebChromeClient(new WebChromeClient() {
|
|
|
|
@Override
|
|
|
|
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback,
|
|
|
|
FileChooserParams fileChooserParams) {
|
|
|
|
CompatActivity.getCompatActivity(webView).startActivityForResult(
|
|
|
|
fileChooserParams.createIntent(), (code, data) ->
|
|
|
|
filePathCallback.onReceiveValue(
|
|
|
|
FileChooserParams.parseResult(code, data)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2022-03-25 01:00:12 +00:00
|
|
|
this.webView.setDownloadListener((
|
|
|
|
downloadUrl, userAgent, contentDisposition, mimetype, contentLength) -> {
|
|
|
|
if (AndroidacyUtil.isAndroidacyLink(downloadUrl)) {
|
|
|
|
AndroidacyWebAPI androidacyWebAPI = this.androidacyWebAPI;
|
|
|
|
if (androidacyWebAPI != null) {
|
|
|
|
if (androidacyWebAPI.consumedAction && !androidacyWebAPI.downloadMode) {
|
|
|
|
return; // Native module popup may cause download after consumed action
|
|
|
|
}
|
|
|
|
androidacyWebAPI.consumedAction = true;
|
|
|
|
androidacyWebAPI.downloadMode = false;
|
|
|
|
} else if (this.backOnResume) return;
|
|
|
|
this.backOnResume = true;
|
|
|
|
IntentHelper.openCustomTab(this, downloadUrl);
|
|
|
|
}
|
|
|
|
});
|
2022-03-26 19:39:47 +00:00
|
|
|
XHooks.onWebViewInitialize(this.webView, allowInstall);
|
2022-02-22 00:45:56 +00:00
|
|
|
this.webView.addJavascriptInterface(androidacyWebAPI =
|
2022-01-30 15:54:26 +00:00
|
|
|
new AndroidacyWebAPI(this, allowInstall), "mmm");
|
2022-03-25 01:00:12 +00:00
|
|
|
if (compatLevel != 0) androidacyWebAPI.notifyCompatModeRaw(compatLevel);
|
2022-02-05 17:07:36 +00:00
|
|
|
this.webView.loadUrl(url);
|
2022-01-28 18:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onBackPressed() {
|
|
|
|
WebView webView = this.webView;
|
|
|
|
if (webView != null && webView.canGoBack()) {
|
|
|
|
webView.goBack();
|
|
|
|
} else {
|
|
|
|
super.onBackPressed();
|
|
|
|
}
|
|
|
|
}
|
2022-01-30 22:32:10 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
if (this.backOnResume) {
|
|
|
|
this.backOnResume = false;
|
|
|
|
this.forceBackPressed();
|
2022-02-22 00:45:56 +00:00
|
|
|
} else if (this.androidacyWebAPI != null) {
|
|
|
|
this.androidacyWebAPI.consumedAction = false;
|
2022-01-30 22:32:10 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-28 18:27:16 +00:00
|
|
|
}
|