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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.libreoffice.impressremote;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map.Entry;
import org.libreoffice.impressremote.communication.CommunicationService;
import org.libreoffice.impressremote.communication.Server;
import org.libreoffice.impressremote.communication.Server.Protocol;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class SelectorActivity extends SherlockActivity {
private CommunicationService mCommunicationService;
private View mBluetoothContainer;
private LinearLayout mBluetoothList;
private View mNetworkContainer;
private LinearLayout mNetworkList;
private TextView mNoServerLabel;
private ActivityChangeBroadcastProcessor mBroadcastProcessor;
ProgressDialog mProgressDialog = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_selector);
IntentFilter aFilter = new IntentFilter(
CommunicationService.MSG_SERVERLIST_CHANGED);
aFilter.addAction(CommunicationService.STATUS_CONNECTION_FAILED);
mBroadcastProcessor = new ActivityChangeBroadcastProcessor(this);
mBroadcastProcessor.addToFilter(aFilter);
LocalBroadcastManager.getInstance(this).registerReceiver(mListener,
aFilter);
mBluetoothContainer = findViewById(R.id.selector_container_bluetooth);
mBluetoothList = (LinearLayout) findViewById(R.id.selector_list_bluetooth);
mNetworkContainer = findViewById(R.id.selector_container_network);
mNetworkList = (LinearLayout) findViewById(R.id.selector_list_network);
mNoServerLabel = (TextView) findViewById(R.id.selector_label_none);
refreshLists();
}
@Override
protected void onDestroy() {
super.onDestroy();
LocalBroadcastManager.getInstance(this).unregisterReceiver(mListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.selector_activity, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_selector_addServer:
AlertDialog.Builder builder;
AlertDialog alertDialog;
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.dialog_addserver,
null);
builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.setTitle(R.string.addserver);
builder.setPositiveButton(R.string.addserver_add,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
EditText aAddressEntry = (EditText) layout
.findViewById(R.id.addserver_addressentry);
EditText aNameEntry = (EditText) layout
.findViewById(R.id.addserver_nameentry);
CheckBox aRememberServer = (CheckBox) layout
.findViewById(R.id.addserver_remember);
mCommunicationService
.addServer(aAddressEntry
.getText()
.toString(),
aNameEntry.getText()
.toString(),
aRememberServer.isChecked());
refreshLists();
}
});
builder.setNegativeButton(R.string.addserver_cancel, null);
alertDialog = builder.create();
alertDialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
if (mCommunicationService != null)
mCommunicationService.stopSearching();
Intent aIntent = new Intent(this, CommunicationService.class);
stopService(aIntent);
super.onBackPressed();
}
@Override
protected void onResume() {
super.onResume();
mNetworkList.removeAllViews();
mBluetoothList.removeAllViews();
mNetworkServers.clear();
mBluetoothServers.clear();
doBindService();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mCommunicationService != null) {
mCommunicationService.stopSearching();
}
doUnbindService();
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
void doBindService() {
Intent aIntent = new Intent(this, CommunicationService.class);
startService(aIntent);
bindService(aIntent, mConnection, Context.BIND_IMPORTANT);
}
void doUnbindService() {
unbindService(mConnection);
mCommunicationService = null;
}
private ServiceConnection mConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName aClassName,
IBinder aService) {
mCommunicationService = ((CommunicationService.CBinder) aService)
.getService();
mCommunicationService.startSearching();
refreshLists();
}
@Override
public void onServiceDisconnected(ComponentName aClassName) {
mCommunicationService = null;
}
};
private BroadcastReceiver mListener = new BroadcastReceiver() {
@Override
public void onReceive(Context aContext, Intent aIntent) {
if (aIntent.getAction().equals(
CommunicationService.MSG_SERVERLIST_CHANGED)) {
refreshLists();
return;
} else if (aIntent.getAction().equals(
CommunicationService.STATUS_CONNECTION_FAILED)) {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
String aFormat = getResources().getString(
R.string.selector_dialog_connectionfailed);
String aDialogText = MessageFormat.format(aFormat,
mCommunicationService
.getPairingDeviceName());
AlertDialog.Builder builder = new AlertDialog.Builder(
SelectorActivity.this);
builder.setMessage(aDialogText)
.setCancelable(false)
.setPositiveButton(
R.string.selector_dialog_connectionfailed_ok,
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int id) {
dialog.dismiss();
}
});
builder.show();
}
}
mBroadcastProcessor.onReceive(aContext, aIntent);
}
};
private HashMap<Server, View> mBluetoothServers = new HashMap<Server, View>();
private HashMap<Server, View> mNetworkServers = new HashMap<Server, View>();
private void deleteServer(View aView) {
for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) {
System.out.println(aEntry.getKey().getName());
System.out.println(aView);
System.out.println(aEntry.getValue());
if (aEntry.getValue() == aView
.findViewById(R.id.selector_sub_label)
|| aEntry.getValue().findViewById(
R.id.selector_sub_label) == aView
.findViewById(R.id.selector_sub_label))
mCommunicationService.removeServer(aEntry.getKey());
}
}
private void refreshLists() {
if (mCommunicationService != null) {
Server[] aServers = mCommunicationService.getServers();
// Bluetooth -- Remove old
for (Entry<Server, View> aEntry : mBluetoothServers.entrySet()) {
if (!Arrays.asList(aServers).contains(aEntry.getKey())) {
mBluetoothServers.remove(aEntry.getKey());
mBluetoothList.removeView((View) aEntry.getValue()
.getParent());
}
}
// Network -- Remove old
for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) {
if (!Arrays.asList(aServers).contains(aEntry.getKey())) {
mNetworkServers.remove(aEntry.getKey());
mNetworkList.removeView((View) aEntry.getValue()
.getParent());
}
}
// Add all new
for (Server aServer : aServers) {
boolean aIsBluetooth = (aServer.getProtocol() == Protocol.BLUETOOTH);
HashMap<Server, View> aMap = aIsBluetooth ? mBluetoothServers
: mNetworkServers;
LinearLayout aLayout = aIsBluetooth ? mBluetoothList
: mNetworkList;
if (!aMap.containsKey(aServer)) {
View aView = getLayoutInflater()
.inflate(R.layout.activity_selector_sublayout_server,
null);
TextView aText = (TextView) aView
.findViewById(R.id.selector_sub_label);
aText.setOnClickListener(mClickListener);
aText.setText(aServer.getName());
aLayout.addView(aView);
aMap.put(aServer, aText);
// registerForContextMenu(aView);
registerForContextMenu(aText);
}
}
}
// Hide as necessary
mBluetoothContainer
.setVisibility((mBluetoothServers.size() != 0) ? View.VISIBLE
: View.GONE);
mNetworkContainer
.setVisibility((mNetworkServers.size() != 0) ? View.VISIBLE
: View.GONE);
mNoServerLabel.setVisibility(((mBluetoothServers.size() == 0) && (mNetworkServers
.size() == 0)) ? View.VISIBLE : View.GONE);
}
private OnClickListener mClickListener = new OnClickListener() {
@Override
public void onClick(View aView) {
mCommunicationService.stopSearching();
Server aDesiredServer = null;
if (mBluetoothServers.containsValue(aView)) {
for (Entry<Server, View> aEntry : mBluetoothServers.entrySet()) {
if (aEntry.getValue() == aView) {
aDesiredServer = aEntry.getKey();
}
}
} else if (mNetworkServers.containsValue(aView)) {
for (Entry<Server, View> aEntry : mNetworkServers.entrySet()) {
if (aEntry.getValue() == aView) {
aDesiredServer = aEntry.getKey();
}
}
}
if (aDesiredServer != null) {
mCommunicationService.connectTo(aDesiredServer);
// Connect Service and wait for broadcast
String aFormat = getResources().getString(
R.string.selector_dialog_connecting);
String aDialogText = MessageFormat.format(aFormat,
aDesiredServer.getName());
mProgressDialog = ProgressDialog.show(SelectorActivity.this,
"", aDialogText, true);
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mCommunicationService.disconnect();
}
});
}
}
};
View aLastSelected = null;
public void onCreateContextMenu(android.view.ContextMenu menu, View v,
android.view.ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
aLastSelected = v;
android.view.MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.selector_contextmenu, menu);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.selector_context_delete:
deleteServer(aLastSelected);
refreshLists();
return true;
}
return super.onContextItemSelected(item);
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|