Make setServer() safe to call multiple times

This makes it safe to call setServer() multiple times with the same
server, different servers, or null.

b/13622801

Change-Id: Id04440df720f830e67106eb543653ace42430d97
This commit is contained in:
Marco Nelissen
2014-03-27 13:25:14 -07:00
parent bc9a5a0fb7
commit 1632fae376

View File

@@ -202,12 +202,17 @@ public class MtpDatabase {
public void setServer(MtpServer server) {
mServer = server;
// always unregister before registering
try {
mContext.unregisterReceiver(mBatteryReceiver);
} catch (IllegalArgumentException e) {
// wasn't previously registered, ignore
}
// register for battery notifications when we are connected
if (server != null) {
mContext.registerReceiver(mBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
} else {
mContext.unregisterReceiver(mBatteryReceiver);
}
}