• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 import androidx.annotation.VisibleForTesting;
36 
37 import com.android.settings.applications.ProcStatsData;
38 import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
39 import com.android.settingslib.net.DataUsageController;
40 
41 import org.json.JSONArray;
42 import org.json.JSONException;
43 import org.json.JSONObject;
44 
45 import java.io.File;
46 import java.io.FileDescriptor;
47 import java.io.PrintWriter;
48 
49 public class SettingsDumpService extends Service {
50     @VisibleForTesting
51     static final String KEY_SERVICE = "service";
52     @VisibleForTesting
53     static final String KEY_STORAGE = "storage";
54     @VisibleForTesting
55     static final String KEY_DATAUSAGE = "datausage";
56     @VisibleForTesting
57     static final String KEY_MEMORY = "memory";
58     @VisibleForTesting
59     static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
60     @VisibleForTesting
61     static final String KEY_ANOMALY_DETECTION = "anomaly_detection";
62     @VisibleForTesting
63     static final Intent BROWSER_INTENT =
64             new Intent("android.intent.action.VIEW", Uri.parse("http://"));
65 
66     @Override
onBind(Intent intent)67     public IBinder onBind(Intent intent) {
68         return null;
69     }
70 
71     @Override
dump(FileDescriptor fd, PrintWriter writer, String[] args)72     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
73         JSONObject dump = new JSONObject();
74 
75         try {
76             dump.put(KEY_SERVICE, "Settings State");
77             dump.put(KEY_STORAGE, dumpStorage());
78             dump.put(KEY_DATAUSAGE, dumpDataUsage());
79             dump.put(KEY_MEMORY, dumpMemory());
80             dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser());
81             dump.put(KEY_ANOMALY_DETECTION, dumpAnomalyDetection());
82         } catch (Exception e) {
83             e.printStackTrace();
84         }
85 
86         writer.println(dump);
87     }
88 
dumpMemory()89     private JSONObject dumpMemory() throws JSONException {
90         JSONObject obj = new JSONObject();
91         ProcStatsData statsManager = new ProcStatsData(this, false);
92         statsManager.refreshStats(true);
93         ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
94 
95         obj.put("used", String.valueOf(memInfo.realUsedRam));
96         obj.put("free", String.valueOf(memInfo.realFreeRam));
97         obj.put("total", String.valueOf(memInfo.realTotalRam));
98         obj.put("state", statsManager.getMemState());
99 
100         return obj;
101     }
102 
dumpDataUsage()103     private JSONObject dumpDataUsage() throws JSONException {
104         JSONObject obj = new JSONObject();
105         DataUsageController controller = new DataUsageController(this);
106         SubscriptionManager manager = this.getSystemService(SubscriptionManager.class);
107         TelephonyManager telephonyManager = this.getSystemService(TelephonyManager.class);
108         final PackageManager packageManager = this.getPackageManager();
109         if (telephonyManager.isDataCapable()) {
110             JSONArray array = new JSONArray();
111             for (SubscriptionInfo info : manager.getAvailableSubscriptionInfoList()) {
112                 telephonyManager = telephonyManager
113                         .createForSubscriptionId(info.getSubscriptionId());
114                 String subscriberId = telephonyManager.getSubscriberId();
115                 // The null subscriberId means that no any mobile/carrier network will be matched.
116                 // Using old API: buildTemplateMobileAll for the null subscriberId to avoid NPE.
117                 NetworkTemplate template = subscriberId != null
118                         ? NetworkTemplate.buildTemplateCarrierMetered(subscriberId)
119                         : NetworkTemplate.buildTemplateMobileAll(subscriberId);
120                 final JSONObject usage = dumpDataUsage(template, controller);
121                 usage.put("subId", info.getSubscriptionId());
122                 array.put(usage);
123             }
124             obj.put("cell", array);
125         }
126         if (packageManager.hasSystemFeature(FEATURE_WIFI)) {
127             obj.put("wifi", dumpDataUsage(
128                     NetworkTemplate.buildTemplateWifi(
129                     NetworkTemplate.WIFI_NETWORKID_ALL, null /* subscriberId */), controller));
130         }
131 
132         if (packageManager.hasSystemFeature(FEATURE_ETHERNET)) {
133             obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
134         }
135         return obj;
136     }
137 
dumpDataUsage(NetworkTemplate template, DataUsageController controller)138     private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
139             throws JSONException {
140         JSONObject obj = new JSONObject();
141         DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
142         obj.put("carrier", usage.carrier);
143         obj.put("start", usage.startDate);
144         obj.put("usage", usage.usageLevel);
145         obj.put("warning", usage.warningLevel);
146         obj.put("limit", usage.limitLevel);
147         return obj;
148     }
149 
dumpStorage()150     private JSONObject dumpStorage() throws JSONException {
151         JSONObject obj = new JSONObject();
152         StorageManager manager = getSystemService(StorageManager.class);
153         for (VolumeInfo volume : manager.getVolumes()) {
154             JSONObject volObj = new JSONObject();
155             if (volume.isMountedReadable()) {
156                 File path = volume.getPath();
157                 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
158                 volObj.put("total", String.valueOf(path.getTotalSpace()));
159             }
160             volObj.put("path", volume.getInternalPath());
161             volObj.put("state", volume.getState());
162             volObj.put("stateDesc", volume.getStateDescription());
163             volObj.put("description", volume.getDescription());
164             obj.put(volume.getId(), volObj);
165         }
166         return obj;
167     }
168 
169     @VisibleForTesting
dumpDefaultBrowser()170     String dumpDefaultBrowser() {
171         final ResolveInfo resolveInfo = getPackageManager().resolveActivity(
172                 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY);
173 
174         if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) {
175             return null;
176         } else {
177             return resolveInfo.activityInfo.packageName;
178         }
179     }
180 
181     @VisibleForTesting
dumpAnomalyDetection()182     JSONObject dumpAnomalyDetection() throws JSONException {
183         final JSONObject obj = new JSONObject();
184         final SharedPreferences sharedPreferences = getSharedPreferences(
185                 AnomalyConfigJobService.PREF_DB,
186                 Context.MODE_PRIVATE);
187         final int currentVersion = sharedPreferences.getInt(
188                 AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION,
189                 0 /* defValue */);
190         obj.put("anomaly_config_version", String.valueOf(currentVersion));
191 
192         return obj;
193     }
194 }
195