OpenDelta: Get rid of more reflection

Just... Why.... Ew.
Also minor improvement to some random code
This commit is contained in:
Ido Ben-Hur 2023-02-27 14:58:51 +02:00
parent 0daa4ad9ef
commit 8c51aa3565
No known key found for this signature in database
GPG Key ID: 0B827201D8C20BFE
3 changed files with 65 additions and 86 deletions

View File

@ -30,7 +30,6 @@ import android.os.SystemProperties;
import androidx.preference.PreferenceManager;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -72,32 +71,14 @@ public class Config {
private final String url_api_history;
private final String android_version;
/*
* Using reflection voodoo instead calling the hidden class directly, to
* dev/test outside of AOSP tree
*/
private String getProperty(Context context, String key) {
try {
Class<?> SystemProperties = context.getClassLoader().loadClass(
"android.os.SystemProperties");
Method get = SystemProperties.getMethod("get", String.class, String.class);
return (String) get.invoke(null, new Object[] { key, ""});
} catch (Exception e) {
// A lot of voodoo could go wrong here, return failure instead of
// crash
Logger.ex(e);
}
return null;
}
private Config(Context context) {
prefs = PreferenceManager.getDefaultSharedPreferences(context);
Resources res = context.getResources();
property_version = getProperty(context,
property_version = SystemProperties.get(
res.getString(R.string.property_version));
property_device = getProperty(context,
property_device = SystemProperties.get(
res.getString(R.string.property_device));
filename_base = String.format(Locale.ENGLISH,
res.getString(R.string.filename_base), property_version);
@ -124,7 +105,7 @@ public class Config {
url_api_history = String.format(
res.getString(R.string.url_api_history),
url_branch_name, property_device, property_device);
android_version = getProperty(context,
android_version = SystemProperties.get(
res.getString(R.string.android_version));
filename_base_prefix = String.format(Locale.ENGLISH,
res.getString(R.string.filename_base), android_version);

View File

@ -24,11 +24,6 @@ package eu.chainfire.opendelta;
import static android.Manifest.permission.POST_NOTIFICATIONS;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.app.ActionBar;
import android.app.AlertDialog;
@ -67,6 +62,11 @@ import android.widget.Toolbar;
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission;
import androidx.preference.PreferenceManager;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends Activity {
private static final int PERMISSIONS_REQUEST_MANAGE_EXTERNAL_STORAGE = 0;
private static final int PERMISSIONS_REQUEST_NOTIFICATION = 1;
@ -287,10 +287,11 @@ public class MainActivity extends Activity {
}
@Override
public void update(String state, Float progress,
public void update(String stateP, Float progress,
Long current, Long total, String filename,
Long ms, int errorCode) {
mHandler.post(() -> {
String state = stateP;
String title = "";
String sub = "";
String sub2 = "";
@ -324,41 +325,40 @@ public class MainActivity extends Activity {
}
// don't try this at home
if (state != null) {
title = tryGetResourceString("state_" + state);
// check for first start until check button has been pressed
// use a special title then - but only once
if (State.ACTION_NONE.equals(state)
&& !mPrefs.getBoolean(SettingsActivity.PREF_START_HINT_SHOWN, false)) {
title = getString(R.string.last_checked_never_title_new);
}
// don't spill for progress
if (!State.isProgressState(state)) {
Logger.d("onReceive state = " + state);
} else if (state.equals(mState)) {
// same progress state as before.
// save a lot of time by only updating progress
disableDataSpeed = State.ACTION_AB_FLASH.equals(state);
final ProgressGenerator pgen = new ProgressGenerator(
localCurrent,
localTotal,
localMS,
progress,
disableDataSpeed,
filename
);
mSub.setText(pgen.sub);
mSub.setSelected(true); // allow scrolling
mSub2.setText(pgen.sub2);
mProgressPercent.setText(pgen.progressPercent);
mProgressCurrent = Math.round(pgen.localCurrent);
mProgressMax = Math.round(pgen.localTotal);
mProgressEnabled = true;
handleProgressBar();
return;
}
mState = state;
if (state == null) state = State.ACTION_NONE;
title = tryGetResourceString("state_" + state);
// check for first start until check button has been pressed
// use a special title then - but only once
if (State.ACTION_NONE.equals(state)
&& !mPrefs.getBoolean(SettingsActivity.PREF_START_HINT_SHOWN, false)) {
title = getString(R.string.last_checked_never_title_new);
}
// don't spill for progress
if (!State.isProgressState(state)) {
Logger.d("onReceive state = " + state);
} else if (state.equals(mState)) {
// same progress state as before.
// save a lot of time by only updating progress
disableDataSpeed = State.ACTION_AB_FLASH.equals(state);
final ProgressGenerator pgen = new ProgressGenerator(
localCurrent,
localTotal,
localMS,
progress,
disableDataSpeed,
filename
);
mSub.setText(pgen.sub);
mSub.setSelected(true); // allow scrolling
mSub2.setText(pgen.sub2);
mProgressPercent.setText(pgen.progressPercent);
mProgressCurrent = Math.round(pgen.localCurrent);
mProgressMax = Math.round(pgen.localTotal);
mProgressEnabled = true;
handleProgressBar();
return;
}
mState = state;
mProgress.setIndeterminate(false);
String flashImage = null;

View File

@ -22,30 +22,6 @@
package eu.chainfire.opendelta;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.lang.StringBuilder;
import java.math.BigInteger;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipFile;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Notification;
@ -73,6 +49,28 @@ import android.os.SystemClock;
import android.os.UpdateEngine;
import android.preference.PreferenceManager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.StringBuilder;
import java.math.BigInteger;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class UpdateService extends Service implements OnSharedPreferenceChangeListener {
public static void start(Context context) {