diff --git a/app/src/main/java/app/olauncher/MainViewModel.kt b/app/src/main/java/app/olauncher/MainViewModel.kt index 21579bd0f..b861aaae6 100644 --- a/app/src/main/java/app/olauncher/MainViewModel.kt +++ b/app/src/main/java/app/olauncher/MainViewModel.kt @@ -30,325 +30,327 @@ import app.olauncher.helper.getAppsList import app.olauncher.helper.hasBeenMinutes import app.olauncher.helper.isOlauncherDefault import app.olauncher.helper.showToast -import kotlinx.coroutines.launch import java.util.concurrent.TimeUnit import kotlin.math.max - +import kotlinx.coroutines.launch class MainViewModel(application: Application) : AndroidViewModel(application) { - private val appContext by lazy { application.applicationContext } - private val prefs = Prefs(appContext) - - val firstOpen = MutableLiveData() - val refreshHome = MutableLiveData() - val toggleDateTime = MutableLiveData() - val updateSwipeApps = MutableLiveData() - val appList = MutableLiveData?>() - val hiddenApps = MutableLiveData?>() - val isOlauncherDefault = MutableLiveData() - val launcherResetFailed = MutableLiveData() - val homeAppAlignment = MutableLiveData() - val screenTimeValue = MutableLiveData() - - val showDialog = SingleLiveEvent() - val checkForMessages = SingleLiveEvent() - val resetLauncherLiveData = SingleLiveEvent() - - fun selectedApp(appModel: AppModel, flag: Int) { - when (flag) { - Constants.FLAG_LAUNCH_APP -> { - launchApp(appModel.appPackage, appModel.activityClassName, appModel.user) - } - - Constants.FLAG_HIDDEN_APPS -> { - launchApp(appModel.appPackage, appModel.activityClassName, appModel.user) - } - - Constants.FLAG_SET_HOME_APP_1 -> { - prefs.appName1 = appModel.appLabel - prefs.appPackage1 = appModel.appPackage - prefs.appUser1 = appModel.user.toString() - prefs.appActivityClassName1 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_2 -> { - prefs.appName2 = appModel.appLabel - prefs.appPackage2 = appModel.appPackage - prefs.appUser2 = appModel.user.toString() - prefs.appActivityClassName2 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_3 -> { - prefs.appName3 = appModel.appLabel - prefs.appPackage3 = appModel.appPackage - prefs.appUser3 = appModel.user.toString() - prefs.appActivityClassName3 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_4 -> { - prefs.appName4 = appModel.appLabel - prefs.appPackage4 = appModel.appPackage - prefs.appUser4 = appModel.user.toString() - prefs.appActivityClassName4 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_5 -> { - prefs.appName5 = appModel.appLabel - prefs.appPackage5 = appModel.appPackage - prefs.appUser5 = appModel.user.toString() - prefs.appActivityClassName5 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_6 -> { - prefs.appName6 = appModel.appLabel - prefs.appPackage6 = appModel.appPackage - prefs.appUser6 = appModel.user.toString() - prefs.appActivityClassName6 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_7 -> { - prefs.appName7 = appModel.appLabel - prefs.appPackage7 = appModel.appPackage - prefs.appUser7 = appModel.user.toString() - prefs.appActivityClassName7 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_HOME_APP_8 -> { - prefs.appName8 = appModel.appLabel - prefs.appPackage8 = appModel.appPackage - prefs.appUser8 = appModel.user.toString() - prefs.appActivityClassName8 = appModel.activityClassName - refreshHome(false) - } - - Constants.FLAG_SET_SWIPE_LEFT_APP -> { - prefs.appNameSwipeLeft = appModel.appLabel - prefs.appPackageSwipeLeft = appModel.appPackage - prefs.appUserSwipeLeft = appModel.user.toString() - prefs.appActivityClassNameSwipeLeft = appModel.activityClassName - updateSwipeApps() - } - - Constants.FLAG_SET_SWIPE_RIGHT_APP -> { - prefs.appNameSwipeRight = appModel.appLabel - prefs.appPackageSwipeRight = appModel.appPackage - prefs.appUserSwipeRight = appModel.user.toString() - prefs.appActivityClassNameRight = appModel.activityClassName - updateSwipeApps() - } - - Constants.FLAG_SET_CLOCK_APP -> { - prefs.clockAppPackage = appModel.appPackage - prefs.clockAppUser = appModel.user.toString() - prefs.clockAppClassName = appModel.activityClassName - } - - Constants.FLAG_SET_CALENDAR_APP -> { - prefs.calendarAppPackage = appModel.appPackage - prefs.calendarAppUser = appModel.user.toString() - prefs.calendarAppClassName = appModel.activityClassName - } - } + private val appContext by lazy { application.applicationContext } + private val prefs = Prefs(appContext) + + val firstOpen = MutableLiveData() + val refreshHome = MutableLiveData() + val toggleDateTime = MutableLiveData() + val updateSwipeApps = MutableLiveData() + val appList = MutableLiveData?>() + val hiddenApps = MutableLiveData?>() + val isOlauncherDefault = MutableLiveData() + val launcherResetFailed = MutableLiveData() + val homeAppAlignment = MutableLiveData() + val screenTimeValue = MutableLiveData() + val appBlockedMessage = SingleLiveEvent() + + val showDialog = SingleLiveEvent() + val checkForMessages = SingleLiveEvent() + val resetLauncherLiveData = SingleLiveEvent() + + fun selectedApp(appModel: AppModel, flag: Int) { + // Check if app is blocked before launching + if (flag == Constants.FLAG_LAUNCH_APP || flag == Constants.FLAG_HIDDEN_APPS) { + val appLimit = Prefs(appContext).getAppLimit(appModel.appPackage) + if (appLimit != null && appLimit.isCurrentlyBlocked()) { + val message = appContext.getString(R.string.app_blocked_until, appLimit.getEndTimeString()) + appBlockedMessage.postValue(message) + return + } } - fun firstOpen(value: Boolean) { - firstOpen.postValue(value) + when (flag) { + Constants.FLAG_LAUNCH_APP -> { + launchApp(appModel.appPackage, appModel.activityClassName, appModel.user) + } + Constants.FLAG_HIDDEN_APPS -> { + launchApp(appModel.appPackage, appModel.activityClassName, appModel.user) + } + Constants.FLAG_SET_HOME_APP_1 -> { + prefs.appName1 = appModel.appLabel + prefs.appPackage1 = appModel.appPackage + prefs.appUser1 = appModel.user.toString() + prefs.appActivityClassName1 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_2 -> { + prefs.appName2 = appModel.appLabel + prefs.appPackage2 = appModel.appPackage + prefs.appUser2 = appModel.user.toString() + prefs.appActivityClassName2 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_3 -> { + prefs.appName3 = appModel.appLabel + prefs.appPackage3 = appModel.appPackage + prefs.appUser3 = appModel.user.toString() + prefs.appActivityClassName3 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_4 -> { + prefs.appName4 = appModel.appLabel + prefs.appPackage4 = appModel.appPackage + prefs.appUser4 = appModel.user.toString() + prefs.appActivityClassName4 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_5 -> { + prefs.appName5 = appModel.appLabel + prefs.appPackage5 = appModel.appPackage + prefs.appUser5 = appModel.user.toString() + prefs.appActivityClassName5 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_6 -> { + prefs.appName6 = appModel.appLabel + prefs.appPackage6 = appModel.appPackage + prefs.appUser6 = appModel.user.toString() + prefs.appActivityClassName6 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_7 -> { + prefs.appName7 = appModel.appLabel + prefs.appPackage7 = appModel.appPackage + prefs.appUser7 = appModel.user.toString() + prefs.appActivityClassName7 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_HOME_APP_8 -> { + prefs.appName8 = appModel.appLabel + prefs.appPackage8 = appModel.appPackage + prefs.appUser8 = appModel.user.toString() + prefs.appActivityClassName8 = appModel.activityClassName + refreshHome(false) + } + Constants.FLAG_SET_SWIPE_LEFT_APP -> { + prefs.appNameSwipeLeft = appModel.appLabel + prefs.appPackageSwipeLeft = appModel.appPackage + prefs.appUserSwipeLeft = appModel.user.toString() + prefs.appActivityClassNameSwipeLeft = appModel.activityClassName + updateSwipeApps() + } + Constants.FLAG_SET_SWIPE_RIGHT_APP -> { + prefs.appNameSwipeRight = appModel.appLabel + prefs.appPackageSwipeRight = appModel.appPackage + prefs.appUserSwipeRight = appModel.user.toString() + prefs.appActivityClassNameRight = appModel.activityClassName + updateSwipeApps() + } + Constants.FLAG_SET_CLOCK_APP -> { + prefs.clockAppPackage = appModel.appPackage + prefs.clockAppUser = appModel.user.toString() + prefs.clockAppClassName = appModel.activityClassName + } + Constants.FLAG_SET_CALENDAR_APP -> { + prefs.calendarAppPackage = appModel.appPackage + prefs.calendarAppUser = appModel.user.toString() + prefs.calendarAppClassName = appModel.activityClassName + } + Constants.FLAG_SET_APP_LIMIT -> { + // This flag is handled in AppLimitsFragment + // Just return without doing anything + return + } } + } - fun refreshHome(appCountUpdated: Boolean) { - refreshHome.value = appCountUpdated - } + fun firstOpen(value: Boolean) { + firstOpen.postValue(value) + } - fun toggleDateTime() { - toggleDateTime.postValue(Unit) - } + fun refreshHome(appCountUpdated: Boolean) { + refreshHome.value = appCountUpdated + } - private fun updateSwipeApps() { - updateSwipeApps.postValue(Unit) - } + fun toggleDateTime() { + toggleDateTime.postValue(Unit) + } + + private fun updateSwipeApps() { + updateSwipeApps.postValue(Unit) + } - private fun launchApp(packageName: String, activityClassName: String?, userHandle: UserHandle) { - val launcher = appContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps - val activityInfo = launcher.getActivityList(packageName, userHandle) + private fun launchApp(packageName: String, activityClassName: String?, userHandle: UserHandle) { + val launcher = appContext.getSystemService(Context.LAUNCHER_APPS_SERVICE) as LauncherApps + val activityInfo = launcher.getActivityList(packageName, userHandle) - val component = if (activityClassName.isNullOrBlank()) { - // activityClassName will be null for hidden apps. - when (activityInfo.size) { + val component = + if (activityClassName.isNullOrBlank()) { + // activityClassName will be null for hidden apps. + when (activityInfo.size) { 0 -> { - appContext.showToast(appContext.getString(R.string.app_not_found)) - return + appContext.showToast(appContext.getString(R.string.app_not_found)) + return } - 1 -> ComponentName(packageName, activityInfo[0].name) else -> ComponentName(packageName, activityInfo[activityInfo.size - 1].name) + } + } else { + ComponentName(packageName, activityClassName) } - } else { - ComponentName(packageName, activityClassName) - } - try { - launcher.startMainActivity(component, userHandle, null, null) - } catch (e: SecurityException) { - try { - launcher.startMainActivity(component, android.os.Process.myUserHandle(), null, null) - } catch (e: Exception) { - appContext.showToast(appContext.getString(R.string.unable_to_open_app)) - } - } catch (e: Exception) { - appContext.showToast(appContext.getString(R.string.unable_to_open_app)) - } - } - - fun getAppList(includeHiddenApps: Boolean = false) { - viewModelScope.launch { - appList.value = getAppsList(appContext, prefs, includeRegularApps = true, includeHiddenApps) - } + try { + launcher.startMainActivity(component, userHandle, null, null) + } catch (e: SecurityException) { + try { + launcher.startMainActivity(component, android.os.Process.myUserHandle(), null, null) + } catch (e: Exception) { + appContext.showToast(appContext.getString(R.string.unable_to_open_app)) + } + } catch (e: Exception) { + appContext.showToast(appContext.getString(R.string.unable_to_open_app)) } + } - fun getHiddenApps() { - viewModelScope.launch { - hiddenApps.value = getAppsList(appContext, prefs, includeRegularApps = false, includeHiddenApps = true) - } + fun getAppList(includeHiddenApps: Boolean = false) { + viewModelScope.launch { + appList.value = getAppsList(appContext, prefs, includeRegularApps = true, includeHiddenApps) } + } - fun isOlauncherDefault() { - isOlauncherDefault.value = isOlauncherDefault(appContext) + fun getHiddenApps() { + viewModelScope.launch { + hiddenApps.value = + getAppsList(appContext, prefs, includeRegularApps = false, includeHiddenApps = true) } - -// fun resetDefaultLauncherApp(context: Context) { -// resetDefaultLauncher(context) -// launcherResetFailed.value = getDefaultLauncherPackage(appContext).contains(".") -// } - - fun setWallpaperWorker() { - val constraints = Constraints.Builder() - .setRequiredNetworkType(NetworkType.CONNECTED) - .build() - val uploadWorkRequest = PeriodicWorkRequestBuilder(8, TimeUnit.HOURS) - .setBackoffCriteria(BackoffPolicy.LINEAR, 1, TimeUnit.HOURS) - .setConstraints(constraints) - .build() - WorkManager - .getInstance(appContext) + } + + fun isOlauncherDefault() { + isOlauncherDefault.value = isOlauncherDefault(appContext) + } + + // fun resetDefaultLauncherApp(context: Context) { + // resetDefaultLauncher(context) + // launcherResetFailed.value = getDefaultLauncherPackage(appContext).contains(".") + // } + + fun setWallpaperWorker() { + val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build() + val uploadWorkRequest = + PeriodicWorkRequestBuilder(8, TimeUnit.HOURS) + .setBackoffCriteria(BackoffPolicy.LINEAR, 1, TimeUnit.HOURS) + .setConstraints(constraints) + .build() + WorkManager.getInstance(appContext) .enqueueUniquePeriodicWork( - Constants.WALLPAPER_WORKER_NAME, - ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, - uploadWorkRequest + Constants.WALLPAPER_WORKER_NAME, + ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, + uploadWorkRequest ) - } - - fun cancelWallpaperWorker() { - WorkManager.getInstance(appContext).cancelUniqueWork(Constants.WALLPAPER_WORKER_NAME) - prefs.dailyWallpaperUrl = "" - prefs.dailyWallpaper = false - } - - fun updateHomeAlignment(gravity: Int) { - prefs.homeAlignment = gravity - homeAppAlignment.value = prefs.homeAlignment - } - - fun getTodaysScreenTime() { - if (prefs.screenTimeLastUpdated.hasBeenMinutes(1).not()) return - - val usageStatsManager = appContext.getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager - val appUsageStatsHashMap: MutableMap = HashMap() - val beginTime = System.currentTimeMillis().convertEpochToMidnight() - val endTime = System.currentTimeMillis() - val events = usageStatsManager.queryEvents(beginTime, endTime) - val eventsMap: MutableMap> = HashMap() - var currentEvent: UsageEvents.Event - - while (events.hasNextEvent()) { - currentEvent = UsageEvents.Event() - if (events.getNextEvent(currentEvent)) { - when (currentEvent.eventType) { - UsageEvents.Event.ACTIVITY_RESUMED, UsageEvents.Event.ACTIVITY_PAUSED, UsageEvents.Event.ACTIVITY_STOPPED, UsageEvents.Event.FOREGROUND_SERVICE_START, UsageEvents.Event.FOREGROUND_SERVICE_STOP -> { - var packageEvents = eventsMap[currentEvent.packageName] - if (packageEvents == null) - packageEvents = ArrayList(listOf(currentEvent)) - else - packageEvents.add(currentEvent) - eventsMap[currentEvent.packageName] = packageEvents - } - } - } + } + + fun cancelWallpaperWorker() { + WorkManager.getInstance(appContext).cancelUniqueWork(Constants.WALLPAPER_WORKER_NAME) + prefs.dailyWallpaperUrl = "" + prefs.dailyWallpaper = false + } + + fun updateHomeAlignment(gravity: Int) { + prefs.homeAlignment = gravity + homeAppAlignment.value = prefs.homeAlignment + } + + fun getTodaysScreenTime() { + if (prefs.screenTimeLastUpdated.hasBeenMinutes(1).not()) return + + val usageStatsManager = appContext.getSystemService(USAGE_STATS_SERVICE) as UsageStatsManager + val appUsageStatsHashMap: MutableMap = HashMap() + val beginTime = System.currentTimeMillis().convertEpochToMidnight() + val endTime = System.currentTimeMillis() + val events = usageStatsManager.queryEvents(beginTime, endTime) + val eventsMap: MutableMap> = HashMap() + var currentEvent: UsageEvents.Event + + while (events.hasNextEvent()) { + currentEvent = UsageEvents.Event() + if (events.getNextEvent(currentEvent)) { + when (currentEvent.eventType) { + UsageEvents.Event.ACTIVITY_RESUMED, + UsageEvents.Event.ACTIVITY_PAUSED, + UsageEvents.Event.ACTIVITY_STOPPED, + UsageEvents.Event.FOREGROUND_SERVICE_START, + UsageEvents.Event.FOREGROUND_SERVICE_STOP -> { + var packageEvents = eventsMap[currentEvent.packageName] + if (packageEvents == null) packageEvents = ArrayList(listOf(currentEvent)) + else packageEvents.add(currentEvent) + eventsMap[currentEvent.packageName] = packageEvents + } } + } + } - for ((key, value) in eventsMap) { - val foregroundBucket = AppUsageStatsBucket() - val backgroundBucketMap: MutableMap = HashMap() - var pos = 0 - while (pos < value.size) { - val event = value[pos] - if (event.className != null) { - var backgroundBucket: AppUsageStatsBucket? = backgroundBucketMap[event.className] - if (backgroundBucket == null) { - backgroundBucket = AppUsageStatsBucket() - backgroundBucketMap[event.className] = backgroundBucket - } - when (event.eventType) { - UsageEvents.Event.ACTIVITY_RESUMED -> foregroundBucket.startMillis = event.timeStamp - - UsageEvents.Event.ACTIVITY_PAUSED, UsageEvents.Event.ACTIVITY_STOPPED -> if (foregroundBucket.startMillis >= foregroundBucket.endMillis) { - if (foregroundBucket.startMillis == 0L) { - foregroundBucket.startMillis = beginTime - } - foregroundBucket.endMillis = event.timeStamp - foregroundBucket.addTotalTime() - } - - UsageEvents.Event.FOREGROUND_SERVICE_START -> backgroundBucket.startMillis = event.timeStamp - UsageEvents.Event.FOREGROUND_SERVICE_STOP -> if (backgroundBucket.startMillis >= backgroundBucket.endMillis) { - if (backgroundBucket.startMillis == 0L) { - backgroundBucket.startMillis = beginTime - } - backgroundBucket.endMillis = event.timeStamp - backgroundBucket.addTotalTime() - } + for ((key, value) in eventsMap) { + val foregroundBucket = AppUsageStatsBucket() + val backgroundBucketMap: MutableMap = HashMap() + var pos = 0 + while (pos < value.size) { + val event = value[pos] + if (event.className != null) { + var backgroundBucket: AppUsageStatsBucket? = backgroundBucketMap[event.className] + if (backgroundBucket == null) { + backgroundBucket = AppUsageStatsBucket() + backgroundBucketMap[event.className] = backgroundBucket + } + when (event.eventType) { + UsageEvents.Event.ACTIVITY_RESUMED -> foregroundBucket.startMillis = event.timeStamp + UsageEvents.Event.ACTIVITY_PAUSED, UsageEvents.Event.ACTIVITY_STOPPED -> + if (foregroundBucket.startMillis >= foregroundBucket.endMillis) { + if (foregroundBucket.startMillis == 0L) { + foregroundBucket.startMillis = beginTime + } + foregroundBucket.endMillis = event.timeStamp + foregroundBucket.addTotalTime() } - if (pos == value.size - 1) { - if (foregroundBucket.startMillis > foregroundBucket.endMillis) { - foregroundBucket.endMillis = endTime - foregroundBucket.addTotalTime() - } - if (backgroundBucket.startMillis > backgroundBucket.endMillis) { - backgroundBucket.endMillis = endTime - backgroundBucket.addTotalTime() - } + UsageEvents.Event.FOREGROUND_SERVICE_START -> + backgroundBucket.startMillis = event.timeStamp + UsageEvents.Event.FOREGROUND_SERVICE_STOP -> + if (backgroundBucket.startMillis >= backgroundBucket.endMillis) { + if (backgroundBucket.startMillis == 0L) { + backgroundBucket.startMillis = beginTime + } + backgroundBucket.endMillis = event.timeStamp + backgroundBucket.addTotalTime() } - } - pos++ + } + if (pos == value.size - 1) { + if (foregroundBucket.startMillis > foregroundBucket.endMillis) { + foregroundBucket.endMillis = endTime + foregroundBucket.addTotalTime() } - - val foregroundEnd: Long = foregroundBucket.endMillis - val totalTimeForeground: Long = foregroundBucket.totalTime - val backgroundEnd: Long = backgroundBucketMap.values - .mapNotNull { it?.endMillis } - .maxOrNull() ?: 0L - - val totalTimeBackground: Long = backgroundBucketMap.values - .mapNotNull { it?.totalTime } - .sum() - - appUsageStatsHashMap[key] = AppUsageStats( - max(foregroundEnd, backgroundEnd), - totalTimeForeground, - backgroundEnd, - totalTimeBackground - ) + if (backgroundBucket.startMillis > backgroundBucket.endMillis) { + backgroundBucket.endMillis = endTime + backgroundBucket.addTotalTime() + } + } } - - val totalTimeInMillis = appUsageStatsHashMap.values.sumOf { it.totalTimeInForegroundMillis } - val viewTimeSpent = appContext.formattedTimeSpent((totalTimeInMillis * 1.1).toLong()) - screenTimeValue.postValue(viewTimeSpent) - prefs.screenTimeLastUpdated = endTime + pos++ + } + + val foregroundEnd: Long = foregroundBucket.endMillis + val totalTimeForeground: Long = foregroundBucket.totalTime + val backgroundEnd: Long = + backgroundBucketMap.values.mapNotNull { it?.endMillis }.maxOrNull() ?: 0L + + val totalTimeBackground: Long = backgroundBucketMap.values.mapNotNull { it?.totalTime }.sum() + + appUsageStatsHashMap[key] = + AppUsageStats( + max(foregroundEnd, backgroundEnd), + totalTimeForeground, + backgroundEnd, + totalTimeBackground + ) } -} \ No newline at end of file + + val totalTimeInMillis = appUsageStatsHashMap.values.sumOf { it.totalTimeInForegroundMillis } + val viewTimeSpent = appContext.formattedTimeSpent((totalTimeInMillis * 1.1).toLong()) + screenTimeValue.postValue(viewTimeSpent) + prefs.screenTimeLastUpdated = endTime + } +} diff --git a/app/src/main/java/app/olauncher/data/AppLimitModel.kt b/app/src/main/java/app/olauncher/data/AppLimitModel.kt new file mode 100644 index 000000000..248b28087 --- /dev/null +++ b/app/src/main/java/app/olauncher/data/AppLimitModel.kt @@ -0,0 +1,70 @@ +package app.olauncher.data + +import org.json.JSONObject + +data class AppLimitModel( + val appPackage: String, + val appName: String, + val startHour: Int, + val startMinute: Int, + val endHour: Int, + val endMinute: Int, + val lockDuringLimit: Boolean +) { + fun toJson(): JSONObject { + return JSONObject().apply { + put("appPackage", appPackage) + put("appName", appName) + put("startHour", startHour) + put("startMinute", startMinute) + put("endHour", endHour) + put("endMinute", endMinute) + put("lockDuringLimit", lockDuringLimit) + } + } + + companion object { + fun fromJson(json: JSONObject): AppLimitModel { + return AppLimitModel( + appPackage = json.getString("appPackage"), + appName = json.getString("appName"), + startHour = json.getInt("startHour"), + startMinute = json.getInt("startMinute"), + endHour = json.getInt("endHour"), + endMinute = json.getInt("endMinute"), + lockDuringLimit = json.getBoolean("lockDuringLimit") + ) + } + } + + fun getStartTimeString(): String { + return formatTime12Hour(startHour, startMinute) + } + + fun getEndTimeString(): String { + return formatTime12Hour(endHour, endMinute) + } + + private fun formatTime12Hour(hour: Int, minute: Int): String { + val hour12 = if (hour == 0) 12 else if (hour > 12) hour - 12 else hour + val amPm = if (hour < 12) "AM" else "PM" + return String.format("%d:%02d %s", hour12, minute, amPm) + } + + fun isCurrentlyBlocked(): Boolean { + val calendar = java.util.Calendar.getInstance() + val currentHour = calendar.get(java.util.Calendar.HOUR_OF_DAY) + val currentMinute = calendar.get(java.util.Calendar.MINUTE) + val currentTimeInMinutes = currentHour * 60 + currentMinute + val startTimeInMinutes = startHour * 60 + startMinute + val endTimeInMinutes = endHour * 60 + endMinute + + return if (startTimeInMinutes <= endTimeInMinutes) { + // Normal case: start before end (e.g., 09:00 to 17:00) + currentTimeInMinutes in startTimeInMinutes until endTimeInMinutes + } else { + // Overnight case: start after end (e.g., 22:00 to 06:00) + currentTimeInMinutes >= startTimeInMinutes || currentTimeInMinutes < endTimeInMinutes + } + } +} diff --git a/app/src/main/java/app/olauncher/data/Constants.kt b/app/src/main/java/app/olauncher/data/Constants.kt index 9b6e922c2..6178fde1c 100644 --- a/app/src/main/java/app/olauncher/data/Constants.kt +++ b/app/src/main/java/app/olauncher/data/Constants.kt @@ -2,118 +2,131 @@ package app.olauncher.data object Constants { - object Key { - const val FLAG = "flag" - const val RENAME = "rename" + object Key { + const val FLAG = "flag" + const val RENAME = "rename" + const val APP_PACKAGE = "app_package" + const val APP_NAME = "app_name" + const val APP_LIMIT = "app_limit" + } + + object Dialog { + const val ABOUT = "ABOUT" + const val WALLPAPER = "WALLPAPER" + const val REVIEW = "REVIEW" + const val RATE = "RATE" + const val SHARE = "SHARE" + const val HIDDEN = "HIDDEN" + const val KEYBOARD = "KEYBOARD" + const val DIGITAL_WELLBEING = "DIGITAL_WELLBEING" + const val PRO_MESSAGE = "PRO_MESSAGE" + } + + object UserState { + const val START = "START" + const val WALLPAPER = "WALLPAPER" + const val REVIEW = "REVIEW" + const val RATE = "RATE" + const val SHARE = "SHARE" + } + + object DateTime { + const val OFF = 0 + const val ON = 1 + const val DATE_ONLY = 2 + + fun isTimeVisible(dateTimeVisibility: Int): Boolean { + return dateTimeVisibility == ON } - object Dialog { - const val ABOUT = "ABOUT" - const val WALLPAPER = "WALLPAPER" - const val REVIEW = "REVIEW" - const val RATE = "RATE" - const val SHARE = "SHARE" - const val HIDDEN = "HIDDEN" - const val KEYBOARD = "KEYBOARD" - const val DIGITAL_WELLBEING = "DIGITAL_WELLBEING" - const val PRO_MESSAGE = "PRO_MESSAGE" + fun isDateVisible(dateTimeVisibility: Int): Boolean { + return dateTimeVisibility == ON || dateTimeVisibility == DATE_ONLY } - - object UserState { - const val START = "START" - const val WALLPAPER = "WALLPAPER" - const val REVIEW = "REVIEW" - const val RATE = "RATE" - const val SHARE = "SHARE" - } - - object DateTime { - const val OFF = 0 - const val ON = 1 - const val DATE_ONLY = 2 - - fun isTimeVisible(dateTimeVisibility: Int): Boolean { - return dateTimeVisibility == ON - } - - fun isDateVisible(dateTimeVisibility: Int): Boolean { - return dateTimeVisibility == ON || dateTimeVisibility == DATE_ONLY - } - } - - object SwipeDownAction { - const val SEARCH = 1 - const val NOTIFICATIONS = 2 - } - - object TextSize { - const val ONE = 0.6f - const val TWO = 0.75f - const val THREE = 0.9f - const val FOUR = 1f - const val FIVE = 1.1f - const val SIX = 1.2f - const val SEVEN = 1.3f - } - - object CharacterIndicator{ - const val SHOW = 102 - const val HIDE = 101 - } - - const val WALL_TYPE_LIGHT = "light" - const val WALL_TYPE_DARK = "dark" - -// const val THEME_MODE_DARK = 0 -// const val THEME_MODE_LIGHT = 1 -// const val THEME_MODE_SYSTEM = 2 - - const val FLAG_LAUNCH_APP = 100 - const val FLAG_HIDDEN_APPS = 101 - - const val FLAG_SET_HOME_APP_1 = 1 - const val FLAG_SET_HOME_APP_2 = 2 - const val FLAG_SET_HOME_APP_3 = 3 - const val FLAG_SET_HOME_APP_4 = 4 - const val FLAG_SET_HOME_APP_5 = 5 - const val FLAG_SET_HOME_APP_6 = 6 - const val FLAG_SET_HOME_APP_7 = 7 - const val FLAG_SET_HOME_APP_8 = 8 - - const val FLAG_SET_SWIPE_LEFT_APP = 11 - const val FLAG_SET_SWIPE_RIGHT_APP = 12 - const val FLAG_SET_CLOCK_APP = 13 - const val FLAG_SET_CALENDAR_APP = 14 - - const val REQUEST_CODE_ENABLE_ADMIN = 666 - const val REQUEST_CODE_LAUNCHER_SELECTOR = 678 - - const val HINT_RATE_US = 15 - - const val LONG_PRESS_DELAY_MS = 500L - const val ONE_DAY_IN_MILLIS = 86400000L - const val ONE_HOUR_IN_MILLIS = 3600000L - const val ONE_MINUTE_IN_MILLIS = 60000L - - const val MIN_ANIM_REFRESH_RATE = 10f - - const val URL_ABOUT_OLAUNCHER = "https://tanujnotes.substack.com/p/olauncher-minimal-af-launcher?utm_source=olauncher" - const val URL_OLAUNCHER_PRIVACY = "https://tanujnotes.notion.site/Olauncher-Privacy-Policy-dd6ac5101ddd4b3da9d27057889d44ab" - const val URL_DOUBLE_TAP = "https://tanujnotes.notion.site/Double-tap-to-lock-Olauncher-0f7fb103ec1f47d7a90cdfdcd7fb86ef" - const val URL_OLAUNCHER_GITHUB = "https://www.github.com/tanujnotes/Olauncher" - const val URL_OLAUNCHER_PLAY_STORE = "https://play.google.com/store/apps/details?id=app.olauncher" - const val URL_OLAUNCHER_PRO = "https://play.google.com/store/apps/details?id=app.prolauncher" - const val URL_PLAY_STORE_DEV = "https://play.google.com/store/apps/dev?id=7198807840081074933" - const val URL_TWITTER_TANUJ = "https://twitter.com/tanujnotes" - const val URL_WALLPAPERS = "https://gist.githubusercontent.com/tanujnotes/85e2d0343ace71e76615ac346fbff82b/raw" - const val URL_DEFAULT_DARK_WALLPAPER = "https://images.unsplash.com/photo-1512551980832-13df02babc9e" - const val URL_DEFAULT_LIGHT_WALLPAPER = "https://images.unsplash.com/photo-1515549832467-8783363e19b6" - const val URL_DUCK_SEARCH = "https://duck.co/?q=" - const val URL_DIGITAL_WELLBEING_LEARN_MORE = "https://tanujnotes.substack.com/p/digital-wellbeing-app-on-android?utm_source=olauncher" - - const val DIGITAL_WELLBEING_PACKAGE_NAME = "com.google.android.apps.wellbeing" - const val DIGITAL_WELLBEING_ACTIVITY = "com.google.android.apps.wellbeing.settings.TopLevelSettingsActivity" - const val DIGITAL_WELLBEING_SAMSUNG_PACKAGE_NAME = "com.samsung.android.forest" - const val DIGITAL_WELLBEING_SAMSUNG_ACTIVITY = "com.samsung.android.forest.launcher.LauncherActivity" - const val WALLPAPER_WORKER_NAME = "WALLPAPER_WORKER_NAME" -} \ No newline at end of file + } + + object SwipeDownAction { + const val SEARCH = 1 + const val NOTIFICATIONS = 2 + } + + object TextSize { + const val ONE = 0.6f + const val TWO = 0.75f + const val THREE = 0.9f + const val FOUR = 1f + const val FIVE = 1.1f + const val SIX = 1.2f + const val SEVEN = 1.3f + } + + object CharacterIndicator { + const val SHOW = 102 + const val HIDE = 101 + } + + const val WALL_TYPE_LIGHT = "light" + const val WALL_TYPE_DARK = "dark" + + // const val THEME_MODE_DARK = 0 + // const val THEME_MODE_LIGHT = 1 + // const val THEME_MODE_SYSTEM = 2 + + const val FLAG_LAUNCH_APP = 100 + const val FLAG_HIDDEN_APPS = 101 + + const val FLAG_SET_HOME_APP_1 = 1 + const val FLAG_SET_HOME_APP_2 = 2 + const val FLAG_SET_HOME_APP_3 = 3 + const val FLAG_SET_HOME_APP_4 = 4 + const val FLAG_SET_HOME_APP_5 = 5 + const val FLAG_SET_HOME_APP_6 = 6 + const val FLAG_SET_HOME_APP_7 = 7 + const val FLAG_SET_HOME_APP_8 = 8 + + const val FLAG_SET_SWIPE_LEFT_APP = 11 + const val FLAG_SET_SWIPE_RIGHT_APP = 12 + const val FLAG_SET_CLOCK_APP = 13 + const val FLAG_SET_CALENDAR_APP = 14 + const val FLAG_SET_APP_LIMIT = 15 + + const val REQUEST_CODE_ENABLE_ADMIN = 666 + const val REQUEST_CODE_LAUNCHER_SELECTOR = 678 + + const val HINT_RATE_US = 15 + + const val LONG_PRESS_DELAY_MS = 500L + const val ONE_DAY_IN_MILLIS = 86400000L + const val ONE_HOUR_IN_MILLIS = 3600000L + const val ONE_MINUTE_IN_MILLIS = 60000L + + const val MIN_ANIM_REFRESH_RATE = 10f + + const val URL_ABOUT_OLAUNCHER = + "https://tanujnotes.substack.com/p/olauncher-minimal-af-launcher?utm_source=olauncher" + const val URL_OLAUNCHER_PRIVACY = + "https://tanujnotes.notion.site/Olauncher-Privacy-Policy-dd6ac5101ddd4b3da9d27057889d44ab" + const val URL_DOUBLE_TAP = + "https://tanujnotes.notion.site/Double-tap-to-lock-Olauncher-0f7fb103ec1f47d7a90cdfdcd7fb86ef" + const val URL_OLAUNCHER_GITHUB = "https://www.github.com/tanujnotes/Olauncher" + const val URL_OLAUNCHER_PLAY_STORE = "https://play.google.com/store/apps/details?id=app.olauncher" + const val URL_OLAUNCHER_PRO = "https://play.google.com/store/apps/details?id=app.prolauncher" + const val URL_PLAY_STORE_DEV = "https://play.google.com/store/apps/dev?id=7198807840081074933" + const val URL_TWITTER_TANUJ = "https://twitter.com/tanujnotes" + const val URL_WALLPAPERS = + "https://gist.githubusercontent.com/tanujnotes/85e2d0343ace71e76615ac346fbff82b/raw" + const val URL_DEFAULT_DARK_WALLPAPER = + "https://images.unsplash.com/photo-1512551980832-13df02babc9e" + const val URL_DEFAULT_LIGHT_WALLPAPER = + "https://images.unsplash.com/photo-1515549832467-8783363e19b6" + const val URL_DUCK_SEARCH = "https://duck.co/?q=" + const val URL_DIGITAL_WELLBEING_LEARN_MORE = + "https://tanujnotes.substack.com/p/digital-wellbeing-app-on-android?utm_source=olauncher" + + const val DIGITAL_WELLBEING_PACKAGE_NAME = "com.google.android.apps.wellbeing" + const val DIGITAL_WELLBEING_ACTIVITY = + "com.google.android.apps.wellbeing.settings.TopLevelSettingsActivity" + const val DIGITAL_WELLBEING_SAMSUNG_PACKAGE_NAME = "com.samsung.android.forest" + const val DIGITAL_WELLBEING_SAMSUNG_ACTIVITY = + "com.samsung.android.forest.launcher.LauncherActivity" + const val WALLPAPER_WORKER_NAME = "WALLPAPER_WORKER_NAME" +} diff --git a/app/src/main/java/app/olauncher/data/Prefs.kt b/app/src/main/java/app/olauncher/data/Prefs.kt index 758fc74b6..c873e2fd3 100644 --- a/app/src/main/java/app/olauncher/data/Prefs.kt +++ b/app/src/main/java/app/olauncher/data/Prefs.kt @@ -6,456 +6,503 @@ import android.view.Gravity import androidx.appcompat.app.AppCompatDelegate class Prefs(context: Context) { - private val PREFS_FILENAME = "app.olauncher" - - private val FIRST_OPEN = "FIRST_OPEN" - private val FIRST_OPEN_TIME = "FIRST_OPEN_TIME" - private val FIRST_SETTINGS_OPEN = "FIRST_SETTINGS_OPEN" - private val FIRST_HIDE = "FIRST_HIDE" - private val USER_STATE = "USER_STATE" - private val LOCK_MODE = "LOCK_MODE" - private val HOME_APPS_NUM = "HOME_APPS_NUM" - private val AUTO_SHOW_KEYBOARD = "AUTO_SHOW_KEYBOARD" - private val KEYBOARD_MESSAGE = "KEYBOARD_MESSAGE" - private val DAILY_WALLPAPER = "DAILY_WALLPAPER" - private val DAILY_WALLPAPER_URL = "DAILY_WALLPAPER_URL" - private val WALLPAPER_UPDATED_DAY = "WALLPAPER_UPDATED_DAY" - private val HOME_ALIGNMENT = "HOME_ALIGNMENT" - private val HOME_BOTTOM_ALIGNMENT = "HOME_BOTTOM_ALIGNMENT" - private val APP_LABEL_ALIGNMENT = "APP_LABEL_ALIGNMENT" - private val STATUS_BAR = "STATUS_BAR" - private val DATE_TIME_VISIBILITY = "DATE_TIME_VISIBILITY" - private val SWIPE_LEFT_ENABLED = "SWIPE_LEFT_ENABLED" - private val SWIPE_RIGHT_ENABLED = "SWIPE_RIGHT_ENABLED" - private val HIDDEN_APPS = "HIDDEN_APPS" - private val HIDDEN_APPS_UPDATED = "HIDDEN_APPS_UPDATED" - private val SHOW_HINT_COUNTER = "SHOW_HINT_COUNTER" - private val APP_THEME = "APP_THEME" - private val ABOUT_CLICKED = "ABOUT_CLICKED" - private val RATE_CLICKED = "RATE_CLICKED" - private val WALLPAPER_MSG_SHOWN = "WALLPAPER_MSG_SHOWN" - private val SHARE_SHOWN_TIME = "SHARE_SHOWN_TIME" - private val SWIPE_DOWN_ACTION = "SWIPE_DOWN_ACTION" - private val TEXT_SIZE_SCALE = "TEXT_SIZE_SCALE" - private val PRO_MESSAGE_SHOWN = "PRO_MESSAGE_SHOWN" - private val HIDE_SET_DEFAULT_LAUNCHER = "HIDE_SET_DEFAULT_LAUNCHER" - private val SCREEN_TIME_LAST_UPDATED = "SCREEN_TIME_LAST_UPDATED" - - private val APP_NAME_1 = "APP_NAME_1" - private val APP_NAME_2 = "APP_NAME_2" - private val APP_NAME_3 = "APP_NAME_3" - private val APP_NAME_4 = "APP_NAME_4" - private val APP_NAME_5 = "APP_NAME_5" - private val APP_NAME_6 = "APP_NAME_6" - private val APP_NAME_7 = "APP_NAME_7" - private val APP_NAME_8 = "APP_NAME_8" - private val APP_PACKAGE_1 = "APP_PACKAGE_1" - private val APP_PACKAGE_2 = "APP_PACKAGE_2" - private val APP_PACKAGE_3 = "APP_PACKAGE_3" - private val APP_PACKAGE_4 = "APP_PACKAGE_4" - private val APP_PACKAGE_5 = "APP_PACKAGE_5" - private val APP_PACKAGE_6 = "APP_PACKAGE_6" - private val APP_PACKAGE_7 = "APP_PACKAGE_7" - private val APP_PACKAGE_8 = "APP_PACKAGE_8" - private val APP_ACTIVITY_CLASS_NAME_1 = "APP_ACTIVITY_CLASS_NAME_1" - private val APP_ACTIVITY_CLASS_NAME_2 = "APP_ACTIVITY_CLASS_NAME_2" - private val APP_ACTIVITY_CLASS_NAME_3 = "APP_ACTIVITY_CLASS_NAME_3" - private val APP_ACTIVITY_CLASS_NAME_4 = "APP_ACTIVITY_CLASS_NAME_4" - private val APP_ACTIVITY_CLASS_NAME_5 = "APP_ACTIVITY_CLASS_NAME_5" - private val APP_ACTIVITY_CLASS_NAME_6 = "APP_ACTIVITY_CLASS_NAME_6" - private val APP_ACTIVITY_CLASS_NAME_7 = "APP_ACTIVITY_CLASS_NAME_7" - private val APP_ACTIVITY_CLASS_NAME_8 = "APP_ACTIVITY_CLASS_NAME_8" - private val APP_USER_1 = "APP_USER_1" - private val APP_USER_2 = "APP_USER_2" - private val APP_USER_3 = "APP_USER_3" - private val APP_USER_4 = "APP_USER_4" - private val APP_USER_5 = "APP_USER_5" - private val APP_USER_6 = "APP_USER_6" - private val APP_USER_7 = "APP_USER_7" - private val APP_USER_8 = "APP_USER_8" - - private val APP_NAME_SWIPE_LEFT = "APP_NAME_SWIPE_LEFT" - private val APP_NAME_SWIPE_RIGHT = "APP_NAME_SWIPE_RIGHT" - private val APP_PACKAGE_SWIPE_LEFT = "APP_PACKAGE_SWIPE_LEFT" - private val APP_PACKAGE_SWIPE_RIGHT = "APP_PACKAGE_SWIPE_RIGHT" - private val APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT = "APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT" - private val APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT = "APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT" - private val APP_USER_SWIPE_LEFT = "APP_USER_SWIPE_LEFT" - private val APP_USER_SWIPE_RIGHT = "APP_USER_SWIPE_RIGHT" - private val CLOCK_APP_PACKAGE = "CLOCK_APP_PACKAGE" - private val CLOCK_APP_USER = "CLOCK_APP_USER" - private val CLOCK_APP_CLASS_NAME = "CLOCK_APP_CLASS_NAME" - private val CALENDAR_APP_PACKAGE = "CALENDAR_APP_PACKAGE" - private val CALENDAR_APP_USER = "CALENDAR_APP_USER" - private val CALENDAR_APP_CLASS_NAME = "CALENDAR_APP_CLASS_NAME" - - private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, 0); - - var firstOpen: Boolean - get() = prefs.getBoolean(FIRST_OPEN, true) - set(value) = prefs.edit().putBoolean(FIRST_OPEN, value).apply() - - var firstOpenTime: Long - get() = prefs.getLong(FIRST_OPEN_TIME, 0L) - set(value) = prefs.edit().putLong(FIRST_OPEN_TIME, value).apply() - - var firstSettingsOpen: Boolean - get() = prefs.getBoolean(FIRST_SETTINGS_OPEN, true) - set(value) = prefs.edit().putBoolean(FIRST_SETTINGS_OPEN, value).apply() - - var firstHide: Boolean - get() = prefs.getBoolean(FIRST_HIDE, true) - set(value) = prefs.edit().putBoolean(FIRST_HIDE, value).apply() - - var userState: String - get() = prefs.getString(USER_STATE, Constants.UserState.START).toString() - set(value) = prefs.edit().putString(USER_STATE, value).apply() - - var lockModeOn: Boolean - get() = prefs.getBoolean(LOCK_MODE, false) - set(value) = prefs.edit().putBoolean(LOCK_MODE, value).apply() - - var autoShowKeyboard: Boolean - get() = prefs.getBoolean(AUTO_SHOW_KEYBOARD, true) - set(value) = prefs.edit().putBoolean(AUTO_SHOW_KEYBOARD, value).apply() - - var keyboardMessageShown: Boolean - get() = prefs.getBoolean(KEYBOARD_MESSAGE, false) - set(value) = prefs.edit().putBoolean(KEYBOARD_MESSAGE, value).apply() - - var dailyWallpaper: Boolean - get() = prefs.getBoolean(DAILY_WALLPAPER, false) - set(value) = prefs.edit().putBoolean(DAILY_WALLPAPER, value).apply() - - var dailyWallpaperUrl: String - get() = prefs.getString(DAILY_WALLPAPER_URL, "").toString() - set(value) = prefs.edit().putString(DAILY_WALLPAPER_URL, value).apply() - - var homeAppsNum: Int - get() = prefs.getInt(HOME_APPS_NUM, 4) - set(value) = prefs.edit().putInt(HOME_APPS_NUM, value).apply() - - var homeAlignment: Int - get() = prefs.getInt(HOME_ALIGNMENT, Gravity.START) - set(value) = prefs.edit().putInt(HOME_ALIGNMENT, value).apply() - - var homeBottomAlignment: Boolean - get() = prefs.getBoolean(HOME_BOTTOM_ALIGNMENT, false) - set(value) = prefs.edit().putBoolean(HOME_BOTTOM_ALIGNMENT, value).apply() - - var appLabelAlignment: Int - get() = prefs.getInt(APP_LABEL_ALIGNMENT, Gravity.START) - set(value) = prefs.edit().putInt(APP_LABEL_ALIGNMENT, value).apply() - - var showStatusBar: Boolean - get() = prefs.getBoolean(STATUS_BAR, false) - set(value) = prefs.edit().putBoolean(STATUS_BAR, value).apply() - - var dateTimeVisibility: Int - get() = prefs.getInt(DATE_TIME_VISIBILITY, Constants.DateTime.ON) - set(value) = prefs.edit().putInt(DATE_TIME_VISIBILITY, value).apply() - - var swipeLeftEnabled: Boolean - get() = prefs.getBoolean(SWIPE_LEFT_ENABLED, true) - set(value) = prefs.edit().putBoolean(SWIPE_LEFT_ENABLED, value).apply() - - var swipeRightEnabled: Boolean - get() = prefs.getBoolean(SWIPE_RIGHT_ENABLED, true) - set(value) = prefs.edit().putBoolean(SWIPE_RIGHT_ENABLED, value).apply() - - var appTheme: Int - get() = prefs.getInt(APP_THEME, AppCompatDelegate.MODE_NIGHT_YES) - set(value) = prefs.edit().putInt(APP_THEME, value).apply() - - var textSizeScale: Float - get() = prefs.getFloat(TEXT_SIZE_SCALE, 1.0f) - set(value) = prefs.edit().putFloat(TEXT_SIZE_SCALE, value).apply() - - var proMessageShown: Boolean - get() = prefs.getBoolean(PRO_MESSAGE_SHOWN, false) - set(value) = prefs.edit().putBoolean(PRO_MESSAGE_SHOWN, value).apply() - - var hideSetDefaultLauncher: Boolean - get() = prefs.getBoolean(HIDE_SET_DEFAULT_LAUNCHER, false) - set(value) = prefs.edit().putBoolean(HIDE_SET_DEFAULT_LAUNCHER, value).apply() - - var screenTimeLastUpdated: Long - get() = prefs.getLong(SCREEN_TIME_LAST_UPDATED, 0L) - set(value) = prefs.edit().putLong(SCREEN_TIME_LAST_UPDATED, value).apply() + private val PREFS_FILENAME = "app.olauncher" + + private val FIRST_OPEN = "FIRST_OPEN" + private val FIRST_OPEN_TIME = "FIRST_OPEN_TIME" + private val FIRST_SETTINGS_OPEN = "FIRST_SETTINGS_OPEN" + private val FIRST_HIDE = "FIRST_HIDE" + private val USER_STATE = "USER_STATE" + private val LOCK_MODE = "LOCK_MODE" + private val HOME_APPS_NUM = "HOME_APPS_NUM" + private val AUTO_SHOW_KEYBOARD = "AUTO_SHOW_KEYBOARD" + private val KEYBOARD_MESSAGE = "KEYBOARD_MESSAGE" + private val DAILY_WALLPAPER = "DAILY_WALLPAPER" + private val DAILY_WALLPAPER_URL = "DAILY_WALLPAPER_URL" + private val WALLPAPER_UPDATED_DAY = "WALLPAPER_UPDATED_DAY" + private val HOME_ALIGNMENT = "HOME_ALIGNMENT" + private val HOME_BOTTOM_ALIGNMENT = "HOME_BOTTOM_ALIGNMENT" + private val APP_LABEL_ALIGNMENT = "APP_LABEL_ALIGNMENT" + private val STATUS_BAR = "STATUS_BAR" + private val DATE_TIME_VISIBILITY = "DATE_TIME_VISIBILITY" + private val SWIPE_LEFT_ENABLED = "SWIPE_LEFT_ENABLED" + private val SWIPE_RIGHT_ENABLED = "SWIPE_RIGHT_ENABLED" + private val HIDDEN_APPS = "HIDDEN_APPS" + private val HIDDEN_APPS_UPDATED = "HIDDEN_APPS_UPDATED" + private val SHOW_HINT_COUNTER = "SHOW_HINT_COUNTER" + private val APP_THEME = "APP_THEME" + private val ABOUT_CLICKED = "ABOUT_CLICKED" + private val RATE_CLICKED = "RATE_CLICKED" + private val WALLPAPER_MSG_SHOWN = "WALLPAPER_MSG_SHOWN" + private val SHARE_SHOWN_TIME = "SHARE_SHOWN_TIME" + private val SWIPE_DOWN_ACTION = "SWIPE_DOWN_ACTION" + private val TEXT_SIZE_SCALE = "TEXT_SIZE_SCALE" + private val PRO_MESSAGE_SHOWN = "PRO_MESSAGE_SHOWN" + private val HIDE_SET_DEFAULT_LAUNCHER = "HIDE_SET_DEFAULT_LAUNCHER" + private val SCREEN_TIME_LAST_UPDATED = "SCREEN_TIME_LAST_UPDATED" + + private val APP_NAME_1 = "APP_NAME_1" + private val APP_NAME_2 = "APP_NAME_2" + private val APP_NAME_3 = "APP_NAME_3" + private val APP_NAME_4 = "APP_NAME_4" + private val APP_NAME_5 = "APP_NAME_5" + private val APP_NAME_6 = "APP_NAME_6" + private val APP_NAME_7 = "APP_NAME_7" + private val APP_NAME_8 = "APP_NAME_8" + private val APP_PACKAGE_1 = "APP_PACKAGE_1" + private val APP_PACKAGE_2 = "APP_PACKAGE_2" + private val APP_PACKAGE_3 = "APP_PACKAGE_3" + private val APP_PACKAGE_4 = "APP_PACKAGE_4" + private val APP_PACKAGE_5 = "APP_PACKAGE_5" + private val APP_PACKAGE_6 = "APP_PACKAGE_6" + private val APP_PACKAGE_7 = "APP_PACKAGE_7" + private val APP_PACKAGE_8 = "APP_PACKAGE_8" + private val APP_ACTIVITY_CLASS_NAME_1 = "APP_ACTIVITY_CLASS_NAME_1" + private val APP_ACTIVITY_CLASS_NAME_2 = "APP_ACTIVITY_CLASS_NAME_2" + private val APP_ACTIVITY_CLASS_NAME_3 = "APP_ACTIVITY_CLASS_NAME_3" + private val APP_ACTIVITY_CLASS_NAME_4 = "APP_ACTIVITY_CLASS_NAME_4" + private val APP_ACTIVITY_CLASS_NAME_5 = "APP_ACTIVITY_CLASS_NAME_5" + private val APP_ACTIVITY_CLASS_NAME_6 = "APP_ACTIVITY_CLASS_NAME_6" + private val APP_ACTIVITY_CLASS_NAME_7 = "APP_ACTIVITY_CLASS_NAME_7" + private val APP_ACTIVITY_CLASS_NAME_8 = "APP_ACTIVITY_CLASS_NAME_8" + private val APP_USER_1 = "APP_USER_1" + private val APP_USER_2 = "APP_USER_2" + private val APP_USER_3 = "APP_USER_3" + private val APP_USER_4 = "APP_USER_4" + private val APP_USER_5 = "APP_USER_5" + private val APP_USER_6 = "APP_USER_6" + private val APP_USER_7 = "APP_USER_7" + private val APP_USER_8 = "APP_USER_8" + + private val APP_NAME_SWIPE_LEFT = "APP_NAME_SWIPE_LEFT" + private val APP_NAME_SWIPE_RIGHT = "APP_NAME_SWIPE_RIGHT" + private val APP_PACKAGE_SWIPE_LEFT = "APP_PACKAGE_SWIPE_LEFT" + private val APP_PACKAGE_SWIPE_RIGHT = "APP_PACKAGE_SWIPE_RIGHT" + private val APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT = "APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT" + private val APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT = "APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT" + private val APP_USER_SWIPE_LEFT = "APP_USER_SWIPE_LEFT" + private val APP_USER_SWIPE_RIGHT = "APP_USER_SWIPE_RIGHT" + private val CLOCK_APP_PACKAGE = "CLOCK_APP_PACKAGE" + private val CLOCK_APP_USER = "CLOCK_APP_USER" + private val CLOCK_APP_CLASS_NAME = "CLOCK_APP_CLASS_NAME" + private val CALENDAR_APP_PACKAGE = "CALENDAR_APP_PACKAGE" + private val CALENDAR_APP_USER = "CALENDAR_APP_USER" + private val CALENDAR_APP_CLASS_NAME = "CALENDAR_APP_CLASS_NAME" + private val APP_LIMITS = "APP_LIMITS" + + private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_FILENAME, 0) + + var firstOpen: Boolean + get() = prefs.getBoolean(FIRST_OPEN, true) + set(value) = prefs.edit().putBoolean(FIRST_OPEN, value).apply() + + var firstOpenTime: Long + get() = prefs.getLong(FIRST_OPEN_TIME, 0L) + set(value) = prefs.edit().putLong(FIRST_OPEN_TIME, value).apply() + + var firstSettingsOpen: Boolean + get() = prefs.getBoolean(FIRST_SETTINGS_OPEN, true) + set(value) = prefs.edit().putBoolean(FIRST_SETTINGS_OPEN, value).apply() + + var firstHide: Boolean + get() = prefs.getBoolean(FIRST_HIDE, true) + set(value) = prefs.edit().putBoolean(FIRST_HIDE, value).apply() + + var userState: String + get() = prefs.getString(USER_STATE, Constants.UserState.START).toString() + set(value) = prefs.edit().putString(USER_STATE, value).apply() + + var lockModeOn: Boolean + get() = prefs.getBoolean(LOCK_MODE, false) + set(value) = prefs.edit().putBoolean(LOCK_MODE, value).apply() + + var autoShowKeyboard: Boolean + get() = prefs.getBoolean(AUTO_SHOW_KEYBOARD, true) + set(value) = prefs.edit().putBoolean(AUTO_SHOW_KEYBOARD, value).apply() + + var keyboardMessageShown: Boolean + get() = prefs.getBoolean(KEYBOARD_MESSAGE, false) + set(value) = prefs.edit().putBoolean(KEYBOARD_MESSAGE, value).apply() + + var dailyWallpaper: Boolean + get() = prefs.getBoolean(DAILY_WALLPAPER, false) + set(value) = prefs.edit().putBoolean(DAILY_WALLPAPER, value).apply() + + var dailyWallpaperUrl: String + get() = prefs.getString(DAILY_WALLPAPER_URL, "").toString() + set(value) = prefs.edit().putString(DAILY_WALLPAPER_URL, value).apply() + + var homeAppsNum: Int + get() = prefs.getInt(HOME_APPS_NUM, 4) + set(value) = prefs.edit().putInt(HOME_APPS_NUM, value).apply() + + var homeAlignment: Int + get() = prefs.getInt(HOME_ALIGNMENT, Gravity.START) + set(value) = prefs.edit().putInt(HOME_ALIGNMENT, value).apply() + + var homeBottomAlignment: Boolean + get() = prefs.getBoolean(HOME_BOTTOM_ALIGNMENT, false) + set(value) = prefs.edit().putBoolean(HOME_BOTTOM_ALIGNMENT, value).apply() + + var appLabelAlignment: Int + get() = prefs.getInt(APP_LABEL_ALIGNMENT, Gravity.START) + set(value) = prefs.edit().putInt(APP_LABEL_ALIGNMENT, value).apply() + + var showStatusBar: Boolean + get() = prefs.getBoolean(STATUS_BAR, false) + set(value) = prefs.edit().putBoolean(STATUS_BAR, value).apply() + + var dateTimeVisibility: Int + get() = prefs.getInt(DATE_TIME_VISIBILITY, Constants.DateTime.ON) + set(value) = prefs.edit().putInt(DATE_TIME_VISIBILITY, value).apply() + + var swipeLeftEnabled: Boolean + get() = prefs.getBoolean(SWIPE_LEFT_ENABLED, true) + set(value) = prefs.edit().putBoolean(SWIPE_LEFT_ENABLED, value).apply() + + var swipeRightEnabled: Boolean + get() = prefs.getBoolean(SWIPE_RIGHT_ENABLED, true) + set(value) = prefs.edit().putBoolean(SWIPE_RIGHT_ENABLED, value).apply() + + var appTheme: Int + get() = prefs.getInt(APP_THEME, AppCompatDelegate.MODE_NIGHT_YES) + set(value) = prefs.edit().putInt(APP_THEME, value).apply() + + var textSizeScale: Float + get() = prefs.getFloat(TEXT_SIZE_SCALE, 1.0f) + set(value) = prefs.edit().putFloat(TEXT_SIZE_SCALE, value).apply() + + var proMessageShown: Boolean + get() = prefs.getBoolean(PRO_MESSAGE_SHOWN, false) + set(value) = prefs.edit().putBoolean(PRO_MESSAGE_SHOWN, value).apply() + + var hideSetDefaultLauncher: Boolean + get() = prefs.getBoolean(HIDE_SET_DEFAULT_LAUNCHER, false) + set(value) = prefs.edit().putBoolean(HIDE_SET_DEFAULT_LAUNCHER, value).apply() + + var screenTimeLastUpdated: Long + get() = prefs.getLong(SCREEN_TIME_LAST_UPDATED, 0L) + set(value) = prefs.edit().putLong(SCREEN_TIME_LAST_UPDATED, value).apply() - var hiddenApps: MutableSet - get() = prefs.getStringSet(HIDDEN_APPS, mutableSetOf()) as MutableSet - set(value) = prefs.edit().putStringSet(HIDDEN_APPS, value).apply() + var hiddenApps: MutableSet + get() = prefs.getStringSet(HIDDEN_APPS, mutableSetOf()) as MutableSet + set(value) = prefs.edit().putStringSet(HIDDEN_APPS, value).apply() - var hiddenAppsUpdated: Boolean - get() = prefs.getBoolean(HIDDEN_APPS_UPDATED, false) - set(value) = prefs.edit().putBoolean(HIDDEN_APPS_UPDATED, value).apply() + var hiddenAppsUpdated: Boolean + get() = prefs.getBoolean(HIDDEN_APPS_UPDATED, false) + set(value) = prefs.edit().putBoolean(HIDDEN_APPS_UPDATED, value).apply() - var toShowHintCounter: Int - get() = prefs.getInt(SHOW_HINT_COUNTER, 1) - set(value) = prefs.edit().putInt(SHOW_HINT_COUNTER, value).apply() + var toShowHintCounter: Int + get() = prefs.getInt(SHOW_HINT_COUNTER, 1) + set(value) = prefs.edit().putInt(SHOW_HINT_COUNTER, value).apply() - var aboutClicked: Boolean - get() = prefs.getBoolean(ABOUT_CLICKED, false) - set(value) = prefs.edit().putBoolean(ABOUT_CLICKED, value).apply() + var aboutClicked: Boolean + get() = prefs.getBoolean(ABOUT_CLICKED, false) + set(value) = prefs.edit().putBoolean(ABOUT_CLICKED, value).apply() - var rateClicked: Boolean - get() = prefs.getBoolean(RATE_CLICKED, false) - set(value) = prefs.edit().putBoolean(RATE_CLICKED, value).apply() + var rateClicked: Boolean + get() = prefs.getBoolean(RATE_CLICKED, false) + set(value) = prefs.edit().putBoolean(RATE_CLICKED, value).apply() - var wallpaperMsgShown: Boolean - get() = prefs.getBoolean(WALLPAPER_MSG_SHOWN, false) - set(value) = prefs.edit().putBoolean(WALLPAPER_MSG_SHOWN, value).apply() + var wallpaperMsgShown: Boolean + get() = prefs.getBoolean(WALLPAPER_MSG_SHOWN, false) + set(value) = prefs.edit().putBoolean(WALLPAPER_MSG_SHOWN, value).apply() - var shareShownTime: Long - get() = prefs.getLong(SHARE_SHOWN_TIME, 0L) - set(value) = prefs.edit().putLong(SHARE_SHOWN_TIME, value).apply() + var shareShownTime: Long + get() = prefs.getLong(SHARE_SHOWN_TIME, 0L) + set(value) = prefs.edit().putLong(SHARE_SHOWN_TIME, value).apply() - var swipeDownAction: Int - get() = prefs.getInt(SWIPE_DOWN_ACTION, Constants.SwipeDownAction.NOTIFICATIONS) - set(value) = prefs.edit().putInt(SWIPE_DOWN_ACTION, value).apply() + var swipeDownAction: Int + get() = prefs.getInt(SWIPE_DOWN_ACTION, Constants.SwipeDownAction.NOTIFICATIONS) + set(value) = prefs.edit().putInt(SWIPE_DOWN_ACTION, value).apply() - var appName1: String - get() = prefs.getString(APP_NAME_1, "").toString() - set(value) = prefs.edit().putString(APP_NAME_1, value).apply() + var appName1: String + get() = prefs.getString(APP_NAME_1, "").toString() + set(value) = prefs.edit().putString(APP_NAME_1, value).apply() - var appName2: String - get() = prefs.getString(APP_NAME_2, "").toString() - set(value) = prefs.edit().putString(APP_NAME_2, value).apply() + var appName2: String + get() = prefs.getString(APP_NAME_2, "").toString() + set(value) = prefs.edit().putString(APP_NAME_2, value).apply() - var appName3: String - get() = prefs.getString(APP_NAME_3, "").toString() - set(value) = prefs.edit().putString(APP_NAME_3, value).apply() + var appName3: String + get() = prefs.getString(APP_NAME_3, "").toString() + set(value) = prefs.edit().putString(APP_NAME_3, value).apply() - var appName4: String - get() = prefs.getString(APP_NAME_4, "").toString() - set(value) = prefs.edit().putString(APP_NAME_4, value).apply() + var appName4: String + get() = prefs.getString(APP_NAME_4, "").toString() + set(value) = prefs.edit().putString(APP_NAME_4, value).apply() - var appName5: String - get() = prefs.getString(APP_NAME_5, "").toString() - set(value) = prefs.edit().putString(APP_NAME_5, value).apply() + var appName5: String + get() = prefs.getString(APP_NAME_5, "").toString() + set(value) = prefs.edit().putString(APP_NAME_5, value).apply() - var appName6: String - get() = prefs.getString(APP_NAME_6, "").toString() - set(value) = prefs.edit().putString(APP_NAME_6, value).apply() + var appName6: String + get() = prefs.getString(APP_NAME_6, "").toString() + set(value) = prefs.edit().putString(APP_NAME_6, value).apply() - var appName7: String - get() = prefs.getString(APP_NAME_7, "").toString() - set(value) = prefs.edit().putString(APP_NAME_7, value).apply() + var appName7: String + get() = prefs.getString(APP_NAME_7, "").toString() + set(value) = prefs.edit().putString(APP_NAME_7, value).apply() - var appName8: String - get() = prefs.getString(APP_NAME_8, "").toString() - set(value) = prefs.edit().putString(APP_NAME_8, value).apply() + var appName8: String + get() = prefs.getString(APP_NAME_8, "").toString() + set(value) = prefs.edit().putString(APP_NAME_8, value).apply() - var appPackage1: String - get() = prefs.getString(APP_PACKAGE_1, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_1, value).apply() + var appPackage1: String + get() = prefs.getString(APP_PACKAGE_1, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_1, value).apply() - var appPackage2: String - get() = prefs.getString(APP_PACKAGE_2, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_2, value).apply() + var appPackage2: String + get() = prefs.getString(APP_PACKAGE_2, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_2, value).apply() - var appPackage3: String - get() = prefs.getString(APP_PACKAGE_3, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_3, value).apply() + var appPackage3: String + get() = prefs.getString(APP_PACKAGE_3, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_3, value).apply() - var appPackage4: String - get() = prefs.getString(APP_PACKAGE_4, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_4, value).apply() + var appPackage4: String + get() = prefs.getString(APP_PACKAGE_4, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_4, value).apply() - var appPackage5: String - get() = prefs.getString(APP_PACKAGE_5, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_5, value).apply() + var appPackage5: String + get() = prefs.getString(APP_PACKAGE_5, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_5, value).apply() - var appPackage6: String - get() = prefs.getString(APP_PACKAGE_6, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_6, value).apply() + var appPackage6: String + get() = prefs.getString(APP_PACKAGE_6, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_6, value).apply() - var appPackage7: String - get() = prefs.getString(APP_PACKAGE_7, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_7, value).apply() + var appPackage7: String + get() = prefs.getString(APP_PACKAGE_7, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_7, value).apply() - var appPackage8: String - get() = prefs.getString(APP_PACKAGE_8, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_8, value).apply() + var appPackage8: String + get() = prefs.getString(APP_PACKAGE_8, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_8, value).apply() - var appActivityClassName1: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_1, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_1, value).apply() + var appActivityClassName1: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_1, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_1, value).apply() - var appActivityClassName2: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_2, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_2, value).apply() + var appActivityClassName2: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_2, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_2, value).apply() - var appActivityClassName3: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_3, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_3, value).apply() + var appActivityClassName3: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_3, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_3, value).apply() - var appActivityClassName4: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_4, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_4, value).apply() + var appActivityClassName4: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_4, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_4, value).apply() - var appActivityClassName5: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_5, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_5, value).apply() + var appActivityClassName5: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_5, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_5, value).apply() - var appActivityClassName6: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_6, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_6, value).apply() + var appActivityClassName6: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_6, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_6, value).apply() - var appActivityClassName7: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_7, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_7, value).apply() - - var appActivityClassName8: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_8, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_8, value).apply() - - var appUser1: String - get() = prefs.getString(APP_USER_1, "").toString() - set(value) = prefs.edit().putString(APP_USER_1, value).apply() - - var appUser2: String - get() = prefs.getString(APP_USER_2, "").toString() - set(value) = prefs.edit().putString(APP_USER_2, value).apply() - - var appUser3: String - get() = prefs.getString(APP_USER_3, "").toString() - set(value) = prefs.edit().putString(APP_USER_3, value).apply() - - var appUser4: String - get() = prefs.getString(APP_USER_4, "").toString() - set(value) = prefs.edit().putString(APP_USER_4, value).apply() - - var appUser5: String - get() = prefs.getString(APP_USER_5, "").toString() - set(value) = prefs.edit().putString(APP_USER_5, value).apply() - - var appUser6: String - get() = prefs.getString(APP_USER_6, "").toString() - set(value) = prefs.edit().putString(APP_USER_6, value).apply() - - var appUser7: String - get() = prefs.getString(APP_USER_7, "").toString() - set(value) = prefs.edit().putString(APP_USER_7, value).apply() - - var appUser8: String - get() = prefs.getString(APP_USER_8, "").toString() - set(value) = prefs.edit().putString(APP_USER_8, value).apply() - - var appNameSwipeLeft: String - get() = prefs.getString(APP_NAME_SWIPE_LEFT, "Camera").toString() - set(value) = prefs.edit().putString(APP_NAME_SWIPE_LEFT, value).apply() - - var appNameSwipeRight: String - get() = prefs.getString(APP_NAME_SWIPE_RIGHT, "Phone").toString() - set(value) = prefs.edit().putString(APP_NAME_SWIPE_RIGHT, value).apply() - - var appPackageSwipeLeft: String - get() = prefs.getString(APP_PACKAGE_SWIPE_LEFT, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_SWIPE_LEFT, value).apply() - - var appActivityClassNameSwipeLeft: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT, value).apply() - - var appPackageSwipeRight: String - get() = prefs.getString(APP_PACKAGE_SWIPE_RIGHT, "").toString() - set(value) = prefs.edit().putString(APP_PACKAGE_SWIPE_RIGHT, value).apply() - - var appActivityClassNameRight: String? - get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT, "").toString() - set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT, value).apply() - - var appUserSwipeLeft: String - get() = prefs.getString(APP_USER_SWIPE_LEFT, "").toString() - set(value) = prefs.edit().putString(APP_USER_SWIPE_LEFT, value).apply() - - var appUserSwipeRight: String - get() = prefs.getString(APP_USER_SWIPE_RIGHT, "").toString() - set(value) = prefs.edit().putString(APP_USER_SWIPE_RIGHT, value).apply() - - var clockAppPackage: String - get() = prefs.getString(CLOCK_APP_PACKAGE, "").toString() - set(value) = prefs.edit().putString(CLOCK_APP_PACKAGE, value).apply() - - var clockAppUser: String - get() = prefs.getString(CLOCK_APP_USER, "").toString() - set(value) = prefs.edit().putString(CLOCK_APP_USER, value).apply() - - var clockAppClassName: String? - get() = prefs.getString(CLOCK_APP_CLASS_NAME, "").toString() - set(value) = prefs.edit().putString(CLOCK_APP_CLASS_NAME, value).apply() - - var calendarAppPackage: String - get() = prefs.getString(CALENDAR_APP_PACKAGE, "").toString() - set(value) = prefs.edit().putString(CALENDAR_APP_PACKAGE, value).apply() - - var calendarAppUser: String - get() = prefs.getString(CALENDAR_APP_USER, "").toString() - set(value) = prefs.edit().putString(CALENDAR_APP_USER, value).apply() - - var calendarAppClassName: String? - get() = prefs.getString(CALENDAR_APP_CLASS_NAME, "").toString() - set(value) = prefs.edit().putString(CALENDAR_APP_CLASS_NAME, value).apply() - - fun getAppName(location: Int): String { - return when (location) { - 1 -> prefs.getString(APP_NAME_1, "").toString() - 2 -> prefs.getString(APP_NAME_2, "").toString() - 3 -> prefs.getString(APP_NAME_3, "").toString() - 4 -> prefs.getString(APP_NAME_4, "").toString() - 5 -> prefs.getString(APP_NAME_5, "").toString() - 6 -> prefs.getString(APP_NAME_6, "").toString() - 7 -> prefs.getString(APP_NAME_7, "").toString() - 8 -> prefs.getString(APP_NAME_8, "").toString() - else -> "" - } + var appActivityClassName7: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_7, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_7, value).apply() + + var appActivityClassName8: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_8, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_8, value).apply() + + var appUser1: String + get() = prefs.getString(APP_USER_1, "").toString() + set(value) = prefs.edit().putString(APP_USER_1, value).apply() + + var appUser2: String + get() = prefs.getString(APP_USER_2, "").toString() + set(value) = prefs.edit().putString(APP_USER_2, value).apply() + + var appUser3: String + get() = prefs.getString(APP_USER_3, "").toString() + set(value) = prefs.edit().putString(APP_USER_3, value).apply() + + var appUser4: String + get() = prefs.getString(APP_USER_4, "").toString() + set(value) = prefs.edit().putString(APP_USER_4, value).apply() + + var appUser5: String + get() = prefs.getString(APP_USER_5, "").toString() + set(value) = prefs.edit().putString(APP_USER_5, value).apply() + + var appUser6: String + get() = prefs.getString(APP_USER_6, "").toString() + set(value) = prefs.edit().putString(APP_USER_6, value).apply() + + var appUser7: String + get() = prefs.getString(APP_USER_7, "").toString() + set(value) = prefs.edit().putString(APP_USER_7, value).apply() + + var appUser8: String + get() = prefs.getString(APP_USER_8, "").toString() + set(value) = prefs.edit().putString(APP_USER_8, value).apply() + + var appNameSwipeLeft: String + get() = prefs.getString(APP_NAME_SWIPE_LEFT, "Camera").toString() + set(value) = prefs.edit().putString(APP_NAME_SWIPE_LEFT, value).apply() + + var appNameSwipeRight: String + get() = prefs.getString(APP_NAME_SWIPE_RIGHT, "Phone").toString() + set(value) = prefs.edit().putString(APP_NAME_SWIPE_RIGHT, value).apply() + + var appPackageSwipeLeft: String + get() = prefs.getString(APP_PACKAGE_SWIPE_LEFT, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_SWIPE_LEFT, value).apply() + + var appActivityClassNameSwipeLeft: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_SWIPE_LEFT, value).apply() + + var appPackageSwipeRight: String + get() = prefs.getString(APP_PACKAGE_SWIPE_RIGHT, "").toString() + set(value) = prefs.edit().putString(APP_PACKAGE_SWIPE_RIGHT, value).apply() + + var appActivityClassNameRight: String? + get() = prefs.getString(APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT, "").toString() + set(value) = prefs.edit().putString(APP_ACTIVITY_CLASS_NAME_SWIPE_RIGHT, value).apply() + + var appUserSwipeLeft: String + get() = prefs.getString(APP_USER_SWIPE_LEFT, "").toString() + set(value) = prefs.edit().putString(APP_USER_SWIPE_LEFT, value).apply() + + var appUserSwipeRight: String + get() = prefs.getString(APP_USER_SWIPE_RIGHT, "").toString() + set(value) = prefs.edit().putString(APP_USER_SWIPE_RIGHT, value).apply() + + var clockAppPackage: String + get() = prefs.getString(CLOCK_APP_PACKAGE, "").toString() + set(value) = prefs.edit().putString(CLOCK_APP_PACKAGE, value).apply() + + var clockAppUser: String + get() = prefs.getString(CLOCK_APP_USER, "").toString() + set(value) = prefs.edit().putString(CLOCK_APP_USER, value).apply() + + var clockAppClassName: String? + get() = prefs.getString(CLOCK_APP_CLASS_NAME, "").toString() + set(value) = prefs.edit().putString(CLOCK_APP_CLASS_NAME, value).apply() + + var calendarAppPackage: String + get() = prefs.getString(CALENDAR_APP_PACKAGE, "").toString() + set(value) = prefs.edit().putString(CALENDAR_APP_PACKAGE, value).apply() + + var calendarAppUser: String + get() = prefs.getString(CALENDAR_APP_USER, "").toString() + set(value) = prefs.edit().putString(CALENDAR_APP_USER, value).apply() + + var calendarAppClassName: String? + get() = prefs.getString(CALENDAR_APP_CLASS_NAME, "").toString() + set(value) = prefs.edit().putString(CALENDAR_APP_CLASS_NAME, value).apply() + + fun getAppName(location: Int): String { + return when (location) { + 1 -> prefs.getString(APP_NAME_1, "").toString() + 2 -> prefs.getString(APP_NAME_2, "").toString() + 3 -> prefs.getString(APP_NAME_3, "").toString() + 4 -> prefs.getString(APP_NAME_4, "").toString() + 5 -> prefs.getString(APP_NAME_5, "").toString() + 6 -> prefs.getString(APP_NAME_6, "").toString() + 7 -> prefs.getString(APP_NAME_7, "").toString() + 8 -> prefs.getString(APP_NAME_8, "").toString() + else -> "" } - - fun getAppPackage(location: Int): String { - return when (location) { - 1 -> prefs.getString(APP_PACKAGE_1, "").toString() - 2 -> prefs.getString(APP_PACKAGE_2, "").toString() - 3 -> prefs.getString(APP_PACKAGE_3, "").toString() - 4 -> prefs.getString(APP_PACKAGE_4, "").toString() - 5 -> prefs.getString(APP_PACKAGE_5, "").toString() - 6 -> prefs.getString(APP_PACKAGE_6, "").toString() - 7 -> prefs.getString(APP_PACKAGE_7, "").toString() - 8 -> prefs.getString(APP_PACKAGE_8, "").toString() - else -> "" - } + } + + fun getAppPackage(location: Int): String { + return when (location) { + 1 -> prefs.getString(APP_PACKAGE_1, "").toString() + 2 -> prefs.getString(APP_PACKAGE_2, "").toString() + 3 -> prefs.getString(APP_PACKAGE_3, "").toString() + 4 -> prefs.getString(APP_PACKAGE_4, "").toString() + 5 -> prefs.getString(APP_PACKAGE_5, "").toString() + 6 -> prefs.getString(APP_PACKAGE_6, "").toString() + 7 -> prefs.getString(APP_PACKAGE_7, "").toString() + 8 -> prefs.getString(APP_PACKAGE_8, "").toString() + else -> "" } - - fun getAppActivityClassName(location: Int): String { - return when (location) { - 1 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_1, "").toString() - 2 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_2, "").toString() - 3 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_3, "").toString() - 4 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_4, "").toString() - 5 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_5, "").toString() - 6 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_6, "").toString() - 7 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_7, "").toString() - 8 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_8, "").toString() - else -> "" - } + } + + fun getAppActivityClassName(location: Int): String { + return when (location) { + 1 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_1, "").toString() + 2 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_2, "").toString() + 3 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_3, "").toString() + 4 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_4, "").toString() + 5 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_5, "").toString() + 6 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_6, "").toString() + 7 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_7, "").toString() + 8 -> prefs.getString(APP_ACTIVITY_CLASS_NAME_8, "").toString() + else -> "" } - - fun getAppUser(location: Int): String { - return when (location) { - 1 -> prefs.getString(APP_USER_1, "").toString() - 2 -> prefs.getString(APP_USER_2, "").toString() - 3 -> prefs.getString(APP_USER_3, "").toString() - 4 -> prefs.getString(APP_USER_4, "").toString() - 5 -> prefs.getString(APP_USER_5, "").toString() - 6 -> prefs.getString(APP_USER_6, "").toString() - 7 -> prefs.getString(APP_USER_7, "").toString() - 8 -> prefs.getString(APP_USER_8, "").toString() - else -> "" - } + } + + fun getAppUser(location: Int): String { + return when (location) { + 1 -> prefs.getString(APP_USER_1, "").toString() + 2 -> prefs.getString(APP_USER_2, "").toString() + 3 -> prefs.getString(APP_USER_3, "").toString() + 4 -> prefs.getString(APP_USER_4, "").toString() + 5 -> prefs.getString(APP_USER_5, "").toString() + 6 -> prefs.getString(APP_USER_6, "").toString() + 7 -> prefs.getString(APP_USER_7, "").toString() + 8 -> prefs.getString(APP_USER_8, "").toString() + else -> "" } - - fun getAppRenameLabel(appPackage: String): String = prefs.getString(appPackage, "").toString() - - fun setAppRenameLabel(appPackage: String, renameLabel: String) = prefs.edit().putString(appPackage, renameLabel).apply() -} \ No newline at end of file + } + + fun getAppRenameLabel(appPackage: String): String = prefs.getString(appPackage, "").toString() + + fun setAppRenameLabel(appPackage: String, renameLabel: String) = + prefs.edit().putString(appPackage, renameLabel).apply() + + // App Limits + fun getAppLimits(): List { + val limitsJson = prefs.getString(APP_LIMITS, "[]") ?: "[]" + val limits = mutableListOf() + try { + val jsonArray = org.json.JSONArray(limitsJson) + for (i in 0 until jsonArray.length()) { + limits.add(AppLimitModel.fromJson(jsonArray.getJSONObject(i))) + } + } catch (e: Exception) { + e.printStackTrace() + } + return limits + } + + fun saveAppLimits(limits: List) { + try { + val jsonArray = org.json.JSONArray() + for (limit in limits) { + jsonArray.put(limit.toJson()) + } + prefs.edit().putString(APP_LIMITS, jsonArray.toString()).apply() + } catch (e: Exception) { + e.printStackTrace() + } + } + + fun addAppLimit(limit: AppLimitModel) { + val limits = getAppLimits().toMutableList() + // Remove any existing limit for this app + limits.removeAll { it.appPackage == limit.appPackage } + limits.add(limit) + saveAppLimits(limits) + } + + fun removeAppLimit(appPackage: String) { + val limits = getAppLimits().toMutableList() + limits.removeAll { it.appPackage == appPackage } + saveAppLimits(limits) + } + + fun getAppLimit(appPackage: String): AppLimitModel? { + return getAppLimits().find { it.appPackage == appPackage } + } +} diff --git a/app/src/main/java/app/olauncher/ui/AppDrawerFragment.kt b/app/src/main/java/app/olauncher/ui/AppDrawerFragment.kt index 1986300fd..b75bcc038 100644 --- a/app/src/main/java/app/olauncher/ui/AppDrawerFragment.kt +++ b/app/src/main/java/app/olauncher/ui/AppDrawerFragment.kt @@ -28,260 +28,276 @@ import app.olauncher.helper.showKeyboard import app.olauncher.helper.showToast import app.olauncher.helper.uninstall - class AppDrawerFragment : Fragment() { - private lateinit var prefs: Prefs - private lateinit var adapter: AppDrawerAdapter - private lateinit var linearLayoutManager: LinearLayoutManager + private lateinit var prefs: Prefs + private lateinit var adapter: AppDrawerAdapter + private lateinit var linearLayoutManager: LinearLayoutManager - private var flag = Constants.FLAG_LAUNCH_APP - private var canRename = false + private var flag = Constants.FLAG_LAUNCH_APP + private var canRename = false - private val viewModel: MainViewModel by activityViewModels() - private var _binding: FragmentAppDrawerBinding? = null - private val binding get() = _binding!! + private val viewModel: MainViewModel by activityViewModels() + private var _binding: FragmentAppDrawerBinding? = null + private val binding + get() = _binding!! - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle?, - ): View { - _binding = FragmentAppDrawerBinding.inflate(inflater, container, false) - return binding.root - } + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle?, + ): View { + _binding = FragmentAppDrawerBinding.inflate(inflater, container, false) + return binding.root + } - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - prefs = Prefs(requireContext()) - arguments?.let { - flag = it.getInt(Constants.Key.FLAG, Constants.FLAG_LAUNCH_APP) - canRename = it.getBoolean(Constants.Key.RENAME, false) - } - initViews() - initSearch() - initAdapter() - initObservers() - initClickListeners() + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + prefs = Prefs(requireContext()) + arguments?.let { + flag = it.getInt(Constants.Key.FLAG, Constants.FLAG_LAUNCH_APP) + canRename = it.getBoolean(Constants.Key.RENAME, false) } + initViews() + initSearch() + initAdapter() + initObservers() + initClickListeners() + } - private fun initViews() { - if (flag == Constants.FLAG_HIDDEN_APPS) + private fun initViews() { + if (flag == Constants.FLAG_HIDDEN_APPS) binding.search.queryHint = getString(R.string.hidden_apps) - else if (flag in Constants.FLAG_SET_HOME_APP_1..Constants.FLAG_SET_CALENDAR_APP) + else if (flag == Constants.FLAG_SET_APP_LIMIT) + binding.search.queryHint = getString(R.string.select_app_for_limit) + else if (flag in Constants.FLAG_SET_HOME_APP_1..Constants.FLAG_SET_CALENDAR_APP) binding.search.queryHint = "Please select an app" - try { - val searchTextView = binding.search.findViewById(R.id.search_src_text) - if (searchTextView != null) searchTextView.gravity = prefs.appLabelAlignment - } catch (e: Exception) { - e.printStackTrace() - } + try { + val searchTextView = binding.search.findViewById(R.id.search_src_text) + if (searchTextView != null) searchTextView.gravity = prefs.appLabelAlignment + } catch (e: Exception) { + e.printStackTrace() } + } - private fun initSearch() { - binding.search.setOnQueryTextListener(object : SearchView.OnQueryTextListener { - override fun onQueryTextSubmit(query: String?): Boolean { + private fun initSearch() { + binding.search.setOnQueryTextListener( + object : SearchView.OnQueryTextListener { + override fun onQueryTextSubmit(query: String?): Boolean { if (query?.startsWith("!") == true) - requireContext().openUrl(Constants.URL_DUCK_SEARCH + query.replace(" ", "%20")) - else if (adapter.itemCount == 0) // && requireContext().searchOnPlayStore(query?.trim()).not()) - requireContext().openSearch(query?.trim()) - else - adapter.launchFirstInList() + requireContext() + .openUrl(Constants.URL_DUCK_SEARCH + query.replace(" ", "%20")) + else if (adapter.itemCount == 0 + ) // && requireContext().searchOnPlayStore(query?.trim()).not()) + requireContext().openSearch(query?.trim()) + else adapter.launchFirstInList() return true - } + } - override fun onQueryTextChange(newText: String): Boolean { + override fun onQueryTextChange(newText: String): Boolean { try { - adapter.filter.filter(newText) - binding.appDrawerTip.visibility = View.GONE - binding.appRename.visibility = if (canRename && newText.isNotBlank()) View.VISIBLE else View.GONE - return true + adapter.filter.filter(newText) + binding.appDrawerTip.visibility = View.GONE + binding.appRename.visibility = + if (canRename && newText.isNotBlank()) View.VISIBLE else View.GONE + return true } catch (e: Exception) { - e.printStackTrace() + e.printStackTrace() } return false + } } - }) - } + ) + } - private fun initAdapter() { - adapter = AppDrawerAdapter( - flag, - prefs.appLabelAlignment, - appClickListener = { - if (it.appPackage.isEmpty()) - return@AppDrawerAdapter - viewModel.selectedApp(it, flag) - if (flag == Constants.FLAG_LAUNCH_APP || flag == Constants.FLAG_HIDDEN_APPS) - findNavController().popBackStack(R.id.mainFragment, false) - else - findNavController().popBackStack() - }, - appInfoListener = { - openAppInfo( - requireContext(), - it.user, - it.appPackage - ) - findNavController().popBackStack(R.id.mainFragment, false) - }, - appDeleteListener = { - requireContext().apply { - if (isSystemApp(it.appPackage)) - showToast(getString(R.string.system_app_cannot_delete)) - else - uninstall(it.appPackage) - } - }, - appHideListener = { appModel, position -> - adapter.appFilteredList.removeAt(position) - adapter.notifyItemRemoved(position) - adapter.appsList.remove(appModel) + private fun initAdapter() { + adapter = + AppDrawerAdapter( + flag, + prefs.appLabelAlignment, + appClickListener = { + if (it.appPackage.isEmpty()) return@AppDrawerAdapter - val newSet = mutableSetOf() - newSet.addAll(prefs.hiddenApps) - if (flag == Constants.FLAG_HIDDEN_APPS) { - newSet.remove(appModel.appPackage) // for backward compatibility - newSet.remove(appModel.appPackage + "|" + appModel.user.toString()) - } else - newSet.add(appModel.appPackage + "|" + appModel.user.toString()) + // Special handling for app limit selection + if (flag == Constants.FLAG_SET_APP_LIMIT) { + findNavController() + .navigate( + R.id.action_appListFragment_to_appLimitConfigFragment, + androidx.core.os.bundleOf( + Constants.Key.APP_PACKAGE to it.appPackage, + Constants.Key.APP_NAME to it.appLabel + ) + ) + return@AppDrawerAdapter + } - prefs.hiddenApps = newSet - if (newSet.isEmpty()) - findNavController().popBackStack() - if (prefs.firstHide) { - binding.search.hideKeyboard() - prefs.firstHide = false - viewModel.showDialog.postValue(Constants.Dialog.HIDDEN) - findNavController().navigate(R.id.action_appListFragment_to_settingsFragment2) - } - viewModel.getAppList() - viewModel.getHiddenApps() - }, - appRenameListener = { appModel, renameLabel -> - prefs.setAppRenameLabel(appModel.appPackage, renameLabel) - viewModel.getAppList() - } - ) + viewModel.selectedApp(it, flag) + if (flag == Constants.FLAG_LAUNCH_APP || flag == Constants.FLAG_HIDDEN_APPS) + findNavController().popBackStack(R.id.mainFragment, false) + else findNavController().popBackStack() + }, + appInfoListener = { + openAppInfo(requireContext(), it.user, it.appPackage) + findNavController().popBackStack(R.id.mainFragment, false) + }, + appDeleteListener = { + requireContext().apply { + if (isSystemApp(it.appPackage)) + showToast(getString(R.string.system_app_cannot_delete)) + else uninstall(it.appPackage) + } + }, + appHideListener = { appModel, position -> + adapter.appFilteredList.removeAt(position) + adapter.notifyItemRemoved(position) + adapter.appsList.remove(appModel) - linearLayoutManager = object : LinearLayoutManager(requireContext()) { - override fun scrollVerticallyBy( - dx: Int, - recycler: Recycler, - state: RecyclerView.State, - ): Int { + val newSet = mutableSetOf() + newSet.addAll(prefs.hiddenApps) + if (flag == Constants.FLAG_HIDDEN_APPS) { + newSet.remove(appModel.appPackage) // for backward compatibility + newSet.remove(appModel.appPackage + "|" + appModel.user.toString()) + } else newSet.add(appModel.appPackage + "|" + appModel.user.toString()) + + prefs.hiddenApps = newSet + if (newSet.isEmpty()) findNavController().popBackStack() + if (prefs.firstHide) { + binding.search.hideKeyboard() + prefs.firstHide = false + viewModel.showDialog.postValue(Constants.Dialog.HIDDEN) + findNavController() + .navigate(R.id.action_appListFragment_to_settingsFragment2) + } + viewModel.getAppList() + viewModel.getHiddenApps() + }, + appRenameListener = { appModel, renameLabel -> + prefs.setAppRenameLabel(appModel.appPackage, renameLabel) + viewModel.getAppList() + } + ) + + linearLayoutManager = + object : LinearLayoutManager(requireContext()) { + override fun scrollVerticallyBy( + dx: Int, + recycler: Recycler, + state: RecyclerView.State, + ): Int { val scrollRange = super.scrollVerticallyBy(dx, recycler, state) val overScroll = dx - scrollRange - if (overScroll < -10 && binding.recyclerView.scrollState == RecyclerView.SCROLL_STATE_DRAGGING) - checkMessageAndExit() + if (overScroll < -10 && + binding.recyclerView.scrollState == + RecyclerView.SCROLL_STATE_DRAGGING + ) + checkMessageAndExit() return scrollRange + } } - } - binding.recyclerView.layoutManager = linearLayoutManager - binding.recyclerView.adapter = adapter - binding.recyclerView.addOnScrollListener(getRecyclerViewOnScrollListener()) - binding.recyclerView.itemAnimator = null - if (requireContext().isEinkDisplay().not()) + binding.recyclerView.layoutManager = linearLayoutManager + binding.recyclerView.adapter = adapter + binding.recyclerView.addOnScrollListener(getRecyclerViewOnScrollListener()) + binding.recyclerView.itemAnimator = null + if (requireContext().isEinkDisplay().not()) binding.recyclerView.layoutAnimation = - AnimationUtils.loadLayoutAnimation(requireContext(), R.anim.layout_anim_from_bottom) - } + AnimationUtils.loadLayoutAnimation( + requireContext(), + R.anim.layout_anim_from_bottom + ) + } - private fun initObservers() { - viewModel.firstOpen.observe(viewLifecycleOwner) { - if (it && flag == Constants.FLAG_LAUNCH_APP) { - binding.appDrawerTip.visibility = View.VISIBLE - binding.appDrawerTip.isSelected = true - } - } - if (flag == Constants.FLAG_HIDDEN_APPS) { - viewModel.hiddenApps.observe(viewLifecycleOwner) { - it?.let { - adapter.setAppList(it.toMutableList()) - } - } - } else { - viewModel.appList.observe(viewLifecycleOwner) { - it?.let { appModels -> - adapter.setAppList(appModels.toMutableList()) - adapter.filter.filter(binding.search.query) - } - } - } + private fun initObservers() { + viewModel.firstOpen.observe(viewLifecycleOwner) { + if (it && flag == Constants.FLAG_LAUNCH_APP) { + binding.appDrawerTip.visibility = View.VISIBLE + binding.appDrawerTip.isSelected = true + } } - - private fun initClickListeners() { - binding.appDrawerTip.setOnClickListener { - binding.appDrawerTip.isSelected = false - binding.appDrawerTip.isSelected = true - } - binding.appRename.setOnClickListener { - val name = binding.search.query.toString().trim() - if (name.isEmpty()) { - requireContext().showToast(getString(R.string.type_a_new_app_name_first)) - binding.search.showKeyboard() - return@setOnClickListener - } - - when (flag) { - Constants.FLAG_SET_HOME_APP_1 -> prefs.appName1 = name - Constants.FLAG_SET_HOME_APP_2 -> prefs.appName2 = name - Constants.FLAG_SET_HOME_APP_3 -> prefs.appName3 = name - Constants.FLAG_SET_HOME_APP_4 -> prefs.appName4 = name - Constants.FLAG_SET_HOME_APP_5 -> prefs.appName5 = name - Constants.FLAG_SET_HOME_APP_6 -> prefs.appName6 = name - Constants.FLAG_SET_HOME_APP_7 -> prefs.appName7 = name - Constants.FLAG_SET_HOME_APP_8 -> prefs.appName8 = name - } - findNavController().popBackStack() + viewModel.appBlockedMessage.observe(viewLifecycleOwner) { + it?.let { message -> requireContext().showToast(message, android.widget.Toast.LENGTH_LONG) } + } + if (flag == Constants.FLAG_HIDDEN_APPS) { + viewModel.hiddenApps.observe(viewLifecycleOwner) { + it?.let { adapter.setAppList(it.toMutableList()) } + } + } else { + viewModel.appList.observe(viewLifecycleOwner) { + it?.let { appModels -> + adapter.setAppList(appModels.toMutableList()) + adapter.filter.filter(binding.search.query) } + } } + } - private fun getRecyclerViewOnScrollListener(): RecyclerView.OnScrollListener { - return object : RecyclerView.OnScrollListener() { + private fun initClickListeners() { + binding.appDrawerTip.setOnClickListener { + binding.appDrawerTip.isSelected = false + binding.appDrawerTip.isSelected = true + } + binding.appRename.setOnClickListener { + val name = binding.search.query.toString().trim() + if (name.isEmpty()) { + requireContext().showToast(getString(R.string.type_a_new_app_name_first)) + binding.search.showKeyboard() + return@setOnClickListener + } - var onTop = false + when (flag) { + Constants.FLAG_SET_HOME_APP_1 -> prefs.appName1 = name + Constants.FLAG_SET_HOME_APP_2 -> prefs.appName2 = name + Constants.FLAG_SET_HOME_APP_3 -> prefs.appName3 = name + Constants.FLAG_SET_HOME_APP_4 -> prefs.appName4 = name + Constants.FLAG_SET_HOME_APP_5 -> prefs.appName5 = name + Constants.FLAG_SET_HOME_APP_6 -> prefs.appName6 = name + Constants.FLAG_SET_HOME_APP_7 -> prefs.appName7 = name + Constants.FLAG_SET_HOME_APP_8 -> prefs.appName8 = name + } + findNavController().popBackStack() + } + } - override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { - super.onScrollStateChanged(recyclerView, newState) - when (newState) { + private fun getRecyclerViewOnScrollListener(): RecyclerView.OnScrollListener { + return object : RecyclerView.OnScrollListener() { - RecyclerView.SCROLL_STATE_DRAGGING -> { - onTop = !recyclerView.canScrollVertically(-1) - if (onTop) - binding.search.hideKeyboard() - } + var onTop = false - RecyclerView.SCROLL_STATE_IDLE -> { - if (!recyclerView.canScrollVertically(1)) - binding.search.hideKeyboard() - else if (!recyclerView.canScrollVertically(-1)) - if (!onTop && isRemoving.not()) - binding.search.showKeyboard(prefs.autoShowKeyboard) - } - } - } + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + super.onScrollStateChanged(recyclerView, newState) + when (newState) { + RecyclerView.SCROLL_STATE_DRAGGING -> { + onTop = !recyclerView.canScrollVertically(-1) + if (onTop) binding.search.hideKeyboard() + } + RecyclerView.SCROLL_STATE_IDLE -> { + if (!recyclerView.canScrollVertically(1)) binding.search.hideKeyboard() + else if (!recyclerView.canScrollVertically(-1)) + if (!onTop && isRemoving.not()) + binding.search.showKeyboard(prefs.autoShowKeyboard) + } } + } } + } - private fun checkMessageAndExit() { - findNavController().popBackStack() - if (flag == Constants.FLAG_LAUNCH_APP) - viewModel.checkForMessages.call() - } + private fun checkMessageAndExit() { + findNavController().popBackStack() + if (flag == Constants.FLAG_LAUNCH_APP) viewModel.checkForMessages.call() + } - override fun onStart() { - super.onStart() - binding.search.showKeyboard(prefs.autoShowKeyboard) - } + override fun onStart() { + super.onStart() + binding.search.showKeyboard(prefs.autoShowKeyboard) + } - override fun onStop() { - binding.search.hideKeyboard() - super.onStop() - } + override fun onStop() { + binding.search.hideKeyboard() + super.onStop() + } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/app/src/main/java/app/olauncher/ui/AppLimitConfigFragment.kt b/app/src/main/java/app/olauncher/ui/AppLimitConfigFragment.kt new file mode 100644 index 000000000..b34458e28 --- /dev/null +++ b/app/src/main/java/app/olauncher/ui/AppLimitConfigFragment.kt @@ -0,0 +1,187 @@ +package app.olauncher.ui + +import android.app.TimePickerDialog +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.view.isVisible +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController +import app.olauncher.R +import app.olauncher.data.AppLimitModel +import app.olauncher.data.Constants +import app.olauncher.data.Prefs +import app.olauncher.databinding.FragmentAppLimitConfigBinding +import app.olauncher.helper.showToast + +class AppLimitConfigFragment : Fragment() { + + private lateinit var prefs: Prefs + private var appPackage: String = "" + private var appName: String = "" + private var startHour: Int = 23 // 11 PM + private var startMinute: Int = 0 + private var endHour: Int = 7 // 7 AM + private var endMinute: Int = 0 + private var lockDuringLimit: Boolean = false + private var existingLimit: AppLimitModel? = null + + private var _binding: FragmentAppLimitConfigBinding? = null + private val binding + get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentAppLimitConfigBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + prefs = Prefs(requireContext()) + + arguments?.let { + appPackage = it.getString(Constants.Key.APP_PACKAGE, "") + appName = it.getString(Constants.Key.APP_NAME, "") + } + + loadExistingLimit() + initViews() + initClickListeners() + } + + private fun loadExistingLimit() { + existingLimit = prefs.getAppLimit(appPackage) + existingLimit?.let { limit -> + startHour = limit.startHour + startMinute = limit.startMinute + endHour = limit.endHour + endMinute = limit.endMinute + lockDuringLimit = limit.lockDuringLimit + } + } + + private fun initViews() { + binding.tvAppName.text = appName + updateTimeDisplays() + updateLockDisplay() + + // Show delete button only if editing existing limit + binding.btnDelete.isVisible = existingLimit != null + } + + private fun initClickListeners() { + binding.tvStartTime.setOnClickListener { + if (canEdit()) { + showTimePicker(startHour, startMinute) { hour, minute -> + startHour = hour + startMinute = minute + updateTimeDisplays() + } + } + } + + binding.tvEndTime.setOnClickListener { + if (canEdit()) { + showTimePicker(endHour, endMinute) { hour, minute -> + endHour = hour + endMinute = minute + updateTimeDisplays() + } + } + } + + binding.tvLockDuringLimit.setOnClickListener { + if (canEdit()) { + lockDuringLimit = !lockDuringLimit + updateLockDisplay() + } + } + + binding.btnSave.setOnClickListener { + if (canEdit()) { + saveLimit() + } + } + + binding.btnDelete.setOnClickListener { + if (canEdit()) { + deleteLimit() + } + } + } + + private fun canEdit(): Boolean { + // Check if limit is currently active and locked + existingLimit?.let { limit -> + if (limit.isCurrentlyBlocked() && limit.lockDuringLimit) { + requireContext().showToast(getString(R.string.limit_is_locked)) + return false + } + } + return true + } + + private fun showTimePicker(hour: Int, minute: Int, onTimeSet: (Int, Int) -> Unit) { + TimePickerDialog( + requireContext(), + { _, selectedHour, selectedMinute -> onTimeSet(selectedHour, selectedMinute) }, + hour, + minute, + false // 12-hour format + ) + .show() + } + + private fun updateTimeDisplays() { + binding.tvStartTime.text = formatTime12Hour(startHour, startMinute) + binding.tvEndTime.text = formatTime12Hour(endHour, endMinute) + } + + private fun formatTime12Hour(hour: Int, minute: Int): String { + val hour12 = if (hour == 0) 12 else if (hour > 12) hour - 12 else hour + val amPm = if (hour < 12) "AM" else "PM" + return String.format("%d:%02d %s", hour12, minute, amPm) + } + + private fun updateLockDisplay() { + binding.tvLockDuringLimit.text = + if (lockDuringLimit) { + getString(R.string.lock_during_limit_on) + } else { + getString(R.string.lock_during_limit_off) + } + } + + private fun saveLimit() { + val limit = + AppLimitModel( + appPackage = appPackage, + appName = appName, + startHour = startHour, + startMinute = startMinute, + endHour = endHour, + endMinute = endMinute, + lockDuringLimit = lockDuringLimit + ) + + prefs.addAppLimit(limit) + requireContext().showToast("App limit saved") + findNavController().popBackStack() + } + + private fun deleteLimit() { + prefs.removeAppLimit(appPackage) + requireContext().showToast("App limit removed") + findNavController().popBackStack() + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/app/src/main/java/app/olauncher/ui/AppLimitsAdapter.kt b/app/src/main/java/app/olauncher/ui/AppLimitsAdapter.kt new file mode 100644 index 000000000..42dd82d7e --- /dev/null +++ b/app/src/main/java/app/olauncher/ui/AppLimitsAdapter.kt @@ -0,0 +1,46 @@ +package app.olauncher.ui + +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import app.olauncher.data.AppLimitModel +import app.olauncher.databinding.AdapterAppLimitBinding + +class AppLimitsAdapter(private val appLimitClickListener: (AppLimitModel) -> Unit) : + RecyclerView.Adapter() { + + private var appLimits: MutableList = mutableListOf() + + inner class ViewHolder(private val binding: AdapterAppLimitBinding) : + RecyclerView.ViewHolder(binding.root) { + + fun bind(appLimit: AppLimitModel) { + binding.tvAppName.text = appLimit.appName + binding.tvTimeRange.text = "${appLimit.getStartTimeString()} - ${appLimit.getEndTimeString()}" + + if (appLimit.isCurrentlyBlocked()) { + binding.tvStatus.text = "Active" + } else { + binding.tvStatus.text = "Inactive" + } + + binding.root.setOnClickListener { appLimitClickListener(appLimit) } + } + } + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { + val binding = AdapterAppLimitBinding.inflate(LayoutInflater.from(parent.context), parent, false) + return ViewHolder(binding) + } + + override fun onBindViewHolder(holder: ViewHolder, position: Int) { + holder.bind(appLimits[position]) + } + + override fun getItemCount(): Int = appLimits.size + + fun setAppLimits(limits: List) { + appLimits = limits.toMutableList() + notifyDataSetChanged() + } +} diff --git a/app/src/main/java/app/olauncher/ui/AppLimitsFragment.kt b/app/src/main/java/app/olauncher/ui/AppLimitsFragment.kt new file mode 100644 index 000000000..584bb3325 --- /dev/null +++ b/app/src/main/java/app/olauncher/ui/AppLimitsFragment.kt @@ -0,0 +1,93 @@ +package app.olauncher.ui + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.os.bundleOf +import androidx.core.view.isVisible +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.findNavController +import androidx.recyclerview.widget.LinearLayoutManager +import app.olauncher.R +import app.olauncher.data.Constants +import app.olauncher.data.Prefs +import app.olauncher.databinding.FragmentAppLimitsBinding + +class AppLimitsFragment : Fragment() { + + private lateinit var prefs: Prefs + private lateinit var adapter: AppLimitsAdapter + + private var _binding: FragmentAppLimitsBinding? = null + private val binding + get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentAppLimitsBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + prefs = Prefs(requireContext()) + + initAdapter() + initClickListeners() + loadAppLimits() + } + + override fun onResume() { + super.onResume() + loadAppLimits() + } + + private fun initAdapter() { + adapter = AppLimitsAdapter { appLimit -> + // Navigate to edit screen + findNavController() + .navigate( + R.id.action_appLimitsFragment_to_appLimitConfigFragment, + bundleOf( + Constants.Key.APP_PACKAGE to appLimit.appPackage, + Constants.Key.APP_NAME to appLimit.appName + ) + ) + } + + binding.recyclerView.layoutManager = LinearLayoutManager(requireContext()) + binding.recyclerView.adapter = adapter + } + + private fun initClickListeners() { + binding.btnAddLimit.setOnClickListener { + // Navigate to app selection screen + findNavController() + .navigate( + R.id.action_appLimitsFragment_to_appListFragment, + bundleOf(Constants.Key.FLAG to Constants.FLAG_SET_APP_LIMIT) + ) + } + } + + private fun loadAppLimits() { + val limits = prefs.getAppLimits() + if (limits.isEmpty()) { + binding.tvNoLimits.isVisible = true + binding.recyclerView.isVisible = false + } else { + binding.tvNoLimits.isVisible = false + binding.recyclerView.isVisible = true + adapter.setAppLimits(limits) + } + } + + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/app/src/main/java/app/olauncher/ui/HomeFragment.kt b/app/src/main/java/app/olauncher/ui/HomeFragment.kt index c2406dbdc..a92ae7098 100644 --- a/app/src/main/java/app/olauncher/ui/HomeFragment.kt +++ b/app/src/main/java/app/olauncher/ui/HomeFragment.kt @@ -50,557 +50,567 @@ import java.util.Locale class HomeFragment : Fragment(), View.OnClickListener, View.OnLongClickListener { - private lateinit var prefs: Prefs - private lateinit var viewModel: MainViewModel - private lateinit var deviceManager: DevicePolicyManager - - private var _binding: FragmentHomeBinding? = null - private val binding get() = _binding!! - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - _binding = FragmentHomeBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - prefs = Prefs(requireContext()) - viewModel = activity?.run { - ViewModelProvider(this)[MainViewModel::class.java] - } ?: throw Exception("Invalid Activity") - - deviceManager = context?.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager - - initObservers() - setHomeAlignment(prefs.homeAlignment) - initSwipeTouchListener() - initClickListeners() - } - - override fun onResume() { - super.onResume() - populateHomeScreen(false) - viewModel.isOlauncherDefault() - if (prefs.showStatusBar) showStatusBar() - else hideStatusBar() - } - - override fun onClick(view: View) { - when (view.id) { - R.id.lock -> {} - R.id.clock -> openClockApp() - R.id.date -> openCalendarApp() - R.id.setDefaultLauncher -> viewModel.resetLauncherLiveData.call() - R.id.tvScreenTime -> openScreenTimeDigitalWellbeing() - - else -> { - try { // Launch app - val appLocation = view.tag.toString().toInt() - homeAppClicked(appLocation) - } catch (e: Exception) { - e.printStackTrace() - } - } + private lateinit var prefs: Prefs + private lateinit var viewModel: MainViewModel + private lateinit var deviceManager: DevicePolicyManager + + private var _binding: FragmentHomeBinding? = null + private val binding + get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentHomeBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + prefs = Prefs(requireContext()) + viewModel = + activity?.run { ViewModelProvider(this)[MainViewModel::class.java] } + ?: throw Exception("Invalid Activity") + + deviceManager = context?.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager + + initObservers() + setHomeAlignment(prefs.homeAlignment) + initSwipeTouchListener() + initClickListeners() + } + + override fun onResume() { + super.onResume() + populateHomeScreen(false) + viewModel.isOlauncherDefault() + if (prefs.showStatusBar) showStatusBar() else hideStatusBar() + } + + override fun onClick(view: View) { + when (view.id) { + R.id.lock -> {} + R.id.clock -> openClockApp() + R.id.date -> openCalendarApp() + R.id.setDefaultLauncher -> viewModel.resetLauncherLiveData.call() + R.id.tvScreenTime -> openScreenTimeDigitalWellbeing() + else -> { + try { // Launch app + val appLocation = view.tag.toString().toInt() + homeAppClicked(appLocation) + } catch (e: Exception) { + e.printStackTrace() } + } } + } - private fun openClockApp() { - if (prefs.clockAppPackage.isBlank()) - openAlarmApp(requireContext()) - else - launchApp( - "Clock", - prefs.clockAppPackage, - prefs.clockAppClassName, - prefs.clockAppUser - ) - } + private fun openClockApp() { + if (prefs.clockAppPackage.isBlank()) openAlarmApp(requireContext()) + else launchApp("Clock", prefs.clockAppPackage, prefs.clockAppClassName, prefs.clockAppUser) + } - private fun openCalendarApp() { - if (prefs.calendarAppPackage.isBlank()) - openCalendar(requireContext()) - else + private fun openCalendarApp() { + if (prefs.calendarAppPackage.isBlank()) openCalendar(requireContext()) + else launchApp( - "Calendar", - prefs.calendarAppPackage, - prefs.calendarAppClassName, - prefs.calendarAppUser + "Calendar", + prefs.calendarAppPackage, + prefs.calendarAppClassName, + prefs.calendarAppUser ) - } - - override fun onLongClick(view: View): Boolean { - when (view.id) { - R.id.homeApp1 -> showAppList(Constants.FLAG_SET_HOME_APP_1, prefs.appName1.isNotEmpty(), true) - R.id.homeApp2 -> showAppList(Constants.FLAG_SET_HOME_APP_2, prefs.appName2.isNotEmpty(), true) - R.id.homeApp3 -> showAppList(Constants.FLAG_SET_HOME_APP_3, prefs.appName3.isNotEmpty(), true) - R.id.homeApp4 -> showAppList(Constants.FLAG_SET_HOME_APP_4, prefs.appName4.isNotEmpty(), true) - R.id.homeApp5 -> showAppList(Constants.FLAG_SET_HOME_APP_5, prefs.appName5.isNotEmpty(), true) - R.id.homeApp6 -> showAppList(Constants.FLAG_SET_HOME_APP_6, prefs.appName6.isNotEmpty(), true) - R.id.homeApp7 -> showAppList(Constants.FLAG_SET_HOME_APP_7, prefs.appName7.isNotEmpty(), true) - R.id.homeApp8 -> showAppList(Constants.FLAG_SET_HOME_APP_8, prefs.appName8.isNotEmpty(), true) - R.id.clock -> { - showAppList(Constants.FLAG_SET_CLOCK_APP) - prefs.clockAppPackage = "" - prefs.clockAppClassName = "" - prefs.clockAppUser = "" - } - - R.id.date -> { - showAppList(Constants.FLAG_SET_CALENDAR_APP) - prefs.calendarAppPackage = "" - prefs.calendarAppClassName = "" - prefs.calendarAppUser = "" - } - - R.id.setDefaultLauncher -> { - prefs.hideSetDefaultLauncher = true - binding.setDefaultLauncher.visibility = View.GONE - if (viewModel.isOlauncherDefault.value != true) { - requireContext().showToast(R.string.set_as_default_launcher) - findNavController().navigate(R.id.action_mainFragment_to_settingsFragment) - } - } + } + + override fun onLongClick(view: View): Boolean { + when (view.id) { + R.id.homeApp1 -> showAppList(Constants.FLAG_SET_HOME_APP_1, prefs.appName1.isNotEmpty(), true) + R.id.homeApp2 -> showAppList(Constants.FLAG_SET_HOME_APP_2, prefs.appName2.isNotEmpty(), true) + R.id.homeApp3 -> showAppList(Constants.FLAG_SET_HOME_APP_3, prefs.appName3.isNotEmpty(), true) + R.id.homeApp4 -> showAppList(Constants.FLAG_SET_HOME_APP_4, prefs.appName4.isNotEmpty(), true) + R.id.homeApp5 -> showAppList(Constants.FLAG_SET_HOME_APP_5, prefs.appName5.isNotEmpty(), true) + R.id.homeApp6 -> showAppList(Constants.FLAG_SET_HOME_APP_6, prefs.appName6.isNotEmpty(), true) + R.id.homeApp7 -> showAppList(Constants.FLAG_SET_HOME_APP_7, prefs.appName7.isNotEmpty(), true) + R.id.homeApp8 -> showAppList(Constants.FLAG_SET_HOME_APP_8, prefs.appName8.isNotEmpty(), true) + R.id.clock -> { + showAppList(Constants.FLAG_SET_CLOCK_APP) + prefs.clockAppPackage = "" + prefs.clockAppClassName = "" + prefs.clockAppUser = "" + } + R.id.date -> { + showAppList(Constants.FLAG_SET_CALENDAR_APP) + prefs.calendarAppPackage = "" + prefs.calendarAppClassName = "" + prefs.calendarAppUser = "" + } + R.id.setDefaultLauncher -> { + prefs.hideSetDefaultLauncher = true + binding.setDefaultLauncher.visibility = View.GONE + if (viewModel.isOlauncherDefault.value != true) { + requireContext().showToast(R.string.set_as_default_launcher) + findNavController().navigate(R.id.action_mainFragment_to_settingsFragment) } - return true + } } - - private fun initObservers() { - if (prefs.firstSettingsOpen) { - binding.firstRunTips.visibility = View.VISIBLE - binding.setDefaultLauncher.visibility = View.GONE - } else binding.firstRunTips.visibility = View.GONE - - viewModel.refreshHome.observe(viewLifecycleOwner) { - populateHomeScreen(it) - } - viewModel.isOlauncherDefault.observe(viewLifecycleOwner, Observer { - if (it != true) { + return true + } + + private fun initObservers() { + if (prefs.firstSettingsOpen) { + binding.firstRunTips.visibility = View.VISIBLE + binding.setDefaultLauncher.visibility = View.GONE + } else binding.firstRunTips.visibility = View.GONE + + viewModel.refreshHome.observe(viewLifecycleOwner) { populateHomeScreen(it) } + viewModel.isOlauncherDefault.observe( + viewLifecycleOwner, + Observer { + if (it != true) { if (prefs.dailyWallpaper) { - prefs.dailyWallpaper = false - viewModel.cancelWallpaperWorker() + prefs.dailyWallpaper = false + viewModel.cancelWallpaperWorker() } prefs.homeBottomAlignment = false setHomeAlignment() + } + if (binding.firstRunTips.visibility == View.VISIBLE) return@Observer + binding.setDefaultLauncher.isVisible = it.not() && prefs.hideSetDefaultLauncher.not() + // if (it) binding.setDefaultLauncher.visibility = View.GONE + // else binding.setDefaultLauncher.visibility = View.VISIBLE } - if (binding.firstRunTips.visibility == View.VISIBLE) return@Observer - binding.setDefaultLauncher.isVisible = it.not() && prefs.hideSetDefaultLauncher.not() -// if (it) binding.setDefaultLauncher.visibility = View.GONE -// else binding.setDefaultLauncher.visibility = View.VISIBLE - }) - viewModel.homeAppAlignment.observe(viewLifecycleOwner) { - setHomeAlignment(it) - } - viewModel.toggleDateTime.observe(viewLifecycleOwner) { - populateDateTime() - } - viewModel.screenTimeValue.observe(viewLifecycleOwner) { - it?.let { binding.tvScreenTime.text = it } - } + ) + viewModel.homeAppAlignment.observe(viewLifecycleOwner) { setHomeAlignment(it) } + viewModel.toggleDateTime.observe(viewLifecycleOwner) { populateDateTime() } + viewModel.screenTimeValue.observe(viewLifecycleOwner) { + it?.let { binding.tvScreenTime.text = it } } - - private fun initSwipeTouchListener() { - val context = requireContext() - binding.mainLayout.setOnTouchListener(getSwipeGestureListener(context)) - binding.homeApp1.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp1)) - binding.homeApp2.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp2)) - binding.homeApp3.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp3)) - binding.homeApp4.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp4)) - binding.homeApp5.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp5)) - binding.homeApp6.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp6)) - binding.homeApp7.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp7)) - binding.homeApp8.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp8)) + viewModel.appBlockedMessage.observe(viewLifecycleOwner) { + it?.let { message -> requireContext().showToast(message, Toast.LENGTH_LONG) } } - - private fun initClickListeners() { - binding.lock.setOnClickListener(this) - binding.clock.setOnClickListener(this) - binding.date.setOnClickListener(this) - binding.clock.setOnLongClickListener(this) - binding.date.setOnLongClickListener(this) - binding.setDefaultLauncher.setOnClickListener(this) - binding.setDefaultLauncher.setOnLongClickListener(this) - binding.tvScreenTime.setOnClickListener(this) + } + + private fun initSwipeTouchListener() { + val context = requireContext() + binding.mainLayout.setOnTouchListener(getSwipeGestureListener(context)) + binding.homeApp1.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp1)) + binding.homeApp2.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp2)) + binding.homeApp3.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp3)) + binding.homeApp4.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp4)) + binding.homeApp5.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp5)) + binding.homeApp6.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp6)) + binding.homeApp7.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp7)) + binding.homeApp8.setOnTouchListener(getViewSwipeTouchListener(context, binding.homeApp8)) + } + + private fun initClickListeners() { + binding.lock.setOnClickListener(this) + binding.clock.setOnClickListener(this) + binding.date.setOnClickListener(this) + binding.clock.setOnLongClickListener(this) + binding.date.setOnLongClickListener(this) + binding.setDefaultLauncher.setOnClickListener(this) + binding.setDefaultLauncher.setOnLongClickListener(this) + binding.tvScreenTime.setOnClickListener(this) + } + + private fun setHomeAlignment(horizontalGravity: Int = prefs.homeAlignment) { + val verticalGravity = if (prefs.homeBottomAlignment) Gravity.BOTTOM else Gravity.CENTER_VERTICAL + binding.homeAppsLayout.gravity = horizontalGravity or verticalGravity + binding.dateTimeLayout.gravity = horizontalGravity + binding.homeApp1.gravity = horizontalGravity + binding.homeApp2.gravity = horizontalGravity + binding.homeApp3.gravity = horizontalGravity + binding.homeApp4.gravity = horizontalGravity + binding.homeApp5.gravity = horizontalGravity + binding.homeApp6.gravity = horizontalGravity + binding.homeApp7.gravity = horizontalGravity + binding.homeApp8.gravity = horizontalGravity + } + + private fun populateDateTime() { + binding.dateTimeLayout.isVisible = prefs.dateTimeVisibility != Constants.DateTime.OFF + binding.clock.isVisible = Constants.DateTime.isTimeVisible(prefs.dateTimeVisibility) + binding.date.isVisible = Constants.DateTime.isDateVisible(prefs.dateTimeVisibility) + + // var dateText = SimpleDateFormat("EEE, d MMM", Locale.getDefault()).format(Date()) + val dateFormat = SimpleDateFormat("EEE, d MMM", Locale.getDefault()) + var dateText = dateFormat.format(Date()) + + if (!prefs.showStatusBar) { + val battery = + (requireContext().getSystemService(Context.BATTERY_SERVICE) as BatteryManager) + .getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) + if (battery > 0) dateText = getString(R.string.day_battery, dateText, battery) } - - private fun setHomeAlignment(horizontalGravity: Int = prefs.homeAlignment) { - val verticalGravity = if (prefs.homeBottomAlignment) Gravity.BOTTOM else Gravity.CENTER_VERTICAL - binding.homeAppsLayout.gravity = horizontalGravity or verticalGravity - binding.dateTimeLayout.gravity = horizontalGravity - binding.homeApp1.gravity = horizontalGravity - binding.homeApp2.gravity = horizontalGravity - binding.homeApp3.gravity = horizontalGravity - binding.homeApp4.gravity = horizontalGravity - binding.homeApp5.gravity = horizontalGravity - binding.homeApp6.gravity = horizontalGravity - binding.homeApp7.gravity = horizontalGravity - binding.homeApp8.gravity = horizontalGravity + binding.date.text = dateText.replace(".,", ",") + } + + @RequiresApi(Build.VERSION_CODES.Q) + private fun populateScreenTime() { + if (requireContext().appUsagePermissionGranted().not()) return + + viewModel.getTodaysScreenTime() + binding.tvScreenTime.visibility = View.VISIBLE + + val isLandscape = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE + val horizontalMargin = if (isLandscape) 64.dpToPx() else 10.dpToPx() + val marginTop = + if (isLandscape) { + if (prefs.dateTimeVisibility == Constants.DateTime.DATE_ONLY) 36.dpToPx() + else 56.dpToPx() + } else { + if (prefs.dateTimeVisibility == Constants.DateTime.DATE_ONLY) 45.dpToPx() + else 72.dpToPx() + } + val params = + FrameLayout.LayoutParams( + FrameLayout.LayoutParams.WRAP_CONTENT, + FrameLayout.LayoutParams.WRAP_CONTENT + ) + .apply { + topMargin = marginTop + marginStart = horizontalMargin + marginEnd = horizontalMargin + gravity = + if (prefs.homeAlignment == Gravity.END) Gravity.START else Gravity.END + } + binding.tvScreenTime.layoutParams = params + binding.tvScreenTime.setPadding(10.dpToPx()) + } + + private fun populateHomeScreen(appCountUpdated: Boolean) { + if (appCountUpdated) hideHomeApps() + populateDateTime() + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) populateScreenTime() + + val homeAppsNum = prefs.homeAppsNum + if (homeAppsNum == 0) return + + binding.homeApp1.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp1, prefs.appName1, prefs.appPackage1, prefs.appUser1)) { + prefs.appName1 = "" + prefs.appPackage1 = "" } + if (homeAppsNum == 1) return - private fun populateDateTime() { - binding.dateTimeLayout.isVisible = prefs.dateTimeVisibility != Constants.DateTime.OFF - binding.clock.isVisible = Constants.DateTime.isTimeVisible(prefs.dateTimeVisibility) - binding.date.isVisible = Constants.DateTime.isDateVisible(prefs.dateTimeVisibility) - -// var dateText = SimpleDateFormat("EEE, d MMM", Locale.getDefault()).format(Date()) - val dateFormat = SimpleDateFormat("EEE, d MMM", Locale.getDefault()) - var dateText = dateFormat.format(Date()) - - if (!prefs.showStatusBar) { - val battery = (requireContext().getSystemService(Context.BATTERY_SERVICE) as BatteryManager) - .getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) - if (battery > 0) - dateText = getString(R.string.day_battery, dateText, battery) - } - binding.date.text = dateText.replace(".,", ",") + binding.homeApp2.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp2, prefs.appName2, prefs.appPackage2, prefs.appUser2)) { + prefs.appName2 = "" + prefs.appPackage2 = "" } + if (homeAppsNum == 2) return - @RequiresApi(Build.VERSION_CODES.Q) - private fun populateScreenTime() { - if (requireContext().appUsagePermissionGranted().not()) return - - viewModel.getTodaysScreenTime() - binding.tvScreenTime.visibility = View.VISIBLE - - val isLandscape = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE - val horizontalMargin = if (isLandscape) 64.dpToPx() else 10.dpToPx() - val marginTop = if (isLandscape) { - if (prefs.dateTimeVisibility == Constants.DateTime.DATE_ONLY) 36.dpToPx() else 56.dpToPx() - } else { - if (prefs.dateTimeVisibility == Constants.DateTime.DATE_ONLY) 45.dpToPx() else 72.dpToPx() - } - val params = FrameLayout.LayoutParams( - FrameLayout.LayoutParams.WRAP_CONTENT, - FrameLayout.LayoutParams.WRAP_CONTENT - ).apply { - topMargin = marginTop - marginStart = horizontalMargin - marginEnd = horizontalMargin - gravity = if (prefs.homeAlignment == Gravity.END) Gravity.START else Gravity.END - } - binding.tvScreenTime.layoutParams = params - binding.tvScreenTime.setPadding(10.dpToPx()) + binding.homeApp3.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp3, prefs.appName3, prefs.appPackage3, prefs.appUser3)) { + prefs.appName3 = "" + prefs.appPackage3 = "" } + if (homeAppsNum == 3) return - private fun populateHomeScreen(appCountUpdated: Boolean) { - if (appCountUpdated) hideHomeApps() - populateDateTime() - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) - populateScreenTime() - - val homeAppsNum = prefs.homeAppsNum - if (homeAppsNum == 0) return - - binding.homeApp1.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp1, prefs.appName1, prefs.appPackage1, prefs.appUser1)) { - prefs.appName1 = "" - prefs.appPackage1 = "" - } - if (homeAppsNum == 1) return - - binding.homeApp2.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp2, prefs.appName2, prefs.appPackage2, prefs.appUser2)) { - prefs.appName2 = "" - prefs.appPackage2 = "" - } - if (homeAppsNum == 2) return - - binding.homeApp3.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp3, prefs.appName3, prefs.appPackage3, prefs.appUser3)) { - prefs.appName3 = "" - prefs.appPackage3 = "" - } - if (homeAppsNum == 3) return - - binding.homeApp4.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp4, prefs.appName4, prefs.appPackage4, prefs.appUser4)) { - prefs.appName4 = "" - prefs.appPackage4 = "" - } - if (homeAppsNum == 4) return - - binding.homeApp5.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp5, prefs.appName5, prefs.appPackage5, prefs.appUser5)) { - prefs.appName5 = "" - prefs.appPackage5 = "" - } - if (homeAppsNum == 5) return - - binding.homeApp6.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp6, prefs.appName6, prefs.appPackage6, prefs.appUser6)) { - prefs.appName6 = "" - prefs.appPackage6 = "" - } - if (homeAppsNum == 6) return - - binding.homeApp7.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp7, prefs.appName7, prefs.appPackage7, prefs.appUser7)) { - prefs.appName7 = "" - prefs.appPackage7 = "" - } - if (homeAppsNum == 7) return - - binding.homeApp8.visibility = View.VISIBLE - if (!setHomeAppText(binding.homeApp8, prefs.appName8, prefs.appPackage8, prefs.appUser8)) { - prefs.appName8 = "" - prefs.appPackage8 = "" - } + binding.homeApp4.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp4, prefs.appName4, prefs.appPackage4, prefs.appUser4)) { + prefs.appName4 = "" + prefs.appPackage4 = "" } + if (homeAppsNum == 4) return - private fun setHomeAppText(textView: TextView, appName: String, packageName: String, userString: String): Boolean { - if (isPackageInstalled(requireContext(), packageName, userString)) { - textView.text = appName - return true - } - textView.text = "" - return false + binding.homeApp5.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp5, prefs.appName5, prefs.appPackage5, prefs.appUser5)) { + prefs.appName5 = "" + prefs.appPackage5 = "" } + if (homeAppsNum == 5) return - private fun hideHomeApps() { - binding.homeApp1.visibility = View.GONE - binding.homeApp2.visibility = View.GONE - binding.homeApp3.visibility = View.GONE - binding.homeApp4.visibility = View.GONE - binding.homeApp5.visibility = View.GONE - binding.homeApp6.visibility = View.GONE - binding.homeApp7.visibility = View.GONE - binding.homeApp8.visibility = View.GONE + binding.homeApp6.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp6, prefs.appName6, prefs.appPackage6, prefs.appUser6)) { + prefs.appName6 = "" + prefs.appPackage6 = "" } + if (homeAppsNum == 6) return - private fun homeAppClicked(location: Int) { - if (prefs.getAppName(location).isEmpty()) showLongPressToast() - else launchApp( - prefs.getAppName(location), - prefs.getAppPackage(location), - prefs.getAppActivityClassName(location), - prefs.getAppUser(location) - ) + binding.homeApp7.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp7, prefs.appName7, prefs.appPackage7, prefs.appUser7)) { + prefs.appName7 = "" + prefs.appPackage7 = "" } + if (homeAppsNum == 7) return - private fun launchApp(appName: String, packageName: String, activityClassName: String?, userString: String) { - viewModel.selectedApp( + binding.homeApp8.visibility = View.VISIBLE + if (!setHomeAppText(binding.homeApp8, prefs.appName8, prefs.appPackage8, prefs.appUser8)) { + prefs.appName8 = "" + prefs.appPackage8 = "" + } + } + + private fun setHomeAppText( + textView: TextView, + appName: String, + packageName: String, + userString: String + ): Boolean { + if (isPackageInstalled(requireContext(), packageName, userString)) { + textView.text = appName + return true + } + textView.text = "" + return false + } + + private fun hideHomeApps() { + binding.homeApp1.visibility = View.GONE + binding.homeApp2.visibility = View.GONE + binding.homeApp3.visibility = View.GONE + binding.homeApp4.visibility = View.GONE + binding.homeApp5.visibility = View.GONE + binding.homeApp6.visibility = View.GONE + binding.homeApp7.visibility = View.GONE + binding.homeApp8.visibility = View.GONE + } + + private fun homeAppClicked(location: Int) { + if (prefs.getAppName(location).isEmpty()) showLongPressToast() + else + launchApp( + prefs.getAppName(location), + prefs.getAppPackage(location), + prefs.getAppActivityClassName(location), + prefs.getAppUser(location) + ) + } + + private fun launchApp( + appName: String, + packageName: String, + activityClassName: String?, + userString: String + ) { + viewModel.selectedApp( AppModel( - appName, - null, - packageName, - activityClassName, - false, - getUserHandleFromString(requireContext(), userString) + appName, + null, + packageName, + activityClassName, + false, + getUserHandleFromString(requireContext(), userString) ), Constants.FLAG_LAUNCH_APP - ) - } - - private fun showAppList(flag: Int, rename: Boolean = false, includeHiddenApps: Boolean = false) { - viewModel.getAppList(includeHiddenApps) - try { - findNavController().navigate( - R.id.action_mainFragment_to_appListFragment, - bundleOf( - Constants.Key.FLAG to flag, - Constants.Key.RENAME to rename - ) - ) - } catch (e: Exception) { - findNavController().navigate( - R.id.appListFragment, - bundleOf( - Constants.Key.FLAG to flag, - Constants.Key.RENAME to rename - ) - ) - e.printStackTrace() - } + ) + } + + private fun showAppList(flag: Int, rename: Boolean = false, includeHiddenApps: Boolean = false) { + viewModel.getAppList(includeHiddenApps) + try { + findNavController() + .navigate( + R.id.action_mainFragment_to_appListFragment, + bundleOf(Constants.Key.FLAG to flag, Constants.Key.RENAME to rename) + ) + } catch (e: Exception) { + findNavController() + .navigate( + R.id.appListFragment, + bundleOf(Constants.Key.FLAG to flag, Constants.Key.RENAME to rename) + ) + e.printStackTrace() } + } - private fun swipeDownAction() { - when (prefs.swipeDownAction) { - Constants.SwipeDownAction.SEARCH -> openSearch(requireContext()) - else -> expandNotificationDrawer(requireContext()) - } + private fun swipeDownAction() { + when (prefs.swipeDownAction) { + Constants.SwipeDownAction.SEARCH -> openSearch(requireContext()) + else -> expandNotificationDrawer(requireContext()) } + } - private fun openSwipeRightApp() { - if (!prefs.swipeRightEnabled) return - if (prefs.appPackageSwipeRight.isNotEmpty()) + private fun openSwipeRightApp() { + if (!prefs.swipeRightEnabled) return + if (prefs.appPackageSwipeRight.isNotEmpty()) launchApp( - prefs.appNameSwipeRight, - prefs.appPackageSwipeRight, - prefs.appActivityClassNameRight, - prefs.appUserSwipeRight + prefs.appNameSwipeRight, + prefs.appPackageSwipeRight, + prefs.appActivityClassNameRight, + prefs.appUserSwipeRight ) - else openDialerApp(requireContext()) - } + else openDialerApp(requireContext()) + } - private fun openSwipeLeftApp() { - if (!prefs.swipeLeftEnabled) return - if (prefs.appPackageSwipeLeft.isNotEmpty()) + private fun openSwipeLeftApp() { + if (!prefs.swipeLeftEnabled) return + if (prefs.appPackageSwipeLeft.isNotEmpty()) launchApp( - prefs.appNameSwipeLeft, - prefs.appPackageSwipeLeft, - prefs.appActivityClassNameSwipeLeft, - prefs.appUserSwipeLeft + prefs.appNameSwipeLeft, + prefs.appPackageSwipeLeft, + prefs.appActivityClassNameSwipeLeft, + prefs.appUserSwipeLeft ) - else openCameraApp(requireContext()) - } - - private fun lockPhone() { - requireActivity().runOnUiThread { - try { - deviceManager.lockNow() - } catch (e: SecurityException) { - requireContext().showToast(getString(R.string.please_turn_on_double_tap_to_unlock), Toast.LENGTH_LONG) - findNavController().navigate(R.id.action_mainFragment_to_settingsFragment) - } catch (e: Exception) { - requireContext().showToast(getString(R.string.launcher_failed_to_lock_device), Toast.LENGTH_LONG) - prefs.lockModeOn = false - } - } + else openCameraApp(requireContext()) + } + + private fun lockPhone() { + requireActivity().runOnUiThread { + try { + deviceManager.lockNow() + } catch (e: SecurityException) { + requireContext() + .showToast( + getString(R.string.please_turn_on_double_tap_to_unlock), + Toast.LENGTH_LONG + ) + findNavController().navigate(R.id.action_mainFragment_to_settingsFragment) + } catch (e: Exception) { + requireContext() + .showToast(getString(R.string.launcher_failed_to_lock_device), Toast.LENGTH_LONG) + prefs.lockModeOn = false + } } + } - private fun showStatusBar() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) + private fun showStatusBar() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requireActivity().window.insetsController?.show(WindowInsets.Type.statusBars()) - else + else @Suppress("DEPRECATION", "InlinedApi") requireActivity().window.decorView.apply { - systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + systemUiVisibility = + View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN } - } + } - private fun hideStatusBar() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) + private fun hideStatusBar() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requireActivity().window.insetsController?.hide(WindowInsets.Type.statusBars()) - else { - @Suppress("DEPRECATION") - requireActivity().window.decorView.apply { - systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_FULLSCREEN - } - } + else { + @Suppress("DEPRECATION") + requireActivity().window.decorView.apply { + systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_FULLSCREEN + } } - - private fun changeAppTheme() { - if (prefs.dailyWallpaper.not()) return - val changedAppTheme = getChangedAppTheme(requireContext(), prefs.appTheme) - prefs.appTheme = changedAppTheme - if (prefs.dailyWallpaper) { - setPlainWallpaperByTheme(requireContext(), changedAppTheme) - viewModel.setWallpaperWorker() - } - requireActivity().recreate() + } + + private fun changeAppTheme() { + if (prefs.dailyWallpaper.not()) return + val changedAppTheme = getChangedAppTheme(requireContext(), prefs.appTheme) + prefs.appTheme = changedAppTheme + if (prefs.dailyWallpaper) { + setPlainWallpaperByTheme(requireContext(), changedAppTheme) + viewModel.setWallpaperWorker() } - - private fun openScreenTimeDigitalWellbeing() { - val intent = Intent() - try { - intent.setClassName( - Constants.DIGITAL_WELLBEING_PACKAGE_NAME, - Constants.DIGITAL_WELLBEING_ACTIVITY - ) - startActivity(intent) - } catch (e: Exception) { - e.printStackTrace() - try { - intent.setClassName( - Constants.DIGITAL_WELLBEING_SAMSUNG_PACKAGE_NAME, - Constants.DIGITAL_WELLBEING_SAMSUNG_ACTIVITY - ) - startActivity(intent) - } catch (e: Exception) { - e.printStackTrace() - } - } + requireActivity().recreate() + } + + private fun openScreenTimeDigitalWellbeing() { + val intent = Intent() + try { + intent.setClassName( + Constants.DIGITAL_WELLBEING_PACKAGE_NAME, + Constants.DIGITAL_WELLBEING_ACTIVITY + ) + startActivity(intent) + } catch (e: Exception) { + e.printStackTrace() + try { + intent.setClassName( + Constants.DIGITAL_WELLBEING_SAMSUNG_PACKAGE_NAME, + Constants.DIGITAL_WELLBEING_SAMSUNG_ACTIVITY + ) + startActivity(intent) + } catch (e: Exception) { + e.printStackTrace() + } } + } - private fun showLongPressToast() = requireContext().showToast(getString(R.string.long_press_to_select_app)) + private fun showLongPressToast() = + requireContext().showToast(getString(R.string.long_press_to_select_app)) - private fun textOnClick(view: View) = onClick(view) + private fun textOnClick(view: View) = onClick(view) - private fun textOnLongClick(view: View) = onLongClick(view) + private fun textOnLongClick(view: View) = onLongClick(view) - private fun getSwipeGestureListener(context: Context): View.OnTouchListener { - return object : OnSwipeTouchListener(context) { - override fun onSwipeLeft() { - super.onSwipeLeft() - openSwipeLeftApp() - } + private fun getSwipeGestureListener(context: Context): View.OnTouchListener { + return object : OnSwipeTouchListener(context) { + override fun onSwipeLeft() { + super.onSwipeLeft() + openSwipeLeftApp() + } - override fun onSwipeRight() { - super.onSwipeRight() - openSwipeRightApp() - } + override fun onSwipeRight() { + super.onSwipeRight() + openSwipeRightApp() + } - override fun onSwipeUp() { - super.onSwipeUp() - showAppList(Constants.FLAG_LAUNCH_APP) - } + override fun onSwipeUp() { + super.onSwipeUp() + showAppList(Constants.FLAG_LAUNCH_APP) + } - override fun onSwipeDown() { - super.onSwipeDown() - swipeDownAction() - } + override fun onSwipeDown() { + super.onSwipeDown() + swipeDownAction() + } - override fun onLongClick() { - super.onLongClick() - try { - findNavController().navigate(R.id.action_mainFragment_to_settingsFragment) - viewModel.firstOpen(false) - } catch (e: Exception) { - e.printStackTrace() - } - } - - override fun onDoubleClick() { - super.onDoubleClick() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) - binding.lock.performClick() - else if (prefs.lockModeOn) - lockPhone() - } - - override fun onClick() { - super.onClick() - viewModel.checkForMessages.call() - } + override fun onLongClick() { + super.onLongClick() + try { + findNavController().navigate(R.id.action_mainFragment_to_settingsFragment) + viewModel.firstOpen(false) + } catch (e: Exception) { + e.printStackTrace() } + } + + override fun onDoubleClick() { + super.onDoubleClick() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) binding.lock.performClick() + else if (prefs.lockModeOn) lockPhone() + } + + override fun onClick() { + super.onClick() + viewModel.checkForMessages.call() + } } - - private fun getViewSwipeTouchListener(context: Context, view: View): View.OnTouchListener { - return object : ViewSwipeTouchListener(context, view) { - override fun onSwipeLeft() { - super.onSwipeLeft() - openSwipeLeftApp() - } - - override fun onSwipeRight() { - super.onSwipeRight() - openSwipeRightApp() - } - - override fun onSwipeUp() { - super.onSwipeUp() - showAppList(Constants.FLAG_LAUNCH_APP) - } - - override fun onSwipeDown() { - super.onSwipeDown() - swipeDownAction() - } - - override fun onLongClick(view: View) { - super.onLongClick(view) - textOnLongClick(view) - } - - override fun onClick(view: View) { - super.onClick(view) - textOnClick(view) - } - } + } + + private fun getViewSwipeTouchListener(context: Context, view: View): View.OnTouchListener { + return object : ViewSwipeTouchListener(context, view) { + override fun onSwipeLeft() { + super.onSwipeLeft() + openSwipeLeftApp() + } + + override fun onSwipeRight() { + super.onSwipeRight() + openSwipeRightApp() + } + + override fun onSwipeUp() { + super.onSwipeUp() + showAppList(Constants.FLAG_LAUNCH_APP) + } + + override fun onSwipeDown() { + super.onSwipeDown() + swipeDownAction() + } + + override fun onLongClick(view: View) { + super.onLongClick(view) + textOnLongClick(view) + } + + override fun onClick(view: View) { + super.onClick(view) + textOnClick(view) + } } + } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } -} \ No newline at end of file + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } +} diff --git a/app/src/main/java/app/olauncher/ui/SettingsFragment.kt b/app/src/main/java/app/olauncher/ui/SettingsFragment.kt index b64a7ca3a..ff520fb76 100644 --- a/app/src/main/java/app/olauncher/ui/SettingsFragment.kt +++ b/app/src/main/java/app/olauncher/ui/SettingsFragment.kt @@ -42,603 +42,630 @@ import app.olauncher.listener.DeviceAdmin class SettingsFragment : Fragment(), View.OnClickListener, View.OnLongClickListener { - private lateinit var prefs: Prefs - private lateinit var viewModel: MainViewModel - private lateinit var deviceManager: DevicePolicyManager - private lateinit var componentName: ComponentName - - private var _binding: FragmentSettingsBinding? = null - private val binding get() = _binding!! - - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { - _binding = FragmentSettingsBinding.inflate(inflater, container, false) - return binding.root - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - prefs = Prefs(requireContext()) - viewModel = activity?.run { - ViewModelProvider(this)[MainViewModel::class.java] - } ?: throw Exception("Invalid Activity") - viewModel.isOlauncherDefault() - - deviceManager = requireContext().getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager - componentName = ComponentName(requireContext(), DeviceAdmin::class.java) - checkAdminPermission() - - binding.homeAppsNum.text = prefs.homeAppsNum.toString() - populateProMessage() - populateKeyboardText() - populateScreenTimeOnOff() - populateLockSettings() - populateWallpaperText() - populateAppThemeText() - populateTextSize() - populateAlignment() - populateStatusBar() - populateDateTime() - populateSwipeApps() - populateSwipeDownAction() - populateActionHints() - initClickListeners() - initObservers() - } - - override fun onClick(view: View) { - binding.appsNumSelectLayout.visibility = View.GONE - binding.dateTimeSelectLayout.visibility = View.GONE - binding.appThemeSelectLayout.visibility = View.GONE - binding.swipeDownSelectLayout.visibility = View.GONE - binding.textSizesLayout.visibility = View.GONE - if (view.id != R.id.alignmentBottom) - binding.alignmentSelectLayout.visibility = View.GONE - - when (view.id) { - R.id.olauncherHiddenApps -> showHiddenApps() - R.id.olauncherPro -> requireContext().openUrl(Constants.URL_OLAUNCHER_PRO) - R.id.screenTimeOnOff -> viewModel.showDialog.postValue(Constants.Dialog.DIGITAL_WELLBEING) - R.id.appInfo -> openAppInfo(requireContext(), Process.myUserHandle(), BuildConfig.APPLICATION_ID) - R.id.setLauncher -> viewModel.resetLauncherLiveData.call() - R.id.toggleLock -> toggleLockMode() - R.id.autoShowKeyboard -> toggleKeyboardText() - R.id.homeAppsNum -> binding.appsNumSelectLayout.visibility = View.VISIBLE - R.id.dailyWallpaperUrl -> requireContext().openUrl(prefs.dailyWallpaperUrl) - R.id.dailyWallpaper -> toggleDailyWallpaperUpdate() - R.id.alignment -> binding.alignmentSelectLayout.visibility = View.VISIBLE - R.id.alignmentLeft -> viewModel.updateHomeAlignment(Gravity.START) - R.id.alignmentCenter -> viewModel.updateHomeAlignment(Gravity.CENTER) - R.id.alignmentRight -> viewModel.updateHomeAlignment(Gravity.END) - R.id.alignmentBottom -> updateHomeBottomAlignment() - R.id.statusBar -> toggleStatusBar() - R.id.dateTime -> binding.dateTimeSelectLayout.visibility = View.VISIBLE - R.id.dateTimeOn -> toggleDateTime(Constants.DateTime.ON) - R.id.dateTimeOff -> toggleDateTime(Constants.DateTime.OFF) - R.id.dateOnly -> toggleDateTime(Constants.DateTime.DATE_ONLY) - R.id.appThemeText -> binding.appThemeSelectLayout.visibility = View.VISIBLE - R.id.themeLight -> updateTheme(AppCompatDelegate.MODE_NIGHT_NO) - R.id.themeDark -> updateTheme(AppCompatDelegate.MODE_NIGHT_YES) - R.id.themeSystem -> updateTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) - R.id.textSizeValue -> binding.textSizesLayout.visibility = View.VISIBLE - R.id.actionAccessibility -> openAccessibilityService() - R.id.closeAccessibility -> toggleAccessibilityVisibility(false) - R.id.notWorking -> requireContext().openUrl(Constants.URL_DOUBLE_TAP) - - R.id.tvGestures -> binding.flSwipeDown.visibility = View.VISIBLE - - R.id.maxApps0 -> updateHomeAppsNum(0) - R.id.maxApps1 -> updateHomeAppsNum(1) - R.id.maxApps2 -> updateHomeAppsNum(2) - R.id.maxApps3 -> updateHomeAppsNum(3) - R.id.maxApps4 -> updateHomeAppsNum(4) - R.id.maxApps5 -> updateHomeAppsNum(5) - R.id.maxApps6 -> updateHomeAppsNum(6) - R.id.maxApps7 -> updateHomeAppsNum(7) - R.id.maxApps8 -> updateHomeAppsNum(8) - - R.id.textSize1 -> updateTextSizeScale(Constants.TextSize.ONE) - R.id.textSize2 -> updateTextSizeScale(Constants.TextSize.TWO) - R.id.textSize3 -> updateTextSizeScale(Constants.TextSize.THREE) - R.id.textSize4 -> updateTextSizeScale(Constants.TextSize.FOUR) - R.id.textSize5 -> updateTextSizeScale(Constants.TextSize.FIVE) - R.id.textSize6 -> updateTextSizeScale(Constants.TextSize.SIX) - R.id.textSize7 -> updateTextSizeScale(Constants.TextSize.SEVEN) - - R.id.swipeLeftApp -> showAppListIfEnabled(Constants.FLAG_SET_SWIPE_LEFT_APP) - R.id.swipeRightApp -> showAppListIfEnabled(Constants.FLAG_SET_SWIPE_RIGHT_APP) - R.id.swipeDownAction -> binding.swipeDownSelectLayout.visibility = View.VISIBLE - R.id.notifications -> updateSwipeDownAction(Constants.SwipeDownAction.NOTIFICATIONS) - R.id.search -> updateSwipeDownAction(Constants.SwipeDownAction.SEARCH) - - R.id.aboutOlauncher -> { - prefs.aboutClicked = true - requireContext().openUrl(Constants.URL_ABOUT_OLAUNCHER) - } - - R.id.share -> requireActivity().shareApp() - R.id.rate -> { - prefs.rateClicked = true - requireActivity().rateApp() - } - - R.id.twitter -> requireContext().openUrl(Constants.URL_TWITTER_TANUJ) - R.id.github -> requireContext().openUrl(Constants.URL_OLAUNCHER_GITHUB) - R.id.privacy -> requireContext().openUrl(Constants.URL_OLAUNCHER_PRIVACY) - R.id.footer -> requireContext().openUrl(Constants.URL_PLAY_STORE_DEV) - } - } - - override fun onLongClick(view: View): Boolean { - when (view.id) { - R.id.alignment -> { - prefs.appLabelAlignment = prefs.homeAlignment - findNavController().navigate(R.id.action_settingsFragment_to_appListFragment) - requireContext().showToast(getString(R.string.alignment_changed)) - } - - R.id.dailyWallpaper -> removeWallpaper() - R.id.appThemeText -> { - binding.appThemeSelectLayout.visibility = View.VISIBLE - binding.themeSystem.visibility = View.VISIBLE - } - - R.id.swipeLeftApp -> toggleSwipeLeft() - R.id.swipeRightApp -> toggleSwipeRight() - R.id.toggleLock -> startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) - } - return true - } - - private fun initClickListeners() { - binding.olauncherHiddenApps.setOnClickListener(this) - binding.scrollLayout.setOnClickListener(this) - binding.appInfo.setOnClickListener(this) - binding.setLauncher.setOnClickListener(this) - binding.aboutOlauncher.setOnClickListener(this) - binding.olauncherPro.setOnClickListener(this) - binding.autoShowKeyboard.setOnClickListener(this) - binding.toggleLock.setOnClickListener(this) - binding.homeAppsNum.setOnClickListener(this) - binding.screenTimeOnOff.setOnClickListener(this) - binding.dailyWallpaperUrl.setOnClickListener(this) - binding.dailyWallpaper.setOnClickListener(this) - binding.alignment.setOnClickListener(this) - binding.alignmentLeft.setOnClickListener(this) - binding.alignmentCenter.setOnClickListener(this) - binding.alignmentRight.setOnClickListener(this) - binding.alignmentBottom.setOnClickListener(this) - binding.statusBar.setOnClickListener(this) - binding.dateTime.setOnClickListener(this) - binding.dateTimeOn.setOnClickListener(this) - binding.dateTimeOff.setOnClickListener(this) - binding.dateOnly.setOnClickListener(this) - binding.swipeLeftApp.setOnClickListener(this) - binding.swipeRightApp.setOnClickListener(this) - binding.swipeDownAction.setOnClickListener(this) - binding.search.setOnClickListener(this) - binding.notifications.setOnClickListener(this) - binding.appThemeText.setOnClickListener(this) - binding.themeLight.setOnClickListener(this) - binding.themeDark.setOnClickListener(this) - binding.themeSystem.setOnClickListener(this) - binding.textSizeValue.setOnClickListener(this) - binding.actionAccessibility.setOnClickListener(this) - binding.closeAccessibility.setOnClickListener(this) - binding.notWorking.setOnClickListener(this) - - binding.share.setOnClickListener(this) - binding.rate.setOnClickListener(this) - binding.twitter.setOnClickListener(this) - binding.github.setOnClickListener(this) - binding.privacy.setOnClickListener(this) - binding.footer.setOnClickListener(this) - - binding.maxApps0.setOnClickListener(this) - binding.maxApps1.setOnClickListener(this) - binding.maxApps2.setOnClickListener(this) - binding.maxApps3.setOnClickListener(this) - binding.maxApps4.setOnClickListener(this) - binding.maxApps5.setOnClickListener(this) - binding.maxApps6.setOnClickListener(this) - binding.maxApps7.setOnClickListener(this) - binding.maxApps8.setOnClickListener(this) - - binding.textSize1.setOnClickListener(this) - binding.textSize2.setOnClickListener(this) - binding.textSize3.setOnClickListener(this) - binding.textSize4.setOnClickListener(this) - binding.textSize5.setOnClickListener(this) - binding.textSize6.setOnClickListener(this) - binding.textSize7.setOnClickListener(this) - - binding.dailyWallpaper.setOnLongClickListener(this) - binding.alignment.setOnLongClickListener(this) - binding.appThemeText.setOnLongClickListener(this) - binding.swipeLeftApp.setOnLongClickListener(this) - binding.swipeRightApp.setOnLongClickListener(this) - binding.toggleLock.setOnLongClickListener(this) - } - - private fun initObservers() { - if (prefs.firstSettingsOpen) { - viewModel.showDialog.postValue(Constants.Dialog.ABOUT) - prefs.firstSettingsOpen = false - } - viewModel.isOlauncherDefault.observe(viewLifecycleOwner) { - if (it) { - binding.setLauncher.text = getString(R.string.change_default_launcher) - prefs.toShowHintCounter += 1 - } - } - viewModel.homeAppAlignment.observe(viewLifecycleOwner) { - populateAlignment() - } - viewModel.updateSwipeApps.observe(viewLifecycleOwner) { - populateSwipeApps() - } - } - - private fun toggleSwipeLeft() { - prefs.swipeLeftEnabled = !prefs.swipeLeftEnabled - if (prefs.swipeLeftEnabled) { - binding.swipeLeftApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColor)) - requireContext().showToast(getString(R.string.swipe_left_app_enabled)) - } else { - binding.swipeLeftApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColorTrans50)) - requireContext().showToast(getString(R.string.swipe_left_app_disabled)) - } - } - - private fun toggleSwipeRight() { - prefs.swipeRightEnabled = !prefs.swipeRightEnabled - if (prefs.swipeRightEnabled) { - binding.swipeRightApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColor)) - requireContext().showToast(getString(R.string.swipe_right_app_enabled)) - } else { - binding.swipeRightApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColorTrans50)) - requireContext().showToast(getString(R.string.swipe_right_app_disabled)) - } - } - - private fun toggleStatusBar() { - prefs.showStatusBar = !prefs.showStatusBar - populateStatusBar() - } - - private fun populateStatusBar() { - if (prefs.showStatusBar) { - showStatusBar() - binding.statusBar.text = getString(R.string.on) - } else { - hideStatusBar() - binding.statusBar.text = getString(R.string.off) - } - } - - private fun toggleDateTime(selected: Int) { - prefs.dateTimeVisibility = selected - populateDateTime() - viewModel.toggleDateTime() - } - - private fun populateDateTime() { - binding.dateTime.text = getString( - when (prefs.dateTimeVisibility) { - Constants.DateTime.DATE_ONLY -> R.string.date - Constants.DateTime.ON -> R.string.on - else -> R.string.off - } - ) - } + private lateinit var prefs: Prefs + private lateinit var viewModel: MainViewModel + private lateinit var deviceManager: DevicePolicyManager + private lateinit var componentName: ComponentName + + private var _binding: FragmentSettingsBinding? = null + private val binding + get() = _binding!! + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentSettingsBinding.inflate(inflater, container, false) + return binding.root + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + prefs = Prefs(requireContext()) + viewModel = + activity?.run { ViewModelProvider(this)[MainViewModel::class.java] } + ?: throw Exception("Invalid Activity") + viewModel.isOlauncherDefault() + + deviceManager = + requireContext().getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager + componentName = ComponentName(requireContext(), DeviceAdmin::class.java) + checkAdminPermission() + + binding.homeAppsNum.text = prefs.homeAppsNum.toString() + populateProMessage() + populateKeyboardText() + populateScreenTimeOnOff() + populateLockSettings() + populateWallpaperText() + populateAppThemeText() + populateTextSize() + populateAlignment() + populateStatusBar() + populateDateTime() + populateSwipeApps() + populateSwipeDownAction() + populateActionHints() + initClickListeners() + initObservers() + } + + override fun onClick(view: View) { + binding.appsNumSelectLayout.visibility = View.GONE + binding.dateTimeSelectLayout.visibility = View.GONE + binding.appThemeSelectLayout.visibility = View.GONE + binding.swipeDownSelectLayout.visibility = View.GONE + binding.textSizesLayout.visibility = View.GONE + if (view.id != R.id.alignmentBottom) binding.alignmentSelectLayout.visibility = View.GONE + + when (view.id) { + R.id.olauncherHiddenApps -> showHiddenApps() + R.id.olauncherPro -> requireContext().openUrl(Constants.URL_OLAUNCHER_PRO) + R.id.screenTimeOnOff -> viewModel.showDialog.postValue(Constants.Dialog.DIGITAL_WELLBEING) + R.id.appLimits -> + findNavController().navigate(R.id.action_settingsFragment_to_appLimitsFragment) + R.id.appInfo -> + openAppInfo(requireContext(), Process.myUserHandle(), BuildConfig.APPLICATION_ID) + R.id.setLauncher -> viewModel.resetLauncherLiveData.call() + R.id.toggleLock -> toggleLockMode() + R.id.autoShowKeyboard -> toggleKeyboardText() + R.id.homeAppsNum -> binding.appsNumSelectLayout.visibility = View.VISIBLE + R.id.dailyWallpaperUrl -> requireContext().openUrl(prefs.dailyWallpaperUrl) + R.id.dailyWallpaper -> toggleDailyWallpaperUpdate() + R.id.alignment -> binding.alignmentSelectLayout.visibility = View.VISIBLE + R.id.alignmentLeft -> viewModel.updateHomeAlignment(Gravity.START) + R.id.alignmentCenter -> viewModel.updateHomeAlignment(Gravity.CENTER) + R.id.alignmentRight -> viewModel.updateHomeAlignment(Gravity.END) + R.id.alignmentBottom -> updateHomeBottomAlignment() + R.id.statusBar -> toggleStatusBar() + R.id.dateTime -> binding.dateTimeSelectLayout.visibility = View.VISIBLE + R.id.dateTimeOn -> toggleDateTime(Constants.DateTime.ON) + R.id.dateTimeOff -> toggleDateTime(Constants.DateTime.OFF) + R.id.dateOnly -> toggleDateTime(Constants.DateTime.DATE_ONLY) + R.id.appThemeText -> binding.appThemeSelectLayout.visibility = View.VISIBLE + R.id.themeLight -> updateTheme(AppCompatDelegate.MODE_NIGHT_NO) + R.id.themeDark -> updateTheme(AppCompatDelegate.MODE_NIGHT_YES) + R.id.themeSystem -> updateTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) + R.id.textSizeValue -> binding.textSizesLayout.visibility = View.VISIBLE + R.id.actionAccessibility -> openAccessibilityService() + R.id.closeAccessibility -> toggleAccessibilityVisibility(false) + R.id.notWorking -> requireContext().openUrl(Constants.URL_DOUBLE_TAP) + R.id.tvGestures -> binding.flSwipeDown.visibility = View.VISIBLE + R.id.maxApps0 -> updateHomeAppsNum(0) + R.id.maxApps1 -> updateHomeAppsNum(1) + R.id.maxApps2 -> updateHomeAppsNum(2) + R.id.maxApps3 -> updateHomeAppsNum(3) + R.id.maxApps4 -> updateHomeAppsNum(4) + R.id.maxApps5 -> updateHomeAppsNum(5) + R.id.maxApps6 -> updateHomeAppsNum(6) + R.id.maxApps7 -> updateHomeAppsNum(7) + R.id.maxApps8 -> updateHomeAppsNum(8) + R.id.textSize1 -> updateTextSizeScale(Constants.TextSize.ONE) + R.id.textSize2 -> updateTextSizeScale(Constants.TextSize.TWO) + R.id.textSize3 -> updateTextSizeScale(Constants.TextSize.THREE) + R.id.textSize4 -> updateTextSizeScale(Constants.TextSize.FOUR) + R.id.textSize5 -> updateTextSizeScale(Constants.TextSize.FIVE) + R.id.textSize6 -> updateTextSizeScale(Constants.TextSize.SIX) + R.id.textSize7 -> updateTextSizeScale(Constants.TextSize.SEVEN) + R.id.swipeLeftApp -> showAppListIfEnabled(Constants.FLAG_SET_SWIPE_LEFT_APP) + R.id.swipeRightApp -> showAppListIfEnabled(Constants.FLAG_SET_SWIPE_RIGHT_APP) + R.id.swipeDownAction -> binding.swipeDownSelectLayout.visibility = View.VISIBLE + R.id.notifications -> updateSwipeDownAction(Constants.SwipeDownAction.NOTIFICATIONS) + R.id.search -> updateSwipeDownAction(Constants.SwipeDownAction.SEARCH) + R.id.aboutOlauncher -> { + prefs.aboutClicked = true + requireContext().openUrl(Constants.URL_ABOUT_OLAUNCHER) + } + R.id.share -> requireActivity().shareApp() + R.id.rate -> { + prefs.rateClicked = true + requireActivity().rateApp() + } + R.id.twitter -> requireContext().openUrl(Constants.URL_TWITTER_TANUJ) + R.id.github -> requireContext().openUrl(Constants.URL_OLAUNCHER_GITHUB) + R.id.privacy -> requireContext().openUrl(Constants.URL_OLAUNCHER_PRIVACY) + R.id.footer -> requireContext().openUrl(Constants.URL_PLAY_STORE_DEV) + } + } + + override fun onLongClick(view: View): Boolean { + when (view.id) { + R.id.alignment -> { + prefs.appLabelAlignment = prefs.homeAlignment + findNavController().navigate(R.id.action_settingsFragment_to_appListFragment) + requireContext().showToast(getString(R.string.alignment_changed)) + } + R.id.dailyWallpaper -> removeWallpaper() + R.id.appThemeText -> { + binding.appThemeSelectLayout.visibility = View.VISIBLE + binding.themeSystem.visibility = View.VISIBLE + } + R.id.swipeLeftApp -> toggleSwipeLeft() + R.id.swipeRightApp -> toggleSwipeRight() + R.id.toggleLock -> startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) + } + return true + } + + private fun initClickListeners() { + binding.olauncherHiddenApps.setOnClickListener(this) + binding.scrollLayout.setOnClickListener(this) + binding.appInfo.setOnClickListener(this) + binding.setLauncher.setOnClickListener(this) + binding.aboutOlauncher.setOnClickListener(this) + binding.olauncherPro.setOnClickListener(this) + binding.autoShowKeyboard.setOnClickListener(this) + binding.toggleLock.setOnClickListener(this) + binding.homeAppsNum.setOnClickListener(this) + binding.screenTimeOnOff.setOnClickListener(this) + binding.appLimits.setOnClickListener(this) + binding.dailyWallpaperUrl.setOnClickListener(this) + binding.dailyWallpaper.setOnClickListener(this) + binding.alignment.setOnClickListener(this) + binding.alignmentLeft.setOnClickListener(this) + binding.alignmentCenter.setOnClickListener(this) + binding.alignmentRight.setOnClickListener(this) + binding.alignmentBottom.setOnClickListener(this) + binding.statusBar.setOnClickListener(this) + binding.dateTime.setOnClickListener(this) + binding.dateTimeOn.setOnClickListener(this) + binding.dateTimeOff.setOnClickListener(this) + binding.dateOnly.setOnClickListener(this) + binding.swipeLeftApp.setOnClickListener(this) + binding.swipeRightApp.setOnClickListener(this) + binding.swipeDownAction.setOnClickListener(this) + binding.search.setOnClickListener(this) + binding.notifications.setOnClickListener(this) + binding.appThemeText.setOnClickListener(this) + binding.themeLight.setOnClickListener(this) + binding.themeDark.setOnClickListener(this) + binding.themeSystem.setOnClickListener(this) + binding.textSizeValue.setOnClickListener(this) + binding.actionAccessibility.setOnClickListener(this) + binding.closeAccessibility.setOnClickListener(this) + binding.notWorking.setOnClickListener(this) + + binding.share.setOnClickListener(this) + binding.rate.setOnClickListener(this) + binding.twitter.setOnClickListener(this) + binding.github.setOnClickListener(this) + binding.privacy.setOnClickListener(this) + binding.footer.setOnClickListener(this) + + binding.maxApps0.setOnClickListener(this) + binding.maxApps1.setOnClickListener(this) + binding.maxApps2.setOnClickListener(this) + binding.maxApps3.setOnClickListener(this) + binding.maxApps4.setOnClickListener(this) + binding.maxApps5.setOnClickListener(this) + binding.maxApps6.setOnClickListener(this) + binding.maxApps7.setOnClickListener(this) + binding.maxApps8.setOnClickListener(this) + + binding.textSize1.setOnClickListener(this) + binding.textSize2.setOnClickListener(this) + binding.textSize3.setOnClickListener(this) + binding.textSize4.setOnClickListener(this) + binding.textSize5.setOnClickListener(this) + binding.textSize6.setOnClickListener(this) + binding.textSize7.setOnClickListener(this) + + binding.dailyWallpaper.setOnLongClickListener(this) + binding.alignment.setOnLongClickListener(this) + binding.appThemeText.setOnLongClickListener(this) + binding.swipeLeftApp.setOnLongClickListener(this) + binding.swipeRightApp.setOnLongClickListener(this) + binding.toggleLock.setOnLongClickListener(this) + } + + private fun initObservers() { + if (prefs.firstSettingsOpen) { + viewModel.showDialog.postValue(Constants.Dialog.ABOUT) + prefs.firstSettingsOpen = false + } + viewModel.isOlauncherDefault.observe(viewLifecycleOwner) { + if (it) { + binding.setLauncher.text = getString(R.string.change_default_launcher) + prefs.toShowHintCounter += 1 + } + } + viewModel.homeAppAlignment.observe(viewLifecycleOwner) { populateAlignment() } + viewModel.updateSwipeApps.observe(viewLifecycleOwner) { populateSwipeApps() } + } + + private fun toggleSwipeLeft() { + prefs.swipeLeftEnabled = !prefs.swipeLeftEnabled + if (prefs.swipeLeftEnabled) { + binding.swipeLeftApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColor)) + requireContext().showToast(getString(R.string.swipe_left_app_enabled)) + } else { + binding.swipeLeftApp.setTextColor( + requireContext().getColorFromAttr(R.attr.primaryColorTrans50) + ) + requireContext().showToast(getString(R.string.swipe_left_app_disabled)) + } + } + + private fun toggleSwipeRight() { + prefs.swipeRightEnabled = !prefs.swipeRightEnabled + if (prefs.swipeRightEnabled) { + binding.swipeRightApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColor)) + requireContext().showToast(getString(R.string.swipe_right_app_enabled)) + } else { + binding.swipeRightApp.setTextColor( + requireContext().getColorFromAttr(R.attr.primaryColorTrans50) + ) + requireContext().showToast(getString(R.string.swipe_right_app_disabled)) + } + } + + private fun toggleStatusBar() { + prefs.showStatusBar = !prefs.showStatusBar + populateStatusBar() + } + + private fun populateStatusBar() { + if (prefs.showStatusBar) { + showStatusBar() + binding.statusBar.text = getString(R.string.on) + } else { + hideStatusBar() + binding.statusBar.text = getString(R.string.off) + } + } + + private fun toggleDateTime(selected: Int) { + prefs.dateTimeVisibility = selected + populateDateTime() + viewModel.toggleDateTime() + } + + private fun populateDateTime() { + binding.dateTime.text = + getString( + when (prefs.dateTimeVisibility) { + Constants.DateTime.DATE_ONLY -> R.string.date + Constants.DateTime.ON -> R.string.on + else -> R.string.off + } + ) + } - private fun showStatusBar() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) + private fun showStatusBar() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requireActivity().window.insetsController?.show(WindowInsets.Type.statusBars()) - else + else @Suppress("DEPRECATION", "InlinedApi") requireActivity().window.decorView.apply { - systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + systemUiVisibility = + View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN } - } + } - private fun hideStatusBar() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) + private fun hideStatusBar() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requireActivity().window.insetsController?.hide(WindowInsets.Type.statusBars()) - else { - @Suppress("DEPRECATION") - requireActivity().window.decorView.apply { - systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_FULLSCREEN - } - } - } - - private fun showHiddenApps() { - if (prefs.hiddenApps.isEmpty()) { - requireContext().showToast(getString(R.string.no_hidden_apps)) - return - } - viewModel.getHiddenApps() - findNavController().navigate( - R.id.action_settingsFragment_to_appListFragment, - bundleOf(Constants.Key.FLAG to Constants.FLAG_HIDDEN_APPS) - ) - } + else { + @Suppress("DEPRECATION") + requireActivity().window.decorView.apply { + systemUiVisibility = View.SYSTEM_UI_FLAG_IMMERSIVE or View.SYSTEM_UI_FLAG_FULLSCREEN + } + } + } + + private fun showHiddenApps() { + if (prefs.hiddenApps.isEmpty()) { + requireContext().showToast(getString(R.string.no_hidden_apps)) + return + } + viewModel.getHiddenApps() + findNavController() + .navigate( + R.id.action_settingsFragment_to_appListFragment, + bundleOf(Constants.Key.FLAG to Constants.FLAG_HIDDEN_APPS) + ) + } - private fun checkAdminPermission() { - val isAdmin: Boolean = deviceManager.isAdminActive(componentName) - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) - prefs.lockModeOn = isAdmin - } + private fun checkAdminPermission() { + val isAdmin: Boolean = deviceManager.isAdminActive(componentName) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) prefs.lockModeOn = isAdmin + } - private fun toggleAccessibilityVisibility(show: Boolean) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) - binding.notWorking.visibility = View.VISIBLE - if (isAccessServiceEnabled(requireContext())) + private fun toggleAccessibilityVisibility(show: Boolean) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) binding.notWorking.visibility = View.VISIBLE + if (isAccessServiceEnabled(requireContext())) binding.actionAccessibility.text = getString(R.string.disable) - binding.accessibilityLayout.isVisible = show - binding.scrollView.animateAlpha(if (show) 0.5f else 1f) - } - - private fun openAccessibilityService() { - toggleAccessibilityVisibility(false) - // prefs.lockModeOn = true - populateLockSettings() - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) + binding.accessibilityLayout.isVisible = show + binding.scrollView.animateAlpha(if (show) 0.5f else 1f) + } + + private fun openAccessibilityService() { + toggleAccessibilityVisibility(false) + // prefs.lockModeOn = true + populateLockSettings() + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)) - } - - private fun toggleLockMode() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - toggleAccessibilityVisibility(true) - if (prefs.lockModeOn) { - prefs.lockModeOn = false - removeActiveAdmin() - } - } else { - val isAdmin: Boolean = deviceManager.isAdminActive(componentName) - if (isAdmin) { - removeActiveAdmin("Admin permission removed.") - prefs.lockModeOn = false - } else { - val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN) - intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName) - intent.putExtra( - DevicePolicyManager.EXTRA_ADD_EXPLANATION, - getString(R.string.admin_permission_message) - ) - requireActivity().startActivityForResult(intent, Constants.REQUEST_CODE_ENABLE_ADMIN) - } - } - populateLockSettings() - } - - private fun removeActiveAdmin(toastMessage: String? = null) { - try { - deviceManager.removeActiveAdmin(componentName) // for backward compatibility - requireContext().showToast(toastMessage) - } catch (e: Exception) { - e.printStackTrace() - } - } - - private fun removeWallpaper() { - setPlainWallpaper(requireContext(), android.R.color.black) - if (!prefs.dailyWallpaper) return - prefs.dailyWallpaper = false - populateWallpaperText() - viewModel.cancelWallpaperWorker() - } - - private fun toggleDailyWallpaperUpdate() { - if (prefs.dailyWallpaper.not() && viewModel.isOlauncherDefault.value == false) { - requireContext().showToast(R.string.set_as_default_launcher_first) - return - } - prefs.dailyWallpaper = !prefs.dailyWallpaper - populateWallpaperText() - if (prefs.dailyWallpaper) { - viewModel.setWallpaperWorker() - showWallpaperToasts() - } else viewModel.cancelWallpaperWorker() - } - - private fun showWallpaperToasts() { - if (isOlauncherDefault(requireContext())) + } + + private fun toggleLockMode() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + toggleAccessibilityVisibility(true) + if (prefs.lockModeOn) { + prefs.lockModeOn = false + removeActiveAdmin() + } + } else { + val isAdmin: Boolean = deviceManager.isAdminActive(componentName) + if (isAdmin) { + removeActiveAdmin("Admin permission removed.") + prefs.lockModeOn = false + } else { + val intent = Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN) + intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName) + intent.putExtra( + DevicePolicyManager.EXTRA_ADD_EXPLANATION, + getString(R.string.admin_permission_message) + ) + requireActivity().startActivityForResult(intent, Constants.REQUEST_CODE_ENABLE_ADMIN) + } + } + populateLockSettings() + } + + private fun removeActiveAdmin(toastMessage: String? = null) { + try { + deviceManager.removeActiveAdmin(componentName) // for backward compatibility + requireContext().showToast(toastMessage) + } catch (e: Exception) { + e.printStackTrace() + } + } + + private fun removeWallpaper() { + setPlainWallpaper(requireContext(), android.R.color.black) + if (!prefs.dailyWallpaper) return + prefs.dailyWallpaper = false + populateWallpaperText() + viewModel.cancelWallpaperWorker() + } + + private fun toggleDailyWallpaperUpdate() { + if (prefs.dailyWallpaper.not() && viewModel.isOlauncherDefault.value == false) { + requireContext().showToast(R.string.set_as_default_launcher_first) + return + } + prefs.dailyWallpaper = !prefs.dailyWallpaper + populateWallpaperText() + if (prefs.dailyWallpaper) { + viewModel.setWallpaperWorker() + showWallpaperToasts() + } else viewModel.cancelWallpaperWorker() + } + + private fun showWallpaperToasts() { + if (isOlauncherDefault(requireContext())) requireContext().showToast(getString(R.string.your_wallpaper_will_update_shortly)) - else - requireContext().showToast(getString(R.string.olauncher_is_not_default_launcher), Toast.LENGTH_LONG) - } - - private fun updateHomeAppsNum(num: Int) { - binding.homeAppsNum.text = num.toString() - binding.appsNumSelectLayout.visibility = View.GONE - prefs.homeAppsNum = num - viewModel.refreshHome(true) - } - - private fun updateTextSizeScale(sizeScale: Float) { - if (prefs.textSizeScale == sizeScale) return - prefs.textSizeScale = sizeScale - requireActivity().recreate() - } - - private fun toggleKeyboardText() { - if (prefs.autoShowKeyboard && prefs.keyboardMessageShown.not()) { - viewModel.showDialog.postValue(Constants.Dialog.KEYBOARD) - prefs.keyboardMessageShown = true - } else { - prefs.autoShowKeyboard = !prefs.autoShowKeyboard - populateKeyboardText() - } - } - - private fun updateTheme(appTheme: Int) { - if (AppCompatDelegate.getDefaultNightMode() == appTheme) return - prefs.appTheme = appTheme - populateAppThemeText(appTheme) - setAppTheme(appTheme) - } - - private fun setAppTheme(theme: Int) { - if (AppCompatDelegate.getDefaultNightMode() == theme) return - if (prefs.dailyWallpaper) { - setPlainWallpaper(theme) - viewModel.setWallpaperWorker() - } - requireActivity().recreate() - } - - private fun setPlainWallpaper(appTheme: Int) { - when (appTheme) { - AppCompatDelegate.MODE_NIGHT_YES -> setPlainWallpaper(requireContext(), android.R.color.black) - AppCompatDelegate.MODE_NIGHT_NO -> setPlainWallpaper(requireContext(), android.R.color.white) - else -> { - if (requireContext().isDarkThemeOn()) - setPlainWallpaper(requireContext(), android.R.color.black) - else setPlainWallpaper(requireContext(), android.R.color.white) + else + requireContext() + .showToast( + getString(R.string.olauncher_is_not_default_launcher), + Toast.LENGTH_LONG + ) + } + + private fun updateHomeAppsNum(num: Int) { + binding.homeAppsNum.text = num.toString() + binding.appsNumSelectLayout.visibility = View.GONE + prefs.homeAppsNum = num + viewModel.refreshHome(true) + } + + private fun updateTextSizeScale(sizeScale: Float) { + if (prefs.textSizeScale == sizeScale) return + prefs.textSizeScale = sizeScale + requireActivity().recreate() + } + + private fun toggleKeyboardText() { + if (prefs.autoShowKeyboard && prefs.keyboardMessageShown.not()) { + viewModel.showDialog.postValue(Constants.Dialog.KEYBOARD) + prefs.keyboardMessageShown = true + } else { + prefs.autoShowKeyboard = !prefs.autoShowKeyboard + populateKeyboardText() + } + } + + private fun updateTheme(appTheme: Int) { + if (AppCompatDelegate.getDefaultNightMode() == appTheme) return + prefs.appTheme = appTheme + populateAppThemeText(appTheme) + setAppTheme(appTheme) + } + + private fun setAppTheme(theme: Int) { + if (AppCompatDelegate.getDefaultNightMode() == theme) return + if (prefs.dailyWallpaper) { + setPlainWallpaper(theme) + viewModel.setWallpaperWorker() + } + requireActivity().recreate() + } + + private fun setPlainWallpaper(appTheme: Int) { + when (appTheme) { + AppCompatDelegate.MODE_NIGHT_YES -> setPlainWallpaper(requireContext(), android.R.color.black) + AppCompatDelegate.MODE_NIGHT_NO -> setPlainWallpaper(requireContext(), android.R.color.white) + else -> { + if (requireContext().isDarkThemeOn()) + setPlainWallpaper(requireContext(), android.R.color.black) + else setPlainWallpaper(requireContext(), android.R.color.white) + } + } + } + + private fun populateAppThemeText(appTheme: Int = prefs.appTheme) { + when (appTheme) { + AppCompatDelegate.MODE_NIGHT_YES -> binding.appThemeText.text = getString(R.string.dark) + AppCompatDelegate.MODE_NIGHT_NO -> binding.appThemeText.text = getString(R.string.light) + else -> binding.appThemeText.text = getString(R.string.system_default) + } + } + + private fun populateTextSize() { + binding.textSizeValue.text = + when (prefs.textSizeScale) { + Constants.TextSize.ONE -> 1 + Constants.TextSize.TWO -> 2 + Constants.TextSize.THREE -> 3 + Constants.TextSize.FOUR -> 4 + Constants.TextSize.FIVE -> 5 + Constants.TextSize.SIX -> 6 + Constants.TextSize.SEVEN -> 7 + else -> "--" + }.toString() + } + + private fun populateScreenTimeOnOff() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + if (requireContext().appUsagePermissionGranted()) + binding.screenTimeOnOff.text = getString(R.string.on) + else binding.screenTimeOnOff.text = getString(R.string.off) + } else binding.screenTimeLayout.visibility = View.GONE + } + + private fun populateKeyboardText() { + if (prefs.autoShowKeyboard) binding.autoShowKeyboard.text = getString(R.string.on) + else binding.autoShowKeyboard.text = getString(R.string.off) + } + + private fun populateWallpaperText() { + if (prefs.dailyWallpaper) binding.dailyWallpaper.text = getString(R.string.on) + else binding.dailyWallpaper.text = getString(R.string.off) + } + + private fun updateHomeBottomAlignment() { + if (viewModel.isOlauncherDefault.value != true) { + requireContext() + .showToast( + getString(R.string.please_set_olauncher_as_default_first), + Toast.LENGTH_LONG + ) + return + } + prefs.homeBottomAlignment = !prefs.homeBottomAlignment + populateAlignment() + viewModel.updateHomeAlignment(prefs.homeAlignment) + } + + private fun populateAlignment() { + when (prefs.homeAlignment) { + Gravity.START -> binding.alignment.text = getString(R.string.left) + Gravity.CENTER -> binding.alignment.text = getString(R.string.center) + Gravity.END -> binding.alignment.text = getString(R.string.right) + } + binding.alignmentBottom.text = + if (prefs.homeBottomAlignment) getString(R.string.bottom_on) + else getString(R.string.bottom_off) + } + + private fun populateLockSettings() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + binding.toggleLock.text = + getString(if (isAccessServiceEnabled(requireContext())) R.string.on else R.string.off) + } else { + binding.toggleLock.text = getString(if (prefs.lockModeOn) R.string.on else R.string.off) + } + } + + private fun populateSwipeDownAction() { + binding.swipeDownAction.text = + when (prefs.swipeDownAction) { + Constants.SwipeDownAction.NOTIFICATIONS -> getString(R.string.notifications) + else -> getString(R.string.search) } - } - } - - private fun populateAppThemeText(appTheme: Int = prefs.appTheme) { - when (appTheme) { - AppCompatDelegate.MODE_NIGHT_YES -> binding.appThemeText.text = getString(R.string.dark) - AppCompatDelegate.MODE_NIGHT_NO -> binding.appThemeText.text = getString(R.string.light) - else -> binding.appThemeText.text = getString(R.string.system_default) - } - } - - private fun populateTextSize() { - binding.textSizeValue.text = when (prefs.textSizeScale) { - Constants.TextSize.ONE -> 1 - Constants.TextSize.TWO -> 2 - Constants.TextSize.THREE -> 3 - Constants.TextSize.FOUR -> 4 - Constants.TextSize.FIVE -> 5 - Constants.TextSize.SIX -> 6 - Constants.TextSize.SEVEN -> 7 - else -> "--" - }.toString() - } - - private fun populateScreenTimeOnOff() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if (requireContext().appUsagePermissionGranted()) binding.screenTimeOnOff.text = getString(R.string.on) - else binding.screenTimeOnOff.text = getString(R.string.off) - } else binding.screenTimeLayout.visibility = View.GONE - } - - private fun populateKeyboardText() { - if (prefs.autoShowKeyboard) binding.autoShowKeyboard.text = getString(R.string.on) - else binding.autoShowKeyboard.text = getString(R.string.off) - } - - private fun populateWallpaperText() { - if (prefs.dailyWallpaper) binding.dailyWallpaper.text = getString(R.string.on) - else binding.dailyWallpaper.text = getString(R.string.off) - } - - private fun updateHomeBottomAlignment() { - if (viewModel.isOlauncherDefault.value != true) { - requireContext().showToast(getString(R.string.please_set_olauncher_as_default_first), Toast.LENGTH_LONG) - return - } - prefs.homeBottomAlignment = !prefs.homeBottomAlignment - populateAlignment() - viewModel.updateHomeAlignment(prefs.homeAlignment) - } - - private fun populateAlignment() { - when (prefs.homeAlignment) { - Gravity.START -> binding.alignment.text = getString(R.string.left) - Gravity.CENTER -> binding.alignment.text = getString(R.string.center) - Gravity.END -> binding.alignment.text = getString(R.string.right) - } - binding.alignmentBottom.text = if (prefs.homeBottomAlignment) - getString(R.string.bottom_on) - else getString(R.string.bottom_off) - } - - private fun populateLockSettings() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { - binding.toggleLock.text = getString( - if (isAccessServiceEnabled(requireContext())) R.string.on - else R.string.off + } + + private fun updateSwipeDownAction(swipeDownFor: Int) { + if (prefs.swipeDownAction == swipeDownFor) return + prefs.swipeDownAction = swipeDownFor + populateSwipeDownAction() + } + + private fun populateSwipeApps() { + binding.swipeLeftApp.text = prefs.appNameSwipeLeft + binding.swipeRightApp.text = prefs.appNameSwipeRight + if (!prefs.swipeLeftEnabled) + binding.swipeLeftApp.setTextColor( + requireContext().getColorFromAttr(R.attr.primaryColorTrans50) ) - } else { - binding.toggleLock.text = getString( - if (prefs.lockModeOn) R.string.on - else R.string.off + if (!prefs.swipeRightEnabled) + binding.swipeRightApp.setTextColor( + requireContext().getColorFromAttr(R.attr.primaryColorTrans50) ) - } - } - - private fun populateSwipeDownAction() { - binding.swipeDownAction.text = when (prefs.swipeDownAction) { - Constants.SwipeDownAction.NOTIFICATIONS -> getString(R.string.notifications) - else -> getString(R.string.search) - } - } - - private fun updateSwipeDownAction(swipeDownFor: Int) { - if (prefs.swipeDownAction == swipeDownFor) return - prefs.swipeDownAction = swipeDownFor - populateSwipeDownAction() - } - - private fun populateSwipeApps() { - binding.swipeLeftApp.text = prefs.appNameSwipeLeft - binding.swipeRightApp.text = prefs.appNameSwipeRight - if (!prefs.swipeLeftEnabled) - binding.swipeLeftApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColorTrans50)) - if (!prefs.swipeRightEnabled) - binding.swipeRightApp.setTextColor(requireContext().getColorFromAttr(R.attr.primaryColorTrans50)) - } - -// private fun populateDigitalWellbeing() { -// binding.digitalWellbeing.isVisible = requireContext().isPackageInstalled(Constants.DIGITAL_WELLBEING_PACKAGE_NAME).not() -// && requireContext().isPackageInstalled(Constants.DIGITAL_WELLBEING_SAMSUNG_PACKAGE_NAME).not() -// && prefs.hideDigitalWellbeing.not() -// } - - private fun showAppListIfEnabled(flag: Int) { - if ((flag == Constants.FLAG_SET_SWIPE_LEFT_APP) and !prefs.swipeLeftEnabled) { - requireContext().showToast(getString(R.string.long_press_to_enable)) - return - } - if ((flag == Constants.FLAG_SET_SWIPE_RIGHT_APP) and !prefs.swipeRightEnabled) { - requireContext().showToast(getString(R.string.long_press_to_enable)) - return - } - viewModel.getAppList(true) - findNavController().navigate( - R.id.action_settingsFragment_to_appListFragment, - bundleOf(Constants.Key.FLAG to flag) - ) - } - - private fun populateActionHints() { - if (prefs.aboutClicked.not()) - binding.aboutOlauncher.setCompoundDrawablesWithIntrinsicBounds(0, 0, android.R.drawable.stat_notify_more, 0) - if (viewModel.isOlauncherDefault.value != true) return - if (prefs.rateClicked.not() && prefs.toShowHintCounter > Constants.HINT_RATE_US && prefs.toShowHintCounter < Constants.HINT_RATE_US + 100) - binding.rate.setCompoundDrawablesWithIntrinsicBounds(0, android.R.drawable.arrow_down_float, 0, 0) - } + } + + // private fun populateDigitalWellbeing() { + // binding.digitalWellbeing.isVisible = + // requireContext().isPackageInstalled(Constants.DIGITAL_WELLBEING_PACKAGE_NAME).not() + // && + // requireContext().isPackageInstalled(Constants.DIGITAL_WELLBEING_SAMSUNG_PACKAGE_NAME).not() + // && prefs.hideDigitalWellbeing.not() + // } + + private fun showAppListIfEnabled(flag: Int) { + if ((flag == Constants.FLAG_SET_SWIPE_LEFT_APP) and !prefs.swipeLeftEnabled) { + requireContext().showToast(getString(R.string.long_press_to_enable)) + return + } + if ((flag == Constants.FLAG_SET_SWIPE_RIGHT_APP) and !prefs.swipeRightEnabled) { + requireContext().showToast(getString(R.string.long_press_to_enable)) + return + } + viewModel.getAppList(true) + findNavController() + .navigate( + R.id.action_settingsFragment_to_appListFragment, + bundleOf(Constants.Key.FLAG to flag) + ) + } + + private fun populateActionHints() { + if (prefs.aboutClicked.not()) + binding.aboutOlauncher.setCompoundDrawablesWithIntrinsicBounds( + 0, + 0, + android.R.drawable.stat_notify_more, + 0 + ) + if (viewModel.isOlauncherDefault.value != true) return + if (prefs.rateClicked.not() && + prefs.toShowHintCounter > Constants.HINT_RATE_US && + prefs.toShowHintCounter < Constants.HINT_RATE_US + 100 + ) + binding.rate.setCompoundDrawablesWithIntrinsicBounds( + 0, + android.R.drawable.arrow_down_float, + 0, + 0 + ) + } - private fun populateProMessage() { - if (prefs.proMessageShown.not() && prefs.userState == Constants.UserState.SHARE) { - prefs.proMessageShown = true - viewModel.showDialog.postValue(Constants.Dialog.PRO_MESSAGE) - } + private fun populateProMessage() { + if (prefs.proMessageShown.not() && prefs.userState == Constants.UserState.SHARE) { + prefs.proMessageShown = true + viewModel.showDialog.postValue(Constants.Dialog.PRO_MESSAGE) } + } - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } + override fun onDestroyView() { + super.onDestroyView() + _binding = null + } - override fun onDestroy() { - viewModel.checkForMessages.call() - super.onDestroy() - } -} \ No newline at end of file + override fun onDestroy() { + viewModel.checkForMessages.call() + super.onDestroy() + } +} diff --git a/app/src/main/res/layout-land/fragment_settings.xml b/app/src/main/res/layout-land/fragment_settings.xml index eeebae41d..34c49b4cc 100644 --- a/app/src/main/res/layout-land/fragment_settings.xml +++ b/app/src/main/res/layout-land/fragment_settings.xml @@ -454,6 +454,18 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_app_limit_config.xml b/app/src/main/res/layout/fragment_app_limit_config.xml new file mode 100644 index 000000000..6327861bc --- /dev/null +++ b/app/src/main/res/layout/fragment_app_limit_config.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_app_limits.xml b/app/src/main/res/layout/fragment_app_limits.xml new file mode 100644 index 000000000..43788774a --- /dev/null +++ b/app/src/main/res/layout/fragment_app_limits.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 514f38544..3cc53847a 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -470,6 +470,18 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 876d807fb..276396f78 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -155,4 +155,19 @@ Alignment changed ✅ Home screen Hey + + + App Limits + Block apps during specific times to reduce distractions + Add app limit + No app limits set + Block from + Block until + Self-control mode: On (prevents changes when active) + Self-control mode: Off (allows changes anytime) + Save + Remove limit + This app is blocked until %s + Select app to limit + This limit is currently active and locked \ No newline at end of file