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

425 lines
19 KiB
Java
Raw Normal View History

2022-05-05 08:53:25 +02:00
package app.fedilab.fedilabtube.activities;
2020-07-01 16:36:08 +02:00
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* 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.
*
* TubeLab 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 TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-06-26 08:50:49 +02:00
2022-05-05 08:53:25 +02:00
import static app.fedilab.fedilabtube.activities.MainActivity.userMe;
2022-05-04 09:43:54 +02:00
import static app.fedilab.fedilabtube.client.RetrofitPeertubeAPI.DataType.MY_CHANNELS;
import static app.fedilab.fedilabtube.helper.Helper.peertubeInformation;
2020-06-26 08:50:49 +02:00
import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
2020-11-20 18:48:29 +01:00
import android.graphics.Color;
2020-06-26 08:50:49 +02:00
import android.net.Uri;
2020-11-24 17:32:18 +01:00
import android.os.Build;
2020-06-26 08:50:49 +02:00
import android.os.Bundle;
2020-06-30 12:00:55 +02:00
import android.view.MenuItem;
2020-06-26 08:50:49 +02:00
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
2020-11-20 18:48:29 +01:00
import androidx.documentfile.provider.DocumentFile;
2020-09-08 10:11:11 +02:00
import androidx.lifecycle.ViewModelProvider;
2020-06-26 08:50:49 +02:00
2020-11-20 18:48:29 +01:00
import net.gotev.uploadservice.data.UploadNotificationAction;
import net.gotev.uploadservice.data.UploadNotificationConfig;
import net.gotev.uploadservice.data.UploadNotificationStatusConfig;
import net.gotev.uploadservice.extensions.ContextExtensionsKt;
import net.gotev.uploadservice.protocols.multipart.MultipartUploadRequest;
import java.util.ArrayList;
2020-06-26 08:50:49 +02:00
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
2020-11-24 17:32:18 +01:00
import java.util.Locale;
2020-06-26 08:50:49 +02:00
import java.util.Map;
2022-05-05 08:53:25 +02:00
import app.fedilab.fedilabtube.BaseFedilabTube;
import app.fedilab.fedilabtube.BuildConfig;
import app.fedilab.fedilabtube.FedilabTube;
import app.fedilab.fedilabtube.R;
2020-06-26 08:50:49 +02:00
import app.fedilab.fedilabtube.client.APIResponse;
2020-11-24 17:32:18 +01:00
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
2020-09-29 17:48:12 +02:00
import app.fedilab.fedilabtube.client.data.ChannelData;
2020-11-24 17:32:18 +01:00
import app.fedilab.fedilabtube.client.entities.UserMe;
2020-11-21 09:37:18 +01:00
import app.fedilab.fedilabtube.databinding.ActivityPeertubeUploadBinding;
2020-06-26 08:50:49 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2022-05-09 10:25:07 +02:00
import app.fedilab.fedilabtube.helper.HelperAcadInstance;
2020-12-01 18:35:54 +01:00
import app.fedilab.fedilabtube.helper.HelperInstance;
2020-09-08 10:11:11 +02:00
import app.fedilab.fedilabtube.viewmodel.ChannelsVM;
2020-06-26 14:34:39 +02:00
import es.dmoral.toasty.Toasty;
2020-06-26 08:50:49 +02:00
2022-05-05 08:53:25 +02:00
public class PeertubeUploadActivity extends BaseActivity {
2020-06-26 08:50:49 +02:00
public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 724;
2020-11-11 10:34:28 +01:00
private final int PICK_IVDEO = 52378;
2020-06-26 08:50:49 +02:00
private HashMap<String, String> channels;
private Uri uri;
private String filename;
private HashMap<Integer, String> privacyToSend;
private HashMap<String, String> channelToSend;
2020-11-21 09:37:18 +01:00
private ActivityPeertubeUploadBinding binding;
2020-06-26 08:50:49 +02:00
2022-05-09 10:25:07 +02:00
2020-06-26 08:50:49 +02:00
@Override
protected void onCreate(Bundle savedInstanceState) {
2022-05-09 10:25:07 +02:00
HelperInstance.setTheme(this, HelperInstance.getLiveInstance(this),false);
2020-06-26 08:50:49 +02:00
super.onCreate(savedInstanceState);
if (getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
2020-06-30 12:00:55 +02:00
2020-11-21 09:37:18 +01:00
binding = ActivityPeertubeUploadBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
2020-06-26 08:50:49 +02:00
2020-11-24 17:32:18 +01:00
new Thread(() -> {
UserMe.VideoQuota videoQuotaReply = new RetrofitPeertubeAPI(PeertubeUploadActivity.this).getVideoQuota();
runOnUiThread(() -> {
if (videoQuotaReply != null) {
long videoQuota = videoQuotaReply.getVideoQuotaUsed();
long dailyQuota = videoQuotaReply.getVideoQuotaUsedDaily();
long instanceVideoQuota = userMe.getVideoQuota();
long instanceDailyQuota = userMe.getVideoQuotaDaily();
2020-12-06 17:15:04 +01:00
if (instanceVideoQuota != -1 && instanceVideoQuota != 0) {
2020-11-24 17:32:18 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.totalQuota.setProgress((int) (videoQuota * 100 / instanceVideoQuota), true);
} else {
binding.totalQuota.setProgress((int) (videoQuota * 100 / instanceVideoQuota));
}
} else {
int progress = videoQuota > 0 ? 30 : 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.totalQuota.setProgress(progress, true);
} else {
binding.totalQuota.setProgress(progress);
}
}
2020-12-06 17:15:04 +01:00
if (instanceDailyQuota != -1 && instanceDailyQuota != 0) {
2020-11-24 17:32:18 +01:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.dailyQuota.setProgress((int) (dailyQuota * 100 / instanceDailyQuota), true);
} else {
binding.dailyQuota.setProgress((int) (dailyQuota * 100 / instanceDailyQuota));
}
} else {
int progress = dailyQuota > 0 ? 30 : 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
binding.dailyQuota.setProgress(progress, true);
} else {
binding.dailyQuota.setProgress(progress);
}
}
binding.totalQuotaValue.setText(
String.format(Locale.getDefault(), "%s/%s",
Helper.returnRoundedSize(PeertubeUploadActivity.this, videoQuota),
Helper.returnRoundedSize(PeertubeUploadActivity.this, instanceVideoQuota)));
binding.dailyQuotaValue.setText(
String.format(Locale.getDefault(), "%s/%s",
Helper.returnRoundedSize(PeertubeUploadActivity.this, dailyQuota),
Helper.returnRoundedSize(PeertubeUploadActivity.this, instanceDailyQuota)));
}
});
}).start();
2020-09-08 10:11:11 +02:00
ChannelsVM viewModelC = new ViewModelProvider(PeertubeUploadActivity.this).get(ChannelsVM.class);
2020-09-26 10:22:11 +02:00
viewModelC.get(MY_CHANNELS, null).observe(PeertubeUploadActivity.this, this::manageVIewChannels);
2020-06-26 08:50:49 +02:00
channels = new HashMap<>();
2020-09-03 19:08:53 +02:00
setTitle(R.string.upload_video);
2020-06-26 08:50:49 +02:00
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IVDEO && resultCode == Activity.RESULT_OK) {
if (data == null || data.getData() == null) {
Toasty.error(PeertubeUploadActivity.this, getString(R.string.toot_select_image_error), Toast.LENGTH_LONG).show();
return;
}
2020-11-21 09:37:18 +01:00
binding.setUploadSubmit.setEnabled(true);
2020-06-26 08:50:49 +02:00
uri = data.getData();
filename = null;
2020-11-20 18:48:29 +01:00
DocumentFile documentFile = DocumentFile.fromSingleUri(this, uri);
if (documentFile != null) {
filename = documentFile.getName();
2020-06-26 08:50:49 +02:00
}
if (filename == null) {
filename = new Date().toString();
}
2020-11-21 09:37:18 +01:00
binding.setUploadFileName.setVisibility(View.VISIBLE);
binding.setUploadFileName.setText(filename);
2020-06-26 08:50:49 +02:00
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
2020-06-30 12:00:55 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
2020-09-08 10:11:11 +02:00
public void manageVIewChannels(APIResponse apiResponse) {
2020-09-29 17:48:12 +02:00
if (apiResponse.getError() != null || apiResponse.getChannels() == null || apiResponse.getChannels().size() == 0) {
2020-06-26 08:50:49 +02:00
if (apiResponse.getError() != null && apiResponse.getError().getError() != null)
Toasty.error(PeertubeUploadActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
else
Toasty.error(PeertubeUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show();
return;
}
//Populate channels
2020-09-29 17:48:12 +02:00
List<ChannelData.Channel> channelsForUser = apiResponse.getChannels();
String[] channelName = new String[channelsForUser.size()];
String[] channelId = new String[channelsForUser.size()];
2020-06-26 08:50:49 +02:00
int i = 0;
2020-09-29 17:48:12 +02:00
for (ChannelData.Channel channel : channelsForUser) {
channels.put(channel.getName(), channel.getId());
channelName[i] = channel.getName();
channelId[i] = channel.getId();
2020-06-26 08:50:49 +02:00
i++;
}
channelToSend = new HashMap<>();
channelToSend.put(channelName[0], channelId[0]);
ArrayAdapter<String> adapterChannel = new ArrayAdapter<>(PeertubeUploadActivity.this,
android.R.layout.simple_spinner_dropdown_item, channelName);
2020-11-21 09:37:18 +01:00
binding.setUploadChannel.setAdapter(adapterChannel);
2020-06-26 08:50:49 +02:00
if (peertubeInformation == null) {
return;
}
LinkedHashMap<String, String> translations = null;
if (peertubeInformation.getTranslations() != null)
translations = new LinkedHashMap<>(peertubeInformation.getTranslations());
LinkedHashMap<Integer, String> privaciesInit = new LinkedHashMap<>(peertubeInformation.getPrivacies());
Map.Entry<Integer, String> entryInt = privaciesInit.entrySet().iterator().next();
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<Map.Entry<Integer, String>> it = privacies.entrySet().iterator();
i = 0;
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
if (translations == null || translations.size() == 0 || !translations.containsKey(pair.getValue()))
privaciesA[i] = pair.getValue();
else
privaciesA[i] = translations.get(pair.getValue());
it.remove();
i++;
}
ArrayAdapter<String> adapterPrivacies = new ArrayAdapter<>(PeertubeUploadActivity.this,
android.R.layout.simple_spinner_dropdown_item, privaciesA);
2020-11-21 09:37:18 +01:00
binding.setUploadPrivacy.setAdapter(adapterPrivacies);
2020-06-26 08:50:49 +02:00
//Manage privacies
2020-11-21 09:37:18 +01:00
binding.setUploadPrivacy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2020-06-26 08:50:49 +02:00
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LinkedHashMap<Integer, String> privaciesCheck = new LinkedHashMap<>(peertubeInformation.getPrivacies());
Iterator<Map.Entry<Integer, String>> it = privaciesCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<Integer, String> pair = it.next();
if (i == position) {
privacyToSend = new HashMap<>();
privacyToSend.put(pair.getKey(), pair.getValue());
break;
}
it.remove();
i++;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
2020-11-21 09:37:18 +01:00
binding.setUploadFile.setEnabled(true);
2020-06-26 08:50:49 +02:00
2020-11-21 09:37:18 +01:00
binding.setUploadFile.setOnClickListener(v -> {
2020-06-26 08:50:49 +02:00
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;
}
2020-11-20 18:48:29 +01:00
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
2020-06-26 08:50:49 +02:00
intent.addCategory(Intent.CATEGORY_OPENABLE);
2020-06-30 12:00:55 +02:00
intent.setType("*/*");
String[] mimetypes = {"video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, PICK_IVDEO);
2020-06-26 08:50:49 +02:00
});
//Manage languages
2020-11-21 09:37:18 +01:00
binding.setUploadChannel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
2020-06-26 08:50:49 +02:00
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
LinkedHashMap<String, String> channelsCheck = new LinkedHashMap<>(channels);
Iterator<Map.Entry<String, String>> it = channelsCheck.entrySet().iterator();
int i = 0;
while (it.hasNext()) {
Map.Entry<String, String> pair = it.next();
if (i == position) {
channelToSend = new HashMap<>();
channelToSend.put(pair.getKey(), pair.getValue());
break;
}
it.remove();
i++;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
2020-11-21 09:37:18 +01:00
binding.setUploadSubmit.setOnClickListener(v -> {
2020-06-26 08:50:49 +02:00
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();
2020-11-21 11:06:26 +01:00
if (binding.videoTitle.getText() != null && binding.videoTitle.getText().toString().trim().length() > 0) {
filename = binding.videoTitle.getText().toString().trim();
}
2020-06-26 08:50:49 +02:00
try {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String token = Helper.getToken(PeertubeUploadActivity.this);
2020-11-21 09:37:18 +01:00
2020-12-01 18:35:54 +01:00
new MultipartUploadRequest(PeertubeUploadActivity.this, "https://" + HelperInstance.getLiveInstance(PeertubeUploadActivity.this) + "/api/v1/videos/upload")
2020-11-20 18:48:29 +01:00
.setMethod("POST")
.setBearerAuth(token)
.addHeader("User-Agent", getString(R.string.app_name) + "/" + BuildConfig.VERSION_NAME)
2020-06-26 08:50:49 +02:00
.addParameter("privacy", String.valueOf(idPrivacy))
.addParameter("nsfw", "false")
2020-11-21 09:37:18 +01:00
.addParameter("name", filename)
2020-06-26 08:50:49 +02:00
.addParameter("commentsEnabled", "true")
2020-11-20 18:48:29 +01:00
.addParameter("downloadEnabled", "true")
2020-06-26 08:50:49 +02:00
.addParameter("waitTranscoding", "true")
2020-11-20 18:48:29 +01:00
.addParameter("channelId", idChannel)
2020-11-21 09:37:18 +01:00
.addFileToUpload(uri.toString(), "videofile")
2020-11-20 18:48:29 +01:00
.setNotificationConfig((context, uploadId) -> getNotificationConfig(uploadId))
.setMaxRetries(2)
2020-06-26 08:50:49 +02:00
.startUpload();
finish();
} catch (Exception exc) {
exc.printStackTrace();
}
}
});
}
2020-11-20 18:48:29 +01:00
UploadNotificationConfig getNotificationConfig(String uploadId) {
PendingIntent clickIntent = PendingIntent.getActivity(
2020-11-21 09:59:33 +01:00
PeertubeUploadActivity.this, 1, new Intent(this, PeertubeEditUploadActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
2020-11-20 18:48:29 +01:00
final boolean autoClear = false;
final boolean clearOnAction = true;
final boolean ringToneEnabled = true;
final ArrayList<UploadNotificationAction> noActions = new ArrayList<>(1);
final UploadNotificationAction cancelAction = new UploadNotificationAction(
R.drawable.ic_baseline_cancel_24,
getString(R.string.cancel),
ContextExtensionsKt.getCancelUploadIntent(this, uploadId)
);
final ArrayList<UploadNotificationAction> progressActions = new ArrayList<>(1);
progressActions.add(cancelAction);
UploadNotificationStatusConfig progress = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.uploading),
R.drawable.ic_baseline_cloud_upload_24,
Color.BLUE,
2020-11-21 11:06:26 +01:00
null,
2020-11-20 18:48:29 +01:00
clickIntent,
progressActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig success = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.upload_video_success),
R.drawable.ic_baseline_check_24,
Color.GREEN,
2020-11-21 11:06:26 +01:00
null,
2020-11-20 18:48:29 +01:00
clickIntent,
2020-11-21 11:06:26 +01:00
noActions,
2020-11-20 18:48:29 +01:00
clearOnAction,
autoClear
);
UploadNotificationStatusConfig error = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.toast_error),
R.drawable.ic_baseline_error_24,
Color.RED,
2020-11-21 11:06:26 +01:00
null,
2020-11-20 18:48:29 +01:00
clickIntent,
noActions,
clearOnAction,
autoClear
);
UploadNotificationStatusConfig cancelled = new UploadNotificationStatusConfig(
getString(R.string.app_name),
getString(R.string.toast_cancelled),
R.drawable.ic_baseline_cancel_24,
Color.YELLOW,
2020-11-21 11:06:26 +01:00
null,
2020-11-20 18:48:29 +01:00
clickIntent,
noActions,
clearOnAction
);
2022-05-05 08:53:25 +02:00
return new UploadNotificationConfig(BaseFedilabTube.UPLOAD_CHANNEL_ID, ringToneEnabled, progress, success, error, cancelled);
2020-11-20 18:48:29 +01:00
}
2020-06-26 08:50:49 +02:00
}