• 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 android.app.Service;
18 import android.content.Intent;
19 import android.content.pm.PackageManager;
20 import android.content.pm.ResolveInfo;
21 import android.net.ConnectivityManager;
22 import android.net.NetworkTemplate;
23 import android.net.Uri;
24 import android.os.IBinder;
25 import android.os.storage.StorageManager;
26 import android.os.storage.VolumeInfo;
27 import android.telephony.SubscriptionInfo;
28 import android.telephony.SubscriptionManager;
29 import android.telephony.TelephonyManager;
30 import com.android.internal.annotations.VisibleForTesting;
31 import com.android.settings.applications.ProcStatsData;
32 import com.android.settingslib.net.DataUsageController;
33 import org.json.JSONArray;
34 import org.json.JSONException;
35 import org.json.JSONObject;
36 
37 import java.io.File;
38 import java.io.FileDescriptor;
39 import java.io.PrintWriter;
40 
41 public class SettingsDumpService extends Service {
42     @VisibleForTesting static final String KEY_SERVICE = "service";
43     @VisibleForTesting static final String KEY_STORAGE = "storage";
44     @VisibleForTesting static final String KEY_DATAUSAGE = "datausage";
45     @VisibleForTesting static final String KEY_MEMORY = "memory";
46     @VisibleForTesting static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
47     @VisibleForTesting static final Intent BROWSER_INTENT =
48             new Intent("android.intent.action.VIEW", Uri.parse("http://"));
49 
50     @Override
onBind(Intent intent)51     public IBinder onBind(Intent intent) {
52         return null;
53     }
54 
55     @Override
dump(FileDescriptor fd, PrintWriter writer, String[] args)56     protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
57         JSONObject dump = new JSONObject();
58 
59         try {
60             dump.put(KEY_SERVICE, "Settings State");
61             dump.put(KEY_STORAGE, dumpStorage());
62             dump.put(KEY_DATAUSAGE, dumpDataUsage());
63             dump.put(KEY_MEMORY, dumpMemory());
64             dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser());
65         } catch (Exception e) {
66             e.printStackTrace();
67         }
68 
69         writer.println(dump);
70     }
71 
dumpMemory()72     private JSONObject dumpMemory() throws JSONException {
73         JSONObject obj = new JSONObject();
74         ProcStatsData statsManager = new ProcStatsData(this, false);
75         statsManager.refreshStats(true);
76         ProcStatsData.MemInfo memInfo = statsManager.getMemInfo();
77 
78         obj.put("used", String.valueOf(memInfo.realUsedRam));
79         obj.put("free", String.valueOf(memInfo.realFreeRam));
80         obj.put("total", String.valueOf(memInfo.realTotalRam));
81         obj.put("state", statsManager.getMemState());
82 
83         return obj;
84     }
85 
dumpDataUsage()86     private JSONObject dumpDataUsage() throws JSONException {
87         JSONObject obj = new JSONObject();
88         DataUsageController controller = new DataUsageController(this);
89         ConnectivityManager connectivityManager = getSystemService(ConnectivityManager.class);
90         SubscriptionManager manager = SubscriptionManager.from(this);
91         TelephonyManager telephonyManager = TelephonyManager.from(this);
92         if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)) {
93             JSONArray array = new JSONArray();
94             for (SubscriptionInfo info : manager.getAllSubscriptionInfoList()) {
95                 NetworkTemplate mobileAll = NetworkTemplate.buildTemplateMobileAll(
96                         telephonyManager.getSubscriberId(info.getSubscriptionId()));
97                 final JSONObject usage = dumpDataUsage(mobileAll, controller);
98                 usage.put("subId", info.getSubscriptionId());
99                 array.put(usage);
100             }
101             obj.put("cell", array);
102         }
103         if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_WIFI)) {
104             obj.put("wifi", dumpDataUsage(NetworkTemplate.buildTemplateWifiWildcard(), controller));
105         }
106         if (connectivityManager.isNetworkSupported(ConnectivityManager.TYPE_ETHERNET)) {
107             obj.put("ethernet", dumpDataUsage(NetworkTemplate.buildTemplateEthernet(), controller));
108         }
109         return obj;
110     }
111 
dumpDataUsage(NetworkTemplate template, DataUsageController controller)112     private JSONObject dumpDataUsage(NetworkTemplate template, DataUsageController controller)
113             throws JSONException {
114         JSONObject obj = new JSONObject();
115         DataUsageController.DataUsageInfo usage = controller.getDataUsageInfo(template);
116         obj.put("carrier", usage.carrier);
117         obj.put("start", usage.startDate);
118         obj.put("usage", usage.usageLevel);
119         obj.put("warning", usage.warningLevel);
120         obj.put("limit", usage.limitLevel);
121         return obj;
122     }
123 
dumpStorage()124     private JSONObject dumpStorage() throws JSONException {
125         JSONObject obj = new JSONObject();
126         StorageManager manager = getSystemService(StorageManager.class);
127         for (VolumeInfo volume : manager.getVolumes()) {
128             JSONObject volObj = new JSONObject();
129             if (volume.isMountedReadable()) {
130                 File path = volume.getPath();
131                 volObj.put("used", String.valueOf(path.getTotalSpace() - path.getFreeSpace()));
132                 volObj.put("total", String.valueOf(path.getTotalSpace()));
133             }
134             volObj.put("path", volume.getInternalPath());
135             volObj.put("state", volume.getState());
136             volObj.put("stateDesc", volume.getStateDescription());
137             volObj.put("description", volume.getDescription());
138             obj.put(volume.getId(), volObj);
139         }
140         return obj;
141     }
142 
143     @VisibleForTesting
dumpDefaultBrowser()144     String dumpDefaultBrowser() {
145         final ResolveInfo resolveInfo = getPackageManager().resolveActivity(
146                 BROWSER_INTENT, PackageManager.MATCH_DEFAULT_ONLY);
147 
148         if (resolveInfo == null || resolveInfo.activityInfo.packageName.equals("android")) {
149             return null;
150         } else {
151             return resolveInfo.activityInfo.packageName;
152         }
153     }
154 }
155