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;
}
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));
}
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);
}
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));
}
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());
}
Aggregations