Attempt to get app to work properly when in the background in newer versions of Android, with assistance from qwen3.6:35b. Testing TBD.
This commit is contained in:
1 parent
1786f03577
commit
3965aa6d88
5 files changed
+221
-154
No files matched your search
@@ -5,13 +5,14 @@ import android.content.DialogInterface;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.media.Ringtone;
|
||||
import android.media.RingtoneManager;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.provider.Settings;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.util.Log;
|
||||
@@ -91,48 +92,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
if (DEBUG) Log.d(tag, "Got preferences.");
|
||||
|
||||
// Sound
|
||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
|
||||
if (DEBUG) Log.d(tag, "ringtoneUri=" + ringtoneUri.toString());
|
||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
||||
if (mediaPlayer == null) {
|
||||
if (DEBUG) Log.d(tag, "Media Player is null, trying alarm");
|
||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
|
||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
||||
}
|
||||
if (mediaPlayer == null) {
|
||||
if (DEBUG) Log.d(tag, "Media Player is null, trying ringtone");
|
||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
|
||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
||||
}
|
||||
if (mediaPlayer == null) {
|
||||
if (DEBUG) Log.d(tag, "Media Player is null, trying all");
|
||||
ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL);
|
||||
mediaPlayer = MediaPlayer.create(this, ringtoneUri);
|
||||
}
|
||||
if (mediaPlayer == null) {
|
||||
if (DEBUG) Log.d(tag, "Media Player is null, trying local sound byte");
|
||||
mediaPlayer = MediaPlayer.create(this, R.raw.chime_sound_7143);
|
||||
}
|
||||
if (mediaPlayer == null) {
|
||||
String message = "Could not find a notification, alarm, or ringtone to play.";
|
||||
message += " App will not be able to function without one of these.";
|
||||
message += " Please exit and install a system notification sound.";
|
||||
|
||||
new AlertDialog.Builder(MainActivity.this)
|
||||
.setTitle("Error")
|
||||
.setMessage(message)
|
||||
.setPositiveButton("Exit", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
finish();
|
||||
}
|
||||
})
|
||||
.setCancelable(false)
|
||||
.show()
|
||||
;
|
||||
} else {
|
||||
if (DEBUG) Log.d(tag, "mediaPlayer=" + mediaPlayer.toString());
|
||||
// Check exact alarm permission for Android 12+
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !Alert.isExactAlarmAllowed(this)) {
|
||||
if (DEBUG) Log.w(tag, "Exact alarm permission not granted, requesting...");
|
||||
Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
if (DEBUG){
|
||||
@@ -383,6 +347,20 @@ public class MainActivity extends AppCompatActivity {
|
||||
setLoopInterval();
|
||||
resetTimeLeft();
|
||||
setStartTime();
|
||||
|
||||
// Start the background alert service using Alert manager
|
||||
long interval = longLoopInterval > 1000 ? longLoopInterval : 60000; // Minimum 1 minute interval for alarms
|
||||
if (Alert.isExactAlarmAllowed(this)) {
|
||||
Alert.start(this, interval);
|
||||
} else {
|
||||
if (DEBUG) Log.w(tag, "Cannot start alert: Exact alarm permission denied.");
|
||||
new AlertDialog.Builder(MainActivity.this)
|
||||
.setTitle("Permission Required")
|
||||
.setMessage("Please grant 'All files access' or 'Alarm' permissions in system settings for this feature to work.")
|
||||
.setPositiveButton("OK", (d, w) -> d.dismiss())
|
||||
.show();
|
||||
}
|
||||
|
||||
}
|
||||
else if (buttonText.equals(getString(R.string.btnPause))){
|
||||
btnStartStop.setText(getString(R.string.btnResume));
|
||||
@@ -665,6 +643,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
String tag = TAG + "resetAll";
|
||||
if (DEBUG) Log.d(tag, "Starting");
|
||||
|
||||
// Stop the background alert service when resetting
|
||||
Alert.stop(this);
|
||||
|
||||
if (DEBUG) Log.d(tag, "Resetting screen values...");
|
||||
setChronometer(0);
|
||||
setEditTexts("");
|
||||
@@ -745,8 +726,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
String tag = TAG + "onDestroy";
|
||||
if (DEBUG) Log.d(tag, "Starting");
|
||||
|
||||
resetAll();
|
||||
|
||||
// Stop the background alert service when destroying activity
|
||||
Alert.stop(this);
|
||||
|
||||
if (DEBUG) Log.d(tag, "Done with local code, calling super..");
|
||||
|
||||
super.onDestroy();
|
||||
|
||||
Reference in new issue
Block a user