From 158f21c8ec63444b31fabe469e5077cc58947ea2 Mon Sep 17 00:00:00 2001 From: Gideon Okuro Date: Fri, 1 Dec 2023 20:13:38 +0300 Subject: [PATCH] actions: refactor `MyPlanet` model to kotlin (fixes #2743) (#2747) Co-authored-by: dogi --- app/build.gradle | 4 +- .../planet/myplanet/datamanager/Service.java | 6 +- .../ole/planet/myplanet/model/MyPlanet.java | 182 ------------------ .../org/ole/planet/myplanet/model/MyPlanet.kt | 112 +++++++++++ .../myplanet/service/AutoSyncWorker.java | 2 +- .../myplanet/utilities/DialogUtils.java | 4 +- 6 files changed, 120 insertions(+), 190 deletions(-) delete mode 100644 app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.java create mode 100644 app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.kt diff --git a/app/build.gradle b/app/build.gradle index c18c32dced..eb8df36f73 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,8 +9,8 @@ android { applicationId "org.ole.planet.myplanet" minSdkVersion 21 targetSdkVersion 34 - versionCode 1172 - versionName "0.11.72" + versionCode 1173 + versionName "0.11.73" ndkVersion '21.3.6528147' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" vectorDrawables.useSupportLibrary = true diff --git a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.java b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.java index 4d6962f715..65d22a1bdd 100644 --- a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.java +++ b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.java @@ -125,15 +125,15 @@ public void onResponse(Call call, Response response) vsn = vsn.replaceAll("\\.", ""); int apkVersion = Integer.parseInt(vsn.startsWith("0") ? vsn.replace("0", "") : vsn); int currentVersion = VersionUtils.getVersionCode(context); - if (Constants.showBetaFeature(KEY_UPGRADE_MAX, context) && p.getLatestapkcode() > currentVersion) { + if (Constants.showBetaFeature(KEY_UPGRADE_MAX, context) && p.latestapkcode > currentVersion) { callback.onUpdateAvailable(p, false); return; } if (apkVersion > currentVersion) { - callback.onUpdateAvailable(p, currentVersion >= p.getMinapkcode()); + callback.onUpdateAvailable(p, currentVersion >= p.minapkcode); return; } - if (currentVersion < p.getMinapkcode() && apkVersion < p.getMinapkcode()) { + if (currentVersion < p.minapkcode && apkVersion < p.minapkcode) { callback.onUpdateAvailable(p, true); } else { callback.onError("Planet up to date", false); diff --git a/app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.java b/app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.java deleted file mode 100644 index ad1f30208a..0000000000 --- a/app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.java +++ /dev/null @@ -1,182 +0,0 @@ -package org.ole.planet.myplanet.model; - -import static android.content.Context.MODE_PRIVATE; -import static org.ole.planet.myplanet.ui.sync.SyncActivity.PREFS_NAME; - -import android.app.usage.UsageStats; -import android.app.usage.UsageStatsManager; -import android.content.Context; -import android.content.SharedPreferences; -import android.os.Build; - -import androidx.annotation.RequiresApi; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -import org.ole.planet.myplanet.MainApplication; -import org.ole.planet.myplanet.utilities.NetworkUtils; -import org.ole.planet.myplanet.utilities.VersionUtils; - -import java.io.Serializable; -import java.util.Calendar; -import java.util.Date; -import java.util.List; - -public class MyPlanet implements Serializable { - private String planetVersion; - - private String latestapk; - - private String minapk; - - private int minapkcode; - - private int latestapkcode; - - private String apkpath; - - private String appname; - - private String localapkpath; - - public String getPlanetVersion() { - return planetVersion; - } - - public void setPlanetVersion(String planetVersion) { - this.planetVersion = planetVersion; - } - - public String getLatestapk() { - return latestapk; - } - - public void setLatestapk(String latestapk) { - this.latestapk = latestapk; - } - - public String getMinapk() { - return minapk; - } - - public void setMinapk(String minapk) { - this.minapk = minapk; - } - - public String getApkpath() { - return apkpath; - } - - public void setApkpath(String apkpath) { - this.apkpath = apkpath; - } - - public String getAppname() { - return appname; - } - - public void setAppname(String appname) { - this.appname = appname; - } - - public int getMinapkcode() { - return minapkcode; - } - - public void setMinapkcode(int minapkcode) { - this.minapkcode = minapkcode; - } - - public int getLatestapkcode() { - return latestapkcode; - } - - public void setLatestapkcode(int latestapkcode) { - this.latestapkcode = latestapkcode; - } - - public String getLocalapkpath() { - return localapkpath; - } - - public void setLocalapkpath(String localapkpath) { - this.localapkpath = localapkpath; - } - - @Override - public String toString() { - return appname; - } - - public static JsonObject getMyPlanetActivities(Context context, SharedPreferences pref, RealmUserModel model) { - JsonObject postJSON = new JsonObject(); - SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE); - - MyPlanet planet = new Gson().fromJson(preferences.getString("versionDetail", ""), MyPlanet.class); - if (planet != null) postJSON.addProperty("planetVersion", planet.getPlanetVersion()); - postJSON.addProperty("_id", VersionUtils.getAndroidId(MainApplication.context) + "@" + NetworkUtils.getUniqueIdentifier()); - postJSON.addProperty("last_synced", pref.getLong("LastSync", 0)); - postJSON.addProperty("parentCode", model.getParentCode()); - postJSON.addProperty("createdOn", model.getPlanetCode()); - postJSON.addProperty("type", "usages"); - postJSON.add("usages", getTabletUsages(context, pref)); - return postJSON; - } - - - public static JsonObject getNormalMyPlanetActivities(Context context, SharedPreferences pref, RealmUserModel model) { - JsonObject postJSON = new JsonObject(); - SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE); - - MyPlanet planet = new Gson().fromJson(preferences.getString("versionDetail", ""), MyPlanet.class); - if (planet != null) postJSON.addProperty("planetVersion", planet.getPlanetVersion()); - postJSON.addProperty("last_synced", pref.getLong("LastSync", 0)); - postJSON.addProperty("parentCode", model.getParentCode()); - postJSON.addProperty("createdOn", model.getPlanetCode()); - postJSON.addProperty("version", VersionUtils.getVersionCode(context)); - postJSON.addProperty("versionName", VersionUtils.getVersionName(context)); - postJSON.addProperty("androidId", NetworkUtils.getUniqueIdentifier()); - postJSON.addProperty("uniqueAndroidId", VersionUtils.getAndroidId(MainApplication.context)); - postJSON.addProperty("customDeviceName", NetworkUtils.getCustomDeviceName(context)); - postJSON.addProperty("deviceName", NetworkUtils.getDeviceName()); - postJSON.addProperty("time", new Date().getTime()); - postJSON.addProperty("type", "sync"); - return postJSON; - } - - public static JsonArray getTabletUsages(Context context, SharedPreferences pref) { - Calendar cal = Calendar.getInstance(); - SharedPreferences settings = MainApplication.context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE); - cal.setTimeInMillis(settings.getLong("lastUsageUploaded", 0)); - JsonArray arr = new JsonArray(); - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) { - UsageStatsManager mUsageStatsManager = (UsageStatsManager) MainApplication.context.getSystemService(Context.USAGE_STATS_SERVICE); - List queryUsageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.getTimeInMillis(), System.currentTimeMillis()); - for (UsageStats s : queryUsageStats) { - addStats(s, arr, context, pref); - } - } - return arr; - } - - @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - private static void addStats(UsageStats s, JsonArray arr, Context context, SharedPreferences pref) { - if (s.getPackageName().equals(MainApplication.context.getPackageName())) { - JsonObject object = new JsonObject(); - object.addProperty("lastTimeUsed", s.getLastTimeUsed() > 0 ? s.getLastTimeUsed() : 0); - object.addProperty("firstTimeUsed", s.getFirstTimeStamp() > 0 ? s.getLastTimeStamp() : 0); - object.addProperty("totalForegroundTime", s.getTotalTimeInForeground()); - long totalUsed = s.getLastTimeUsed() - s.getFirstTimeStamp(); - object.addProperty("totalUsed", totalUsed > 0 ? totalUsed : 0); - object.addProperty("version", VersionUtils.getVersionCode(context)); - object.addProperty("versionName", VersionUtils.getVersionName(context)); - object.addProperty("androidId", NetworkUtils.getUniqueIdentifier()); - object.addProperty("customDeviceName", NetworkUtils.getCustomDeviceName(context)); - object.addProperty("deviceName", NetworkUtils.getDeviceName()); - object.addProperty("time", new Date().getTime()); - arr.add(object); - } - } -} diff --git a/app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.kt b/app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.kt new file mode 100644 index 0000000000..c937701238 --- /dev/null +++ b/app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.kt @@ -0,0 +1,112 @@ +package org.ole.planet.myplanet.model + +import android.app.usage.UsageStats +import android.app.usage.UsageStatsManager +import android.content.Context +import android.content.SharedPreferences +import android.os.Build +import androidx.annotation.RequiresApi +import com.google.gson.Gson +import com.google.gson.JsonArray +import com.google.gson.JsonObject +import org.ole.planet.myplanet.MainApplication +import org.ole.planet.myplanet.ui.sync.SyncActivity +import org.ole.planet.myplanet.utilities.NetworkUtils +import org.ole.planet.myplanet.utilities.VersionUtils +import java.io.Serializable +import java.util.Calendar +import java.util.Date + +class MyPlanet : Serializable { + @JvmField + var planetVersion: String? = null + @JvmField + var latestapk: String? = null + @JvmField + var minapk: String? = null + @JvmField + var minapkcode = 0 + @JvmField + var latestapkcode = 0 + @JvmField + var apkpath: String? = null + @JvmField + var appname: String? = null + @JvmField + var localapkpath: String? = null + override fun toString(): String { + return appname!! + } + + companion object { + @JvmStatic + fun getMyPlanetActivities(context: Context, pref: SharedPreferences, model: RealmUserModel): JsonObject { + val postJSON = JsonObject() + val preferences = context.getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE) + val planet = Gson().fromJson(preferences.getString("versionDetail", ""), MyPlanet::class.java) + if (planet != null) postJSON.addProperty("planetVersion", planet.planetVersion) + postJSON.addProperty("_id", VersionUtils.getAndroidId(MainApplication.context) + "@" + NetworkUtils.getUniqueIdentifier()) + postJSON.addProperty("last_synced", pref.getLong("LastSync", 0)) + postJSON.addProperty("parentCode", model.parentCode) + postJSON.addProperty("createdOn", model.planetCode) + postJSON.addProperty("type", "usages") + postJSON.add("usages", getTabletUsages(context, pref)) + return postJSON + } + + @JvmStatic + fun getNormalMyPlanetActivities(context: Context, pref: SharedPreferences, model: RealmUserModel): JsonObject { + val postJSON = JsonObject() + val preferences = context.getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE) + val planet = Gson().fromJson(preferences.getString("versionDetail", ""), MyPlanet::class.java) + if (planet != null) postJSON.addProperty("planetVersion", planet.planetVersion) + postJSON.addProperty("last_synced", pref.getLong("LastSync", 0)) + postJSON.addProperty("parentCode", model.parentCode) + postJSON.addProperty("createdOn", model.planetCode) + postJSON.addProperty("version", VersionUtils.getVersionCode(context)) + postJSON.addProperty("versionName", VersionUtils.getVersionName(context)) + postJSON.addProperty("androidId", NetworkUtils.getUniqueIdentifier()) + postJSON.addProperty("uniqueAndroidId", VersionUtils.getAndroidId(MainApplication.context)) + postJSON.addProperty("customDeviceName", NetworkUtils.getCustomDeviceName(context)) + postJSON.addProperty("deviceName", NetworkUtils.getDeviceName()) + postJSON.addProperty("time", Date().time) + postJSON.addProperty("type", "sync") + return postJSON + } + + @JvmStatic + fun getTabletUsages(context: Context, pref: SharedPreferences): JsonArray { + val cal = Calendar.getInstance() + val settings = MainApplication.context.getSharedPreferences(SyncActivity.PREFS_NAME, Context.MODE_PRIVATE) + cal.timeInMillis = settings.getLong("lastUsageUploaded", 0) + val arr = JsonArray() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { + val mUsageStatsManager = MainApplication.context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager + val queryUsageStats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, cal.timeInMillis, System.currentTimeMillis()) + for (s in queryUsageStats) { + addStats(s, arr, context, pref) + } + } + return arr + } + + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + private fun addStats(s: UsageStats, arr: JsonArray, context: Context, pref: SharedPreferences) { + if (s.packageName == MainApplication.context.packageName) { + val `object` = JsonObject() + `object`.addProperty("lastTimeUsed", if (s.lastTimeUsed > 0) s.lastTimeUsed else 0) + `object`.addProperty("firstTimeUsed", if (s.firstTimeStamp > 0) s.lastTimeStamp else 0) + `object`.addProperty("totalForegroundTime", s.totalTimeInForeground) + val totalUsed = s.lastTimeUsed - s.firstTimeStamp + `object`.addProperty("totalUsed", if (totalUsed > 0) totalUsed else 0) + `object`.addProperty("version", VersionUtils.getVersionCode(context)) + `object`.addProperty("versionName", VersionUtils.getVersionName(context)) + `object`.addProperty("androidId", NetworkUtils.getUniqueIdentifier()) + `object`.addProperty("customDeviceName", NetworkUtils.getCustomDeviceName(context)) + `object`.addProperty("deviceName", NetworkUtils.getDeviceName()) + `object`.addProperty("time", Date().time) + arr.add(`object`) + } + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/ole/planet/myplanet/service/AutoSyncWorker.java b/app/src/main/java/org/ole/planet/myplanet/service/AutoSyncWorker.java index 5e7252ce99..31ebccfd35 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/AutoSyncWorker.java +++ b/app/src/main/java/org/ole/planet/myplanet/service/AutoSyncWorker.java @@ -104,7 +104,7 @@ public void onSyncFailed(String msg) { @Override public void onUpdateAvailable(MyPlanet info, boolean cancelable) { if (Constants.showBetaFeature(Constants.KEY_AUTOUPDATE, context)) { - DialogUtils.startDownloadUpdate(context, Utilities.getApkUpdateUrl(info.getLocalapkpath()), null); + DialogUtils.startDownloadUpdate(context, Utilities.getApkUpdateUrl(info.localapkpath), null); } } diff --git a/app/src/main/java/org/ole/planet/myplanet/utilities/DialogUtils.java b/app/src/main/java/org/ole/planet/myplanet/utilities/DialogUtils.java index a1b1ae66ee..352c1e9e7c 100644 --- a/app/src/main/java/org/ole/planet/myplanet/utilities/DialogUtils.java +++ b/app/src/main/java/org/ole/planet/myplanet/utilities/DialogUtils.java @@ -90,9 +90,9 @@ public static AlertDialog getAlertDialog(Context context, String title, View v) public static AlertDialog.Builder getUpdateDialog(Context context, MyPlanet info, ProgressDialog progressDialog) { return new AlertDialog.Builder(context).setTitle(R.string.new_version_of_my_planet_available).setMessage(R.string.download_first_to_continue).setNeutralButton(R.string.upgrade_local, (dialogInterface, i) -> { - startDownloadUpdate(context, Utilities.getApkUpdateUrl(info.getLocalapkpath()), progressDialog); + startDownloadUpdate(context, Utilities.getApkUpdateUrl(info.localapkpath), progressDialog); }).setPositiveButton(R.string.upgrade, (dialogInterface, i) -> { - startDownloadUpdate(context, info.getApkpath(), progressDialog); + startDownloadUpdate(context, info.apkpath, progressDialog); }); }