From 4bcbeae1e0f418e156ca3bd7c54aedafe7269aad Mon Sep 17 00:00:00 2001 From: Colin Edwards Date: Fri, 5 Apr 2024 23:27:41 -0500 Subject: [PATCH] android: Move service client into it's own file The service file was getting harder to read so lets pull the client code into it's own file --- .../java/org/musicpd/AutomationReceiver.kt | 9 + .../app/src/main/java/org/musicpd/Main.java | 151 +---------------- .../src/main/java/org/musicpd/MainActivity.kt | 2 +- .../java/org/musicpd/MainServiceClient.java | 157 ++++++++++++++++++ .../src/main/java/org/musicpd/Receiver.java | 2 +- .../java/org/musicpd/ui/SettingsViewModel.kt | 6 +- 6 files changed, 172 insertions(+), 155 deletions(-) create mode 100644 android/app/src/main/java/org/musicpd/AutomationReceiver.kt create mode 100644 android/app/src/main/java/org/musicpd/MainServiceClient.java diff --git a/android/app/src/main/java/org/musicpd/AutomationReceiver.kt b/android/app/src/main/java/org/musicpd/AutomationReceiver.kt new file mode 100644 index 000000000..bb7f42a91 --- /dev/null +++ b/android/app/src/main/java/org/musicpd/AutomationReceiver.kt @@ -0,0 +1,9 @@ +package org.musicpd + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent + +class AutomationReceiver : BroadcastReceiver() { + override fun onReceive(context: Context, intent: Intent) {} +} diff --git a/android/app/src/main/java/org/musicpd/Main.java b/android/app/src/main/java/org/musicpd/Main.java index 96a87934e..cc5a52837 100644 --- a/android/app/src/main/java/org/musicpd/Main.java +++ b/android/app/src/main/java/org/musicpd/Main.java @@ -8,11 +8,9 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; -import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.ServiceConnection; import android.media.AudioManager; import android.os.Build; import android.os.IBinder; @@ -39,7 +37,6 @@ import dagger.hilt.android.AndroidEntryPoint; public class Main extends Service implements Runnable { private static final String TAG = "Main"; private static final String WAKELOCK_TAG = "mpd:wakelockmain"; - private static final String REMOTE_ERROR = "MPD process was killed"; private static final int MAIN_STATUS_ERROR = -1; private static final int MAIN_STATUS_STOPPED = 0; private static final int MAIN_STATUS_STARTED = 1; @@ -307,156 +304,10 @@ public class Main extends Service implements Runnable { } } - /* - * Client that bind the Main Service in order to send commands and receive callback - */ - public static class Client { - - public interface Callback { - public void onStarted(); - public void onStopped(); - public void onError(String error); - } - - private boolean mBound = false; - private final Context mContext; - private Callback mCallback; - private IMain mIMain = null; - - private final IMainCallback.Stub mICallback = new IMainCallback.Stub() { - - @Override - public void onStopped() throws RemoteException { - mCallback.onStopped(); - } - - @Override - public void onStarted() throws RemoteException { - mCallback.onStarted(); - } - - @Override - public void onError(String error) throws RemoteException { - mCallback.onError(error); - } - }; - - private final ServiceConnection mServiceConnection = new ServiceConnection() { - - @Override - public void onServiceConnected(ComponentName name, IBinder service) { - synchronized (this) { - mIMain = IMain.Stub.asInterface(service); - try { - if (mCallback != null) - mIMain.registerCallback(mICallback); - } catch (RemoteException e) { - if (mCallback != null) - mCallback.onError(REMOTE_ERROR); - } - } - } - - @Override - public void onServiceDisconnected(ComponentName name) { - if (mCallback != null) - mCallback.onError(REMOTE_ERROR); - } - }; - - public Client(Context context, Callback cb) throws IllegalArgumentException { - if (context == null) - throw new IllegalArgumentException("Context can't be null"); - mContext = context; - mCallback = cb; - mBound = mContext.bindService(new Intent(mContext, Main.class), mServiceConnection, Context.BIND_AUTO_CREATE); - } - - public boolean start() { - synchronized (this) { - if (mIMain != null) { - try { - mIMain.start(); - return true; - } catch (RemoteException e) { - } - } - return false; - } - } - - public boolean stop() { - synchronized (this) { - if (mIMain != null) { - try { - mIMain.stop(); - return true; - } catch (RemoteException e) { - } - } - return false; - } - } - - public boolean setPauseOnHeadphonesDisconnect(boolean enabled) { - synchronized (this) { - if (mIMain != null) { - try { - mIMain.setPauseOnHeadphonesDisconnect(enabled); - return true; - } catch (RemoteException e) { - } - } - return false; - } - } - - public boolean setWakelockEnabled(boolean enabled) { - synchronized (this) { - if (mIMain != null) { - try { - mIMain.setWakelockEnabled(enabled); - return true; - } catch (RemoteException e) { - } - } - return false; - } - } - - public boolean isRunning() { - synchronized (this) { - if (mIMain != null) { - try { - return mIMain.isRunning(); - } catch (RemoteException e) { - } - } - return false; - } - } - - public void release() { - if (mBound) { - synchronized (this) { - if (mIMain != null && mICallback != null) { - try { - if (mCallback != null) - mIMain.unregisterCallback(mICallback); - } catch (RemoteException e) { - } - } - } - mBound = false; - mContext.unbindService(mServiceConnection); - } - } - } - /* * start Main service without any callback */ - public static void start(Context context, boolean wakelock) { + public static void startService(Context context, boolean wakelock) { Intent intent = new Intent(context, Main.class) .putExtra("wakelock", wakelock); diff --git a/android/app/src/main/java/org/musicpd/MainActivity.kt b/android/app/src/main/java/org/musicpd/MainActivity.kt index 87965e613..de5ae802b 100644 --- a/android/app/src/main/java/org/musicpd/MainActivity.kt +++ b/android/app/src/main/java/org/musicpd/MainActivity.kt @@ -25,7 +25,7 @@ class MainActivity : ComponentActivity() { } private fun connectClient() { - val client = Main.Client(this, object : Main.Client.Callback { + val client = MainServiceClient(this, object : MainServiceClient.Callback { override fun onStopped() { settingsViewModel.updateStatus("", false) } diff --git a/android/app/src/main/java/org/musicpd/MainServiceClient.java b/android/app/src/main/java/org/musicpd/MainServiceClient.java new file mode 100644 index 000000000..e94364945 --- /dev/null +++ b/android/app/src/main/java/org/musicpd/MainServiceClient.java @@ -0,0 +1,157 @@ +package org.musicpd; + +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.os.RemoteException; + + +/* + * Client that bind the Main Service in order to send commands and receive callback + */ +public class MainServiceClient { + + private static final String REMOTE_ERROR = "MPD process was killed"; + + public interface Callback { + public void onStarted(); + public void onStopped(); + public void onError(String error); + } + + private boolean mBound = false; + private final Context mContext; + private Callback mCallback; + private IMain mIMain = null; + + private final IMainCallback.Stub mICallback = new IMainCallback.Stub() { + + @Override + public void onStopped() throws RemoteException { + mCallback.onStopped(); + } + + @Override + public void onStarted() throws RemoteException { + mCallback.onStarted(); + } + + @Override + public void onError(String error) throws RemoteException { + mCallback.onError(error); + } + }; + + private final ServiceConnection mServiceConnection = new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + synchronized (this) { + mIMain = IMain.Stub.asInterface(service); + try { + if (mCallback != null) + mIMain.registerCallback(mICallback); + } catch (RemoteException e) { + if (mCallback != null) + mCallback.onError(REMOTE_ERROR); + } + } + } + + @Override + public void onServiceDisconnected(ComponentName name) { + if (mCallback != null) + mCallback.onError(REMOTE_ERROR); + } + }; + + public MainServiceClient(Context context, Callback cb) throws IllegalArgumentException { + if (context == null) + throw new IllegalArgumentException("Context can't be null"); + mContext = context; + mCallback = cb; + mBound = mContext.bindService(new Intent(mContext, Main.class), mServiceConnection, Context.BIND_AUTO_CREATE); + } + + public boolean start() { + synchronized (this) { + if (mIMain != null) { + try { + mIMain.start(); + return true; + } catch (RemoteException e) { + } + } + return false; + } + } + + public boolean stop() { + synchronized (this) { + if (mIMain != null) { + try { + mIMain.stop(); + return true; + } catch (RemoteException e) { + } + } + return false; + } + } + + public boolean setPauseOnHeadphonesDisconnect(boolean enabled) { + synchronized (this) { + if (mIMain != null) { + try { + mIMain.setPauseOnHeadphonesDisconnect(enabled); + return true; + } catch (RemoteException e) { + } + } + return false; + } + } + + public boolean setWakelockEnabled(boolean enabled) { + synchronized (this) { + if (mIMain != null) { + try { + mIMain.setWakelockEnabled(enabled); + return true; + } catch (RemoteException e) { + } + } + return false; + } + } + + public boolean isRunning() { + synchronized (this) { + if (mIMain != null) { + try { + return mIMain.isRunning(); + } catch (RemoteException e) { + } + } + return false; + } + } + + public void release() { + if (mBound) { + synchronized (this) { + if (mIMain != null && mICallback != null) { + try { + if (mCallback != null) + mIMain.unregisterCallback(mICallback); + } catch (RemoteException e) { + } + } + } + mBound = false; + mContext.unbindService(mServiceConnection); + } + } +} diff --git a/android/app/src/main/java/org/musicpd/Receiver.java b/android/app/src/main/java/org/musicpd/Receiver.java index 6ee3e51a0..92a6513ec 100644 --- a/android/app/src/main/java/org/musicpd/Receiver.java +++ b/android/app/src/main/java/org/musicpd/Receiver.java @@ -27,7 +27,7 @@ public class Receiver extends BroadcastReceiver { final boolean wakelock = Preferences.getBoolean(context, Preferences.KEY_WAKELOCK, false); - Main.start(context, wakelock); + Main.startService(context, wakelock); } } } diff --git a/android/app/src/main/java/org/musicpd/ui/SettingsViewModel.kt b/android/app/src/main/java/org/musicpd/ui/SettingsViewModel.kt index 6fc2c7002..62929ee4f 100644 --- a/android/app/src/main/java/org/musicpd/ui/SettingsViewModel.kt +++ b/android/app/src/main/java/org/musicpd/ui/SettingsViewModel.kt @@ -6,7 +6,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow -import org.musicpd.Main +import org.musicpd.MainServiceClient import org.musicpd.Preferences import org.musicpd.data.LoggingRepository import javax.inject.Inject @@ -16,7 +16,7 @@ import javax.inject.Inject class SettingsViewModel @Inject constructor( private var loggingRepository: LoggingRepository ) : ViewModel() { - private var mClient: Main.Client? = null + private var mClient: MainServiceClient? = null data class StatusUiState( val statusMessage: String = "", @@ -34,7 +34,7 @@ class SettingsViewModel @Inject constructor( _statusUIState.value = StatusUiState(message, running) } - fun setClient(client: Main.Client) { + fun setClient(client: MainServiceClient) { mClient = client }