blob: 67a8f502b799e84effd33cac7bb951aac0e88f75 [file] [log] [blame]
Jason Monkdc252eb2016-01-30 11:36:52 -05001/*
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
15package com.android.settings;
16
Jason Monkdc252eb2016-01-30 11:36:52 -050017import android.app.Service;
18import android.content.Intent;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080019import android.content.pm.PackageManager;
20import android.content.pm.ResolveInfo;
Jason Monkdc252eb2016-01-30 11:36:52 -050021import android.net.ConnectivityManager;
22import android.net.NetworkTemplate;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080023import android.net.Uri;
Jason Monkdc252eb2016-01-30 11:36:52 -050024import android.os.IBinder;
25import android.os.storage.StorageManager;
Jason Monkdc252eb2016-01-30 11:36:52 -050026import android.os.storage.VolumeInfo;
27import android.telephony.SubscriptionInfo;
28import android.telephony.SubscriptionManager;
29import android.telephony.TelephonyManager;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080030import com.android.internal.annotations.VisibleForTesting;
Jason Monkf3f6bd42016-02-03 13:13:15 -050031import com.android.settings.applications.ProcStatsData;
32import com.android.settingslib.net.DataUsageController;
33import org.json.JSONArray;
34import org.json.JSONException;
35import org.json.JSONObject;
Jason Monkdc252eb2016-01-30 11:36:52 -050036
37import java.io.File;
38import java.io.FileDescriptor;
39import java.io.PrintWriter;
40
41public class SettingsDumpService extends Service {
jackqdyuleia9eb4a02016-12-20 10:08:19 -080042 @VisibleForTesting static final String KEY_SERVICE = "service";
43 @VisibleForTesting static final String KEY_STORAGE = "storage";
44 @VisibleForTesting static final String KEY_DATAUSAGE = "datausage";
45 @VisibleForTesting static final String KEY_MEMORY = "memory";
46 @VisibleForTesting static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
47 @VisibleForTesting static final Intent BROWSER_INTENT =
48 new Intent("android.intent.action.VIEW", Uri.parse("http://"));
Jason Monkdc252eb2016-01-30 11:36:52 -050049
50 @Override
51 public IBinder onBind(Intent intent) {
52 return null;
53 }
54
55 @Override
56 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
57 JSONObject dump = new JSONObject();
58
59 try {
jackqdyuleia9eb4a02016-12-20 10:08:19 -080060 dump.put(KEY_SERVICE, "Settings State");
61 dump.put(KEY_STORAGE, dumpStorage());
62 dump.put(KEY_DATAUSAGE, dumpDataUsage());
63 dump.put(KEY_MEMORY, dumpMemory());
64 dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser());
Jason Monkdc252eb2016-01-30 11:36:52 -050065 } catch (Exception e) {
66 e.printStackTrace();
67 }
68
69 writer.println(dump);
70 }
71
72 private JSONObject dumpMemory() throws JSONException {
73 JSONObject obj = new JSONObject();
74 ProcStatsData statsManager = new ProcStatsData(this, false);
75 statsManager.refreshStats(true);
76 ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
77
78 obj.put("used", String.valueOf(memInfo.realUsedRam));
79 obj.put("free", String.valueOf(memInfo.realFreeRam));
80 obj.put("total", String.valueOf(memInfo.realTotalRam));
81 obj.put("state", statsManager.getMemState());
82
83 return obj;
84 }
85
86 private JSONObject dumpDataUsage() throws JSONException {
87 JSONObject obj = new JSONObject();
88 DataUsageController controller = new DataUsageController(this);
89 ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class);
90 SubscriptionManager manager = SubscriptionManager.from(this);
91 TelephonyManager telephonyManager = TelephonyManager.from(this);
92 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
Jason Monkf3f6bd42016-02-03 13:13:15 -050093 JSONArray array = new JSONArray();
Jason Monkdc252eb2016-01-30 11:36:52 -050094 for (SubscriptionInfo info : manager.getAllSubscriptionInfoList()) {
95 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
96 telephonyManager.getSubscriberId(info.getSubscriptionId()));
Jason Monkf3f6bd42016-02-03 13:13:15 -050097 final JSONObject usage = dumpDataUsage(mobileAll, controller);
98 usage.put("subId", info.getSubscriptionId());
99 array.put(usage);
Jason Monkdc252eb2016-01-30 11:36:52 -0500100 }
Jason Monkf3f6bd42016-02-03 13:13:15 -0500101 obj.put("cell", array);
Jason Monkdc252eb2016-01-30 11:36:52 -0500102 }
103 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) {
104 obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller));
105 }
106 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
107 obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
108 }
109 return obj;
110 }
111
112 private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
113 throws JSONException {
114 JSONObject obj = new JSONObject();
115 DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
116 obj.put("carrier", usage.carrier);
117 obj.put("start", usage.startDate);
118 obj.put("usage", usage.usageLevel);
119 obj.put("warning", usage.warningLevel);
120 obj.put("limit", usage.limitLevel);
121 return obj;
122 }
123
124 private JSONObject dumpStorage() throws JSONException {
125 JSONObject obj = new JSONObject();
126 StorageManager manager = getSystemService(StorageManager.class);
127 for (VolumeInfo volume : manager.getVolumes()) {
128 JSONObject volObj = new JSONObject();
129 if (volume.isMountedReadable()) {
130 File path = volume.getPath();
131 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
132 volObj.put("total", String.valueOf(path.getTotalSpace()));
133 }
134 volObj.put("path", volume.getInternalPath());
135 volObj.put("state", volume.getState());
136 volObj.put("stateDesc", volume.getStateDescription());
137 volObj.put("description", volume.getDescription());
138 obj.put(volume.getId(), volObj);
139 }
140 return obj;
141 }
jackqdyuleia9eb4a02016-12-20 10:08:19 -0800142
143 @VisibleForTesting
144 String dumpDefaultBrowser() {
145 final ResolveInfo resolveInfo = getPackageManager().resolveActivity(
146 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY);
147
148 if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) {
149 return null;
150 } else {
151 return resolveInfo.activityInfo.packageName;
152 }
153 }
Jason Monkdc252eb2016-01-30 11:36:52 -0500154}