2021-03-26 13:28:11 +01:00
|
|
|
package org.schabi.newpipe.util.external_communication;
|
2019-04-06 20:11:23 +02:00
|
|
|
|
2020-12-15 20:11:55 +01:00
|
|
|
import android.content.ActivityNotFoundException;
|
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;
|
2021-01-18 21:45:36 +01:00
|
|
|
import android.content.pm.ResolveInfo;
|
2019-04-06 20:11:23 +02:00
|
|
|
import android.net.Uri;
|
2021-03-20 16:35:14 +01:00
|
|
|
import android.os.Build;
|
2020-06-11 00:14:08 +02:00
|
|
|
import android.widget.Toast;
|
2019-04-06 20:11:23 +02:00
|
|
|
|
2021-05-21 14:52:21 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2020-09-13 13:50:29 +02:00
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 17:11:04 +01:00
|
|
|
/**
|
|
|
|
* Open an Intent to install an app.
|
|
|
|
* <p>
|
2021-01-16 17:49:01 +01:00
|
|
|
* This method tries to open the default app market with the package id passed as the
|
|
|
|
* second param (a system chooser will be opened if there are multiple markets and no default)
|
|
|
|
* and falls back to Google Play Store web URL if no app to handle the market scheme was found.
|
|
|
|
* <p>
|
2021-03-20 16:35:14 +01:00
|
|
|
* It uses {@link #openIntentInApp(Context, Intent, boolean)} to open market scheme
|
|
|
|
* and {@link #openUrlInBrowser(Context, String, boolean)} to open Google Play Store
|
2021-03-13 12:37:54 +01:00
|
|
|
* web URL with false for the boolean param.
|
2021-01-15 17:11:04 +01:00
|
|
|
*
|
2021-01-16 17:49:01 +01:00
|
|
|
* @param context the context to use
|
|
|
|
* @param packageId the package id of the app to be installed
|
2021-01-15 17:11:04 +01:00
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static void installApp(@NonNull final Context context, final String packageId) {
|
2021-03-20 16:35:14 +01:00
|
|
|
// Try market scheme
|
2021-01-16 17:49:01 +01:00
|
|
|
final boolean marketSchemeResult = openIntentInApp(context, new Intent(Intent.ACTION_VIEW,
|
|
|
|
Uri.parse("market://details?id=" + packageId))
|
2021-03-13 12:37:54 +01:00
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), false);
|
2021-01-16 17:49:01 +01:00
|
|
|
if (!marketSchemeResult) {
|
|
|
|
// Fall back to Google Play Store Web URL (F-Droid can handle it)
|
2021-01-15 17:11:04 +01:00
|
|
|
openUrlInBrowser(context,
|
2021-01-16 17:49:01 +01:00
|
|
|
"https://play.google.com/store/apps/details?id=" + packageId, false);
|
2021-01-15 17:11:04 +01: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
|
2021-03-20 16:35:14 +01:00
|
|
|
* {@link #openAppChooser(Context, Intent, boolean)}
|
2020-12-15 20:11:55 +01:00
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param url the url to browse
|
2021-01-15 17:11:04 +01:00
|
|
|
* @param httpDefaultBrowserTest the boolean to set if the test for the default browser will be
|
|
|
|
* for HTTP protocol or for the created intent
|
2021-01-16 17:49:01 +01:00
|
|
|
* @return true if the URL can be opened or false if it cannot
|
2020-12-15 20:11:55 +01:00
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static boolean openUrlInBrowser(@NonNull final Context context,
|
2021-03-20 16:35:14 +01:00
|
|
|
final String url,
|
2021-01-16 17:49:01 +01:00
|
|
|
final boolean httpDefaultBrowserTest) {
|
2020-12-15 20:11:55 +01:00
|
|
|
final String defaultPackageName;
|
|
|
|
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
|
|
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2021-01-16 17:49:01 +01:00
|
|
|
|
2020-12-15 20:11:55 +01:00
|
|
|
if (httpDefaultBrowserTest) {
|
2021-01-16 17:49:01 +01:00
|
|
|
defaultPackageName = getDefaultAppPackageName(context, new Intent(Intent.ACTION_VIEW,
|
|
|
|
Uri.parse("http://")).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
|
2020-12-15 20:11:55 +01:00
|
|
|
} else {
|
|
|
|
defaultPackageName = getDefaultAppPackageName(context, intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (defaultPackageName.equals("android")) {
|
2021-01-15 17:11:04 +01:00
|
|
|
// No browser set as default (doesn't work on some devices)
|
2021-03-13 12:37:54 +01:00
|
|
|
openAppChooser(context, intent, true);
|
2020-12-15 20:11:55 +01:00
|
|
|
} else {
|
2021-01-18 21:45:36 +01:00
|
|
|
if (defaultPackageName.isEmpty()) {
|
|
|
|
// No app installed to open a web url
|
|
|
|
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG).show();
|
2021-01-16 17:49:01 +01:00
|
|
|
return false;
|
2021-01-18 21:45:36 +01:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
intent.setPackage(defaultPackageName);
|
|
|
|
context.startActivity(intent);
|
|
|
|
} catch (final ActivityNotFoundException e) {
|
|
|
|
// Not a browser but an app chooser because of OEMs changes
|
|
|
|
intent.setPackage(null);
|
2021-03-13 12:37:54 +01:00
|
|
|
openAppChooser(context, intent, true);
|
2021-01-18 21:45:36 +01:00
|
|
|
}
|
2020-12-15 20:11:55 +01:00
|
|
|
}
|
|
|
|
}
|
2021-01-16 17:49:01 +01:00
|
|
|
|
|
|
|
return true;
|
2020-12-15 20:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open the url with the system default browser.
|
|
|
|
* <p>
|
|
|
|
* If no browser is set as default, fallbacks to
|
2021-03-20 16:35:14 +01:00
|
|
|
* {@link #openAppChooser(Context, Intent, boolean)}
|
2020-12-15 20:11:55 +01:00
|
|
|
* <p>
|
2021-03-20 16:35:14 +01:00
|
|
|
* This calls {@link #openUrlInBrowser(Context, String, boolean)} with true
|
2020-12-15 20:11:55 +01:00
|
|
|
* for the boolean parameter
|
2020-04-28 21:12:41 +02:00
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param url the url to browse
|
2021-01-16 17:49:01 +01:00
|
|
|
* @return true if the URL can be opened or false if it cannot be
|
2020-12-15 20:11:55 +01:00
|
|
|
**/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static boolean openUrlInBrowser(@NonNull final Context context, final String url) {
|
2021-01-16 17:49:01 +01:00
|
|
|
return openUrlInBrowser(context, url, true);
|
2020-12-15 20:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-15 17:11:04 +01:00
|
|
|
* Open an intent with the system default app.
|
|
|
|
* <p>
|
|
|
|
* The intent can be of every type, excepted a web intent for which
|
2021-03-20 16:35:14 +01:00
|
|
|
* {@link #openUrlInBrowser(Context, String, boolean)} should be used.
|
2020-12-15 20:11:55 +01:00
|
|
|
* <p>
|
2021-04-02 18:48:58 +02:00
|
|
|
* If no app can open the intent, a toast with the message {@code No app on your device can
|
|
|
|
* open this} is shown.
|
2020-12-15 20:11:55 +01:00
|
|
|
*
|
2021-03-13 12:37:54 +01:00
|
|
|
* @param context the context to use
|
|
|
|
* @param intent the intent to open
|
2021-03-20 16:35:14 +01:00
|
|
|
* @param showToast a boolean to set if a toast is displayed to user when no app is installed
|
2021-03-13 12:37:54 +01:00
|
|
|
* to open the intent (true) or not (false)
|
2021-01-16 17:49:01 +01:00
|
|
|
* @return true if the intent can be opened or false if it cannot be
|
2020-12-15 20:11:55 +01:00
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static boolean openIntentInApp(@NonNull final Context context,
|
|
|
|
@NonNull final Intent intent,
|
2021-03-13 12:37:54 +01:00
|
|
|
final boolean showToast) {
|
2021-01-18 21:45:36 +01:00
|
|
|
final String defaultPackageName = getDefaultAppPackageName(context, intent);
|
2020-04-28 21:12:41 +02:00
|
|
|
|
2021-04-02 18:48:58 +02:00
|
|
|
if (defaultPackageName.isEmpty()) {
|
|
|
|
// No app installed to open the intent
|
|
|
|
if (showToast) {
|
|
|
|
Toast.makeText(context, R.string.no_app_to_open_intent, Toast.LENGTH_LONG)
|
|
|
|
.show();
|
2020-12-15 20:11:55 +01:00
|
|
|
}
|
2021-04-02 18:48:58 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
context.startActivity(intent);
|
2020-04-28 21:12:41 +02:00
|
|
|
}
|
2021-01-16 17:49:01 +01:00
|
|
|
|
|
|
|
return true;
|
2020-04-28 21:12:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-01-16 17:49:01 +01:00
|
|
|
* Open the system chooser to launch an intent.
|
2020-04-28 21:12:41 +02:00
|
|
|
* <p>
|
2021-01-16 17:49:01 +01:00
|
|
|
* This method opens an {@link android.content.Intent#ACTION_CHOOSER} of the intent putted
|
2021-03-13 12:37:54 +01:00
|
|
|
* as the intent param. If the setTitleChooser boolean is true, the string "Open with" will be
|
|
|
|
* set as the title of the system chooser.
|
|
|
|
* For Android P and higher, title for {@link android.content.Intent#ACTION_SEND} system
|
|
|
|
* choosers must be set on this intent, not on the
|
|
|
|
* {@link android.content.Intent#ACTION_CHOOSER} intent.
|
2020-04-28 21:12:41 +02:00
|
|
|
*
|
2021-03-13 12:37:54 +01:00
|
|
|
* @param context the context to use
|
|
|
|
* @param intent the intent to open
|
|
|
|
* @param setTitleChooser set the title "Open with" to the chooser if true, else not
|
2020-12-15 20:11:55 +01:00
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
private static void openAppChooser(@NonNull final Context context,
|
|
|
|
@NonNull final Intent intent,
|
2021-03-13 12:37:54 +01:00
|
|
|
final boolean setTitleChooser) {
|
2021-01-16 17:49:01 +01:00
|
|
|
final Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
|
|
|
|
chooserIntent.putExtra(Intent.EXTRA_INTENT, intent);
|
|
|
|
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
2021-03-13 12:37:54 +01:00
|
|
|
if (setTitleChooser) {
|
|
|
|
chooserIntent.putExtra(Intent.EXTRA_TITLE, context.getString(R.string.open_with));
|
|
|
|
}
|
2021-03-20 16:35:14 +01:00
|
|
|
|
|
|
|
// Migrate any clip data and flags from the original intent.
|
|
|
|
final int permFlags;
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
permFlags = intent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
|
|
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
|
|
|
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
|
|
|
|
| Intent.FLAG_GRANT_PREFIX_URI_PERMISSION);
|
|
|
|
} else {
|
|
|
|
permFlags = intent.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION
|
|
|
|
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
|
|
|
|
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
|
|
|
|
}
|
|
|
|
if (permFlags != 0) {
|
|
|
|
ClipData targetClipData = intent.getClipData();
|
|
|
|
if (targetClipData == null && intent.getData() != null) {
|
|
|
|
final ClipData.Item item = new ClipData.Item(intent.getData());
|
|
|
|
final String[] mimeTypes;
|
|
|
|
if (intent.getType() != null) {
|
|
|
|
mimeTypes = new String[] {intent.getType()};
|
|
|
|
} else {
|
|
|
|
mimeTypes = new String[] {};
|
|
|
|
}
|
|
|
|
targetClipData = new ClipData(null, mimeTypes, item);
|
|
|
|
}
|
|
|
|
if (targetClipData != null) {
|
|
|
|
chooserIntent.setClipData(targetClipData);
|
|
|
|
chooserIntent.addFlags(permFlags);
|
|
|
|
}
|
|
|
|
}
|
2021-01-16 17:49:01 +01:00
|
|
|
context.startActivity(chooserIntent);
|
2020-12-15 20:11:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the default app package name.
|
|
|
|
* <p>
|
2021-01-18 21:45:36 +01:00
|
|
|
* If no app is set as default, it will return "android" (not on some devices because some
|
|
|
|
* OEMs changed the app chooser).
|
2020-12-15 20:11:55 +01:00
|
|
|
* <p>
|
2021-01-18 21:45:36 +01:00
|
|
|
* If no app is installed on user's device to handle the intent, it will return an empty string.
|
2020-12-15 20:11:55 +01:00
|
|
|
*
|
2020-04-28 21:12:41 +02:00
|
|
|
* @param context the context to use
|
2020-12-15 20:11:55 +01:00
|
|
|
* @param intent the intent to get default app
|
2021-01-16 17:49:01 +01:00
|
|
|
* @return the package name of the default app, an empty string if there's no app installed to
|
|
|
|
* handle the intent or the app chooser if there's no default
|
2020-04-28 21:12:41 +02:00
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
private static String getDefaultAppPackageName(@NonNull final Context context,
|
|
|
|
@NonNull final Intent intent) {
|
2021-01-18 21:45:36 +01:00
|
|
|
final ResolveInfo resolveInfo = context.getPackageManager().resolveActivity(intent,
|
|
|
|
PackageManager.MATCH_DEFAULT_ONLY);
|
2019-04-06 20:11:23 +02:00
|
|
|
|
2021-01-18 21:45:36 +01:00
|
|
|
if (resolveInfo == null) {
|
|
|
|
return "";
|
|
|
|
} else {
|
|
|
|
return resolveInfo.activityInfo.packageName;
|
|
|
|
}
|
2020-04-28 21:12:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-26 13:28:11 +01:00
|
|
|
* Open the android share sheet to share a content.
|
2021-03-20 16:35:14 +01:00
|
|
|
*
|
|
|
|
* For Android 10+ users, a content preview is shown, which includes the title of the shared
|
|
|
|
* content.
|
|
|
|
* Support sharing the image of the content needs to done, if possible.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
2021-03-26 13:28:11 +01:00
|
|
|
* @param title the title of the content
|
|
|
|
* @param content the content to share
|
2021-03-20 16:35:14 +01:00
|
|
|
* @param imagePreviewUrl the image of the subject
|
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static void shareText(@NonNull final Context context,
|
|
|
|
@NonNull final String title,
|
2021-03-26 13:28:11 +01:00
|
|
|
final String content,
|
|
|
|
final String imagePreviewUrl) {
|
2020-12-15 20:11:55 +01:00
|
|
|
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
|
|
|
|
shareIntent.setType("text/plain");
|
2021-04-02 18:48:58 +02:00
|
|
|
shareIntent.putExtra(Intent.EXTRA_TEXT, content);
|
2021-03-26 13:28:11 +01:00
|
|
|
if (!title.isEmpty()) {
|
|
|
|
shareIntent.putExtra(Intent.EXTRA_TITLE, title);
|
2021-08-27 19:26:32 +02:00
|
|
|
shareIntent.putExtra(Intent.EXTRA_SUBJECT, title);
|
2021-03-13 12:37:54 +01:00
|
|
|
}
|
2021-03-26 13:28:11 +01:00
|
|
|
|
|
|
|
/* TODO: add the image of the content to Android share sheet with setClipData after
|
|
|
|
generating a content URI of this image, then use ClipData.newUri(the content resolver,
|
|
|
|
null, the content URI) and set the ClipData to the share intent with
|
|
|
|
shareIntent.setClipData(generated ClipData).
|
|
|
|
if (!imagePreviewUrl.isEmpty()) {
|
|
|
|
//shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
|
|
}*/
|
2021-01-15 17:11:04 +01:00
|
|
|
|
2021-03-13 12:37:54 +01:00
|
|
|
openAppChooser(context, shareIntent, false);
|
2019-04-06 20:11:23 +02:00
|
|
|
}
|
2020-06-11 00:14:08 +02:00
|
|
|
|
2021-04-25 14:14:56 +02:00
|
|
|
/**
|
|
|
|
* Open the android share sheet to share a content.
|
|
|
|
*
|
|
|
|
* For Android 10+ users, a content preview is shown, which includes the title of the shared
|
|
|
|
* content.
|
|
|
|
* <p>
|
|
|
|
* This calls {@link #shareText(Context, String, String, String)} with an empty string for the
|
|
|
|
* imagePreviewUrl parameter.
|
|
|
|
*
|
|
|
|
* @param context the context to use
|
|
|
|
* @param title the title of the content
|
|
|
|
* @param content the content to share
|
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static void shareText(@NonNull final Context context,
|
|
|
|
@NonNull final String title,
|
|
|
|
final String content) {
|
2021-04-25 14:14:56 +02:00
|
|
|
shareText(context, title, content, "");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2021-05-21 14:52:21 +02:00
|
|
|
public static void copyToClipboard(@NonNull final Context context, final String text) {
|
2020-06-11 18:36:57 +02:00
|
|
|
final ClipboardManager clipboardManager =
|
2020-09-13 13:50:29 +02:00
|
|
|
ContextCompat.getSystemService(context, ClipboardManager.class);
|
2020-06-11 00:14:08 +02:00
|
|
|
|
|
|
|
if (clipboardManager == null) {
|
2020-12-15 20:11:55 +01:00
|
|
|
Toast.makeText(context, R.string.permission_denied, Toast.LENGTH_LONG).show();
|
2020-06-11 00:14:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, text));
|
2020-12-15 20:11:55 +01:00
|
|
|
Toast.makeText(context, R.string.msg_copied, Toast.LENGTH_SHORT).show();
|
2019-04-06 20:11:23 +02:00
|
|
|
}
|
|
|
|
}
|