Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

[MRG] Add the location permission for android 9. #286

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
✨ feat: added permission check to receiver.
  • Loading branch information
huangyz0918 committed Jul 29, 2019
commit 9a4539e5d13556d37eddbc4692049b654191a5eb
4 changes: 4 additions & 0 deletions skunkworks_crow/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ dependencies {

//MPAndroidChart
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

// Permissions Dispatcher
implementation "org.permissionsdispatcher:permissionsdispatcher:4.5.0"
lakshyagupta21 marked this conversation as resolved.
Show resolved Hide resolved
annotationProcessor "org.permissionsdispatcher:permissionsdispatcher-processor:4.5.0"
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package org.odk.share.utilities;


import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.provider.Settings;

import androidx.appcompat.app.AlertDialog;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import org.odk.share.R;

Expand All @@ -24,9 +20,6 @@
*/
public class PermissionUtils {

private static final int PERMISSIONS_REQUEST_FINE_LOCATION = 0x110;
private static final int PERMISSIONS_REQUEST_COARSE_LOCATION = 0x111;

private PermissionUtils() {
}

Expand Down Expand Up @@ -63,31 +56,6 @@ public static void showLocationAlertDialog(Activity targetActivity) {
builder.setCancelable(false);
builder.show();
}

/**
* Request for the location permission.
*/
public static void requestLocationPermission(Activity activity) {
// ACCESS_FINE_LOCATION
if (ContextCompat.checkSelfPermission(activity.getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_FINE_LOCATION);
ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_FINE_LOCATION);
}

// ACCESS_COARSE_LOCATION
if (ContextCompat.checkSelfPermission(activity.getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
PERMISSIONS_REQUEST_COARSE_LOCATION);
ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_COARSE_LOCATION);
}
}
}


Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package org.odk.share.views.ui.bluetooth;

import android.Manifest;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand All @@ -28,15 +32,23 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.OnNeverAskAgain;
import permissions.dispatcher.OnPermissionDenied;
import permissions.dispatcher.OnShowRationale;
import permissions.dispatcher.PermissionRequest;
import permissions.dispatcher.RuntimePermissions;
import timber.log.Timber;

/**
* Receive activity, for testing, needs refactor.
*
* @author huangyz0918 (huangyz0918@gmail.com)
*/
@RuntimePermissions
public class BtReceiverActivity extends InjectableActivity implements
BluetoothReceiver.BluetoothReceiverListener, BluetoothListAdapter.OnDeviceClickListener {

Expand Down Expand Up @@ -83,12 +95,29 @@ protected void onCreate(Bundle savedInstanceState) {
// checking for if bluetooth enabled
if (!BluetoothUtils.isBluetoothEnabled()) {
BluetoothUtils.enableBluetooth();
updateDeviceList();
updateDeviceListWithPermissionCheck();
}

setupScanningDialog();
}

/**
* Init the basic events for our views.
*/
private void initEvents() {
progressDialog = new ProgressDialog(this);
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(bluetoothListAdapter);
bluetoothReceiver = new BluetoothReceiver(this, this);

updateDeviceListWithPermissionCheck();
}

/**
* build a new progress dialog waiting for the scanning progress.
*/
Expand All @@ -106,33 +135,74 @@ private void setupScanningDialog() {
/**
* Rescan the bluetooth devices and update the list.
*/
public void updateDeviceList() {
@NeedsPermission({Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION})
void updateDeviceList() {
if (!bluetoothAdapter.isDiscovering()) {
bluetoothAdapter.startDiscovery();
}
bluetoothListAdapter.notifyDataSetChanged();
}

private void initEvents() {
bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
linearLayoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setAdapter(bluetoothListAdapter);
bluetoothReceiver = new BluetoothReceiver(this, this);
bluetoothAdapter.startDiscovery();

// click to refresh the devices list.
btnRefresh.setOnClickListener((View v) -> {
if (BluetoothUtils.isBluetoothEnabled()) {
updateDeviceList();
} else {
BluetoothUtils.enableBluetooth();
Toast.makeText(this, "bluetooth has been disabled, turning on...", Toast.LENGTH_SHORT).show();
}
});
private void updateDeviceListWithPermissionCheck() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
BtReceiverActivityPermissionsDispatcher.updateDeviceListWithPermissionCheck(this);
} else {
updateDeviceList();
}
}

/**
* Show the rationale dialog for location permission, only if the permission was granted,
* we can start using the bluetooth.
*/
@OnShowRationale({Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION})
void showLocationPermissionDialog(final PermissionRequest request) {
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage(R.string.permission_location_rationale)
.setPositiveButton(R.string.button_allow, (dialog, button) -> request.proceed())
.setNegativeButton(R.string.button_deny, (dialog, button) -> request.cancel())
.show();
}

/**
* If the permission was denied, finishing this activity.
*/
@OnPermissionDenied({Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION})
void showDeniedForLocation() {
Toast.makeText(this, R.string.permission_location_never_ask, Toast.LENGTH_SHORT).show();
finish();
}

/**
* If clicked the "never ask", we should show a toast to guide user.
*/
@OnNeverAskAgain({Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION})
void showNeverAskForCamera() {
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
Toast.makeText(this, R.string.permission_location_never_ask, Toast.LENGTH_SHORT).show();
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
BtReceiverActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
}

/**
* Clicking to refresh the devices list.
*/
@OnClick(R.id.btn_refresh)
public void refresh() {
if (BluetoothUtils.isBluetoothEnabled()) {
updateDeviceListWithPermissionCheck();
} else {
BluetoothUtils.enableBluetooth();
Toast.makeText(this, "bluetooth has been disabled, turning on...", Toast.LENGTH_SHORT).show();
}
}

/**
Expand Down Expand Up @@ -217,6 +287,9 @@ public void onDiscoveryFinished() {
checkEmptyList();
}

/**
* Clicking the item to connect.
*/
@Override
public void onItemClick(BluetoothDevice device) {
if (BluetoothUtils.isBluetoothEnabled()) {
Expand All @@ -225,7 +298,6 @@ public void onItemClick(BluetoothDevice device) {
}

receiverService.startBtDownloading(device.getAddress());
progressDialog = new ProgressDialog(this);
progressDialog.setTitle(getString(R.string.connecting_title));
progressDialog.setMessage(getString(R.string.connecting_message));
progressDialog.setIndeterminate(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.odk.share.dao.TransferDao;
import org.odk.share.utilities.ActivityUtils;
import org.odk.share.utilities.DialogUtils;
import org.odk.share.utilities.PermissionUtils;
import org.odk.share.views.listeners.ItemClickListener;
import org.odk.share.views.ui.about.AboutActivity;
import org.odk.share.views.ui.common.basecursor.BaseCursorViewHolder;
Expand Down Expand Up @@ -99,11 +98,6 @@ protected void onCreate(Bundle savedInstanceState) {
//check the storage permission and start the loader
setUpLoader();

//checking the location permission for bluetooth for android 9.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
PermissionUtils.requestLocationPermission(this);
}

addListItemDivider();
}

Expand Down
4 changes: 4 additions & 0 deletions skunkworks_crow/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,8 @@
<string name="dev_already_connected">already connected to the device, start transferring now&#8230;</string>
<string name="tv_form_send_success">form sent with success! \n</string>
<string name="bluetooth_no_device_scanned">No devices are available. \n Tap refresh to search again.</string>
<string name="button_allow">Allow</string>
<string name="button_deny">Deny</string>
<string name="permission_location_rationale">In Android 9, we need the location permission to make the bluetooth work, please give us the location permission and move forward.</string>
huangyz0918 marked this conversation as resolved.
Show resolved Hide resolved
<string name="permission_location_never_ask">You need to get location permission to enable bluetooth feature!</string>
</resources>