better handling notifications. fix 'null' notification message

This commit is contained in:
Alexey Kuznetsov 2022-03-09 21:25:11 +03:00
commit 115dbe9977
4 changed files with 40 additions and 39 deletions

View file

@ -53,7 +53,7 @@ android {
dependencies {
testImplementation 'junit:junit:4.12'
implementation ('com.github.axet:android-audio-library:1.1.8') // implementation project(':android-audio-library')
implementation ('com.github.axet:android-audio-library:1.1.9') // implementation project(':android-audio-library')
implementation ('com.github.axet:wget:1.7.0') { exclude group: 'org.json', module: 'json' }
assets('com.google.android.exoplayer:exoplayer:2.7.3') { exclude group: 'com.android.support' }
}

View file

@ -136,7 +136,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
if (msg.what == RecordingStorage.END) {
pitch.drawEnd();
if (!recording.interrupt.get()) {
stopRecording(getString(R.string.recording_status_pause));
stopRecording(getString(R.string.recording_status_pause), false);
String text = "Error reading from stream";
if (Build.VERSION.SDK_INT >= 28)
muted = RecordingActivity.startActivity(RecordingActivity.this, text, getString(R.string.mic_muted_pie));
@ -269,7 +269,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
@Override
public void onDisconnected() {
if (recording.thread != null) {
stopRecording(getString(R.string.hold_by_bluetooth));
stopRecording(getString(R.string.hold_by_bluetooth), false);
super.onDisconnected();
}
}
@ -305,7 +305,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
case TelephonyManager.CALL_STATE_OFFHOOK:
wasRinging = true;
if (recording.thread != null) {
stopRecording(getString(R.string.hold_by_call));
stopRecording(getString(R.string.hold_by_call), false);
pausedByCall = true;
}
break;
@ -452,7 +452,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
msg = getString(R.string.recording_status_recording);
else
msg = getString(R.string.recording_status_encoding);
stopRecording(msg);
stopRecording(msg, true);
try {
encoding(new Runnable() {
@Override
@ -476,7 +476,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
String a = intent.getAction();
if (a != null && a.equals(START_PAUSE)) { // pretend we already start it
start = false;
stopRecording(getString(R.string.recording_status_pause));
stopRecording(getString(R.string.recording_status_pause), false);
}
onIntent(intent);
}
@ -594,7 +594,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
void pauseButton() {
if (recording.thread != null) {
receiver.errors = false;
stopRecording(getString(R.string.recording_status_pause));
stopRecording(getString(R.string.recording_status_pause), false);
receiver.stopBluetooth();
headset(true, false);
} else {
@ -619,7 +619,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
if (receiver.isRecordingReady())
startRecording();
else
stopRecording(getString(R.string.hold_by_bluetooth));
stopRecording(getString(R.string.hold_by_bluetooth), false);
}
}
@ -649,14 +649,19 @@ public class RecordingActivity extends AppCompatThemeActivity {
progress.onPause();
}
void stopRecording(String status) {
void stopRecording(String status, boolean stop) {
setState(status);
pause.setImageResource(R.drawable.ic_mic_24dp);
pause.setContentDescription(getString(R.string.record_button));
stopRecording();
RecordingService.startService(this, Storage.getName(this, recording.targetUri), false, duration);
if (stop) {
receiver.close();
RecordingService.stop(this, Storage.getName(this, recording.targetUri), duration);
} else {
RecordingService.startService(this, Storage.getName(this, recording.targetUri), false, duration);
}
final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
@ -906,7 +911,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
progress = null;
}
RecordingService.stopRecording(this);
RecordingService.stop(this, null, null);
ControlsService.startIfEnabled(this);
if (pscl != null) {

View file

@ -30,6 +30,7 @@ public class BluetoothReceiver extends BroadcastReceiver {
public boolean errors = false; // show errors
public boolean connecting = false;
public IntentFilter filter = new IntentFilter();
public AudioManager am;
public Runnable connected = new Runnable() {
@Override
@ -67,10 +68,14 @@ public class BluetoothReceiver extends BroadcastReceiver {
public void registerReceiver(Context context) {
this.context = context;
context.registerReceiver(this, filter);
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
}
public void close() {
context.unregisterReceiver(this);
if (context != null) {
context.unregisterReceiver(this);
context = null;
}
}
public void onConnected() {
@ -109,7 +114,6 @@ public class BluetoothReceiver extends BroadcastReceiver {
@SuppressWarnings("deprecation")
public boolean startBluetooth() {
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (am.isBluetoothScoAvailableOffCall()) {
if (!bluetoothStart) {
if (Build.VERSION.SDK_INT == 21) {
@ -130,7 +134,6 @@ public class BluetoothReceiver extends BroadcastReceiver {
public void stopBluetooth() {
handler.removeCallbacks(connected);
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
if (bluetoothStart) {
bluetoothStart = false;
am.stopBluetoothSco();

View file

@ -4,9 +4,11 @@ import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
@ -39,8 +41,6 @@ public class RecordingService extends PersistentService {
public static final int NOTIFICATION_RECORDING_ICON = 1;
public static String SHOW_ACTIVITY = RecordingService.class.getCanonicalName() + ".SHOW_ACTIVITY";
public static String PAUSE_BUTTON = RecordingService.class.getCanonicalName() + ".PAUSE_BUTTON";
public static String RECORD_BUTTON = RecordingService.class.getCanonicalName() + ".RECORD_BUTTON";
static {
OptimizationPreferenceCompat.REFRESH = AlarmManager.MIN1;
@ -82,12 +82,11 @@ public class RecordingService extends PersistentService {
);
}
public static void stopRecording(Context context) {
stop(context);
}
public static void stop(Context context) {
stop(context, new Intent(context, RecordingService.class));
public static void stop(Context context, String targetFile, String duration) {
stop(context, new Intent(context, RecordingService.class)
.putExtra("targetFile", targetFile)
.putExtra("duration", duration)
.putExtra("stop", true));
}
public RecordingService() {
@ -120,9 +119,10 @@ public class RecordingService extends PersistentService {
@SuppressLint("RestrictedApi")
public Notification build(Intent intent) {
Log.d(TAG, "" + intent);
String targetFile = intent.getStringExtra("targetFile");
boolean recording = intent.getBooleanExtra("recording", false);
boolean encoding = false;
boolean stop = intent.getBooleanExtra("stop", false);
String duration = intent.getStringExtra("duration");
PendingIntent main;
@ -148,8 +148,7 @@ public class RecordingService extends PersistentService {
RemoteViewsCompat.mergeRemoteViews(icon.notification.bigContentView, a);
}
return icon.notification;
} catch (RuntimeException e) {
Log.d(TAG, "merge failed", e);
} catch (RuntimeException ignore) { // merge view failed
}
}
}
@ -157,22 +156,21 @@ public class RecordingService extends PersistentService {
builder = new RemoteNotificationCompat.Builder(context, R.layout.notifictaion);
builder.setViewVisibility(R.id.notification_record, View.GONE);
builder.setViewVisibility(R.id.notification_pause, View.VISIBLE);
main = PendingIntent.getService(context, 0, new Intent(context, RecordingService.class)
.setAction(SHOW_ACTIVITY)
.putExtra("targetFile", targetFile)
.putExtra("recording", recording), PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pe = PendingIntent.getService(context, 0,
new Intent(context, RecordingService.class).setAction(PAUSE_BUTTON),
main = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent re = PendingIntent.getService(context, 0,
new Intent(context, RecordingService.class).setAction(RECORD_BUTTON),
PendingIntent pe = PendingIntent.getBroadcast(context, 0,
new Intent(RecordingActivity.PAUSE_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT);
if (encoding) {
PendingIntent re = PendingIntent.getBroadcast(context, 0,
new Intent(RecordingActivity.PAUSE_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT);
if (stop) { // service exiting
builder.setViewVisibility(R.id.notification_pause, View.GONE);
title = getString(R.string.encoding_title);
main = null;
}
builder.setOnClickPendingIntent(R.id.notification_pause, pe);
@ -210,11 +208,6 @@ public class RecordingService extends PersistentService {
String a = intent.getAction();
if (a == null) {
optimization.icon.updateIcon(intent);
} else if (a.equals(PAUSE_BUTTON)) {
Intent i = new Intent(RecordingActivity.PAUSE_BUTTON);
sendBroadcast(i);
} else if (a.equals(RECORD_BUTTON)) {
RecordingActivity.startActivity(this, false);
} else if (a.equals(SHOW_ACTIVITY)) {
ProximityShader.closeSystemDialogs(this);
if (intent.getStringExtra("targetFile") == null)