OSDN Git Service

Fix getTempContainerId()
authorKenny Root <kroot@google.com>
Tue, 27 Jul 2010 22:18:38 +0000 (15:18 -0700)
committerKenny Root <kroot@google.com>
Tue, 12 Apr 2011 20:08:05 +0000 (13:08 -0700)
getTempContainerId() would always return "smdl2tmp1" unless you had
MAX_CONTAINERS number of SD card SDKs, because of an array sort that put
all the zeros at the beginning.

Switch from trying to find a hole in the series of numbers to just
getting a number that's one larger than the previous. This reduces the
algorithmic complexity and the memory requirements.

Bug: 2832580
Change-Id: I3ef5ea0f9d13079dfb64e5fe6c5309a076f91fe7

services/java/com/android/server/PackageManagerService.java

index 8508537..7862960 100644 (file)
@@ -185,7 +185,9 @@ class PackageManagerService extends IPackageManager.Stub {
     static final ComponentName DEFAULT_CONTAINER_COMPONENT = new ComponentName(
             "com.android.defcontainer",
             "com.android.defcontainer.DefaultContainerService");
-    
+
+    static final String mTempContainerPrefix = "smdl2tmp";
+
     final HandlerThread mHandlerThread = new HandlerThread("PackageManager",
             Process.THREAD_PRIORITY_BACKGROUND);
     final PackageHandler mHandler;
@@ -9464,48 +9466,28 @@ class PackageManagerService extends IPackageManager.Stub {
         }
     }
 
-   static String getTempContainerId() {
-       String prefix = "smdl2tmp";
-       int tmpIdx = 1;
-       String list[] = PackageHelper.getSecureContainerList();
-       if (list != null) {
-           int idx = 0;
-           int idList[] = new int[MAX_CONTAINERS];
-           boolean neverFound = true;
-           for (String name : list) {
-               // Ignore null entries
-               if (name == null) {
-                   continue;
-               }
-               int sidx = name.indexOf(prefix);
-               if (sidx == -1) {
-                   // Not a temp file. just ignore
-                   continue;
-               }
-               String subStr = name.substring(sidx + prefix.length());
-               idList[idx] = -1;
-               if (subStr != null) {
-                   try {
-                       int cid = Integer.parseInt(subStr);
-                       idList[idx++] = cid;
-                       neverFound = false;
-                   } catch (NumberFormatException e) {
-                   }
-               }
-           }
-           if (!neverFound) {
-               // Sort idList
-               Arrays.sort(idList);
-               for (int j = 1; j <= idList.length; j++) {
-                   if (idList[j-1] != j) {
-                       tmpIdx = j;
-                       break;
-                   }
-               }
-           }
-       }
-       return prefix + tmpIdx;
-   }
+    /* package */ static String getTempContainerId() {
+        int tmpIdx = 1;
+        String list[] = PackageHelper.getSecureContainerList();
+        if (list != null) {
+            for (final String name : list) {
+                // Ignore null and non-temporary container entries
+                if (name == null || !name.startsWith(mTempContainerPrefix)) {
+                    continue;
+                }
+
+                String subStr = name.substring(mTempContainerPrefix.length());
+                try {
+                    int cid = Integer.parseInt(subStr);
+                    if (cid >= tmpIdx) {
+                        tmpIdx = cid + 1;
+                    }
+                } catch (NumberFormatException e) {
+                }
+            }
+        }
+        return mTempContainerPrefix + tmpIdx;
+    }
 
    /*
     * Update media status on PackageManager.