| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 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 | |
| Chiachang Wang | ffbe4e9 | 2021-03-16 17:03:31 +0800 | [diff] [blame] | 17 | import static android.content.pm.PackageManager.FEATURE_ETHERNET; |
| 18 | import static android.content.pm.PackageManager.FEATURE_WIFI; |
| 19 | |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 20 | import android.app.Service; |
| Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 22 | import android.content.Intent; |
| Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 23 | import android.content.SharedPreferences; |
| jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.content.pm.ResolveInfo; |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 26 | import android.net.NetworkTemplate; |
| jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 27 | import android.net.Uri; |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 28 | import android.os.IBinder; |
| 29 | import android.os.storage.StorageManager; |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 30 | import android.os.storage.VolumeInfo; |
| 31 | import android.telephony.SubscriptionInfo; |
| 32 | import android.telephony.SubscriptionManager; |
| 33 | import android.telephony.TelephonyManager; |
| Zoey Chen | d6f3ad9 | 2023-03-25 08:00:09 +0000 | [diff] [blame] | 34 | import android.util.IndentingPrintWriter; |
| 35 | import android.util.Log; |
| Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 36 | |
| Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 37 | import androidx.annotation.VisibleForTesting; |
| 38 | |
| Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 39 | import com.android.settings.applications.ProcStatsData; |
| Junyu Lai | 123f2e1 | 2022-01-10 13:26:04 +0000 | [diff] [blame] | 40 | import com.android.settings.datausage.lib.DataUsageLib; |
| Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 41 | import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService; |
| Zoey Chen | d6f3ad9 | 2023-03-25 08:00:09 +0000 | [diff] [blame] | 42 | import com.android.settings.network.MobileNetworkRepository; |
| Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 43 | import com.android.settingslib.net.DataUsageController; |
| Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 44 | |
| Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 45 | import org.json.JSONArray; |
| 46 | import org.json.JSONException; |
| 47 | import org.json.JSONObject; |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 48 | |
| 49 | import java.io.File; |
| 50 | import java.io.FileDescriptor; |
| 51 | import java.io.PrintWriter; |
| 52 | |
| 53 | public class SettingsDumpService extends Service { |
| Zoey Chen | d6f3ad9 | 2023-03-25 08:00:09 +0000 | [diff] [blame] | 54 | |
| 55 | public static final String EXTRA_KEY_SHOW_NETWORK_DUMP = "show_network_dump"; |
| 56 | |
| 57 | private static final String TAG = "SettingsDumpService"; |
| Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 58 | @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 = |
| jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 72 | new Intent("android.intent.action.VIEW", Uri.parse("http://")); |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 73 | |
| Zoey Chen | d6f3ad9 | 2023-03-25 08:00:09 +0000 | [diff] [blame] | 74 | 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 Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 84 | @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 Chen | d6f3ad9 | 2023-03-25 08:00:09 +0000 | [diff] [blame] | 91 | 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 Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 111 | } |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 112 | } |
| 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); |
| changbetty | 50d7506 | 2020-01-15 16:57:19 +0800 | [diff] [blame] | 131 | SubscriptionManager manager = this.getSystemService(SubscriptionManager.class); |
| Bonian Chen | 228dd34 | 2019-12-23 04:54:51 +0800 | [diff] [blame] | 132 | TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class); |
| Chiachang Wang | ffbe4e9 | 2021-03-16 17:03:31 +0800 | [diff] [blame] | 133 | final PackageManager packageManager = this.getPackageManager(); |
| 134 | if (telephonyManager.isDataCapable()) { |
| Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 135 | JSONArray array = new JSONArray(); |
| changbetty | 50d7506 | 2020-01-15 16:57:19 +0800 | [diff] [blame] | 136 | for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) { |
| Junyu Lai | 123f2e1 | 2022-01-10 13:26:04 +0000 | [diff] [blame] | 137 | NetworkTemplate template = DataUsageLib.getMobileTemplateForSubId( |
| 138 | telephonyManager, info.getSubscriptionId()); |
| Les Lee | 204f021 | 2021-06-05 10:35:14 +0800 | [diff] [blame] | 139 | final JSONObject usage = dumpDataUsage(template, controller); |
| Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 140 | usage.put("subId", info.getSubscriptionId()); |
| 141 | array.put(usage); |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 142 | } |
| Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 143 | obj.put("cell", array); |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 144 | } |
| Chiachang Wang | ffbe4e9 | 2021-03-16 17:03:31 +0800 | [diff] [blame] | 145 | if (packageManager.hasSystemFeature(FEATURE_WIFI)) { |
| lesl | 26128e8 | 2021-03-11 22:15:29 +0800 | [diff] [blame] | 146 | obj.put("wifi", dumpDataUsage( |
| Junyu Lai | 123f2e1 | 2022-01-10 13:26:04 +0000 | [diff] [blame] | 147 | new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build(), controller)); |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 148 | } |
| Chiachang Wang | ffbe4e9 | 2021-03-16 17:03:31 +0800 | [diff] [blame] | 149 | |
| 150 | if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) { |
| Junyu Lai | 123f2e1 | 2022-01-10 13:26:04 +0000 | [diff] [blame] | 151 | obj.put("ethernet", dumpDataUsage(new NetworkTemplate.Builder( |
| 152 | NetworkTemplate.MATCH_ETHERNET).build(), controller)); |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 153 | } |
| 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 | } |
| jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 187 | |
| 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 Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 199 | |
| 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 Chen | d6f3ad9 | 2023-03-25 08:00:09 +0000 | [diff] [blame] | 213 | |
| 214 | private void dumpMobileNetworkSettings(IndentingPrintWriter writer) { |
| 215 | MobileNetworkRepository.getInstance(this).dump(writer); |
| 216 | } |
| Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 217 | } |