From b5eed31d1bdfcb8e8b316c9c49331295718001bd Mon Sep 17 00:00:00 2001 From: Yu Ping Hu Date: Fri, 9 Aug 2013 11:50:48 -0700 Subject: [PATCH] Synchronize all access to the EmailServiceInfo map. Bug: 10255995 Change-Id: I36bee575a923b5c8bfbaea7babdf9f2c5e9bd378 --- .../EmailBroadcastProcessorService.java | 5 ++- .../email/service/EmailServiceUtils.java | 35 ++++++++++--------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/com/android/email/service/EmailBroadcastProcessorService.java b/src/com/android/email/service/EmailBroadcastProcessorService.java index aa8aaa155..8e301f275 100644 --- a/src/com/android/email/service/EmailBroadcastProcessorService.java +++ b/src/com/android/email/service/EmailBroadcastProcessorService.java @@ -252,9 +252,8 @@ public class EmailBroadcastProcessorService extends IntentService { // Fix periodic syncs. final Map syncIntervals = getSyncIntervals(); - final List serviceList = - EmailServiceUtils.getServiceInfoList(this); - for (final EmailServiceUtils.EmailServiceInfo service : serviceList) { + for (final EmailServiceUtils.EmailServiceInfo service + : EmailServiceUtils.getServiceInfoList(this)) { fixPeriodicSyncs(service.accountType, syncIntervals); } diff --git a/src/com/android/email/service/EmailServiceUtils.java b/src/com/android/email/service/EmailServiceUtils.java index e311bcfb6..7f7edd48d 100644 --- a/src/com/android/email/service/EmailServiceUtils.java +++ b/src/com/android/email/service/EmailServiceUtils.java @@ -55,22 +55,19 @@ import com.android.emailcommon.service.IEmailServiceCallback; import com.android.emailcommon.service.SearchParams; import com.android.emailcommon.service.SyncWindow; import com.android.mail.utils.LogUtils; -import com.google.common.collect.Lists; -import com.google.common.collect.Maps; +import com.google.common.collect.ImmutableMap; import org.xmlpull.v1.XmlPullParserException; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.Collection; import java.util.Map; /** * Utility functions for EmailService support. */ public class EmailServiceUtils { - private static final ArrayList sServiceList = Lists.newArrayList(); - private static final Map sServiceMap = Maps.newHashMap(); + private static Map sServiceMap = null; /** * Starts an EmailService by protocol @@ -217,20 +214,18 @@ public class EmailServiceUtils { return getServiceInfo(context, protocol); } - public static EmailServiceInfo getServiceInfo(Context context, String protocol) { - if (sServiceList.isEmpty()) { + public static synchronized EmailServiceInfo getServiceInfo(Context context, String protocol) { + if (sServiceMap == null) { findServices(context); } return sServiceMap.get(protocol); } - public static List getServiceInfoList(Context context) { - synchronized(sServiceList) { - if (sServiceList.isEmpty()) { - findServices(context); - } - return sServiceList; + public static synchronized Collection getServiceInfoList(Context context) { + if (sServiceMap == null) { + findServices(context); } + return sServiceMap.values(); } private static void finishAccountManagerBlocker(AccountManagerFuture future) { @@ -438,7 +433,13 @@ public class EmailServiceUtils { * Parse services.xml file to find our available email services */ @SuppressWarnings("unchecked") - private static synchronized void findServices(Context context) { + private static synchronized void findServices(final Context context) { + if (sServiceMap != null) { + return; + } + + final ImmutableMap.Builder builder = ImmutableMap.builder(); + try { final Resources res = context.getResources(); final XmlResourceParser xml = res.getXml(R.xml.services); @@ -512,8 +513,7 @@ public class EmailServiceUtils { throw new IllegalStateException( "Both class and intent action specified in service descriptor"); } - sServiceList.add(info); - sServiceMap.put(info.protocol, info); + builder.put(info.protocol, info); } } } catch (XmlPullParserException e) { @@ -521,6 +521,7 @@ public class EmailServiceUtils { } catch (IOException e) { // ignore } + sServiceMap = builder.build(); } private static Uri asCalendarSyncAdapter(Uri uri, String account, String accountType) {