forked from upstream-mirrors/quik
Move packages to com.moez.QKSMS
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
import com.moez.QKSMS.compat.SubscriptionInfoCompat
|
||||
import com.moez.QKSMS.compat.SubscriptionManagerCompat
|
||||
import io.reactivex.Observable
|
||||
import io.reactivex.Observer
|
||||
import io.reactivex.disposables.Disposable
|
||||
|
||||
class ActiveSubscriptionObservable(
|
||||
private val subscriptionManager: SubscriptionManagerCompat
|
||||
) : Observable<List<SubscriptionInfoCompat>>() {
|
||||
|
||||
override fun subscribeActual(observer: Observer<in List<SubscriptionInfoCompat>>) {
|
||||
observer.onNext(subscriptionManager.activeSubscriptionInfoList)
|
||||
|
||||
val listener = Listener(subscriptionManager, observer)
|
||||
observer.onSubscribe(listener)
|
||||
subscriptionManager.addOnSubscriptionsChangedListener(listener)
|
||||
}
|
||||
|
||||
internal class Listener(
|
||||
private val subscriptionManager: SubscriptionManagerCompat,
|
||||
private val observer: Observer<in List<SubscriptionInfoCompat>>
|
||||
) : Disposable, SubscriptionManagerCompat.OnSubscriptionsChangedListener() {
|
||||
|
||||
private var disposed: Boolean = false
|
||||
|
||||
override fun onSubscriptionsChanged() {
|
||||
if (!isDisposed) {
|
||||
observer.onNext(subscriptionManager.activeSubscriptionInfoList)
|
||||
}
|
||||
}
|
||||
|
||||
override fun isDisposed(): Boolean = disposed
|
||||
|
||||
override fun dispose() {
|
||||
disposed = true
|
||||
subscriptionManager.removeOnSubscriptionsChangedListener(this)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
import android.app.AlarmManager
|
||||
import android.app.PendingIntent
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.moez.QKSMS.receiver.NightModeReceiver
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class NightModeManager @Inject constructor(
|
||||
private val context: Context,
|
||||
private val prefs: Preferences) {
|
||||
|
||||
fun updateCurrentTheme() {
|
||||
// If night mode is not on auto, then there's nothing to do here
|
||||
if (prefs.nightMode.get() != Preferences.NIGHT_MODE_AUTO) {
|
||||
return
|
||||
}
|
||||
|
||||
val nightStartTime = getPreviousInstanceOfTime(prefs.nightStart.get())
|
||||
val nightEndTime = getPreviousInstanceOfTime(prefs.nightEnd.get())
|
||||
|
||||
// If the last nightStart was more recent than the last nightEnd, then it's night time
|
||||
prefs.night.set(nightStartTime > nightEndTime)
|
||||
}
|
||||
|
||||
fun updateNightMode(mode: Int) {
|
||||
prefs.nightMode.set(mode)
|
||||
|
||||
// If it's not on auto mode, set the appropriate night mode
|
||||
if (mode != Preferences.NIGHT_MODE_AUTO) {
|
||||
prefs.night.set(mode == Preferences.NIGHT_MODE_ON)
|
||||
}
|
||||
|
||||
updateAlarms()
|
||||
}
|
||||
|
||||
fun setNightStart(hour: Int, minute: Int) {
|
||||
prefs.nightStart.set("$hour:$minute")
|
||||
updateAlarms()
|
||||
}
|
||||
|
||||
fun setNightEnd(hour: Int, minute: Int) {
|
||||
prefs.nightEnd.set("$hour:$minute")
|
||||
updateAlarms()
|
||||
}
|
||||
|
||||
private fun updateAlarms() {
|
||||
val dayCalendar = createCalendar(prefs.nightEnd.get())
|
||||
val day = Intent(context, NightModeReceiver::class.java)
|
||||
val dayIntent = PendingIntent.getBroadcast(context, 0, day, 0)
|
||||
|
||||
val nightCalendar = createCalendar(prefs.nightStart.get())
|
||||
val night = Intent(context, NightModeReceiver::class.java)
|
||||
val nightIntent = PendingIntent.getBroadcast(context, 1, night, 0)
|
||||
|
||||
context.sendBroadcast(day)
|
||||
context.sendBroadcast(night)
|
||||
|
||||
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
if (prefs.nightMode.get() == Preferences.NIGHT_MODE_AUTO) {
|
||||
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, dayCalendar.timeInMillis, AlarmManager.INTERVAL_DAY, dayIntent)
|
||||
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, nightCalendar.timeInMillis, AlarmManager.INTERVAL_DAY, nightIntent)
|
||||
} else {
|
||||
alarmManager.cancel(dayIntent)
|
||||
alarmManager.cancel(nightIntent)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createCalendar(time: String): Calendar {
|
||||
val calendar = parseTime(time)
|
||||
|
||||
return Calendar.getInstance().apply {
|
||||
set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY))
|
||||
set(Calendar.MINUTE, calendar.get(Calendar.MINUTE))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the hour and minute out of the [time], which should be formatted h:mm
|
||||
*/
|
||||
fun parseTime(time: String): Calendar {
|
||||
return tryOrNull {
|
||||
val parsedTime = SimpleDateFormat("H:mm", Locale.US).parse(time)
|
||||
Calendar.getInstance().apply { this.time = parsedTime }
|
||||
} ?: tryOrNull {
|
||||
// Parse the legacy timestamp format (<=3.1.3)
|
||||
val parsedTime = SimpleDateFormat("h:mm a", Locale.US).parse(time)
|
||||
Calendar.getInstance().apply { this.time = parsedTime }
|
||||
} ?: Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 18) }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Calendar set to the most recent occurrence of this time
|
||||
*/
|
||||
private fun getPreviousInstanceOfTime(time: String): Calendar {
|
||||
val currentTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10)
|
||||
val calendar = createCalendar(time)
|
||||
|
||||
while (calendar.timeInMillis > currentTime) {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -1)
|
||||
}
|
||||
|
||||
return calendar
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
import android.os.Build
|
||||
import android.provider.Settings
|
||||
import com.f2prateek.rx.preferences2.Preference
|
||||
import com.f2prateek.rx.preferences2.RxSharedPreferences
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences) {
|
||||
|
||||
companion object {
|
||||
const val NIGHT_MODE_OFF = 0
|
||||
const val NIGHT_MODE_ON = 1
|
||||
const val NIGHT_MODE_AUTO = 2
|
||||
|
||||
const val TEXT_SIZE_SMALL = 0
|
||||
const val TEXT_SIZE_NORMAL = 1
|
||||
const val TEXT_SIZE_LARGE = 2
|
||||
const val TEXT_SIZE_LARGER = 3
|
||||
|
||||
const val NOTIFICATION_PREVIEWS_ALL = 0
|
||||
const val NOTIFICATION_PREVIEWS_NAME = 1
|
||||
const val NOTIFICATION_PREVIEWS_NONE = 2
|
||||
|
||||
const val SEND_DELAY_NONE = 0
|
||||
const val SEND_DELAY_SHORT = 1
|
||||
const val SEND_DELAY_MEDIUM = 2
|
||||
const val SEND_DELAY_LONG = 3
|
||||
}
|
||||
|
||||
val night = rxPrefs.getBoolean("night", false)
|
||||
val nightMode = rxPrefs.getInteger("nightModeSummary", NIGHT_MODE_OFF)
|
||||
val nightStart = rxPrefs.getString("nightStart", "18:00")
|
||||
val nightEnd = rxPrefs.getString("nightEnd", "6:00")
|
||||
val black = rxPrefs.getBoolean("black", false)
|
||||
val sia = rxPrefs.getBoolean("sia", false)
|
||||
val sendDelay = rxPrefs.getInteger("sendDelay", SEND_DELAY_NONE)
|
||||
val autoEmoji = rxPrefs.getBoolean("autoEmoji", true)
|
||||
val delivery = rxPrefs.getBoolean("delivery", false)
|
||||
val systemFont = rxPrefs.getBoolean("systemFont", false)
|
||||
val textSize = rxPrefs.getInteger("textSize", TEXT_SIZE_NORMAL)
|
||||
val qkreply = rxPrefs.getBoolean("qkreply", Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
||||
val qkreplyTapDismiss = rxPrefs.getBoolean("qkreplyTapDismiss", true)
|
||||
val unicode = rxPrefs.getBoolean("unicode", false)
|
||||
val mmsSize = rxPrefs.getInteger("mmsSize", 300)
|
||||
|
||||
fun theme(threadId: Long = 0): Preference<Int> {
|
||||
val default = rxPrefs.getInteger("theme", 0xFF0097A7.toInt())
|
||||
|
||||
return when (threadId) {
|
||||
0L -> default
|
||||
else -> rxPrefs.getInteger("theme_$threadId", default.get())
|
||||
}
|
||||
}
|
||||
|
||||
fun notifications(threadId: Long = 0): Preference<Boolean> {
|
||||
val default = rxPrefs.getBoolean("notifications", true)
|
||||
|
||||
return when (threadId) {
|
||||
0L -> default
|
||||
else -> rxPrefs.getBoolean("notifications_$threadId", default.get())
|
||||
}
|
||||
}
|
||||
|
||||
fun notificationPreviews(threadId: Long = 0): Preference<Int> {
|
||||
val default = rxPrefs.getInteger("notification_previews", 0)
|
||||
|
||||
return when (threadId) {
|
||||
0L -> default
|
||||
else -> rxPrefs.getInteger("notification_previews_$threadId", default.get())
|
||||
}
|
||||
}
|
||||
|
||||
fun vibration(threadId: Long = 0): Preference<Boolean> {
|
||||
val default = rxPrefs.getBoolean("vibration", true)
|
||||
|
||||
return when (threadId) {
|
||||
0L -> default
|
||||
else -> rxPrefs.getBoolean("vibration$threadId", default.get())
|
||||
}
|
||||
}
|
||||
|
||||
fun ringtone(threadId: Long = 0): Preference<String> {
|
||||
val default = rxPrefs.getString("ringtone", Settings.System.DEFAULT_NOTIFICATION_URI.toString())
|
||||
|
||||
return when (threadId) {
|
||||
0L -> default
|
||||
else -> rxPrefs.getString("ringtone_$threadId", default.get())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.util
|
||||
|
||||
fun <T> tryOrNull(body: () -> T?): T? {
|
||||
return try {
|
||||
body()
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user