Skip to content

Commit 5e6ce8a

Browse files
allinelaraAlline Haiduski
andauthored
feat: Upgrade android-upload-service to 4.3.0
BREAKING CHANGE: The android-upload-service has been upgraded to version 4.3.0. This solves some problems with the event listener 'completed' not being called (randomly). It also requires a minSdkVersion of 21. Written in Kotlin. Closes #162 #181 #129 Co-authored-by: Alline Haiduski <[email protected]>
1 parent 2cc9dcf commit 5e6ce8a

File tree

12 files changed

+493
-559
lines changed

12 files changed

+493
-559
lines changed

android/build.gradle

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
11
buildscript {
2+
ext {
3+
kotlinVersion = '1.3.10'
4+
buildToolsVersion = '29.0.0'
5+
compileSdkVersion = 29
6+
targetSdkVersion = 29
7+
minSdkVersion = 18
8+
}
9+
ext.detoxKotlinVersion = ext.kotlinVersion
10+
211
repositories {
3-
google()
412
jcenter()
13+
google()
514
}
615
dependencies {
7-
classpath 'com.android.tools.build:gradle:3.4.1'
16+
classpath 'com.android.tools.build:gradle:3.5.2'
17+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
818
}
919
}
1020

1121
apply plugin: 'com.android.library'
22+
apply plugin: 'kotlin-android'
23+
apply plugin: 'kotlin-android-extensions'
1224

13-
def DEFAULT_COMPILE_SDK_VERSION = 28
14-
def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
15-
def DEFAULT_TARGET_SDK_VERSION = 28
25+
def DEFAULT_COMPILE_SDK_VERSION = 27
26+
def DEFAULT_BUILD_TOOLS_VERSION = "27.0.3"
27+
def DEFAULT_TARGET_SDK_VERSION = 27
1628

1729
android {
1830
compileSdkVersion project.hasProperty('compileSdkVersion') ? project.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
1931
buildToolsVersion project.hasProperty('buildToolsVersion') ? project.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
2032

2133
defaultConfig {
22-
minSdkVersion 18
34+
minSdkVersion 21
2335
targetSdkVersion project.hasProperty('targetSdkVersion') ? project.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
2436
versionCode 1
2537
versionName "1.0"
@@ -35,7 +47,18 @@ android {
3547
repositories {
3648
mavenCentral()
3749
}
50+
51+
def _ext = ext
52+
53+
def _kotlinVersion = _ext.has('detoxKotlinVersion') ? _ext.detoxKotlinVersion : '1.3.10'
54+
def _kotlinStdlib = _ext.has('detoxKotlinStdlib') ? _ext.detoxKotlinStdlib : 'kotlin-stdlib-jdk8'
55+
3856
dependencies {
39-
compile 'com.facebook.react:react-native:+'
40-
compile 'net.gotev:uploadservice-okhttp:3.4.2'
57+
implementation 'com.facebook.react:react-native:+'
58+
59+
implementation "org.jetbrains.kotlin:$_kotlinStdlib:$_kotlinVersion"
60+
61+
implementation 'net.gotev:uploadservice-okhttp:4.3.0'
62+
63+
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.0.0'
4164
}

android/src/main/java/com/vydia/NotificationActions.java

Lines changed: 0 additions & 31 deletions
This file was deleted.

android/src/main/java/com/vydia/NotificationActionsReceiver.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.vydia.RNUploader
2+
3+
import android.content.Context
4+
import android.util.Log
5+
import com.facebook.react.bridge.Arguments
6+
import com.facebook.react.bridge.ReactApplicationContext
7+
import com.facebook.react.bridge.WritableMap
8+
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
9+
import net.gotev.uploadservice.data.UploadInfo
10+
import net.gotev.uploadservice.network.ServerResponse
11+
import net.gotev.uploadservice.observer.request.RequestObserverDelegate
12+
13+
class GlobalRequestObserverDelegate(reactContext: ReactApplicationContext) : RequestObserverDelegate {
14+
private val TAG = "UploadReceiver"
15+
16+
private var reactContext: ReactApplicationContext = reactContext
17+
18+
override fun onCompleted(context: Context, uploadInfo: UploadInfo) {
19+
}
20+
21+
override fun onCompletedWhileNotObserving() {
22+
}
23+
24+
override fun onError(context: Context, uploadInfo: UploadInfo, exception: Throwable) {
25+
26+
val params = Arguments.createMap()
27+
params.putString("id", uploadInfo.uploadId)
28+
29+
// Make sure we do not try to call getMessage() on a null object
30+
if (exception != null) {
31+
params.putString("error", exception.message)
32+
} else {
33+
params.putString("error", "Unknown exception")
34+
}
35+
36+
sendEvent("error", params, context)
37+
}
38+
39+
override fun onProgress(context: Context, uploadInfo: UploadInfo) {
40+
val params = Arguments.createMap()
41+
params.putString("id", uploadInfo.uploadId)
42+
params.putInt("progress", uploadInfo.progressPercent) //0-100
43+
44+
sendEvent("progress", params, context)
45+
}
46+
47+
override fun onSuccess(context: Context, uploadInfo: UploadInfo, serverResponse: ServerResponse) {
48+
val params = Arguments.createMap()
49+
params.putString("id", uploadInfo.uploadId)
50+
params.putInt("responseCode", serverResponse.code)
51+
params.putString("responseBody", serverResponse.bodyString)
52+
sendEvent("completed", params, context)
53+
}
54+
55+
/**
56+
* Sends an event to the JS module.
57+
*/
58+
private fun sendEvent(eventName: String, params: WritableMap?, context: Context) {
59+
reactContext?.getJSModule(RCTDeviceEventEmitter::class.java)?.emit("RNFileUploader-$eventName", params)
60+
?: Log.e(TAG, "sendEvent() failed due reactContext == null!")
61+
}
62+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.vydia.RNUploader
2+
3+
import android.app.PendingIntent
4+
import android.content.Context
5+
import android.content.Intent
6+
7+
class NotificationActions {
8+
var INTENT_ACTION = "com.vydia.RNUploader.notification.action"
9+
10+
val PARAM_ACTION = "action"
11+
val PARAM_UPLOAD_ID = "uploadId"
12+
13+
val ACTION_CANCEL_UPLOAD = "cancelUpload"
14+
15+
16+
fun getCancelUploadAction(context: Context?,
17+
requestCode: Int,
18+
uploadID: String?): PendingIntent? {
19+
val intent = Intent(INTENT_ACTION)
20+
intent.putExtra(PARAM_ACTION, ACTION_CANCEL_UPLOAD)
21+
intent.putExtra(PARAM_UPLOAD_ID, uploadID)
22+
return PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
23+
}
24+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.vydia.RNUploader
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.util.Log
7+
import com.facebook.react.bridge.Arguments
8+
import com.facebook.react.bridge.ReactApplicationContext
9+
import com.facebook.react.bridge.WritableMap
10+
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
11+
import net.gotev.uploadservice.UploadService
12+
13+
class NotificationActionsReceiver : BroadcastReceiver() {
14+
15+
private val TAG = "NotificationActReceiver"
16+
17+
private val reactContext: ReactApplicationContext? = null
18+
override fun onReceive(context: Context?, intent: Intent?) {
19+
if (intent == null || NotificationActions().INTENT_ACTION == intent.action) {
20+
return
21+
}
22+
23+
if (NotificationActions().ACTION_CANCEL_UPLOAD == intent.getStringExtra(NotificationActions().PARAM_ACTION)) {
24+
onUserRequestedUploadCancellation(context!!, intent.getStringExtra(NotificationActions().PARAM_UPLOAD_ID))
25+
}
26+
}
27+
28+
private fun onUserRequestedUploadCancellation(context: Context, uploadId: String) {
29+
Log.e("CANCEL_UPLOAD", "User requested cancellation of upload with ID: $uploadId")
30+
UploadService.stopUpload(uploadId)
31+
val params = Arguments.createMap()
32+
params.putString("id", uploadId)
33+
sendEvent("cancelled", params, context)
34+
}
35+
36+
/**
37+
* Sends an event to the JS module.
38+
*/
39+
private fun sendEvent(eventName: String, params: WritableMap?, context: Context) {
40+
reactContext?.getJSModule(RCTDeviceEventEmitter::class.java)?.emit("RNFileUploader-$eventName", params)
41+
?: Log.e(TAG, "sendEvent() failed due reactContext == null!")
42+
}
43+
44+
}

0 commit comments

Comments
 (0)