2016-05-20 20:24:34 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Synced Synapse. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.xbmc.kore.service;
|
|
|
|
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.Manifest;
|
2016-05-20 20:24:34 +02:00
|
|
|
import android.app.Service;
|
|
|
|
import android.content.Intent;
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.content.pm.PackageManager;
|
2017-12-24 18:49:40 +01:00
|
|
|
import android.os.Handler;
|
2016-05-20 20:24:34 +02:00
|
|
|
import android.os.IBinder;
|
|
|
|
import android.preference.PreferenceManager;
|
2016-05-25 18:18:25 +02:00
|
|
|
import android.support.v4.content.ContextCompat;
|
2016-05-20 20:24:34 +02:00
|
|
|
|
|
|
|
import org.xbmc.kore.Settings;
|
|
|
|
import org.xbmc.kore.host.HostConnectionObserver;
|
|
|
|
import org.xbmc.kore.host.HostManager;
|
2017-07-13 20:10:49 +02:00
|
|
|
import org.xbmc.kore.jsonrpc.notification.Player;
|
2016-05-20 20:24:34 +02:00
|
|
|
import org.xbmc.kore.jsonrpc.type.ListType;
|
|
|
|
import org.xbmc.kore.jsonrpc.type.PlayerType;
|
|
|
|
import org.xbmc.kore.utils.LogUtils;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This service is a wrapper over {@link HostConnectionObserver} that
|
|
|
|
* manages connection observers, and does it in a service that keeps running
|
|
|
|
* until the connection is lost.
|
|
|
|
* The observers are created here.
|
|
|
|
* This service stops itself as soon as there's no connection.
|
|
|
|
*
|
|
|
|
* A {@link HostConnectionObserver} singleton is used to keep track of Kodi's
|
|
|
|
* state. This singleton should be the same as used in the app's activities
|
|
|
|
*/
|
|
|
|
public class ConnectionObserversManagerService extends Service
|
|
|
|
implements HostConnectionObserver.PlayerEventsObserver {
|
|
|
|
public static final String TAG = LogUtils.makeLogTag(ConnectionObserversManagerService.class);
|
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
private HostConnectionObserver hostConnectionObserver = null;
|
2016-05-20 20:24:34 +02:00
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
private List<HostConnectionObserver.PlayerEventsObserver> observers = new ArrayList<>();
|
|
|
|
private NotificationObserver notificationObserver;
|
2017-12-24 18:49:40 +01:00
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
private boolean somethingIsPlaying = false;
|
|
|
|
private Handler stopHandler = new Handler();
|
2016-05-20 20:24:34 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
// We do not create any thread because all the works is supposed to
|
|
|
|
// be done on the main thread, so that the connection observer
|
|
|
|
// can be shared with the app, and notify it on the UI thread
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
|
LogUtils.LOGD(TAG, "onStartCommand");
|
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
// Create the observers we are managing and immediatelly call
|
|
|
|
// startForeground() to avoid ANRs
|
|
|
|
if (observers.isEmpty()) {
|
|
|
|
createObservers();
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
2019-05-11 16:52:01 +02:00
|
|
|
startForeground(NotificationObserver.NOTIFICATION_ID,
|
|
|
|
notificationObserver.getCurrentNotification());
|
2016-05-20 20:24:34 +02:00
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
HostConnectionObserver connectionObserver =
|
|
|
|
HostManager.getInstance(this).getHostConnectionObserver();
|
2017-12-24 18:49:40 +01:00
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
if (hostConnectionObserver == null) {
|
|
|
|
hostConnectionObserver = connectionObserver;
|
|
|
|
hostConnectionObserver.registerPlayerObserver(this, true);
|
|
|
|
} else if (hostConnectionObserver != connectionObserver) {
|
|
|
|
// There has been a change in hosts.
|
|
|
|
// Unregister the previous one and register the current one
|
|
|
|
hostConnectionObserver.unregisterPlayerObserver(this);
|
|
|
|
hostConnectionObserver = connectionObserver;
|
|
|
|
hostConnectionObserver.registerPlayerObserver(this, true);
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
// If we get killed after returning from here, restart
|
2016-05-20 20:24:34 +02:00
|
|
|
return START_STICKY;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createObservers() {
|
2019-05-11 16:52:01 +02:00
|
|
|
observers = new ArrayList<>();
|
2016-05-20 20:24:34 +02:00
|
|
|
|
2017-12-24 18:49:40 +01:00
|
|
|
// Always show a notification
|
2019-05-11 16:52:01 +02:00
|
|
|
notificationObserver = new NotificationObserver(this);
|
|
|
|
observers.add(notificationObserver);
|
2016-05-20 20:24:34 +02:00
|
|
|
|
2016-05-25 18:18:25 +02:00
|
|
|
// Check whether we should react to phone state changes and wether
|
|
|
|
// we have permissions to do so
|
2016-05-20 20:24:34 +02:00
|
|
|
boolean shouldPause = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(this)
|
2016-05-25 18:18:25 +02:00
|
|
|
.getBoolean(Settings.KEY_PREF_PAUSE_DURING_CALLS,
|
|
|
|
Settings.DEFAULT_PREF_PAUSE_DURING_CALLS);
|
|
|
|
boolean hasPhonePermission =
|
2019-05-11 16:52:01 +02:00
|
|
|
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) ==
|
|
|
|
PackageManager.PERMISSION_GRANTED;
|
2016-05-25 18:18:25 +02:00
|
|
|
if (shouldPause && hasPhonePermission) {
|
2019-05-11 16:52:01 +02:00
|
|
|
observers.add(new PauseCallObserver(this));
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
|
// We don't provide binding, so return null
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTaskRemoved (Intent rootIntent) {
|
|
|
|
// Gracefully stop
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2017-03-15 19:49:38 +01:00
|
|
|
observer.playerOnConnectionError(0, "Task removed");
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LogUtils.LOGD(TAG, "Shutting down observer service - Task removed");
|
2019-05-11 16:52:01 +02:00
|
|
|
if (hostConnectionObserver != null) {
|
|
|
|
hostConnectionObserver.unregisterPlayerObserver(this);
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
2017-12-24 18:49:40 +01:00
|
|
|
stopForeground(true);
|
2016-05-20 20:24:34 +02:00
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
// Gracefully stop
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.playerOnConnectionError(0, "Service destroyed");
|
|
|
|
}
|
|
|
|
LogUtils.LOGD(TAG, "Shutting down observer service - destroyed");
|
2019-05-11 16:52:01 +02:00
|
|
|
if (hostConnectionObserver != null) {
|
|
|
|
hostConnectionObserver.unregisterPlayerObserver(this);
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-13 20:10:49 +02:00
|
|
|
@Override
|
|
|
|
public void playerOnPropertyChanged(Player.NotificationsData notificationsData) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-20 20:24:34 +02:00
|
|
|
/**
|
|
|
|
* HostConnectionObserver.PlayerEventsObserver interface callbacks
|
|
|
|
*/
|
|
|
|
public void playerOnPlay(PlayerType.GetActivePlayersReturnType getActivePlayerResult,
|
|
|
|
PlayerType.PropertyValue getPropertiesResult,
|
|
|
|
ListType.ItemsAll getItemResult) {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.playerOnPlay(getActivePlayerResult, getPropertiesResult, getItemResult);
|
|
|
|
}
|
2019-05-11 16:52:01 +02:00
|
|
|
somethingIsPlaying = true;
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void playerOnPause(PlayerType.GetActivePlayersReturnType getActivePlayerResult,
|
|
|
|
PlayerType.PropertyValue getPropertiesResult,
|
|
|
|
ListType.ItemsAll getItemResult) {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.playerOnPause(getActivePlayerResult, getPropertiesResult, getItemResult);
|
|
|
|
}
|
2019-05-11 16:52:01 +02:00
|
|
|
somethingIsPlaying = true;
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void playerOnStop() {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.playerOnStop();
|
|
|
|
}
|
|
|
|
|
2019-05-11 16:52:01 +02:00
|
|
|
somethingIsPlaying = false;
|
2017-12-24 18:49:40 +01:00
|
|
|
|
|
|
|
// Stop service if nothing starts in a couple of seconds
|
2019-05-11 16:52:01 +02:00
|
|
|
stopHandler.postDelayed(new Runnable() {
|
2017-12-24 18:49:40 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2019-05-11 16:52:01 +02:00
|
|
|
if (!somethingIsPlaying) {
|
2017-12-24 18:49:40 +01:00
|
|
|
LogUtils.LOGD(TAG, "Stopping service");
|
2019-05-11 16:52:01 +02:00
|
|
|
if (hostConnectionObserver != null) {
|
|
|
|
hostConnectionObserver.unregisterPlayerObserver(ConnectionObserversManagerService.this);
|
2017-12-24 18:49:40 +01:00
|
|
|
}
|
|
|
|
stopForeground(true);
|
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, 5000);
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void playerNoResultsYet() {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.playerNoResultsYet();
|
|
|
|
}
|
2019-05-11 16:52:01 +02:00
|
|
|
somethingIsPlaying = false;
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void playerOnConnectionError(int errorCode, String description) {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.playerOnConnectionError(errorCode, description);
|
|
|
|
}
|
2019-05-11 16:52:01 +02:00
|
|
|
somethingIsPlaying = false;
|
2016-05-20 20:24:34 +02:00
|
|
|
|
|
|
|
// Stop service
|
|
|
|
LogUtils.LOGD(TAG, "Shutting down observer service - Connection error");
|
2019-05-11 16:52:01 +02:00
|
|
|
if (hostConnectionObserver != null) {
|
|
|
|
hostConnectionObserver.unregisterPlayerObserver(this);
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
2017-12-24 18:49:40 +01:00
|
|
|
stopForeground(true);
|
2016-05-20 20:24:34 +02:00
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void systemOnQuit() {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.systemOnQuit();
|
|
|
|
}
|
2019-05-11 16:52:01 +02:00
|
|
|
somethingIsPlaying = false;
|
2016-05-20 20:24:34 +02:00
|
|
|
|
|
|
|
// Stop service
|
|
|
|
LogUtils.LOGD(TAG, "Shutting down observer service - System quit");
|
2019-05-11 16:52:01 +02:00
|
|
|
if (hostConnectionObserver != null) {
|
|
|
|
hostConnectionObserver.unregisterPlayerObserver(this);
|
2016-05-20 20:24:34 +02:00
|
|
|
}
|
2017-12-24 18:49:40 +01:00
|
|
|
stopForeground(true);
|
2016-05-20 20:24:34 +02:00
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore this
|
|
|
|
public void inputOnInputRequested(String title, String type, String value) {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.inputOnInputRequested(title, type, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void observerOnStopObserving() {
|
2019-05-11 16:52:01 +02:00
|
|
|
for (HostConnectionObserver.PlayerEventsObserver observer : observers) {
|
2016-05-20 20:24:34 +02:00
|
|
|
observer.observerOnStopObserving();
|
|
|
|
}
|
|
|
|
// Called when the user changes host
|
|
|
|
LogUtils.LOGD(TAG, "Shutting down observer service - Stop observing");
|
2017-12-24 18:49:40 +01:00
|
|
|
stopForeground(true);
|
2016-05-20 20:24:34 +02:00
|
|
|
stopSelf();
|
|
|
|
}
|
|
|
|
}
|