1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.settings;
16 
17 import android.app.Service;
18 import android.content.Intent;
19 import android.net.ConnectivityManager;
20 import android.net.NetworkTemplate;
21 import android.os.IBinder;
22 import android.os.storage.StorageManager;
23 import android.os.storage.VolumeInfo;
24 import android.telephony.SubscriptionInfo;
25 import android.telephony.SubscriptionManager;
26 import android.telephony.TelephonyManager;
27 import com.android.settings.applications.ProcStatsData;
28 import com.android.settingslib.net.DataUsageController;
29 import org.json.JSONArray;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 
33 import java.io.File;
34 import java.io.FileDescriptor;
35 import java.io.PrintWriter;
36 
37 public class SettingsDumpService extends Service {
38 
39     @Override
onBind(Intent intent)40     public IBinder onBind(Intent intent) {
41         return null;
42     }
43 
44     @Override
dump(FileDescriptor fd, PrintWriter writer, String[] args)45     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
46         JSONObject dump = new JSONObject();
47 
48         try {
49             dump.put("service", "Settings State");
50             dump.put("storage", dumpStorage());
51             dump.put("datausage", dumpDataUsage());
52             dump.put("memory", dumpMemory());
53         } catch (Exception e) {
54             e.printStackTrace();
55         }
56 
57         writer.println(dump);
58     }
59 
dumpMemory()60     private JSONObject dumpMemory() throws JSONException {
61         JSONObject obj = new JSONObject();
62         ProcStatsData statsManager = new ProcStatsData(this, false);
63         statsManager.refreshStats(true);
64         ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
65 
66         obj.put("used", String.valueOf(memInfo.realUsedRam));
67         obj.put("free", String.valueOf(memInfo.realFreeRam));
68         obj.put("total", String.valueOf(memInfo.realTotalRam));
69         obj.put("state", statsManager.getMemState());
70 
71         return obj;
72     }
73 
dumpDataUsage()74     private JSONObject dumpDataUsage() throws JSONException {
75         JSONObject obj = new JSONObject();
76         DataUsageController controller = new DataUsageController(this);
77         ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class);
78         SubscriptionManager manager = SubscriptionManager.from(this);
79         TelephonyManager telephonyManager = TelephonyManager.from(this);
80         if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
81             JSONArray array = new JSONArray();
82             for (SubscriptionInfo info : manager.getAllSubscriptionInfoList()) {
83                 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
84                         telephonyManager.getSubscriberId(info.getSubscriptionId()));
85                 final JSONObject usage = dumpDataUsage(mobileAll, controller);
86                 usage.put("subId", info.getSubscriptionId());
87                 array.put(usage);
88             }
89             obj.put("cell", array);
90         }
91         if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) {
92             obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller));
93         }
94         if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
95             obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
96         }
97         return obj;
98     }
99 
dumpDataUsage(NetworkTemplate template, DataUsageController controller)100     private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
101             throws JSONException {
102         JSONObject obj = new JSONObject();
103         DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
104         obj.put("carrier", usage.carrier);
105         obj.put("start", usage.startDate);
106         obj.put("usage", usage.usageLevel);
107         obj.put("warning", usage.warningLevel);
108         obj.put("limit", usage.limitLevel);
109         return obj;
110     }
111 
dumpStorage()112     private JSONObject dumpStorage() throws JSONException {
113         JSONObject obj = new JSONObject();
114         StorageManager manager = getSystemService(StorageManager.class);
115         for (VolumeInfo volume : manager.getVolumes()) {
116             JSONObject volObj = new JSONObject();
117             if (volume.isMountedReadable()) {
118                 File path = volume.getPath();
119                 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
120                 volObj.put("total", String.valueOf(path.getTotalSpace()));
121             }
122             volObj.put("path", volume.getInternalPath());
123             volObj.put("state", volume.getState());
124             volObj.put("stateDesc", volume.getStateDescription());
125             volObj.put("description", volume.getDescription());
126             obj.put(volume.getId(), volObj);
127         }
128         return obj;
129     }
130 }
131