From 34a3e55f76a5adc0f40123fccfaac1ed158c21c8 Mon Sep 17 00:00:00 2001 From: HeCodes2Much Date: Sat, 23 Nov 2024 10:55:03 +0000 Subject: [PATCH] Fix: Fixed the usage time Closes #571 --- .../mlauncher/helper/AppDetailsHelper.kt | 51 ++++++------ .../mlauncher/ui/FavoriteFragment.kt | 83 ++++++++++++------- 2 files changed, 80 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/github/droidworksstudio/mlauncher/helper/AppDetailsHelper.kt b/app/src/main/java/com/github/droidworksstudio/mlauncher/helper/AppDetailsHelper.kt index 753cd89f..e6a78fa2 100644 --- a/app/src/main/java/com/github/droidworksstudio/mlauncher/helper/AppDetailsHelper.kt +++ b/app/src/main/java/com/github/droidworksstudio/mlauncher/helper/AppDetailsHelper.kt @@ -1,5 +1,6 @@ package com.github.droidworksstudio.mlauncher.helper +import android.annotation.SuppressLint import android.app.usage.UsageStatsManager import android.content.Context import android.content.pm.ApplicationInfo @@ -20,7 +21,9 @@ object AppDetailsHelper { } } + @SuppressLint("NewApi") fun getUsageStats(context: Context, packageName: String): Long { + // Set calendar to midnight of today (start of the day) val calendar = Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 0) set(Calendar.MINUTE, 0) @@ -28,19 +31,25 @@ object AppDetailsHelper { set(Calendar.MILLISECOND, 0) } - val startTime = calendar.timeInMillis - val endTime = System.currentTimeMillis() + val startTime = calendar.timeInMillis // Midnight today + val endTime = System.currentTimeMillis() // Current time + + // Get UsageStatsManager system service + val usageStatsManager = context.getSystemService(Context.USAGE_STATS_SERVICE) as? UsageStatsManager + + // Query usage stats for the specific time range (startTime to endTime) + val usageStatsList = usageStatsManager?.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime) - val usageStatsManager = - context.getSystemService(Context.USAGE_STATS_SERVICE) as? UsageStatsManager - val usageStatsList = - usageStatsManager?.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime) var totalUsageTime: Long = 0 + // Iterate through the stats to get the specific package usage usageStatsList?.let { statsList -> for (usageStats in statsList) { if (usageStats.packageName == packageName) { - totalUsageTime = usageStats.totalTimeInForeground + // Use totalTimeInForeground for actual usage time + if (usageStats.totalTimeVisible > 0) { + totalUsageTime += usageStats.totalTimeVisible + } } } } @@ -48,45 +57,40 @@ object AppDetailsHelper { return totalUsageTime } + + @SuppressLint("NewApi") fun getTotalScreenTime(context: Context): Long { - // Get the current time + // Get the start of the current day (midnight) val calendar = Calendar.getInstance().apply { - set(Calendar.HOUR_OF_DAY, 0) // Set to the start of the day + set(Calendar.HOUR_OF_DAY, 0) set(Calendar.MINUTE, 0) set(Calendar.SECOND, 0) set(Calendar.MILLISECOND, 0) } - // Define the start and end time range for stats - val start = calendar.timeInMillis - val end = System.currentTimeMillis() + val startTime = calendar.timeInMillis + val endTime = System.currentTimeMillis() // Get the UsageStatsManager system service val usm = context.getSystemService(Context.USAGE_STATS_SERVICE) as UsageStatsManager - // Query usage stats for the given time range - val usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_BEST, start, end) + // Query usage stats for today (from midnight to current time) + val usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, startTime, endTime) if (usageStatsList.isEmpty()) { Log.w("getTotalScreenTime", "No usage stats available.") return 0L } - // Sort the stats based on last used time in descending order - usageStatsList.sortWith(compareByDescending { it.lastTimeUsed }) - // Calculate the total screen time for all apps (excluding the current app) var totalScreenTime: Long = 0 val packageName = context.packageName for (usageStats in usageStatsList) { if (usageStats.packageName != packageName) { - if (usageStats.totalTimeInForeground.toInt() != 0) { - Log.d( - "usageStatsList", - "App: ${usageStats.packageName}, Foreground time: ${usageStats.totalTimeInForeground}" - ) - totalScreenTime += usageStats.totalTimeInForeground + // Only consider apps with non-zero foreground time + if (usageStats.totalTimeVisible > 0) { + totalScreenTime += usageStats.totalTimeVisible } } } @@ -94,7 +98,6 @@ object AppDetailsHelper { return totalScreenTime } - fun formatMillisToHMS(millis: Long): String { val hours = millis / (1000 * 60 * 60) val minutes = (millis % (1000 * 60 * 60)) / (1000 * 60) diff --git a/app/src/main/java/com/github/droidworksstudio/mlauncher/ui/FavoriteFragment.kt b/app/src/main/java/com/github/droidworksstudio/mlauncher/ui/FavoriteFragment.kt index 4c0f416a..e86fdd90 100644 --- a/app/src/main/java/com/github/droidworksstudio/mlauncher/ui/FavoriteFragment.kt +++ b/app/src/main/java/com/github/droidworksstudio/mlauncher/ui/FavoriteFragment.kt @@ -15,10 +15,11 @@ import android.os.Bundle import android.os.Vibrator import android.util.Log import android.view.DragEvent -import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.ImageView +import android.widget.LinearLayout import android.widget.ScrollView import android.widget.TextView import androidx.annotation.RequiresApi @@ -90,14 +91,6 @@ class FavoriteFragment : Fragment() { @RequiresApi(Build.VERSION_CODES.Q) private fun initObservers() { with(viewModel) { - homeAppsAlignment.observe(viewLifecycleOwner) { (gravity, onBottom) -> - val horizontalAlignment = if (onBottom) Gravity.BOTTOM else Gravity.CENTER_VERTICAL - binding.homeAppsLayout.gravity = gravity.value() or horizontalAlignment - - binding.homeAppsLayout.children.forEach { view -> - (view as TextView).gravity = gravity.value() - } - } homeAppsCount.observe(viewLifecycleOwner) { updateAppCount(it) } @@ -193,25 +186,48 @@ class FavoriteFragment : Fragment() { } else if (diff < 0) { val prefixDrawable: Drawable? = context?.let { ContextCompat.getDrawable(it, R.drawable.ic_prefix_drawable) } - // add all missing apps to list + + // Add all missing apps to the list for (i in oldAppsNum until newAppsNum) { + // Create a LinearLayout with horizontal orientation + val horizontalLayout = LinearLayout(context).apply { + orientation = LinearLayout.HORIZONTAL + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ) + } + + // Create the TextView val view = layoutInflater.inflate(R.layout.home_app_button, null) as TextView view.apply { val appLabel = prefs.getHomeAppModel(i).activityLabel.ifEmpty { getString(R.string.app) } textSize = prefs.appSize.toFloat() id = i - text = " $appLabel" - setCompoundDrawablesWithIntrinsicBounds(prefixDrawable, null, null, null) + text = appLabel + if (!prefs.extendHomeAppsArea) { - layoutParams = ViewGroup.LayoutParams( - ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT ) } } + + // Create the ImageView for the drawable + val imageView = ImageView(context).apply { + layoutParams = LinearLayout.LayoutParams( + LinearLayout.LayoutParams.WRAP_CONTENT, + LinearLayout.LayoutParams.WRAP_CONTENT + ) + prefixDrawable?.let { setImageDrawable(it) } + } + + // Add padding and other styling val padding: Int = prefs.textPaddingSize view.setPadding(0, padding, 0, padding) + binding.pageName.text = getString(R.string.favorite_apps) binding.pageName.textSize = prefs.appSize * 1.5f @@ -220,25 +236,32 @@ class FavoriteFragment : Fragment() { view.setTextColor(fontColor) } - binding.homeAppsLayout.addView(view) + // Add the TextView and ImageView to the horizontal layout + horizontalLayout.addView(view) + horizontalLayout.addView(imageView) + + // Add the horizontal layout to the home apps layout + binding.homeAppsLayout.addView(horizontalLayout) } } - for (i in 0 until newAppsNum) { - val view = binding.homeAppsLayout.getChildAt(i) as TextView - view.setOnDragListener { v, event -> - handleDragEvent(event, v as TextView) - } - view.setOnLongClickListener { v -> - val dragData = ClipData.newPlainText("", "") - val shadowBuilder = View.DragShadowBuilder(v) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { - v.startDragAndDrop(dragData, shadowBuilder, v, 0) - } else { - @Suppress("DEPRECATION") - v.startDrag(dragData, shadowBuilder, v, 0) + (0 until newAppsNum).forEach { i -> + val view = layoutInflater.inflate(R.layout.home_app_button, null) as TextView + view.apply { + setOnDragListener { v, event -> + handleDragEvent(event, v as TextView) + } + setOnLongClickListener { v -> + val dragData = ClipData.newPlainText("", "") + val shadowBuilder = View.DragShadowBuilder(v) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + v.startDragAndDrop(dragData, shadowBuilder, v, 0) + } else { + @Suppress("DEPRECATION") + v.startDrag(dragData, shadowBuilder, v, 0) + } + true } - true } }