Skip to content

Commit

Permalink
修复了下载文件的异常, 但是还有部分资源地址报400
Browse files Browse the repository at this point in the history
Signed-off-by: Nages <[email protected]>
  • Loading branch information
nageslan committed Jun 10, 2023
1 parent 3a6b520 commit 6ebbfb2
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.iml
.idea
.gradle
/local.properties
/.idea/caches
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.aof.mcinabox">

<uses-feature
Expand All @@ -21,6 +20,7 @@
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme">
<activity
android:name=".activity.LoginActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
}
11 changes: 11 additions & 0 deletions app/src/main/java/com/aof/mcinabox/activity/OldMainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.view.KeyEvent;
import android.widget.Button;

import androidx.annotation.Nullable;

import com.aof.mcinabox.R;
import com.aof.mcinabox.gamecontroller.definitions.manifest.AppManifest;
import com.aof.mcinabox.launcher.lang.LangManager;
Expand All @@ -18,6 +20,7 @@
import com.aof.mcinabox.launcher.uis.BaseUI;
import com.aof.mcinabox.launcher.uis.achieve.UiManager;
import com.aof.mcinabox.utils.FileTool;
import com.aof.mcinabox.utils.dialog.FileSelectUtils;

import java.io.File;
import java.io.IOException;
Expand All @@ -44,6 +47,7 @@ public class OldMainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_old_main);
SettingJson.initStaticConfig(getApplicationContext());
//静态对象
CURRENT_ACTIVITY = new WeakReference<>(this);
//使用语言管理器切换语言
Expand Down Expand Up @@ -169,6 +173,13 @@ private void setMCinaBoxNoMedia() {
}
}

// 为了兼容旧版本的AndroidSDK做的一些妥协
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
FileSelectUtils.getCallback().onResult(requestCode, resultCode, data);
}

/**
* 【移除缓存文件夹】
**/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ private void initSourceMap(String[][] originMap) {
}

public String getSourceUrl(String sourceName, String type) {
if (sourceName.equals("origin")) {
return Objects.requireNonNull(SourceMap.get("official")).get(type);
}
return Objects.requireNonNull(SourceMap.get(sourceName)).get(type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class GamedirManager {

private final static String TAG = "GamedirManager";

public final static String PUBLIC_GAMEDIR = SettingJson.DEFAULT_GAMEDIR;
public static String PUBLIC_GAMEDIR = SettingJson.getDEFAULT_GAMEDIR();
public final static String PRIVATE_GAMEDIR = AppManifest.MCINABOX_HOME + "/gamedir";

public static boolean setGamedir(Context context, SettingJson setting, String gamedir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.aof.mcinabox.launcher.setting.support;

import android.os.Environment;
import android.content.Context;

public class SettingJson {

public final static String USER_TYPE_OFFLINE = "offline";
public final static String USER_TYPE_ONLINE = "online";
public final static String USER_TYPE_EXTERNAL = "external";

public final static String DOWNLOAD_SOURCE_ORIGIN = "official";
public final static String DOWNLOAD_SOURCE_OFFICIAL = "official";
public final static String DOWNLOAD_SOURCE_BMCLAPI = "bmclapi";
public final static String DOWNLOAD_SOURCE_MCBBS = "mcbbs";
public final static String[] DOWNLOAD_SOURCES = {DOWNLOAD_SOURCE_OFFICIAL, DOWNLOAD_SOURCE_BMCLAPI, DOWNLOAD_SOURCE_MCBBS};
public final static String[] DOWNLOAD_SOURCES = {DOWNLOAD_SOURCE_ORIGIN, DOWNLOAD_SOURCE_OFFICIAL, DOWNLOAD_SOURCE_BMCLAPI, DOWNLOAD_SOURCE_MCBBS};

public final static String DEFAULT_GAMEDIR = Environment.getExternalStorageDirectory().getPath() + "/MCinaBox/gamedir";
public static String DEFAULT_GAMEDIR;

private String downloadType; //下载源
private Configurations configurations; //全局游戏设置
Expand All @@ -27,9 +28,9 @@ public SettingJson() {
//默认模板初始化
super();

downloadType = DOWNLOAD_SOURCE_OFFICIAL;
downloadType = DOWNLOAD_SOURCE_ORIGIN;
lastVersion = "";
gamedir = DEFAULT_GAMEDIR;
gamedir = getDEFAULT_GAMEDIR();

configurations = new Configurations()
.setJavaArgs("")
Expand All @@ -41,6 +42,17 @@ public SettingJson() {
accounts = new Account[]{};
}

public static void initStaticConfig(Context mContext) {
if (SettingJson.DEFAULT_GAMEDIR == null) {
SettingJson.DEFAULT_GAMEDIR = mContext.getExternalCacheDir().getPath() + "/MCinaBox/gamedir";
}

}

public static String getDEFAULT_GAMEDIR() {
return SettingJson.DEFAULT_GAMEDIR;
}

//全局游戏设置
public class Configurations {
private String javaArgs; //Java虚拟机附加启动参数
Expand Down Expand Up @@ -299,7 +311,7 @@ public SettingJson setLastVersion(String lastVersion) {
}

public String getGamedir() {
return this.gamedir;
return SettingJson.getDEFAULT_GAMEDIR();
}

public SettingJson setGameDir(String dir) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public void onClick(View v) {
DialogUtils.createBothChoicesDialog(mContext, mContext.getString(R.string.title_warn), mContext.getString(R.string.tips_target_dir_is_not_exist), mContext.getString(R.string.title_ok), mContext.getString(R.string.title_cancel), new DialogSupports() {
@Override
public void runWhenPositive() {
if (!GamedirManager.setGamedir(mContext, OldMainActivity.Setting, dir.getAbsolutePath())) {
if (!GamedirManager.setGamedir(mContext, OldMainActivity.Setting, SettingJson.getDEFAULT_GAMEDIR())) {
DialogUtils.createSingleChoiceDialog(mContext, mContext.getString(R.string.title_error), mContext.getString(R.string.tips_failed_to_revise_game_dir), mContext.getString(R.string.title_ok), null);
} else {
DialogUtils.createSingleChoiceDialog(mContext, mContext.getString(R.string.title_note), mContext.getString(R.string.tips_successed_to_revise_game_dir), mContext.getString(R.string.title_ok), null);
}
}
});
} else {
if (!GamedirManager.setGamedir(mContext, OldMainActivity.Setting, dir.getAbsolutePath())) {
if (!GamedirManager.setGamedir(mContext, OldMainActivity.Setting, SettingJson.getDEFAULT_GAMEDIR())) {
DialogUtils.createSingleChoiceDialog(mContext, mContext.getString(R.string.title_error), mContext.getString(R.string.tips_failed_to_revise_game_dir), mContext.getString(R.string.title_ok), null);
} else {
DialogUtils.createSingleChoiceDialog(mContext, mContext.getString(R.string.title_note), mContext.getString(R.string.tips_successed_to_revise_game_dir), mContext.getString(R.string.title_ok), null);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.aof.mcinabox.launcher.uis;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
Expand All @@ -11,6 +13,7 @@
import android.widget.LinearLayout;
import android.widget.Spinner;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;

import com.aof.mcinabox.R;
Expand All @@ -23,9 +26,16 @@
import com.aof.mcinabox.utils.FileTool;
import com.aof.mcinabox.utils.ZipUtils;
import com.aof.mcinabox.utils.dialog.DialogUtils;
import com.aof.mcinabox.utils.dialog.FileSelectUtils;
import com.aof.mcinabox.utils.dialog.support.DialogSupports;
import com.aof.mcinabox.utils.dialog.support.TaskDialog;

import org.apache.commons.compress.utils.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Objects;

Expand All @@ -46,6 +56,8 @@ public LauncherSettingUI(Context context) {
private Animation showAnim;
private SettingJson setting;

private final static int SELECT_FILE_REQUEST_CODE = 1145141;

private final View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -66,10 +78,22 @@ public void onClick(View v) {
public void runWhenItemsSelected(int pos) {
super.runWhenItemsSelected(pos);
if (pos == 0) {
DialogUtils.createFileSelectorDialog(mContext, mContext.getString(R.string.title_import_runtime), AppManifest.SDCARD_HOME, "xz", new DialogSupports() {

FileSelectUtils.startActivityForResult((Activity) mContext, SELECT_FILE_REQUEST_CODE, new FileSelectUtils.Callback() {
@Override
public void runWhenItemsSelected(Object path) {
RuntimeManager.installRuntimeFromPath(mContext, (String) path);
public void onResult(int requestCode, int resultCode, @Nullable Intent data) {
try {
File tempFile = new File(mContext.getExternalCacheDir(), "temp.tar.xz");
FileOutputStream out = new FileOutputStream(tempFile);
// InputStream open = mContext.getAssets().open("aarch64-20210204.tar.xz");
InputStream open = mContext.getContentResolver().openInputStream(data.getData());
IOUtils.copy(open, out);
open.close();
out.close();
RuntimeManager.installRuntimeFromPath(mContext, tempFile.getPath());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
} else {
Expand Down Expand Up @@ -193,7 +217,7 @@ public void onCreate() {
}
listDownloaderSources.setOnItemSelectedListener(this);

setConfigureToDownloadtype(setting.getDownloadType(), listDownloaderSources);
setConfigureToDownloadtype("origin", listDownloaderSources);

//调用主题管理器设定主题
if (setting.isBackgroundAutoSwitch()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.aof.mcinabox.utils.dialog;


import android.app.Activity;
import android.content.Intent;

import androidx.annotation.Nullable;

/**
* 文件选择器
*/
public class FileSelectUtils {

private static Callback callback = new Callback() {
@Override
public void onResult(int requestCode, int resultCode, @Nullable Intent data) {

}
};

public static Callback getCallback() {
return callback;
}

public static void setCallback(Callback callback) {
FileSelectUtils.callback = callback;
}

public static void startActivityForResult(Activity activity, int requestCode, Callback callback) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/x-xz");
activity.startActivityForResult(intent, requestCode);
FileSelectUtils.callback = callback;
}

public static interface Callback {
void onResult(int requestCode, int resultCode, @Nullable Intent data);
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/xml/network_security_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!-- 默认配置,是否开启Http流量允许-->
<base-config cleartextTrafficPermitted="true" />
</network-security-config>

0 comments on commit 6ebbfb2

Please sign in to comment.