|
|
@ -31,7 +31,6 @@ import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.IntentFilter;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.ConnectivityManager;
|
|
|
|
import android.net.LinkProperties;
|
|
|
|
import android.net.LinkProperties;
|
|
|
|
import android.net.Network;
|
|
|
|
import android.net.Network;
|
|
|
@ -46,6 +45,7 @@ import android.text.TextUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
|
|
|
import androidx.car.app.connection.CarConnection;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.core.content.ContextCompat;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
|
import androidx.lifecycle.Lifecycle;
|
|
|
@ -116,6 +116,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
private int lastAccounts = 0;
|
|
|
|
private int lastAccounts = 0;
|
|
|
|
private int lastOperations = 0;
|
|
|
|
private int lastOperations = 0;
|
|
|
|
private ConnectionHelper.NetworkState lastNetworkState = null;
|
|
|
|
private ConnectionHelper.NetworkState lastNetworkState = null;
|
|
|
|
|
|
|
|
private boolean isInCall = false;
|
|
|
|
|
|
|
|
private boolean isInCar = false;
|
|
|
|
|
|
|
|
|
|
|
|
private boolean foreground = false;
|
|
|
|
private boolean foreground = false;
|
|
|
|
private final Map<Long, Core.State> coreStates = new Hashtable<>();
|
|
|
|
private final Map<Long, Core.State> coreStates = new Hashtable<>();
|
|
|
@ -768,58 +770,45 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
final TwoStateOwner mowner = new TwoStateOwner(this, "mutableUnseenNotify");
|
|
|
|
final TwoStateOwner mowner = new TwoStateOwner(this, "mutableUnseenNotify");
|
|
|
|
|
|
|
|
mowner.getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
|
|
|
|
public void onStateChanged() {
|
|
|
|
|
|
|
|
Lifecycle.State state = mowner.getLifecycle().getCurrentState();
|
|
|
|
|
|
|
|
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Debug, "Owner state=" + state);
|
|
|
|
|
|
|
|
if (state.equals(Lifecycle.State.DESTROYED))
|
|
|
|
|
|
|
|
mowner.getLifecycle().removeObserver(this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
AudioManager am = Helper.getSystemService(this, AudioManager.class);
|
|
|
|
MediaPlayerHelper.liveInCall(this, this, new MediaPlayerHelper.IInCall() {
|
|
|
|
if (am == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
|
|
|
|
|
|
|
|
mowner.start();
|
|
|
|
|
|
|
|
Log.i("Audio mode legacy");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
AudioManager.OnModeChangedListener listener = new AudioManager.OnModeChangedListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onModeChanged(int mode) {
|
|
|
|
public void onChanged(boolean inCall) {
|
|
|
|
getMainHandler().post(new RunnableEx("AudioMode") {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void delegate() {
|
|
|
|
|
|
|
|
boolean incall = MediaPlayerHelper.isInCall(mode);
|
|
|
|
|
|
|
|
boolean suppress = prefs.getBoolean("notify_suppress_in_call", false);
|
|
|
|
boolean suppress = prefs.getBoolean("notify_suppress_in_call", false);
|
|
|
|
boolean start = (!suppress || !incall);
|
|
|
|
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Debug,
|
|
|
|
Log.i("Audio mode start=" + start +
|
|
|
|
"In call=" + inCall + " suppress=" + suppress);
|
|
|
|
" incall=" + incall +
|
|
|
|
isInCall = (inCall && suppress);
|
|
|
|
" suppress=" + suppress);
|
|
|
|
if (isInCall || isInCar)
|
|
|
|
if (start)
|
|
|
|
|
|
|
|
mowner.start();
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
mowner.stop();
|
|
|
|
mowner.stop();
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
mowner.start();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
listener.onModeChanged(am.getMode()); // Init
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getLifecycle().addObserver(new LifecycleObserver() {
|
|
|
|
|
|
|
|
private boolean registered = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
|
|
|
|
new CarConnection(this).getType().observe(this, new Observer<Integer>() {
|
|
|
|
public void onStateChanged() {
|
|
|
|
@Override
|
|
|
|
try {
|
|
|
|
public void onChanged(Integer connectionState) {
|
|
|
|
if (ServiceSynchronize.this.getLifecycle().getCurrentState().isAtLeast(Lifecycle.State.STARTED)) {
|
|
|
|
boolean projection = (connectionState != null &&
|
|
|
|
if (!registered) {
|
|
|
|
connectionState == CarConnection.CONNECTION_TYPE_PROJECTION);
|
|
|
|
am.addOnModeChangedListener(executor, listener);
|
|
|
|
boolean suppress = prefs.getBoolean("notify_suppress_in_car", false);
|
|
|
|
registered = true;
|
|
|
|
EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Debug,
|
|
|
|
}
|
|
|
|
"Projection=" + projection + " state=" + connectionState + " suppress=" + suppress);
|
|
|
|
} else {
|
|
|
|
isInCar = (projection && suppress);
|
|
|
|
if (registered) {
|
|
|
|
if (isInCall || isInCar)
|
|
|
|
am.removeOnModeChangedListener(listener);
|
|
|
|
mowner.stop();
|
|
|
|
registered = false;
|
|
|
|
else
|
|
|
|
}
|
|
|
|
mowner.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
Log.i("Audio mode registered=" + registered);
|
|
|
|
|
|
|
|
} catch (Throwable ex) {
|
|
|
|
|
|
|
|
Log.e(ex);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mutableUnseenNotify.observe(mowner, new Observer<List<TupleMessageEx>>() {
|
|
|
|
mutableUnseenNotify.observe(mowner, new Observer<List<TupleMessageEx>>() {
|
|
|
|
private final ExecutorService executor =
|
|
|
|
private final ExecutorService executor =
|
|
|
|