正在查看: HW华为币 v1.0 应用的 MainActivity.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
正在查看: HW华为币 v1.0 应用的 MainActivity.java JAVA 源代码文件
本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。
package com.example.webapp;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.DownloadListener;
import android.webkit.GeolocationPermissions;
import android.webkit.PermissionRequest;
import android.webkit.URLUtil;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private static final long CACHE_DURATION = 86400000;
private static final String CACHE_FILE_NAME = "resolved_urls.txt";
private static final int PERMISSION_REQUEST_CODE = 100;
private AppConfig appConfig;
private ActivityResultLauncher<Intent> cameraLauncher;
private Uri cameraPhotoUri;
private ValueCallback<Uri[]> filePathCallback;
private ActivityResultLauncher<Intent> galleryLauncher;
private RelativeLayout loadingLayout;
private TextView loadingText;
private Runnable pendingAction;
private List<String> resolvedUrlList;
private Runnable timeoutRunnable;
private List<String> urlList;
private WebView webView;
private int currentUrlIndex = 0;
private boolean isExiting = false;
private boolean isFirstLoad = true;
private Handler timeoutHandler = new Handler(Looper.getMainLooper());
private boolean isPageLoading = false;
private int currentVersion = 1;
private boolean versionCheckCompleted = false;
private int remoteVersionNumber = -1;
private String remoteDownloadUrl = "";
private BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.DOWNLOAD_COMPLETE".equals(intent.getAction())) {
Toast.makeText(MainActivity.this, "文件下载完成", 0).show();
}
}
};
private static class AppConfig {
String appName;
String backupUrl;
String mainUrl;
String packageName;
boolean showSplash;
int version;
private AppConfig() {
this.appName = "应用1";
this.packageName = "com.example.webapp.v1";
this.version = 1;
this.mainUrl = "one1party.top";
this.backupUrl = "txtjsonxml.men";
this.showSplash = false;
}
public String getUrlDomain() {
return "url." + this.mainUrl;
}
public String getUpdateDomain() {
return "update." + this.mainUrl;
}
public String getBackupUrlDomain() {
return "url." + this.backupUrl;
}
public String getBackupUpdateDomain() {
return "update." + this.backupUrl;
}
public String toString() {
return "AppConfig{appName='" + this.appName + "', packageName='" + this.packageName + "', version=" + this.version + ", mainUrl='" + this.mainUrl + "', backupUrl='" + this.backupUrl + "', showSplash=" + this.showSplash + '}';
}
}
private File getCacheFile() {
File file = new File(getExternalFilesDir(null), getPackageName());
if (!file.exists()) {
file.mkdirs();
}
File file2 = new File(file, CACHE_FILE_NAME);
Log.i("WebApp11", "缓存文件路径: " + file2.getAbsolutePath());
return file2;
}
public void showLoading(String str) {
if (this.loadingLayout != null) {
this.loadingText.setText(str);
this.loadingLayout.setVisibility(0);
}
}
public void hideLoading() {
RelativeLayout relativeLayout = this.loadingLayout;
if (relativeLayout != null) {
relativeLayout.setVisibility(8);
}
}
private void loadAppConfig() {
BufferedReader bufferedReader;
this.appConfig = new AppConfig();
try {
bufferedReader = new BufferedReader(new InputStreamReader(getAssets().open("config.txt")));
} catch (IOException e) {
e.printStackTrace();
Log.w("WebApp11", "读取配置文件失败,使用默认配置: " + e.getMessage());
return;
}
while (true) {
String readLine = bufferedReader.readLine();
if (readLine != null) {
String trim = readLine.trim();
if (!trim.isEmpty() && !trim.startsWith("#") && !trim.startsWith("//")) {
String[] split = trim.split("=", 2);
if (split.length == 2) {
char c = 0;
String trim2 = split[0].trim();
String trim3 = split[1].trim();
switch (trim2.hashCode()) {
case -1718339631:
if (trim2.equals("PackageName")) {
c = 1;
break;
}
c = 65535;
break;
case 109757538:
if (trim2.equals("start")) {
c = 5;
break;
}
c = 65535;
break;
case 351608024:
if (trim2.equals("version")) {
c = 2;
break;
}
c = 65535;
break;
case 831013110:
if (trim2.equals("mainUrl")) {
c = 3;
break;
}
c = 65535;
break;
case 870516780:
if (trim2.equals("AppName")) {
break;
}
c = 65535;
break;
case 1355343949:
if (trim2.equals("backupUrl")) {
c = 4;
break;
}
c = 65535;
break;
default:
c = 65535;
break;
}
if (c == 0) {
this.appConfig.appName = trim3;
} else if (c == 1) {
this.appConfig.packageName = trim3;
} else if (c == 2) {
try {
this.appConfig.version = Integer.parseInt(trim3);
this.currentVersion = this.appConfig.version;
} catch (NumberFormatException unused) {
Log.w("WebApp11", "无效的版本号: " + trim3);
}
} else if (c == 3) {
this.appConfig.mainUrl = trim3;
} else if (c == 4) {
this.appConfig.backupUrl = trim3;
} else if (c != 5) {
Log.d("WebApp11", "未知的配置项: " + trim2 + "=" + trim3);
} else {
this.appConfig.showSplash = "1".equals(trim3);
}
}
}
} else {
bufferedReader.close();
Log.i("WebApp11", "应用配置加载成功: " + this.appConfig.toString());
return;
}
e.printStackTrace();
Log.w("WebApp11", "读取配置文件失败,使用默认配置: " + e.getMessage());
return;
}
}
private void loadCurrentVersion() {
Log.d("WebApp11", "当前应用版本: " + this.currentVersion + " (从配置文件读取)");
}
private void checkVersionUpdate() {
Log.d("WebApp11", "checkVersionUpdate方法被调用");
showLoading("请稍后,正在加载页面…");
new Thread(new Runnable() {
@Override
public void run() {
Log.d("WebApp11", "版本检查后台线程开始执行");
final String performVersionCheck = MainActivity.this.performVersionCheck();
Log.d("WebApp11", "版本检查后台线程完成,结果: " + performVersionCheck);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
Log.d("WebApp11", "开始在主线程处理版本检查结果");
MainActivity.this.handleVersionCheckResult(performVersionCheck);
}
});
}
}).start();
Log.d("WebApp11", "版本检查线程已启动");
}
public String performVersionCheck() {
List<String> queryTxtRecords;
int parseInt;
String trim;
try {
Log.d("WebApp11", "开始检查版本更新...");
AppConfig appConfig = this.appConfig;
if (appConfig == null || appConfig.mainUrl == null || this.appConfig.mainUrl.isEmpty()) {
Log.w("WebApp11", "版本检查配置无效,使用默认域名");
this.appConfig = new AppConfig();
}
Log.d("WebApp11", "查询版本检查域名: " + this.appConfig.getUpdateDomain());
try {
queryTxtRecords = queryTxtRecords(this.appConfig.getUpdateDomain());
} catch (Exception e) {
Log.w("WebApp11", "主域名查询失败,尝试备用域名: " + e.getMessage());
try {
Log.d("WebApp11", "查询备用版本检查域名: " + this.appConfig.getBackupUpdateDomain());
queryTxtRecords = queryTxtRecords(this.appConfig.getBackupUpdateDomain());
} catch (Exception e2) {
Log.e("WebApp11", "备用域名也查询失败: " + e2.getMessage());
throw e2;
}
}
if (queryTxtRecords != null && !queryTxtRecords.isEmpty()) {
StringBuilder sb = new StringBuilder();
String str = "";
String str2 = "";
for (String str3 : queryTxtRecords) {
Log.d("WebApp11", "版本检查TXT记录片段: " + str3);
if (str3.trim().startsWith("https://www.") && str3.length() > "https://www.".length()) {
String substring = str3.trim().substring("https://www.".length());
try {
Integer.parseInt(substring);
try {
Log.d("WebApp11", "找到版本记录: " + substring);
str = substring;
} catch (NumberFormatException unused) {
str = substring;
Log.d("WebApp11", "不是有效的版本记录: " + str3);
if (str3.trim().startsWith("https://")) {
str2 = str3.trim();
Log.d("WebApp11", "找到下载链接记录: " + str2);
}
sb.append(str3.trim());
}
} catch (NumberFormatException unused2) {
}
}
if (str3.trim().startsWith("https://") && !str3.trim().startsWith("https://www.")) {
str2 = str3.trim();
Log.d("WebApp11", "找到下载链接记录: " + str2);
}
sb.append(str3.trim());
}
if (!str.isEmpty() && !str2.isEmpty()) {
try {
int parseInt2 = Integer.parseInt(str);
Log.d("WebApp11", "分离解析成功 - 远程版本: " + parseInt2 + ", 下载链接: " + str2);
if (parseInt2 > this.currentVersion && !str2.isEmpty()) {
this.remoteDownloadUrl = str2;
this.remoteVersionNumber = parseInt2;
return "update_available";
}
if (parseInt2 <= this.currentVersion) {
return "up_to_date";
}
} catch (NumberFormatException unused3) {
Log.w("WebApp11", "解析分离的版本号失败: " + str);
}
}
String sb2 = sb.toString();
Log.d("WebApp11", "合并后的完整记录: " + sb2);
String trim2 = sb2.trim();
if (trim2.startsWith("https://www.")) {
trim2 = trim2.substring("https://www.".length());
Log.d("WebApp11", "清理后的记录: " + trim2);
}
String[] split = trim2.split("\\|");
char c = 1;
int i = 2;
if (split.length >= 2) {
try {
parseInt = Integer.parseInt(split[0].trim());
trim = split[1].trim();
if (!trim.startsWith("http://") && !trim.startsWith("https://")) {
trim = "https://" + trim;
}
Log.d("WebApp11", "解析成功 - 远程版本: " + parseInt + ", 下载链接: " + trim);
} catch (NumberFormatException unused4) {
Log.w("WebApp11", "解析版本号失败: " + split[0]);
}
if (parseInt <= this.currentVersion || trim.isEmpty()) {
return parseInt <= this.currentVersion ? "up_to_date" : "no_update_info";
}
this.remoteDownloadUrl = trim;
this.remoteVersionNumber = parseInt;
return "update_available";
}
Log.w("WebApp11", "记录格式不正确,无法找到|分隔符: " + trim2);
for (String str4 : queryTxtRecords) {
Log.d("WebApp11", "尝试单独解析记录: " + str4);
String trim3 = str4.trim();
if (trim3.startsWith("https://www.")) {
trim3 = trim3.substring("https://www.".length());
Log.d("WebApp11", "单条记录清理后: " + trim3);
}
String[] split2 = trim3.split("\\|");
if (split2.length >= i) {
try {
int parseInt3 = Integer.parseInt(split2[0].trim());
String trim4 = split2[c].trim();
if (!trim4.startsWith("http://") && !trim4.startsWith("https://")) {
trim4 = "https://" + trim4;
}
Log.d("WebApp11", "单条记录解析成功 - 远程版本: " + parseInt3 + ", 下载链接: " + trim4);
if (parseInt3 > this.currentVersion && !trim4.isEmpty()) {
this.remoteDownloadUrl = trim4;
this.remoteVersionNumber = parseInt3;
return "update_available";
}
if (parseInt3 <= this.currentVersion) {
return "up_to_date";
}
} catch (NumberFormatException unused5) {
Log.w("WebApp11", "单条记录解析版本号失败: " + split2[0]);
}
}
c = 1;
i = 2;
}
}
Log.w("WebApp11", "未获取到任何TXT记录");
return "no_update_info";
} catch (Exception e3) {
Log.e("WebApp11", "版本检查失败: " + e3.getMessage());
return "check_failed";
}
}
public void handleVersionCheckResult(String str) {
char c;
Log.d("WebApp11", "版本检查结果: " + str);
switch (str.hashCode()) {
case -1205004589:
if (str.equals("update_available")) {
c = 0;
break;
}
c = 65535;
break;
case -885565106:
if (str.equals("up_to_date")) {
c = 1;
break;
}
c = 65535;
break;
case -298814636:
if (str.equals("check_failed")) {
c = 3;
break;
}
c = 65535;
break;
case 819317990:
if (str.equals("no_update_info")) {
c = 2;
break;
}
c = 65535;
break;
default:
c = 65535;
break;
}
if (c == 0) {
Log.i("WebApp11", "发现新版本 " + this.remoteVersionNumber + ", 当前版本 " + this.currentVersion);
showUpdateDialog(this.remoteDownloadUrl);
} else if (c == 1) {
Log.i("WebApp11", "当前版本已是最新");
continueWithCurrentApp();
} else if (c == 2) {
Log.w("WebApp11", "未找到版本更新信息");
continueWithCurrentApp();
} else {
Log.w("WebApp11", "版本检查失败,继续使用当前版本");
continueWithCurrentApp();
}
}
private void showUpdateDialog(final String str) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("发现新版本");
builder.setMessage("检测到有新版本可用,是否立即下载更新?");
builder.setCancelable(false);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(str));
if (intent.resolveActivity(MainActivity.this.getPackageManager()) != null) {
MainActivity.this.startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "无法打开浏览器", 0).show();
}
dialogInterface.dismiss();
MainActivity.this.continueWithCurrentApp();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
MainActivity.this.continueWithCurrentApp();
}
});
builder.show();
}
public void continueWithCurrentApp() {
Log.d("WebApp11", "continueWithCurrentApp方法被调用,开始正常应用流程");
this.versionCheckCompleted = true;
loadUrlList();
checkPermissions();
}
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setStatusBarTransparent();
setContentView(R.layout.activity_main);
this.webView = (WebView) findViewById(R.id.webview);
this.loadingLayout = (RelativeLayout) findViewById(R.id.loading_layout);
this.loadingText = (TextView) findViewById(R.id.loading_text);
showLoading("请稍后,正在加载页面…");
IntentFilter intentFilter = new IntentFilter("android.intent.action.DOWNLOAD_COMPLETE");
if (Build.VERSION.SDK_INT >= 33) {
ContextCompat.registerReceiver(this, this.downloadCompleteReceiver, intentFilter, 4);
} else {
ContextCompat.registerReceiver(this, this.downloadCompleteReceiver, intentFilter, 4);
}
this.galleryLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback() {
@Override
public final void onActivityResult(Object obj) {
MainActivity.this.m93lambda$onCreate$0$comexamplewebappMainActivity((ActivityResult) obj);
}
});
this.cameraLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback() {
@Override
public final void onActivityResult(Object obj) {
MainActivity.this.m94lambda$onCreate$1$comexamplewebappMainActivity((ActivityResult) obj);
}
});
Log.d("WebApp11", "开始读取当前版本号...");
loadAppConfig();
loadCurrentVersion();
Log.d("WebApp11", "开始执行版本检查...");
checkVersionUpdate();
}
void m93lambda$onCreate$0$comexamplewebappMainActivity(ActivityResult activityResult) {
String dataString;
if (this.filePathCallback != null) {
this.filePathCallback.onReceiveValue((activityResult.getResultCode() != -1 || activityResult.getData() == null || (dataString = activityResult.getData().getDataString()) == null) ? null : new Uri[]{Uri.parse(dataString)});
this.filePathCallback = null;
}
}
void m94lambda$onCreate$1$comexamplewebappMainActivity(ActivityResult activityResult) {
Uri uri;
if (this.filePathCallback != null) {
this.filePathCallback.onReceiveValue((activityResult.getResultCode() != -1 || (uri = this.cameraPhotoUri) == null) ? null : new Uri[]{uri});
this.filePathCallback = null;
}
}
@Override
protected void onDestroy() {
Runnable runnable;
super.onDestroy();
this.isExiting = true;
Handler handler = this.timeoutHandler;
if (handler != null && (runnable = this.timeoutRunnable) != null) {
handler.removeCallbacks(runnable);
}
try {
unregisterReceiver(this.downloadCompleteReceiver);
} catch (IllegalArgumentException unused) {
}
WebView webView = this.webView;
if (webView != null) {
webView.stopLoading();
this.webView.destroy();
}
}
private void loadUrlList() {
this.urlList = new ArrayList();
this.resolvedUrlList = new ArrayList();
AppConfig appConfig = this.appConfig;
if (appConfig != null && !appConfig.mainUrl.isEmpty()) {
this.urlList.add(this.appConfig.getUrlDomain());
Log.d("WebApp11", "从配置文件读取到主域名: " + this.appConfig.getUrlDomain());
if (!this.appConfig.backupUrl.isEmpty() && !this.appConfig.backupUrl.equals(this.appConfig.mainUrl)) {
this.urlList.add(this.appConfig.getBackupUrlDomain());
Log.d("WebApp11", "从配置文件读取到备用域名: " + this.appConfig.getBackupUrlDomain());
}
}
Log.i("WebApp11", "总共读取到 " + this.urlList.size() + " 个域名: " + this.urlList.toString());
if (isCacheValid()) {
loadResolvedUrlsFromCache();
} else {
new DnsResolverTask().execute(new Void[0]);
}
}
private boolean isCacheValid() {
File cacheFile = getCacheFile();
if (cacheFile.exists()) {
return System.currentTimeMillis() - cacheFile.lastModified() < CACHE_DURATION;
}
return false;
}
private void loadResolvedUrlsFromCache() {
showLoading("请稍后,正在加载页面…");
File cacheFile = getCacheFile();
if (!cacheFile.exists()) {
Log.w("WebApp11", "缓存文件不存在,重新解析DNS TXT记录");
new DnsResolverTask().execute(new Void[0]);
return;
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(cacheFile)));
while (true) {
String readLine = bufferedReader.readLine();
if (readLine != null) {
String trim = readLine.trim();
if (!trim.isEmpty()) {
this.resolvedUrlList.add(trim);
}
} else {
bufferedReader.close();
Log.d("WebApp11", "从缓存加载了 " + this.resolvedUrlList.size() + " 个URL");
return;
}
}
} catch (IOException e) {
e.printStackTrace();
Log.w("WebApp11", "从缓存加载URL失败,重新解析DNS TXT记录");
new DnsResolverTask().execute(new Void[0]);
}
}
public void saveResolvedUrlsToCache() {
File cacheFile = getCacheFile();
try {
File parentFile = cacheFile.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(cacheFile)));
Iterator<String> it = this.resolvedUrlList.iterator();
while (it.hasNext()) {
bufferedWriter.write(it.next());
bufferedWriter.newLine();
}
bufferedWriter.close();
Log.d("WebApp11", "保存了 " + this.resolvedUrlList.size() + " 个URL到缓存");
Log.i("WebApp11", "缓存文件已保存到: " + cacheFile.getAbsolutePath());
Log.i("WebApp11", "用户可访问路径: " + getUserFriendlyPath(cacheFile));
} catch (IOException e) {
e.printStackTrace();
Log.w("WebApp11", "保存URL到缓存失败: " + e.getMessage());
}
}
private String getUserFriendlyPath(File file) {
String absolutePath = file.getAbsolutePath();
if (absolutePath.contains("/Download")) {
return "下载文件夹/WebApp_resolved_urls.txt";
}
return absolutePath.contains("Android/data") ? "Android/data/" + getPackageName() + "/files/Documents/resolved_urls.txt" : absolutePath;
}
private void checkPermissions() {
Log.d("WebApp11", "跳过启动时权限检查,权限将在使用时申请");
setupWebView();
}
@Override
public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
super.onRequestPermissionsResult(i, strArr, iArr);
if (i == 100) {
for (int i2 = 0; i2 < strArr.length; i2++) {
Log.d("WebApp11", "权限 " + strArr[i2] + " 申请结果: " + (iArr[i2] == 0 ? "已授予" : "被拒绝"));
}
Runnable runnable = this.pendingAction;
if (runnable != null) {
runnable.run();
this.pendingAction = null;
}
Log.d("WebApp11", "权限申请完成,继续使用应用");
}
}
private void setupWebView() {
showLoading("请稍后,正在加载页面…");
Log.i("WebApp11", "解析的URL缓存文件路径: " + getCacheFile().getAbsolutePath());
WebSettings settings = this.webView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
settings.setDatabaseEnabled(true);
settings.setAllowFileAccess(true);
settings.setAllowContentAccess(true);
settings.setMediaPlaybackRequiresUserGesture(false);
settings.setMixedContentMode(0);
settings.setBuiltInZoomControls(false);
settings.setSupportZoom(false);
settings.setSupportMultipleWindows(true);
settings.setCacheMode(-1);
settings.setGeolocationEnabled(false);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
settings.setUserAgentString(settings.getUserAgentString() + " WebApp11/1.0");
CookieManager.getInstance().setAcceptCookie(true);
CookieManager.getInstance().setAcceptThirdPartyCookies(this.webView, true);
this.webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String str, String str2, String str3, String str4, long j) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(str));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, URLUtil.guessFileName(str, str3, str4));
request.setNotificationVisibility(1);
request.setTitle("下载图片");
request.setDescription("正在下载压缩后的图片");
request.addRequestHeader("User-Agent", str2);
DownloadManager downloadManager = (DownloadManager) MainActivity.this.getSystemService("download");
if (downloadManager != null) {
try {
downloadManager.enqueue(request);
Toast.makeText(MainActivity.this, "开始下载图片", 0).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "下载失败: " + e.getMessage(), 0).show();
}
}
}
});
this.webView.setWebViewClient(new AnonymousClass6());
this.webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String str, GeolocationPermissions.Callback callback) {
callback.invoke(str, true, false);
}
@Override
public void onPermissionRequest(PermissionRequest permissionRequest) {
permissionRequest.grant(permissionRequest.getResources());
}
@Override
public boolean onCreateWindow(WebView webView, boolean z, boolean z2, Message message) {
String extra = webView.getHitTestResult().getExtra();
if (extra == null) {
return false;
}
Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(extra));
if (intent.resolveActivity(MainActivity.this.getPackageManager()) == null) {
return false;
}
MainActivity.this.startActivity(intent);
return false;
}
@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, WebChromeClient.FileChooserParams fileChooserParams) {
MainActivity.this.filePathCallback = valueCallback;
MainActivity.this.showImagePickerDialog();
return true;
}
});
loadNextUrl();
}
class AnonymousClass6 extends WebViewClient {
AnonymousClass6() {
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
String uri = webResourceRequest.getUrl().toString();
if (uri.contains("download") || uri.endsWith(".zip") || uri.endsWith(".png") || uri.endsWith(".jpg") || uri.endsWith(".jpeg")) {
return false;
}
uri.endsWith(".webp");
return false;
}
@Override
public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
super.onPageStarted(webView, str, bitmap);
Log.d("WebApp11", "开始加载页面: " + str);
MainActivity.this.isPageLoading = true;
MainActivity.this.showLoading("请稍后,正在加载页面…");
if (MainActivity.this.timeoutRunnable != null) {
MainActivity.this.timeoutHandler.removeCallbacks(MainActivity.this.timeoutRunnable);
}
MainActivity.this.timeoutRunnable = new AnonymousClass1(str);
MainActivity.this.timeoutHandler.postDelayed(MainActivity.this.timeoutRunnable, 5000L);
}
class AnonymousClass1 implements Runnable {
final String val$url;
AnonymousClass1(String str) {
this.val$url = str;
}
@Override
public void run() {
if (!MainActivity.this.isPageLoading || MainActivity.this.isExiting) {
return;
}
Log.w("WebApp11", "页面加载超时: " + this.val$url);
MainActivity.this.isPageLoading = false;
if (MainActivity.this.webView != null) {
MainActivity.this.webView.stopLoading();
}
if (MainActivity.this.isFirstLoad && MainActivity.this.currentUrlIndex > 0) {
String str = (String) MainActivity.this.resolvedUrlList.get(MainActivity.this.currentUrlIndex - 1);
Log.d("WebApp11", "超时URL移到末尾: " + str);
MainActivity.this.resolvedUrlList.remove(MainActivity.this.currentUrlIndex - 1);
MainActivity.this.resolvedUrlList.add(str);
MainActivity mainActivity = MainActivity.this;
mainActivity.currentUrlIndex--;
MainActivity.this.saveResolvedUrlsToCache();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting || MainActivity.this.webView == null) {
return;
}
MainActivity.this.webView.loadUrl("about:blank");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
MainActivity.this.loadNextUrl();
}
}, 100L);
}
});
}
}
@Override
public void onPageFinished(WebView webView, String str) {
super.onPageFinished(webView, str);
Log.d("WebApp11", "页面加载完成: " + str);
MainActivity.this.isPageLoading = false;
MainActivity.this.isFirstLoad = false;
MainActivity.this.hideLoading();
if (MainActivity.this.timeoutRunnable != null) {
MainActivity.this.timeoutHandler.removeCallbacks(MainActivity.this.timeoutRunnable);
}
webView.evaluateJavascript("(function() { var links = document.getElementsByTagName('a'); for (var i = 0; i < links.length; i++) { var link = links[i]; if (link.target === '_blank') { link.onclick = function(e) { e.preventDefault(); window.open(this.href, '_blank'); return false; }; } }})();", null);
}
@Override
public void onReceivedError(final WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
if (MainActivity.this.isExiting) {
Log.d("WebApp11", "应用正在退出,停止加载URL");
return;
}
Log.w("WebApp11", "页面加载失败 - 错误码: " + webResourceError.getErrorCode() + ", 描述: " + ((Object) webResourceError.getDescription()) + ", URL: " + webResourceRequest.getUrl());
if (webResourceRequest.isForMainFrame()) {
Log.d("WebApp11", "主页面加载失败: " + webResourceRequest.getUrl().toString());
MainActivity.this.isPageLoading = false;
if (MainActivity.this.timeoutRunnable != null) {
MainActivity.this.timeoutHandler.removeCallbacks(MainActivity.this.timeoutRunnable);
}
webView.stopLoading();
if (MainActivity.this.isFirstLoad && MainActivity.this.currentUrlIndex > 0) {
String str = (String) MainActivity.this.resolvedUrlList.get(MainActivity.this.currentUrlIndex - 1);
Log.d("WebApp11", "将失败的URL移到末尾: " + str);
MainActivity.this.resolvedUrlList.remove(MainActivity.this.currentUrlIndex - 1);
MainActivity.this.resolvedUrlList.add(str);
MainActivity mainActivity = MainActivity.this;
mainActivity.currentUrlIndex--;
MainActivity.this.saveResolvedUrlsToCache();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
MainActivity.this.showLoading("请稍后,正在加载页面…");
webView.loadUrl("about:blank");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
MainActivity.this.loadNextUrl();
}
}, 100L);
}
});
}
}
@Override
public void onReceivedError(final WebView webView, int i, String str, String str2) {
if (MainActivity.this.isExiting) {
Log.d("WebApp11", "应用正在退出,停止加载URL");
return;
}
Log.w("WebApp11", "页面加载失败(旧版本) - 错误码: " + i + ", 描述: " + str + ", URL: " + str2);
MainActivity.this.isPageLoading = false;
if (MainActivity.this.timeoutRunnable != null) {
MainActivity.this.timeoutHandler.removeCallbacks(MainActivity.this.timeoutRunnable);
}
webView.stopLoading();
if (MainActivity.this.isFirstLoad && MainActivity.this.currentUrlIndex > 0) {
String str3 = (String) MainActivity.this.resolvedUrlList.get(MainActivity.this.currentUrlIndex - 1);
Log.d("WebApp11", "将失败的URL移到末尾(旧版本): " + str3);
MainActivity.this.resolvedUrlList.remove(MainActivity.this.currentUrlIndex - 1);
MainActivity.this.resolvedUrlList.add(str3);
MainActivity mainActivity = MainActivity.this;
mainActivity.currentUrlIndex--;
MainActivity.this.saveResolvedUrlsToCache();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
webView.loadUrl("about:blank");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
MainActivity.this.loadNextUrl();
}
}, 100L);
}
});
}
@Override
public void onReceivedHttpError(final WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
if (!MainActivity.this.isExiting && webResourceRequest.isForMainFrame()) {
Log.w("WebApp11", "HTTP错误: " + webResourceResponse.getStatusCode() + " - " + webResourceRequest.getUrl());
MainActivity.this.isPageLoading = false;
if (MainActivity.this.timeoutRunnable != null) {
MainActivity.this.timeoutHandler.removeCallbacks(MainActivity.this.timeoutRunnable);
}
webView.stopLoading();
if (MainActivity.this.isFirstLoad && MainActivity.this.currentUrlIndex > 0) {
String str = (String) MainActivity.this.resolvedUrlList.get(MainActivity.this.currentUrlIndex - 1);
Log.d("WebApp11", "HTTP错误URL移到末尾: " + str);
MainActivity.this.resolvedUrlList.remove(MainActivity.this.currentUrlIndex - 1);
MainActivity.this.resolvedUrlList.add(str);
MainActivity mainActivity = MainActivity.this;
mainActivity.currentUrlIndex--;
MainActivity.this.saveResolvedUrlsToCache();
}
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
webView.loadUrl("about:blank");
new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
@Override
public void run() {
if (MainActivity.this.isExiting) {
return;
}
MainActivity.this.loadNextUrl();
}
}, 100L);
}
});
}
}
}
public void showImagePickerDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("选择图片");
builder.setItems(new String[]{"从相册选择", "拍照"}, new DialogInterface.OnClickListener() {
@Override
public final void onClick(DialogInterface dialogInterface, int i) {
MainActivity.this.m97lambda$showImagePickerDialog$4$comexamplewebappMainActivity(dialogInterface, i);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public final void onCancel(DialogInterface dialogInterface) {
MainActivity.this.m98lambda$showImagePickerDialog$5$comexamplewebappMainActivity(dialogInterface);
}
});
builder.show();
}
void m97lambda$showImagePickerDialog$4$comexamplewebappMainActivity(DialogInterface dialogInterface, int i) {
if (i == 0) {
checkAndRequestStoragePermission(new Runnable() {
@Override
public final void run() {
MainActivity.this.m95lambda$showImagePickerDialog$2$comexamplewebappMainActivity();
}
});
} else {
if (i != 1) {
return;
}
checkAndRequestCameraPermission(new Runnable() {
@Override
public final void run() {
MainActivity.this.m96lambda$showImagePickerDialog$3$comexamplewebappMainActivity();
}
});
}
}
void m98lambda$showImagePickerDialog$5$comexamplewebappMainActivity(DialogInterface dialogInterface) {
ValueCallback<Uri[]> valueCallback = this.filePathCallback;
if (valueCallback != null) {
valueCallback.onReceiveValue(null);
this.filePathCallback = null;
}
}
private void checkAndRequestStoragePermission(Runnable runnable) {
ArrayList arrayList = new ArrayList();
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(this, "android.permission.READ_MEDIA_IMAGES") != 0) {
arrayList.add("android.permission.READ_MEDIA_IMAGES");
}
} else if (ContextCompat.checkSelfPermission(this, "android.permission.READ_EXTERNAL_STORAGE") != 0) {
arrayList.add("android.permission.READ_EXTERNAL_STORAGE");
}
if (arrayList.isEmpty()) {
runnable.run();
} else {
this.pendingAction = runnable;
ActivityCompat.requestPermissions(this, (String[]) arrayList.toArray(new String[0]), 100);
}
}
private void checkAndRequestCameraPermission(Runnable runnable) {
ArrayList arrayList = new ArrayList();
if (ContextCompat.checkSelfPermission(this, "android.permission.CAMERA") != 0) {
arrayList.add("android.permission.CAMERA");
}
if (Build.VERSION.SDK_INT >= 33) {
if (ContextCompat.checkSelfPermission(this, "android.permission.READ_MEDIA_IMAGES") != 0) {
arrayList.add("android.permission.READ_MEDIA_IMAGES");
}
} else {
if (ContextCompat.checkSelfPermission(this, "android.permission.READ_EXTERNAL_STORAGE") != 0) {
arrayList.add("android.permission.READ_EXTERNAL_STORAGE");
}
if (ContextCompat.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE") != 0) {
arrayList.add("android.permission.WRITE_EXTERNAL_STORAGE");
}
}
if (arrayList.isEmpty()) {
runnable.run();
} else {
this.pendingAction = runnable;
ActivityCompat.requestPermissions(this, (String[]) arrayList.toArray(new String[0]), 100);
}
}
public void m95lambda$showImagePickerDialog$2$comexamplewebappMainActivity() {
if (Build.VERSION.SDK_INT < 33 ? ContextCompat.checkSelfPermission(this, "android.permission.READ_EXTERNAL_STORAGE") != 0 : ContextCompat.checkSelfPermission(this, "android.permission.READ_MEDIA_IMAGES") != 0) {
Toast.makeText(this, "需要存储权限才能访问相册", 0).show();
ValueCallback<Uri[]> valueCallback = this.filePathCallback;
if (valueCallback != null) {
valueCallback.onReceiveValue(null);
this.filePathCallback = null;
return;
}
return;
}
Intent intent = new Intent("android.intent.action.PICK");
intent.setType("image/*");
this.galleryLauncher.launch(intent);
}
public void m96lambda$showImagePickerDialog$3$comexamplewebappMainActivity() {
if (ContextCompat.checkSelfPermission(this, "android.permission.CAMERA") != 0) {
Toast.makeText(this, "需要摄像头权限才能拍照", 0).show();
ValueCallback<Uri[]> valueCallback = this.filePathCallback;
if (valueCallback != null) {
valueCallback.onReceiveValue(null);
this.filePathCallback = null;
return;
}
return;
}
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File createImageFile = createImageFile();
if (createImageFile != null) {
try {
Uri uriForFile = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", createImageFile);
this.cameraPhotoUri = uriForFile;
intent.putExtra("output", uriForFile);
if (Build.VERSION.SDK_INT <= 28) {
intent.addFlags(2);
intent.addFlags(1);
}
if (intent.resolveActivity(getPackageManager()) != null) {
this.cameraLauncher.launch(intent);
return;
}
Toast.makeText(this, "没有找到相机应用", 0).show();
ValueCallback<Uri[]> valueCallback2 = this.filePathCallback;
if (valueCallback2 != null) {
valueCallback2.onReceiveValue(null);
this.filePathCallback = null;
return;
}
return;
} catch (Exception e) {
Toast.makeText(this, "相机启动失败: " + e.getMessage(), 0).show();
ValueCallback<Uri[]> valueCallback3 = this.filePathCallback;
if (valueCallback3 != null) {
valueCallback3.onReceiveValue(null);
this.filePathCallback = null;
return;
}
return;
}
}
Toast.makeText(this, "无法创建临时文件", 0).show();
ValueCallback<Uri[]> valueCallback4 = this.filePathCallback;
if (valueCallback4 != null) {
valueCallback4.onReceiveValue(null);
this.filePathCallback = null;
}
}
private File createImageFile() {
File file;
try {
String str = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()) + "_";
if (Build.VERSION.SDK_INT >= 29) {
file = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
} else {
file = new File(Environment.getExternalStorageDirectory(), "Pictures");
if (!file.exists()) {
file.mkdirs();
}
}
if (file == null) {
file = new File(getFilesDir(), "Pictures");
if (!file.exists()) {
file.mkdirs();
}
}
return File.createTempFile(str, ".jpg", file);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public void loadNextUrl() {
if (this.isExiting) {
Log.d("WebApp11", "应用正在退出,停止加载URL");
return;
}
if (this.webView == null) {
Log.e("WebApp11", "WebView为空,无法加载URL");
return;
}
List<String> list = this.resolvedUrlList;
if (list == null || list.isEmpty()) {
Log.d("WebApp11", "等待DNS解析完成...");
return;
}
if (this.currentUrlIndex < this.resolvedUrlList.size()) {
String str = this.resolvedUrlList.get(this.currentUrlIndex);
Log.d("WebApp11", "正在加载URL: " + str + " (第" + (this.currentUrlIndex + 1) + "个,共" + this.resolvedUrlList.size() + "个)");
try {
this.webView.loadUrl(str);
this.currentUrlIndex++;
return;
} catch (Exception e) {
Log.e("WebApp11", "加载URL失败: " + e.getMessage());
if (this.isFirstLoad && this.currentUrlIndex < this.resolvedUrlList.size()) {
String str2 = this.resolvedUrlList.get(this.currentUrlIndex);
Log.d("WebApp11", "将加载失败的URL移到末尾: " + str2);
this.resolvedUrlList.remove(this.currentUrlIndex);
this.resolvedUrlList.add(str2);
saveResolvedUrlsToCache();
} else {
this.currentUrlIndex++;
}
loadNextUrl();
return;
}
}
if (this.isFirstLoad) {
Log.w("WebApp11", "所有URL都无法访问,显示错误页面");
startActivity(new Intent(this, (Class<?>) ErrorActivity.class));
finish();
return;
}
Log.w("WebApp11", "所有URL都无法访问,重新解析DNS TXT记录");
this.currentUrlIndex = 0;
this.isFirstLoad = true;
this.resolvedUrlList.clear();
File cacheFile = getCacheFile();
if (cacheFile.exists()) {
cacheFile.delete();
Log.d("WebApp11", "已删除缓存文件: " + cacheFile.getAbsolutePath());
}
new DnsResolverTask().execute(new Void[0]);
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.isExiting = true;
Log.d("WebApp11", "用户按下返回键,退出应用");
finishAffinity();
}
private void setStatusBarTransparent() {
Window window = getWindow();
if (Build.VERSION.SDK_INT >= 30) {
window.setDecorFitsSystemWindows(false);
window.setStatusBarColor(0);
} else {
window.getDecorView().setSystemUiVisibility(1280);
window.setStatusBarColor(0);
}
}
private class DnsResolverTask extends AsyncTask<Void, Void, List<String>> {
private DnsResolverTask() {
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.d("WebApp11", "开始解析DNS TXT记录...");
MainActivity.this.showLoading("请稍后,正在加载页面…");
}
@Override
public List<String> doInBackground(Void... voidArr) {
ArrayList arrayList = new ArrayList();
for (String str : MainActivity.this.urlList) {
try {
List queryTxtRecords = MainActivity.this.queryTxtRecords(str);
arrayList.addAll(queryTxtRecords);
Log.d("WebApp11", "域名 " + str + " 解析到 " + queryTxtRecords.size() + " 条记录");
} catch (Exception e) {
Log.e("WebApp11", "解析域名 " + str + " 失败: " + e.getMessage());
}
}
return arrayList;
}
@Override
public void onPostExecute(List<String> list) {
super.onPostExecute((DnsResolverTask) list);
if (list == null || list.isEmpty()) {
Log.w("WebApp11", "DNS解析失败,没有获得任何有效URL");
MainActivity.this.startActivity(new Intent(MainActivity.this, (Class<?>) ErrorActivity.class));
MainActivity.this.finish();
return;
}
MainActivity.this.resolvedUrlList.clear();
MainActivity.this.resolvedUrlList.addAll(list);
MainActivity.this.saveResolvedUrlsToCache();
Log.d("WebApp11", "DNS解析完成,共获得 " + MainActivity.this.resolvedUrlList.size() + " 个URL");
MainActivity.this.currentUrlIndex = 0;
MainActivity.this.isFirstLoad = true;
MainActivity.this.loadNextUrl();
}
}
public List<String> queryTxtRecords(String str) throws Exception {
ArrayList arrayList = new ArrayList();
try {
HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("https://dns.alidns.com/resolve?name=" + str + "&type=TXT").openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; WebApp/1.0)");
Log.d("WebApp11", "查询域名: " + str + " 的TXT记录 (使用阿里云DNS)");
int responseCode = httpURLConnection.getResponseCode();
if (responseCode != 200) {
Log.e("WebApp11", "DNS查询失败,响应码: " + responseCode);
throw new Exception("DNS查询失败,响应码: " + responseCode);
}
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
StringBuilder sb = new StringBuilder();
while (true) {
String readLine = bufferedReader.readLine();
if (readLine == null) {
break;
}
sb.append(readLine);
}
bufferedReader.close();
Log.d("WebApp11", "DNS响应: " + sb.toString());
JSONObject jSONObject = new JSONObject(sb.toString());
if (!jSONObject.has("Answer")) {
Log.w("WebApp11", "域名 " + str + " 没有TXT记录");
return arrayList;
}
JSONArray jSONArray = jSONObject.getJSONArray("Answer");
Log.d("WebApp11", "找到 " + jSONArray.length() + " 条DNS记录");
for (int i = 0; i < jSONArray.length(); i++) {
String string = jSONArray.getJSONObject(i).getString("data");
Log.d("WebApp11", "原始TXT记录: " + string);
String replaceAll = string.replaceAll("\"", "");
Log.d("WebApp11", "处理后TXT记录: " + replaceAll);
String[] split = replaceAll.split("\\|");
Log.d("WebApp11", "分割后得到 " + split.length + " 个部分");
for (String str2 : split) {
String trim = str2.trim();
if (!trim.startsWith("http://") && !trim.startsWith("https://")) {
trim = "https://www." + trim;
}
if (isValidUrl(trim)) {
arrayList.add(trim);
Log.d("WebApp11", "找到有效URL: " + trim);
} else {
Log.w("WebApp11", "无效URL: " + trim);
}
}
}
httpURLConnection.disconnect();
return arrayList;
} catch (Exception e) {
Log.e("WebApp11", "DNS查询失败: " + e.getMessage());
throw e;
}
}
private boolean isValidUrl(String str) {
if (str != null) {
return (str.startsWith("http://") || str.startsWith("https://")) && str.contains(".");
}
return false;
}
}