[android]: Use native sharing dialog

Signed-off-by: Roman Tsisyk <roman@tsisyk.com>
This commit is contained in:
Roman Tsisyk 2021-04-10 09:57:53 +03:00
parent 04bc294c85
commit fda516626f
23 changed files with 235 additions and 834 deletions

View file

@ -19,7 +19,6 @@ import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageButton;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -30,6 +29,7 @@ import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import com.mapswithme.maps.Framework.PlacePageActivationListener;
import com.mapswithme.maps.api.ParsedMwmRequest;
import com.mapswithme.maps.background.AppBackgroundTracker;
@ -130,6 +130,7 @@ import com.mapswithme.maps.widget.placepage.RoutingModeListener;
import com.mapswithme.util.InputUtils;
import com.mapswithme.util.PermissionsUtils;
import com.mapswithme.util.SharedPropertiesUtils;
import com.mapswithme.util.SharingUtils;
import com.mapswithme.util.ThemeSwitcher;
import com.mapswithme.util.ThemeUtils;
import com.mapswithme.util.UTM;
@ -138,9 +139,6 @@ import com.mapswithme.util.Utils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.permissions.PermissionsResult;
import com.mapswithme.util.sharing.ShareOption;
import com.mapswithme.util.sharing.SharingHelper;
import com.mapswithme.util.sharing.TargetUtils;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import java.util.List;
@ -439,12 +437,7 @@ public class MwmActivity extends BaseMwmFragmentActivity
final Location loc = LocationHelper.INSTANCE.getSavedLocation();
if (loc != null)
{
final String geoUrl = Framework.nativeGetGe0Url(loc.getLatitude(), loc.getLongitude(), Framework
.nativeGetDrawScale(), "");
final String httpUrl = Framework.getHttpGe0Url(loc.getLatitude(), loc.getLongitude(), Framework
.nativeGetDrawScale(), "");
final String body = getString(R.string.my_position_share_sms, geoUrl, httpUrl);
ShareOption.AnyShareOption.ANY.share(this, body);
SharingUtils.shareLocation(this, loc);
return;
}
@ -531,8 +524,6 @@ public class MwmActivity extends BaseMwmFragmentActivity
SearchEngine.INSTANCE.addListener(this);
SharingHelper.INSTANCE.initialize(this);
initControllersAndValidatePurchases(savedInstanceState);
boolean isConsumed = savedInstanceState == null && processIntent(getIntent());
@ -2289,12 +2280,22 @@ public class MwmActivity extends BaseMwmFragmentActivity
if (mLocationErrorDialogAnnoying)
return;
Intent intent = TargetUtils.makeAppSettingsLocationIntent(getApplicationContext());
Intent intent = makeAppSettingsLocationIntent();
if (intent == null)
return;
showLocationErrorDialog(intent);
}
private Intent makeAppSettingsLocationIntent()
{
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if (intent.resolveActivity(getPackageManager()) != null)
return intent;
intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
return intent.resolveActivity(getPackageManager()) == null ? null : intent;
}
@Override
public void onTranslationChanged(float translation)
{

View file

@ -21,7 +21,6 @@ import com.mapswithme.maps.base.DataChangedListener;
import com.mapswithme.maps.bookmarks.data.AbstractCategoriesSnapshot;
import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.BookmarkSharingResult;
import com.mapswithme.maps.bookmarks.data.FilterStrategy;
import com.mapswithme.maps.dialog.EditTextDialogFragment;
import com.mapswithme.maps.ugc.routes.UgcRouteEditSettingsActivity;
@ -30,7 +29,6 @@ import com.mapswithme.maps.widget.PlaceholderView;
import com.mapswithme.maps.widget.recycler.ItemDecoratorFactory;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.sharing.SharingHelper;
import java.util.List;
@ -270,7 +268,7 @@ public abstract class BaseBookmarkCategoriesFragment extends BaseMwmRecyclerFrag
protected void onShareActionSelected(@NonNull BookmarkCategory category)
{
SharingHelper.INSTANCE.prepareBookmarkCategoryForSharing(getActivity(), category.getId());
BookmarksSharingHelper.INSTANCE.prepareBookmarkCategoryForSharing(getActivity(), category.getId());
}
private void onDeleteActionSelected(@NonNull BookmarkCategory category)

View file

@ -14,8 +14,6 @@ import com.mapswithme.maps.bookmarks.data.BookmarkCategory;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.BookmarkSharingResult;
import com.mapswithme.maps.widget.BookmarkBackupView;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.sharing.SharingHelper;
public class BookmarkCategoriesFragment extends BaseBookmarkCategoriesFragment
implements TargetFragmentCallback, AuthCompleteListener, BookmarkManager.BookmarksSharingListener
@ -36,7 +34,7 @@ public class BookmarkCategoriesFragment extends BaseBookmarkCategoriesFragment
@Override
public void onPreparedFileForSharing(@NonNull BookmarkSharingResult result)
{
SharingHelper.INSTANCE.onPreparedFileForSharing(requireActivity(), result);
BookmarksSharingHelper.INSTANCE.onPreparedFileForSharing(requireActivity(), result);
}
@Override

View file

@ -11,7 +11,6 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -22,6 +21,7 @@ import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import com.cocosw.bottomsheet.BottomSheet;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseMwmRecyclerFragment;
@ -47,9 +47,8 @@ import com.mapswithme.maps.widget.placepage.Sponsored;
import com.mapswithme.maps.widget.recycler.ItemDecoratorFactory;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.CrashlyticsUtils;
import com.mapswithme.util.SharingUtils;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.sharing.ShareOption;
import com.mapswithme.util.sharing.SharingHelper;
import java.util.List;
@ -693,8 +692,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<MergeAdapter>
switch (menuItem.getItemId())
{
case R.id.share:
ShareOption.AnyShareOption.ANY.shareBookmarkObject(requireActivity(), item,
Sponsored.nativeGetCurrent());
SharingUtils.shareBookmark(requireContext(), item, Sponsored.nativeGetCurrent());
break;
case R.id.edit:
@ -738,7 +736,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<MergeAdapter>
case R.id.share_category:
long catId = mCategoryDataSource.getData().getId();
SharingHelper.INSTANCE.prepareBookmarkCategoryForSharing(requireActivity(), catId);
BookmarksSharingHelper.INSTANCE.prepareBookmarkCategoryForSharing(requireActivity(), catId);
return false;
case R.id.settings:
@ -827,7 +825,7 @@ public class BookmarksListFragment extends BaseMwmRecyclerFragment<MergeAdapter>
@Override
public void onPreparedFileForSharing(@NonNull BookmarkSharingResult result)
{
SharingHelper.INSTANCE.onPreparedFileForSharing(requireActivity(), result);
BookmarksSharingHelper.INSTANCE.onPreparedFileForSharing(requireActivity(), result);
}
private void openSharingOptionsScreen()

View file

@ -0,0 +1,60 @@
package com.mapswithme.maps.bookmarks;
import android.app.Activity;
import android.app.ProgressDialog;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.BookmarkSharingResult;
import com.mapswithme.maps.dialog.DialogUtils;
import com.mapswithme.util.SharingUtils;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
public enum BookmarksSharingHelper
{
INSTANCE;
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = BookmarksSharingHelper.class.getSimpleName();
@Nullable
private ProgressDialog mProgressDialog;
public void prepareBookmarkCategoryForSharing(@NonNull Activity context, long catId)
{
mProgressDialog = DialogUtils.createModalProgressDialog(context, R.string.please_wait);
mProgressDialog.show();
BookmarkManager.INSTANCE.prepareCategoryForSharing(catId);
}
public void onPreparedFileForSharing(@NonNull Activity context,
@NonNull BookmarkSharingResult result)
{
if (mProgressDialog != null && mProgressDialog.isShowing())
mProgressDialog.dismiss();
switch (result.getCode())
{
case BookmarkSharingResult.SUCCESS:
SharingUtils.shareBookmarkFile(context, result.getSharingPath());
break;
case BookmarkSharingResult.EMPTY_CATEGORY:
DialogUtils.showAlertDialog(context, R.string.bookmarks_error_title_share_empty,
R.string.bookmarks_error_message_share_empty);
break;
case BookmarkSharingResult.ARCHIVE_ERROR:
case BookmarkSharingResult.FILE_ERROR:
DialogUtils.showAlertDialog(context, R.string.dialog_routing_system_error,
R.string.bookmarks_error_message_share_general);
String catName = BookmarkManager.INSTANCE.getCategoryById(result.getCategoryId()).getName();
LOGGER.e(TAG, "Failed to share bookmark category '" + catName + "', error code: "
+ result.getCode());
break;
default:
throw new AssertionError("Unsupported bookmark sharing code: " + result.getCode());
}
}
}

View file

@ -2,11 +2,11 @@ package com.mapswithme.maps.bookmarks.data;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import com.mapswithme.maps.Framework;
import com.mapswithme.util.GeoUtils;
import com.mapswithme.util.sharing.ShareableInfoProvider;
public class BookmarkInfo implements ShareableInfoProvider
public class BookmarkInfo
{
private final long mCategoryId;
private final long mBookmarkId;
@ -59,7 +59,6 @@ public class BookmarkInfo implements ShareableInfoProvider
public String getFeatureType() { return mFeatureType; }
@NonNull
@Override
public String getName()
{
return mTitle;
@ -77,26 +76,22 @@ public class BookmarkInfo implements ShareableInfoProvider
return getDistanceAndAzimuth(latitude, longitude, v).getDistance();
}
@Override
public double getLat()
{
return mLatLonPoint.x;
}
@Override
public double getLon()
{
return mLatLonPoint.y;
}
@Override
public double getScale()
{
return mScale;
}
@NonNull
@Override
public String getAddress()
{
return mAddress;

View file

@ -2,10 +2,10 @@ package com.mapswithme.maps.bookmarks.data;
import android.os.Parcel;
import android.text.TextUtils;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.routing.RoutePointInfo;
import com.mapswithme.maps.search.HotelsFilter;
import com.mapswithme.maps.search.Popularity;
@ -14,7 +14,6 @@ import com.mapswithme.maps.search.PriceFilterView;
import com.mapswithme.maps.taxi.TaxiType;
import com.mapswithme.maps.ugc.UGC;
import com.mapswithme.maps.widget.placepage.PlacePageData;
import com.mapswithme.util.sharing.ShareableInfoProvider;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@ -24,8 +23,7 @@ import java.util.List;
// TODO(yunikkk): Refactor. Displayed information is different from edited information, and it's better to
// separate them. Simple getters from jni place_page::Info and osm::EditableFeature should be enough.
public class MapObject implements PopularityProvider, ShareableInfoProvider,
PlacePageData
public class MapObject implements PopularityProvider, PlacePageData
{
// Order must correspond indexer/map_object.hpp
public enum OsmProps
@ -294,7 +292,6 @@ public class MapObject implements PopularityProvider, ShareableInfoProvider,
}
@NonNull
@Override
public String getName()
{
return getTitle();

View file

@ -5,21 +5,21 @@ import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.BuildConfig;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.util.Constants;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.SharingUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.sharing.ShareOption;
public class AboutFragment extends BaseSettingsFragment
implements View.OnClickListener
@ -102,8 +102,7 @@ public class AboutFragment extends BaseSettingsFragment
break;
case R.id.share:
ShareOption.AnyShareOption.ANY.share(getActivity(), getString(R.string.tell_friends_text),
R.string.tell_friends);
SharingUtils.shareApplication(getContext());
break;
case R.id.copyright:

View file

@ -1,13 +1,13 @@
package com.mapswithme.maps.ugc.routes;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.core.app.ShareCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ShareCompat;
import androidx.fragment.app.FragmentActivity;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.BaseAuthFragment;
@ -17,7 +17,6 @@ import com.mapswithme.maps.bookmarks.data.CatalogCustomProperty;
import com.mapswithme.maps.bookmarks.data.CatalogTagsGroup;
import com.mapswithme.maps.dialog.AlertDialog;
import com.mapswithme.maps.dialog.AlertDialogCallback;
import com.mapswithme.util.sharing.TargetUtils;
import java.util.List;
import java.util.Objects;
@ -88,7 +87,7 @@ public class SendLinkPlaceholderFragment extends BaseAuthFragment implements Boo
+ BODY_STRINGS_SEPARATOR + url;
ShareCompat.IntentBuilder.from(activity)
.setType(TargetUtils.TYPE_TEXT_PLAIN)
.setType("text/plain")
.setSubject(activity.getString(R.string.edit_guide_title))
.setText(emailBody)
.setChooserTitle(activity.getString(R.string.share))

View file

@ -3,10 +3,6 @@ package com.mapswithme.maps.ugc.routes;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
@ -14,6 +10,10 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
@ -33,7 +33,6 @@ import com.mapswithme.maps.widget.ToolbarController;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.UiUtils;
import com.mapswithme.util.Utils;
import com.mapswithme.util.sharing.TargetUtils;
import java.util.List;
import java.util.Objects;
@ -259,7 +258,7 @@ public class UgcSharingOptionsFragment extends BaseToolbarAuthFragment implement
private void shareCategory(@NonNull String link)
{
Intent intent = new Intent(Intent.ACTION_SEND)
.setType(TargetUtils.TYPE_TEXT_PLAIN)
.setType("text/plain")
.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_bookmarks_email_body_link, link));
startActivity(Intent.createChooser(intent, getString(R.string.share)));
}

View file

@ -29,7 +29,6 @@ import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -39,6 +38,7 @@ import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MwmActivity;
import com.mapswithme.maps.MwmApplication;
@ -80,6 +80,7 @@ import com.mapswithme.maps.widget.recycler.RecyclerClickListener;
import com.mapswithme.util.ConnectionState;
import com.mapswithme.util.Graphics;
import com.mapswithme.util.NetworkPolicy;
import com.mapswithme.util.SharingUtils;
import com.mapswithme.util.SponsoredLinks;
import com.mapswithme.util.StringUtils;
import com.mapswithme.util.ThemeUtils;
@ -88,7 +89,6 @@ import com.mapswithme.util.Utils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import com.mapswithme.util.sharing.ShareOption;
import java.util.ArrayList;
import java.util.Arrays;
@ -621,7 +621,7 @@ public class PlacePageView extends NestedScrollViewClickFixed
LOGGER.e(TAG, "A map object cannot be shared, it's null!");
return;
}
ShareOption.AnyShareOption.ANY.shareMapObject(getActivity(), mMapObject, mSponsored);
SharingUtils.shareMapObject(getContext(), mMapObject, mSponsored);
}
private void onBackBtnClicked()

View file

@ -0,0 +1,132 @@
package com.mapswithme.util;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.BookmarkInfo;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.widget.placepage.Sponsored;
public class SharingUtils
{
private static final String KMZ_MIME_TYPE = "application/vnd.google-earth.kmz";
private static final String TEXT_MIME_TYPE = "text/plain";
// This utility class has only static methods
private SharingUtils()
{
}
public static void shareLocation(@NonNull Context context, @NonNull Location loc)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(TEXT_MIME_TYPE);
final String subject = context.getString(R.string.share);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
final String geoUrl = Framework.nativeGetGe0Url(loc.getLatitude(), loc.getLongitude(), Framework
.nativeGetDrawScale(), "");
final String httpUrl = Framework.getHttpGe0Url(loc.getLatitude(), loc.getLongitude(), Framework
.nativeGetDrawScale(), "");
final String text = context.getString(R.string.my_position_share_sms, geoUrl, httpUrl);
intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)));
}
public static void shareMapObject(@NonNull Context context, @NonNull MapObject object, @Nullable Sponsored sponsored)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(TEXT_MIME_TYPE);
final String subject = MapObject.isOfType(MapObject.MY_POSITION, object) ?
context.getString(R.string.my_position_share_email_subject) :
context.getString(R.string.bookmark_share_email_subject);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
final String geoUrl = Framework.nativeGetGe0Url(object.getLat(), object.getLon(),
object.getScale(), object.getName());
final String httpUrl = Framework.getHttpGe0Url(object.getLat(), object.getLon(),
object.getScale(), object.getName());
final String address = TextUtils.isEmpty(object.getAddress()) ? object.getName() : object.getAddress();
final String text = context.getString(R.string.my_position_share_email, address, geoUrl, httpUrl);
intent.putExtra(Intent.EXTRA_TEXT, text);
// Sponsored is unused.
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)));
}
public static void shareBookmark(@NonNull Context context, @NonNull BookmarkInfo bookmark, @Nullable Sponsored sponsored)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(TEXT_MIME_TYPE);
final String subject = context.getString(R.string.bookmark_share_email_subject);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
final String geoUrl = Framework.nativeGetGe0Url(bookmark.getLat(), bookmark.getLon(),
bookmark.getScale(), bookmark.getName());
final String httpUrl = Framework.getHttpGe0Url(bookmark.getLat(), bookmark.getLon(),
bookmark.getScale(), bookmark.getName());
StringBuilder text = new StringBuilder();
text.append(bookmark.getName());
if (!TextUtils.isEmpty(bookmark.getAddress()))
{
text.append(UiUtils.NEW_STRING_DELIMITER);
text.append(bookmark.getAddress());
}
text.append(UiUtils.NEW_STRING_DELIMITER);
text.append(geoUrl);
text.append(UiUtils.NEW_STRING_DELIMITER);
text.append(httpUrl);
if (sponsored != null && sponsored.getType() == Sponsored.TYPE_BOOKING)
{
text.append(UiUtils.NEW_STRING_DELIMITER);
text.append(context.getString(R.string.sharing_booking));
text.append(sponsored.getUrl());
}
intent.putExtra(Intent.EXTRA_TEXT, text.toString());
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)));
}
public static void shareBookmarkFile(Context context, String fileName)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(KMZ_MIME_TYPE);
final String subject = context.getString(R.string.share_bookmarks_email_subject);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
final String text = context.getString(R.string.share_bookmarks_email_body);
intent.putExtra(Intent.EXTRA_TEXT, text.toString());
Uri fileUri = StorageUtils.getUriForFilePath(context, fileName);
intent.putExtra(android.content.Intent.EXTRA_STREAM, fileUri);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)));
}
public static void shareApplication(@NonNull Context context)
{
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(TEXT_MIME_TYPE);
final String subject = context.getString(R.string.tell_friends);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
final String text = context.getString(R.string.tell_friends_text);
intent.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(intent, context.getString(R.string.share)));
}
}

View file

@ -1,99 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import android.text.TextUtils;
abstract class BaseShareable
{
private final Activity mActivity;
protected Intent mBaseIntent;
protected String mText;
protected String mSubject;
public BaseShareable(Activity activity)
{
mActivity = activity;
mBaseIntent = new Intent(Intent.ACTION_SEND);
}
public Activity getActivity()
{
return mActivity;
}
protected void modifyIntent(Intent intent, @Nullable SharingTarget target) {}
protected Intent getBaseIntent()
{
return mBaseIntent;
}
public Intent getTargetIntent(@Nullable SharingTarget target)
{
Intent res = getBaseIntent();
if (!TextUtils.isEmpty(mText))
res.putExtra(Intent.EXTRA_TEXT, mText);
if (!TextUtils.isEmpty(mSubject))
res.putExtra(Intent.EXTRA_SUBJECT, mSubject);
String mime = getMimeType();
if (!TextUtils.isEmpty(mime))
res.setType(mime);
modifyIntent(res, target);
return res;
}
public void share(SharingTarget target)
{
Intent intent = getTargetIntent(target);
target.setupComponentName(intent);
try
{
mActivity.startActivity(intent);
} catch (ActivityNotFoundException ignored)
{
}
}
public BaseShareable setBaseIntent(Intent intent)
{
mBaseIntent = intent;
return this;
}
public BaseShareable setText(String text)
{
mText = text;
return this;
}
public BaseShareable setSubject(String subject)
{
mSubject = subject;
return this;
}
public BaseShareable setText(@StringRes int textRes)
{
mText = getActivity().getString(textRes);
return this;
}
public BaseShareable setSubject(@StringRes int subjectRes)
{
mSubject = getActivity().getString(subjectRes);
return this;
}
public abstract String getMimeType();
}

View file

@ -1,85 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.widget.placepage.Sponsored;
import com.mapswithme.util.UiUtils;
import java.util.Arrays;
class BookmarkInfoShareable<T extends ShareableInfoProvider> extends BaseShareable
{
@NonNull
private final T mProvider;
BookmarkInfoShareable(@NonNull Activity activity, @NonNull T provider,
@Nullable Sponsored sponsored)
{
super(activity);
mProvider = provider;
setSubject(R.string.bookmark_share_email_subject);
String text = makeEmailBody(activity, sponsored, getEmailBodyContent());
setText(text);
}
@NonNull
private static String makeEmailBody(@NonNull Activity activity, @Nullable Sponsored sponsored,
@NonNull Iterable<String> emailBodyContent)
{
String text = TextUtils.join(UiUtils.NEW_STRING_DELIMITER, emailBodyContent);
if (sponsored != null && sponsored.getType() == Sponsored.TYPE_BOOKING)
text = concatSponsoredText(activity, sponsored, text);
return text;
}
@NonNull
private static String concatSponsoredText(@NonNull Activity activity, @NonNull Sponsored sponsored,
@NonNull String src)
{
String mainSegment = TextUtils.join(UiUtils.NEW_STRING_DELIMITER,
Arrays.asList(src, activity.getString(R.string.sharing_booking)));
return mainSegment + sponsored.getUrl();
}
@NonNull
protected Iterable<String> getEmailBodyContent()
{
return TextUtils.isEmpty(getProvider().getAddress())
? Arrays.asList(getProvider().getName(), getGeoUrl(), getHttpUrl())
: Arrays.asList(getProvider().getName(), getProvider().getAddress(), getGeoUrl(), getHttpUrl());
}
@NonNull
protected T getProvider()
{
return mProvider;
}
@NonNull
protected final String getGeoUrl()
{
return Framework.nativeGetGe0Url(getProvider().getLat(), getProvider().getLon(),
getProvider().getScale(), getProvider().getName());
}
@NonNull
protected final String getHttpUrl()
{
return Framework.getHttpGe0Url(getProvider().getLat(), getProvider().getLon(),
getProvider().getScale(), getProvider().getName());
}
@Override
public String getMimeType()
{
return null;
}
}

View file

@ -1,35 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.Nullable;
import com.mapswithme.util.StorageUtils;
public class LocalFileShareable extends BaseShareable
{
private final String mFileName;
private final String mMimeType;
LocalFileShareable(Activity context, String fileName, String mimeType)
{
super(context);
mFileName = fileName;
mMimeType = mimeType;
}
@Override
protected void modifyIntent(Intent intent, @Nullable SharingTarget target)
{
super.modifyIntent(intent, target);
Uri fileUri = StorageUtils.getUriForFilePath(getActivity(), mFileName);
intent.putExtra(android.content.Intent.EXTRA_STREAM, fileUri);
}
@Override
public String getMimeType()
{
return mMimeType;
}
}

View file

@ -1,46 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.text.TextUtils;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.widget.placepage.Sponsored;
import java.util.Arrays;
class MapObjectShareable extends BookmarkInfoShareable<MapObject>
{
MapObjectShareable(Activity context, @NonNull MapObject mapObject, @Nullable Sponsored sponsored)
{
super(context, mapObject, sponsored);
if (MapObject.isOfType(MapObject.MY_POSITION, mapObject))
{
setSubject(R.string.my_position_share_email_subject);
String text = makeMyPositionEmailBodyContent();
setText(text);
}
}
@NonNull
private String makeMyPositionEmailBodyContent()
{
return getActivity().getString(R.string.my_position_share_email,
Framework.nativeGetAddress(getProvider().getLat(),
getProvider().getLon()),
getGeoUrl(), getHttpUrl());
}
@NonNull
@Override
protected Iterable<String> getEmailBodyContent()
{
return Arrays.asList(getProvider().getName(), getProvider().getSubtitle(), getProvider().getAddress(),
getGeoUrl(), getHttpUrl());
}
}

View file

@ -1,81 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.mapswithme.maps.R;
import com.mapswithme.maps.bookmarks.data.BookmarkInfo;
import com.mapswithme.maps.bookmarks.data.MapObject;
import com.mapswithme.maps.widget.placepage.Sponsored;
import com.mapswithme.util.Utils;
public abstract class ShareOption
{
@StringRes
private final int mNameResId;
@NonNull
private final Intent mBaseIntent;
ShareOption(int nameResId, @NonNull Intent baseIntent)
{
mNameResId = nameResId;
mBaseIntent = baseIntent;
}
public boolean isSupported(Context context)
{
return Utils.isIntentSupported(context, mBaseIntent);
}
public void shareMapObject(Activity activity, @NonNull MapObject mapObject, @Nullable Sponsored sponsored)
{
MapObjectShareable mapObjectShareable = new MapObjectShareable(activity, mapObject, sponsored);
shareObjectInternal(mapObjectShareable);
}
public void shareBookmarkObject(Activity activity, @NonNull BookmarkInfo mapObject,
@Nullable Sponsored sponsored)
{
BookmarkInfoShareable<BookmarkInfo> shareable =
new BookmarkInfoShareable<>(activity, mapObject, sponsored);
shareObjectInternal(shareable);
}
private void shareObjectInternal(@NonNull BaseShareable shareable)
{
SharingHelper.shareOutside(shareable
.setBaseIntent(new Intent(mBaseIntent)), mNameResId);
}
public static class EmailShareOption extends ShareOption
{
protected EmailShareOption()
{
super(R.string.share_by_email, new Intent(Intent.ACTION_SEND).setType(TargetUtils.TYPE_MESSAGE_RFC822));
}
}
public static class AnyShareOption extends ShareOption
{
public static final AnyShareOption ANY = new AnyShareOption();
AnyShareOption()
{
super(R.string.share, new Intent(Intent.ACTION_SEND).setType(TargetUtils.TYPE_TEXT_PLAIN));
}
public void share(Activity activity, String body)
{
SharingHelper.shareOutside(new TextShareable(activity, body));
}
public void share(Activity activity, String body, @StringRes int titleRes)
{
SharingHelper.shareOutside(new TextShareable(activity, body), titleRes);
}
}
}

View file

@ -1,18 +0,0 @@
package com.mapswithme.util.sharing;
import androidx.annotation.NonNull;
public interface ShareableInfoProvider
{
@NonNull
String getName();
double getLat();
double getLon();
double getScale();
@NonNull
String getAddress();
}

View file

@ -1,259 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.cocosw.bottomsheet.BottomSheet;
import com.google.gson.Gson;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.base.Initializable;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.bookmarks.data.BookmarkSharingResult;
import com.mapswithme.maps.dialog.DialogUtils;
import com.mapswithme.util.BottomSheetHelper;
import com.mapswithme.util.concurrency.ThreadPool;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public enum SharingHelper implements Initializable<Context>
{
INSTANCE;
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.MISC);
private static final String TAG = SharingHelper.class.getSimpleName();
private static final String PREFS_STORAGE = "sharing";
private static final String PREFS_KEY_ITEMS = "items";
private static final String KMZ_MIME_TYPE = "application/vnd.google-earth.kmz";
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private Context mContext;
@SuppressWarnings("NotNullFieldNotInitialized")
@NonNull
private SharedPreferences mPrefs;
private final Map<String, SharingTarget> mItems = new HashMap<>();
@Nullable
private ProgressDialog mProgressDialog;
@Override
public void initialize(@Nullable Context context)
{
mContext = context;
mPrefs = MwmApplication.from(context).getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE);
ThreadPool.getStorage().execute(
() ->
{
SharingTarget[] items;
String json = mPrefs.getString(PREFS_KEY_ITEMS, null);
items = parse(json);
if (items != null)
{
for (SharingTarget item : items)
INSTANCE.mItems.put(item.packageName, item);
}
});
}
@Override
public void destroy()
{
// No op.
}
private static SharingTarget[] parse(String json)
{
if (TextUtils.isEmpty(json))
return null;
try
{
return new Gson().fromJson(json, SharingTarget[].class);
} catch (Exception e)
{
return null;
}
}
private void save()
{
String json = new Gson().toJson(mItems.values());
mPrefs.edit().putString(PREFS_KEY_ITEMS, json).apply();
}
private static String guessAppName(PackageManager pm, ResolveInfo ri)
{
CharSequence name = ri.activityInfo.loadLabel(pm);
if (TextUtils.isEmpty(name))
{
name = ri.loadLabel(pm);
if (TextUtils.isEmpty(name))
name = ri.activityInfo.packageName;
}
return name.toString();
}
private List<SharingTarget> findItems(BaseShareable data)
{
Set<String> missed = new HashSet<>(mItems.keySet());
Intent it = data.getTargetIntent(null);
PackageManager pm = MwmApplication.from(mContext).getPackageManager();
List<ResolveInfo> rlist = pm.queryIntentActivities(it, 0);
final List<SharingTarget> res = new ArrayList<>(rlist.size());
for (ResolveInfo ri : rlist)
{
ActivityInfo ai = ri.activityInfo;
if (ai == null)
continue;
missed.remove(ai.packageName);
SharingTarget target = new SharingTarget(ai.packageName);
target.name = guessAppName(pm, ri);
target.activityName = ai.name;
SharingTarget original = mItems.get(ai.packageName);
if (original != null)
target.usageCount = original.usageCount;
target.drawableIcon = ai.loadIcon(pm);
res.add(target);
}
Collections.sort(res, SharingTarget::compareTo);
for (String item : missed)
mItems.remove(item);
if (!missed.isEmpty())
save();
return res;
}
public static void shareOutside(BaseShareable data)
{
shareOutside(data, R.string.share);
}
public static void shareOutside(final BaseShareable data, @StringRes int titleRes)
{
shareInternal(data, titleRes, INSTANCE.findItems(data));
}
private static void shareInternal(final BaseShareable data, int titleRes, final List<SharingTarget> items)
{
final BottomSheet.Builder builder = BottomSheetHelper.createGrid(data.getActivity(), titleRes)
.limit(R.integer.sharing_initial_rows);
int i = 0;
for (SharingTarget item : items)
builder.sheet(i++, item.drawableIcon, item.name);
builder.listener((dialog, which) ->
{
if (which < 0)
return;
SharingTarget target = items.get(which);
INSTANCE.updateItem(target);
data.share(target);
});
UiThread.runLater(builder::show, 500);
}
private void updateItem(SharingTarget item)
{
SharingTarget stored = mItems.get(item.packageName);
if (stored == null)
{
stored = new SharingTarget(item.packageName);
mItems.put(stored.packageName, stored);
}
stored.usageCount++;
save();
}
public void prepareBookmarkCategoryForSharing(@NonNull Activity context, long catId)
{
mProgressDialog = DialogUtils.createModalProgressDialog(context, R.string.please_wait);
mProgressDialog.show();
BookmarkManager.INSTANCE.prepareCategoryForSharing(catId);
}
public void onPreparedFileForSharing(@NonNull Activity context,
@NonNull BookmarkSharingResult result)
{
if (mProgressDialog != null && mProgressDialog.isShowing())
mProgressDialog.dismiss();
shareBookmarksCategory(context, result);
}
private static void shareBookmarksCategory(@NonNull Activity context,
@NonNull BookmarkSharingResult result)
{
switch (result.getCode())
{
case BookmarkSharingResult.SUCCESS:
String name = new File(result.getSharingPath()).getName();
shareOutside(new LocalFileShareable(context, result.getSharingPath(), KMZ_MIME_TYPE)
.setText(context.getString(R.string.share_bookmarks_email_body))
.setSubject(R.string.share_bookmarks_email_subject));
break;
case BookmarkSharingResult.EMPTY_CATEGORY:
DialogUtils.showAlertDialog(context, R.string.bookmarks_error_title_share_empty,
R.string.bookmarks_error_message_share_empty);
break;
case BookmarkSharingResult.ARCHIVE_ERROR:
case BookmarkSharingResult.FILE_ERROR:
DialogUtils.showAlertDialog(context, R.string.dialog_routing_system_error,
R.string.bookmarks_error_message_share_general);
String catName = BookmarkManager.INSTANCE.getCategoryById(result.getCategoryId()).getName();
LOGGER.e(TAG, "Failed to share bookmark category '" + catName + "', error code: "
+ result.getCode());
break;
default:
throw new AssertionError("Unsupported bookmark sharing code: " + result.getCode());
}
}
public static void shareViralEditor(Activity context,@StringRes int subject, @StringRes int text)
{
shareOutside(new ViralEditorShareable(context)
.setText(text)
.setSubject(subject));
}
}

View file

@ -1,44 +0,0 @@
package com.mapswithme.util.sharing;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import com.google.gson.annotations.SerializedName;
import com.mapswithme.util.Gsonable;
public class SharingTarget implements Gsonable, Comparable<SharingTarget>
{
@SerializedName("package_name")
public String packageName;
@SerializedName("usage_count")
public int usageCount;
public transient String name;
public transient String activityName;
public transient Drawable drawableIcon;
@SuppressWarnings("UnusedDeclaration")
public SharingTarget()
{}
public SharingTarget(String packageName)
{
this.packageName = packageName;
}
@Override
public int compareTo(@NonNull SharingTarget another)
{
return (another.usageCount - usageCount);
}
public void setupComponentName(Intent intent)
{
if (TextUtils.isEmpty(activityName))
intent.setPackage(packageName);
else
intent.setComponent(new ComponentName(packageName, activityName));
}
}

View file

@ -1,35 +0,0 @@
package com.mapswithme.util.sharing;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public final class TargetUtils
{
public static final String TYPE_TEXT_PLAIN = "text/plain";
static final String TYPE_MESSAGE_RFC822 = "message/rfc822";
static final String EXTRA_SMS_BODY = "sms_body";
static final String EXTRA_SMS_TEXT = Intent.EXTRA_TEXT;
static final String URI_STRING_SMS = "sms:";
private TargetUtils() {}
public static void fillSmsIntent(Intent smsIntent, String body)
{
smsIntent.setData(Uri.parse(URI_STRING_SMS));
smsIntent.putExtra(EXTRA_SMS_BODY, body);
}
@Nullable
public static Intent makeAppSettingsLocationIntent(@NonNull Context context)
{
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
if (intent.resolveActivity(context.getPackageManager()) != null)
return intent;
intent = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
return intent.resolveActivity(context.getPackageManager()) == null ? null : intent;
}
}

View file

@ -1,18 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
public class TextShareable extends BaseShareable
{
public TextShareable(Activity context, String text)
{
super(context);
setText(text);
}
@Override
public String getMimeType()
{
return TargetUtils.TYPE_TEXT_PLAIN;
}
}

View file

@ -1,55 +0,0 @@
package com.mapswithme.util.sharing;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import com.mapswithme.util.UiUtils;
public class ViralEditorShareable extends BaseShareable
{
private static final String VIRAL_TAIL = " https://omaps.app/im_get";
public ViralEditorShareable(Activity context)
{
super(context);
}
@Override
protected void modifyIntent(Intent intent, @Nullable SharingTarget target)
{
super.modifyIntent(intent, target);
// intent.putExtra(Intent.EXTRA_STREAM);
}
@Override
public String getMimeType()
{
return TargetUtils.TYPE_TEXT_PLAIN;
}
@Override
public void share(SharingTarget target)
{
Intent intent = getTargetIntent(target);
String lowerCaseName = target.activityName.toLowerCase();
setText(mText + VIRAL_TAIL);
if (lowerCaseName.contains("sms") || lowerCaseName.contains("mms"))
TargetUtils.fillSmsIntent(intent, mText);
else if (lowerCaseName.contains("twitter"))
setSubject("");
else if (!lowerCaseName.contains("mail"))
{
setText(mSubject + "\n" + mText);
setSubject("");
}
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
super.share(target);
}
}