location: Optimize use of mProviders ArrayList.

Signed-off-by: Mike Lockwood <lockwood@android.com>
diff --git a/location/java/com/android/internal/location/LocationProviderImpl.java b/location/java/com/android/internal/location/LocationProviderImpl.java
index 5e9c074..fc830f5 100644
--- a/location/java/com/android/internal/location/LocationProviderImpl.java
+++ b/location/java/com/android/internal/location/LocationProviderImpl.java
@@ -25,7 +25,6 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.List;
 
 /**
  * An abstract superclass for location provider implementations.
@@ -62,8 +61,8 @@
         sProvidersByName.remove(provider.getName());
     }
 
-    public static List<LocationProviderImpl> getProviders() {
-        return new ArrayList<LocationProviderImpl>(sProviders);
+    public static ArrayList<LocationProviderImpl> getProviders() {
+        return sProviders;
     }
 
     public static LocationProviderImpl getProvider(String name) {
diff --git a/services/java/com/android/server/LocationManagerService.java b/services/java/com/android/server/LocationManagerService.java
index 2e7f2c1..0e1e0d9 100644
--- a/services/java/com/android/server/LocationManagerService.java
+++ b/services/java/com/android/server/LocationManagerService.java
@@ -703,10 +703,10 @@
         if (LOCAL_LOGV) {
             Log.v(TAG, "getAllProviders");
         }
-        List<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
+        ArrayList<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
         ArrayList<String> out = new ArrayList<String>(providers.size());
-
-        for (LocationProviderImpl p : providers) {
+        for (int i = providers.size() - 1; i >= 0; i--) {
+            LocationProviderImpl p = providers.get(i);
             out.add(p.getName());
         }
         return out;
@@ -729,10 +729,10 @@
         if (LOCAL_LOGV) {
             Log.v(TAG, "getProviders");
         }
-        List<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
-        ArrayList<String> out = new ArrayList<String>();
-
-        for (LocationProviderImpl p : providers) {
+        ArrayList<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
+        ArrayList<String> out = new ArrayList<String>(providers.size());
+        for (int i = providers.size() - 1; i >= 0; i--) {
+            LocationProviderImpl p = providers.get(i);
             String name = p.getName();
             if (isAllowedProviderSafe(name)) {
                 if (enabledOnly && !isAllowedBySettingsLocked(name)) {
@@ -745,7 +745,9 @@
     }
 
     private void updateProvidersLocked() {
-        for (LocationProviderImpl p : LocationProviderImpl.getProviders()) {
+        ArrayList<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
+        for (int i = providers.size() - 1; i >= 0; i--) {
+            LocationProviderImpl p = providers.get(i);
             boolean isEnabled = p.isEnabled();
             String name = p.getName();
             boolean shouldBeEnabled = isAllowedBySettingsLocked(name);
@@ -1718,8 +1720,9 @@
 
                 // Notify location providers of current network state
                 synchronized (mLock) {
-                    List<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
-                    for (LocationProviderImpl provider : providers) {
+                    ArrayList<LocationProviderImpl> providers = LocationProviderImpl.getProviders();
+                    for (int i = providers.size() - 1; i >= 0; i--) {
+                        LocationProviderImpl provider = providers.get(i);
                         if (provider.requiresNetwork()) {
                             provider.updateNetworkState(mNetworkState);
                         }