Skip to content

Commit 2021baa

Browse files
maskaravivekashishkumar468
authored andcommitted
Consistent handling of web and geo urls (commons-app#2750)
* Consistent handling of web and geo urls * Remove Google map intent
1 parent 7c77530 commit 2021baa

11 files changed

+59
-105
lines changed

app/src/main/java/fr/free/nrw/commons/Utils.java

+17-13
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package fr.free.nrw.commons;
22

3-
import android.content.ActivityNotFoundException;
43
import android.content.ClipData;
54
import android.content.ClipboardManager;
65
import android.content.Context;
76
import android.content.Intent;
87
import android.graphics.Bitmap;
98
import android.net.Uri;
10-
import androidx.annotation.NonNull;
11-
import androidx.browser.customtabs.CustomTabsIntent;
12-
import androidx.core.content.ContextCompat;
139
import android.view.View;
1410
import android.widget.Toast;
1511

@@ -24,7 +20,12 @@
2420
import java.util.regex.Matcher;
2521
import java.util.regex.Pattern;
2622

23+
import androidx.annotation.NonNull;
24+
import androidx.browser.customtabs.CustomTabsIntent;
25+
import androidx.core.content.ContextCompat;
26+
import fr.free.nrw.commons.location.LatLng;
2727
import fr.free.nrw.commons.settings.Prefs;
28+
import fr.free.nrw.commons.utils.ViewUtil;
2829
import timber.log.Timber;
2930

3031
import static android.widget.Toast.LENGTH_SHORT;
@@ -175,7 +176,7 @@ public static void rateApp(Context context) {
175176
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
176177
}
177178
catch (android.content.ActivityNotFoundException anfe) {
178-
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
179+
handleWebUrl(context, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName));
179180
}
180181
}
181182

@@ -204,15 +205,18 @@ public static void handleWebUrl(Context context, Uri url) {
204205
customTabsIntent.launchUrl(context, url);
205206
}
206207

207-
public static void handleGeoCoordinates(Context context, String coords) {
208-
try {
209-
Uri gmmIntentUri = Uri.parse("google.streetview:cbll=" + coords);
210-
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
211-
mapIntent.setPackage("com.google.android.apps.maps");
208+
/**
209+
* Util function to handle geo coordinates
210+
* It no longer depends on google maps and any app capable of handling the map intent can handle it
211+
* @param context
212+
* @param latLng
213+
*/
214+
public static void handleGeoCoordinates(Context context, LatLng latLng) {
215+
Intent mapIntent = new Intent(Intent.ACTION_VIEW, latLng.getGmmIntentUri());
216+
if (mapIntent.resolveActivity(context.getPackageManager()) != null) {
212217
context.startActivity(mapIntent);
213-
} catch (ActivityNotFoundException ex) {
214-
Toast toast = Toast.makeText(context, context.getString(R.string.map_application_missing), LENGTH_SHORT);
215-
toast.show();
218+
} else {
219+
ViewUtil.showShortToast(context, context.getString(R.string.map_application_missing));
216220
}
217221
}
218222

app/src/main/java/fr/free/nrw/commons/campaigns/CampaignView.java

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package fr.free.nrw.commons.campaigns;
22

33
import android.content.Context;
4-
import android.content.Intent;
54
import android.net.Uri;
6-
import androidx.annotation.NonNull;
7-
import androidx.annotation.Nullable;
85
import android.util.AttributeSet;
96
import android.view.View;
107
import android.widget.TextView;
@@ -13,9 +10,12 @@
1310
import java.text.SimpleDateFormat;
1411
import java.util.Date;
1512

13+
import androidx.annotation.NonNull;
14+
import androidx.annotation.Nullable;
1615
import butterknife.BindView;
1716
import butterknife.ButterKnife;
1817
import fr.free.nrw.commons.R;
18+
import fr.free.nrw.commons.Utils;
1919
import fr.free.nrw.commons.contributions.MainActivity;
2020
import fr.free.nrw.commons.utils.SwipableCardView;
2121
import fr.free.nrw.commons.utils.ViewUtil;
@@ -66,21 +66,11 @@ private void init() {
6666
viewHolder = new ViewHolder(rootView);
6767
setOnClickListener(view -> {
6868
if (campaign != null) {
69-
showCampaignInBrowser(campaign.getLink());
69+
Utils.handleWebUrl(getContext(), Uri.parse(campaign.getLink()));
7070
}
7171
});
7272
}
7373

74-
/**
75-
* open the url associated with the campaign in the system's default browser
76-
*/
77-
private void showCampaignInBrowser(String link) {
78-
Intent view = new Intent();
79-
view.setAction(Intent.ACTION_VIEW);
80-
view.setData(Uri.parse(link));
81-
getContext().startActivity(view);
82-
}
83-
8474
public class ViewHolder {
8575

8676
@BindView(R.id.tv_title) TextView tvTitle;

app/src/main/java/fr/free/nrw/commons/category/CategoryDetailsActivity.java

+7-17
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,31 @@
44
import android.content.Intent;
55
import android.database.DataSetObserver;
66
import android.os.Bundle;
7-
import com.google.android.material.tabs.TabLayout;
8-
import androidx.fragment.app.Fragment;
9-
import androidx.fragment.app.FragmentManager;
10-
import androidx.viewpager.widget.ViewPager;
117
import android.view.Menu;
128
import android.view.MenuInflater;
139
import android.view.MenuItem;
1410
import android.view.View;
1511
import android.widget.AdapterView;
1612
import android.widget.FrameLayout;
17-
import android.widget.Toast;
13+
14+
import com.google.android.material.tabs.TabLayout;
1815

1916
import java.util.ArrayList;
2017
import java.util.List;
2118

19+
import androidx.fragment.app.Fragment;
20+
import androidx.fragment.app.FragmentManager;
21+
import androidx.viewpager.widget.ViewPager;
2222
import butterknife.BindView;
2323
import butterknife.ButterKnife;
2424
import fr.free.nrw.commons.Media;
2525
import fr.free.nrw.commons.PageTitle;
2626
import fr.free.nrw.commons.R;
27+
import fr.free.nrw.commons.Utils;
2728
import fr.free.nrw.commons.explore.ViewPagerAdapter;
2829
import fr.free.nrw.commons.media.MediaDetailPagerFragment;
2930
import fr.free.nrw.commons.theme.NavigationBaseActivity;
3031

31-
import static android.widget.Toast.LENGTH_SHORT;
32-
3332
/**
3433
* This activity displays details of a particular category
3534
* Its generic and simply takes the name of category name in its start intent to load all images, subcategories in
@@ -220,16 +219,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
220219
// Handle item selection
221220
switch (item.getItemId()) {
222221
case R.id.menu_browser_current_category:
223-
Intent viewIntent = new Intent();
224-
viewIntent.setAction(Intent.ACTION_VIEW);
225-
viewIntent.setData(new PageTitle(categoryName).getCanonicalUri());
226-
//check if web browser available
227-
if (viewIntent.resolveActivity(this.getPackageManager()) != null) {
228-
startActivity(viewIntent);
229-
} else {
230-
Toast toast = Toast.makeText(this, getString(R.string.no_web_browser), LENGTH_SHORT);
231-
toast.show();
232-
}
222+
Utils.handleWebUrl(this, new PageTitle(categoryName).getCanonicalUri());
233223
return true;
234224
default:
235225
return super.onOptionsItemSelected(item);

app/src/main/java/fr/free/nrw/commons/location/LatLng.java

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.net.Uri;
55
import android.os.Parcel;
66
import android.os.Parcelable;
7+
78
import androidx.annotation.NonNull;
89

910
/**

app/src/main/java/fr/free/nrw/commons/media/MediaDetailFragment.java

+8-31
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import android.os.Bundle;
1111
import android.text.Editable;
1212
import android.text.Html;
13-
import android.text.TextUtils;
1413
import android.text.TextWatcher;
1514
import android.util.TypedValue;
1615
import android.view.LayoutInflater;
@@ -51,18 +50,17 @@
5150
import fr.free.nrw.commons.delete.DeleteTask;
5251
import fr.free.nrw.commons.delete.ReasonBuilder;
5352
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
54-
import fr.free.nrw.commons.location.LatLng;
5553
import fr.free.nrw.commons.mwapi.MediaWikiApi;
5654
import fr.free.nrw.commons.ui.widget.CompatTextView;
5755
import fr.free.nrw.commons.utils.DateUtils;
56+
import fr.free.nrw.commons.utils.StringUtils;
5857
import io.reactivex.Single;
5958
import io.reactivex.android.schedulers.AndroidSchedulers;
6059
import io.reactivex.schedulers.Schedulers;
6160
import timber.log.Timber;
6261

6362
import static android.view.View.GONE;
6463
import static android.view.View.VISIBLE;
65-
import static android.widget.Toast.LENGTH_SHORT;
6664

6765
public class MediaDetailFragment extends CommonsDaggerSupportFragment {
6866

@@ -371,8 +369,9 @@ private void setTextFields(Media media) {
371369

372370
@OnClick(R.id.mediaDetailLicense)
373371
public void onMediaDetailLicenceClicked(){
374-
if (!TextUtils.isEmpty(licenseLink(media))) {
375-
openWebBrowser(licenseLink(media));
372+
String url = licenseLink(media);
373+
if (!StringUtils.isNullOrWhiteSpace(url) && getActivity() != null) {
374+
Utils.handleWebUrl(getActivity(), Uri.parse(url));
376375
} else {
377376
if (isCategoryImage) {
378377
Timber.d("Unable to fetch license URL for %s", media.getLicense());
@@ -385,8 +384,8 @@ public void onMediaDetailLicenceClicked(){
385384

386385
@OnClick(R.id.mediaDetailCoordinates)
387386
public void onMediaDetailCoordinatesClicked(){
388-
if (media.getCoordinates() != null) {
389-
openMap(media.getCoordinates());
387+
if (media.getCoordinates() != null && getActivity() != null) {
388+
Utils.handleGeoCoordinates(getActivity(), media.getCoordinates());
390389
}
391390
}
392391

@@ -484,8 +483,8 @@ private void onDeleteClicked(Spinner spinner) {
484483

485484
@OnClick(R.id.seeMore)
486485
public void onSeeMoreClicked(){
487-
if (nominatedForDeletion.getVisibility()== VISIBLE) {
488-
openWebBrowser(media.getFilePageTitle().getMobileUri().toString());
486+
if (nominatedForDeletion.getVisibility() == VISIBLE && getActivity() != null) {
487+
Utils.handleWebUrl(getActivity(), media.getFilePageTitle().getMobileUri());
489488
}
490489
}
491490

@@ -623,26 +622,4 @@ String licenseLink(Media media) {
623622
}
624623
}
625624

626-
private void openWebBrowser(String url) {
627-
Intent browser = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
628-
//check if web browser available
629-
if (browser.resolveActivity(getActivity().getPackageManager()) != null) {
630-
startActivity(browser);
631-
} else {
632-
Toast toast = Toast.makeText(getContext(), getString(R.string.no_web_browser), LENGTH_SHORT);
633-
toast.show();
634-
}
635-
636-
}
637-
638-
private void openMap(LatLng coordinates) {
639-
//Open map app at given position
640-
Uri gmmIntentUri = Uri.parse(
641-
"geo:0,0?q=" + coordinates.getLatitude() + "," + coordinates.getLongitude());
642-
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
643-
644-
if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
645-
startActivity(mapIntent);
646-
}
647-
}
648625
}

app/src/main/java/fr/free/nrw/commons/media/MediaDetailPagerFragment.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99
import android.os.Bundle;
1010
import android.os.Environment;
1111
import android.os.Handler;
12-
import com.google.android.material.snackbar.Snackbar;
13-
import androidx.core.app.ActivityCompat;
14-
import androidx.fragment.app.Fragment;
15-
import androidx.fragment.app.FragmentManager;
16-
import androidx.fragment.app.FragmentStatePagerAdapter;
17-
import androidx.core.content.ContextCompat;
18-
import androidx.viewpager.widget.ViewPager;
1912
import android.view.LayoutInflater;
2013
import android.view.Menu;
2114
import android.view.MenuInflater;
@@ -24,9 +17,17 @@
2417
import android.view.ViewGroup;
2518
import android.widget.Toast;
2619

20+
import com.google.android.material.snackbar.Snackbar;
21+
2722
import javax.inject.Inject;
2823
import javax.inject.Named;
2924

25+
import androidx.core.app.ActivityCompat;
26+
import androidx.core.content.ContextCompat;
27+
import androidx.fragment.app.Fragment;
28+
import androidx.fragment.app.FragmentManager;
29+
import androidx.fragment.app.FragmentStatePagerAdapter;
30+
import androidx.viewpager.widget.ViewPager;
3031
import butterknife.BindView;
3132
import butterknife.ButterKnife;
3233
import fr.free.nrw.commons.Media;

app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -847,13 +847,7 @@ private void passInfoToSheet(Place place) {
847847
wikidataButton.setVisibility(place.hasWikidataLink()?View.VISIBLE:View.GONE);
848848
wikidataButton.setOnClickListener(view -> openWebView(this.place.siteLinks.getWikidataLink()));
849849

850-
directionsButton.setOnClickListener(view -> {
851-
//Open map app at given position
852-
Intent mapIntent = new Intent(Intent.ACTION_VIEW, this.place.location.getGmmIntentUri());
853-
if (mapIntent.resolveActivity(getActivity().getPackageManager()) != null) {
854-
startActivity(mapIntent);
855-
}
856-
});
850+
directionsButton.setOnClickListener(view -> Utils.handleGeoCoordinates(getActivity(), this.place.getLocation()));
857851

858852
commonsButton.setVisibility(this.place.hasCommonsLink()?View.VISIBLE:View.GONE);
859853
commonsButton.setOnClickListener(view -> openWebView(this.place.siteLinks.getCommonsLink()));

app/src/main/java/fr/free/nrw/commons/nearby/PlaceRenderer.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
import android.content.Intent;
44
import android.net.Uri;
5-
import androidx.transition.TransitionManager;
6-
import androidx.fragment.app.Fragment;
7-
import androidx.appcompat.app.AlertDialog;
8-
import androidx.recyclerview.widget.LinearLayoutManager;
9-
import androidx.appcompat.widget.PopupMenu;
10-
import androidx.recyclerview.widget.RecyclerView;
115
import android.view.LayoutInflater;
126
import android.view.MenuItem;
137
import android.view.View;
@@ -24,6 +18,12 @@
2418
import javax.inject.Inject;
2519
import javax.inject.Named;
2620

21+
import androidx.appcompat.app.AlertDialog;
22+
import androidx.appcompat.widget.PopupMenu;
23+
import androidx.fragment.app.Fragment;
24+
import androidx.recyclerview.widget.LinearLayoutManager;
25+
import androidx.recyclerview.widget.RecyclerView;
26+
import androidx.transition.TransitionManager;
2727
import butterknife.BindView;
2828
import butterknife.ButterKnife;
2929
import fr.free.nrw.commons.R;
@@ -223,13 +223,7 @@ public void render() {
223223

224224
icon.setImageResource(place.getLabel().getIcon());
225225

226-
directionsButton.setOnClickListener(view -> {
227-
//Open map app at given position
228-
Intent mapIntent = new Intent(Intent.ACTION_VIEW, place.location.getGmmIntentUri());
229-
if (mapIntent.resolveActivity(view.getContext().getPackageManager()) != null) {
230-
view.getContext().startActivity(mapIntent);
231-
}
232-
});
226+
directionsButton.setOnClickListener(view -> Utils.handleGeoCoordinates(getContext(), this.place.getLocation()));
233227

234228
iconOverflow.setVisibility(showMenu() ? View.VISIBLE : View.GONE);
235229
iconOverflow.setOnClickListener(v -> popupMenuListener());

app/src/main/java/fr/free/nrw/commons/upload/UploadActivity.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import fr.free.nrw.commons.contributions.ContributionController;
5858
import fr.free.nrw.commons.filepicker.UploadableFile;
5959
import fr.free.nrw.commons.kvstore.JsonKvStore;
60+
import fr.free.nrw.commons.location.LatLng;
6061
import fr.free.nrw.commons.mwapi.MediaWikiApi;
6162
import fr.free.nrw.commons.nearby.Place;
6263
import fr.free.nrw.commons.theme.BaseActivity;
@@ -415,7 +416,7 @@ public void hideProgressDialog() {
415416
}
416417

417418
@Override
418-
public void launchMapActivity(String decCoords) {
419+
public void launchMapActivity(LatLng decCoords) {
419420
Utils.handleGeoCoordinates(this, decCoords);
420421
}
421422

app/src/main/java/fr/free/nrw/commons/upload/UploadPresenter.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import fr.free.nrw.commons.contributions.Contribution;
88
import fr.free.nrw.commons.filepicker.UploadableFile;
99
import fr.free.nrw.commons.kvstore.JsonKvStore;
10+
import fr.free.nrw.commons.location.LatLng;
1011
import fr.free.nrw.commons.nearby.Place;
1112
import fr.free.nrw.commons.settings.Prefs;
1213
import fr.free.nrw.commons.utils.CustomProxy;
@@ -225,7 +226,7 @@ void handleSubmit(CategoriesModel categoriesModel) {
225226
void openCoordinateMap() {
226227
GPSExtractor gpsObj = uploadModel.getCurrentItem().getGpsCoords();
227228
if (gpsObj != null && gpsObj.imageCoordsExists) {
228-
view.launchMapActivity(gpsObj.getDecLatitude() + "," + gpsObj.getDecLongitude());
229+
view.launchMapActivity(new LatLng(gpsObj.getDecLatitude(), gpsObj.getDecLongitude(), 0.0f));
229230
}
230231
}
231232

0 commit comments

Comments
 (0)