MainActivity.java
7.36 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.xgimi.smartscreen;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.gimi.common.cinema.model.MessageEvent;
import com.gimi.common.cinema.utils.Utils;
import com.xgimi.gimicinema.R;
import com.xgimi.smartscreen.service.ConfigService;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
public static final int UPDATE_CURRENT_CTRL_SCREEN = 0x998;
private ListView lvDevice;
private TextView txt_header_current;
List<String> listData;
ArrayAdapter<String> adapter;
// private SwipeRefreshLayout swipeRefreshLayout;
private boolean isSearching = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_switch_control);
EventBus.getDefault().register(this);
startService(new Intent(this, ConfigService.class));
lvDevice = (ListView) findViewById(R.id.lv_device);
txt_header_current = (TextView) findViewById(R.id.txt_header_current);
lvDevice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ControlDialog dialog = new ControlDialog(MainActivity.this, listData.get(position));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //去除标题栏
dialog.show();
}
});
String string = Utils.getString(this, "screen-ip", "");
int anInt = Utils.getInt(this, "screen-sn", 0);
if (!TextUtils.isEmpty(string)) {
txt_header_current.setText("当前控制幕布IP:" + string + ", sn:" + anInt);
}
new SearchAsyncTask().execute();
}
@Override
protected void onPause() {
super.onPause();
isSearching = false;
// swipeRefreshLayout.setRefreshing(false);
}
@Override
protected void onResume() {
super.onResume();
}
private void resetCurrent() {
String string = Utils.getString(this, "screen-ip", "");
int anInt = Utils.getInt(this, "screen-sn", 0);
if (!TextUtils.isEmpty(string)) {
txt_header_current.setText("当前控制幕布IP:" + string + ",sn:" + anInt);
}
}
public void down(View view) {
MessageEvent msgEvent = new MessageEvent();
msgEvent.setEventId(SmartScreenBean.DOWNACTION);
msgEvent.setMessage(Utils.getString(this, "screen-ip", ""));
EventBus.getDefault().post(msgEvent);
}
public void up(View view) {
MessageEvent msgEvent = new MessageEvent();
msgEvent.setEventId(SmartScreenBean.UPACTION);
msgEvent.setMessage(Utils.getString(this, "screen-ip", ""));
EventBus.getDefault().post(msgEvent);
}
private class SearchAsyncTask extends AsyncTask<Void, Integer, String> {
InetAddress deviceAddress = null;
DatagramSocket socket = null;
@Override
protected String doInBackground(Void... params) {
isSearching = true;
try {
socket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
//接收
Thread receiveThread = new Thread(new Runnable() {
@Override
public void run() {
byte[] dataReceive = new byte[1024];
DatagramPacket packetReceive = new DatagramPacket(dataReceive, dataReceive.length);
try {
socket.receive(packetReceive);
String reply = new String(dataReceive, 0, packetReceive.getLength());
if (reply.contains("I'm a screen Controller")) {
isSearching = false;
deviceAddress = packetReceive.getAddress();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
receiveThread.start();
int cnt = 3; //搜索次数
while (isSearching) {
Log.i(TAG, "开始搜索...");
if (--cnt == 0) {
break;
}
//发送
String data = "Are You Screen Controller?";
DatagramPacket packetSend = null;
try {
packetSend = new DatagramPacket(data.getBytes(), data.getBytes().length,
InetAddress.getByName("255.255.255.255"), 12419);
} catch (UnknownHostException e) {
e.printStackTrace();
}
try {
socket.send(packetSend);
} catch (IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
receiveThread.interrupt();
if (deviceAddress != null) {
return deviceAddress.toString().substring(1); //去除ip地址前的斜杠
} else {
return null;
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// swipeRefreshLayout.setRefreshing(true);
if (lvDevice.getCount() != 0) {
adapter.clear();
adapter.notifyDataSetChanged();
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// swipeRefreshLayout.setRefreshing(false);
if (s != null) {
listData = new ArrayList<String>();
listData.add(s);
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, listData);
lvDevice.setAdapter(adapter);
} else {
Toast.makeText(MainActivity.this, "没有搜索到设备,再次下拉搜索", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMoonEvent(MessageEvent messageEvent) {
switch (messageEvent.getEventId()) {
case UPDATE_CURRENT_CTRL_SCREEN:
resetCurrent();
Log.d(TAG, messageEvent.getMessage());
break;
}
}
}