[MA] revisar permisos via snackbar

This commit is contained in:
Luis Guzmán 2026-03-12 11:35:14 -06:00
parent a3cc0808e6
commit e784d515dc
3 changed files with 32 additions and 2 deletions

View File

@ -77,4 +77,5 @@ android {
dependencies { dependencies {
implementation 'androidx.appcompat:appcompat:1.4.1' implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'androidx.biometric:biometric:1.1.0' implementation 'androidx.biometric:biometric:1.1.0'
implementation 'com.google.android.material:material:1.11.0'
} }

View File

@ -49,6 +49,7 @@ import androidx.annotation.NonNull;
import androidx.biometric.BiometricManager; import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt; import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat; import androidx.core.content.ContextCompat;
import com.google.android.material.snackbar.Snackbar;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
@ -99,6 +100,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
private ActivityResultLauncher<Intent> vpnPermissionLauncher; private ActivityResultLauncher<Intent> vpnPermissionLauncher;
private ActivityResultLauncher<String[]> requestPermissionsLauncher; private ActivityResultLauncher<String[]> requestPermissionsLauncher;
private ActivityResultLauncher<Intent> batteryOptLauncher;
private boolean isReadingLogs = false; private boolean isReadingLogs = false;
private final Handler sizeUpdateHandler = new Handler(); private final Handler sizeUpdateHandler = new Handler();
@ -133,6 +135,14 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
} }
); );
batteryOptLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
Log.d(TAG, "Regresamos de la pantalla de ajustes de batería");
checkBatteryOptimizations();
}
);
requestPermissionsLauncher = registerForActivityResult( requestPermissionsLauncher = registerForActivityResult(
new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultContracts.RequestMultiplePermissions(),
result -> { result -> {
@ -221,6 +231,13 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
}; };
} }
private void showBatterySnackbar() {
View rootView = findViewById(android.R.id.content);
Snackbar.make(rootView, R.string.battery_opt_denied, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.fix_action, v -> checkBatteryOptimizations())
.show();
}
private void initiatePermissionChain() { private void initiatePermissionChain() {
List<String> permissions = new ArrayList<>(); List<String> permissions = new ArrayList<>();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@ -356,6 +373,16 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
// Comprobamos batería siempre que volvemos a la app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (pm != null && !pm.isIgnoringBatteryOptimizations(getPackageName())) {
// Si no está ignorado, mostramos el aviso (o Snackbar si ya se lanzó el diálogo)
Log.d(TAG, "onResume: Batería aún optimizada, mostrando aviso");
showBatterySnackbar();
}
}
if (getIntent() != null && getIntent().getBooleanExtra(VpnRecoveryReceiver.EXTRA_RECOVERY, false)) { if (getIntent() != null && getIntent().getBooleanExtra(VpnRecoveryReceiver.EXTRA_RECOVERY, false)) {
addToLog(getString(R.string.recovery_pulse_received)); addToLog(getString(R.string.recovery_pulse_received));
Intent vpnIntent = new Intent(this, TProxyService.class); Intent vpnIntent = new Intent(this, TProxyService.class);
@ -398,7 +425,7 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
else { else {
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + getPackageName())); intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent); batteryOptLauncher.launch(intent);
} }
} }
} }

View File

@ -79,5 +79,7 @@
<!-- Brand specific battery warnings --> <!-- Brand specific battery warnings -->
<string name="battery_opt_oppo_extra">\n\nOPPO/Realme detected: Please ensure you also enable \'Allow background activity\' in this app\'s settings.</string> <string name="battery_opt_oppo_extra">\n\nOPPO/Realme detected: Please ensure you also enable \'Allow background activity\' in this app\'s settings.</string>
<string name="battery_opt_xiaomi_extra">\n\nXiaomi detected: Please set battery saver to \'No restrictions\' for this app.</string> <string name="battery_opt_xiaomi_extra">\n\nXiaomi detected: Please set battery saver to \'No restrictions\' in settings.</string>
<string name="battery_opt_denied">Para que la app funcione al 100%, desactiva la optimización de batería.</string>
<string name="fix_action">SOLUCIONAR</string>
</resources> </resources>