More work on issue #17656716: Unhandled exception in Window Manager

Fix Slog.wtf to not acquire the activity manager lock in its code
path, so that it can never deadlock.  This was the original intention
of it, but part was missed.

Now we can put back in the code to detect when strict mode data is
getting large (a little more targeted now to the actual problem),
and use Slog.wtf to report it.  And as a bonus, when this happens
we will now clear all of the collected violations, to avoid getting
in to the bad case where IPCs start failing.  So this should be
good enough for L to fix the problem, with wtf reports for us to
see if the underlying issue is still happening.

Finally, switch a butch of stuff in the system process from Log.wtf
to Slog.wtf, since many of those are deadlocks waiting to happen.

Oh and fix a crash in the settings provider I noticed in APR.

Change-Id: I307d51b7a4db238fd1e5fe2f3f9bf1b9c6f1c041
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index ba5d11d..8084eda 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -351,8 +351,11 @@
         }
 
         public void onEvent(int event, String path) {
-            int modsInFlight = sKnownMutationsInFlight.get(mUserHandle).get();
-            if (modsInFlight > 0) {
+            final AtomicInteger mutationCount;
+            synchronized (SettingsProvider.this) {
+                mutationCount = sKnownMutationsInFlight.get(mUserHandle);
+            }
+            if (mutationCount != null && mutationCount.get() > 0) {
                 // our own modification.
                 return;
             }
@@ -952,8 +955,13 @@
         checkWritePermissions(args);
         SettingsCache cache = cacheForTable(callingUser, args.table);
 
-        final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
-        mutationCount.incrementAndGet();
+        final AtomicInteger mutationCount;
+        synchronized (this) {
+            mutationCount = sKnownMutationsInFlight.get(callingUser);
+        }
+        if (mutationCount != null) {
+            mutationCount.incrementAndGet();
+        }
         DatabaseHelper dbH = getOrEstablishDatabase(
                 TABLE_GLOBAL.equals(args.table) ? UserHandle.USER_OWNER : callingUser);
         SQLiteDatabase db = dbH.getWritableDatabase();
@@ -969,7 +977,9 @@
             db.setTransactionSuccessful();
         } finally {
             db.endTransaction();
-            mutationCount.decrementAndGet();
+            if (mutationCount != null) {
+                mutationCount.decrementAndGet();
+            }
         }
 
         sendNotify(uri, callingUser);
@@ -1105,12 +1115,19 @@
             return Uri.withAppendedPath(url, name);
         }
 
-        final AtomicInteger mutationCount = sKnownMutationsInFlight.get(desiredUserHandle);
-        mutationCount.incrementAndGet();
+        final AtomicInteger mutationCount;
+        synchronized (this) {
+            mutationCount = sKnownMutationsInFlight.get(callingUser);
+        }
+        if (mutationCount != null) {
+            mutationCount.incrementAndGet();
+        }
         DatabaseHelper dbH = getOrEstablishDatabase(desiredUserHandle);
         SQLiteDatabase db = dbH.getWritableDatabase();
         final long rowId = db.insert(args.table, null, initialValues);
-        mutationCount.decrementAndGet();
+        if (mutationCount != null) {
+            mutationCount.decrementAndGet();
+        }
         if (rowId <= 0) return null;
 
         SettingsCache.populate(cache, initialValues);  // before we notify
@@ -1137,12 +1154,19 @@
         }
         checkWritePermissions(args);
 
-        final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
-        mutationCount.incrementAndGet();
+        final AtomicInteger mutationCount;
+        synchronized (this) {
+            mutationCount = sKnownMutationsInFlight.get(callingUser);
+        }
+        if (mutationCount != null) {
+            mutationCount.incrementAndGet();
+        }
         DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
         SQLiteDatabase db = dbH.getWritableDatabase();
         int count = db.delete(args.table, args.where, args.args);
-        mutationCount.decrementAndGet();
+        if (mutationCount != null) {
+            mutationCount.decrementAndGet();
+        }
         if (count > 0) {
             invalidateCache(callingUser, args.table);  // before we notify
             sendNotify(url, callingUser);
@@ -1170,12 +1194,19 @@
         checkWritePermissions(args);
         checkUserRestrictions(initialValues.getAsString(Settings.Secure.NAME), callingUser);
 
-        final AtomicInteger mutationCount = sKnownMutationsInFlight.get(callingUser);
-        mutationCount.incrementAndGet();
+        final AtomicInteger mutationCount;
+        synchronized (this) {
+            mutationCount = sKnownMutationsInFlight.get(callingUser);
+        }
+        if (mutationCount != null) {
+            mutationCount.incrementAndGet();
+        }
         DatabaseHelper dbH = getOrEstablishDatabase(callingUser);
         SQLiteDatabase db = dbH.getWritableDatabase();
         int count = db.update(args.table, initialValues, args.where, args.args);
-        mutationCount.decrementAndGet();
+        if (mutationCount != null) {
+            mutationCount.decrementAndGet();
+        }
         if (count > 0) {
             invalidateCache(callingUser, args.table);  // before we notify
             sendNotify(url, callingUser);