Merge "Add anomaly log to dumpsys" into pi-dev

This commit is contained in:
TreeHugger Robot
2018-04-06 19:28:55 +00:00
committed by Android (Google) Code Review
3 changed files with 64 additions and 11 deletions

View File

@@ -15,7 +15,9 @@
package com.android.settings;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.ConnectivityManager;
@@ -27,9 +29,12 @@ import android.os.storage.VolumeInfo;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import com.android.internal.annotations.VisibleForTesting;
import com.android.settings.applications.ProcStatsData;
import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
import com.android.settingslib.net.DataUsageController;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@@ -39,12 +44,20 @@ import java.io.FileDescriptor;
import java.io.PrintWriter;
public class SettingsDumpService extends Service {
@VisibleForTesting static final String KEY_SERVICE = "service";
@VisibleForTesting static final String KEY_STORAGE = "storage";
@VisibleForTesting static final String KEY_DATAUSAGE = "datausage";
@VisibleForTesting static final String KEY_MEMORY = "memory";
@VisibleForTesting static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
@VisibleForTesting static final Intent BROWSER_INTENT =
@VisibleForTesting
static final String KEY_SERVICE = "service";
@VisibleForTesting
static final String KEY_STORAGE = "storage";
@VisibleForTesting
static final String KEY_DATAUSAGE = "datausage";
@VisibleForTesting
static final String KEY_MEMORY = "memory";
@VisibleForTesting
static final String KEY_DEFAULT_BROWSER_APP = "default_browser_app";
@VisibleForTesting
static final String KEY_ANOMALY_DETECTION = "anomaly_detection";
@VisibleForTesting
static final Intent BROWSER_INTENT =
new Intent("android.intent.action.VIEW", Uri.parse("http://"));
@Override
@@ -62,6 +75,7 @@ public class SettingsDumpService extends Service {
dump.put(KEY_DATAUSAGE, dumpDataUsage());
dump.put(KEY_MEMORY, dumpMemory());
dump.put(KEY_DEFAULT_BROWSER_APP, dumpDefaultBrowser());
dump.put(KEY_ANOMALY_DETECTION, dumpAnomalyDetection());
} catch (Exception e) {
e.printStackTrace();
}
@@ -151,4 +165,18 @@ public class SettingsDumpService extends Service {
return resolveInfo.activityInfo.packageName;
}
}
@VisibleForTesting
JSONObject dumpAnomalyDetection() throws JSONException {
final JSONObject obj = new JSONObject();
final SharedPreferences sharedPreferences = getSharedPreferences(
AnomalyConfigJobService.PREF_DB,
Context.MODE_PRIVATE);
final int currentVersion = sharedPreferences.getInt(
AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION,
0 /* defValue */);
obj.put("anomaly_config_version", String.valueOf(currentVersion));
return obj;
}
}

View File

@@ -39,9 +39,8 @@ import java.util.concurrent.TimeUnit;
public class AnomalyConfigJobService extends JobService {
private static final String TAG = "AnomalyConfigJobService";
@VisibleForTesting
static final String PREF_DB = "anomaly_pref";
private static final String KEY_ANOMALY_CONFIG_VERSION = "anomaly_config_version";
public static final String PREF_DB = "anomaly_pref";
public static final String KEY_ANOMALY_CONFIG_VERSION = "anomaly_config_version";
private static final int DEFAULT_VERSION = 0;
@VisibleForTesting

View File

@@ -16,13 +16,21 @@
package com.android.settings;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.annotation.NonNull;
import com.android.settings.fuelgauge.batterytip.AnomalyConfigJobService;
import com.android.settings.testutils.SettingsRobolectricTestRunner;
import org.json.JSONException;
@@ -32,6 +40,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RuntimeEnvironment;
import java.io.OutputStream;
import java.io.PrintWriter;
@@ -41,6 +50,7 @@ public class SettingsDumpServiceTest {
private static final String PACKAGE_BROWSER = "com.android.test.browser";
private static final String PACKAGE_NULL = "android";
private static final int ANOMALY_VERSION = 2;
@Mock
private PackageManager mPackageManager;
@@ -54,7 +64,7 @@ public class SettingsDumpServiceTest {
when(mPackageManager.resolveActivity(TestService.BROWSER_INTENT,
PackageManager.MATCH_DEFAULT_ONLY)).thenReturn(mResolveInfo);
mTestService = new TestService();
mTestService = spy(new TestService());
mTestService.setPackageManager(mPackageManager);
}
@@ -74,6 +84,22 @@ public class SettingsDumpServiceTest {
assertThat(mTestService.dumpDefaultBrowser()).isEqualTo(null);
}
@Test
public void testDumpAnomalyDetection_returnAnomalyInfo() throws JSONException {
final SharedPreferences sharedPreferences =
RuntimeEnvironment.application.getSharedPreferences(AnomalyConfigJobService.PREF_DB,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION, ANOMALY_VERSION);
editor.commit();
doReturn(sharedPreferences).when(mTestService).getSharedPreferences(anyString(), anyInt());
final JSONObject jsonObject = mTestService.dumpAnomalyDetection();
assertThat(jsonObject.getInt(AnomalyConfigJobService.KEY_ANOMALY_CONFIG_VERSION)).isEqualTo(
ANOMALY_VERSION);
}
@Test
public void testDump_ReturnJsonObject() throws JSONException {
mResolveInfo.activityInfo = new ActivityInfo();