Added verbose debugging to diagnose swap state. Foreground/background service correctly.

This commit is contained in:
Peter Serwylo 2015-07-19 23:52:47 +10:00
parent 240bcce445
commit 9da6893ac3
8 changed files with 142 additions and 38 deletions

View File

@ -39,6 +39,7 @@ import org.fdroid.fdroid.localrepo.peers.BluetoothFinder;
import org.fdroid.fdroid.localrepo.peers.BonjourFinder; import org.fdroid.fdroid.localrepo.peers.BonjourFinder;
import org.fdroid.fdroid.localrepo.peers.Peer; import org.fdroid.fdroid.localrepo.peers.Peer;
import org.fdroid.fdroid.localrepo.type.BluetoothSwap; import org.fdroid.fdroid.localrepo.type.BluetoothSwap;
import org.fdroid.fdroid.localrepo.type.BonjourBroadcast;
import org.fdroid.fdroid.localrepo.type.SwapType; import org.fdroid.fdroid.localrepo.type.SwapType;
import org.fdroid.fdroid.localrepo.type.WifiSwap; import org.fdroid.fdroid.localrepo.type.WifiSwap;
import org.fdroid.fdroid.net.WifiStateChangeService; import org.fdroid.fdroid.net.WifiStateChangeService;
@ -412,12 +413,8 @@ public class SwapService extends Service {
return bluetoothSwap.isConnected(); return bluetoothSwap.isConnected();
} }
private boolean isWifiConnected() {
return !TextUtils.isEmpty(FDroidApp.ssid);
}
public boolean isBonjourDiscoverable() { public boolean isBonjourDiscoverable() {
return isWifiConnected() && isEnabled(); return wifiSwap.isConnected() && wifiSwap.getBonjour().isConnected();
} }
public boolean isScanningForPeers() { public boolean isScanningForPeers() {
@ -434,6 +431,7 @@ public class SwapService extends Service {
public static final String BONJOUR_STATE_CHANGE = "org.fdroid.fdroid.BONJOUR_STATE_CHANGE"; public static final String BONJOUR_STATE_CHANGE = "org.fdroid.fdroid.BONJOUR_STATE_CHANGE";
public static final String BLUETOOTH_STATE_CHANGE = "org.fdroid.fdroid.BLUETOOTH_STATE_CHANGE"; public static final String BLUETOOTH_STATE_CHANGE = "org.fdroid.fdroid.BLUETOOTH_STATE_CHANGE";
public static final String WIFI_STATE_CHANGE = "org.fdroid.fdroid.WIFI_STATE_CHANGE";
public static final String EXTRA_STARTING = "STARTING"; public static final String EXTRA_STARTING = "STARTING";
public static final String EXTRA_STARTED = "STARTED"; public static final String EXTRA_STARTED = "STARTED";
public static final String EXTRA_STOPPED = "STOPPED"; public static final String EXTRA_STOPPED = "STOPPED";
@ -459,7 +457,7 @@ public class SwapService extends Service {
return bluetoothSwap; return bluetoothSwap;
} }
public SwapType getWifiSwap() { public WifiSwap getWifiSwap() {
return wifiSwap; return wifiSwap;
} }
@ -485,8 +483,31 @@ public class SwapService extends Service {
Preferences.get().registerLocalRepoHttpsListeners(httpsEnabledListener); Preferences.get().registerLocalRepoHttpsListeners(httpsEnabledListener);
LocalBroadcastManager.getInstance(this).registerReceiver(onWifiChange, new IntentFilter(WifiStateChangeService.BROADCAST)); LocalBroadcastManager.getInstance(this).registerReceiver(onWifiChange, new IntentFilter(WifiStateChangeService.BROADCAST));
IntentFilter filter = new IntentFilter(BLUETOOTH_STATE_CHANGE);
filter.addAction(WIFI_STATE_CHANGE);
LocalBroadcastManager.getInstance(this).registerReceiver(receiveSwapStatusChanged, filter);
} }
/**
* Responsible for moving the service into the foreground or the background, depending on
* whether or not there are any swap services (i.e. bluetooth or wifi) running or not.
*/
private final BroadcastReceiver receiveSwapStatusChanged = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.hasExtra(EXTRA_STARTED)) {
if (getWifiSwap().isConnected() || getBluetoothSwap().isConnected()) {
attachService();
}
} else if (intent.hasExtra(EXTRA_STOPPED)) {
if (!getWifiSwap().isConnected() && !getBluetoothSwap().isConnected()) {
detachService();
}
}
}
};
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
@ -517,6 +538,7 @@ public class SwapService extends Service {
disableAllSwapping(); disableAllSwapping();
Preferences.get().unregisterLocalRepoHttpsListeners(httpsEnabledListener); Preferences.get().unregisterLocalRepoHttpsListeners(httpsEnabledListener);
LocalBroadcastManager.getInstance(this).unregisterReceiver(onWifiChange); LocalBroadcastManager.getInstance(this).unregisterReceiver(onWifiChange);
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiveSwapStatusChanged);
} }
private Notification createNotification() { private Notification createNotification() {

View File

@ -8,6 +8,8 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.util.Log; import android.util.Log;
import org.fdroid.fdroid.net.bluetooth.BluetoothServer;
// TODO: Still to be implemented // TODO: Still to be implemented
public class BluetoothFinder extends PeerFinder<BluetoothPeer> { public class BluetoothFinder extends PeerFinder<BluetoothPeer> {
@ -20,6 +22,9 @@ public class BluetoothFinder extends PeerFinder<BluetoothPeer> {
adapter = BluetoothAdapter.getDefaultAdapter(); adapter = BluetoothAdapter.getDefaultAdapter();
} }
private BroadcastReceiver deviceFoundReceiver;
private BroadcastReceiver scanCompleteReceiver;
@Override @Override
public void scan() { public void scan() {
@ -28,35 +33,45 @@ public class BluetoothFinder extends PeerFinder<BluetoothPeer> {
return; return;
} }
if (isScanning) {
// TODO: Can we reset the discovering timeout, so that it doesn't, e.g. time out
// in 3 seconds because we had already almost completed the previous scan?
Log.d(TAG, "Requested bluetooth scan when already scanning, will ignore request.");
return;
}
isScanning = true; isScanning = true;
final BroadcastReceiver deviceFoundReceiver = new BroadcastReceiver() { if (deviceFoundReceiver == null) {
@Override deviceFoundReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) { @Override
if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) { public void onReceive(Context context, Intent intent) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (BluetoothDevice.ACTION_FOUND.equals(intent.getAction())) {
onDeviceFound(device); BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
onDeviceFound(device);
}
} }
} };
}; context.registerReceiver(deviceFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
}
final BroadcastReceiver scanCompleteReceiver = new BroadcastReceiver() { if (scanCompleteReceiver == null) {
@Override scanCompleteReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) { @Override
Log.d(TAG, "Scan complete: " + intent.getAction()); public void onReceive(Context context, Intent intent) {
isScanning = false; if (isScanning) {
} Log.d(TAG, "Scan complete, but we haven't been asked to stop scanning yet, so will restart scan.");
}; startDiscovery();
}
}
};
context.registerReceiver(scanCompleteReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
}
context.registerReceiver(deviceFoundReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND)); startDiscovery();
context.registerReceiver(scanCompleteReceiver, new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)); }
private void startDiscovery() {
if (adapter.isDiscovering()) {
// TODO: Can we reset the discovering timeout, so that it doesn't, e.g. time out
// in 3 seconds because we had already almost completed the previous scan?
Log.d(TAG, "Requested bluetooth scan when already scanning, will cancel previous scan before continuing.");
adapter.cancelDiscovery();
}
if (!adapter.startDiscovery()) { if (!adapter.startDiscovery()) {
Log.e(TAG, "Couldn't start bluetooth scanning."); Log.e(TAG, "Couldn't start bluetooth scanning.");
@ -75,7 +90,9 @@ public class BluetoothFinder extends PeerFinder<BluetoothPeer> {
} }
private void onDeviceFound(BluetoothDevice device) { private void onDeviceFound(BluetoothDevice device) {
foundPeer(new BluetoothPeer(device)); if (device != null && device.getName() != null && device.getName().startsWith(BluetoothServer.BLUETOOTH_NAME_TAG)) {
foundPeer(new BluetoothPeer(device));
}
} }
} }

View File

@ -4,6 +4,7 @@ import android.bluetooth.BluetoothDevice;
import android.os.Parcel; import android.os.Parcel;
import org.fdroid.fdroid.R; import org.fdroid.fdroid.R;
import org.fdroid.fdroid.net.bluetooth.BluetoothServer;
// TODO: Still to be implemented. // TODO: Still to be implemented.
public class BluetoothPeer implements Peer { public class BluetoothPeer implements Peer {
@ -21,7 +22,7 @@ public class BluetoothPeer implements Peer {
@Override @Override
public String getName() { public String getName() {
return device.getName(); return device.getName().replaceAll("^" + BluetoothServer.BLUETOOTH_NAME_TAG, "");
} }
@Override @Override

View File

@ -103,5 +103,10 @@ public class BluetoothSwap extends SwapType {
@Override @Override
public void stop() {} public void stop() {}
@Override
protected String getBroadcastAction() {
return null;
}
} }
} }

View File

@ -30,9 +30,7 @@ public abstract class SwapType {
abstract public void stop(); abstract public void stop();
protected String getBroadcastAction() { abstract protected String getBroadcastAction();
return null;
}
protected final void setConnected(boolean connected) { protected final void setConnected(boolean connected) {
if (connected) { if (connected) {

View File

@ -10,6 +10,7 @@ import android.util.Log;
import org.fdroid.fdroid.FDroidApp; import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.Preferences; import org.fdroid.fdroid.Preferences;
import org.fdroid.fdroid.localrepo.SwapService;
import org.fdroid.fdroid.net.LocalHTTPD; import org.fdroid.fdroid.net.LocalHTTPD;
import org.fdroid.fdroid.net.WifiStateChangeService; import org.fdroid.fdroid.net.WifiStateChangeService;
@ -23,13 +24,21 @@ public class WifiSwap extends SwapType {
private Handler webServerThreadHandler = null; private Handler webServerThreadHandler = null;
private LocalHTTPD localHttpd; private LocalHTTPD localHttpd;
private final SwapType bonjourBroadcast; private final BonjourBroadcast bonjourBroadcast;
public WifiSwap(Context context) { public WifiSwap(Context context) {
super(context); super(context);
bonjourBroadcast = new BonjourBroadcast(context); bonjourBroadcast = new BonjourBroadcast(context);
} }
protected String getBroadcastAction() {
return SwapService.WIFI_STATE_CHANGE;
}
public BonjourBroadcast getBonjour() {
return bonjourBroadcast;
}
@Override @Override
public void start() { public void start() {

View File

@ -63,7 +63,7 @@ public class BluetoothServer extends Thread {
} }
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter(); BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
adapter.setName(deviceBluetoothName.replace(BLUETOOTH_NAME_TAG,"")); adapter.setName(deviceBluetoothName.replaceAll("/^" + BLUETOOTH_NAME_TAG + "/",""));
} }
@ -76,7 +76,7 @@ public class BluetoothServer extends Thread {
//store the original bluetoothname, and update this one to be unique //store the original bluetoothname, and update this one to be unique
deviceBluetoothName = adapter.getName(); deviceBluetoothName = adapter.getName();
if (!deviceBluetoothName.contains(BLUETOOTH_NAME_TAG)) if (!deviceBluetoothName.startsWith(BLUETOOTH_NAME_TAG))
adapter.setName(BLUETOOTH_NAME_TAG + deviceBluetoothName); adapter.setName(BLUETOOTH_NAME_TAG + deviceBluetoothName);

View File

@ -42,8 +42,12 @@ import org.fdroid.fdroid.localrepo.peers.Peer;
import org.fdroid.fdroid.net.bluetooth.BluetoothServer; import org.fdroid.fdroid.net.bluetooth.BluetoothServer;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
/** /**
* This activity will do its best to show the most relevant screen about swapping to the user. * This activity will do its best to show the most relevant screen about swapping to the user.
@ -156,6 +160,8 @@ public class SwapWorkflowActivity extends AppCompatActivity {
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
container = (ViewGroup) findViewById(R.id.fragment_container); container = (ViewGroup) findViewById(R.id.fragment_container);
new SwapDebug().logStatus();
} }
@Override @Override
@ -603,4 +609,50 @@ public class SwapWorkflowActivity extends AppCompatActivity {
} }
class SwapDebug {
private StringBuilder status = new StringBuilder("\n");
public void logStatus() {
append("service = " + service);
if (service != null) {
append("Swap Services:");
append(" service.getBluetoothSwap() = " + service.getBluetoothSwap());
append(" service.getBluetoothSwap().isConnected() = " + service.getBluetoothSwap().isConnected());
append(" service.getWifiSwap() = " + service.getWifiSwap());
append(" service.getWifiSwap().isConnected() = " + service.getWifiSwap().isConnected());
append(" service.getWifiSwap().getBonjour() = " + service.getWifiSwap().getBonjour());
append(" service.getWifiSwap().getBonjour().isConnected() = " + service.getWifiSwap().getBonjour().isConnected());
append("Discovering Services:");
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter != null) {
Map<Integer, String> scanModes = new HashMap<>(3);
scanModes.put(BluetoothAdapter.SCAN_MODE_CONNECTABLE, "SCAN_MODE_CONNECTABLE");
scanModes.put(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE, "SCAN_MODE_CONNECTABLE_DISCOVERABLE");
scanModes.put(BluetoothAdapter.SCAN_MODE_NONE, "SCAN_MODE_NONE");
append(" Bluetooth.isEnabled() = " + adapter.isEnabled());
append(" Bluetooth.isDiscovering() = " + adapter.isDiscovering());
append(" Bluetooth.getScanMode() = " + scanModes.get(adapter.getScanMode()));
}
}
Log.d("SwapStatus", status.toString());
new Timer().schedule(new TimerTask() {
@Override
public void run() {
new SwapDebug().logStatus();
}
},
2000
);
}
private void append(String line) {
status.append(" ").append(line).append("\n");
}
}
} }