From d8a0585d28f966733e4d5f8f2fc4f3bdb3ad82b7 Mon Sep 17 00:00:00 2001 From: zachh Date: Thu, 17 Aug 2017 17:38:03 -0700 Subject: Workaround bug where system clobbered StrictMode thread policy. There is a bug (b/36951662) in many versions of the system where if you set the thread policy in Application.onCreate it could get clobbered. When this happened, strict mode violations could sometimes go unreported. This CL works around the problem by re-enabling our custom policy after onCreate by posting to the main thread. Additionally, it copies most of java/com/google/android/libraries/strictmode/StrictModeUtils.java into our codebase and uses it to better handle known violations. Finally, fixed or bypassed a few violations which were exposed by this change. Bug: 64690143,64772057 Test: observed that certain strict mode violations which were flaky before now consistently reproduce PiperOrigin-RevId: 165653027 Change-Id: If58966cfce251a22945f3aada6570b303c6af0ee --- .../com/android/dialer/app/list/ListsFragment.java | 2 +- .../dialer/app/list/RegularSearchFragment.java | 46 +++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) (limited to 'java/com/android/dialer/app') diff --git a/java/com/android/dialer/app/list/ListsFragment.java b/java/com/android/dialer/app/list/ListsFragment.java index dbb6c8b5c..86a3d2fbb 100644 --- a/java/com/android/dialer/app/list/ListsFragment.java +++ b/java/com/android/dialer/app/list/ListsFragment.java @@ -98,7 +98,7 @@ public class ListsFragment extends Fragment implements OnPageChangeListener, Lis LogUtil.d("ListsFragment.onCreate", null); Trace.beginSection(TAG + " onCreate"); super.onCreate(savedInstanceState); - mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); + mPrefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); Trace.endSection(); } diff --git a/java/com/android/dialer/app/list/RegularSearchFragment.java b/java/com/android/dialer/app/list/RegularSearchFragment.java index 728948bfc..73120c547 100644 --- a/java/com/android/dialer/app/list/RegularSearchFragment.java +++ b/java/com/android/dialer/app/list/RegularSearchFragment.java @@ -18,7 +18,10 @@ package com.android.dialer.app.list; import static android.Manifest.permission.READ_CONTACTS; import android.app.Activity; +import android.content.Context; import android.content.pm.PackageManager; +import android.os.Bundle; +import android.support.annotation.Nullable; import android.support.v13.app.FragmentCompat; import android.view.LayoutInflater; import android.view.ViewGroup; @@ -27,7 +30,11 @@ import com.android.contacts.common.list.PinnedHeaderListView; import com.android.dialer.app.R; import com.android.dialer.callintent.CallInitiationType; import com.android.dialer.common.LogUtil; +import com.android.dialer.common.concurrent.DialerExecutor; +import com.android.dialer.common.concurrent.DialerExecutor.Worker; +import com.android.dialer.common.concurrent.DialerExecutors; import com.android.dialer.phonenumbercache.CachedNumberLookupService; +import com.android.dialer.phonenumbercache.CachedNumberLookupService.CachedContactInfo; import com.android.dialer.phonenumbercache.PhoneNumberCache; import com.android.dialer.util.PermissionsUtil; import com.android.dialer.widget.EmptyContentView; @@ -43,6 +50,8 @@ public class RegularSearchFragment extends SearchFragment private static final int SEARCH_DIRECTORY_RESULT_LIMIT = 5; protected String mPermissionToRequest; + private DialerExecutor addContactTask; + public RegularSearchFragment() { configureDirectorySearch(); } @@ -52,6 +61,18 @@ public class RegularSearchFragment extends SearchFragment setDirectoryResultLimit(SEARCH_DIRECTORY_RESULT_LIMIT); } + @Override + public void onCreate(Bundle savedState) { + super.onCreate(savedState); + + addContactTask = + DialerExecutors.createUiTaskBuilder( + getFragmentManager(), + "RegularSearchFragment.addContact", + new AddContactWorker(getContext().getApplicationContext())) + .build(); + } + @Override protected void onCreateView(LayoutInflater inflater, ViewGroup container) { super.onCreateView(inflater, container); @@ -73,8 +94,9 @@ public class RegularSearchFragment extends SearchFragment PhoneNumberCache.get(getContext()).getCachedNumberLookupService(); if (cachedNumberLookupService != null) { final RegularSearchListAdapter adapter = (RegularSearchListAdapter) getAdapter(); - cachedNumberLookupService.addContact( - getContext(), adapter.getContactInfo(cachedNumberLookupService, position)); + CachedContactInfo cachedContactInfo = + adapter.getContactInfo(cachedNumberLookupService, position); + addContactTask.executeSerial(cachedContactInfo); } } @@ -152,4 +174,24 @@ public class RegularSearchFragment extends SearchFragment boolean isNearbyPlacesSearchEnabled(); } + + private static class AddContactWorker implements Worker { + + private final Context appContext; + + private AddContactWorker(Context appContext) { + this.appContext = appContext; + } + + @Nullable + @Override + public Void doInBackground(@Nullable CachedContactInfo contactInfo) throws Throwable { + CachedNumberLookupService cachedNumberLookupService = + PhoneNumberCache.get(appContext).getCachedNumberLookupService(); + if (cachedNumberLookupService != null) { + cachedNumberLookupService.addContact(appContext, contactInfo); + } + return null; + } + } } -- cgit v1.2.3