Search in sources :

Example 21 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class DeviceInfoHelper method getDeviceInfo.

/**
 * Gets device information.
 *
 * @param context The context of the application.
 * @return {@link Device}
 * @throws DeviceInfoException If device information cannot be retrieved
 */
public static synchronized Device getDeviceInfo(Context context) throws DeviceInfoException {
    Device device = new Device();
    /* Application version. */
    PackageInfo packageInfo;
    try {
        PackageManager packageManager = context.getPackageManager();
        packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        device.setAppVersion(packageInfo.versionName);
        device.setAppBuild(String.valueOf(getVersionCode(packageInfo)));
    } catch (Exception e) {
        AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve package info", e);
        throw new DeviceInfoException("Cannot retrieve package info", e);
    }
    /* Application namespace. */
    device.setAppNamespace(context.getPackageName());
    /* Carrier info. */
    try {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        String networkCountryIso = telephonyManager.getNetworkCountryIso();
        if (!TextUtils.isEmpty(networkCountryIso)) {
            device.setCarrierCountry(networkCountryIso);
        }
        String networkOperatorName = telephonyManager.getNetworkOperatorName();
        if (!TextUtils.isEmpty(networkOperatorName)) {
            device.setCarrierName(networkOperatorName);
        }
    } catch (Exception e) {
        AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve carrier info", e);
    }
    /* Set country code. */
    if (mCountryCode != null) {
        device.setCarrierCountry(mCountryCode);
    }
    /* Locale. */
    device.setLocale(Locale.getDefault().toString());
    /* Hardware info. */
    device.setModel(Build.MODEL);
    device.setOemName(Build.MANUFACTURER);
    /* OS version. */
    device.setOsApiLevel(Build.VERSION.SDK_INT);
    device.setOsName(OS_NAME);
    device.setOsVersion(Build.VERSION.RELEASE);
    device.setOsBuild(Build.ID);
    /* Screen size. */
    try {
        device.setScreenSize(getScreenSize(context));
    } catch (Exception e) {
        AppCenterLog.error(AppCenter.LOG_TAG, "Cannot retrieve screen size", e);
    }
    /* Set SDK name and version. Don't add the BuildConfig import or it will trigger a Javadoc warning... */
    device.setSdkName(com.microsoft.appcenter.BuildConfig.SDK_NAME);
    device.setSdkVersion(com.microsoft.appcenter.BuildConfig.VERSION_NAME);
    /* Timezone offset in minutes (including DST). */
    device.setTimeZoneOffset(TimeZone.getDefault().getOffset(System.currentTimeMillis()) / 60 / 1000);
    /* Add wrapper SDK information if any. */
    if (sWrapperSdk != null) {
        device.setWrapperSdkVersion(sWrapperSdk.getWrapperSdkVersion());
        device.setWrapperSdkName(sWrapperSdk.getWrapperSdkName());
        device.setWrapperRuntimeVersion(sWrapperSdk.getWrapperRuntimeVersion());
        device.setLiveUpdateReleaseLabel(sWrapperSdk.getLiveUpdateReleaseLabel());
        device.setLiveUpdateDeploymentKey(sWrapperSdk.getLiveUpdateDeploymentKey());
        device.setLiveUpdatePackageHash(sWrapperSdk.getLiveUpdatePackageHash());
    }
    /* Return device properties. */
    return device;
}
Also used : PackageManager(android.content.pm.PackageManager) TelephonyManager(android.telephony.TelephonyManager) Device(com.microsoft.appcenter.ingestion.models.Device) PackageInfo(android.content.pm.PackageInfo)

Example 22 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class DeviceInfoHelperTest method getDeviceInfoMissingScreenSize.

@Test
public void getDeviceInfoMissingScreenSize() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mocking instances. */
    mockStatic(AppCenterLog.class);
    /* Delegates to mock instances. */
    when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
    when(mContext.getSystemService(Context.DISPLAY_SERVICE)).thenThrow(new RuntimeException());
    // noinspection WrongConstant
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo);
    /* Verify. */
    Device device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertNull(device.getScreenSize());
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(Exception.class));
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class DeviceInfoHelperTest method setNullCountryCode.

@Test
public void setNullCountryCode() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mock system calls. */
    String expectedCountryCode = "aa";
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo);
    when(mTelephonyManager.getNetworkCountryIso()).thenReturn(expectedCountryCode);
    when(mTelephonyManager.getNetworkOperatorName()).thenReturn(expectedCountryCode);
    when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
    /* Set country code. */
    DeviceInfoHelper.setCountryCode(null);
    /* Verify that system method was called. */
    Device device = DeviceInfoHelper.getDeviceInfo(mContext);
    verify(mTelephonyManager).getNetworkCountryIso();
    assertEquals(device.getCarrierCountry(), expectedCountryCode);
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) Mockito.anyString(org.mockito.Mockito.anyString) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 24 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class DeviceInfoHelperTest method getDeviceInfoMissingCarrierInfo.

@Test
public void getDeviceInfoMissingCarrierInfo() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Mocking instances. */
    mockStatic(AppCenterLog.class);
    /* Delegates to mock instances. */
    when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenThrow(new RuntimeException());
    when(mContext.getSystemService(Context.DISPLAY_SERVICE)).thenReturn(mDisplayManager);
    when(mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY)).thenReturn(mDisplay);
    when(mContext.getResources()).thenReturn(mResources);
    when(mResources.getDisplayMetrics()).thenReturn(mDisplayMetrics);
    // noinspection WrongConstant
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo);
    /* Verify. */
    Device device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertNull(device.getCarrierCountry());
    assertNull(device.getCarrierName());
    verifyStatic();
    AppCenterLog.error(eq(AppCenter.LOG_TAG), anyString(), any(Exception.class));
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 25 with Device

use of com.microsoft.appcenter.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.

the class DeviceInfoHelperTest method getDeviceInfoEmptyCarrierInfo.

@Test
public void getDeviceInfoEmptyCarrierInfo() throws DeviceInfoHelper.DeviceInfoException, PackageManager.NameNotFoundException {
    /* Delegates to mock instances. */
    // noinspection WrongConstant
    when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(mPackageInfo);
    when(mTelephonyManager.getNetworkCountryIso()).thenReturn("");
    when(mTelephonyManager.getNetworkOperatorName()).thenReturn("");
    when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
    mockStatic(TextUtils.class);
    when(TextUtils.isEmpty(anyString())).thenReturn(true);
    /* Verify. */
    Device device = DeviceInfoHelper.getDeviceInfo(mContext);
    assertNull(device.getCarrierCountry());
    assertNull(device.getCarrierName());
}
Also used : Device(com.microsoft.appcenter.ingestion.models.Device) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Device (com.microsoft.appcenter.ingestion.models.Device)46 Test (org.junit.Test)32 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)29 Context (android.content.Context)18 File (java.io.File)15 Date (java.util.Date)11 ManagedErrorLog (com.microsoft.appcenter.crashes.ingestion.models.ManagedErrorLog)10 Matchers.anyString (org.mockito.Matchers.anyString)10 TestCrashException (com.microsoft.appcenter.crashes.model.TestCrashException)9 IOException (java.io.IOException)9 ErrorReport (com.microsoft.appcenter.crashes.model.ErrorReport)8 Log (com.microsoft.appcenter.ingestion.models.Log)8 JSONException (org.json.JSONException)8 PackageInfo (android.content.pm.PackageInfo)7 PackageManager (android.content.pm.PackageManager)7 Build (android.os.Build)7 InvocationOnMock (org.mockito.invocation.InvocationOnMock)7 TelephonyManager (android.telephony.TelephonyManager)6 Log.getStackTraceString (android.util.Log.getStackTraceString)6 DefaultLogSerializer (com.microsoft.appcenter.ingestion.models.json.DefaultLogSerializer)6