Fix issue #21571700: Need to be smarter about how foreground...

...services get out of app idle

Introduce a new process state to even better distinguish foreground
services from other states.  Rework the INTERACTION reporting to
usage stats to do it less when the screen is off -- require that
an app sit in the foreground service or top activity state for
at least 30 minutes before we consider it an interaction.

Also eradicate a bunch of logging in package manager.

Change-Id: I94249e67f9a9c62e9a92ae104710e6747b16327e
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 1046b29..e81bfce 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -355,6 +355,10 @@
     // giving up on them and unfreezing the screen.
     static final int USER_SWITCH_TIMEOUT = 2*1000;
 
+    // This is the amount of time an app needs to be running a foreground service before
+    // we will consider it to be doing interaction for usage stats.
+    static final int SERVICE_USAGE_INTERACTION_TIME = 30*60*1000;
+
     // Maximum number of users we allow to be running at a time.
     static final int MAX_RUNNING_USERS = 3;
 
@@ -17781,13 +17785,13 @@
                                     // give them the best state after that.
                                     if ((cr.flags&Context.BIND_FOREGROUND_SERVICE) != 0) {
                                         clientProcState =
-                                                ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
+                                                ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
                                     } else if (mWakefulness
                                                     == PowerManagerInternal.WAKEFULNESS_AWAKE &&
                                             (cr.flags&Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE)
                                                     != 0) {
                                         clientProcState =
-                                                ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
+                                                ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
                                     } else {
                                         clientProcState =
                                                 ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
@@ -17901,7 +17905,7 @@
                         // into the top state, since they are not on top.  Instead
                         // give them the best state after that.
                         clientProcState =
-                                ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
+                                ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
                     }
                 }
                 if (procState > clientProcState) {
@@ -17941,7 +17945,7 @@
                 case ActivityManager.PROCESS_STATE_SERVICE:
                     // These all are longer-term states, so pull them up to the top
                     // of the background states, but not all the way to the top state.
-                    procState = ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE;
+                    procState = ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE;
                     break;
                 default:
                     // Otherwise, top is a better choice, so take it.
@@ -18511,7 +18515,7 @@
             }
             // Inform UsageStats of important process state change
             // Must be called before updating setProcState
-            maybeUpdateUsageStats(app);
+            maybeUpdateUsageStatsLocked(app);
 
             app.setProcState = app.curProcState;
             if (app.setProcState >= ActivityManager.PROCESS_STATE_HOME) {
@@ -18600,7 +18604,7 @@
         uidRec.pendingChange.processState = uidRec.setProcState;
     }
 
-    private void maybeUpdateUsageStats(ProcessRecord app) {
+    private void maybeUpdateUsageStatsLocked(ProcessRecord app) {
         if (DEBUG_USAGE_STATS) {
             Slog.d(TAG, "Checking proc [" + Arrays.toString(app.getPackageList())
                     + "] state changes: old = " + app.setProcState + ", new = "
@@ -18609,9 +18613,32 @@
         if (mUsageStatsService == null) {
             return;
         }
-        if (app.curProcState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
-                && (app.setProcState > ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
-                        || app.setProcState < 0)) {
+        boolean isInteraction;
+        if (!mSleeping) {
+            isInteraction = app.curProcState <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
+            app.fgInteractionTime = 0;
+        } else {
+            // If the display is off, we are going to be more restrictive about what we consider
+            // to be an app interaction.  Being the top activity doesn't count, nor do generally
+            // foreground services.
+            if (app.curProcState <= ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE) {
+                isInteraction = true;
+                app.fgInteractionTime = 0;
+            } else if (app.curProcState <= ActivityManager.PROCESS_STATE_TOP_SLEEPING) {
+                final long now = SystemClock.elapsedRealtime();
+                if (app.fgInteractionTime == 0) {
+                    app.fgInteractionTime = now;
+                    isInteraction = false;
+                } else {
+                    isInteraction = now > app.fgInteractionTime + SERVICE_USAGE_INTERACTION_TIME;
+                }
+            } else {
+                isInteraction = app.curProcState
+                        <= ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND;
+                app.fgInteractionTime = 0;
+            }
+        }
+        if (isInteraction && !app.reportedInteraction) {
             String[] packages = app.getPackageList();
             if (packages != null) {
                 for (int i = 0; i < packages.length; i++) {
@@ -18620,6 +18647,7 @@
                 }
             }
         }
+        app.reportedInteraction = isInteraction;
     }
 
     private final void setProcessTrackerStateLocked(ProcessRecord proc, int memFactor, long now) {
diff --git a/services/core/java/com/android/server/am/ProcessList.java b/services/core/java/com/android/server/am/ProcessList.java
index cdfcd0c..0e24952 100644
--- a/services/core/java/com/android/server/am/ProcessList.java
+++ b/services/core/java/com/android/server/am/ProcessList.java
@@ -368,8 +368,11 @@
             case ActivityManager.PROCESS_STATE_TOP:
                 procState = "T ";
                 break;
+            case ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE:
+                procState = "SB";
+                break;
             case ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE:
-                procState = "FS";
+                procState = "SF";
                 break;
             case ActivityManager.PROCESS_STATE_TOP_SLEEPING:
                 procState = "TS";
@@ -481,6 +484,7 @@
         PROC_MEM_PERSISTENT,            // ActivityManager.PROCESS_STATE_PERSISTENT
         PROC_MEM_PERSISTENT,            // ActivityManager.PROCESS_STATE_PERSISTENT_UI
         PROC_MEM_TOP,                   // ActivityManager.PROCESS_STATE_TOP
+        PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
         PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
         PROC_MEM_TOP,                   // ActivityManager.PROCESS_STATE_TOP_SLEEPING
         PROC_MEM_IMPORTANT,             // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
@@ -500,6 +504,7 @@
         PSS_SHORT_INTERVAL,             // ActivityManager.PROCESS_STATE_PERSISTENT
         PSS_SHORT_INTERVAL,             // ActivityManager.PROCESS_STATE_PERSISTENT_UI
         PSS_FIRST_TOP_INTERVAL,         // ActivityManager.PROCESS_STATE_TOP
+        PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_TOP_SLEEPING
         PSS_FIRST_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
@@ -519,6 +524,7 @@
         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_PERSISTENT
         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_PERSISTENT_UI
         PSS_SHORT_INTERVAL,             // ActivityManager.PROCESS_STATE_TOP
+        PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_TOP_SLEEPING
         PSS_SAME_IMPORTANT_INTERVAL,    // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
@@ -538,6 +544,7 @@
         PSS_TEST_FIRST_TOP_INTERVAL,        // ActivityManager.PROCESS_STATE_PERSISTENT
         PSS_TEST_FIRST_TOP_INTERVAL,        // ActivityManager.PROCESS_STATE_PERSISTENT_UI
         PSS_TEST_FIRST_TOP_INTERVAL,        // ActivityManager.PROCESS_STATE_TOP
+        PSS_FIRST_BACKGROUND_INTERVAL,      // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
         PSS_FIRST_BACKGROUND_INTERVAL,      // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
         PSS_FIRST_BACKGROUND_INTERVAL,      // ActivityManager.PROCESS_STATE_TOP_SLEEPING
         PSS_TEST_FIRST_BACKGROUND_INTERVAL, // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
@@ -557,6 +564,7 @@
         PSS_TEST_SAME_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_PERSISTENT
         PSS_TEST_SAME_BACKGROUND_INTERVAL,  // ActivityManager.PROCESS_STATE_PERSISTENT_UI
         PSS_TEST_SAME_IMPORTANT_INTERVAL,   // ActivityManager.PROCESS_STATE_TOP
+        PSS_TEST_SAME_IMPORTANT_INTERVAL,   // ActivityManager.PROCESS_STATE_BOUND_FOREGROUND_SERVICE
         PSS_TEST_SAME_IMPORTANT_INTERVAL,   // ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE
         PSS_TEST_SAME_IMPORTANT_INTERVAL,   // ActivityManager.PROCESS_STATE_TOP_SLEEPING
         PSS_TEST_SAME_IMPORTANT_INTERVAL,   // ActivityManager.PROCESS_STATE_IMPORTANT_FOREGROUND
diff --git a/services/core/java/com/android/server/am/ProcessRecord.java b/services/core/java/com/android/server/am/ProcessRecord.java
index 14759c3..3acd3a3 100644
--- a/services/core/java/com/android/server/am/ProcessRecord.java
+++ b/services/core/java/com/android/server/am/ProcessRecord.java
@@ -112,6 +112,8 @@
     boolean killedByAm;         // True when proc has been killed by activity manager, not for RAM
     boolean killed;             // True once we know the process has been killed
     boolean procStateChanged;   // Keep track of whether we changed 'setAdj'.
+    boolean reportedInteraction;// Whether we have told usage stats about it being an interaction
+    long fgInteractionTime;     // When we became foreground for interaction purposes
     String waitingToKill;       // Process is waiting to be killed when in the bg, and reason
     IBinder forcingToForeground;// Token that is forcing this process to be foreground
     int adjSeq;                 // Sequence id for identifying oom_adj assignment cycles
@@ -291,6 +293,15 @@
                     pw.print(" foregroundServices="); pw.print(foregroundServices);
                     pw.print(" forcingToForeground="); pw.println(forcingToForeground);
         }
+        if (reportedInteraction || fgInteractionTime != 0) {
+            pw.print(prefix); pw.print("reportedInteraction=");
+            pw.print(reportedInteraction);
+            if (fgInteractionTime != 0) {
+                pw.print(" fgInteractionTime=");
+                TimeUtils.formatDuration(fgInteractionTime, SystemClock.elapsedRealtime(), pw);
+            }
+            pw.println();
+        }
         if (persistent || removed) {
             pw.print(prefix); pw.print("persistent="); pw.print(persistent);
                     pw.print(" removed="); pw.println(removed);
diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index 604ac97..e2cc3f7 100644
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -278,6 +278,7 @@
     private static final boolean DEBUG_VERIFY = false;
     private static final boolean DEBUG_DEXOPT = false;
     private static final boolean DEBUG_ABI_SELECTION = false;
+    private static final boolean DEBUG_DOMAIN_VERIFICATION = false;
 
     private static final int RADIO_UID = Process.PHONE_UID;
     private static final int LOG_UID = Process.LOG_UID;
@@ -620,7 +621,8 @@
 
             UserHandle user = new UserHandle(userId);
             mContext.sendBroadcastAsUser(verificationIntent, user);
-            Slog.d(TAG, "Sending IntenFilter verification broadcast");
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "Sending IntenFilter verification broadcast");
         }
 
         public void receiveVerificationResponse(int verificationId) {
@@ -634,8 +636,9 @@
                 PackageParser.ActivityIntentInfo filter = filters.get(n);
                 filter.setVerified(verified);
 
-                Slog.d(TAG, "IntentFilter " + filter.toString() + " verified with result:"
-                        + verified + " and hosts:" + ivs.getHostsString());
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "IntentFilter " + filter.toString()
+                        + " verified with result:" + verified + " and hosts:"
+                        + ivs.getHostsString());
             }
 
             mIntentFilterVerificationStates.remove(verificationId);
@@ -651,8 +654,8 @@
                         + verificationId + " packageName:" + packageName);
                 return;
             }
-            Slog.d(TAG, "Updating IntentFilterVerificationInfo for verificationId:"
-                    + verificationId);
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "Updating IntentFilterVerificationInfo for verificationId:" + verificationId);
 
             synchronized (mPackages) {
                 if (verified) {
@@ -707,7 +710,8 @@
                     ActivityIntentInfo filter, String packageName) {
             if (!(filter.hasDataScheme(IntentFilter.SCHEME_HTTP) ||
                     filter.hasDataScheme(IntentFilter.SCHEME_HTTPS))) {
-                Slog.d(TAG, "IntentFilter does not contain HTTP nor HTTPS data scheme");
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                        "IntentFilter does not contain HTTP nor HTTPS data scheme");
                 return false;
             }
             IntentFilterVerificationState ivs = mIntentFilterVerificationStates.get(verificationId);
@@ -736,16 +740,11 @@
     }
 
     private static boolean hasValidDomains(ActivityIntentInfo filter) {
-        return hasValidDomains(filter, true);
-    }
-
-    private static boolean hasValidDomains(ActivityIntentInfo filter, boolean logging) {
         boolean hasHTTPorHTTPS = filter.hasDataScheme(IntentFilter.SCHEME_HTTP) ||
                 filter.hasDataScheme(IntentFilter.SCHEME_HTTPS);
         if (!hasHTTPorHTTPS) {
-            if (logging) {
-                Slog.d(TAG, "IntentFilter does not contain any HTTP or HTTPS data scheme");
-            }
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "IntentFilter does not contain any HTTP or HTTPS data scheme");
             return false;
         }
         return true;
@@ -1515,7 +1514,8 @@
 
                     final int userId = state.getUserId();
 
-                    Slog.d(TAG, "Processing IntentFilter verification with token:"
+                    if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                            "Processing IntentFilter verification with token:"
                             + verificationId + " and userId:" + userId);
 
                     final IntentFilterVerificationResponse response =
@@ -1523,20 +1523,22 @@
 
                     state.setVerifierResponse(response.callerUid, response.code);
 
-                    Slog.d(TAG, "IntentFilter verification with token:" + verificationId
+                    if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                            "IntentFilter verification with token:" + verificationId
                             + " and userId:" + userId
                             + " is settings verifier response with response code:"
                             + response.code);
 
                     if (response.code == PackageManager.INTENT_FILTER_VERIFICATION_FAILURE) {
-                        Slog.d(TAG, "Domains failing verification: "
+                        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "Domains failing verification: "
                                 + response.getFailedDomainsString());
                     }
 
                     if (state.isVerificationComplete()) {
                         mIntentFilterVerifier.receiveVerificationResponse(verificationId);
                     } else {
-                        Slog.d(TAG, "IntentFilter verification with token:" + verificationId
+                        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                                "IntentFilter verification with token:" + verificationId
                                 + " was not said to be complete");
                     }
 
@@ -2160,7 +2162,7 @@
                 mSettings.mFingerprint = Build.FINGERPRINT;
             }
 
-            primeDomainVerificationsLPw(false);
+            primeDomainVerificationsLPw();
             checkDefaultBrowser();
 
             // All the changes are done during package scanning.
@@ -2268,37 +2270,34 @@
             if (priority < info.priority) {
                 priority = info.priority;
                 verifierComponentName = new ComponentName(packageName, info.activityInfo.name);
-                Slog.d(TAG, "Selecting IntentFilterVerifier: " + verifierComponentName +
-                        " with priority: " + info.priority);
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "Selecting IntentFilterVerifier: "
+                        + verifierComponentName + " with priority: " + info.priority);
             }
         }
 
         return verifierComponentName;
     }
 
-    private void primeDomainVerificationsLPw(boolean logging) {
-        Slog.d(TAG, "Start priming domain verifications");
+    private void primeDomainVerificationsLPw() {
+        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "Start priming domain verifications");
         boolean updated = false;
         ArraySet<String> allHostsSet = new ArraySet<>();
         for (PackageParser.Package pkg : mPackages.values()) {
             final String packageName = pkg.packageName;
             if (!hasDomainURLs(pkg)) {
-                if (logging) {
-                    Slog.d(TAG, "No priming domain verifications for " +
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "No priming domain verifications for " +
                             "package with no domain URLs: " + packageName);
-                }
                 continue;
             }
             if (!pkg.isSystemApp()) {
-                if (logging) {
-                    Slog.d(TAG, "No priming domain verifications for a non system package : " +
-                            packageName);
-                }
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                        "No priming domain verifications for a non system package : " +
+                                packageName);
                 continue;
             }
             for (PackageParser.Activity a : pkg.activities) {
                 for (ActivityIntentInfo filter : a.intents) {
-                    if (hasValidDomains(filter, false)) {
+                    if (hasValidDomains(filter)) {
                         allHostsSet.addAll(filter.getHostsList());
                     }
                 }
@@ -2310,25 +2309,23 @@
             IntentFilterVerificationInfo ivi =
                     mSettings.createIntentFilterVerificationIfNeededLPw(packageName, allHostsList);
             if (ivi != null) {
-                // We will always log this
-                Slog.d(TAG, "Priming domain verifications for package: " + packageName +
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                        "Priming domain verifications for package: " + packageName +
                         " with hosts:" + ivi.getDomainsString());
                 ivi.setStatus(INTENT_FILTER_DOMAIN_VERIFICATION_STATUS_ALWAYS);
                 updated = true;
             }
             else {
-                if (logging) {
-                    Slog.d(TAG, "No priming domain verifications for package: " + packageName);
-                }
+                if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                        "No priming domain verifications for package: " + packageName);
             }
             allHostsSet.clear();
         }
         if (updated) {
-            if (logging) {
-                Slog.d(TAG, "Will need to write primed domain verifications");
-            }
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "Will need to write primed domain verifications");
         }
-        Slog.d(TAG, "End priming domain verifications");
+        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "End priming domain verifications");
     }
 
     private void checkDefaultBrowser() {
@@ -10344,7 +10341,7 @@
 
         int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException {
             if (origin.staged) {
-                Slog.d(TAG, origin.file + " already staged; skipping copy");
+                if (DEBUG_INSTALL) Slog.d(TAG, origin.file + " already staged; skipping copy");
                 codeFile = origin.file;
                 resourceFile = origin.file;
                 return PackageManager.INSTALL_SUCCEEDED;
@@ -10417,16 +10414,16 @@
             final File beforeCodeFile = codeFile;
             final File afterCodeFile = getNextCodePath(targetDir, pkg.packageName);
 
-            Slog.d(TAG, "Renaming " + beforeCodeFile + " to " + afterCodeFile);
+            if (DEBUG_INSTALL) Slog.d(TAG, "Renaming " + beforeCodeFile + " to " + afterCodeFile);
             try {
                 Os.rename(beforeCodeFile.getAbsolutePath(), afterCodeFile.getAbsolutePath());
             } catch (ErrnoException e) {
-                Slog.d(TAG, "Failed to rename", e);
+                Slog.w(TAG, "Failed to rename", e);
                 return false;
             }
 
             if (!SELinux.restoreconRecursive(afterCodeFile)) {
-                Slog.d(TAG, "Failed to restorecon");
+                Slog.w(TAG, "Failed to restorecon");
                 return false;
             }
 
@@ -10589,7 +10586,7 @@
 
         int copyApk(IMediaContainerService imcs, boolean temp) throws RemoteException {
             if (origin.staged) {
-                Slog.d(TAG, origin.cid + " already staged; skipping copy");
+                if (DEBUG_INSTALL) Slog.d(TAG, origin.cid + " already staged; skipping copy");
                 cid = origin.cid;
                 setMountPath(PackageHelper.getSdDir(cid));
                 return PackageManager.INSTALL_SUCCEEDED;
@@ -10849,8 +10846,8 @@
         }
 
         int copyApk(IMediaContainerService imcs, boolean temp) {
-            Slog.d(TAG, "Moving " + move.packageName + " from " + move.fromUuid + " to "
-                    + move.toUuid);
+            if (DEBUG_INSTALL) Slog.d(TAG, "Moving " + move.packageName + " from "
+                    + move.fromUuid + " to " + move.toUuid);
             synchronized (mInstaller) {
                 if (mInstaller.moveCompleteApp(move.fromUuid, move.toUuid, move.packageName,
                         move.dataAppName, move.appId, move.seinfo) != 0) {
@@ -10860,7 +10857,7 @@
 
             codeFile = new File(Environment.getDataAppDirectory(move.toUuid), move.dataAppName);
             resourceFile = codeFile;
-            Slog.d(TAG, "codeFile after move is " + codeFile);
+            if (DEBUG_INSTALL) Slog.d(TAG, "codeFile after move is " + codeFile);
 
             return PackageManager.INSTALL_SUCCEEDED;
         }
@@ -11695,7 +11692,7 @@
 
     private void startIntentFilterVerifications(int userId, PackageParser.Package pkg) {
         if (mIntentFilterVerifierComponent == null) {
-            Slog.d(TAG, "No IntentFilter verification will not be done as "
+            Slog.w(TAG, "No IntentFilter verification will not be done as "
                     + "there is no IntentFilterVerifier available!");
             return;
         }
@@ -11717,17 +11714,20 @@
             PackageParser.Package pkg) {
         int size = pkg.activities.size();
         if (size == 0) {
-            Slog.d(TAG, "No activity, so no need to verify any IntentFilter!");
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "No activity, so no need to verify any IntentFilter!");
             return;
         }
 
         final boolean hasDomainURLs = hasDomainURLs(pkg);
         if (!hasDomainURLs) {
-            Slog.d(TAG, "No domain URLs, so no need to verify any IntentFilter!");
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "No domain URLs, so no need to verify any IntentFilter!");
             return;
         }
 
-        Slog.d(TAG, "Checking for userId:" + userId + " if any IntentFilter from the " + size
+        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "Checking for userId:" + userId
+                + " if any IntentFilter from the " + size
                 + " Activities needs verification ...");
 
         final int verificationId = mIntentFilterVerificationToken++;
@@ -11740,12 +11740,14 @@
                 for (ActivityIntentInfo filter : a.intents) {
                     boolean needsFilterVerification = filter.needsVerification();
                     if (needsFilterVerification && needsNetworkVerificationLPr(filter)) {
-                        Slog.d(TAG, "Verification needed for IntentFilter:" + filter.toString());
+                        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                                "Verification needed for IntentFilter:" + filter.toString());
                         mIntentFilterVerifier.addOneIntentFilterVerification(
                                 verifierUid, userId, verificationId, filter, packageName);
                         count++;
                     } else if (!needsFilterVerification) {
-                        Slog.d(TAG, "No verification needed for IntentFilter:" + filter.toString());
+                        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                                "No verification needed for IntentFilter:" + filter.toString());
                         if (hasValidDomains(filter)) {
                             ArrayList<String> hosts = filter.getHostsList();
                             if (hosts.size() > 0) {
@@ -11757,8 +11759,8 @@
                             }
                         }
                     } else {
-                        Slog.d(TAG, "Verification already done for IntentFilter:"
-                                + filter.toString());
+                        if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                                "Verification already done for IntentFilter:" + filter.toString());
                     }
                 }
             }
@@ -11766,10 +11768,12 @@
 
         if (count > 0) {
             mIntentFilterVerifier.startVerifications(userId);
-            Slog.d(TAG, "Started " + count + " IntentFilter verification"
-                    + (count > 1 ? "s" : "") +  " for userId:" + userId + "!");
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG, "Started " + count
+                    + " IntentFilter verification" + (count > 1 ? "s" : "")
+                    +  " for userId:" + userId + "!");
         } else {
-            Slog.d(TAG, "No need to start any IntentFilter verification!");
+            if (DEBUG_DOMAIN_VERIFICATION) Slog.d(TAG,
+                    "No need to start any IntentFilter verification!");
             if (allHosts.size() > 0 && mSettings.createIntentFilterVerificationIfNeededLPw(
                     packageName, allHosts) != null) {
                 scheduleWriteSettingsLocked();
@@ -14558,7 +14562,7 @@
         }
         }
 
-        Slog.d(TAG, "Loaded packages " + loaded);
+        if (DEBUG_INSTALL) Slog.d(TAG, "Loaded packages " + loaded);
         sendResourcesChangedBroadcast(true, false, loaded, null);
     }
 
@@ -14584,7 +14588,7 @@
         }
         }
 
-        Slog.d(TAG, "Unloaded packages " + unloaded);
+        if (DEBUG_INSTALL) Slog.d(TAG, "Unloaded packages " + unloaded);
         sendResourcesChangedBroadcast(false, false, unloaded, null);
     }
 
@@ -14605,7 +14609,7 @@
         try {
             movePackageInternal(packageName, volumeUuid, moveId);
         } catch (PackageManagerException e) {
-            Slog.d(TAG, "Failed to move " + packageName, e);
+            Slog.w(TAG, "Failed to move " + packageName, e);
             mMoveCallbacks.notifyStatusChanged(moveId,
                     PackageManager.MOVE_FAILED_INTERNAL_ERROR);
         }
@@ -14714,7 +14718,8 @@
             }
         }
 
-        Slog.d(TAG, "Measured code size " + stats.codeSize + ", data size " + stats.dataSize);
+        if (DEBUG_INSTALL) Slog.d(TAG, "Measured code size " + stats.codeSize + ", data size "
+                + stats.dataSize);
 
         final long startFreeBytes = measurePath.getFreeSpace();
         final long sizeBytes;
@@ -14742,7 +14747,7 @@
             @Override
             public void onPackageInstalled(String basePackageName, int returnCode, String msg,
                     Bundle extras) throws RemoteException {
-                Slog.d(TAG, "Install result for move: "
+                if (DEBUG_INSTALL) Slog.d(TAG, "Install result for move: "
                         + PackageManager.installStatusToString(returnCode, msg));
 
                 installedLatch.countDown();
@@ -14891,7 +14896,7 @@
             for (VolumeInfo vol : vols) {
                 if (vol.getType() == VolumeInfo.TYPE_PRIVATE && vol.isMountedWritable()) {
                     final String volumeUuid = vol.getFsUuid();
-                    Slog.d(TAG, "Removing user data on volume " + volumeUuid);
+                    if (DEBUG_INSTALL) Slog.d(TAG, "Removing user data on volume " + volumeUuid);
                     mInstaller.removeUserDataDirs(volumeUuid, userHandle);
                 }
             }