Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1632fae3 authored by Marco Nelissen's avatar Marco Nelissen
Browse files

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
parent bc9a5a0f
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -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);
        }
    }