fedilab-Android-App/app/src/main/java/app/fedilab/android/activities/PeertubeUploadActivity.java

413 lines
18 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.activities;
2019-01-07 15:27:06 +01:00
/* Copyright 2019 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-01-07 15:27:06 +01:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-01-07 15:27:06 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-01-07 15:27:06 +01:00
* see <http://www.gnu.org/licenses>. */
2019-01-07 16:44:19 +01:00
import android.Manifest;
2020-05-17 11:49:02 +02:00
import android.annotation.SuppressLint;
2019-01-07 15:27:06 +01:00
import android.app.Activity;
2019-01-09 15:27:02 +01:00
import android.app.PendingIntent;
2019-01-07 15:27:06 +01:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2019-01-07 16:44:19 +01:00
import android.content.pm.PackageManager;
import android.database.Cursor;
2019-11-13 12:54:01 +01:00
import android.graphics.drawable.ColorDrawable;
2019-01-07 16:44:19 +01:00
import android.net.Uri;
import android.os.AsyncTask;
2019-01-07 15:27:06 +01:00
import android.os.Bundle;
2019-01-07 16:44:19 +01:00
import android.provider.OpenableColumns;
2019-01-07 15:27:06 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-11-20 11:57:25 +01:00
import android.widget.AdapterView;
2019-01-07 16:44:19 +01:00
import android.widget.ArrayAdapter;
import android.widget.Button;
2019-10-31 11:56:15 +01:00
import android.widget.EditText;
2019-01-07 15:27:06 +01:00
import android.widget.ImageView;
2019-08-18 17:41:10 +02:00
import android.widget.LinearLayout;
2019-11-20 11:57:25 +01:00
import android.widget.Spinner;
2019-01-07 15:27:06 +01:00
import android.widget.TextView;
2019-01-07 16:44:19 +01:00
import android.widget.Toast;
2019-01-07 15:27:06 +01:00
2019-11-15 16:32:25 +01:00
import androidx.appcompat.app.ActionBar;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
2019-01-09 15:27:02 +01:00
import net.gotev.uploadservice.MultipartUploadRequest;
import net.gotev.uploadservice.ServerResponse;
import net.gotev.uploadservice.UploadInfo;
2019-01-09 16:04:14 +01:00
import net.gotev.uploadservice.UploadNotificationAction;
2019-01-09 15:27:02 +01:00
import net.gotev.uploadservice.UploadNotificationConfig;
2019-06-06 15:47:38 +02:00
import net.gotev.uploadservice.UploadServiceSingleBroadcastReceiver;
2019-01-09 15:27:02 +01:00
import net.gotev.uploadservice.UploadStatusDelegate;
import org.json.JSONException;
import org.json.JSONObject;
2019-01-07 16:44:19 +01:00
import java.io.File;
2019-11-24 18:17:22 +01:00
import java.util.Date;
2019-01-07 16:44:19 +01:00
import java.util.HashMap;
2019-01-09 15:27:02 +01:00
import java.util.Iterator;
import java.util.LinkedHashMap;
2019-01-07 16:44:19 +01:00
import java.util.List;
2019-01-09 15:27:02 +01:00
import java.util.Map;
2019-06-06 15:47:38 +02:00
import java.util.UUID;
2019-01-07 16:44:19 +01:00
2019-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrievePeertubeChannelsAsyncTask;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.interfaces.OnRetrievePeertubeInterface;
2019-11-15 16:32:25 +01:00
import es.dmoral.toasty.Toasty;
2019-01-07 15:27:06 +01:00
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.asynctasks.RetrievePeertubeInformationAsyncTask.peertubeInformation;
2019-01-07 15:27:06 +01:00
2019-06-06 15:47:38 +02:00
public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePeertubeInterface, UploadStatusDelegate {
2019-01-07 15:27:06 +01:00
2019-01-07 16:44:19 +01:00
private final int PICK_IVDEO = 52378;
2019-11-15 16:32:25 +01:00
private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 724;
2019-01-07 16:44:19 +01:00
private Button set_upload_file, set_upload_submit;
2019-11-20 11:57:25 +01:00
private Spinner set_upload_privacy, set_upload_channel;
2019-01-07 16:44:19 +01:00
private TextView set_upload_file_name;
2019-10-31 11:56:15 +01:00
private EditText video_title;
2019-01-07 16:44:19 +01:00
private HashMap<String, String> channels;
2019-01-09 15:27:02 +01:00
private Uri uri;
private String filename;
private HashMap<Integer, String> privacyToSend;
private HashMap<String, String> channelToSend;
2019-06-06 15:47:38 +02:00
private UploadServiceSingleBroadcastReceiver uploadReceiver;
2019-01-07 15:27:06 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
2019-09-06 17:55:14 +02:00
switch (theme) {
2019-01-07 15:27:06 +01:00
case Helper.THEME_LIGHT:
2019-11-09 15:47:38 +01:00
setTheme(R.style.AppTheme_Fedilab);
2019-01-07 15:27:06 +01:00
break;
case Helper.THEME_BLACK:
setTheme(R.style.AppThemeBlack);
break;
default:
setTheme(R.style.AppThemeDark);
}
2019-09-06 17:55:14 +02:00
if (getSupportActionBar() != null)
2019-01-07 15:27:06 +01:00
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ActionBar actionBar = getSupportActionBar();
2019-09-06 17:55:14 +02:00
if (actionBar != null) {
2019-01-07 15:27:06 +01:00
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
assert inflater != null;
2020-04-08 12:42:15 +02:00
View view = inflater.inflate(R.layout.simple_bar, new LinearLayout(PeertubeUploadActivity.this), false);
2019-11-15 19:36:39 +01:00
view.setBackground(new ColorDrawable(ContextCompat.getColor(PeertubeUploadActivity.this, R.color.cyanea_primary)));
2019-01-07 15:27:06 +01:00
actionBar.setCustomView(view, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
ImageView toolbar_close = actionBar.getCustomView().findViewById(R.id.toolbar_close);
TextView toolbar_title = actionBar.getCustomView().findViewById(R.id.toolbar_title);
2020-04-08 13:01:28 +02:00
toolbar_close.setOnClickListener(v -> finish());
2019-01-08 10:50:43 +01:00
toolbar_title.setText(R.string.upload_video);
2019-01-07 15:27:06 +01:00
}
setContentView(R.layout.activity_peertube_upload);
2019-01-07 16:44:19 +01:00
set_upload_file = findViewById(R.id.set_upload_file);
set_upload_file_name = findViewById(R.id.set_upload_file_name);
set_upload_channel = findViewById(R.id.set_upload_channel);
set_upload_privacy = findViewById(R.id.set_upload_privacy);
set_upload_submit = findViewById(R.id.set_upload_submit);
2019-10-31 11:56:15 +01:00
video_title = findViewById(R.id.video_title);
2019-01-07 15:27:06 +01:00
2019-01-07 16:44:19 +01:00
new RetrievePeertubeChannelsAsyncTask(PeertubeUploadActivity.this, PeertubeUploadActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
channels = new HashMap<>();
2019-01-09 15:27:02 +01:00
2019-06-06 15:47:38 +02:00
uploadReceiver = new UploadServiceSingleBroadcastReceiver(this);
uploadReceiver.register(this);
2019-01-09 15:27:02 +01:00
2019-01-07 15:27:06 +01:00
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IVDEO && resultCode == Activity.RESULT_OK) {
2019-01-07 16:44:19 +01:00
if (data == null || data.getData() == null) {
2020-04-08 12:42:15 +02:00
Toasty.error(PeertubeUploadActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
2019-01-07 16:44:19 +01:00
return;
}
set_upload_submit.setEnabled(true);
2019-01-09 15:27:02 +01:00
uri = data.getData();
2019-01-07 16:44:19 +01:00
String uriString = uri.toString();
File myFile = new File(uriString);
2019-01-09 15:27:02 +01:00
filename = null;
2019-01-07 16:44:19 +01:00
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
assert cursor != null;
cursor.close();
}
} else if (uriString.startsWith("file://")) {
filename = myFile.getName();
}
2020-03-08 10:29:06 +01:00
if (filename == null) {
2019-11-24 18:17:22 +01:00
filename = new Date().toString();
2019-01-07 16:44:19 +01:00
}
2019-11-24 18:17:22 +01:00
set_upload_file_name.setVisibility(View.VISIBLE);
set_upload_file_name.setText(filename);
2019-01-07 16:44:19 +01:00
}
}
2019-06-06 15:47:38 +02:00
@Override
public void onDestroy() {
super.onDestroy();
uploadReceiver.unregister(this);
}
2019-01-07 16:44:19 +01:00
@Override
public void onRetrievePeertube(APIResponse apiResponse) {
2019-01-07 19:16:15 +01:00
}
@Override
public void onRetrievePeertubeComments(APIResponse apiResponse) {
}
2020-05-17 11:49:02 +02:00
@SuppressWarnings("SuspiciousMethodCalls")
2019-01-07 19:16:15 +01:00
@Override
public void onRetrievePeertubeChannels(APIResponse apiResponse) {
2019-09-06 17:55:14 +02:00
if (apiResponse.getError() != null || apiResponse.getAccounts() == null || apiResponse.getAccounts().size() == 0) {
if (apiResponse.getError() != null && apiResponse.getError().getError() != null)
2020-04-08 12:42:15 +02:00
Toasty.error(PeertubeUploadActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-01-07 16:44:19 +01:00
else
2020-04-08 12:42:15 +02:00
Toasty.error(PeertubeUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
2019-01-07 16:44:19 +01:00
return;
}
2019-01-07 15:27:06 +01:00
2019-01-07 16:44:19 +01:00
//Populate channels
List<Account> accounts = apiResponse.getAccounts();
String[] channelName = new String[accounts.size()];
2019-09-06 17:55:14 +02:00
String[] channelId = new String[accounts.size()];
2019-01-07 16:44:19 +01:00
int i = 0;
2019-09-06 17:55:14 +02:00
for (Account account : accounts) {
channels.put(account.getUsername(), account.getId());
2019-01-07 16:44:19 +01:00
channelName[i] = account.getUsername();
2019-01-09 15:27:02 +01:00
channelId[i] = account.getId();
2019-01-07 16:44:19 +01:00
i++;
2019-01-07 15:27:06 +01:00
}
2019-01-09 15:27:02 +01:00
channelToSend = new HashMap<>();
channelToSend.put(channelName[0], channelId[0]);
2019-01-07 16:44:19 +01:00
ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(PeertubeUploadActivity.this,
android.R.layout.simple_spinner_dropdown_item, channelName);
set_upload_channel.setAdapter(adapterChannel);
2019-11-15 16:32:25 +01:00
if (peertubeInformation == null) {
2019-10-31 11:56:15 +01:00
return;
}
2019-01-09 15:27:02 +01:00
LinkedHashMap<String, String> translations = null;
2019-11-15 16:32:25 +01:00
if (peertubeInformation.getTranslations() != null)
2019-01-09 15:27:02 +01:00
translations = new LinkedHashMap<>(peertubeInformation.getTranslations());
LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPrivacies());
2019-09-06 17:55:14 +02:00
Map.Entry<Integer, String> entryInt = privaciesInit.entrySet().iterator().next();
2019-01-09 15:27:02 +01:00
privacyToSend = new HashMap<>();
privacyToSend.put(entryInt.getKey(), entryInt.getValue());
LinkedHashMap<Integer, String> privacies = new LinkedHashMap<>(peertubeInformation.getPrivacies());
//Populate privacies
String[] privaciesA = new String[privacies.size()];
Iterator it = privacies.entrySet().iterator();
i = 0;
while (it.hasNext()) {
2019-09-06 17:55:14 +02:00
Map.Entry pair = (Map.Entry) it.next();
2019-11-15 16:32:25 +01:00
if (translations == null || translations.size() == 0 || !translations.containsKey(pair.getValue()))
2019-09-06 17:55:14 +02:00
privaciesA[i] = (String) pair.getValue();
2019-01-09 15:27:02 +01:00
else
2019-10-31 11:56:15 +01:00
privaciesA[i] = translations.get(pair.getValue());
2019-01-09 15:27:02 +01:00
it.remove();
i++;
}
ArrayAdapter<String> adapterPrivacies = new ArrayAdapter<>(PeertubeUploadActivity.this,
android.R.layout.simple_spinner_dropdown_item, privaciesA);
set_upload_privacy.setAdapter(adapterPrivacies);
//Manage privacies
2019-11-20 11:57:25 +01:00
set_upload_privacy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2019-01-09 15:27:02 +01:00
@Override
2019-11-20 11:57:25 +01:00
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
2019-01-09 15:27:02 +01:00
LinkedHashMap<Integer, String> privaciesCheck = new LinkedHashMap<>(peertubeInformation.getPrivacies());
Iterator it = privaciesCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
2019-09-06 17:55:14 +02:00
Map.Entry pair = (Map.Entry) it.next();
if (i == position) {
2019-01-09 15:27:02 +01:00
privacyToSend = new HashMap<>();
2019-09-06 17:55:14 +02:00
privacyToSend.put((Integer) pair.getKey(), (String) pair.getValue());
2019-01-09 15:27:02 +01:00
break;
}
it.remove();
i++;
}
}
2019-01-07 16:44:19 +01:00
2019-11-20 11:57:25 +01:00
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
2019-01-07 16:44:19 +01:00
set_upload_file.setEnabled(true);
2020-05-17 11:49:02 +02:00
set_upload_file.setOnClickListener(v -> {
if (ContextCompat.checkSelfPermission(PeertubeUploadActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(PeertubeUploadActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
return;
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
intent.setType("*/*");
String[] mimetypes = {"video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, PICK_IVDEO);
} else {
intent.setType("video/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent chooserIntent = Intent.createChooser(intent, getString(R.string.toot_select_image));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooserIntent, PICK_IVDEO);
2019-01-07 16:44:19 +01:00
}
2020-05-17 11:49:02 +02:00
2019-01-07 16:44:19 +01:00
});
2019-01-09 15:27:02 +01:00
//Manage languages
2019-11-20 11:57:25 +01:00
set_upload_channel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2019-01-09 15:27:02 +01:00
@Override
2019-11-20 11:57:25 +01:00
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
2019-01-09 15:27:02 +01:00
LinkedHashMap<String, String> channelsCheck = new LinkedHashMap<>(channels);
Iterator it = channelsCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
2019-09-06 17:55:14 +02:00
Map.Entry pair = (Map.Entry) it.next();
if (i == position) {
2019-01-09 15:27:02 +01:00
channelToSend = new HashMap<>();
2019-09-06 17:55:14 +02:00
channelToSend.put((String) pair.getKey(), (String) pair.getValue());
2019-01-09 15:27:02 +01:00
break;
}
it.remove();
i++;
}
}
2019-11-20 11:57:25 +01:00
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
2019-01-09 15:27:02 +01:00
});
2020-04-08 13:01:28 +02:00
set_upload_submit.setOnClickListener(v -> {
if (uri != null) {
Map.Entry<String, String> channelM = channelToSend.entrySet().iterator().next();
String idChannel = channelM.getValue();
Map.Entry<Integer, String> privacyM = privacyToSend.entrySet().iterator().next();
Integer idPrivacy = privacyM.getKey();
try {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
UploadNotificationConfig uploadConfig = new UploadNotificationConfig();
Intent in = new Intent(PeertubeUploadActivity.this, PeertubeEditUploadActivity.class);
PendingIntent clickIntent = PendingIntent.getActivity(PeertubeUploadActivity.this, 1, in, PendingIntent.FLAG_UPDATE_CURRENT);
uploadConfig
.setClearOnActionForAllStatuses(true);
uploadConfig.getProgress().message = getString(R.string.uploading);
uploadConfig.getCompleted().message = getString(R.string.upload_video_success);
uploadConfig.getError().message = getString(R.string.toast_error);
uploadConfig.getCancelled().message = getString(R.string.toast_cancelled);
uploadConfig.getCompleted().actions.add(new UploadNotificationAction(R.drawable.ic_check, getString(R.string.video_uploaded_action), clickIntent));
if (video_title != null && video_title.getText() != null && video_title.getText().toString().trim().length() > 0) {
filename = video_title.getText().toString().trim();
2019-01-09 15:27:02 +01:00
}
2020-04-08 13:01:28 +02:00
String uploadId = UUID.randomUUID().toString();
uploadReceiver.setUploadID(uploadId);
new MultipartUploadRequest(PeertubeUploadActivity.this, uploadId, "https://" + Helper.getLiveInstance(PeertubeUploadActivity.this) + "/api/v1/videos/upload")
.addFileToUpload(uri.toString().replace("file://", ""), "videofile")
.addHeader("Authorization", "Bearer " + token)
.setNotificationConfig(uploadConfig)
.addParameter("name", filename)
.addParameter("channelId", idChannel)
.addParameter("privacy", String.valueOf(idPrivacy))
.addParameter("nsfw", "false")
.addParameter("commentsEnabled", "true")
.addParameter("waitTranscoding", "true")
.setMaxRetries(2)
.startUpload();
finish();
} catch (Exception exc) {
exc.printStackTrace();
2019-01-09 15:27:02 +01:00
}
}
});
2019-01-07 16:44:19 +01:00
}
2019-06-06 15:47:38 +02:00
@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
exception.printStackTrace();
}
2020-05-17 11:49:02 +02:00
@SuppressLint("ApplySharedPref")
2019-06-06 15:47:38 +02:00
@Override
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
try {
JSONObject response = new JSONObject(serverResponse.getBodyAsString());
String videoID = response.getJSONObject("video").get("id").toString();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.VIDEO_ID, videoID);
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onCancelled(Context context, UploadInfo uploadInfo) {
// your code here
}
2019-01-07 15:27:06 +01:00
}