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 17 import android.app.Service; 18 import android.content.Context; 19 import android.content.Intent; 20 import android.content.SharedPreferences; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 import android.net.ConnectivityManager; 24 import android.net.NetworkTemplate; 25 import android.net.Uri; 26 import android.os.IBinder; 27 import android.os.storage.StorageManager; 28 import android.os.storage.VolumeInfo; 29 import android.telephony.SubscriptionInfo; 30 import android.telephony.SubscriptionManager; 31 import android.telephony.TelephonyManager; 32 33 import androidx.annotation.VisibleForTesting; 34 35 import com.android.settings.applications.ProcStatsData; 36 import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService; 37 import com.android.settingslib.net.DataUsageController; 38 39 import org.json.JSONArray; 40 import org.json.JSONException; 41 import org.json.JSONObject; 42 43 import java.io.File; 44 import java.io.FileDescriptor; 45 import java.io.PrintWriter; 46 47 public class SettingsDumpService extends Service { 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 = 62 new Intent("android.intent.action.VIEW", Uri.parse("http://")); 63 64 @Override onBind(Intent intent)65 public IBinder onBind(Intent intent) { 66 return null; 67 } 68 69 @Override dump(FileDescriptor fd, PrintWriter writer, String[] args)70 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 71 JSONObject dump = new JSONObject(); 72 73 try { 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()); 79 dump.put(KEY_ANOMALY_DETECTION, dumpAnomalyDetection()); 80 } catch (Exception e) { 81 e.printStackTrace(); 82 } 83 84 writer.println(dump); 85 } 86 dumpMemory()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 dumpDataUsage()101 private JSONObject dumpDataUsage() throws JSONException { 102 JSONObject obj = new JSONObject(); 103 DataUsageController controller = new DataUsageController(this); 104 ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class); 105 SubscriptionManager manager = SubscriptionManager.from(this); 106 TelephonyManager telephonyManager = TelephonyManager.from(this); 107 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) { 108 JSONArray array = new JSONArray(); 109 for (SubscriptionInfo info : manager.getAllSubscriptionInfoList()) { 110 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll( 111 telephonyManager.getSubscriberId(info.getSubscriptionId())); 112 final JSONObject usage = dumpDataUsage(mobileAll, controller); 113 usage.put("subId", info.getSubscriptionId()); 114 array.put(usage); 115 } 116 obj.put("cell", array); 117 } 118 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) { 119 obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller)); 120 } 121 if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) { 122 obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller)); 123 } 124 return obj; 125 } 126 dumpDataUsage(NetworkTemplate template, DataUsageController controller)127 private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller) 128 throws JSONException { 129 JSONObject obj = new JSONObject(); 130 DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template); 131 obj.put("carrier", usage.carrier); 132 obj.put("start", usage.startDate); 133 obj.put("usage", usage.usageLevel); 134 obj.put("warning", usage.warningLevel); 135 obj.put("limit", usage.limitLevel); 136 return obj; 137 } 138 dumpStorage()139 private JSONObject dumpStorage() throws JSONException { 140 JSONObject obj = new JSONObject(); 141 StorageManager manager = getSystemService(StorageManager.class); 142 for (VolumeInfo volume : manager.getVolumes()) { 143 JSONObject volObj = new JSONObject(); 144 if (volume.isMountedReadable()) { 145 File path = volume.getPath(); 146 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace())); 147 volObj.put("total", String.valueOf(path.getTotalSpace())); 148 } 149 volObj.put("path", volume.getInternalPath()); 150 volObj.put("state", volume.getState()); 151 volObj.put("stateDesc", volume.getStateDescription()); 152 volObj.put("description", volume.getDescription()); 153 obj.put(volume.getId(), volObj); 154 } 155 return obj; 156 } 157 158 @VisibleForTesting dumpDefaultBrowser()159 String dumpDefaultBrowser() { 160 final ResolveInfo resolveInfo = getPackageManager().resolveActivity( 161 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY); 162 163 if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) { 164 return null; 165 } else { 166 return resolveInfo.activityInfo.packageName; 167 } 168 } 169 170 @VisibleForTesting dumpAnomalyDetection()171 JSONObject dumpAnomalyDetection() throws JSONException { 172 final JSONObject obj = new JSONObject(); 173 final SharedPreferences sharedPreferences = getSharedPreferences( 174 AnomalyConfigJobService.PREF_DB, 175 Context.MODE_PRIVATE); 176 final int currentVersion = sharedPreferences.getInt( 177 AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION, 178 0 /* defValue */); 179 obj.put("anomaly_config_version", String.valueOf(currentVersion)); 180 181 return obj; 182 } 183 } 184