mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-02 15:15:09 +00:00
Removing connectionReceivers of MainActivity and MprisActivity to prevent leaking the activities memory
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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">
|
<component name="FacetManager">
|
||||||
<facet type="android" name="Android">
|
<facet type="android" name="Android">
|
||||||
<configuration>
|
<configuration>
|
||||||
|
@@ -162,12 +162,12 @@ public class BroadcastTcpLinkProvider extends BaseLinkProvider {
|
|||||||
String deviceId = identityPackage.getString("deviceId");
|
String deviceId = identityPackage.getString("deviceId");
|
||||||
Log.e("BroadcastTcpLinkProvider","addLink to "+deviceId);
|
Log.e("BroadcastTcpLinkProvider","addLink to "+deviceId);
|
||||||
BaseComputerLink oldLink = visibleComputers.get(deviceId);
|
BaseComputerLink oldLink = visibleComputers.get(deviceId);
|
||||||
|
visibleComputers.put(deviceId, link);
|
||||||
|
connectionAccepted(identityPackage, link);
|
||||||
if (oldLink != null) {
|
if (oldLink != null) {
|
||||||
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
|
Log.e("BroadcastTcpLinkProvider","Removing old connection to same device");
|
||||||
connectionLost(oldLink);
|
connectionLost(oldLink);
|
||||||
}
|
}
|
||||||
visibleComputers.put(deviceId, link);
|
|
||||||
connectionAccepted(identityPackage, link);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BroadcastTcpLinkProvider(Context context) {
|
public BroadcastTcpLinkProvider(Context context) {
|
||||||
|
@@ -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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -89,55 +136,22 @@ public class MainActivity extends Activity {
|
|||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.onNetworkChange();
|
service.onNetworkChange();
|
||||||
|
|
||||||
|
service.addConnectionListener(connectionReceiver);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
updateComputerList();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onDestroy() {
|
||||||
|
super.onDestroy();
|
||||||
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(MainActivity.this, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(final BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
|
service.removeConnectionListener(connectionReceiver);
|
||||||
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();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@@ -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
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -132,17 +155,7 @@ public class MprisActivity extends Activity {
|
|||||||
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
|
BackgroundService.RunCommand(MprisActivity.this, new BackgroundService.InstanceCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onServiceStart(BackgroundService service) {
|
public void onServiceStart(BackgroundService service) {
|
||||||
service.addConnectionListener(new BaseLinkProvider.ConnectionReceiver() {
|
service.addConnectionListener(connectionReceiver);
|
||||||
@Override
|
|
||||||
public void onConnectionReceived(NetworkPackage identityPackage, BaseComputerLink link) {
|
|
||||||
connectToPlugin();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onConnectionLost(BaseComputerLink link) {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
connectToPlugin();
|
connectToPlugin();
|
||||||
|
Reference in New Issue
Block a user