use of com.microsoft.azure.mobile.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(packageInfo.versionCode));
} catch (Exception e) {
MobileCenterLog.error(MobileCenter.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) {
MobileCenterLog.error(MobileCenter.LOG_TAG, "Cannot retrieve carrier info", e);
}
/* 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) {
MobileCenterLog.error(MobileCenter.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.azure.mobile.BuildConfig.SDK_NAME);
device.setSdkVersion(com.microsoft.azure.mobile.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.azure.mobile.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class DeviceInfoHelperTest method getDeviceInfo.
@Test
public void getDeviceInfo() throws PackageManager.NameNotFoundException, DeviceInfoHelper.DeviceInfoException {
/* Mock data. */
final String appVersion = "1.0";
final String appBuild = "1";
//noinspection SpellCheckingInspection
final String appNamespace = "com.contoso.app";
final String carrierCountry = "us";
final String carrierName = "mock-service";
final Locale locale = Locale.KOREA;
final String model = "mock-model";
final String oemName = "mock-manufacture";
final Integer osApiLevel = 23;
final String osName = "Android";
final String osVersion = "mock-version";
final String osBuild = "mock-os-build";
final String screenSizeLandscape = "100x200";
final String screenSizePortrait = "200x100";
final TimeZone timeZone = TimeZone.getTimeZone("KST");
final Integer timeZoneOffset = timeZone.getOffset(System.currentTimeMillis());
Locale.setDefault(locale);
TimeZone.setDefault(timeZone);
/* Mocking instances. */
Context contextMock = mock(Context.class);
PackageManager packageManagerMock = mock(PackageManager.class);
PackageInfo packageInfoMock = mock(PackageInfo.class);
WindowManager windowManagerMock = mock(WindowManager.class);
TelephonyManager telephonyManagerMock = mock(TelephonyManager.class);
Display displayMock = mock(Display.class);
/* Delegates to mock instances. */
when(contextMock.getPackageName()).thenReturn(appNamespace);
when(contextMock.getPackageManager()).thenReturn(packageManagerMock);
//noinspection WrongConstant
when(contextMock.getSystemService(eq(Context.TELEPHONY_SERVICE))).thenReturn(telephonyManagerMock);
//noinspection WrongConstant
when(contextMock.getSystemService(eq(Context.WINDOW_SERVICE))).thenReturn(windowManagerMock);
//noinspection WrongConstant
when(packageManagerMock.getPackageInfo(anyString(), eq(0))).thenReturn(packageInfoMock);
when(telephonyManagerMock.getNetworkCountryIso()).thenReturn(carrierCountry);
when(telephonyManagerMock.getNetworkOperatorName()).thenReturn(carrierName);
when(windowManagerMock.getDefaultDisplay()).thenReturn(displayMock);
when(displayMock.getRotation()).thenReturn(Surface.ROTATION_0, Surface.ROTATION_90, Surface.ROTATION_180, Surface.ROTATION_270);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] args = invocationOnMock.getArguments();
/* DO NOT call set method and assign values directly to variables. */
((Point) args[0]).x = 100;
((Point) args[0]).y = 200;
return null;
}
}).when(displayMock).getSize(any(Point.class));
/* Sets values of fields for static classes. */
Whitebox.setInternalState(packageInfoMock, "versionName", appVersion);
Whitebox.setInternalState(packageInfoMock, "versionCode", Integer.parseInt(appBuild));
Whitebox.setInternalState(Build.class, "MODEL", model);
Whitebox.setInternalState(Build.class, "MANUFACTURER", oemName);
Whitebox.setInternalState(Build.VERSION.class, "SDK_INT", osApiLevel);
Whitebox.setInternalState(Build.class, "ID", osBuild);
Whitebox.setInternalState(Build.VERSION.class, "RELEASE", osVersion);
/* First call */
Device device = DeviceInfoHelper.getDeviceInfo(contextMock);
/* Verify device information. */
assertNull(device.getWrapperSdkName());
assertNull(device.getWrapperSdkVersion());
assertEquals(BuildConfig.VERSION_NAME, device.getSdkVersion());
assertEquals(appVersion, device.getAppVersion());
assertEquals(appBuild, device.getAppBuild());
assertEquals(appNamespace, device.getAppNamespace());
assertEquals(carrierCountry, device.getCarrierCountry());
assertEquals(carrierName, device.getCarrierName());
assertEquals(locale.toString(), device.getLocale());
assertEquals(model, device.getModel());
assertEquals(oemName, device.getOemName());
assertEquals(osApiLevel, device.getOsApiLevel());
assertEquals(osName, device.getOsName());
assertEquals(osVersion, device.getOsVersion());
assertEquals(osBuild, device.getOsBuild());
assertEquals(screenSizeLandscape, device.getScreenSize());
assertEquals(timeZoneOffset, device.getTimeZoneOffset());
/* Verify screen size based on different orientations (Surface.ROTATION_90). */
device = DeviceInfoHelper.getDeviceInfo(contextMock);
assertEquals(screenSizePortrait, device.getScreenSize());
/* Verify screen size based on different orientations (Surface.ROTATION_180). */
device = DeviceInfoHelper.getDeviceInfo(contextMock);
assertEquals(screenSizeLandscape, device.getScreenSize());
/* Verify screen size based on different orientations (Surface.ROTATION_270). */
device = DeviceInfoHelper.getDeviceInfo(contextMock);
assertEquals(screenSizePortrait, device.getScreenSize());
/* Make sure screen size is verified for all orientations. */
verify(displayMock, times(4)).getRotation();
/* Set wrapper sdk information. */
WrapperSdk wrapperSdk = new WrapperSdk();
wrapperSdk.setWrapperSdkVersion("1.2.3.4");
wrapperSdk.setWrapperSdkName("ReactNative");
wrapperSdk.setWrapperRuntimeVersion("4.13");
wrapperSdk.setLiveUpdateReleaseLabel("2.0.3-beta2");
wrapperSdk.setLiveUpdateDeploymentKey("staging");
wrapperSdk.setLiveUpdatePackageHash("aa896f791b26a7f464c0f62b0ba69f2b");
DeviceInfoHelper.setWrapperSdk(wrapperSdk);
Device device2 = DeviceInfoHelper.getDeviceInfo(contextMock);
assertEquals(wrapperSdk.getWrapperSdkVersion(), device2.getWrapperSdkVersion());
assertEquals(wrapperSdk.getWrapperSdkName(), device2.getWrapperSdkName());
assertEquals(wrapperSdk.getWrapperRuntimeVersion(), device2.getWrapperRuntimeVersion());
assertEquals(wrapperSdk.getLiveUpdateReleaseLabel(), device2.getLiveUpdateReleaseLabel());
assertEquals(wrapperSdk.getLiveUpdateDeploymentKey(), device2.getLiveUpdateDeploymentKey());
assertEquals(wrapperSdk.getLiveUpdatePackageHash(), device2.getLiveUpdatePackageHash());
/* Check non wrapped sdk information are still generated correctly. */
device2.setWrapperSdkVersion(null);
device2.setWrapperSdkName(null);
device2.setWrapperRuntimeVersion(null);
device2.setLiveUpdateReleaseLabel(null);
device2.setLiveUpdateDeploymentKey(null);
device2.setLiveUpdatePackageHash(null);
assertEquals(device, device2);
/* Remove wrapper SDK information. */
DeviceInfoHelper.setWrapperSdk(null);
assertEquals(device, DeviceInfoHelper.getDeviceInfo(contextMock));
}
use of com.microsoft.azure.mobile.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 {
/* Mocking instances. */
Context contextMock = mock(Context.class);
PackageManager packageManagerMock = mock(PackageManager.class);
/* Delegates to mock instances. */
when(contextMock.getPackageManager()).thenReturn(packageManagerMock);
//noinspection WrongConstant
when(packageManagerMock.getPackageInfo(anyString(), anyInt())).thenReturn(mock(PackageInfo.class));
TelephonyManager telephonyManager = mock(TelephonyManager.class);
when(telephonyManager.getNetworkCountryIso()).thenReturn("");
when(telephonyManager.getNetworkOperatorName()).thenReturn("");
when(contextMock.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(telephonyManager);
mockStatic(TextUtils.class);
when(TextUtils.isEmpty(anyString())).thenReturn(true);
/* Verify. */
Device device = DeviceInfoHelper.getDeviceInfo(contextMock);
assertNull(device.getCarrierCountry());
assertNull(device.getCarrierName());
}
use of com.microsoft.azure.mobile.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class AnalyticsSerializerTest method someBatch.
@Test
public void someBatch() throws JSONException {
LogContainer expectedContainer = new LogContainer();
Device device = new Device();
device.setSdkName("mobilecenter.android");
device.setSdkVersion("1.2.3");
device.setModel("S5");
device.setOemName("HTC");
device.setOsName("Android");
device.setOsVersion("4.0.3");
device.setOsBuild("LMY47X");
device.setOsApiLevel(15);
device.setLocale("en_US");
device.setTimeZoneOffset(120);
device.setScreenSize("800x600");
device.setAppVersion("3.2.1");
device.setAppBuild("42");
List<Log> logs = new ArrayList<>();
{
logs.add(new StartSessionLog());
}
expectedContainer.setLogs(logs);
{
PageLog pageLog = new PageLog();
pageLog.setName("home");
logs.add(pageLog);
}
{
PageLog pageLog = new PageLog();
pageLog.setName("settings");
pageLog.setProperties(new HashMap<String, String>() {
{
put("from", "home_menu");
put("orientation", "portrait");
}
});
logs.add(pageLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("subscribe");
logs.add(eventLog);
}
{
EventLog eventLog = new EventLog();
eventLog.setId(UUIDUtils.randomUUID());
eventLog.setName("click");
eventLog.setProperties(new HashMap<String, String>() {
{
put("x", "1");
put("y", "2");
}
});
logs.add(eventLog);
}
UUID sid = UUIDUtils.randomUUID();
for (Log log : logs) {
log.setSid(sid);
log.setDevice(device);
}
LogSerializer serializer = new DefaultLogSerializer();
serializer.addLogFactory(StartSessionLog.TYPE, new StartSessionLogFactory());
serializer.addLogFactory(PageLog.TYPE, new PageLogFactory());
serializer.addLogFactory(EventLog.TYPE, new EventLogFactory());
String payload = serializer.serializeContainer(expectedContainer);
android.util.Log.v(TAG, payload);
LogContainer actualContainer = serializer.deserializeContainer(payload);
Assert.assertEquals(expectedContainer, actualContainer);
}
use of com.microsoft.azure.mobile.ingestion.models.Device in project mobile-center-sdk-android by Microsoft.
the class DeviceInfoActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_info);
mNetworkStateHelper = NetworkStateHelper.getSharedInstance(this);
mNetworkStateHelper.addListener(this);
onNetworkStateUpdated(mNetworkStateHelper.isNetworkConnected());
Device log;
try {
log = DeviceInfoHelper.getDeviceInfo(getApplicationContext());
} catch (DeviceInfoHelper.DeviceInfoException e) {
Toast.makeText(getBaseContext(), R.string.error_device_info, Toast.LENGTH_LONG).show();
return;
}
final List<DeviceInfoDisplayModel> list = getDeviceInfoDisplayModelList(log);
ArrayAdapter<DeviceInfoDisplayModel> adapter = new ArrayAdapter<DeviceInfoDisplayModel>(this, android.R.layout.simple_list_item_2, android.R.id.text1, list) {
@NonNull
@Override
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text1 = (TextView) view.findViewById(android.R.id.text1);
TextView text2 = (TextView) view.findViewById(android.R.id.text2);
text1.setText(list.get(position).title);
text2.setText(list.get(position).value);
return view;
}
};
((ListView) findViewById(R.id.device_info_list_view)).setAdapter(adapter);
}
Aggregations