1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-23 16:17:32 +01:00

Pop up a dialog asking about Location permissions (although others get requested too).

Conversation at https://codeberg.org/Freeyourgadget/Gadgetbridge/pulls/2675#issuecomment-455468
This commit is contained in:
Gordon Williams 2022-05-20 08:39:47 +01:00
parent 6a4b28c5e0
commit 3f053927e0

View File

@ -89,6 +89,8 @@ public class ControlCenterv2 extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener, GBActivity {
public static final int MENU_REFRESH_CODE = 1;
public static final String ACTION_REQUEST_PERMISSIONS
= "nodomain.freeyourgadget.gadgetbridge.activities.controlcenter.requestpermissions";
private static PhoneStateListener fakeStateListener;
//needed for KK compatibility
@ -123,6 +125,9 @@ public class ControlCenterv2 extends AppCompatActivity
case DeviceService.ACTION_REALTIME_SAMPLES:
handleRealtimeSample(intent.getSerializableExtra(DeviceService.EXTRA_REALTIME_SAMPLE));
break;
case ACTION_REQUEST_PERMISSIONS:
checkAndRequestPermissions(false);
break;
}
}
};
@ -234,6 +239,7 @@ public class ControlCenterv2 extends AppCompatActivity
filterLocal.addAction(GBApplication.ACTION_NEW_DATA);
filterLocal.addAction(DeviceManager.ACTION_DEVICES_CHANGED);
filterLocal.addAction(DeviceService.ACTION_REALTIME_SAMPLES);
filterLocal.addAction(ACTION_REQUEST_PERMISSIONS);
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filterLocal);
refreshPairedDevices();
@ -250,7 +256,7 @@ public class ControlCenterv2 extends AppCompatActivity
// Put up a dialog explaining why we need permissions (Polite, but also Play Store policy)
// When accepted, we open the Activity for Notification access
DialogFragment dialog = new NotifyListenerPermissionsDialogFragment();
dialog.show(getSupportFragmentManager(), "PermissionsDialogFragment");
dialog.show(getSupportFragmentManager(), "NotifyListenerPermissionsDialogFragment");
}
}
@ -263,11 +269,11 @@ public class ControlCenterv2 extends AppCompatActivity
// Put up a dialog explaining why we need permissions (Polite, but also Play Store policy)
// When accepted, we open the Activity for Notification access
DialogFragment dialog = new NotifyPolicyPermissionsDialogFragment();
dialog.show(getSupportFragmentManager(), "PermissionsDialogFragment");
dialog.show(getSupportFragmentManager(), "NotifyPolicyPermissionsDialogFragment");
}
}
// Check all the other permissions that we need to for Android M + later
checkAndRequestPermissions();
checkAndRequestPermissions(true);
}
ChangeLog cl = createChangeLog();
@ -405,7 +411,7 @@ public class ControlCenterv2 extends AppCompatActivity
}
@TargetApi(Build.VERSION_CODES.M)
private void checkAndRequestPermissions() {
private void checkAndRequestPermissions(boolean showDialogFirst) {
List<String> wantedPermissions = new ArrayList<>();
if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH) == PackageManager.PERMISSION_DENIED)
@ -478,15 +484,20 @@ public class ControlCenterv2 extends AppCompatActivity
}
}
wantedPermissions.removeAll(shouldNotAsk);
} else {
} else if (!showDialogFirst) {
// Permissions have not been asked yet, but now will be
prefs.getPreferences().edit().putBoolean("permissions_asked", true).apply();
}
if (!wantedPermissions.isEmpty()) {
GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR);
ActivityCompat.requestPermissions(this, wantedPermissions.toArray(new String[0]), 0);
GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR);
if (showDialogFirst) {
// Show a dialog - thus will then call checkAndRequestPermissions(false)
DialogFragment dialog = new LocationPermissionsDialogFragment();
dialog.show(getSupportFragmentManager(), "LocationPermissionsDialogFragment");
} else {
GB.toast(this, getString(R.string.permission_granting_mandatory), Toast.LENGTH_LONG, GB.ERROR);
ActivityCompat.requestPermissions(this, wantedPermissions.toArray(new String[0]), 0);
}
}
}
@ -579,4 +590,25 @@ public class ControlCenterv2 extends AppCompatActivity
}
}
/// Called from checkAndRequestPermissions - this puts up a dialog explaining we need permissions, and then calls checkAndRequestPermissions (via an intent) when 'ok' pressed
public static class LocationPermissionsDialogFragment extends DialogFragment {
ControlCenterv2 controlCenter;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
Context context = getContext();
builder.setMessage(context.getString(R.string.permission_location,
getContext().getString(R.string.app_name),
getContext().getString(R.string.ok)))
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(ACTION_REQUEST_PERMISSIONS);
LocalBroadcastManager.getInstance(getContext()).sendBroadcast(intent);
}
});
return builder.create();
}
}
}