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 static android.content.pm.PackageManager.FEATURE_ETHERNET; 18 import static android.content.pm.PackageManager.FEATURE_WIFI; 19 20 import android.app.Service; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.content.pm.PackageManager; 25 import android.content.pm.ResolveInfo; 26 import android.net.NetworkTemplate; 27 import android.net.Uri; 28 import android.os.IBinder; 29 import android.os.storage.StorageManager; 30 import android.os.storage.VolumeInfo; 31 import android.telephony.SubscriptionInfo; 32 import android.telephony.SubscriptionManager; 33 import android.telephony.TelephonyManager; 34 import android.util.IndentingPrintWriter; 35 import android.util.Log; 36 37 import androidx.annotation.VisibleForTesting; 38 39 import com.android.settings.applications.ProcStatsData; 40 import com.android.settings.datausage.lib.DataUsageLib; 41 import com.android.settings.network.MobileNetworkRepository; 42 import com.android.settingslib.net.DataUsageController; 43 44 import org.json.JSONArray; 45 import org.json.JSONException; 46 import org.json.JSONObject; 47 48 import java.io.File; 49 import java.io.FileDescriptor; 50 import java.io.PrintWriter; 51 52 public class SettingsDumpService extends Service { 53 54 public static final String EXTRA_KEY_SHOW_NETWORK_DUMP = "show_network_dump"; 55 56 private static final String TAG = "SettingsDumpService"; 57 @VisibleForTesting 58 static final String KEY_SERVICE = "service"; 59 @VisibleForTesting 60 static final String KEY_STORAGE = "storage"; 61 @VisibleForTesting 62 static final String KEY_DATAUSAGE = "datausage"; 63 @VisibleForTesting 64 static final String KEY_MEMORY = "memory"; 65 @VisibleForTesting 66 static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app"; 67 @VisibleForTesting 68 static final String KEY_ANOMALY_DETECTION = "anomaly_detection"; 69 @VisibleForTesting 70 static final Intent BROWSER_INTENT = 71 new Intent("android.intent.action.VIEW", Uri.parse("http://")); 72 73 private boolean mShouldShowNetworkDump = false; 74 75 @Override onStartCommand(Intent intent, int flags, int startId)76 public int onStartCommand(Intent intent, int flags, int startId) { 77 if (intent != null) { 78 mShouldShowNetworkDump = intent.getBooleanExtra(EXTRA_KEY_SHOW_NETWORK_DUMP, false); 79 } 80 return Service.START_REDELIVER_INTENT; 81 } 82 83 @Override onBind(Intent intent)84 public IBinder onBind(Intent intent) { 85 return null; 86 } 87 88 @Override dump(FileDescriptor fd, PrintWriter writer, String[] args)89 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { 90 IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); 91 if (!mShouldShowNetworkDump) { 92 JSONObject dump = new JSONObject(); 93 pw.println(TAG + ": "); 94 pw.increaseIndent(); 95 try { 96 dump.put(KEY_SERVICE, "Settings State"); 97 dump.put(KEY_STORAGE, dumpStorage()); 98 dump.put(KEY_DATAUSAGE, dumpDataUsage()); 99 dump.put(KEY_MEMORY, dumpMemory()); 100 dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser()); 101 } catch (Exception e) { 102 Log.w(TAG, "exception in dump: ", e); 103 } 104 pw.println(dump); 105 pw.flush(); 106 pw.decreaseIndent(); 107 } else { 108 dumpMobileNetworkSettings(pw); 109 } 110 } 111 dumpMemory()112 private JSONObject dumpMemory() throws JSONException { 113 JSONObject obj = new JSONObject(); 114 ProcStatsData statsManager = new ProcStatsData(this, false); 115 statsManager.refreshStats(true); 116 ProcStatsData.MemInfo memInfo = statsManager.getMemInfo(); 117 118 obj.put("used", String.valueOf(memInfo.realUsedRam)); 119 obj.put("free", String.valueOf(memInfo.realFreeRam)); 120 obj.put("total", String.valueOf(memInfo.realTotalRam)); 121 obj.put("state", statsManager.getMemState()); 122 123 return obj; 124 } 125 dumpDataUsage()126 private JSONObject dumpDataUsage() throws JSONException { 127 JSONObject obj = new JSONObject(); 128 DataUsageController controller = new DataUsageController(this); 129 SubscriptionManager manager = this.getSystemService(SubscriptionManager.class); 130 TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class); 131 final PackageManager packageManager = this.getPackageManager(); 132 if (telephonyManager.isDataCapable()) { 133 JSONArray array = new JSONArray(); 134 for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) { 135 NetworkTemplate template = DataUsageLib.getMobileTemplateForSubId( 136 telephonyManager, info.getSubscriptionId()); 137 final JSONObject usage = dumpDataUsage(template, controller); 138 usage.put("subId", info.getSubscriptionId()); 139 array.put(usage); 140 } 141 obj.put("cell", array); 142 } 143 if (packageManager.hasSystemFeature(FEATURE_WIFI)) { 144 obj.put("wifi", dumpDataUsage( 145 new NetworkTemplate.Builder(NetworkTemplate.MATCH_WIFI).build(), controller)); 146 } 147 148 if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) { 149 obj.put("ethernet", dumpDataUsage(new NetworkTemplate.Builder( 150 NetworkTemplate.MATCH_ETHERNET).build(), controller)); 151 } 152 return obj; 153 } 154 dumpDataUsage(NetworkTemplate template, DataUsageController controller)155 private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller) 156 throws JSONException { 157 JSONObject obj = new JSONObject(); 158 DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template); 159 obj.put("carrier", usage.carrier); 160 obj.put("start", usage.startDate); 161 obj.put("usage", usage.usageLevel); 162 obj.put("warning", usage.warningLevel); 163 obj.put("limit", usage.limitLevel); 164 return obj; 165 } 166 dumpStorage()167 private JSONObject dumpStorage() throws JSONException { 168 JSONObject obj = new JSONObject(); 169 StorageManager manager = getSystemService(StorageManager.class); 170 for (VolumeInfo volume : manager.getVolumes()) { 171 JSONObject volObj = new JSONObject(); 172 if (volume.isMountedReadable()) { 173 File path = volume.getPath(); 174 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace())); 175 volObj.put("total", String.valueOf(path.getTotalSpace())); 176 } 177 volObj.put("path", volume.getInternalPath()); 178 volObj.put("state", volume.getState()); 179 volObj.put("stateDesc", volume.getStateDescription()); 180 volObj.put("description", volume.getDescription()); 181 obj.put(volume.getId(), volObj); 182 } 183 return obj; 184 } 185 186 @VisibleForTesting dumpDefaultBrowser()187 String dumpDefaultBrowser() { 188 final ResolveInfo resolveInfo = getPackageManager().resolveActivity( 189 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY); 190 191 if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) { 192 return null; 193 } else { 194 return resolveInfo.activityInfo.packageName; 195 } 196 } 197 dumpMobileNetworkSettings(IndentingPrintWriter writer)198 private void dumpMobileNetworkSettings(IndentingPrintWriter writer) { 199 MobileNetworkRepository.getInstance(this).dump(writer); 200 } 201 } 202