Bug 745889: SUT agent/watcher - avoid NullPointerException when there are no running app processes. r=ctalbert, a=test-only
authorGeoff Brown <gbrown@mozilla.com>
Tue, 17 Apr 2012 19:44:53 -0400
changeset 91899 8f52882518b6d8a64294d3f53b85b896419aeb26
parent 91898 96425a35b8838e6f8905e0b3d0c98edece4cb24a
child 91900 eee73897136b4fb54a6e1cac13cc9b48e6af6c25
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersctalbert, test-only
bugs745889
milestone14.0a1
Bug 745889: SUT agent/watcher - avoid NullPointerException when there are no running app processes. r=ctalbert, a=test-only
build/mobile/sutagent/android/DoCommand.java
build/mobile/sutagent/android/watcher/WatcherService.java
--- a/build/mobile/sutagent/android/DoCommand.java
+++ b/build/mobile/sutagent/android/DoCommand.java
@@ -2383,18 +2383,22 @@ private void CancelNotification()
         theArgs[2] = "kill";
 
         String sRet = sErrorPrefix + "Unable to kill " + sProcName + "\n";
         ActivityManager aMgr = (ActivityManager) contextWrapper.getSystemService(Activity.ACTIVITY_SERVICE);
         List <ActivityManager.RunningAppProcessInfo> lProcesses = aMgr.getRunningAppProcesses();
         int lcv = 0;
         String strProcName = "";
         int    nPID = 0;
-
-        for (lcv = 0; lcv < lProcesses.size(); lcv++)
+        int nProcs = 0;
+
+        if (lProcesses != null) 
+            nProcs = lProcesses.size();
+
+        for (lcv = 0; lcv < nProcs; lcv++)
             {
             if (lProcesses.get(lcv).processName.contains(sProcName))
                 {
                 strProcName = lProcesses.get(lcv).processName;
                 nPID = lProcesses.get(lcv).pid;
                 sRet = sErrorPrefix + "Failed to kill " + nPID + " " + strProcName + "\n";
 
                 theArgs[2] += " " + nPID;
@@ -2436,17 +2440,20 @@ private void CancelNotification()
                 break;
                 }
             }
 
         if (nPID > 0)
             {
             sRet = "Successfully killed " + nPID + " " + strProcName + "\n";
             lProcesses = aMgr.getRunningAppProcesses();
-            for (lcv = 0; lcv < lProcesses.size(); lcv++)
+            nProcs = 0;
+            if (lProcesses != null) 
+                nProcs = lProcesses.size();
+            for (lcv = 0; lcv < nProcs; lcv++)
                 {
                 if (lProcesses.get(lcv).processName.contains(sProcName))
                     {
                     sRet = sErrorPrefix + "Unable to kill " + nPID + " " + strProcName + "\n";
                     break;
                     }
                 }
             }
@@ -2519,22 +2526,25 @@ private void CancelNotification()
         return (bRet);
         }
 
     public String GetProcessInfo()
         {
         String sRet = "";
         ActivityManager aMgr = (ActivityManager) contextWrapper.getSystemService(Activity.ACTIVITY_SERVICE);
         List <ActivityManager.RunningAppProcessInfo> lProcesses = aMgr.getRunningAppProcesses();
-        int    nProcs = lProcesses.size();
+        int    nProcs = 0;
         int lcv = 0;
         String strProcName = "";
         int    nPID = 0;
         int nUser = 0;
 
+        if (lProcesses != null) 
+            nProcs = lProcesses.size();
+
         for (lcv = 0; lcv < nProcs; lcv++)
             {
             strProcName = lProcesses.get(lcv).processName;
             nPID = lProcesses.get(lcv).pid;
             nUser = lProcesses.get(lcv).uid;
             sRet += nUser + "\t" + nPID + "\t" + strProcName;
             if (lcv < (nProcs - 1))
                 sRet += "\n";
--- a/build/mobile/sutagent/android/watcher/WatcherService.java
+++ b/build/mobile/sutagent/android/watcher/WatcherService.java
@@ -490,20 +490,23 @@ public class WatcherService extends Serv
         return(nRet);
         }
 
     public boolean GetProcessInfo(String sProcName)
         {
         boolean bRet = false;
         ActivityManager aMgr = (ActivityManager) getApplicationContext().getSystemService(Activity.ACTIVITY_SERVICE);
         List <ActivityManager.RunningAppProcessInfo> lProcesses = aMgr.getRunningAppProcesses();
-        int    nProcs = lProcesses.size();
+        int    nProcs = 0;
         int lcv = 0;
         String strProcName = "";
 
+        if (lProcesses != null)
+            nProcs = lProcesses.size();
+
         for (lcv = 0; lcv < nProcs; lcv++)
             {
             strProcName = lProcesses.get(lcv).processName;
             if (strProcName.contains(sProcName))
                 {
                 bRet = true;
                 }
             }
@@ -549,18 +552,22 @@ public class WatcherService extends Serv
         theArgs[2] = "kill";
 
         String sRet = sErrorPrefix + "Unable to kill " + sProcName + "\n";
         ActivityManager aMgr = (ActivityManager) getSystemService(Activity.ACTIVITY_SERVICE);
         List <ActivityManager.RunningAppProcessInfo> lProcesses = aMgr.getRunningAppProcesses();
         int lcv = 0;
         String strProcName = "";
         int    nPID = 0;
+        int nProcs = 0;
 
-        for (lcv = 0; lcv < lProcesses.size(); lcv++)
+        if (lProcesses != null)
+            nProcs = lProcesses.size();
+
+        for (lcv = 0; lcv < nProcs; lcv++)
             {
             if (lProcesses.get(lcv).processName.contains(sProcName))
                 {
                 strProcName = lProcesses.get(lcv).processName;
                 nPID = lProcesses.get(lcv).pid;
                 sRet = sErrorPrefix + "Failed to kill " + nPID + " " + strProcName + "\n";
 
                 theArgs[2] += " " + nPID;
@@ -593,17 +600,20 @@ public class WatcherService extends Serv
                 break;
                 }
             }
 
         if (nPID > 0)
             {
             sRet = "Successfully killed " + nPID + " " + strProcName + "\n";
             lProcesses = aMgr.getRunningAppProcesses();
-            for (lcv = 0; lcv < lProcesses.size(); lcv++)
+            nProcs = 0;
+            if (lProcesses != null)
+                nProcs = lProcesses.size();
+            for (lcv = 0; lcv < nProcs; lcv++)
                 {
                 if (lProcesses.get(lcv).processName.contains(sProcName))
                     {
                     sRet = sErrorPrefix + "Unable to kill " + nPID + " " + strProcName + "\n";
                     break;
                     }
                 }
             }