-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathCarConnectionStateReceiver.java
More file actions
62 lines (51 loc) · 2.43 KB
/
Copy pathCarConnectionStateReceiver.java
File metadata and controls
62 lines (51 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.schabi.newpipe.util;
import android.content.*;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;
import androidx.car.app.connection.CarConnection;
import org.schabi.newpipe.player.PlayerBinderInterface;
import org.schabi.newpipe.player.mediasession.PlayerServiceInterface;
import project.pipepipe.app.service.PlaybackService;
public class CarConnectionStateReceiver extends BroadcastReceiver {
private static final String TAG = "CarConnectionReceiver";
private static volatile boolean isCarConnected = false;
@Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && CarConnection.ACTION_CAR_CONNECTION_UPDATED.equals(intent.getAction())) {
int connectionState = intent.getIntExtra(CarConnection.CAR_CONNECTION_STATE, -1);
boolean isConnected = (connectionState != CarConnection.CONNECTION_TYPE_NOT_CONNECTED);
Log.d(TAG, "Android Auto connection state changed. Is connected: " + isConnected);
shutdownOldService(context); // shutdown old Service or phone and auto are interacting to different service
setCarConnectionState(isConnected);
}
}
public static boolean isCarConnected() {
Log.i(TAG, String.valueOf(isCarConnected));
return isCarConnected;
}
public static void setCarConnectionState(boolean connected) {
if (isCarConnected != connected) {
isCarConnected = connected;
}
}
private static void shutdownOldService(Context context) {
if (ThemeHelper.shouldUseExperimentalNewUi(context)) {
context.stopService(new Intent(context, PlaybackService.class));
return;
}
ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// 获取服务引用
PlayerBinderInterface binder = (PlayerBinderInterface) service;
PlayerServiceInterface binderService = binder.getService();
binderService.stopService();
}
@Override
public void onServiceDisconnected(ComponentName name) {}
};
Intent serviceIntent = new Intent(context, DeviceUtils.getPlayerServiceClass());
context.bindService(serviceIntent, connection, Context.BIND_AUTO_CREATE);
}
}