mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-27 04:43:54 +01:00
Merge pull request #707 from misaochan/check-location-permission
Check for location permissions when loading Nearby
This commit is contained in:
commit
ab487ad4f9
5 changed files with 127 additions and 18 deletions
|
|
@ -55,7 +55,7 @@ android {
|
||||||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
|
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
|
||||||
}
|
}
|
||||||
debug {
|
debug {
|
||||||
testCoverageEnabled true
|
testCoverageEnabled true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,4 +64,9 @@ android {
|
||||||
disable 'ExtraTranslation'
|
disable 'ExtraTranslation'
|
||||||
abortOnError false
|
abortOnError false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//FIXME: Temporary fix for https://github.com/commons-app/apps-android-commons/issues/709
|
||||||
|
configurations.all {
|
||||||
|
resolutionStrategy.force 'com.android.support:support-annotations:25.2.0'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,20 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
|
import android.Manifest;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
import android.location.LocationManager;
|
import android.location.LocationManager;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v4.app.FragmentTransaction;
|
import android.support.v4.app.FragmentTransaction;
|
||||||
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.support.v7.app.AlertDialog;
|
import android.support.v7.app.AlertDialog;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
|
|
@ -38,6 +44,7 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
@BindView(R.id.progressBar)
|
@BindView(R.id.progressBar)
|
||||||
ProgressBar progressBar;
|
ProgressBar progressBar;
|
||||||
private boolean isMapViewActive = false;
|
private boolean isMapViewActive = false;
|
||||||
|
private static final int LOCATION_REQUEST = 1;
|
||||||
|
|
||||||
private LocationServiceManager locationManager;
|
private LocationServiceManager locationManager;
|
||||||
private LatLng curLatLang;
|
private LatLng curLatLang;
|
||||||
|
|
@ -52,13 +59,8 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
if (getSupportActionBar() != null) {
|
if (getSupportActionBar() != null) {
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
}
|
}
|
||||||
|
checkLocationPermission();
|
||||||
bundle = new Bundle();
|
bundle = new Bundle();
|
||||||
locationManager = new LocationServiceManager(this);
|
|
||||||
locationManager.registerLocationManager();
|
|
||||||
curLatLang = locationManager.getLatestLocation();
|
|
||||||
nearbyAsyncTask = new NearbyAsyncTask(this);
|
|
||||||
nearbyAsyncTask.execute();
|
|
||||||
initDrawer();
|
initDrawer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -89,6 +91,51 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void startLookingForNearby() {
|
||||||
|
locationManager = new LocationServiceManager(this);
|
||||||
|
locationManager.registerLocationManager();
|
||||||
|
curLatLang = locationManager.getLatestLocation();
|
||||||
|
nearbyAsyncTask = new NearbyAsyncTask(this);
|
||||||
|
nearbyAsyncTask.execute();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkLocationPermission() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||||
|
if (ContextCompat.checkSelfPermission(this,
|
||||||
|
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
startLookingForNearby();
|
||||||
|
} else {
|
||||||
|
ActivityCompat.requestPermissions(this,
|
||||||
|
new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
startLookingForNearby();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
||||||
|
switch (requestCode) {
|
||||||
|
case LOCATION_REQUEST: {
|
||||||
|
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
startLookingForNearby();
|
||||||
|
} else {
|
||||||
|
//If permission not granted, go to page that says Nearby Places cannot be displayed
|
||||||
|
if (nearbyAsyncTask != null) {
|
||||||
|
nearbyAsyncTask.cancel(true);
|
||||||
|
}
|
||||||
|
if (progressBar != null) {
|
||||||
|
progressBar.setVisibility(View.GONE);
|
||||||
|
}
|
||||||
|
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
|
||||||
|
Fragment noPermissionsFragment = new NoPermissionsFragment();
|
||||||
|
fragmentTransaction.replace(R.id.container, noPermissionsFragment);
|
||||||
|
fragmentTransaction.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected void checkGps() {
|
protected void checkGps() {
|
||||||
LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
|
LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
|
||||||
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
||||||
|
|
@ -128,16 +175,18 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showMapView() {
|
private void showMapView() {
|
||||||
if (!isMapViewActive) {
|
if (nearbyAsyncTask != null) {
|
||||||
isMapViewActive = true;
|
if (!isMapViewActive) {
|
||||||
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
|
isMapViewActive = true;
|
||||||
setMapFragment();
|
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
|
||||||
}
|
setMapFragment();
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
isMapViewActive = false;
|
isMapViewActive = false;
|
||||||
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
|
if (nearbyAsyncTask.getStatus() == AsyncTask.Status.FINISHED) {
|
||||||
setListFragment();
|
setListFragment();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -151,7 +200,9 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
nearbyAsyncTask.cancel(true);
|
if (nearbyAsyncTask != null) {
|
||||||
|
nearbyAsyncTask.cancel(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void refreshView() {
|
protected void refreshView() {
|
||||||
|
|
@ -166,7 +217,9 @@ public class NearbyActivity extends NavigationBaseActivity {
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
locationManager.unregisterLocationManager();
|
if (locationManager != null) {
|
||||||
|
locationManager.unregisterLocationManager();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
|
private class NearbyAsyncTask extends AsyncTask<Void, Integer, List<Place>> {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
|
import butterknife.ButterKnife;
|
||||||
|
import fr.free.nrw.commons.R;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tells user that Nearby Places cannot be displayed if location permissions are denied
|
||||||
|
*/
|
||||||
|
public class NoPermissionsFragment extends Fragment {
|
||||||
|
|
||||||
|
public NoPermissionsFragment() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
||||||
|
Bundle savedInstanceState) {
|
||||||
|
Timber.d("NoPermissionsFragment created");
|
||||||
|
View view = inflater.inflate(R.layout.fragment_no_permissions, container, false);
|
||||||
|
ButterKnife.bind(this, view);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
app/src/main/res/layout/fragment_no_permissions.xml
Normal file
19
app/src/main/res/layout/fragment_no_permissions.xml
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
tools:context="fr.free.nrw.commons.nearby.NoPermissionsFragment">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/textView2"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:gravity="center"
|
||||||
|
android:paddingLeft="30dp"
|
||||||
|
android:paddingRight="30dp"
|
||||||
|
android:text="@string/nearby_needs_permissions"
|
||||||
|
android:textAlignment="center"
|
||||||
|
android:textSize="18sp" />
|
||||||
|
|
||||||
|
|
||||||
|
</FrameLayout>
|
||||||
|
|
@ -194,7 +194,9 @@ Tap this message (or hit back) to skip this step.</string>
|
||||||
<string name="navigation_item_logout">Logout</string>
|
<string name="navigation_item_logout">Logout</string>
|
||||||
<string name="navigation_item_info">Tutorial</string>
|
<string name="navigation_item_info">Tutorial</string>
|
||||||
|
|
||||||
|
<string name="nearby_needs_permissions">Nearby places cannot be displayed without location permissions</string>
|
||||||
<string name="no_description_found">no description found</string>
|
<string name="no_description_found">no description found</string>
|
||||||
<string name="nearby_info_menu_commons_article">Commons Article</string>
|
<string name="nearby_info_menu_commons_article">Commons Article</string>
|
||||||
<string name="nearby_info_menu_wikidata_article">Wikidata item</string>
|
<string name="nearby_info_menu_wikidata_article">Wikidata item</string>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue