Skip to content

Commit

Permalink
actions: refactor MyPlanet model to kotlin (fixes #2743) (#2747)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored Dec 1, 2023
1 parent a7ad168 commit 158f21c
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 190 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> 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);
Expand Down
182 changes: 0 additions & 182 deletions app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.java

This file was deleted.

112 changes: 112 additions & 0 deletions app/src/main/java/org/ole/planet/myplanet/model/MyPlanet.kt
Original file line number Diff line number Diff line change
@@ -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`)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}

Expand Down

0 comments on commit 158f21c

Please sign in to comment.