2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-29 05:07:40 +00:00

Removing connectionReceivers of MainActivity and MprisActivity to prevent leaking the activities memory

This commit is contained in:
Albert Vaca 2013-08-20 09:58:17 +02:00
parent 099839565f
commit d3989d54e1
4 changed files with 85 additions and 58 deletions

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<module external.linked.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="android" name="Android">
<configuration>

View File

@ -162,12 +162,12 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
String deviceId = identityPackage.getString("deviceId");
Log.e("BroadcastTcpLinkProvider","addLink to "+deviceId);
BaseComputerLink oldLink = visibleComputers.get(deviceId);
visibleComputers.put(deviceId, link);
connectionAccepted(identityPackage, link);
if (oldLink != null) {
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
connectionLost(oldLink);
}
visibleComputers.put(deviceId, link);
connectionAccepted(identityPackage, link);
}
public BroadcastTcpLinkProvider(Context context) {

View File

@ -75,6 +75,53 @@ public class MainActivity extends Activity {
}
};
void updateComputerList() {
Log.e("MainActivity","updateComputerList");
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
HashMap<String, Device> devices = service.getDevices();
final String[] ids = devices.keySet().toArray(new String[devices.size()]);
final String[] names = new String[devices.size()];
for(int i = 0; i < ids.length; i++) {
Device d = devices.get(ids[i]);
names[i] = d.getName() + " " + d.isTrusted() + " " + d.isReachable();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
ListView list = (ListView)findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, names));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, DeviceActivity.class);
intent.putExtra("deviceId", ids[position]);
startActivity(intent);
}
});
}
});
}
});
};
BaseLinkProvider.ConnectionReceiver connectionReceiver = new BaseLinkProvider.ConnectionReceiver() {
@Override
public void onConnectionReceived(NetworkPackage identityPackage, BaseComputerLink link) {
updateComputerList();
}
@Override
public void onConnectionLost(BaseComputerLink link) {
updateComputerList();
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -89,55 +136,22 @@ public class MainActivity extends Activity {
@Override
public void onServiceStart(BackgroundService service) {
service.onNetworkChange();
service.addConnectionListener(connectionReceiver);
}
});
updateComputerList();
}
@Override
protected void onDestroy() {
super.onDestroy();
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(final BackgroundService service) {
final Runnable updateComputerList = new Runnable() {
@Override
public void run() {
Log.e("MainActivity","updateComputerList");
HashMap<String, Device> devices = service.getDevices();
final String[] ids = devices.keySet().toArray(new String[devices.size()]);
String[] names = new String[devices.size()];
for(int i = 0; i < ids.length; i++) {
Device d = devices.get(ids[i]);
names[i] = d.getName() + " " + d.isTrusted() + " " + d.isReachable();
}
ListView list = (ListView)findViewById(R.id.listView1);
list.setAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, names));
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, DeviceActivity.class);
intent.putExtra("deviceId", ids[position]);
startActivity(intent);
}
});
}
};
service.addConnectionListener(new BaseLinkProvider.ConnectionReceiver() {
@Override
public void onConnectionReceived(NetworkPackage identityPackage, BaseComputerLink link) {
runOnUiThread(updateComputerList);
}
@Override
public void onConnectionLost(BaseComputerLink link) {
runOnUiThread(updateComputerList);
}
});
updateComputerList.run();
public void onServiceStart(BackgroundService service) {
service.removeConnectionListener(connectionReceiver);
}
});
}

View File

@ -122,6 +122,29 @@ public class MprisActivity extends Activity {
}
BaseLinkProvider.ConnectionReceiver connectionReceiver = new BaseLinkProvider.ConnectionReceiver() {
@Override
public void onConnectionReceived(NetworkPackage identityPackage, BaseComputerLink link) {
connectToPlugin();
}
@Override
public void onConnectionLost(BaseComputerLink link) {
}
};
@Override
protected void onDestroy() {
super.onDestroy();
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.removeConnectionListener(connectionReceiver);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -132,17 +155,7 @@ public class MprisActivity extends Activity {
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
@Override
public void onServiceStart(BackgroundService service) {
service.addConnectionListener(new BaseLinkProvider.ConnectionReceiver() {
@Override
public void onConnectionReceived(NetworkPackage identityPackage, BaseComputerLink link) {
connectToPlugin();
}
@Override
public void onConnectionLost(BaseComputerLink link) {
}
});
service.addConnectionListener(connectionReceiver);
}
});
connectToPlugin();