Simplify and fix management of the server lists

The existing code in refreshLists() was somewhat hard to understand, and it
actually crashed (if uncommented-out). Now it simply empties the maps and view
lists and then rebuilds them.

The visible end result, at least for me, is that I no longer get duplicate
servers in the list...

Change-Id: I1543292e219e666e7dcbc68473f40a11e2eb3381
This commit is contained in:
Tor Lillqvist
2013-02-13 00:34:49 +02:00
parent 0d18ec16be
commit 5e037cef38
3 changed files with 34 additions and 39 deletions

View File

@@ -9,8 +9,8 @@
package org.libreoffice.impressremote;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import org.libreoffice.impressremote.communication.CommunicationService;
@@ -267,27 +267,18 @@ public class SelectorActivity extends SherlockActivity {
private void refreshLists() {
if (mCommunicationService != null) {
Server[] aServers = mCommunicationService.getServers();
List<Server> aServers = mCommunicationService.getServers();
Log.i(Globals.TAG, "SelectorActivity.refreshLists: got " + aServers.size() + " servers");
// Simply replace the lists... first clear the old lists,
// Then add those currently found.
mNetworkServers.clear();
mBluetoothServers.clear();
mNetworkList.removeAllViews();
mBluetoothList.removeAllViews();
/* TODO: this crashes currently - some concurrent modification on mBluetoothServers
// 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
@@ -295,22 +286,19 @@ public class SelectorActivity extends SherlockActivity {
LinearLayout aLayout = aIsBluetooth ? mBluetoothList
: mNetworkList;
if (!aMap.containsKey(aServer)) {
View aView = getLayoutInflater()
.inflate(R.layout.activity_selector_sublayout_server,
null);
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);
}
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);
}
}

View File

@@ -34,10 +34,10 @@ public class BluetoothFinder {
public BluetoothFinder(Context aContext) {
mContext = aContext;
mAdapter = BluetoothAdapter.getDefaultAdapter();
}
public void startFinding() {
Log.i(Globals.TAG, "BluetoothFinder.startFinding(): mAdapter=" + mAdapter);
if (mAdapter == null) {
return; // No bluetooth adapter found (emulator, special devices)
}
@@ -49,6 +49,7 @@ public class BluetoothFinder {
}
public void stopFinding() {
Log.i(Globals.TAG, "BluetoothFinder.stopFinding(): mAdapter=" + mAdapter);
if (mAdapter == null) {
return; // No bluetooth adapter found (emulator, special devices)
}
@@ -57,6 +58,7 @@ public class BluetoothFinder {
mContext.unregisterReceiver(mReceiver);
} catch (IllegalArgumentException e) {
// The receiver wasn't registered
Log.i(Globals.TAG, "BluetoothFinder.stopFinding: " + e);
}
}
@@ -70,6 +72,7 @@ public class BluetoothFinder {
@Override
public void onReceive(Context context, Intent aIntent) {
Log.i(Globals.TAG, "BluetoothFinder: BroadcastReceiver.onReceive: aIntent=" + aIntent);
if (aIntent.getAction().equals(BluetoothDevice.ACTION_FOUND)) {
BluetoothDevice aDevice = (BluetoothDevice) aIntent.getExtras()
.get(BluetoothDevice.EXTRA_DEVICE);

View File

@@ -11,9 +11,11 @@ package org.libreoffice.impressremote.communication;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.libreoffice.impressremote.Globals;
import org.libreoffice.impressremote.communication.Server.Protocol;
import android.app.Service;
@@ -23,6 +25,7 @@ import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
@@ -126,6 +129,7 @@ public class CommunicationService extends Service implements Runnable {
private boolean mBluetoothPreviouslyEnabled;
public void startSearching() {
Log.i(Globals.TAG, "CommunicationService.startSearching()");
SharedPreferences aPref = PreferenceManager.getDefaultSharedPreferences(this);
boolean bEnableWifi = aPref.getBoolean("option_enablewifi", false);
if (bEnableWifi)
@@ -140,12 +144,12 @@ public class CommunicationService extends Service implements Runnable {
}
public void stopSearching() {
Log.i(Globals.TAG, "CommunicationService.stopSearching()");
mNetworkFinder.stopFinding();
mBluetoothFinder.stopFinding();
BluetoothAdapter aAdapter = BluetoothAdapter.getDefaultAdapter();
if (aAdapter != null) {
if (!mBluetoothPreviouslyEnabled) {
aAdapter.disable();
}
}
@@ -253,12 +257,12 @@ public class CommunicationService extends Service implements Runnable {
return mTransmitter;
}
public Server[] getServers() {
public List<Server> getServers() {
ArrayList<Server> aServers = new ArrayList<Server>();
aServers.addAll(mNetworkFinder.getServerList());
aServers.addAll(mBluetoothFinder.getServerList());
aServers.addAll(mManualServers.values());
return aServers.toArray(new Server[aServers.size()]);
return aServers;
}
public SlideShow getSlideShow() {