Fix the default APN restoration thread always hold the TelephonyProvider lock.

The APN restoration with service acquired two locks and waited for binding service which caused other thread cannot access TelephonyProvider.
Since the symptom cannot be reproduced and there's no more information to find out the reason why it took so much time to bind service.
Add more log and release the lock to check if the binding is dead and make sure the TelephonyProvider won't be continuously locked.

Bug: 146932662
Test: atest TelephonyProviderTest
Change-Id: Id47bcb894b16844602d890e2f9b371c86dd9ccca
diff --git a/src/com/android/providers/telephony/TelephonyProvider.java b/src/com/android/providers/telephony/TelephonyProvider.java
index 27930dc..3c1c174 100644
--- a/src/com/android/providers/telephony/TelephonyProvider.java
+++ b/src/com/android/providers/telephony/TelephonyProvider.java
@@ -129,6 +129,7 @@
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -2575,6 +2576,7 @@
     private void restoreApnsWithService(int subId) {
         Context context = getContext();
         Resources r = context.getResources();
+        AtomicBoolean connectionBindingInvalid = new AtomicBoolean(false);
         ServiceConnection connection = new ServiceConnection() {
             @Override
             public void onServiceConnected(ComponentName className,
@@ -2593,6 +2595,24 @@
                     mIApnSourceService = null;
                 }
             }
+
+            @Override
+            public void onBindingDied(ComponentName name) {
+                loge("The binding to the apn service connection is dead: " + name);
+                synchronized (mLock) {
+                    connectionBindingInvalid.set(true);
+                    mLock.notifyAll();
+                }
+            }
+
+            @Override
+            public void onNullBinding(ComponentName name) {
+                loge("Null binding: " + name);
+                synchronized (mLock) {
+                    connectionBindingInvalid.set(true);
+                    mLock.notifyAll();
+                }
+            }
         };
 
         Intent intent = new Intent(IApnSourceService.class.getName());
@@ -2603,13 +2623,17 @@
             if (context.bindService(intent, connection, Context.BIND_IMPORTANT |
                         Context.BIND_AUTO_CREATE)) {
                 synchronized (mLock) {
-                    while (mIApnSourceService == null) {
+                    while (mIApnSourceService == null && !connectionBindingInvalid.get()) {
                         try {
                             mLock.wait();
                         } catch (InterruptedException e) {
                             loge("Error while waiting for service connection: " + e);
                         }
                     }
+                    if (connectionBindingInvalid.get()) {
+                        loge("The binding is invalid.");
+                        return;
+                    }
                     try {
                         ContentValues[] values = mIApnSourceService.getApns(subId);
                         if (values != null) {