From a143c3c923ed8732b28a3b0eb769d103710febdc Mon Sep 17 00:00:00 2001 From: Vishal Nehra Date: Mon, 15 Jul 2024 00:49:13 +0530 Subject: [PATCH 1/2] Show usage hours in most used apps analyisis --- .../ui/analyse/ReviewAnalysisAdapter.kt | 7 +++++++ .../home_page/ui/files/FilesViewModel.kt | 19 +++++++++---------- .../home_page/ui/files/MediaFileInfo.kt | 8 +++++--- .../com/amaze/fileutilities/utilis/Utils.kt | 7 +++++++ app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt b/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt index 908dc356..4ec92041 100644 --- a/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt +++ b/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt @@ -30,7 +30,9 @@ import com.amaze.fileutilities.home_page.ui.files.MediaInfoRecyclerViewHolder import com.amaze.fileutilities.utilis.AbstractMediaFilesAdapter import com.amaze.fileutilities.utilis.EmptyViewHolder import com.amaze.fileutilities.utilis.FileUtils +import com.amaze.fileutilities.utilis.Utils import java.lang.ref.WeakReference +import java.util.concurrent.TimeUnit class ReviewAnalysisAdapter( val context: Context, @@ -99,6 +101,11 @@ class ReviewAnalysisAdapter( } else { holder.infoSummary.text = totalSize } + } else if (analysisType == ReviewImagesFragment.TYPE_MOST_USED_APPS) { + val timeForeground = Utils.convertMillisToHoursMinutes(this.mediaFileInfo?.extraInfo?.apkMetaData?.timeForeground ?: 0L) + val size = this.mediaFileInfo?.getFormattedSize(context) + holder.infoSummary.text = "${context.getString(R.string.size)}: $size" + holder.extraInfo.text = "${context.getString(R.string.usage)}: $timeForeground" } else { holder.infoSummary.text = this.mediaFileInfo?.getFormattedSize(context) } diff --git a/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt b/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt index acc28eb7..26489952 100644 --- a/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt +++ b/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt @@ -1665,7 +1665,7 @@ class FilesViewModel(val applicationContext: Application) : !usageStatsPackages.contains(it.first.packageName) }?.mapNotNull { MediaFileInfo.fromApplicationInfo(applicationContext, it.first, it.second) - } + }?.sortedByDescending { it.longSize } unusedAppsLiveData?.postValue(unusedAppsList?.let { ArrayList(it) }) } } @@ -1742,14 +1742,12 @@ class FilesViewModel(val applicationContext: Application) : usageStats.filter { it.lastTimeUsed != 0L }.forEach { - if (!freqMap.contains(it.packageName)) { - freqMap[it.packageName] = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) - it.totalTimeVisible else it.totalTimeInForeground - } else { - freqMap[it.packageName] = freqMap[it.packageName]!! + - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) - it.totalTimeVisible else it.totalTimeInForeground + var timeForeground = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + it.totalTimeVisible else it.totalTimeInForeground + if (freqMap[it.packageName] != null) { + timeForeground += freqMap[it.packageName]!! } + freqMap[it.packageName] = timeForeground } val mostUsedAppsListRaw = arrayListOf() freqMap.entries.stream() @@ -1765,7 +1763,8 @@ class FilesViewModel(val applicationContext: Application) : }?.let { MediaFileInfo.fromApplicationInfo( applicationContext, - it.first, it.second + it.first, it.second, + timeForeground = freqMap[appName] ?: 0L, )?.let { mediaFileInfo -> mostUsedApps.add(mediaFileInfo) @@ -2365,7 +2364,7 @@ class FilesViewModel(val applicationContext: Application) : Utils.applicationIsGame(it.first) }?.mapNotNull { MediaFileInfo.fromApplicationInfo(applicationContext, it.first, it.second) - }?.let { + }?.sortedByDescending { it.longSize }?.let { gamesInstalledLiveData?.postValue(ArrayList(it)) } } diff --git a/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/MediaFileInfo.kt b/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/MediaFileInfo.kt index f6ce9db8..6dbbc671 100644 --- a/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/MediaFileInfo.kt +++ b/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/MediaFileInfo.kt @@ -150,7 +150,8 @@ data class MediaFileInfo( context: Context, applicationInfo: ApplicationInfo, packageInfo: PackageInfo?, - sizeDiff: Long = -1 + sizeDiff: Long = -1, + timeForeground: Long = 0L ): MediaFileInfo? { if (applicationInfo.sourceDir == null) { return null @@ -173,7 +174,7 @@ data class MediaFileInfo( applicationInfo.packageName, packageManager.getApplicationIcon(applicationInfo.packageName), Utils.getApplicationNetworkBytes(context, applicationInfo), - sizeDiff + sizeDiff, timeForeground ) ) mediaFileInfo.extraInfo = extraInfo @@ -409,7 +410,8 @@ data class MediaFileInfo( val packageName: String, val drawable: Drawable?, val networkBytes: Long, - val sizeDiff: Long = -1 + val sizeDiff: Long = -1, + val timeForeground: Long = 0 ) data class ExtraMetaData(val checksum: String) data class Playlist(var id: Long, var name: String) diff --git a/app/src/main/java/com/amaze/fileutilities/utilis/Utils.kt b/app/src/main/java/com/amaze/fileutilities/utilis/Utils.kt index 335fe70c..d09d2197 100644 --- a/app/src/main/java/com/amaze/fileutilities/utilis/Utils.kt +++ b/app/src/main/java/com/amaze/fileutilities/utilis/Utils.kt @@ -1543,6 +1543,13 @@ class Utils { ) } + fun convertMillisToHoursMinutes(millis: Long): String { + val hours = TimeUnit.MILLISECONDS.toHours(millis) + val minutes = TimeUnit.MILLISECONDS.toMinutes(millis) % 60 + + return String.format("%02d:%02d", hours, minutes) + } + private fun findApplicationInfoSizeFallback(applicationInfo: ApplicationInfo): Long { var cacheSize = 0L File(applicationInfo.sourceDir).parentFile?.let { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 5e1156bb..31da1269 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1043,4 +1043,6 @@ Sort By Terms and Conditions + + Usage (hrs) From ad783c025c2e334a7db8a9ef98a3c73c2f79d04f Mon Sep 17 00:00:00 2001 From: Vishal Nehra Date: Mon, 15 Jul 2024 00:59:46 +0530 Subject: [PATCH 2/2] Apply spotless --- .../home_page/ui/analyse/ReviewAnalysisAdapter.kt | 12 ++++++++---- .../home_page/ui/files/FilesViewModel.kt | 2 +- .../image_viewer/ImageViewerActivity.kt | 3 +-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt b/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt index 4ec92041..9f6349e1 100644 --- a/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt +++ b/app/src/main/java/com/amaze/fileutilities/home_page/ui/analyse/ReviewAnalysisAdapter.kt @@ -32,7 +32,6 @@ import com.amaze.fileutilities.utilis.EmptyViewHolder import com.amaze.fileutilities.utilis.FileUtils import com.amaze.fileutilities.utilis.Utils import java.lang.ref.WeakReference -import java.util.concurrent.TimeUnit class ReviewAnalysisAdapter( val context: Context, @@ -102,10 +101,15 @@ class ReviewAnalysisAdapter( holder.infoSummary.text = totalSize } } else if (analysisType == ReviewImagesFragment.TYPE_MOST_USED_APPS) { - val timeForeground = Utils.convertMillisToHoursMinutes(this.mediaFileInfo?.extraInfo?.apkMetaData?.timeForeground ?: 0L) - val size = this.mediaFileInfo?.getFormattedSize(context) + val timeForeground = + Utils.convertMillisToHoursMinutes( + this.mediaFileInfo?.extraInfo + ?.apkMetaData?.timeForeground ?: 0L + ) + val size = this.mediaFileInfo?.getFormattedSize(context) holder.infoSummary.text = "${context.getString(R.string.size)}: $size" - holder.extraInfo.text = "${context.getString(R.string.usage)}: $timeForeground" + holder.extraInfo.text = "${context.getString(R.string.usage)}: " + + "$timeForeground" } else { holder.infoSummary.text = this.mediaFileInfo?.getFormattedSize(context) } diff --git a/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt b/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt index 26489952..dee633c5 100644 --- a/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt +++ b/app/src/main/java/com/amaze/fileutilities/home_page/ui/files/FilesViewModel.kt @@ -1742,7 +1742,7 @@ class FilesViewModel(val applicationContext: Application) : usageStats.filter { it.lastTimeUsed != 0L }.forEach { - var timeForeground = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) + var timeForeground = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) it.totalTimeVisible else it.totalTimeInForeground if (freqMap[it.packageName] != null) { timeForeground += freqMap[it.packageName]!! diff --git a/app/src/main/java/com/amaze/fileutilities/image_viewer/ImageViewerActivity.kt b/app/src/main/java/com/amaze/fileutilities/image_viewer/ImageViewerActivity.kt index 8c9eeccd..d35e6c65 100644 --- a/app/src/main/java/com/amaze/fileutilities/image_viewer/ImageViewerActivity.kt +++ b/app/src/main/java/com/amaze/fileutilities/image_viewer/ImageViewerActivity.kt @@ -29,10 +29,9 @@ import com.amaze.fileutilities.R import com.amaze.fileutilities.databinding.GenericPagerViewerActivityBinding import com.amaze.fileutilities.utilis.Utils.Companion.showProcessingDialog import com.amaze.fileutilities.utilis.showToastInCenter -import java.io.File import org.slf4j.Logger import org.slf4j.LoggerFactory - +import java.io.File class ImageViewerActivity : PermissionsActivity() {