Clear cache after system update.

CarrierConfigLoader should clear its cache after system update just in
case some behavior related to carrier config has changed. In particular,
the default config app may have different config after an update, and
it's not guaranteed that the package version will have changed. (We
definitely don't get package install events if the package changes
during an OTA.)

Bug: 21999181
Change-Id: I6881afcb7754a74b14ada43c29d2496dd32c84f9
diff --git a/src/com/android/phone/CarrierConfigLoader.java b/src/com/android/phone/CarrierConfigLoader.java
index 5130a83..e0834c7 100644
--- a/src/com/android/phone/CarrierConfigLoader.java
+++ b/src/com/android/phone/CarrierConfigLoader.java
@@ -26,12 +26,14 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.ServiceConnection;
+import android.content.SharedPreferences;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.os.AsyncResult;
 import android.os.Binder;
+import android.os.Build;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
@@ -39,6 +41,7 @@
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.UserHandle;
+import android.preference.PreferenceManager;
 import android.service.carrier.CarrierIdentifier;
 import android.service.carrier.CarrierService;
 import android.service.carrier.ICarrierService;
@@ -113,6 +116,8 @@
     private static final int EVENT_BIND_DEFAULT_TIMEOUT = 10;
     // Bind timed out for a carrier app.
     private static final int EVENT_BIND_CARRIER_TIMEOUT = 11;
+    // Check if the system fingerprint has changed.
+    private static final int EVENT_CHECK_SYSTEM_UPDATE = 12;
 
     private static final int BIND_TIMEOUT_MILLIS = 10000;
 
@@ -121,6 +126,9 @@
     private static final String TAG_VERSION = "package_version";
     private static final String TAG_BUNDLE = "bundle_data";
 
+    // SharedPreferences key for last known build fingerprint.
+    private static final String KEY_FINGERPRINT = "build_fingerprint";
+
     // Handler to process various events.
     //
     // For each phoneId, the event sequence should be:
@@ -163,7 +171,7 @@
                     carrierPackageName = (String) msg.obj;
                     // Only update if there are cached config removed to avoid updating config
                     // for unrelated packages.
-                    if (deleteConfigForPackage(carrierPackageName)) {
+                    if (clearCachedConfigForPackage(carrierPackageName)) {
                         int numPhones = TelephonyManager.from(mContext).getPhoneCount();
                         for (int i = 0; i < numPhones; ++i) {
                             updateConfigForPhoneId(i);
@@ -301,6 +309,18 @@
                     }
                     broadcastConfigChangedIntent(phoneId);
                     break;
+
+                case EVENT_CHECK_SYSTEM_UPDATE:
+                    SharedPreferences sharedPrefs =
+                            PreferenceManager.getDefaultSharedPreferences(mContext);
+                    final String lastFingerprint = sharedPrefs.getString(KEY_FINGERPRINT, null);
+                    if (!Build.FINGERPRINT.equals(lastFingerprint)) {
+                        log("Build fingerprint changed. old: "
+                                + lastFingerprint + " new: " + Build.FINGERPRINT);
+                        clearCachedConfigForPackage(null);
+                        sharedPrefs.edit().putString(KEY_FINGERPRINT, Build.FINGERPRINT).apply();
+                    }
+                    break;
             }
         }
     };
@@ -328,6 +348,7 @@
         // Make this service available through ServiceManager.
         ServiceManager.addService(Context.CARRIER_CONFIG_SERVICE, this);
         log("CarrierConfigLoader has started");
+        mHandler.sendEmptyMessage(EVENT_CHECK_SYSTEM_UPDATE);
     }
 
     /**
@@ -534,14 +555,23 @@
     }
 
     /**
-     * Deletes all saved XML files associated with the given package name.
-     * Return false if can't find matching XML files.
+     * Clears cached carrier config.
+     * This deletes all saved XML files associated with the given package name. If packageName is
+     * null, then it deletes all saved XML files.
+     *
+     * @param packageName the name of a carrier package, or null if all cached config should be
+     *                    cleared.
+     * @return true iff one or more files were deleted.
      */
-    private boolean deleteConfigForPackage(final String packageName) {
+    private boolean clearCachedConfigForPackage(final String packageName) {
         File dir = mContext.getFilesDir();
         File[] packageFiles = dir.listFiles(new FilenameFilter() {
             public boolean accept(File dir, String filename) {
-                return filename.startsWith("carrierconfig-" + packageName + "-");
+                if (packageName != null) {
+                    return filename.startsWith("carrierconfig-" + packageName + "-");
+                } else {
+                    return filename.startsWith("carrierconfig-");
+                }
             }
         });
         if (packageFiles == null || packageFiles.length < 1) return false;
@@ -620,7 +650,7 @@
         // This method should block until deleting has completed, so that an error which prevents us
         // from clearing the cache is passed back to the carrier app. With the files successfully
         // deleted, this can return and we will eventually bind to the carrier app.
-        deleteConfigForPackage(callingPackageName);
+        clearCachedConfigForPackage(callingPackageName);
         updateConfigForPhoneId(phoneId);
     }