Comment #16 - Redirect to another app if the URL can't be open with TubeLab

This commit is contained in:
Thomas 2020-10-26 19:02:55 +01:00
parent 9af7934448
commit 824c4e0cbb
2 changed files with 64 additions and 11 deletions

View File

@ -163,6 +163,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
private List<Comment> commentsThread;
private BroadcastReceiver mPowerKeyReceiver = null;
private boolean isPlayInMinimized;
private Intent urlIntent;
public static void hideKeyboard(Activity activity) {
if (activity != null && activity.getWindow() != null) {
@ -183,7 +184,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
setContentView(view);
max_id = "0";
urlIntent = null;
SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
@ -220,16 +221,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
peertube = b.getParcelable("video");
}
if(intent.getData() != null) { //Comes from a link
String url = intent.getData().toString();
Pattern link = Pattern.compile("(https?://[\\da-z.-]+\\.[a-z.]{2,10})/videos/watch/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})$");
Matcher matcherLink = link.matcher(url);
if (matcherLink.find()) {
peertubeInstance = matcherLink.group(1).replace("https://","").replace("http://","");
sepiaSearch = true; // Sepia search flag is used because, at this time we don't know if the video is federated.
videoUuid = matcherLink.group(2);
}
}
manageIntentUrl(intent);
if (!Helper.isLoggedIn(PeertubeActivity.this) || sepiaSearch) {
binding.writeCommentContainerReply.setVisibility(View.GONE);
@ -332,6 +324,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
private void manageVIewVideos(APIResponse apiResponse) {
if (apiResponse == null || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0) {
playVideo();
if(urlIntent != null) {
Helper.forwardToAnotherApp(PeertubeActivity.this, urlIntent);
urlIntent = null;
finish();
}
return;
}
peertube = apiResponse.getPeertubes().get(0);
@ -411,6 +408,29 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd
}
playVideo();
}
manageIntentUrl(intent);
}
private void manageIntentUrl(Intent intent) {
if(intent.getData() != null) { //Comes from a link
String url = intent.getData().toString();
Pattern link = Pattern.compile("(https?://[\\da-z.-]+\\.[a-z.]{2,10})/videos/watch/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})$");
Matcher matcherLink = link.matcher(url);
if (matcherLink.find()) {
peertubeInstance = matcherLink.group(1).replace("https://","").replace("http://","");
sepiaSearch = true; // Sepia search flag is used because, at this time we don't know if the video is federated.
videoUuid = matcherLink.group(2);
peertube = new VideoData.Video();
peertube.setUuid(matcherLink.group(2));
peertube.setEmbedUrl(url);
urlIntent = intent;
SearchVM viewModelSearch = new ViewModelProvider(PeertubeActivity.this).get(SearchVM.class);
viewModelSearch.getVideos("0", peertube.getEmbedUrl()).observe(PeertubeActivity.this, this::manageVIewVideos);
}else{
Helper.forwardToAnotherApp(PeertubeActivity.this, intent);
finish();
}
}
}
private void playVideo() {

View File

@ -17,9 +17,11 @@ package app.fedilab.fedilabtube.helper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
@ -27,6 +29,7 @@ import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Parcelable;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
@ -48,6 +51,7 @@ import com.bumptech.glide.request.RequestOptions;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
@ -700,4 +704,33 @@ public class Helper {
}
}
}
/**
* Forward the intent (open an URL) to another app
* @param activity Activity
* @param i Intent
*/
public static void forwardToAnotherApp(Activity activity, Intent i) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(i.getData(), i.getType());
List<ResolveInfo> activities = activity.getPackageManager().queryIntentActivities(intent, 0);
ArrayList<Intent> targetIntents = new ArrayList<>();
String thisPackageName = activity.getPackageName();
for (ResolveInfo currentInfo : activities) {
String packageName = currentInfo.activityInfo.packageName;
if (!thisPackageName.equals(packageName)) {
Intent targetIntent = new Intent(android.content.Intent.ACTION_VIEW);
targetIntent.setDataAndType(intent.getData(), intent.getType());
targetIntent.setPackage(intent.getPackage());
targetIntent.setComponent(new ComponentName(packageName, currentInfo.activityInfo.name));
targetIntents.add(targetIntent);
}
}
if (targetIntents.size() > 0) {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), activity.getString(R.string.open_with));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
activity.startActivity(chooserIntent);
}
}
}