Fix notification click handler

This commit is contained in:
edr
2024-02-17 01:00:55 +01:00
committed by GitHub
parent 5b0ccaa8a1
commit 7359716c09

View File

@@ -32,18 +32,10 @@ import android.widget.Toast;
public class ListenActivity extends Activity { public class ListenActivity extends Activity {
final String TAG = "ListenActivity"; final String TAG = "ListenActivity";
String address;
int port;
String name;
// Don't attempt to unbind from the service unless the client has received some // Don't attempt to unbind from the service unless the client has received some
// information about the service's state. // information about the service's state.
private boolean shouldUnbind; private boolean shouldUnbind;
// To invoke the bound service, first make sure that this value
// is not null.
private ListenService boundService;
private final ServiceConnection connection = new ServiceConnection() { private final ServiceConnection connection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) { public void onServiceConnected(ComponentName className, IBinder service) {
// This is called when the connection with the service has been // This is called when the connection with the service has been
@@ -55,16 +47,15 @@ public class ListenActivity extends Activity {
Toast.makeText(ListenActivity.this, R.string.connect, Toast.makeText(ListenActivity.this, R.string.connect,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
final TextView connectedText = findViewById(R.id.connectedTo);
connectedText.setText(bs.getChildDeviceName());
final VolumeView volumeView = findViewById(R.id.volume); final VolumeView volumeView = findViewById(R.id.volume);
volumeView.setVolumeHistory(bs.getVolumeHistory()); volumeView.setVolumeHistory(bs.getVolumeHistory());
bs.setUpdateCallback(volumeView::postInvalidate); bs.setUpdateCallback(volumeView::postInvalidate);
bs.setErrorCallback(() -> { bs.setErrorCallback(() -> {
TextView status = findViewById(R.id.textStatus); TextView status = findViewById(R.id.textStatus);
status.setText(R.string.disconnected); status.setText(R.string.disconnected);
}); });
boundService = bs;
} }
public void onServiceDisconnected(ComponentName className) { public void onServiceDisconnected(ComponentName className) {
@@ -72,20 +63,21 @@ public class ListenActivity extends Activity {
// unexpectedly disconnected -- that is, its process crashed. // unexpectedly disconnected -- that is, its process crashed.
// Because it is running in our same process, we should never // Because it is running in our same process, we should never
// see this happen. // see this happen.
boundService = null;
Toast.makeText(ListenActivity.this, R.string.disconnected, Toast.makeText(ListenActivity.this, R.string.disconnected,
Toast.LENGTH_SHORT).show(); Toast.LENGTH_SHORT).show();
} }
}; };
void startAndBindService() { void ensureServiceRunningAndBind(Bundle bundle) {
final Context context = this; final Context context = this;
final Intent intent = new Intent(context, ListenService.class); final Intent intent = new Intent(context, ListenService.class);
intent.putExtra("name", name); if (bundle != null) {
intent.putExtra("address", address); intent.putExtra("name", bundle.getString("name"));
intent.putExtra("port", port); intent.putExtra("address", bundle.getString("address"));
ContextCompat.startForegroundService(context, intent); intent.putExtra("port", bundle.getInt("port"));
ContextCompat.startForegroundService(context, intent);
}
// Attempts to establish a connection with the service. We use an // Attempts to establish a connection with the service. We use an
// explicit class name because we want a specific service // explicit class name because we want a specific service
// implementation that we know will be running in our own process // implementation that we know will be running in our own process
@@ -116,32 +108,18 @@ public class ListenActivity extends Activity {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
final Bundle bundle = getIntent().getExtras(); final Bundle bundle = getIntent().getExtras();
if (bundle != null) { ensureServiceRunningAndBind(bundle);
address = bundle.getString("address");
port = bundle.getInt("port");
name = bundle.getString("name");
startAndBindService();
}
setVolumeControlStream(AudioManager.STREAM_MUSIC); setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_listen); setContentView(R.layout.activity_listen);
final TextView connectedText = findViewById(R.id.connectedTo);
connectedText.setText(name);
final TextView statusText = findViewById(R.id.textStatus); final TextView statusText = findViewById(R.id.textStatus);
if (bundle != null) { statusText.setText(R.string.listening);
statusText.setText(R.string.listening);
}
else {
statusText.setText(R.string.error_please_retry);
}
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
doUnbindAndStopService(); doUnbindAndStopService();
boundService = null;
super.onDestroy(); super.onDestroy();
} }
} }