2019-04-06 20:11:23 +02:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
2020-06-11 00:14:08 +02:00
|
|
|
import android.content.ClipData;
|
|
|
|
import android.content.ClipboardManager;
|
2019-04-06 20:11:23 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2020-04-28 21:12:41 +02:00
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.content.pm.ResolveInfo;
|
2019-04-06 20:11:23 +02:00
|
|
|
import android.net.Uri;
|
2020-06-11 00:14:08 +02:00
|
|
|
import android.widget.Toast;
|
2019-04-06 20:11:23 +02:00
|
|
|
|
|
|
|
import org.schabi.newpipe.R;
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public final class ShareUtils {
|
2020-04-28 21:12:41 +02:00
|
|
|
private ShareUtils() {
|
2019-04-06 20:11:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 21:12:41 +02:00
|
|
|
/**
|
|
|
|
* Open the url with the system default browser.
|
|
|
|
* <p>
|
|
|
|
* If no browser is set as default, fallbacks to
|
2020-05-19 21:51:39 +02:00
|
|
|
* {@link ShareUtils#openInDefaultApp(Context, String)}
|
2020-04-28 21:12:41 +02:00
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param url the url to browse
|
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
public static void openUrlInBrowser(final Context context, final String url) {
|
2020-04-28 21:12:41 +02:00
|
|
|
final String defaultBrowserPackageName = getDefaultBrowserPackageName(context);
|
|
|
|
|
|
|
|
if (defaultBrowserPackageName.equals("android")) {
|
|
|
|
// no browser set as default
|
2020-05-19 21:51:39 +02:00
|
|
|
openInDefaultApp(context, url);
|
2020-04-28 21:12:41 +02:00
|
|
|
} else {
|
2020-07-13 03:17:21 +02:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
|
|
|
.setPackage(defaultBrowserPackageName)
|
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2020-04-28 21:12:41 +02:00
|
|
|
context.startActivity(intent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-05-19 21:57:46 +02:00
|
|
|
* Open the url in the default app set to open this type of link.
|
2020-04-28 21:12:41 +02:00
|
|
|
* <p>
|
2020-05-19 21:51:39 +02:00
|
|
|
* If no app is set as default, it will open a chooser
|
2020-04-28 21:12:41 +02:00
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param url the url to browse
|
|
|
|
*/
|
2020-05-19 21:51:39 +02:00
|
|
|
private static void openInDefaultApp(final Context context, final String url) {
|
2020-04-28 21:12:41 +02:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
2020-03-31 19:20:15 +02:00
|
|
|
context.startActivity(Intent.createChooser(
|
2020-07-13 03:17:21 +02:00
|
|
|
intent, context.getString(R.string.share_dialog_title))
|
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
2019-04-06 20:11:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-28 21:12:41 +02:00
|
|
|
/**
|
|
|
|
* Get the default browser package name.
|
|
|
|
* <p>
|
|
|
|
* If no browser is set as default, it will return "android"
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @return the package name of the default browser, or "android" if there's no default
|
|
|
|
*/
|
|
|
|
private static String getDefaultBrowserPackageName(final Context context) {
|
2020-07-13 03:17:21 +02:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
|
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2020-04-28 21:12:41 +02:00
|
|
|
final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(
|
|
|
|
intent, PackageManager.MATCH_DEFAULT_ONLY);
|
|
|
|
return resolveInfo.activityInfo.packageName;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the android share menu to share the current url.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param subject the url subject, typically the title
|
|
|
|
* @param url the url to share
|
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
public static void shareUrl(final Context context, final String subject, final String url) {
|
2020-08-16 10:24:58 +02:00
|
|
|
final Intent intent = new Intent(Intent.ACTION_SEND);
|
2019-04-06 20:11:23 +02:00
|
|
|
intent.setType("text/plain");
|
|
|
|
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, url);
|
2020-03-31 19:20:15 +02:00
|
|
|
context.startActivity(Intent.createChooser(
|
2020-07-13 03:17:21 +02:00
|
|
|
intent, context.getString(R.string.share_dialog_title))
|
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
2019-04-06 20:11:23 +02:00
|
|
|
}
|
2020-06-11 00:14:08 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy the text to clipboard, and indicate to the user whether the operation was completed
|
|
|
|
* successfully using a Toast.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param text the text to copy
|
|
|
|
*/
|
|
|
|
public static void copyToClipboard(final Context context, final String text) {
|
2020-06-11 18:36:57 +02:00
|
|
|
final ClipboardManager clipboardManager =
|
2020-06-11 00:14:08 +02:00
|
|
|
(ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
|
|
|
|
|
|
|
|
if (clipboardManager == null) {
|
|
|
|
Toast.makeText(context,
|
|
|
|
R.string.permission_denied,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text));
|
|
|
|
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT)
|
|
|
|
.show();
|
2019-04-06 20:11:23 +02:00
|
|
|
}
|
|
|
|
}
|