2023-03-06 14:42:04 +01:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
// Copyright The Music Player Daemon Project
|
2014-02-18 08:33:06 +01:00
|
|
|
|
|
|
|
package org.musicpd;
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
import android.annotation.TargetApi;
|
|
|
|
import android.app.Notification;
|
2020-03-25 22:08:17 +01:00
|
|
|
import android.app.NotificationManager;
|
2014-09-16 12:00:59 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.app.Service;
|
2021-07-26 19:55:39 +02:00
|
|
|
import android.content.BroadcastReceiver;
|
2014-09-16 12:00:59 +02:00
|
|
|
import android.content.ComponentName;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2021-07-26 19:55:39 +02:00
|
|
|
import android.content.IntentFilter;
|
2014-09-16 12:00:59 +02:00
|
|
|
import android.content.ServiceConnection;
|
2022-01-04 12:19:52 +01:00
|
|
|
import android.media.AudioManager;
|
2014-02-18 08:33:06 +01:00
|
|
|
import android.os.Build;
|
2014-09-16 12:00:59 +02:00
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.os.RemoteCallbackList;
|
|
|
|
import android.os.RemoteException;
|
2014-02-18 08:33:06 +01:00
|
|
|
import android.util.Log;
|
2014-09-16 12:00:59 +02:00
|
|
|
import android.widget.RemoteViews;
|
2014-02-18 08:33:06 +01:00
|
|
|
|
2020-03-25 22:08:17 +01:00
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
public class Main extends Service implements Runnable {
|
|
|
|
private static final String TAG = "Main";
|
|
|
|
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;
|
2014-02-18 08:33:06 +01:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private static final int MSG_SEND_STATUS = 0;
|
|
|
|
private static final int MSG_SEND_LOG = 1;
|
2014-02-18 08:33:06 +01:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private Thread mThread = null;
|
|
|
|
private int mStatus = MAIN_STATUS_STOPPED;
|
|
|
|
private boolean mAbort = false;
|
|
|
|
private String mError = null;
|
|
|
|
private final RemoteCallbackList<IMainCallback> mCallbacks = new RemoteCallbackList<IMainCallback>();
|
|
|
|
private final IBinder mBinder = new MainStub(this);
|
2021-07-26 19:55:39 +02:00
|
|
|
private boolean mPauseOnHeadphonesDisconnect = false;
|
2014-09-16 12:00:59 +02:00
|
|
|
private PowerManager.WakeLock mWakelock = null;
|
2014-07-30 10:12:18 +02:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
static class MainStub extends IMain.Stub {
|
|
|
|
private Main mService;
|
|
|
|
MainStub(Main service) {
|
|
|
|
mService = service;
|
|
|
|
}
|
|
|
|
public void start() {
|
|
|
|
mService.start();
|
|
|
|
}
|
|
|
|
public void stop() {
|
|
|
|
mService.stop();
|
|
|
|
}
|
2021-07-26 19:55:39 +02:00
|
|
|
public void setPauseOnHeadphonesDisconnect(boolean enabled) {
|
|
|
|
mService.setPauseOnHeadphonesDisconnect(enabled);
|
|
|
|
}
|
2014-09-16 12:00:59 +02:00
|
|
|
public void setWakelockEnabled(boolean enabled) {
|
|
|
|
mService.setWakelockEnabled(enabled);
|
|
|
|
}
|
|
|
|
public boolean isRunning() {
|
|
|
|
return mService.isRunning();
|
|
|
|
}
|
|
|
|
public void registerCallback(IMainCallback cb) {
|
|
|
|
mService.registerCallback(cb);
|
|
|
|
}
|
|
|
|
public void unregisterCallback(IMainCallback cb) {
|
|
|
|
mService.unregisterCallback(cb);
|
|
|
|
}
|
|
|
|
}
|
2014-07-30 10:12:18 +02:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private synchronized void sendMessage(int what, int arg1, int arg2, Object obj) {
|
|
|
|
int i = mCallbacks.beginBroadcast();
|
|
|
|
while (i > 0) {
|
|
|
|
i--;
|
|
|
|
final IMainCallback cb = mCallbacks.getBroadcastItem(i);
|
|
|
|
try {
|
|
|
|
switch (what) {
|
|
|
|
case MSG_SEND_STATUS:
|
|
|
|
switch (arg1) {
|
|
|
|
case MAIN_STATUS_ERROR:
|
|
|
|
cb.onError((String)obj);
|
|
|
|
break;
|
|
|
|
case MAIN_STATUS_STOPPED:
|
|
|
|
cb.onStopped();
|
|
|
|
break;
|
|
|
|
case MAIN_STATUS_STARTED:
|
|
|
|
cb.onStarted();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MSG_SEND_LOG:
|
|
|
|
cb.onLog(arg1, (String) obj);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (RemoteException e) {
|
2014-07-30 10:12:18 +02:00
|
|
|
}
|
2014-09-16 12:00:59 +02:00
|
|
|
}
|
|
|
|
mCallbacks.finishBroadcast();
|
|
|
|
}
|
2014-07-30 10:12:18 +02:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private Bridge.LogListener mLogListener = new Bridge.LogListener() {
|
|
|
|
@Override
|
|
|
|
public void onLog(int priority, String msg) {
|
|
|
|
sendMessage(MSG_SEND_LOG, priority, 0, msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
return mBinder;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
start();
|
|
|
|
if (intent != null && intent.getBooleanExtra("wakelock", false))
|
|
|
|
setWakelockEnabled(true);
|
|
|
|
return START_STICKY;
|
|
|
|
}
|
2014-02-18 08:33:06 +01:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-18 08:33:06 +01:00
|
|
|
if (!Loader.loaded) {
|
2014-09-16 12:00:59 +02:00
|
|
|
final String error = "Failed to load the native MPD libary.\n" +
|
|
|
|
"Report this problem to us, and include the following information:\n" +
|
|
|
|
"SUPPORTED_ABIS=" + String.join(", ", Build.SUPPORTED_ABIS) + "\n" +
|
|
|
|
"PRODUCT=" + Build.PRODUCT + "\n" +
|
|
|
|
"FINGERPRINT=" + Build.FINGERPRINT + "\n" +
|
|
|
|
"error=" + Loader.error;
|
|
|
|
setStatus(MAIN_STATUS_ERROR, error);
|
|
|
|
stopSelf();
|
2014-02-18 08:33:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-09-16 12:00:59 +02:00
|
|
|
synchronized (this) {
|
|
|
|
if (mAbort)
|
|
|
|
return;
|
|
|
|
setStatus(MAIN_STATUS_STARTED, null);
|
|
|
|
}
|
|
|
|
Bridge.run(this, mLogListener);
|
|
|
|
setStatus(MAIN_STATUS_STOPPED, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private synchronized void setStatus(int status, String error) {
|
|
|
|
mStatus = status;
|
|
|
|
mError = error;
|
|
|
|
sendMessage(MSG_SEND_STATUS, mStatus, 0, mError);
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:08:17 +01:00
|
|
|
private Notification.Builder createNotificationBuilderWithChannel() {
|
|
|
|
final NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
if (notificationManager == null)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
final String id = "org.musicpd";
|
|
|
|
final String name = "MPD service";
|
|
|
|
final int importance = 3; /* NotificationManager.IMPORTANCE_DEFAULT */
|
|
|
|
|
|
|
|
try {
|
|
|
|
Class<?> ncClass = Class.forName("android.app.NotificationChannel");
|
|
|
|
Constructor<?> ncCtor = ncClass.getConstructor(String.class, CharSequence.class, int.class);
|
|
|
|
Object nc = ncCtor.newInstance(id, name, importance);
|
|
|
|
|
|
|
|
Method nmCreateNotificationChannelMethod =
|
|
|
|
NotificationManager.class.getMethod("createNotificationChannel", ncClass);
|
|
|
|
nmCreateNotificationChannelMethod.invoke(notificationManager, nc);
|
|
|
|
|
|
|
|
Constructor nbCtor = Notification.Builder.class.getConstructor(Context.class, String.class);
|
|
|
|
return (Notification.Builder) nbCtor.newInstance(this, id);
|
|
|
|
} catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(TAG, "error creating the NotificationChannel", e);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private void start() {
|
|
|
|
if (mThread != null)
|
|
|
|
return;
|
|
|
|
|
2021-07-26 19:55:39 +02:00
|
|
|
IntentFilter filter = new IntentFilter();
|
2022-01-04 12:19:52 +01:00
|
|
|
filter.addAction(AudioManager.ACTION_AUDIO_BECOMING_NOISY);
|
2021-07-26 19:55:39 +02:00
|
|
|
registerReceiver(new BroadcastReceiver() {
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2022-01-04 12:19:52 +01:00
|
|
|
if (!mPauseOnHeadphonesDisconnect)
|
2021-07-26 19:55:39 +02:00
|
|
|
return;
|
2022-01-04 12:19:52 +01:00
|
|
|
if (intent.getAction() == AudioManager.ACTION_AUDIO_BECOMING_NOISY)
|
|
|
|
pause();
|
2021-07-26 19:55:39 +02:00
|
|
|
}
|
|
|
|
}, filter);
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
final Intent mainIntent = new Intent(this, Settings.class);
|
|
|
|
mainIntent.setAction("android.intent.action.MAIN");
|
|
|
|
mainIntent.addCategory("android.intent.category.LAUNCHER");
|
|
|
|
final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
|
|
|
|
mainIntent, PendingIntent.FLAG_CANCEL_CURRENT);
|
|
|
|
|
2020-03-25 22:08:17 +01:00
|
|
|
Notification.Builder nBuilder;
|
|
|
|
if (Build.VERSION.SDK_INT >= 26 /* Build.VERSION_CODES.O */)
|
|
|
|
{
|
|
|
|
nBuilder = createNotificationBuilderWithChannel();
|
|
|
|
if (nBuilder == null)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nBuilder = new Notification.Builder(this);
|
|
|
|
|
|
|
|
Notification notification = nBuilder.setContentTitle(getText(R.string.notification_title_mpd_running))
|
2018-08-20 11:25:47 +02:00
|
|
|
.setContentText(getText(R.string.notification_text_mpd_running))
|
|
|
|
.setSmallIcon(R.drawable.notification_icon)
|
|
|
|
.setContentIntent(contentIntent)
|
|
|
|
.build();
|
2014-09-16 12:00:59 +02:00
|
|
|
|
2020-03-25 22:08:17 +01:00
|
|
|
mThread = new Thread(this);
|
|
|
|
mThread.start();
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
startForeground(R.string.notification_title_mpd_running, notification);
|
|
|
|
startService(new Intent(this, Main.class));
|
|
|
|
}
|
|
|
|
|
|
|
|
private void stop() {
|
|
|
|
if (mThread != null) {
|
|
|
|
if (mThread.isAlive()) {
|
|
|
|
synchronized (this) {
|
|
|
|
if (mStatus == MAIN_STATUS_STARTED)
|
|
|
|
Bridge.shutdown();
|
|
|
|
else
|
|
|
|
mAbort = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
mThread.join();
|
|
|
|
mThread = null;
|
|
|
|
mAbort = false;
|
|
|
|
} catch (InterruptedException ie) {}
|
|
|
|
}
|
|
|
|
setWakelockEnabled(false);
|
|
|
|
stopForeground(true);
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:55:39 +02:00
|
|
|
private void pause() {
|
|
|
|
if (mThread != null) {
|
|
|
|
if (mThread.isAlive()) {
|
|
|
|
synchronized (this) {
|
|
|
|
if (mStatus == MAIN_STATUS_STARTED)
|
|
|
|
Bridge.pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void setPauseOnHeadphonesDisconnect(boolean enabled) {
|
|
|
|
mPauseOnHeadphonesDisconnect = enabled;
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private void setWakelockEnabled(boolean enabled) {
|
|
|
|
if (enabled && mWakelock == null) {
|
|
|
|
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
|
|
|
|
mWakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
|
|
|
|
mWakelock.acquire();
|
|
|
|
Log.d(TAG, "Wakelock acquired");
|
|
|
|
} else if (!enabled && mWakelock != null) {
|
|
|
|
mWakelock.release();
|
|
|
|
mWakelock = null;
|
|
|
|
Log.d(TAG, "Wakelock released");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isRunning() {
|
|
|
|
return mThread != null && mThread.isAlive();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void registerCallback(IMainCallback cb) {
|
|
|
|
if (cb != null) {
|
|
|
|
mCallbacks.register(cb);
|
|
|
|
sendMessage(MSG_SEND_STATUS, mStatus, 0, mError);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 08:33:06 +01:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
private void unregisterCallback(IMainCallback cb) {
|
|
|
|
if (cb != null) {
|
|
|
|
mCallbacks.unregister(cb);
|
2014-02-18 08:33:06 +01:00
|
|
|
}
|
2014-09-16 12:00:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Client that bind the Main Service in order to send commands and receive callback
|
|
|
|
*/
|
|
|
|
public static class Client {
|
2014-02-18 08:33:06 +01:00
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
public interface Callback {
|
|
|
|
public void onStarted();
|
|
|
|
public void onStopped();
|
|
|
|
public void onError(String error);
|
|
|
|
public void onLog(int priority, String msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLog(int priority, String msg) throws RemoteException {
|
|
|
|
mCallback.onLog(priority, msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 19:55:39 +02:00
|
|
|
public boolean setPauseOnHeadphonesDisconnect(boolean enabled) {
|
|
|
|
synchronized (this) {
|
|
|
|
if (mIMain != null) {
|
|
|
|
try {
|
|
|
|
mIMain.setPauseOnHeadphonesDisconnect(enabled);
|
|
|
|
return true;
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-02-18 08:33:06 +01:00
|
|
|
}
|
|
|
|
|
2014-09-16 12:00:59 +02:00
|
|
|
/*
|
|
|
|
* start Main service without any callback
|
|
|
|
*/
|
|
|
|
public static void start(Context context, boolean wakelock) {
|
2021-05-31 20:37:10 +02:00
|
|
|
Intent intent = new Intent(context, Main.class)
|
|
|
|
.putExtra("wakelock", wakelock);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
|
|
|
/* in Android 8+, we need to use this method
|
|
|
|
or else we'll get "IllegalStateException:
|
|
|
|
app is in background" */
|
|
|
|
context.startForegroundService(intent);
|
|
|
|
else
|
|
|
|
context.startService(intent);
|
2014-02-18 08:33:06 +01:00
|
|
|
}
|
|
|
|
}
|