Commit 2077fb34 authored by Sergey Galin's avatar Sergey Galin

QAndroidDesktopUtils::sendTo now can filter out some possible recipient packages

Return simple version of sendTo() as it works faster
parent 2bbf8619
......@@ -89,11 +89,16 @@ void showApplicationSettings()
}
bool sendTo(const QString & chooser_caption, const QString & text, const QString & content_type)
bool sendTo(
const QString & chooser_caption,
const QString & text,
const QString & content_type)
{
QJniClass du(c_full_class_name_);
QAndroidQPAPluginGap::Context activity;
return du.callStaticParamBoolean("sendTo", "Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;",
return du.callStaticParamBoolean(
"sendTo",
"Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;",
activity.jObject(),
QJniLocalRef(chooser_caption).jObject(),
QJniLocalRef(text).jObject(),
......@@ -101,6 +106,30 @@ bool sendTo(const QString & chooser_caption, const QString & text, const QString
}
bool sendTo(
const QString & chooser_caption,
const QString & text,
const QString & content_type,
const QStringList & filter_packages)
{
// Fallback to the faster version if filter_packages is actually empty
if (filter_packages.isEmpty())
{
return sendTo(chooser_caption, text, content_type);
}
QJniClass du(c_full_class_name_);
QAndroidQPAPluginGap::Context activity;
return du.callStaticParamBoolean(
"sendTo",
"Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;",
activity.jObject(),
QJniLocalRef(chooser_caption).jObject(),
QJniLocalRef(text).jObject(),
QJniLocalRef(content_type).jObject(),
QJniLocalRef(filter_packages.join(QLatin1Char('\n'))).jObject());
}
bool sendSMS(const QString & number, const QString & text)
{
QJniClass du(c_full_class_name_);
......
......@@ -13,13 +13,13 @@
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the DoubleGIS, LLC nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
......@@ -53,19 +53,33 @@ int getNetworkType();
void showApplicationSettings();
bool sendTo(const QString & chooser_caption, const QString & text, const QString & content_type);
// Send a text to any application in the system.
bool sendTo(
const QString & chooser_caption,
const QString & text,
const QString & content_type = QLatin1String("text/plain"));
// Send a text to any application in the system.
// If some application packages are to be removed from possible recipients, pass list of the
// package names via filter_packages. Use * at end of package name to ignore by the prefix.
bool sendTo(
const QString & chooser_caption,
const QString & text,
const QString & content_type,
const QStringList & filter_packages);
bool sendSMS(const QString & number, const QString & text);
// WARNING: For this function to work properly with e-mail attachments on Android 6+
// or when using force_content_provider, please follow instructions in
// DesktopUtils.java => DesktopUtils.sendEmail().
bool sendEmail(
const QString & to,
const QString & subject,
const QString & body,
const QString & attach_file = QString::null,
bool force_content_provider = false,
const QString & authorities = QLatin1String("ru.dublgis.sharefileprovider"));
const QString & to,
const QString & subject,
const QString & body,
const QString & attach_file = QString::null,
bool force_content_provider = false,
const QString & authorities = QLatin1String("ru.dublgis.sharefileprovider"));
bool sendEmail(
const QString & to,
......
......@@ -132,26 +132,105 @@ public class DesktopUtils
}
// Show generic "send to" menu
public static boolean sendTo(final Context ctx, final String chooser_caption, final String text, final String content_type)
public static boolean sendTo(
final Context ctx,
final String chooserCaption,
final String text,
final String contentType)
{
try
{
Log.d(TAG, "Will send-to text \""+text+"\" of type: \""+content_type+"\"");
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(content_type);
Log.d(TAG, "sendTo " + contentType);
final Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType(contentType);
sendIntent.putExtra(Intent.EXTRA_TEXT, text);
Intent chooser = Intent.createChooser(sendIntent, chooser_caption);
final Intent chooser = Intent.createChooser(sendIntent, chooserCaption);
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(chooser);
return true;
}
catch (final Throwable e)
{
} catch (final Throwable e) {
Log.e(TAG, "sendTo exception: ", e);
return false;
}
}
// Show generic "send to" menu for a plain text and optionally filter out some applications.
// filterPackages is a "\n" - separated list of package names of prefixes (ended by "*").
public static boolean sendTo(
final Context ctx,
final String chooserCaption,
final String text,
final String contentType,
final String filterPackages)
{
try {
final String[] packages = (filterPackages == null) ? null : filterPackages.split("\n");
Log.d(TAG, "sendTo: sending " + contentType + " with package filtering.");
// Create intent with the text
final Intent i = new Intent(Intent.ACTION_SEND);
i.setType(contentType);
i.putExtra(Intent.EXTRA_TEXT, text);
// Find what activities can handle the text intent
final List<ResolveInfo> resolveInfos = ctx.getPackageManager().queryIntentActivities(i, 0);
// Filter the activities
Intent chooserIntent = null;
final List<Intent> intentList = new ArrayList<Intent>();
for (ResolveInfo resolveInfo : resolveInfos) {
boolean skip = false;
final String pn = resolveInfo.activityInfo.packageName;
final String name = resolveInfo.activityInfo.name;
if (packages != null) {
for (String filter : packages) {
if (filter == null || filter.isEmpty()) {
continue;
}
if (filter.endsWith("*")) {
if (pn.startsWith(filter.substring(0, filter.length() - 1))) {
skip = true;
break;
}
} else if (pn.equals(filter)) {
skip = true;
break;
}
}
}
if (skip) {
Log.d(TAG, "sendTo: skipping package: " + pn + ": " + name);
continue;
}
// Log.d(TAG, "sendTo: adding: " + pn + ": " + name);
final Intent targetedIntent = (Intent)i.clone();
targetedIntent.setComponent(new ComponentName(pn, name));
if (chooserIntent == null) {
chooserIntent = targetedIntent;
} else {
intentList.add(targetedIntent);
}
}
if (chooserIntent == null) {
// No activities found, fall back to simple sharing
chooserIntent = Intent.createChooser(i, chooserCaption);
chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else if (!intentList.isEmpty()) {
// Have more than one activity - build chooser with the list
final Intent keepIntent = chooserIntent; // should be moved to extra
chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, keepIntent);
final Intent[] extraIntents = intentList.toArray(new Intent[intentList.size()]);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
}
ctx.startActivity(chooserIntent);
return true;
} catch (final Throwable e) {
Log.e(TAG, "sendTo (2) exception: ", e);
return false;
}
}
public static boolean sendSMS(final Context ctx, final String number, final String text)
{
try
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment