forked from upstream-mirrors/quik
Move packages to com.moez.QKSMS
This commit is contained in:
@@ -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.manager
|
||||
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.ServiceConnection
|
||||
import android.os.Handler
|
||||
import android.os.IBinder
|
||||
import android.os.Message
|
||||
import android.os.Messenger
|
||||
import androidx.os.bundleOf
|
||||
import com.moez.QKSMS.util.Preferences
|
||||
import com.moez.QKSMS.util.tryOrNull
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.subjects.SingleSubject
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
class ExternalBlockingManagerImpl @Inject constructor(
|
||||
private val context: Context,
|
||||
private val prefs: Preferences
|
||||
) : ExternalBlockingManager {
|
||||
|
||||
companion object {
|
||||
const val RATING_UNKNOWN = 0
|
||||
const val RATING_POSITIVE = 1
|
||||
const val RATING_NEGATIVE = 2
|
||||
const val RATING_NEUTRAL = 3
|
||||
|
||||
const val GET_NUMBER_RATING = 1
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a Single<Boolean> which emits whether or not the given [address] should be blocked
|
||||
*/
|
||||
override fun shouldBlock(address: String): Single<Boolean> {
|
||||
return Binder(context, prefs, address).shouldBlock()
|
||||
}
|
||||
|
||||
private class Binder(
|
||||
private val context: Context,
|
||||
private val prefs: Preferences,
|
||||
private val address: String
|
||||
) : ServiceConnection {
|
||||
|
||||
private val subject: SingleSubject<Boolean> = SingleSubject.create()
|
||||
private var serviceMessenger: Messenger? = null
|
||||
private var isBound: Boolean = false
|
||||
|
||||
fun shouldBlock(): Single<Boolean> {
|
||||
|
||||
var intent: Intent? = null
|
||||
|
||||
// If either version of Should I Answer? is installed and SIA is enabled, build the
|
||||
// intent to request a rating
|
||||
if (prefs.sia.get()) {
|
||||
intent = tryOrNull {
|
||||
context.packageManager.getApplicationInfo("org.mistergroup.shouldianswerpersonal", 0).enabled
|
||||
Intent("org.mistergroup.shouldianswerpersonal.PublicService").setPackage("org.mistergroup.shouldianswerpersonal")
|
||||
} ?: tryOrNull {
|
||||
context.packageManager.getApplicationInfo("org.mistergroup.muzutozvednout", 0).enabled
|
||||
Intent("org.mistergroup.muzutozvednout.PublicService").setPackage("org.mistergroup.muzutozvednout")
|
||||
}
|
||||
}
|
||||
|
||||
// If the intent isn't null, bind the service and wait for a result. Otherwise, don't block
|
||||
if (intent != null) {
|
||||
context.bindService(intent, this, Context.BIND_AUTO_CREATE)
|
||||
} else {
|
||||
subject.onSuccess(false)
|
||||
}
|
||||
|
||||
return subject
|
||||
}
|
||||
|
||||
override fun onServiceConnected(className: ComponentName, service: IBinder) {
|
||||
serviceMessenger = Messenger(service)
|
||||
isBound = true
|
||||
|
||||
val msg = Message()
|
||||
msg.what = GET_NUMBER_RATING
|
||||
msg.data = bundleOf(Pair("number", address))
|
||||
msg.replyTo = Messenger(IncomingHandler { rating ->
|
||||
subject.onSuccess(rating == RATING_NEGATIVE)
|
||||
Timber.v("Should block: ${rating == RATING_NEGATIVE}")
|
||||
|
||||
// We're done, so unbind the service
|
||||
if (isBound && serviceMessenger != null) {
|
||||
context.unbindService(this)
|
||||
}
|
||||
})
|
||||
|
||||
serviceMessenger?.send(msg)
|
||||
}
|
||||
|
||||
override fun onServiceDisconnected(className: ComponentName) {
|
||||
serviceMessenger = null
|
||||
isBound = false
|
||||
}
|
||||
}
|
||||
|
||||
private class IncomingHandler(private val callback: (rating: Int) -> Unit) : Handler() {
|
||||
|
||||
override fun handleMessage(msg: Message) {
|
||||
when (msg.what) {
|
||||
GET_NUMBER_RATING -> callback(msg.data.getInt("rating"))
|
||||
else -> super.handleMessage(msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* 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.manager
|
||||
|
||||
import com.moez.QKSMS.model.Message
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class KeyManagerImpl @Inject constructor() : KeyManager {
|
||||
|
||||
private var initialized = false
|
||||
private var maxValue: Long = 0
|
||||
|
||||
/**
|
||||
* Should be called when a new sync is being started
|
||||
*/
|
||||
override fun reset() {
|
||||
initialized = true
|
||||
maxValue = 0L
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a valid ID that can be used to store a new message
|
||||
*/
|
||||
override fun newId(): Long {
|
||||
if (!initialized) {
|
||||
maxValue = Realm.getDefaultInstance().use { realm ->
|
||||
realm.where(Message::class.java).max("id")?.toLong() ?: 0L
|
||||
}
|
||||
initialized = true
|
||||
}
|
||||
|
||||
maxValue++
|
||||
return maxValue
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.manager
|
||||
|
||||
import android.Manifest
|
||||
import android.content.Context
|
||||
import android.content.pm.PackageManager.PERMISSION_GRANTED
|
||||
import android.provider.Telephony
|
||||
import android.support.v4.content.ContextCompat
|
||||
import javax.inject.Inject
|
||||
|
||||
class PermissionManagerImpl @Inject constructor(private val context: Context) : PermissionManager {
|
||||
|
||||
override fun isDefaultSms(): Boolean {
|
||||
return Telephony.Sms.getDefaultSmsPackage(context) == context.packageName
|
||||
}
|
||||
|
||||
override fun hasSmsAndContacts(): Boolean {
|
||||
return hasSms() && hasContacts()
|
||||
}
|
||||
|
||||
override fun hasSms(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_SMS) == PERMISSION_GRANTED
|
||||
|
||||
override fun hasContacts(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PERMISSION_GRANTED
|
||||
|
||||
override fun hasPhone(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PERMISSION_GRANTED
|
||||
|
||||
override fun hasStorage(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.manager
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import com.klinker.android.send_message.BroadcastUtils
|
||||
import me.leolin.shortcutbadger.ShortcutBadger
|
||||
import javax.inject.Inject
|
||||
|
||||
class WidgetManagerImpl @Inject constructor(private val context: Context) : WidgetManager {
|
||||
|
||||
override fun updateUnreadCount(count: Int) {
|
||||
ShortcutBadger.applyCount(context, count)
|
||||
|
||||
BroadcastUtils.sendExplicitBroadcast(context, Intent(), WidgetManager.ACTION_NOTIFY_DATASET_CHANGED)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user