Skip to content

Commit

Permalink
调整[设置]: 添加手动获取启动器更新&调整布局
Browse files Browse the repository at this point in the history
  • Loading branch information
Vera-Firefly committed Sep 11, 2024
1 parent 4ad17b6 commit ec574f8
Show file tree
Hide file tree
Showing 11 changed files with 282 additions and 164 deletions.
163 changes: 103 additions & 60 deletions app_pojavlauncher/src/main/java/com/firefly/feature/UpdateLauncher.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.firefly.feature;

import static net.kdt.pojavlaunch.Architecture.*;
import static net.kdt.pojavlaunch.prefs.LauncherPreferences.DEFAULT_PREF;

import androidx.core.content.FileProvider;

Expand Down Expand Up @@ -38,9 +39,11 @@ public class UpdateLauncher {
private static final String GITHUB_RELEASE_URL = "github.com/Vera-Firefly/Pojav-Glow-Worm/releases/download/%s/Pojav-Glow-Worm-%s-%s.apk";
private Context context;
private int localVersionCode;
File dir;

public UpdateLauncher(Context context) {
this.context = context;
this.dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
try {
String versionCodeString = context.getString(R.string.base_version_code);
this.localVersionCode = Integer.parseInt(versionCodeString);
Expand All @@ -50,49 +53,17 @@ public UpdateLauncher(Context context) {
}
}

public void checkCachedApk() {
File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File apkFile = new File(dir, "cache.apk");
File apkVersionFile = new File(dir, "apk_version");

if (apkFile.exists() && apkVersionFile.exists()) {
try {
String savedTagName = new String(java.nio.file.Files.readAllBytes(apkVersionFile.toPath()));
int savedVersionCode = Integer.parseInt(savedTagName.replaceAll("[^\\d]", ""));
if (savedVersionCode > localVersionCode) {
new CustomDialog.Builder(context)
.setTitle(context.getString(R.string.pgw_settings_updatelauncher_install_prompt_title))
.setMessage(context.getString(R.string.pgw_settings_updatelauncher_install_prompt_message, apkFile.getAbsolutePath()))
.setConfirmListener(R.string.pgw_settings_updatelauncher_install, customView -> {
installApk(apkFile);
return true;
})
.setCancelListener(R.string.alertdialog_cancel, customView -> true)
.setCancelable(false)
.build()
.show();
} else {
apkFile.delete();
apkVersionFile.delete();
checkForUpdates();
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
}
} else if (apkFile.exists() || apkVersionFile.exists()) {
if (apkFile.exists()) apkFile.delete();
if (apkVersionFile.exists()) apkVersionFile.delete();
checkForUpdates();
} else {
checkForUpdates();
}
}

private void checkForUpdates() {
new GetLatestReleaseTask().execute(GITHUB_API);
public void checkForUpdates(boolean ignore) {
new GetLatestReleaseTask(ignore).execute(GITHUB_API);
}

private class GetLatestReleaseTask extends AsyncTask<String, Void, JSONObject> {
private boolean ignore;

public GetLatestReleaseTask(boolean ignore) {
this.ignore = ignore;
}

@Override
protected JSONObject doInBackground(String... urls) {
OkHttpClient client = new OkHttpClient();
Expand All @@ -115,8 +86,8 @@ protected void onPostExecute(JSONObject result) {
int remoteVersionCode = Integer.parseInt(result.getString("tag_name").replaceAll("[^\\d]", ""));
String version = String.valueOf(localVersionCode);
if (remoteVersionCode > localVersionCode) {
showUpdateDialog(result);
} else {
checkCachedApk(result, ignore);
} else if (!ignore) {
Toast.makeText(context, context.getString(R.string.pgw_settings_updatelauncher_updated, version), Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Expand All @@ -126,27 +97,100 @@ protected void onPostExecute(JSONObject result) {
}
}

private void showUpdateDialog(JSONObject releaseInfo) {
private void checkCachedApk(JSONObject releaseInfo, boolean ignore) {
boolean INGORE = false;
String tagName;
String versionName;
String releaseNotes;
if (!dir.exists()) {
dir.mkdirs();
}
File apkFile = new File(dir, "cache.apk");
File apkVersionFile = new File(dir, "apk_version");
File ignoreVersionFile = new File(dir, "ignore_version");

try {
String tagName = releaseInfo.getString("tag_name");
String versionName = releaseInfo.getString("name");
String releaseNotes = releaseInfo.getString("body");
String archModel = getArchModel();

CustomDialog.Builder builder = new CustomDialog.Builder(context);
builder.setTitle(context.getString(R.string.pgw_settings_updatelauncher_new_version, versionName))
.setScrollMessage(releaseNotes)
.setConfirmListener(R.string.pgw_settings_updatelauncher_update, customView -> {
showDownloadSourceDialog(tagName, versionName, archModel);
return true;
})
.setButton1Listener(context.getString(R.string.pgw_settings_updatelauncher_cancel), customView -> true)
.setCancelListener(R.string.alertdialog_cancel, customView -> true)
.build()
.show();
tagName = releaseInfo.getString("tag_name");
versionName = releaseInfo.getString("name");
releaseNotes = releaseInfo.getString("body");
} catch (JSONException e) {
e.printStackTrace();
return;
}

if (ignoreVersionFile.exists()) {
try {
String savedIgnoreVersion = new String(java.nio.file.Files.readAllBytes(ignoreVersionFile.toPath()));
String localIgnoreVersion = DEFAULT_PREF.getString("ignoreVersion", null);
if (savedIgnoreVersion.equals(localIgnoreVersion) && savedIgnoreVersion.equals(tagName)) {
if (ignore) INGORE = true;
} else {
ignoreVersionFile.delete();
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
}
}

if (apkFile.exists() && apkVersionFile.exists()) {
try {
String savedTagName = new String(java.nio.file.Files.readAllBytes(apkVersionFile.toPath()));
int savedVersionCode = Integer.parseInt(savedTagName.replaceAll("[^\\d]", ""));
int releaseVersionCode = Integer.parseInt(tagName.replaceAll("[^\\d]", ""));
if (savedVersionCode > localVersionCode && savedVersionCode >= releaseVersionCode) {
new CustomDialog.Builder(context)
.setTitle(context.getString(R.string.pgw_settings_updatelauncher_install_prompt_title))
.setMessage(context.getString(R.string.pgw_settings_updatelauncher_install_prompt_message, apkFile.getAbsolutePath()))
.setConfirmListener(R.string.pgw_settings_updatelauncher_install, customView -> {
installApk(apkFile);
return true;
})
.setCancelListener(R.string.alertdialog_cancel, customView -> true)
.setCancelable(false)
.build()
.show();
} else {
apkFile.delete();
apkVersionFile.delete();
if (!INGORE) showUpdateDialog(tagName, versionName, releaseNotes);
}
} catch (IOException | NumberFormatException e) {
e.printStackTrace();
}
} else if (apkFile.exists() || apkVersionFile.exists()) {
if (apkFile.exists()) apkFile.delete();
if (apkVersionFile.exists()) apkVersionFile.delete();
if (!INGORE) showUpdateDialog(tagName, versionName, releaseNotes);
} else {
if (!INGORE) showUpdateDialog(tagName, versionName, releaseNotes);
}
}

private void showUpdateDialog(String tagName, String versionName, String releaseNotes) {
String archModel = getArchModel();

new CustomDialog.Builder(context)
.setTitle(context.getString(R.string.pgw_settings_updatelauncher_new_version, versionName))
.setScrollMessage(releaseNotes)
.setConfirmListener(R.string.pgw_settings_updatelauncher_update, customView -> {
showDownloadSourceDialog(tagName, versionName, archModel);
return true;
})
.setButton1Listener(context.getString(R.string.pgw_settings_updatelauncher_cancel), customView -> {
File ignoreVersion = new File(dir, "ignore_version");
try (FileOutputStream OutputStream = new FileOutputStream(ignoreVersion)) {
OutputStream.write(tagName.getBytes());
OutputStream.flush();
DEFAULT_PREF.edit().putString("ignoreVersion", tagName).apply();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
})
.setCancelListener(R.string.alertdialog_cancel, customView -> true)
.build()
.show();
}

private void showDownloadSourceDialog(String tagName, String versionName, String archModel) {
Expand Down Expand Up @@ -210,7 +254,6 @@ protected File doInBackground(String... urls) {
try {
Response response = client.newCall(request).execute();
if (response.isSuccessful()) {
File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
apkFile = new File(dir, "cache.apk");
File apkVersionFile = new File(dir, "apk_version");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ protected void onCreate(Bundle savedInstanceState) {
mProgressLayout.observe(ProgressLayout.DOWNLOAD_VERSION_LIST);
// 初始化并调用 UpdateLauncher 进行更新检查
UpdateLauncher updateLauncher = new UpdateLauncher(this);
updateLauncher.checkCachedApk();
updateLauncher.checkForUpdates(true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package net.kdt.pojavlaunch.prefs.screens;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.os.Bundle;

import android.widget.Toast;
import androidx.preference.Preference;

import com.firefly.feature.UpdateLauncher;
import com.kdt.pickafile.FileListView;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

public class LauncherPreferenceLauncherFragment extends LauncherPreferenceFragment {
@Override
public void onCreatePreferences(Bundle b, String str) {
addPreferencesFromResource(R.xml.pref_launcher);

findPreference("control_mouse_setting").setOnPreferenceClickListener((preference) -> {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
return true;
});

findPreference("control_mouse_remove").setOnPreferenceClickListener((preference) -> {
File file = new File(Tools.DIR_GAME_HOME, "mouse");
if (file.exists()) {
file.delete();
}
Toast.makeText(getContext(), R.string.notif_mouse1, Toast.LENGTH_SHORT).show();
return true;
});

Preference updatePreference = requirePreference("update_launcher");
updatePreference.setOnPreferenceClickListener(preference -> {
UpdateLauncher updateLauncher = new UpdateLauncher(getContext());
updateLauncher.checkForUpdates(false);
return true;
});

}

@Override
public void onActivityResult(
int requestCode, int resultCode, final Intent data) {
if (resultCode != Activity.RESULT_OK) {
// Handle error
return;
}

if (requestCode == 1) {// Get photo picker response for single select.
Uri currentUri = data.getData();
try {
File file = new File(Tools.DIR_GAME_HOME, "mouse");
if (file.exists()) {
file.delete();
}

InputStream stream1 = getContext().getContentResolver().openInputStream(currentUri);
FileOutputStream stream = new FileOutputStream(file);

IOUtils.copy(stream1, stream);
stream.close();
stream1.close();
Toast.makeText(getContext(), R.string.notif_mouse, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
package net.kdt.pojavlaunch.prefs.screens;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.os.Bundle;

import android.widget.Toast;
import androidx.preference.Preference;

import com.kdt.pickafile.FileListView;
import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

public class LauncherPreferenceMiscellaneousFragment extends LauncherPreferenceFragment {
@Override
Expand All @@ -30,50 +15,5 @@ public void onCreatePreferences(Bundle b, String str) {
if(!Tools.checkVulkanSupport(driverPreference.getContext().getPackageManager())) {
driverPreference.setVisible(false);
}

findPreference("control_mouse_setting").setOnPreferenceClickListener((preference) -> {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 1);
return true;
});

findPreference("control_mouse_remove").setOnPreferenceClickListener((preference) -> {
File file = new File(Tools.DIR_GAME_HOME, "mouse");
if (file.exists()) {
file.delete();
}
Toast.makeText(getContext(), R.string.notif_mouse1, Toast.LENGTH_SHORT).show();
return true;
});

}
@Override
public void onActivityResult(
int requestCode, int resultCode, final Intent data) {
if (resultCode != Activity.RESULT_OK) {
// Handle error
return;
}

if (requestCode == 1) {// Get photo picker response for single select.
Uri currentUri = data.getData();
try {
File file = new File(Tools.DIR_GAME_HOME, "mouse");
if (file.exists()) {
file.delete();
}

InputStream stream1 = getContext().getContentResolver().openInputStream(currentUri);
FileOutputStream stream = new FileOutputStream(file);

IOUtils.copy(stream1, stream);
stream.close();
stream1.close();
Toast.makeText(getContext(), R.string.notif_mouse, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
Loading

0 comments on commit ec574f8

Please sign in to comment.