fedilab-Android-App/app/src/main/java/app/fedilab/android/services/PeertubeUploadReceiver.java

81 lines
2.9 KiB
Java
Raw Normal View History

2019-06-07 15:18:09 +02:00
package app.fedilab.android.services;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program 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.
*
* Fedilab 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 Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
2019-09-06 17:55:14 +02:00
2019-06-07 15:18:09 +02:00
import android.content.Context;
2019-07-24 18:54:57 +02:00
import android.content.Intent;
2019-06-07 15:18:09 +02:00
import android.content.SharedPreferences;
2019-06-27 18:08:07 +02:00
2019-07-24 18:54:57 +02:00
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
2019-06-07 15:18:09 +02:00
import net.gotev.uploadservice.ServerResponse;
import net.gotev.uploadservice.UploadInfo;
import net.gotev.uploadservice.UploadServiceBroadcastReceiver;
import org.json.JSONException;
import org.json.JSONObject;
2019-07-24 18:54:57 +02:00
import java.util.ArrayList;
2019-06-07 15:18:09 +02:00
import app.fedilab.android.helper.Helper;
/**
* Created by Thomas on 06/06/2018
* BroadcastReceiver when Peertube videos have uploaded
*/
public class PeertubeUploadReceiver extends UploadServiceBroadcastReceiver {
@Override
public void onProgress(Context context, UploadInfo uploadInfo) {
// your code here
}
@Override
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse,
Exception exception) {
// your code here
}
@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
try {
JSONObject response = new JSONObject(serverResponse.getBodyAsString());
2019-07-24 18:54:57 +02:00
2019-09-06 17:55:14 +02:00
if (!response.has("video")) { //IT's not from Peertube
2019-07-24 18:54:57 +02:00
ArrayList<String> file = uploadInfo.getSuccessfullyUploadedFiles();
Intent addMedia = new Intent(Helper.INTENT_ADD_UPLOADED_MEDIA);
addMedia.putExtra("response", serverResponse.getBodyAsString());
addMedia.putStringArrayListExtra("uploadInfo", file);
LocalBroadcastManager.getInstance(context).sendBroadcast(addMedia);
2019-09-06 17:55:14 +02:00
} else {
2019-07-24 18:54:57 +02:00
String videoID = response.getJSONObject("video").get("id").toString();
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.VIDEO_ID, videoID);
editor.commit();
}
2019-06-07 15:18:09 +02:00
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
// your code here
}
}