Update intro screen to show toast when permission is not granted.

This commit is contained in:
Electric1447
2021-08-14 20:31:53 +03:00
parent 0285ea2dff
commit 624fa67ba7
4 changed files with 78 additions and 56 deletions
@@ -4,13 +4,16 @@ import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
@@ -24,6 +27,7 @@ public class IntroActivity extends AppCompatActivity {
private Button btn;
private static final int STEP_PERMISSION_BLUETOOTH = 1, STEP_PERMISSION_BATTERY_OPTIMIZATION = 2, STEP_PERMISSION_LOCATION = 3, STEP_PERMISSION_BACKGROUND_LOCATION = 4;
private static final int BLUETOOTH_REQUEST_CODE = 101, LOCATION_REQUEST_CODE = 102, BACKGROUND_LOCATION_REQUEST_CODE = 103;
@Override
protected void onCreate (Bundle savedInstanceState) {
@@ -77,37 +81,63 @@ public class IntroActivity extends AppCompatActivity {
int currentStep = getPermissionState() - ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 0 : 1);
int numOfSteps = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) ? ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) ? 4 : 3) : 2;
switch (getPermissionState()) {
case STEP_PERMISSION_BLUETOOTH:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bt_perm)));
btn.setOnClickListener(view -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
requestPermissions(new String[] {Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT}, 101);
});
runOnUiThread(() -> {
switch (getPermissionState()) {
case STEP_PERMISSION_BLUETOOTH:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bt_perm)));
btn.setOnClickListener(view -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
requestPermissions(new String[] {Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT}, BLUETOOTH_REQUEST_CODE);
});
break;
case STEP_PERMISSION_BATTERY_OPTIMIZATION:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bat_perm)));
btn.setOnClickListener(view -> {
Intent intent = new Intent();
getSystemService(Context.POWER_SERVICE);
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
});
break;
case STEP_PERMISSION_LOCATION:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_loc1_perm)));
btn.setOnClickListener(view -> requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST_CODE)); // Location (for BLE)
break;
case STEP_PERMISSION_BACKGROUND_LOCATION:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_loc2_perm)));
btn.setOnClickListener(view -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requestPermissions(new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, BACKGROUND_LOCATION_REQUEST_CODE);
});
break;
}
btn.setText(String.format(Locale.getDefault(), "%s (%d/%d)", getString(R.string.intro_allow), currentStep, numOfSteps));
});
}
@Override
public void onRequestPermissionsResult (int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case BLUETOOTH_REQUEST_CODE:
if (!hasAllPermissionsGranted(grantResults)) Toast.makeText(this, getString(R.string.msg_bt_perm), Toast.LENGTH_LONG).show();
break;
case STEP_PERMISSION_BATTERY_OPTIMIZATION:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_bat_perm)));
btn.setOnClickListener(view -> {
Intent intent = new Intent();
getSystemService(Context.POWER_SERVICE);
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
});
case LOCATION_REQUEST_CODE:
if (!hasAllPermissionsGranted(grantResults)) Toast.makeText(this, getString(R.string.msg_loc1_perm), Toast.LENGTH_LONG).show();
break;
case STEP_PERMISSION_LOCATION:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_loc1_perm)));
btn.setOnClickListener(view -> requestPermissions(new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 102)); // Location (for BLE)
break;
case STEP_PERMISSION_BACKGROUND_LOCATION:
msg.setText(String.format(Locale.getDefault(), "%s %d/%d: %s", getString(R.string.intro_step), currentStep, numOfSteps, getString(R.string.intro_loc2_perm)));
btn.setOnClickListener(view -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) requestPermissions(new String[] {Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 103);
});
case BACKGROUND_LOCATION_REQUEST_CODE:
if (!hasAllPermissionsGranted(grantResults)) Toast.makeText(this, getString(R.string.msg_loc2_perm), Toast.LENGTH_LONG).show();
break;
}
}
runOnUiThread(() -> btn.setText(String.format(Locale.getDefault(), "%s (%d/%d)", getString(R.string.intro_allow), currentStep, numOfSteps)));
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean hasAllPermissionsGranted (@NonNull int[] grantResults) {
for (int grantResult : grantResults)
if (grantResult == PackageManager.PERMISSION_DENIED)
return false;
return true;
}
}
@@ -1,5 +1,7 @@
package com.dosse.airpods;
import static androidx.core.content.ContextCompat.checkSelfPermission;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
@@ -7,7 +9,6 @@ import android.os.Build;
import android.os.PowerManager;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import java.util.Objects;
@@ -23,26 +24,31 @@ public class PermissionUtils {
@RequiresApi(api = Build.VERSION_CODES.S)
public static boolean getBluetoothPermissions (Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED;
return checkSelfPermission(context, Manifest.permission.BLUETOOTH_SCAN) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(context, Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED;
}
public static boolean getFineLocationPermission (Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
return checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
@RequiresApi(api = Build.VERSION_CODES.Q)
public static boolean getBackgroundLocationPermission (Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;
return checkSelfPermission(context, Manifest.permission.ACCESS_BACKGROUND_LOCATION) == PackageManager.PERMISSION_GRANTED;
}
public static boolean getLocationPermissions (Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
return getFineLocationPermission(context) && getBackgroundLocationPermission(context);
else
return getFineLocationPermission(context);
}
public static boolean checkAllPermissions (Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context) && getBluetoothPermissions(context);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context) && getBackgroundLocationPermission(context);
return getBatteryOptimizationsPermission(context) && getLocationPermissions(context) && getBluetoothPermissions(context);
else
return getBatteryOptimizationsPermission(context) && getFineLocationPermission(context);
return getBatteryOptimizationsPermission(context) && getLocationPermissions(context);
}
}
@@ -21,7 +21,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Build;
import android.os.IBinder;
import android.os.ParcelUuid;
@@ -257,20 +256,6 @@ public class PodsService extends Service {
private class NotificationThread extends Thread {
private boolean isLocationEnabled () {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
getApplicationContext();
LocationManager service = (LocationManager)getSystemService(LOCATION_SERVICE);
return service != null && service.isLocationEnabled();
} else {
try {
return Settings.Secure.getInt(getContentResolver(), Settings.Secure.LOCATION_MODE) != Settings.Secure.LOCATION_MODE_OFF;
} catch (Throwable t) {
return true;
}
}
}
private final NotificationManager mNotifyManager;
@SuppressWarnings("WeakerAccess")
@@ -292,8 +277,6 @@ public class PodsService extends Service {
boolean notificationShowing = false;
String compat = getPackageManager().getInstallerPackageName(getPackageName());
RemoteViews[] notificationLocation = new RemoteViews[] {new RemoteViews(getPackageName(), R.layout.location_disabled_big), new RemoteViews(getPackageName(), R.layout.location_disabled_small)};
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(PodsService.this, TAG);
mBuilder.setShowWhen(false);
mBuilder.setOngoing(true);
@@ -302,8 +285,8 @@ public class PodsService extends Service {
for (; ; ) {
RemoteViews[] notificationArr = new RemoteViews[] {new RemoteViews(getPackageName(), R.layout.status_big), new RemoteViews(getPackageName(), R.layout.status_small)};
RemoteViews[] notificationLocation = new RemoteViews[] {new RemoteViews(getPackageName(), R.layout.location_disabled_big), new RemoteViews(getPackageName(), R.layout.location_disabled_small)};
/*&&System.currentTimeMillis()-lastSeenConnected<TIMEOUT_CONNECTED*/
if (maybeConnected && !(leftStatus == 15 && rightStatus == 15 && caseStatus == 15)) {
if (!notificationShowing) {
OpenPodsDebugLog("Creating notification");
@@ -319,8 +302,8 @@ public class PodsService extends Service {
mNotifyManager.cancel(1);
}
// Apparently this restriction was removed in android Q
if (isLocationEnabled() || Build.VERSION.SDK_INT >= 29) {
// Apparently this restriction was removed ONLY in android Q
if (PermissionUtils.getLocationPermissions(getApplicationContext()) || Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
mBuilder.setCustomContentView(notificationArr[1]);
mBuilder.setCustomBigContentView(notificationArr[0]);
} else {
@@ -603,7 +586,7 @@ public class PodsService extends Service {
.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName())
.putExtra(Settings.EXTRA_CHANNEL_ID, notChannelID);
PendingIntent notPendingIntent = PendingIntent.getActivity(this, 1110, notIntent, 0);
PendingIntent notPendingIntent = PendingIntent.getActivity(this, 1110, notIntent, PendingIntent.FLAG_IMMUTABLE);
Notification.Builder builder = new Notification.Builder(this, notChannelID)
.setSmallIcon(R.drawable.pod_case)
@@ -14,6 +14,9 @@
<string name="intro_bat_perm" tools:ignore="MissingTranslation">Battery Optimization</string>
<string name="intro_loc1_perm" tools:ignore="MissingTranslation">Location Permission</string>
<string name="intro_loc2_perm" tools:ignore="MissingTranslation">Background Location Permission</string>
<string name="msg_bt_perm" tools:ignore="MissingTranslation">Bluetooth permissions not granted, please enable them in settings.</string>
<string name="msg_loc1_perm" tools:ignore="MissingTranslation">Location permission not granted, please enable it in settings.</string>
<string name="msg_loc2_perm" tools:ignore="MissingTranslation">Background Location permission not granted, please enable it in settings.</string>
<string name="main_description">The app will show a notification when your AirPods are connected</string>
<string name="hide_message">You can hide this app if you want</string>