Skip to content

Commit

Permalink
Fixes #217 (maybe?)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkourlas committed Sep 27, 2020
1 parent 5aed690 commit 2a82b99
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.6.7 ##

* Bug fixes

## 0.6.6 ##

* Restore pre-0.6.2 font kerning
Expand Down
7 changes: 4 additions & 3 deletions voipms-sms/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId 'net.kourlas.voipms_sms'
minSdkVersion 21
targetSdkVersion 30
versionCode 124
versionName '0.6.6'
versionCode 125
versionName '0.6.7'
}
flavorDimensions 'version', 'demo'
productFlavors {
Expand Down Expand Up @@ -80,9 +80,10 @@ dependencies {
implementation 'androidx.preference:preference:1.1.1'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
implementation 'androidx.work:work-gcm:2.4.0'
implementation 'androidx.work:work-runtime-ktx:2.4.0'
implementation 'androidx.work:work-runtime:2.4.0'
implementation 'com.google.android.material:material:1.3.0-alpha02'
implementation 'com.google.android.material:material:1.2.1'

// Firebase libraries
primaryImplementation 'com.google.firebase:firebase-analytics:17.5.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import androidx.appcompat.app.AppCompatDelegate;
import androidx.work.ExistingWorkPolicy;
import okhttp3.OkHttpClient;

import static net.kourlas.voipms_sms.preferences.PreferencesKt.getAppTheme;
import static net.kourlas.voipms_sms.preferences.fragments.AppearancePreferencesFragment.DARK;
Expand All @@ -51,6 +52,7 @@ public class CustomApplication extends Application {
private int conversationsActivitiesVisible = 0;
private final Map<ConversationId, Integer> conversationActivitiesVisible =
new HashMap<>();
private OkHttpClient okHttpClient;

public static CustomApplication getInstance() {
return instance;
Expand Down Expand Up @@ -93,12 +95,18 @@ public void conversationActivityDecrementCount(
conversationActivitiesVisible.put(conversationId, count - 1);
}

public OkHttpClient getOkHttpClient() {
return okHttpClient;
}

@Override
public void onCreate() {
super.onCreate();

instance = this;

okHttpClient = new OkHttpClient();

// Update theme
String theme = getAppTheme(getApplicationContext());
switch (theme) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,19 @@ import com.squareup.moshi.Json
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
import net.kourlas.voipms_sms.CustomApplication
import net.kourlas.voipms_sms.R
import net.kourlas.voipms_sms.preferences.getDids
import net.kourlas.voipms_sms.preferences.getEmail
import net.kourlas.voipms_sms.preferences.getPassword
import net.kourlas.voipms_sms.preferences.setDids
import net.kourlas.voipms_sms.utils.*
import okhttp3.OkHttpClient
import java.io.IOException

/**
* Service used to retrieve DIDs for a particular account from VoIP.ms.
*/
class RetrieveDidsService : JobIntentService() {
private val okHttp = OkHttpClient()
private val moshi: Moshi = Moshi.Builder().build()
private var error: String? = null

Expand Down Expand Up @@ -119,7 +118,8 @@ class RetrieveDidsService : JobIntentService() {
private fun getApiResponse(): DidsResponse? {
try {
return httpPostWithMultipartFormData(
applicationContext, okHttp, moshi,
applicationContext,
(application as CustomApplication).okHttpClient, moshi,
"https://www.voip.ms/api/v1/rest.php",
mapOf("api_username" to getEmail(applicationContext),
"api_password" to getPassword(applicationContext),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.core.app.RemoteInput
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
import net.kourlas.voipms_sms.CustomApplication
import net.kourlas.voipms_sms.R
import net.kourlas.voipms_sms.network.NetworkManager
import net.kourlas.voipms_sms.notifications.Notifications
Expand All @@ -38,7 +39,6 @@ import net.kourlas.voipms_sms.utils.JobId
import net.kourlas.voipms_sms.utils.httpPostWithMultipartFormData
import net.kourlas.voipms_sms.utils.logException
import net.kourlas.voipms_sms.utils.validatePhoneNumber
import okhttp3.OkHttpClient
import java.io.IOException
import java.text.BreakIterator
import java.util.*
Expand All @@ -48,7 +48,6 @@ import java.util.*
* specified DID with the VoIP.ms API.
*/
class SendMessageService : JobIntentService() {
private val okHttp = OkHttpClient()
private val moshi: Moshi = Moshi.Builder().build()
private var error: String? = null

Expand Down Expand Up @@ -267,7 +266,8 @@ class SendMessageService : JobIntentService() {
val response: MessageResponse?
try {
response = httpPostWithMultipartFormData(
applicationContext, okHttp, moshi,
applicationContext,
(application as CustomApplication).okHttpClient, moshi,
"https://www.voip.ms/api/v1/rest.php",
mapOf("api_username" to getEmail(applicationContext),
"api_password" to getPassword(applicationContext),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ import androidx.core.app.JobIntentService
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
import net.kourlas.voipms_sms.CustomApplication
import net.kourlas.voipms_sms.R
import net.kourlas.voipms_sms.utils.JobId
import net.kourlas.voipms_sms.utils.httpPostWithMultipartFormData
import net.kourlas.voipms_sms.utils.logException
import okhttp3.OkHttpClient
import java.io.IOException

/**
* Service used to test credentials for a particular account from VoIP.ms.
*/
class VerifyCredentialsService : JobIntentService() {
private val okHttp = OkHttpClient()
private val moshi: Moshi = Moshi.Builder().build()
private var error: String? = null

Expand Down Expand Up @@ -100,7 +99,8 @@ class VerifyCredentialsService : JobIntentService() {
password: String): VerifyCredentialsResponse? {
try {
return httpPostWithMultipartFormData(
applicationContext, okHttp, moshi,
applicationContext,
(application as CustomApplication).okHttpClient, moshi,
"https://www.voip.ms/api/v1/rest.php",
mapOf("api_username" to email,
"api_password" to password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.content.Context
import android.content.pm.ServiceInfo
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationManagerCompat
import androidx.work.*
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonDataException
Expand All @@ -36,7 +37,6 @@ import net.kourlas.voipms_sms.utils.httpPostWithMultipartFormData
import net.kourlas.voipms_sms.utils.logException
import net.kourlas.voipms_sms.utils.toBoolean
import net.kourlas.voipms_sms.utils.validatePhoneNumber
import okhttp3.OkHttpClient
import java.io.IOException
import java.net.URLEncoder
import java.text.SimpleDateFormat
Expand All @@ -50,7 +50,6 @@ import kotlin.math.ceil
class SyncWorker(applicationContext: Context,
workerParams: WorkerParameters) : CoroutineWorker(
applicationContext, workerParams) {
private val okHttp = OkHttpClient()
private val moshi: Moshi = Moshi.Builder().build()
private var error: String? = null

Expand All @@ -60,7 +59,7 @@ class SyncWorker(applicationContext: Context,

// Show notification during synchronization to prevent phone from
// going to sleep
showOrUpdateNotification()
showNotification()

// Extract the boolean properties from the input data
val forceRecent = tags.contains(applicationContext.getString(
Expand Down Expand Up @@ -104,11 +103,11 @@ class SyncWorker(applicationContext: Context,
}

/**
* Shows or updates the synchronization notification.
* Shows the synchronization notification for the first time.
*/
private suspend fun showOrUpdateNotification(progress: Int = 0) {
private suspend fun showNotification() {
val notification = Notifications.getInstance(
CustomApplication.getInstance()).getSyncNotification(id, progress)
CustomApplication.getInstance()).getSyncNotification(id, 0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
setForeground(ForegroundInfo(
Notifications.SYNC_NOTIFICATION_ID, notification,
Expand All @@ -119,10 +118,20 @@ class SyncWorker(applicationContext: Context,
}
}

/**
* Updates the synchronization notification.
*/
private fun updateNotification(progress: Int) {
val notification = Notifications.getInstance(
CustomApplication.getInstance()).getSyncNotification(id, progress)
NotificationManagerCompat.from(applicationContext).notify(
Notifications.SYNC_NOTIFICATION_ID, notification)
}

/**
* Perform synchronization.
*/
private suspend fun handleSync(forceRecent: Boolean) {
private fun handleSync(forceRecent: Boolean) {
try {
// Terminate quietly if account inactive
if (!accountConfigured(applicationContext) || !didsConfigured(
Expand Down Expand Up @@ -254,7 +263,7 @@ class SyncWorker(applicationContext: Context,
* @param retrieveDeletedMessages If true, messages are retrieved from
* VoIP.ms even after being deleted locally.
*/
private suspend fun processRequests(
private fun processRequests(
retrievalRequests: List<RetrievalRequest>,
retrieveDeletedMessages: Boolean) {
val incomingMessages = mutableListOf<IncomingMessage>()
Expand All @@ -266,7 +275,7 @@ class SyncWorker(applicationContext: Context,
} else {
return
}
showOrUpdateNotification(((i + 1) * 100) / retrievalRequests.size)
updateNotification(((i + 1) * 100) / retrievalRequests.size)
}

// Add new messages from the server
Expand Down Expand Up @@ -362,7 +371,8 @@ class SyncWorker(applicationContext: Context,
request: RetrievalRequest): MessagesResponse? {
try {
return httpPostWithMultipartFormData(
applicationContext, okHttp, moshi,
applicationContext,
CustomApplication.getInstance().okHttpClient, moshi,
"https://www.voip.ms/api/v1/rest.php",
request.formData)
} catch (e: IOException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ import androidx.core.app.JobIntentService
import com.squareup.moshi.JsonClass
import com.squareup.moshi.JsonDataException
import com.squareup.moshi.Moshi
import net.kourlas.voipms_sms.CustomApplication
import net.kourlas.voipms_sms.R
import net.kourlas.voipms_sms.notifications.Notifications
import net.kourlas.voipms_sms.preferences.*
import net.kourlas.voipms_sms.utils.httpPostWithMultipartFormData
import net.kourlas.voipms_sms.utils.logException
import okhttp3.OkHttpClient
import java.io.IOException

/**
* Service that registers a VoIP.ms callback for each DID.
*/
class NotificationsRegistrationService : JobIntentService() {
private val okHttp = OkHttpClient()
private val moshi: Moshi = Moshi.Builder().build()

override fun onHandleWork(intent: Intent) {
Expand Down Expand Up @@ -93,7 +92,8 @@ class NotificationsRegistrationService : JobIntentService() {
for (did in dids) {
try {
responses[did] = httpPostWithMultipartFormData(
applicationContext, okHttp, moshi,
applicationContext,
(application as CustomApplication).okHttpClient, moshi,
"https://www.voip.ms/api/v1/rest.php",
mapOf("api_username" to getEmail(applicationContext),
"api_password" to getPassword(applicationContext),
Expand Down

0 comments on commit 2a82b99

Please sign in to comment.