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
|
package com.orbekk;
import java.net.InetAddress;
import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.orbekk.same.android.net.Broadcaster;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class SameControllerActivity extends Activity {
private Logger logger = LoggerFactory.getLogger(getClass());
private Messenger sameService = null;
private ServiceConnection sameConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
sameService = new Messenger(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
sameService = null;
}
};
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (SameService.AVAILABLE_NETWORKS_UPDATE.equals(intent.getAction())) {
ArrayList<String> networkList = intent.getStringArrayListExtra(
SameService.AVAILABLE_NETWORKS);
Toast.makeText(SameControllerActivity.this, networkList.toString(),
Toast.LENGTH_SHORT).show();
}
}
};
public void createNetwork(View unused) {
logger.info("Creating network");
Intent intent = new Intent(this, SameService.class);
intent.setAction("create");
startService(intent);
}
public void joinNetwork(View unused) {
logger.info("Joining network");
Intent intent = new Intent(this, SameService.class);
intent.setAction("join");
// InetAddress address = new Broadcaster(this).getBroadcastAddress();
EditText t = (EditText)findViewById(R.id.master_service_url);
intent.putExtra("masterUrl", t.getText().toString());
startService(intent);
}
private void showIpAddress() {
TextView t = (TextView)findViewById(R.id.ipAddress);
t.setText("My IP: ");
t.append(new Broadcaster(this).getWlanAddress().getHostAddress());
}
private void showBroadcastAddress() {
EditText t = (EditText)findViewById(R.id.master_service_url);
t.setText(new Broadcaster(this).getBroadcastAddress().getHostAddress());
}
public void doneClicked(View unused) {
finish();
}
public void searchNetworks(View unused) {
logger.info("SearchNetworks()");
// Toast.makeText(this, "Discovering networks", Toast.LENGTH_SHORT).show();
// final String[] listItems = new String[]{"First item", "Second item"};
// ListView list = (ListView)findViewById(R.id.network_list);
// list.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_item,
// listItems));
// Message message = Message.obtain(null, SameService.DISPLAY_MESSAGE,
// "Message from Activity!");
Message searchMessage = Message.obtain(null, SameService.SEARCH_NETWORKS);
try {
sameService.send(searchMessage);
} catch (RemoteException e) {
logger.error("Failed to send message", e);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
setContentView(R.layout.controller);
showIpAddress();
}
@Override public void onStart() {
super.onResume();
Intent intent = new Intent(this, SameService.class);
bindService(intent, sameConnection, Context.BIND_AUTO_CREATE);
IntentFilter sameServiceUpdates = new IntentFilter(
SameService.AVAILABLE_NETWORKS_UPDATE);
registerReceiver(broadcastReceiver, sameServiceUpdates);
}
@Override public void onStop() {
super.onStop();
if (sameService != null) {
unbindService(sameConnection);
}
unregisterReceiver(broadcastReceiver);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
|