blob: 2b6c7d87f89d169f5df5bbaffe8c9d399981ecff [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;
Lei Yu4daf2dc2018-04-03 13:09:28 -070018import android.content.Context;
Jason Monkdc252eb2016-01-30 11:36:52 -050019import android.content.Intent;
Lei Yu4daf2dc2018-04-03 13:09:28 -070020import android.content.SharedPreferences;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080021import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Jason Monkdc252eb2016-01-30 11:36:52 -050023import android.net.ConnectivityManager;
24import android.net.NetworkTemplate;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080025import android.net.Uri;
Jason Monkdc252eb2016-01-30 11:36:52 -050026import android.os.IBinder;
27import android.os.storage.StorageManager;
Jason Monkdc252eb2016-01-30 11:36:52 -050028import android.os.storage.VolumeInfo;
29import android.telephony.SubscriptionInfo;
30import android.telephony.SubscriptionManager;
31import android.telephony.TelephonyManager;
Lei Yu4daf2dc2018-04-03 13:09:28 -070032
Fan Zhang23f8d592018-08-28 15:11:40 -070033import androidx.annotation.VisibleForTesting;
34
Jason Monkf3f6bd42016-02-03 13:13:15 -050035import com.android.settings.applications.ProcStatsData;
Lei Yu4daf2dc2018-04-03 13:09:28 -070036import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
Jason Monkf3f6bd42016-02-03 13:13:15 -050037import com.android.settingslib.net.DataUsageController;
Lei Yu4daf2dc2018-04-03 13:09:28 -070038
Jason Monkf3f6bd42016-02-03 13:13:15 -050039import org.json.JSONArray;
40import org.json.JSONException;
41import org.json.JSONObject;
Jason Monkdc252eb2016-01-30 11:36:52 -050042
43import java.io.File;
44import java.io.FileDescriptor;
45import java.io.PrintWriter;
46
47public class SettingsDumpService extends Service {
Lei Yu4daf2dc2018-04-03 13:09:28 -070048 @VisibleForTesting
49 static final String KEY_SERVICE = "service";
50 @VisibleForTesting
51 static final String KEY_STORAGE = "storage";
52 @VisibleForTesting
53 static final String KEY_DATAUSAGE = "datausage";
54 @VisibleForTesting
55 static final String KEY_MEMORY = "memory";
56 @VisibleForTesting
57 static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
58 @VisibleForTesting
59 static final String KEY_ANOMALY_DETECTION = "anomaly_detection";
60 @VisibleForTesting
61 static final Intent BROWSER_INTENT =
jackqdyuleia9eb4a02016-12-20 10:08:19 -080062 new Intent("android.intent.action.VIEW", Uri.parse("http://"));
Jason Monkdc252eb2016-01-30 11:36:52 -050063
64 @Override
65 public IBinder onBind(Intent intent) {
66 return null;
67 }
68
69 @Override
70 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
71 JSONObject dump = new JSONObject();
72
73 try {
jackqdyuleia9eb4a02016-12-20 10:08:19 -080074 dump.put(KEY_SERVICE, "Settings State");
75 dump.put(KEY_STORAGE, dumpStorage());
76 dump.put(KEY_DATAUSAGE, dumpDataUsage());
77 dump.put(KEY_MEMORY, dumpMemory());
78 dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser());
Lei Yu4daf2dc2018-04-03 13:09:28 -070079 dump.put(KEY_ANOMALY_DETECTION, dumpAnomalyDetection());
Jason Monkdc252eb2016-01-30 11:36:52 -050080 } catch (Exception e) {
81 e.printStackTrace();
82 }
83
84 writer.println(dump);
85 }
86
87 private JSONObject dumpMemory() throws JSONException {
88 JSONObject obj = new JSONObject();
89 ProcStatsData statsManager = new ProcStatsData(this, false);
90 statsManager.refreshStats(true);
91 ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
92
93 obj.put("used", String.valueOf(memInfo.realUsedRam));
94 obj.put("free", String.valueOf(memInfo.realFreeRam));
95 obj.put("total", String.valueOf(memInfo.realTotalRam));
96 obj.put("state", statsManager.getMemState());
97
98 return obj;
99 }
100
101 private JSONObject dumpDataUsage() throws JSONException {
102 JSONObject obj = new JSONObject();
103 DataUsageController controller = new DataUsageController(this);
104 ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class);
changbetty50d75062020-01-15 16:57:19 +0800105 SubscriptionManager manager = this.getSystemService(SubscriptionManager.class);
Bonian Chen228dd342019-12-23 04:54:51 +0800106 TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class);
Jason Monkdc252eb2016-01-30 11:36:52 -0500107 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
Jason Monkf3f6bd42016-02-03 13:13:15 -0500108 JSONArray array = new JSONArray();
changbetty50d75062020-01-15 16:57:19 +0800109 for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) {
zoey chen353926a2019-12-24 19:37:11 +0800110 telephonyManager = telephonyManager
111 .createForSubscriptionId(info.getSubscriptionId());
Jason Monkdc252eb2016-01-30 11:36:52 -0500112 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
zoey chen353926a2019-12-24 19:37:11 +0800113 telephonyManager.getSubscriberId());
Jason Monkf3f6bd42016-02-03 13:13:15 -0500114 final JSONObject usage = dumpDataUsage(mobileAll, controller);
115 usage.put("subId", info.getSubscriptionId());
116 array.put(usage);
Jason Monkdc252eb2016-01-30 11:36:52 -0500117 }
Jason Monkf3f6bd42016-02-03 13:13:15 -0500118 obj.put("cell", array);
Jason Monkdc252eb2016-01-30 11:36:52 -0500119 }
120 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) {
121 obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller));
122 }
123 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
124 obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
125 }
126 return obj;
127 }
128
129 private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
130 throws JSONException {
131 JSONObject obj = new JSONObject();
132 DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
133 obj.put("carrier", usage.carrier);
134 obj.put("start", usage.startDate);
135 obj.put("usage", usage.usageLevel);
136 obj.put("warning", usage.warningLevel);
137 obj.put("limit", usage.limitLevel);
138 return obj;
139 }
140
141 private JSONObject dumpStorage() throws JSONException {
142 JSONObject obj = new JSONObject();
143 StorageManager manager = getSystemService(StorageManager.class);
144 for (VolumeInfo volume : manager.getVolumes()) {
145 JSONObject volObj = new JSONObject();
146 if (volume.isMountedReadable()) {
147 File path = volume.getPath();
148 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
149 volObj.put("total", String.valueOf(path.getTotalSpace()));
150 }
151 volObj.put("path", volume.getInternalPath());
152 volObj.put("state", volume.getState());
153 volObj.put("stateDesc", volume.getStateDescription());
154 volObj.put("description", volume.getDescription());
155 obj.put(volume.getId(), volObj);
156 }
157 return obj;
158 }
jackqdyuleia9eb4a02016-12-20 10:08:19 -0800159
160 @VisibleForTesting
161 String dumpDefaultBrowser() {
162 final ResolveInfo resolveInfo = getPackageManager().resolveActivity(
163 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY);
164
165 if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) {
166 return null;
167 } else {
168 return resolveInfo.activityInfo.packageName;
169 }
170 }
Lei Yu4daf2dc2018-04-03 13:09:28 -0700171
172 @VisibleForTesting
173 JSONObject dumpAnomalyDetection() throws JSONException {
174 final JSONObject obj = new JSONObject();
175 final SharedPreferences sharedPreferences = getSharedPreferences(
176 AnomalyConfigJobService.PREF_DB,
177 Context.MODE_PRIVATE);
178 final int currentVersion = sharedPreferences.getInt(
179 AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION,
180 0 /* defValue */);
181 obj.put("anomaly_config_version", String.valueOf(currentVersion));
182
183 return obj;
184 }
Jason Monkdc252eb2016-01-30 11:36:52 -0500185}