Fix playlist export/import

This commit is contained in:
Thomas 2022-06-03 11:41:26 +02:00
parent fda17ee8fe
commit 3d85e74957
7 changed files with 85 additions and 25 deletions

View File

@ -3,4 +3,9 @@
<external-path
name="my_images"
path="/TubeAcad/" />
<external-path
name="external_files"
path="/"/>
</paths>

View File

@ -16,13 +16,15 @@
<activity
android:name=".activities.MainActivity"
tools:node="merge">
tools:node="merge"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:host="*" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.tubelab" />
<data android:pathPattern=".*\\..*\\.tubelab" />

View File

@ -24,7 +24,8 @@
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="content" />
<data android:scheme="file" />
<data android:mimeType="*/*" />
<data android:host="*" />
<data android:mimeType="application/json" />
<data android:pathPattern=".*\\.tubelab" />
<data android:pathPattern=".*\\..*\\.tubelab" />
<data android:pathPattern=".*\\..*\\..*\\.tubelab" />

View File

@ -26,6 +26,7 @@ import android.view.MenuItem;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
@ -39,7 +40,6 @@ import java.util.regex.Matcher;
import app.fedilab.fedilabtube.BuildConfig;
import app.fedilab.fedilabtube.R;
import app.fedilab.fedilabtube.client.RetrofitPeertubeAPI;
import app.fedilab.fedilabtube.client.entities.AcadInstances;
import app.fedilab.fedilabtube.client.entities.Error;
import app.fedilab.fedilabtube.client.entities.OauthParams;
import app.fedilab.fedilabtube.client.entities.Token;
@ -137,6 +137,53 @@ public class WebviewConnectActivity extends BaseActivity {
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if( request.getUrl() != null) {
String url = request.getUrl().toString();
Matcher matcher = Helper.redirectPattern.matcher(url);
if (matcher.find()) {
String externalAuthToken = matcher.group(1);
String username = matcher.group(2);
new Thread(() -> {
try {
OauthParams oauthParams = new OauthParams();
oauthParams.setClient_id(sharedpreferences.getString(Helper.CLIENT_ID, null));
oauthParams.setClient_secret(sharedpreferences.getString(Helper.CLIENT_SECRET, null));
oauthParams.setGrant_type("password");
oauthParams.setScope("upload");
oauthParams.setResponse_type("code");
oauthParams.setUsername(username);
oauthParams.setExternalAuthToken(externalAuthToken);
oauthParams.setPassword(externalAuthToken);
String instance = new URL(url).getHost();
Token token = null;
try {
token = new RetrofitPeertubeAPI(WebviewConnectActivity.this, instance, null).manageToken(oauthParams);
} catch (Error error) {
error.printStackTrace();
Error.displayError(WebviewConnectActivity.this, error);
}
if (token != null) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token.getAccess_token());
editor.putString(Helper.PREF_SOFTWARE, null);
editor.putString(Helper.PREF_REMOTE_INSTANCE, null);
editor.putString(Helper.PREF_INSTANCE, instance);
editor.apply();
updateCredential(WebviewConnectActivity.this, token.getAccess_token(), clientId, clientSecret, token.getRefresh_token(), new URL(url).getHost(), null);
finish();
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
return true;
}
}
return super.shouldOverrideUrlLoading(view, request);
}
/* @Override
public void onPageFinished(WebView view, String url) {
Matcher matcher = Helper.redirectPattern.matcher(url);
if (matcher.find()) {
@ -177,7 +224,7 @@ public class WebviewConnectActivity extends BaseActivity {
}).start();
}
super.onPageFinished(view, url);
}
}*/
});
webView.loadUrl(url);

View File

@ -205,7 +205,7 @@ public class PlaylistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private void doExport(Playlist playlist) {
new Thread(() -> {
File file = null;
File file;
RetrofitPeertubeAPI retrofitPeertubeAPI = new RetrofitPeertubeAPI(context);
APIResponse apiResponse = retrofitPeertubeAPI.playlistAction(GET_LIST_VIDEOS, playlist.getId(), null, null, null);
if (apiResponse != null) {
@ -219,12 +219,14 @@ public class PlaylistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
String data = PlaylistExportHelper.playlistToStringStorage(videoPlaylistExport);
File root = new File(Environment.getExternalStorageDirectory(), context.getString(R.string.app_name));
File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),"");
if (!root.exists()) {
//noinspection ResultOfMethodCallIgnored
root.mkdirs();
}
file = new File(root, "playlist_" + playlist.getUuid() + ".tubelab");
String fileName = "playlist_" + playlist.getUuid() + ".tubelab";
file = new File(root, fileName);
FileWriter writer;
try {
writer = new FileWriter(file);
@ -238,33 +240,29 @@ public class PlaylistAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
mainHandler.post(myRunnable);
return;
}
}
String urlAvatar = playlist.getThumbnailPath() != null ? HelperInstance.getLiveInstance(context) + playlist.getThumbnailPath() : null;
FutureTarget<Bitmap> futureBitmapChannel = Glide.with(context.getApplicationContext())
.asBitmap()
.load(urlAvatar != null ? urlAvatar : R.drawable.missing_peertube).submit();
Bitmap icon = null;
try {
icon = futureBitmapChannel.get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
if (file != null) {
String urlAvatar = playlist.getThumbnailPath() != null ? HelperInstance.getLiveInstance(context) + playlist.getThumbnailPath() : null;
FutureTarget<Bitmap> futureBitmapChannel = Glide.with(context.getApplicationContext())
.asBitmap()
.load(urlAvatar != null ? urlAvatar : R.drawable.missing_peertube).submit();
Bitmap icon = null;
try {
icon = futureBitmapChannel.get();
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("message/rfc822");
Uri contentUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileProvider", file);
Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID+".fileProvider", file);
mailIntent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.export_notification_subjet));
mailIntent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.export_notification_body));
mailIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
mailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
NotificationHelper.notify_user(context.getApplicationContext(),
playlist.getOwnerAccount(), mailIntent, icon,
context.getString(R.string.export_notification_title),
context.getString(R.string.export_notification_content));
}
}).start();
}

View File

@ -17,6 +17,7 @@ package app.fedilab.fedilabtube.helper;
import static app.fedilab.fedilabtube.worker.NotificationsWorker.FETCH_NOTIFICATION_CHANNEL_ID;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
@ -45,10 +46,16 @@ public class NotificationHelper {
* @param title String title of the notification
* @param message String message for the notification
*/
@SuppressLint("UnspecifiedImmutableFlag")
public static void notify_user(Context context, AccountData.Account account, Intent intent, Bitmap icon, String title, String message) {
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
int notificationId = (int) System.currentTimeMillis();
PendingIntent pIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
PendingIntent pIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
pIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_ONE_SHOT|PendingIntent.FLAG_IMMUTABLE);
} else {
pIntent = PendingIntent.getActivity(context, notificationId, intent, PendingIntent.FLAG_ONE_SHOT);
}
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP);
RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

View File

@ -81,7 +81,7 @@ public class PlaylistExportHelper {
try {
cursor = activity.getContentResolver().query(intent.getData(), null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
filename = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME));
}
} finally {
assert cursor != null;