CinemaControlService.java
6.66 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright 2016 wugian
*
* 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 com.xgimi.gimicinema.service;
import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.xgimi.gimicinema.ICinemaControl;
import com.xgimi.gimicinema.ICinemaSMC;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class CinemaControlService extends Service {
private ArrayList<String> movies = new ArrayList<>();
private long duration = 0;
private long currentPosition = 0;
private int currentState = 0;
private String currentPath = null;
private final ICinemaControl.Stub cinemaControl = new ICinemaControl.Stub() {
@Override
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public void setMovieDuration(long duration) throws RemoteException {
// Log.d("aidl", "setMovieDuration: " + duration);
CinemaControlService.this.duration = duration;
}
@Override
public long getMovieDuration() throws RemoteException {
return duration;
}
@Override
public void setCurrentMoviePosition(long duration) throws RemoteException {
// Log.d("aidl", "setCurrentMoviePosition: " + duration);
currentPosition = duration;
}
@Override
public long getCurrentMoviePosition() throws RemoteException {
return currentPosition;
}
@Override
public int getCurrentStatus() throws RemoteException {
return currentState;
}
@Override
public void setCurrentStatus(int state) throws RemoteException {
Log.d("aidl", "setCurrentStatus: " + state);
if (state != currentState) {
currentState = state;
Log.d("aidl", "state change," + duration + "," + currentPosition);
try {
if (cinemaSMC != null) {
Log.d("aidl", "state change cinemaSMC not null");
switch (state) {
case 0:
cinemaSMC.open();
break;
case 1:
cinemaSMC.close();
break;
case 2:
cinemaSMC.open();
break;
}
} else {
Log.d("aidl", "state change cinemaSMC null");
}
} catch (RemoteException e) {
Log.d("aidl", "RemoteException:" + e.getMessage());
e.printStackTrace();
}
}
}
@Override
public void addPlayPath(String path) throws RemoteException {
movies.add(path);//strict contract
}
@Override
public void setCurrentPlayPath(String path) throws RemoteException {
// Log.d("aidl", "setCurrentPlayPath: " + path);
CinemaControlService.this.currentPath = path;
}
@Override
public String getCurrentPath() throws RemoteException {
return CinemaControlService.this.currentPath;
}
@Override
public List<String> getPlayList() throws RemoteException {
return movies;
}
@Override
public void setPlayList(List<String> movies) throws RemoteException {
CinemaControlService.this.movies.clear();
if (movies == null || movies.size() == 0) {
return;
}
CinemaControlService.this.movies.addAll(movies);
}
};
public CinemaControlService() {
}
@Override
public void onCreate() {
super.onCreate();
if (!mBound) {
attemptToBindService();
}
}
@Override
public IBinder onBind(Intent intent) {
return cinemaControl;
}
//由AIDL文件生成的Java类
private ICinemaSMC cinemaSMC = null;
//标志当前与服务端连接状况的布尔值,false为未连接,true为连接中
private boolean mBound = false;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
cinemaSMC = ICinemaSMC.Stub.asInterface(service);
mBound = true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBound = false;
}
};
/**
* 尝试与服务端建立连接
*/
private void attemptToBindService() {
Intent intent = new Intent();
intent.setPackage("com.qnbar.smc");
intent.setAction("com.xgimi.cinemasmc");
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
private boolean isSMCRun(Context context) {
return isServiceRunning(context, "com.qnbar.smc.service.SMCService");
}
private boolean isServiceRunning(Context context, String className) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
boolean isRunning = false;
List serviceList = activityManager.getRunningServices(2147483647);
if (serviceList.size() <= 0) {
return false;
} else {
Iterator i$ = serviceList.iterator();
while (i$.hasNext()) {
ActivityManager.RunningServiceInfo aServiceList = (ActivityManager.RunningServiceInfo) i$.next();
if (aServiceList.service.getClassName().equals(className)) {
isRunning = true;
break;
}
}
Log.i("SystemUtils", "service is running?==" + isRunning);
return isRunning;
}
}
}