blob: 4feb6b2056211050599550d494a9c8d206a3b131 [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
Chiachang Wangffbe4e92021-03-16 17:03:31 +080017import static android.content.pm.PackageManager.FEATURE_ETHERNET;
18import static android.content.pm.PackageManager.FEATURE_WIFI;
19
Jason Monkdc252eb2016-01-30 11:36:52 -050020import android.app.Service;
Lei Yu4daf2dc2018-04-03 13:09:28 -070021import android.content.Context;
Jason Monkdc252eb2016-01-30 11:36:52 -050022import android.content.Intent;
Lei Yu4daf2dc2018-04-03 13:09:28 -070023import android.content.SharedPreferences;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080024import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Jason Monkdc252eb2016-01-30 11:36:52 -050026import android.net.NetworkTemplate;
jackqdyuleia9eb4a02016-12-20 10:08:19 -080027import android.net.Uri;
Jason Monkdc252eb2016-01-30 11:36:52 -050028import android.os.IBinder;
29import android.os.storage.StorageManager;
Jason Monkdc252eb2016-01-30 11:36:52 -050030import android.os.storage.VolumeInfo;
31import android.telephony.SubscriptionInfo;
32import android.telephony.SubscriptionManager;
33import android.telephony.TelephonyManager;
Zoey Chend6f3ad92023-03-25 08:00:09 +000034import android.util.IndentingPrintWriter;
35import android.util.Log;
Lei Yu4daf2dc2018-04-03 13:09:28 -070036
Fan Zhang23f8d592018-08-28 15:11:40 -070037import androidx.annotation.VisibleForTesting;
38
Jason Monkf3f6bd42016-02-03 13:13:15 -050039import com.android.settings.applications.ProcStatsData;
Junyu Lai123f2e12022-01-10 13:26:04 +000040import com.android.settings.datausage.lib.DataUsageLib;
Lei Yu4daf2dc2018-04-03 13:09:28 -070041import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
Zoey Chend6f3ad92023-03-25 08:00:09 +000042import com.android.settings.network.MobileNetworkRepository;
Jason Monkf3f6bd42016-02-03 13:13:15 -050043import com.android.settingslib.net.DataUsageController;
Lei Yu4daf2dc2018-04-03 13:09:28 -070044
Jason Monkf3f6bd42016-02-03 13:13:15 -050045import org.json.JSONArray;
46import org.json.JSONException;
47import org.json.JSONObject;
Jason Monkdc252eb2016-01-30 11:36:52 -050048
49import java.io.File;
50import java.io.FileDescriptor;
51import java.io.PrintWriter;
52
53public class SettingsDumpService extends Service {
Zoey Chend6f3ad92023-03-25 08:00:09 +000054
55 public static final String EXTRA_KEY_SHOW_NETWORK_DUMP = "show_network_dump";
56
57 private static final String TAG = "SettingsDumpService";
Lei Yu4daf2dc2018-04-03 13:09:28 -070058 @VisibleForTesting
59 static final String KEY_SERVICE = "service";
60 @VisibleForTesting
61 static final String KEY_STORAGE = "storage";
62 @VisibleForTesting
63 static final String KEY_DATAUSAGE = "datausage";
64 @VisibleForTesting
65 static final String KEY_MEMORY = "memory";
66 @VisibleForTesting
67 static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
68 @VisibleForTesting
69 static final String KEY_ANOMALY_DETECTION = "anomaly_detection";
70 @VisibleForTesting
71 static final Intent BROWSER_INTENT =
jackqdyuleia9eb4a02016-12-20 10:08:19 -080072 new Intent("android.intent.action.VIEW", Uri.parse("http://"));
Jason Monkdc252eb2016-01-30 11:36:52 -050073
Zoey Chend6f3ad92023-03-25 08:00:09 +000074 private boolean mShouldShowNetworkDump = false;
75
76 @Override
77 public int onStartCommand(Intent intent, int flags, int startId) {
78 if (intent != null) {
79 mShouldShowNetworkDump = intent.getBooleanExtra(EXTRA_KEY_SHOW_NETWORK_DUMP, false);
80 }
81 return Service.START_REDELIVER_INTENT;
82 }
83
Jason Monkdc252eb2016-01-30 11:36:52 -050084 @Override
85 public IBinder onBind(Intent intent) {
86 return null;
87 }
88
89 @Override
90 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
Zoey Chend6f3ad92023-03-25 08:00:09 +000091 IndentingPrintWriter pw = new IndentingPrintWriter(writer, " ");
92 if (!mShouldShowNetworkDump) {
93 JSONObject dump = new JSONObject();
94 pw.println(TAG + ": ");
95 pw.increaseIndent();
96 try {
97 dump.put(KEY_SERVICE, "Settings State");
98 dump.put(KEY_STORAGE, dumpStorage());
99 dump.put(KEY_DATAUSAGE, dumpDataUsage());
100 dump.put(KEY_MEMORY, dumpMemory());
101 dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser());
102 dump.put(KEY_ANOMALY_DETECTION, dumpAnomalyDetection());
103 } catch (Exception e) {
104 Log.w(TAG, "exception in dump: ", e);
105 }
106 pw.println(dump);
107 pw.flush();
108 pw.decreaseIndent();
109 } else {
110 dumpMobileNetworkSettings(pw);
Jason Monkdc252eb2016-01-30 11:36:52 -0500111 }
Jason Monkdc252eb2016-01-30 11:36:52 -0500112 }
113
114 private JSONObject dumpMemory() throws JSONException {
115 JSONObject obj = new JSONObject();
116 ProcStatsData statsManager = new ProcStatsData(this, false);
117 statsManager.refreshStats(true);
118 ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
119
120 obj.put("used", String.valueOf(memInfo.realUsedRam));
121 obj.put("free", String.valueOf(memInfo.realFreeRam));
122 obj.put("total", String.valueOf(memInfo.realTotalRam));
123 obj.put("state", statsManager.getMemState());
124
125 return obj;
126 }
127
128 private JSONObject dumpDataUsage() throws JSONException {
129 JSONObject obj = new JSONObject();
130 DataUsageController controller = new DataUsageController(this);
changbetty50d75062020-01-15 16:57:19 +0800131 SubscriptionManager manager = this.getSystemService(SubscriptionManager.class);
Bonian Chen228dd342019-12-23 04:54:51 +0800132 TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class);
Chiachang Wangffbe4e92021-03-16 17:03:31 +0800133 final PackageManager packageManager = this.getPackageManager();
134 if (telephonyManager.isDataCapable()) {
Jason Monkf3f6bd42016-02-03 13:13:15 -0500135 JSONArray array = new JSONArray();
changbetty50d75062020-01-15 16:57:19 +0800136 for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) {
Junyu Lai123f2e12022-01-10 13:26:04 +0000137 NetworkTemplate template = DataUsageLib.getMobileTemplateForSubId(
138 telephonyManager, info.getSubscriptionId());
Les Lee204f0212021-06-05 10:35:14 +0800139 final JSONObject usage = dumpDataUsage(template, controller);
Jason Monkf3f6bd42016-02-03 13:13:15 -0500140 usage.put("subId", info.getSubscriptionId());
141 array.put(usage);
Jason Monkdc252eb2016-01-30 11:36:52 -0500142 }
Jason Monkf3f6bd42016-02-03 13:13:15 -0500143 obj.put("cell", array);
Jason Monkdc252eb2016-01-30 11:36:52 -0500144 }
Chiachang Wangffbe4e92021-03-16 17:03:31 +0800145 if (packageManager.hasSystemFeature(FEATURE_WIFI)) {
lesl26128e82021-03-11 22:15:29 +0800146 obj.put("wifi", dumpDataUsage(
Junyu Lai123f2e12022-01-10 13:26:04 +0000147 new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build(), controller));
Jason Monkdc252eb2016-01-30 11:36:52 -0500148 }
Chiachang Wangffbe4e92021-03-16 17:03:31 +0800149
150 if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) {
Junyu Lai123f2e12022-01-10 13:26:04 +0000151 obj.put("ethernet", dumpDataUsage(new NetworkTemplate.Builder(
152 NetworkTemplate.MATCH_ETHERNET).build(), controller));
Jason Monkdc252eb2016-01-30 11:36:52 -0500153 }
154 return obj;
155 }
156
157 private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
158 throws JSONException {
159 JSONObject obj = new JSONObject();
160 DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
161 obj.put("carrier", usage.carrier);
162 obj.put("start", usage.startDate);
163 obj.put("usage", usage.usageLevel);
164 obj.put("warning", usage.warningLevel);
165 obj.put("limit", usage.limitLevel);
166 return obj;
167 }
168
169 private JSONObject dumpStorage() throws JSONException {
170 JSONObject obj = new JSONObject();
171 StorageManager manager = getSystemService(StorageManager.class);
172 for (VolumeInfo volume : manager.getVolumes()) {
173 JSONObject volObj = new JSONObject();
174 if (volume.isMountedReadable()) {
175 File path = volume.getPath();
176 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
177 volObj.put("total", String.valueOf(path.getTotalSpace()));
178 }
179 volObj.put("path", volume.getInternalPath());
180 volObj.put("state", volume.getState());
181 volObj.put("stateDesc", volume.getStateDescription());
182 volObj.put("description", volume.getDescription());
183 obj.put(volume.getId(), volObj);
184 }
185 return obj;
186 }
jackqdyuleia9eb4a02016-12-20 10:08:19 -0800187
188 @VisibleForTesting
189 String dumpDefaultBrowser() {
190 final ResolveInfo resolveInfo = getPackageManager().resolveActivity(
191 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY);
192
193 if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) {
194 return null;
195 } else {
196 return resolveInfo.activityInfo.packageName;
197 }
198 }
Lei Yu4daf2dc2018-04-03 13:09:28 -0700199
200 @VisibleForTesting
201 JSONObject dumpAnomalyDetection() throws JSONException {
202 final JSONObject obj = new JSONObject();
203 final SharedPreferences sharedPreferences = getSharedPreferences(
204 AnomalyConfigJobService.PREF_DB,
205 Context.MODE_PRIVATE);
206 final int currentVersion = sharedPreferences.getInt(
207 AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION,
208 0 /* defValue */);
209 obj.put("anomaly_config_version", String.valueOf(currentVersion));
210
211 return obj;
212 }
Zoey Chend6f3ad92023-03-25 08:00:09 +0000213
214 private void dumpMobileNetworkSettings(IndentingPrintWriter writer) {
215 MobileNetworkRepository.getInstance(this).dump(writer);
216 }
Jason Monkdc252eb2016-01-30 11:36:52 -0500217}