Broadcast credential storage changes.

Bug: 6009802

Cherry pick 0cb57ed171d7898f5f052e86e485771cbcbadcd8

When credential storage changes (adding/deleting a user CA) or
reset, send a broadcast intent so user can update any cached
credential storage state.

Change-Id: I3a3e93a0408e6db281e850268fe688182bfa4aa7
diff --git a/src/com/android/keychain/KeyChainService.java b/src/com/android/keychain/KeyChainService.java
index 418f575..1ab3ad3 100644
--- a/src/com/android/keychain/KeyChainService.java
+++ b/src/com/android/keychain/KeyChainService.java
@@ -28,6 +28,7 @@
 import android.os.IBinder;
 import android.security.Credentials;
 import android.security.IKeyChainService;
+import android.security.KeyChain;
 import android.security.KeyStore;
 import android.util.Log;
 import java.io.ByteArrayInputStream;
@@ -39,6 +40,7 @@
 import org.apache.harmony.xnet.provider.jsse.TrustedCertificateStore;
 
 public class KeyChainService extends IntentService {
+
     private static final String TAG = "KeyChain";
 
     private static final String DATABASE_NAME = "grants.db";
@@ -123,6 +125,7 @@
             } catch (CertificateException e) {
                 throw new IllegalStateException(e);
             }
+            broadcastStorageChange();
         }
 
         private X509Certificate parseCertificate(byte[] bytes) throws CertificateException {
@@ -144,14 +147,20 @@
                         }
                     }
                 }
-                return ok;
             }
+            broadcastStorageChange();
+            return ok;
         }
 
         @Override public boolean deleteCaCertificate(String alias) {
             // only Settings should be able to delete
             checkSystemCaller();
-            return deleteCertificateEntry(alias);
+            boolean ok = true;
+            synchronized (mTrustedCertificateStore) {
+                ok = deleteCertificateEntry(alias);
+            }
+            broadcastStorageChange();
+            return ok;
         }
 
         private boolean deleteCertificateEntry(String alias) {
@@ -196,6 +205,7 @@
         @Override public void setGrant(int uid, String alias, boolean value) {
             checkSystemCaller();
             setGrantInternal(mDatabaseHelper.getWritableDatabase(), uid, alias, value);
+            broadcastStorageChange();
         }
     };
 
@@ -289,4 +299,10 @@
             db.endTransaction();
         }
     }
+
+    private void broadcastStorageChange() {
+        Intent intent = new Intent(KeyChain.ACTION_STORAGE_CHANGED);
+        sendBroadcast(intent);
+    }
+
 }