From 780a361bb4416d1b3e34ed8bb3dab213070fcd17 Mon Sep 17 00:00:00 2001 From: zachh Date: Wed, 16 Aug 2017 12:42:37 -0700 Subject: Init PhoneNumberFormattingTextWatcher on background thread. A strict violation was being caused by calling this on the main thread: android.telephony.PhoneNumberFormattingTextWatcher.(PhoneNumberFormattingTextWatcher.java:71) We now initialize the watcher on a background thread and pass it back to the main thread to be attached to the textview. Behavior before: Dialpad fragment would fail to appear until libphonenumber finished (verified by adding sleep and observing) Behavior after: Dialpad fragment loads immediately, but formatting is not enabled until libphonenumber finishes reading metadata. When adding sleeps, I could type "4084" before init finished and it would remain unformatted. When init finished I could type another character like "6" and the text view would be correctly updated to "408-46". Bug: 64716944 Test: manual PiperOrigin-RevId: 165480191 Change-Id: Ie1136a5a0e0b7ed66d4882e96c5830ca1e7523f0 --- .../dialer/app/dialpad/DialpadFragment.java | 36 ++++++++++++++++++++-- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'java/com/android/dialer/app') diff --git a/java/com/android/dialer/app/dialpad/DialpadFragment.java b/java/com/android/dialer/app/dialpad/DialpadFragment.java index 3cd3ac27c..d58caa50a 100644 --- a/java/com/android/dialer/app/dialpad/DialpadFragment.java +++ b/java/com/android/dialer/app/dialpad/DialpadFragment.java @@ -42,6 +42,7 @@ import android.provider.Contacts.People; import android.provider.Contacts.Phones; import android.provider.Contacts.PhonesColumns; import android.provider.Settings; +import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.support.design.widget.FloatingActionButton; import android.support.v4.content.ContextCompat; @@ -82,6 +83,9 @@ import com.android.dialer.callintent.CallInitiationType; import com.android.dialer.callintent.CallIntentBuilder; import com.android.dialer.calllogutils.PhoneAccountUtils; 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.dialpadview.DialpadKeyButton; import com.android.dialer.dialpadview.DialpadView; import com.android.dialer.location.GeoUtil; @@ -178,6 +182,8 @@ public class DialpadFragment extends Fragment private boolean mFirstLaunch = false; private boolean mAnimate = false; + private DialerExecutor initPhoneNumberFormattingTextWatcherExecutor; + /** * Determines whether an add call operation is requested. * @@ -346,6 +352,14 @@ public class DialpadFragment extends Fragment mCallStateReceiver = new CallStateReceiver(); getActivity().registerReceiver(mCallStateReceiver, callStateIntentFilter); } + + initPhoneNumberFormattingTextWatcherExecutor = + DialerExecutors.createUiTaskBuilder( + getFragmentManager(), + "DialpadFragment.initPhoneNumberFormattingTextWatcher", + new InitPhoneNumberFormattingTextWatcherWorker()) + .onSuccess(watcher -> mDialpadView.getDigits().addTextChangedListener(watcher)) + .build(); Trace.endSection(); } @@ -371,9 +385,8 @@ public class DialpadFragment extends Fragment mDigits.addTextChangedListener(this); mDigits.setElegantTextHeight(false); - PhoneNumberFormattingTextWatcher watcher = - new PhoneNumberFormattingTextWatcher(GeoUtil.getCurrentCountryIso(getActivity())); - mDigits.addTextChangedListener(watcher); + initPhoneNumberFormattingTextWatcherExecutor.executeSerial( + GeoUtil.getCurrentCountryIso(getActivity())); // Check for the presence of the keypad View oneButton = fragmentView.findViewById(R.id.one); @@ -1707,4 +1720,21 @@ public class DialpadFragment extends Fragment } } } + + /** + * Input: the ISO 3166-1 two letters country code of the country the user is in + * + *

Output: PhoneNumberFormattingTextWatcher. Note: It is unusual to return a non-data value + * from a worker, but it is a limitation in libphonenumber API that the watcher cannot be + * initialized on the main thread. + */ + private static class InitPhoneNumberFormattingTextWatcherWorker + implements Worker { + + @Nullable + @Override + public PhoneNumberFormattingTextWatcher doInBackground(@Nullable String countryCode) { + return new PhoneNumberFormattingTextWatcher(countryCode); + } + } } -- cgit v1.2.3