SettingsActivity: Migrate permissions check to AppCompat

Pretty straightforward, I was clearly overthinking this.

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2018-04-30 17:28:13 +05:30
parent a6e530049c
commit f1fa46829d

View File

@ -3,6 +3,7 @@ package com.wireguard.android.activity;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.Preference; import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat; import android.support.v7.preference.PreferenceFragmentCompat;
@ -31,18 +32,9 @@ public class SettingsActivity extends AppCompatActivity {
private int permissionRequestCounter = 0; private int permissionRequestCounter = 0;
public synchronized void ensurePermissions(String[] permissions, PermissionRequestCallback cb) { public synchronized void ensurePermissions(String[] permissions, PermissionRequestCallback cb) {
/* TODO(MSF): since when porting to AppCompat, you'll be replacing checkSelfPermission
* and requestPermission with AppCompat.checkSelfPermission and AppCompat.requestPermission,
* you can remove this SDK_INT block entirely here, and count on the compat lib to do
* the right thing. */
if (android.os.Build.VERSION.SDK_INT < 23) {
int[] granted = new int[permissions.length];
Arrays.fill(granted, PackageManager.PERMISSION_GRANTED);
cb.done(permissions, granted);
} else {
List<String> needPermissions = new ArrayList<>(permissions.length); List<String> needPermissions = new ArrayList<>(permissions.length);
for (final String permission : permissions) { for (final String permission : permissions) {
if (getApplicationContext().checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED)
needPermissions.add(permission); needPermissions.add(permission);
} }
if (needPermissions.isEmpty()) { if (needPermissions.isEmpty()) {
@ -53,8 +45,7 @@ public class SettingsActivity extends AppCompatActivity {
} }
int idx = permissionRequestCounter++; int idx = permissionRequestCounter++;
permissionRequestCallbacks.put(idx, cb); permissionRequestCallbacks.put(idx, cb);
requestPermissions(needPermissions.toArray(new String[needPermissions.size()]), idx); ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), idx);
}
} }
@Override @Override