-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
221 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,33 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="ee.forgr.capacitor.uploader"> | ||
|
||
<application> | ||
<service | ||
android:name="net.gotev.uploadservice.UploadService" | ||
android:enabled="true" | ||
android:exported="false" | ||
android:foregroundServiceType="dataSync"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.RUN" /> | ||
</intent-filter> | ||
</service> | ||
|
||
<service | ||
android:name="net.gotev.uploadservice.UploadJobService" | ||
android:enabled="true" | ||
android:exported="false" | ||
android:permission="android.permission.BIND_JOB_SERVICE"> | ||
</service> | ||
|
||
<receiver | ||
android:name="net.gotev.uploadservice.UploadServiceBroadcastReceiver" | ||
android:enabled="true" | ||
android:exported="false"> | ||
<intent-filter> | ||
<action android:name="net.gotev.uploadservice.broadcast.status" /> | ||
</intent-filter> | ||
</receiver> | ||
</application> | ||
|
||
</manifest> |
112 changes: 108 additions & 4 deletions
112
android/src/main/java/ee/forgr/capacitor/uploader/Uploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,115 @@ | ||
package ee.forgr.capacitor.uploader; | ||
|
||
import android.util.Log; | ||
import android.app.Application; | ||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.provider.OpenableColumns; | ||
import android.database.Cursor; | ||
|
||
import net.gotev.uploadservice.UploadServiceConfig; | ||
import net.gotev.uploadservice.data.UploadNotificationConfig; | ||
import net.gotev.uploadservice.data.UploadNotificationStatusConfig; | ||
import net.gotev.uploadservice.protocols.multipart.MultipartUploadRequest; | ||
import net.gotev.uploadservice.observer.request.RequestObserverDelegate; | ||
|
||
import java.util.Map; | ||
|
||
public class Uploader { | ||
|
||
public String echo(String value) { | ||
Log.i("Echo", value); | ||
return value; | ||
private final Context context; | ||
private final RequestObserverDelegate delegate; | ||
|
||
public Uploader(Context context, RequestObserverDelegate delegate) { | ||
this.context = context; | ||
this.delegate = delegate; | ||
initializeUploadService(context); | ||
} | ||
|
||
private void initializeUploadService(Context context) { | ||
Application application = getApplication(context); | ||
if (application != null) { | ||
UploadServiceConfig.initialize(application, "ee.forgr.capacitor.uploader.notification_channel_id", true); | ||
} else { | ||
throw new IllegalStateException("Unable to get Application instance"); | ||
} | ||
} | ||
|
||
private Application getApplication(Context context) { | ||
if (context == null) { | ||
return null; | ||
} else if (context instanceof Application) { | ||
return (Application) context; | ||
} else { | ||
return getApplication(context.getApplicationContext()); | ||
} | ||
} | ||
|
||
public String startUpload(String filePath, String serverUrl, Map<String, String> headers, String notificationTitle) throws Exception { | ||
UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - In Progress"); | ||
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Completed"); | ||
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Error"); | ||
UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(notificationTitle, notificationTitle + " - Cancelled"); | ||
|
||
UploadNotificationConfig notificationConfig = new UploadNotificationConfig( | ||
"ee.forgr.capacitor.uploader.notification_channel_id", | ||
false, | ||
progress, | ||
success, | ||
error, | ||
cancelled | ||
); | ||
|
||
MultipartUploadRequest request = new MultipartUploadRequest(context, serverUrl) | ||
.setMethod("POST") | ||
.addFileToUpload(filePath, "file") | ||
.setNotificationConfig((ctx, uploadId) -> notificationConfig); | ||
|
||
// Add headers | ||
for (Map.Entry<String, String> entry : headers.entrySet()) { | ||
request.addHeader(entry.getKey(), entry.getValue()); | ||
} | ||
|
||
// Set max retries | ||
request.setMaxRetries(2); | ||
|
||
// Set file name if it's a content URI | ||
if (filePath.startsWith("content://")) { | ||
Uri uri = Uri.parse(filePath); | ||
String fileName = getFileNameFromUri(uri); | ||
if (fileName != null) { | ||
request.addParameter("filename", fileName); | ||
} | ||
} | ||
|
||
// Start the upload | ||
return request.startUpload(); | ||
} | ||
|
||
public void removeUpload(String uploadId) { | ||
net.gotev.uploadservice.UploadService.stopUpload(uploadId); | ||
} | ||
|
||
private String getFileNameFromUri(Uri uri) { | ||
String result = null; | ||
if (uri.getScheme().equals("content")) { | ||
try (Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) { | ||
if (cursor != null && cursor.moveToFirst()) { | ||
int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); | ||
if (index != -1) { | ||
result = cursor.getString(index); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
if (result == null) { | ||
result = uri.getPath(); | ||
int cut = result.lastIndexOf('/'); | ||
if (cut != -1) { | ||
result = result.substring(cut + 1); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters