😎 Make code more Kotliney
This commit is contained in:
parent
3767d976c8
commit
ccfdff7fd8
@ -35,7 +35,7 @@ import ch.dissem.apps.abit.ComposeMessageActivity.Companion.EXTRA_SUBJECT
|
|||||||
import ch.dissem.apps.abit.adapter.ContactAdapter
|
import ch.dissem.apps.abit.adapter.ContactAdapter
|
||||||
import ch.dissem.apps.abit.dialog.SelectEncodingDialogFragment
|
import ch.dissem.apps.abit.dialog.SelectEncodingDialogFragment
|
||||||
import ch.dissem.apps.abit.service.Singleton
|
import ch.dissem.apps.abit.service.Singleton
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
import ch.dissem.bitmessage.entity.BitmessageAddress
|
import ch.dissem.bitmessage.entity.BitmessageAddress
|
||||||
import ch.dissem.bitmessage.entity.Plaintext
|
import ch.dissem.bitmessage.entity.Plaintext
|
||||||
import ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST
|
import ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST
|
||||||
@ -94,8 +94,7 @@ class ComposeMessageFragment : Fragment() {
|
|||||||
if (containsKey(EXTRA_CONTENT)) {
|
if (containsKey(EXTRA_CONTENT)) {
|
||||||
content = getString(EXTRA_CONTENT)
|
content = getString(EXTRA_CONTENT)
|
||||||
}
|
}
|
||||||
encoding = getSerializable(EXTRA_ENCODING) as? Plaintext.Encoding ?:
|
encoding = getSerializable(EXTRA_ENCODING) as? Plaintext.Encoding ?: Plaintext.Encoding.SIMPLE
|
||||||
Plaintext.Encoding.SIMPLE
|
|
||||||
|
|
||||||
if (containsKey(EXTRA_PARENT)) {
|
if (containsKey(EXTRA_PARENT)) {
|
||||||
val parent = getSerializable(EXTRA_PARENT) as Plaintext
|
val parent = getSerializable(EXTRA_PARENT) as Plaintext
|
||||||
@ -221,7 +220,7 @@ class ComposeMessageFragment : Fragment() {
|
|||||||
}
|
}
|
||||||
val sender = sender_input.selectedItem as? ch.dissem.bitmessage.entity.BitmessageAddress
|
val sender = sender_input.selectedItem as? ch.dissem.bitmessage.entity.BitmessageAddress
|
||||||
sender?.let { builder.from(it) }
|
sender?.let { builder.from(it) }
|
||||||
if (!Preferences.requestAcknowledgements(ctx)) {
|
if (!ctx.preferences.requestAcknowledgements) {
|
||||||
builder.preventAck()
|
builder.preventAck()
|
||||||
}
|
}
|
||||||
when (encoding) {
|
when (encoding) {
|
||||||
|
@ -31,10 +31,7 @@ import ch.dissem.apps.abit.listener.ListSelectionListener
|
|||||||
import ch.dissem.apps.abit.repository.AndroidLabelRepository.Companion.LABEL_ARCHIVE
|
import ch.dissem.apps.abit.repository.AndroidLabelRepository.Companion.LABEL_ARCHIVE
|
||||||
import ch.dissem.apps.abit.service.Singleton
|
import ch.dissem.apps.abit.service.Singleton
|
||||||
import ch.dissem.apps.abit.service.Singleton.currentLabel
|
import ch.dissem.apps.abit.service.Singleton.currentLabel
|
||||||
import ch.dissem.apps.abit.util.NetworkUtils
|
import ch.dissem.apps.abit.util.*
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
|
||||||
import ch.dissem.apps.abit.util.getColor
|
|
||||||
import ch.dissem.apps.abit.util.getIcon
|
|
||||||
import ch.dissem.bitmessage.BitmessageContext
|
import ch.dissem.bitmessage.BitmessageContext
|
||||||
import ch.dissem.bitmessage.entity.BitmessageAddress
|
import ch.dissem.bitmessage.entity.BitmessageAddress
|
||||||
import ch.dissem.bitmessage.entity.Conversation
|
import ch.dissem.bitmessage.entity.Conversation
|
||||||
@ -253,11 +250,11 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
|
|||||||
.withIdentifier(ID_NODE_SWITCH)
|
.withIdentifier(ID_NODE_SWITCH)
|
||||||
.withName(R.string.online)
|
.withName(R.string.online)
|
||||||
.withIcon(CommunityMaterial.Icon.cmd_cloud_outline)
|
.withIcon(CommunityMaterial.Icon.cmd_cloud_outline)
|
||||||
.withChecked(Preferences.isOnline(this))
|
.withChecked(preferences.online)
|
||||||
.withOnCheckedChangeListener { _, _, isChecked ->
|
.withOnCheckedChangeListener { _, _, isChecked ->
|
||||||
Preferences.setOnline(this, isChecked)
|
preferences.online = isChecked
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
NetworkUtils.enableNode(this, true)
|
network.enableNode(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,7 +357,7 @@ class MainActivity : AppCompatActivity(), ListSelectionListener<Serializable> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onResume() {
|
override fun onResume() {
|
||||||
NetworkUtils.enableNode(this, false)
|
network.enableNode(false)
|
||||||
updateUnread()
|
updateUnread()
|
||||||
Singleton.getMessageListener(this).resetNotification()
|
Singleton.getMessageListener(this).resetNotification()
|
||||||
currentLabel.addObserver(this) { label ->
|
currentLabel.addObserver(this) { label ->
|
||||||
|
@ -17,7 +17,10 @@
|
|||||||
package ch.dissem.apps.abit
|
package ch.dissem.apps.abit
|
||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.*
|
import android.content.ComponentName
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.ServiceConnection
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.os.IBinder
|
import android.os.IBinder
|
||||||
import android.support.v4.app.Fragment
|
import android.support.v4.app.Fragment
|
||||||
@ -34,8 +37,8 @@ import ch.dissem.apps.abit.service.BatchProcessorService
|
|||||||
import ch.dissem.apps.abit.service.SimpleJob
|
import ch.dissem.apps.abit.service.SimpleJob
|
||||||
import ch.dissem.apps.abit.service.Singleton
|
import ch.dissem.apps.abit.service.Singleton
|
||||||
import ch.dissem.apps.abit.util.Exports
|
import ch.dissem.apps.abit.util.Exports
|
||||||
import ch.dissem.apps.abit.util.NetworkUtils
|
import ch.dissem.apps.abit.util.network
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
import ch.dissem.bitmessage.entity.Plaintext
|
import ch.dissem.bitmessage.entity.Plaintext
|
||||||
import com.mikepenz.aboutlibraries.Libs
|
import com.mikepenz.aboutlibraries.Libs
|
||||||
import com.mikepenz.aboutlibraries.LibsBuilder
|
import com.mikepenz.aboutlibraries.LibsBuilder
|
||||||
@ -99,7 +102,7 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceFragmentCompat.On
|
|||||||
val bmc = Singleton.getBitmessageContext(ctx)
|
val bmc = Singleton.getBitmessageContext(ctx)
|
||||||
bmc.internals.nodeRegistry.clear()
|
bmc.internals.nodeRegistry.clear()
|
||||||
bmc.cleanup()
|
bmc.cleanup()
|
||||||
Preferences.cleanupExportDirectory(ctx)
|
ctx.preferences.cleanupExportDirectory()
|
||||||
|
|
||||||
uiThread {
|
uiThread {
|
||||||
Toast.makeText(
|
Toast.makeText(
|
||||||
@ -118,7 +121,7 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceFragmentCompat.On
|
|||||||
|
|
||||||
indeterminateProgressDialog(R.string.export_data_summary, R.string.export_data).apply {
|
indeterminateProgressDialog(R.string.export_data_summary, R.string.export_data).apply {
|
||||||
doAsync {
|
doAsync {
|
||||||
val exportDirectory = Preferences.getExportDirectory(ctx)
|
val exportDirectory = ctx.preferences.exportDirectory
|
||||||
exportDirectory.mkdirs()
|
exportDirectory.mkdirs()
|
||||||
val file = Exports.exportData(exportDirectory, ctx)
|
val file = Exports.exportData(exportDirectory, ctx)
|
||||||
val contentUri = getUriForFile(ctx, "ch.dissem.apps.abit.fileprovider", file)
|
val contentUri = getUriForFile(ctx, "ch.dissem.apps.abit.fileprovider", file)
|
||||||
@ -157,7 +160,7 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceFragmentCompat.On
|
|||||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||||
val ctx = context ?: throw IllegalStateException("No context available")
|
val ctx = context ?: throw IllegalStateException("No context available")
|
||||||
when (requestCode) {
|
when (requestCode) {
|
||||||
WRITE_EXPORT_REQUEST_CODE -> Preferences.cleanupExportDirectory(ctx)
|
WRITE_EXPORT_REQUEST_CODE -> ctx.preferences.cleanupExportDirectory()
|
||||||
READ_IMPORT_REQUEST_CODE -> {
|
READ_IMPORT_REQUEST_CODE -> {
|
||||||
if (resultCode == Activity.RESULT_OK && data?.data != null) {
|
if (resultCode == Activity.RESULT_OK && data?.data != null) {
|
||||||
indeterminateProgressDialog(R.string.import_data_summary, R.string.import_data).apply {
|
indeterminateProgressDialog(R.string.import_data_summary, R.string.import_data).apply {
|
||||||
@ -228,7 +231,7 @@ class SettingsFragment : PreferenceFragmentCompat(), PreferenceFragmentCompat.On
|
|||||||
|
|
||||||
private fun connectivityChangeListener() =
|
private fun connectivityChangeListener() =
|
||||||
OnPreferenceChangeListener { _, _ ->
|
OnPreferenceChangeListener { _, _ ->
|
||||||
context?.let { ctx -> NetworkUtils.scheduleNodeStart(ctx) }
|
context?.network?.scheduleNodeStart()
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,8 +19,8 @@ package ch.dissem.apps.abit.dialog
|
|||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import ch.dissem.apps.abit.R
|
import ch.dissem.apps.abit.R
|
||||||
import ch.dissem.apps.abit.util.NetworkUtils
|
import ch.dissem.apps.abit.util.network
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
import kotlinx.android.synthetic.main.dialog_full_node.*
|
import kotlinx.android.synthetic.main.dialog_full_node.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,12 +31,12 @@ class FullNodeDialogActivity : Activity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.dialog_full_node)
|
setContentView(R.layout.dialog_full_node)
|
||||||
ok.setOnClickListener {
|
ok.setOnClickListener {
|
||||||
Preferences.setWifiOnly(this@FullNodeDialogActivity, false)
|
preferences.wifiOnly = false
|
||||||
NetworkUtils.enableNode(applicationContext)
|
network.enableNode()
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
dismiss.setOnClickListener {
|
dismiss.setOnClickListener {
|
||||||
NetworkUtils.scheduleNodeStart(applicationContext)
|
network.scheduleNodeStart()
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,7 +19,7 @@ package ch.dissem.apps.abit.listener
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import ch.dissem.apps.abit.MainActivity
|
import ch.dissem.apps.abit.MainActivity
|
||||||
import ch.dissem.apps.abit.notification.NewMessageNotification
|
import ch.dissem.apps.abit.notification.NewMessageNotification
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
import ch.dissem.bitmessage.BitmessageContext
|
import ch.dissem.bitmessage.BitmessageContext
|
||||||
import ch.dissem.bitmessage.entity.Plaintext
|
import ch.dissem.bitmessage.entity.Plaintext
|
||||||
import ch.dissem.bitmessage.ports.MessageRepository
|
import ch.dissem.bitmessage.ports.MessageRepository
|
||||||
@ -50,7 +50,7 @@ class MessageListener(ctx: Context) : BitmessageContext.Listener.WithContext {
|
|||||||
private lateinit var conversationService: ConversationService
|
private lateinit var conversationService: ConversationService
|
||||||
|
|
||||||
init {
|
init {
|
||||||
emulateConversations = Preferences.isEmulateConversations(ctx)
|
emulateConversations = ctx.preferences.emulateConversations
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun receive(plaintext: Plaintext) {
|
override fun receive(plaintext: Plaintext) {
|
||||||
|
@ -18,7 +18,7 @@ package ch.dissem.apps.abit.service
|
|||||||
|
|
||||||
import android.app.IntentService
|
import android.app.IntentService
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import ch.dissem.apps.abit.util.NetworkUtils
|
import ch.dissem.apps.abit.util.network
|
||||||
import ch.dissem.bitmessage.BitmessageContext
|
import ch.dissem.bitmessage.BitmessageContext
|
||||||
import ch.dissem.bitmessage.entity.Plaintext
|
import ch.dissem.bitmessage.entity.Plaintext
|
||||||
|
|
||||||
@ -44,10 +44,10 @@ class BitmessageIntentService : IntentService("BitmessageIntentService") {
|
|||||||
Singleton.getMessageListener(this).resetNotification()
|
Singleton.getMessageListener(this).resetNotification()
|
||||||
}
|
}
|
||||||
if (it.hasExtra(EXTRA_STARTUP_NODE)) {
|
if (it.hasExtra(EXTRA_STARTUP_NODE)) {
|
||||||
NetworkUtils.enableNode(this)
|
network.enableNode()
|
||||||
}
|
}
|
||||||
if (it.hasExtra(EXTRA_SHUTDOWN_NODE)) {
|
if (it.hasExtra(EXTRA_SHUTDOWN_NODE)) {
|
||||||
NetworkUtils.disableNode(this)
|
network.disableNode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,10 +23,8 @@ import android.content.Intent
|
|||||||
import android.content.IntentFilter
|
import android.content.IntentFilter
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import ch.dissem.apps.abit.R
|
|
||||||
import ch.dissem.apps.abit.notification.ErrorNotification
|
|
||||||
import ch.dissem.apps.abit.notification.NetworkNotification
|
import ch.dissem.apps.abit.notification.NetworkNotification
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
import ch.dissem.bitmessage.BitmessageContext
|
import ch.dissem.bitmessage.BitmessageContext
|
||||||
import ch.dissem.bitmessage.utils.Property
|
import ch.dissem.bitmessage.utils.Property
|
||||||
import org.jetbrains.anko.doAsync
|
import org.jetbrains.anko.doAsync
|
||||||
@ -43,7 +41,7 @@ class BitmessageService : Service() {
|
|||||||
|
|
||||||
private val connectivityReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
private val connectivityReceiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context, intent: Intent?) {
|
override fun onReceive(context: Context, intent: Intent?) {
|
||||||
if (bmc.isRunning() && !Preferences.isConnectionAllowed(this@BitmessageService)) {
|
if (bmc.isRunning() && !preferences.connectionAllowed) {
|
||||||
bmc.shutdown()
|
bmc.shutdown()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@ package ch.dissem.apps.abit.service
|
|||||||
|
|
||||||
import android.app.job.JobParameters
|
import android.app.job.JobParameters
|
||||||
import android.app.job.JobService
|
import android.app.job.JobService
|
||||||
import ch.dissem.apps.abit.util.NetworkUtils
|
import ch.dissem.apps.abit.util.network
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the full node if
|
* Starts the full node if
|
||||||
@ -15,9 +15,8 @@ import ch.dissem.apps.abit.util.Preferences
|
|||||||
class NodeStartupService : JobService() {
|
class NodeStartupService : JobService() {
|
||||||
|
|
||||||
override fun onStartJob(params: JobParameters?): Boolean {
|
override fun onStartJob(params: JobParameters?): Boolean {
|
||||||
val bmc = Singleton.getBitmessageContext(this)
|
if (preferences.online) {
|
||||||
if (Preferences.isOnline(this) && !bmc.isRunning()) {
|
network.scheduleNodeStart()
|
||||||
NetworkUtils.doStartBitmessageService(applicationContext)
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -26,6 +25,9 @@ class NodeStartupService : JobService() {
|
|||||||
* Don't actually stop the service, otherwise it will be stopped after 1 or 10 minutes
|
* Don't actually stop the service, otherwise it will be stopped after 1 or 10 minutes
|
||||||
* depending on Android version.
|
* depending on Android version.
|
||||||
*/
|
*/
|
||||||
override fun onStopJob(params: JobParameters?) = false
|
override fun onStopJob(params: JobParameters?): Boolean {
|
||||||
|
network.scheduleNodeStart()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,16 +4,16 @@ import android.content.BroadcastReceiver
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.Intent.ACTION_BOOT_COMPLETED
|
import android.content.Intent.ACTION_BOOT_COMPLETED
|
||||||
import ch.dissem.apps.abit.util.NetworkUtils
|
import ch.dissem.apps.abit.util.network
|
||||||
import ch.dissem.apps.abit.util.Preferences
|
import ch.dissem.apps.abit.util.preferences
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts the Bitmessage "full node" service if conditions allow it
|
* Starts the Bitmessage "full node" service if conditions allow it
|
||||||
*/
|
*/
|
||||||
class StartServiceReceiver : BroadcastReceiver() {
|
class StartServiceReceiver : BroadcastReceiver() {
|
||||||
override fun onReceive(context: Context, intent: Intent?) {
|
override fun onReceive(context: Context, intent: Intent?) {
|
||||||
if (intent?.action == ACTION_BOOT_COMPLETED && Preferences.isOnline(context)) {
|
if (intent?.action == ACTION_BOOT_COMPLETED && context.preferences.online) {
|
||||||
NetworkUtils.enableNode(context, false)
|
context.network.enableNode(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,14 +10,14 @@ import android.os.Build
|
|||||||
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity
|
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity
|
||||||
import ch.dissem.apps.abit.service.BitmessageService
|
import ch.dissem.apps.abit.service.BitmessageService
|
||||||
import ch.dissem.apps.abit.service.NodeStartupService
|
import ch.dissem.apps.abit.service.NodeStartupService
|
||||||
|
import java.lang.ref.WeakReference
|
||||||
|
|
||||||
|
val Context.network get() = NetworkUtils.getInstance(this)
|
||||||
|
|
||||||
object NetworkUtils {
|
class NetworkUtils internal constructor(private val ctx: Context) {
|
||||||
|
|
||||||
fun enableNode(ctx: Context, ask: Boolean = true) {
|
fun enableNode(ask: Boolean = true) {
|
||||||
if (Preferences.isConnectionAllowed(ctx) || !ask) {
|
if (ask && !ctx.preferences.connectionAllowed) {
|
||||||
scheduleNodeStart(ctx)
|
|
||||||
} else {
|
|
||||||
// Ask for connection
|
// Ask for connection
|
||||||
val dialogIntent = Intent(ctx, FullNodeDialogActivity::class.java)
|
val dialogIntent = Intent(ctx, FullNodeDialogActivity::class.java)
|
||||||
if (ctx !is Activity) {
|
if (ctx !is Activity) {
|
||||||
@ -25,34 +25,42 @@ object NetworkUtils {
|
|||||||
ctx.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
|
ctx.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
|
||||||
}
|
}
|
||||||
ctx.startActivity(dialogIntent)
|
ctx.startActivity(dialogIntent)
|
||||||
|
} else {
|
||||||
|
scheduleNodeStart()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun doStartBitmessageService(ctx: Context) {
|
fun disableNode() {
|
||||||
ctx.startService(Intent(ctx, BitmessageService::class.java))
|
|
||||||
}
|
|
||||||
|
|
||||||
fun disableNode(ctx: Context) {
|
|
||||||
ctx.stopService(Intent(ctx, BitmessageService::class.java))
|
ctx.stopService(Intent(ctx, BitmessageService::class.java))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun scheduleNodeStart(ctx: Context) {
|
fun scheduleNodeStart() {
|
||||||
val jobScheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
val jobScheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
|
||||||
val serviceComponent = ComponentName(ctx, NodeStartupService::class.java)
|
val serviceComponent = ComponentName(ctx, NodeStartupService::class.java)
|
||||||
val builder = JobInfo.Builder(0, serviceComponent)
|
val builder = JobInfo.Builder(0, serviceComponent)
|
||||||
when {
|
when {
|
||||||
Preferences.isWifiOnly(ctx) ->
|
ctx.preferences.wifiOnly ->
|
||||||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
|
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
|
||||||
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ->
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ->
|
||||||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING)
|
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING)
|
||||||
else ->
|
else ->
|
||||||
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
|
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
|
||||||
}
|
}
|
||||||
if (Preferences.requireCharging(ctx)) {
|
builder.setRequiresCharging(ctx.preferences.requireCharging)
|
||||||
builder.setRequiresCharging(true)
|
|
||||||
}
|
|
||||||
builder.setPeriodic(15 * 60 * 1000L)
|
|
||||||
builder.setPersisted(true)
|
builder.setPersisted(true)
|
||||||
jobScheduler.schedule(builder.build())
|
jobScheduler.schedule(builder.build())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private var instance: WeakReference<NetworkUtils>? = null
|
||||||
|
|
||||||
|
internal fun getInstance(ctx: Context): NetworkUtils {
|
||||||
|
var networkUtils = instance?.get()
|
||||||
|
if (networkUtils == null) {
|
||||||
|
networkUtils = NetworkUtils(ctx.applicationContext)
|
||||||
|
instance = WeakReference(networkUtils)
|
||||||
|
}
|
||||||
|
return networkUtils
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,21 +31,25 @@ import org.jetbrains.anko.connectivityManager
|
|||||||
import org.jetbrains.anko.defaultSharedPreferences
|
import org.jetbrains.anko.defaultSharedPreferences
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.lang.ref.WeakReference
|
||||||
|
|
||||||
|
|
||||||
|
val Context.preferences get() = Preferences.getInstance(this)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Christian Basler
|
* @author Christian Basler
|
||||||
*/
|
*/
|
||||||
object Preferences {
|
class Preferences internal constructor(private val ctx: Context) {
|
||||||
private val LOG = LoggerFactory.getLogger(Preferences::class.java)
|
private val LOG = LoggerFactory.getLogger(Preferences::class.java)
|
||||||
|
|
||||||
fun isConnectionAllowed(ctx: Context) = isAllowedForWiFi(ctx) && isAllowedForCharging(ctx)
|
val connectionAllowed get() = isAllowedForWiFi && isAllowedForCharging
|
||||||
|
|
||||||
private fun isAllowedForWiFi(ctx: Context) = !isWifiOnly(ctx) || !ctx.connectivityManager.isActiveNetworkMetered
|
private val isAllowedForWiFi get() = !wifiOnly || !ctx.connectivityManager.isActiveNetworkMetered
|
||||||
|
|
||||||
private fun isAllowedForCharging(ctx: Context) = !requireCharging(ctx) || isCharging(ctx)
|
private val isAllowedForCharging get() = !requireCharging || isCharging
|
||||||
|
|
||||||
private fun isCharging(ctx: Context) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
private val isCharging
|
||||||
|
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
ctx.batteryManager.isCharging
|
ctx.batteryManager.isCharging
|
||||||
} else {
|
} else {
|
||||||
val intent = ctx.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
val intent = ctx.registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED))
|
||||||
@ -53,25 +57,23 @@ object Preferences {
|
|||||||
status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL
|
status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isWifiOnly(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_WIFI_ONLY, true)
|
var wifiOnly
|
||||||
|
get() = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_WIFI_ONLY, true)
|
||||||
fun setWifiOnly(ctx: Context, status: Boolean) {
|
set(value) {
|
||||||
ctx.defaultSharedPreferences.edit()
|
ctx.defaultSharedPreferences.edit()
|
||||||
.putBoolean(PREFERENCE_WIFI_ONLY, status)
|
.putBoolean(PREFERENCE_WIFI_ONLY, value)
|
||||||
.apply()
|
.apply()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requireCharging(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUIRE_CHARGING, true)
|
val requireCharging get() = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUIRE_CHARGING, true)
|
||||||
|
|
||||||
fun isEmulateConversations(ctx: Context) =
|
val emulateConversations get() = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_EMULATE_CONVERSATIONS, true)
|
||||||
ctx.defaultSharedPreferences.getBoolean(PREFERENCE_EMULATE_CONVERSATIONS, true)
|
|
||||||
|
|
||||||
fun getExportDirectory(ctx: Context) = File(ctx.filesDir, "exports")
|
val exportDirectory by lazy { File(ctx.filesDir, "exports") }
|
||||||
|
|
||||||
fun requestAcknowledgements(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUEST_ACK, true)
|
val requestAcknowledgements = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_REQUEST_ACK, true)
|
||||||
|
|
||||||
fun cleanupExportDirectory(ctx: Context) {
|
fun cleanupExportDirectory() {
|
||||||
val exportDirectory = getExportDirectory(ctx)
|
|
||||||
if (exportDirectory.exists()) {
|
if (exportDirectory.exists()) {
|
||||||
exportDirectory.listFiles().forEach { file ->
|
exportDirectory.listFiles().forEach { file ->
|
||||||
try {
|
try {
|
||||||
@ -85,17 +87,29 @@ object Preferences {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun isOnline(ctx: Context) = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_ONLINE, true)
|
var online
|
||||||
|
get() = ctx.defaultSharedPreferences.getBoolean(PREFERENCE_ONLINE, true)
|
||||||
fun setOnline(ctx: Context, status: Boolean) {
|
set(value) {
|
||||||
ctx.defaultSharedPreferences.edit()
|
ctx.defaultSharedPreferences.edit()
|
||||||
.putBoolean(PREFERENCE_ONLINE, status)
|
.putBoolean(PREFERENCE_ONLINE, value)
|
||||||
.apply()
|
.apply()
|
||||||
if (status) {
|
if (value) {
|
||||||
NetworkUtils.enableNode(ctx, true)
|
ctx.network.enableNode(true)
|
||||||
} else {
|
} else {
|
||||||
NetworkUtils.disableNode(ctx)
|
ctx.network.disableNode()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private var instance: WeakReference<Preferences>? = null
|
||||||
|
|
||||||
|
internal fun getInstance(ctx: Context): Preferences {
|
||||||
|
var prefs = instance?.get()
|
||||||
|
if (prefs == null) {
|
||||||
|
prefs = Preferences(ctx.applicationContext)
|
||||||
|
instance = WeakReference(prefs)
|
||||||
|
}
|
||||||
|
return prefs
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlin_version = '1.2.50'
|
ext.kotlin_version = '1.2.51'
|
||||||
ext.anko_version = '0.10.5'
|
ext.anko_version = '0.10.5'
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
jcenter()
|
||||||
|
Loading…
Reference in New Issue
Block a user