Polish MemoryIntArray

1. Add close guard
2. Adopt instead of clone the ahsmem fd to fix a dangling fd
3. Clear only the return flag when writing fd to parcel
4. Immediately destroy remote MemoryIntArray if stale
5. Throw Java exception if someone closed the fd under us

Change-Id: I85533fec336c40e3380e10d5448e18c9616ec341
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java b/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java
index f4f7986..222cc5c 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/GenerationRegistry.java
@@ -32,7 +32,7 @@
  * processes can read to determine if their local caches are stale,
  */
 final class GenerationRegistry {
-    private static final String LOG_TAG = "GenerationTracker";
+    private static final String LOG_TAG = "GenerationRegistry";
 
     private static final boolean DEBUG = false;
 
@@ -120,6 +120,9 @@
             final int size = 1 + 2 + 10 + 2 * UserManager.getMaxSupportedUsers();
             try {
                 mBackingStore = new MemoryIntArray(size, false);
+                if (DEBUG) {
+                    Slog.e(LOG_TAG, "Created backing store " + mBackingStore);
+                }
             } catch (IOException e) {
                 Slog.e(LOG_TAG, "Error creating generation tracker", e);
             }
@@ -131,6 +134,9 @@
         if (mBackingStore != null) {
             try {
                 mBackingStore.close();
+                if (DEBUG) {
+                    Slog.e(LOG_TAG, "Destroyed backing store " + mBackingStore);
+                }
             } catch (IOException e) {
                 Slog.e(LOG_TAG, "Cannot close generation memory array", e);
             }