package com.aethex.os; import android.content.Context; import android.content.SharedPreferences; import org.json.JSONObject; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Locale; /** * Tracks app launch counts and screen time per app. * Used for auto-categorization and analytics. */ public class ScreenTimeTracker { private static final String PREFS_NAME = "aethex_screen_time"; private static final String KEY_LAUNCH_COUNTS = "launch_counts"; private static final String KEY_PINNED_APPS = "pinned_apps"; private static final String KEY_HIDDEN_APPS = "hidden_apps"; private final SharedPreferences prefs; public ScreenTimeTracker(Context context) { prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); } /** * Record an app launch. */ public void recordLaunch(String appId) { try { String json = prefs.getString(KEY_LAUNCH_COUNTS, "{}"); JSONObject counts = new JSONObject(json); int current = counts.optInt(appId, 0); counts.put(appId, current + 1); prefs.edit().putString(KEY_LAUNCH_COUNTS, counts.toString()).apply(); } catch (Exception ignored) {} } /** * Get launch count for an app. */ public int getLaunchCount(String appId) { try { String json = prefs.getString(KEY_LAUNCH_COUNTS, "{}"); JSONObject counts = new JSONObject(json); return counts.optInt(appId, 0); } catch (Exception e) { return 0; } } /** * Get total launches across all apps. */ public int getTotalLaunches() { try { String json = prefs.getString(KEY_LAUNCH_COUNTS, "{}"); JSONObject counts = new JSONObject(json); int total = 0; Iterator keys = counts.keys(); while (keys.hasNext()) { total += counts.optInt(keys.next(), 0); } return total; } catch (Exception e) { return 0; } } /** * Get the top N most used app IDs. */ public List getMostUsed(int limit) { try { String json = prefs.getString(KEY_LAUNCH_COUNTS, "{}"); JSONObject counts = new JSONObject(json); List entries = new ArrayList<>(); Iterator keys = counts.keys(); while (keys.hasNext()) { String key = keys.next(); entries.add(new String[]{key, String.valueOf(counts.optInt(key, 0))}); } Collections.sort(entries, (a, b) -> Integer.parseInt(b[1]) - Integer.parseInt(a[1])); List result = new ArrayList<>(); for (int i = 0; i < Math.min(limit, entries.size()); i++) { result.add(entries.get(i)[0]); } return result; } catch (Exception e) { return new ArrayList<>(); } } /** * Categorize an app as: "pinned", "most_used", "essential", or "other". */ public String categorize(String appId) { if (isPinned(appId)) return "pinned"; int count = getLaunchCount(appId); if (count >= 10) return "most_used"; // Essential = built-in AeThexOS core apps if (isEssential(appId)) return "essential"; return "other"; } private boolean isEssential(String appId) { switch (appId) { case "terminal": case "browser": case "settings": case "files": case "notes": case "chat": case "mail": case "camera": case "photos": return true; default: return false; } } // ── Pinning ── public boolean isPinned(String appId) { return prefs.getStringSet(KEY_PINNED_APPS, Collections.emptySet()).contains(appId); } public void setPinned(String appId, boolean pinned) { java.util.Set set = new java.util.HashSet<>( prefs.getStringSet(KEY_PINNED_APPS, Collections.emptySet())); if (pinned) set.add(appId); else set.remove(appId); prefs.edit().putStringSet(KEY_PINNED_APPS, set).apply(); } // ── Hiding ── public boolean isHidden(String appId) { return prefs.getStringSet(KEY_HIDDEN_APPS, Collections.emptySet()).contains(appId); } public void setHidden(String appId, boolean hidden) { java.util.Set set = new java.util.HashSet<>( prefs.getStringSet(KEY_HIDDEN_APPS, Collections.emptySet())); if (hidden) set.add(appId); else set.remove(appId); prefs.edit().putStringSet(KEY_HIDDEN_APPS, set).apply(); } /** * Get all hidden app IDs. */ public java.util.Set getHiddenApps() { return prefs.getStringSet(KEY_HIDDEN_APPS, Collections.emptySet()); } }