Skip to content

Commit

Permalink
Performance fix
Browse files Browse the repository at this point in the history
ItooInts now will not recollect int/codes until the app is updated, to increase efficiency.
  • Loading branch information
XomaDev committed Nov 22, 2022
1 parent 96845f1 commit f779255
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/xyz/kumaraswamy/itoox/ItooInt.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import com.google.appinventor.components.runtime.Form;
import gnu.expr.ModuleMethod;
Expand Down Expand Up @@ -53,9 +55,38 @@ public String getScreenPkgName(String name) throws JSONException {

public static void saveIntStuff(Form form, String refScreen) throws Throwable {
if (form instanceof InstanceForm.FormX) return;
saveIntsNames(form, getSharedPreference(form, refScreen, 0));
saveComponentNames(form, getSharedPreference(form, refScreen, 1));
saveScreenPkgNames(form, getSharedPreference(form, "", 2));

PackageManager pm = form.getPackageManager();
String pkgName = form.getPackageName();
PackageInfo pkgInfo = null;
try {
pkgInfo = pm.getPackageInfo(pkgName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
assert pkgInfo != null;
long lastUpdateTime = pkgInfo.lastUpdateTime;

SharedPreferences prefs = getSharedPreference(form, refScreen, 3);

boolean isChanged = !prefs.contains("lastUpdateTime")
|| prefs.getLong("lastUpdateTime", -1)
!= lastUpdateTime;

if (isChanged) {
// saving components, ints, pkgNames
// is a very intensive task, since they/it
// also involves reflection, so we only save them once
// when the app starts or when the app is updated
saveIntsNames(form, getSharedPreference(form, refScreen, 0));
saveComponentNames(form, getSharedPreference(form, refScreen, 1));
saveScreenPkgNames(form, getSharedPreference(form, "", 2));

prefs.edit().putBoolean("saved", true).commit();
prefs.edit().putLong("lastUpdateTime", lastUpdateTime).commit();
return;
}
Log.d("ItooCreator", "Skipping save, already saved ints");
}

private static void saveScreenPkgNames(Form form, SharedPreferences prefs) throws JSONException {
Expand Down Expand Up @@ -88,11 +119,11 @@ private static void saveComponentNames(Form form, SharedPreferences prefs) throw

private static SharedPreferences getSharedPreference(Form form, String refScreen, int type) {
return form.getSharedPreferences(
"ItooInt_" + type + "_" + refScreen, Context.MODE_PRIVATE);
"ItooInt_" + type + "_" + refScreen, Context.MODE_PRIVATE);
}

private static void saveIntsNames(Form form, SharedPreferences prefs)
throws Throwable {
throws Throwable {
Editor editor = prefs.edit();
Field field = form.getClass().getField(VARS_FIELD_NAME);
LList variables = (LList) field.get(form);
Expand All @@ -105,7 +136,7 @@ private static void saveIntsNames(Form form, SharedPreferences prefs)
String name = ((Symbol) asPair.get(0)).getName();
if (name.startsWith(PROCEDURE_PREFIX)) {
ModuleMethod method = (ModuleMethod)
((ModuleMethod) asPair.get(1)).apply0();
((ModuleMethod) asPair.get(1)).apply0();
int selector = method.selector;
Log.d("ItooCreator", "Put(" + name + ", " + selector + ")");
editor.putInt(name.substring(PROCEDURE_PREFIX.length()), selector);
Expand Down

0 comments on commit f779255

Please sign in to comment.