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 | |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 17 | import android.app.Service; |
Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 18 | import android.content.Context; |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 19 | import android.content.Intent; |
Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 20 | import android.content.SharedPreferences; |
jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
| 22 | import android.content.pm.ResolveInfo; |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 23 | import android.net.ConnectivityManager; |
| 24 | import android.net.NetworkTemplate; |
jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 25 | import android.net.Uri; |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 26 | import android.os.IBinder; |
| 27 | import android.os.storage.StorageManager; |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 28 | import android.os.storage.VolumeInfo; |
| 29 | import android.telephony.SubscriptionInfo; |
| 30 | import android.telephony.SubscriptionManager; |
| 31 | import android.telephony.TelephonyManager; |
Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 32 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 33 | import androidx.annotation.VisibleForTesting; |
| 34 | |
Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 35 | import com.android.settings.applications.ProcStatsData; |
Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 36 | import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService; |
Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 37 | import com.android.settingslib.net.DataUsageController; |
Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 38 | |
Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 39 | import org.json.JSONArray; |
| 40 | import org.json.JSONException; |
| 41 | import org.json.JSONObject; |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 42 | |
| 43 | import java.io.File; |
| 44 | import java.io.FileDescriptor; |
| 45 | import java.io.PrintWriter; |
| 46 | |
| 47 | public class SettingsDumpService extends Service { |
Lei Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 48 | @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 = |
jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 62 | new Intent("android.intent.action.VIEW", Uri.parse("http://")); |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 63 | |
| 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 { |
jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 74 | 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 Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 79 | dump.put(KEY_ANOMALY_DETECTION, dumpAnomalyDetection()); |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 80 | } 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); |
changbetty | 50d7506 | 2020-01-15 16:57:19 +0800 | [diff] [blame] | 105 | SubscriptionManager manager = this.getSystemService(SubscriptionManager.class); |
Bonian Chen | 228dd34 | 2019-12-23 04:54:51 +0800 | [diff] [blame] | 106 | TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class); |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 107 | if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) { |
Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 108 | JSONArray array = new JSONArray(); |
changbetty | 50d7506 | 2020-01-15 16:57:19 +0800 | [diff] [blame] | 109 | for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) { |
zoey chen | 353926a | 2019-12-24 19:37:11 +0800 | [diff] [blame] | 110 | telephonyManager = telephonyManager |
| 111 | .createForSubscriptionId(info.getSubscriptionId()); |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 112 | NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll( |
zoey chen | 353926a | 2019-12-24 19:37:11 +0800 | [diff] [blame] | 113 | telephonyManager.getSubscriberId()); |
Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 114 | final JSONObject usage = dumpDataUsage(mobileAll, controller); |
| 115 | usage.put("subId", info.getSubscriptionId()); |
| 116 | array.put(usage); |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 117 | } |
Jason Monk | f3f6bd4 | 2016-02-03 13:13:15 -0500 | [diff] [blame] | 118 | obj.put("cell", array); |
Jason Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 119 | } |
| 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 | } |
jackqdyulei | a9eb4a0 | 2016-12-20 10:08:19 -0800 | [diff] [blame] | 159 | |
| 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 Yu | 4daf2dc | 2018-04-03 13:09:28 -0700 | [diff] [blame] | 171 | |
| 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 Monk | dc252eb | 2016-01-30 11:36:52 -0500 | [diff] [blame] | 185 | } |