Metrics more of the things

Add a dump for some of the state of the device.

Bug: 26687707
Change-Id: Icf517912b3006cc84c26863eb1b93d586a55464a
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4e6f303..637e710 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2787,6 +2787,11 @@
                        android:value="com.android.settings.applications.ManageDefaultApps" />
         </activity>
 
+        <service
+            android:name=".SettingsDumpService"
+            android:exported="true"
+            android:permission="android.permission.DUMP" />
+
         <!-- Conditional receivers, only enabled during silenced state, default off-->
         <receiver
             android:name=".dashboard.conditional.HotspotCondition$Receiver"
diff --git a/src/com/android/settings/SettingsDumpService.java b/src/com/android/settings/SettingsDumpService.java
new file mode 100644
index 0000000..cd9ef01
--- /dev/null
+++ b/src/com/android/settings/SettingsDumpService.java
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under the
+ * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the specific language governing
+ * permissions and limitations under the License.
+ */
+
+package com.android.settings;
+
+import com.android.internal.os.BatteryStatsHelper;
+import com.android.settings.applications.ProcStatsData;
+import com.android.settingslib.net.DataUsageController;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import android.app.Service;
+import android.content.Intent;
+import android.net.ConnectivityManager;
+import android.net.NetworkTemplate;
+import android.os.IBinder;
+import android.os.storage.StorageManager;
+import android.os.storage.StorageVolume;
+import android.os.storage.VolumeInfo;
+import android.telephony.SubscriptionInfo;
+import android.telephony.SubscriptionManager;
+import android.telephony.TelephonyManager;
+
+import java.io.File;
+import java.io.FileDescriptor;
+import java.io.PrintWriter;
+
+public class SettingsDumpService extends Service {
+
+    @Override
+    public IBinder onBind(Intent intent) {
+        return null;
+    }
+
+    @Override
+    protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
+        JSONObject dump = new JSONObject();
+
+        try {
+            dump.put("service", "Settings State");
+            dump.put("storage", dumpStorage());
+            dump.put("datausage", dumpDataUsage());
+            dump.put("memory", dumpMemory());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+        writer.println(dump);
+    }
+
+    private JSONObject dumpMemory() throws JSONException {
+        JSONObject obj = new JSONObject();
+        ProcStatsData statsManager = new ProcStatsData(this, false);
+        statsManager.refreshStats(true);
+        ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
+
+        obj.put("used", String.valueOf(memInfo.realUsedRam));
+        obj.put("free", String.valueOf(memInfo.realFreeRam));
+        obj.put("total", String.valueOf(memInfo.realTotalRam));
+        obj.put("state", statsManager.getMemState());
+
+        return obj;
+    }
+
+    private JSONObject dumpDataUsage() throws JSONException {
+        JSONObject obj = new JSONObject();
+        DataUsageController controller = new DataUsageController(this);
+        ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class);
+        SubscriptionManager manager = SubscriptionManager.from(this);
+        TelephonyManager telephonyManager = TelephonyManager.from(this);
+        if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
+            for (SubscriptionInfo info : manager.getAllSubscriptionInfoList()) {
+                NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
+                        telephonyManager.getSubscriberId(info.getSubscriptionId()));
+                obj.put("cell(" + info.getSubscriptionId() + ")",
+                        dumpDataUsage(mobileAll, controller));
+            }
+        }
+        if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) {
+            obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller));
+        }
+        if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
+            obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
+        }
+        return obj;
+    }
+
+    private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
+            throws JSONException {
+        JSONObject obj = new JSONObject();
+        DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
+        obj.put("carrier", usage.carrier);
+        obj.put("start", usage.startDate);
+        obj.put("usage", usage.usageLevel);
+        obj.put("warning", usage.warningLevel);
+        obj.put("limit", usage.limitLevel);
+        return obj;
+    }
+
+    private JSONObject dumpStorage() throws JSONException {
+        JSONObject obj = new JSONObject();
+        StorageManager manager = getSystemService(StorageManager.class);
+        for (VolumeInfo volume : manager.getVolumes()) {
+            JSONObject volObj = new JSONObject();
+            if (volume.isMountedReadable()) {
+                File path = volume.getPath();
+                volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
+                volObj.put("total", String.valueOf(path.getTotalSpace()));
+            }
+            volObj.put("path", volume.getInternalPath());
+            volObj.put("state", volume.getState());
+            volObj.put("stateDesc", volume.getStateDescription());
+            volObj.put("description", volume.getDescription());
+            obj.put(volume.getId(), volObj);
+        }
+        return obj;
+    }
+}
diff --git a/src/com/android/settings/applications/ProcStatsData.java b/src/com/android/settings/applications/ProcStatsData.java
index 0cd80e7..5e330b8 100644
--- a/src/com/android/settings/applications/ProcStatsData.java
+++ b/src/com/android/settings/applications/ProcStatsData.java
@@ -356,9 +356,9 @@
     }
 
     public static class MemInfo {
-        double realUsedRam;
-        double realFreeRam;
-        double realTotalRam;
+        public double realUsedRam;
+        public double realFreeRam;
+        public double realTotalRam;
         long baseCacheRam;
 
         double[] mMemStateWeights = new double[ProcessStats.STATE_COUNT];