From 1632fae376eb460cf992e543b13c8690b71fe8d5 Mon Sep 17 00:00:00 2001 From: Marco Nelissen Date: Thu, 27 Mar 2014 13:25:14 -0700 Subject: 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 --- media/java/android/mtp/MtpDatabase.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/media/java/android/mtp/MtpDatabase.java b/media/java/android/mtp/MtpDatabase.java index 15ae238..fce3fd0 100755 --- a/media/java/android/mtp/MtpDatabase.java +++ b/media/java/android/mtp/MtpDatabase.java @@ -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); } } -- cgit v1.1