use of android.os.Build 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 android.os.Build in project AntennaPod by AntennaPod.
the class StorageErrorActivity method showChooseDataFolderDialog.
// see PreferenceController.showChooseDataFolderDialog()
private void showChooseDataFolderDialog() {
File dataFolder = UserPreferences.getDataFolder(null);
if (dataFolder == null) {
new MaterialDialog.Builder(this).title(R.string.error_label).content(R.string.external_storage_error_msg).neutralText(android.R.string.ok).show();
return;
}
String dataFolderPath = dataFolder.getAbsolutePath();
int selectedIndex = -1;
File[] mediaDirs = ContextCompat.getExternalFilesDirs(this, null);
List<String> folders = new ArrayList<>(mediaDirs.length);
List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
for (int i = 0; i < mediaDirs.length; i++) {
File dir = mediaDirs[i];
if (dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
continue;
}
String path = mediaDirs[i].getAbsolutePath();
folders.add(path);
if (dataFolderPath.equals(path)) {
selectedIndex = i;
}
int index = path.indexOf("Android");
String choice;
if (index >= 0) {
choice = path.substring(0, index);
} else {
choice = path;
}
long bytes = StorageUtils.getFreeSpaceAvailable(path);
String freeSpace = String.format(getString(R.string.free_space_label), Converter.byteToString(bytes));
choices.add(Html.fromHtml("<html><small>" + choice + " [" + freeSpace + "]" + "</small></html>"));
}
if (choices.size() == 0) {
new MaterialDialog.Builder(this).title(R.string.error_label).content(R.string.external_storage_error_msg).neutralText(android.R.string.ok).show();
return;
}
MaterialDialog dialog = new MaterialDialog.Builder(this).title(R.string.choose_data_directory).content(R.string.choose_data_directory_message).items(choices.toArray(new CharSequence[choices.size()])).itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
String folder = folders.get(which);
UserPreferences.setDataFolder(folder);
leaveErrorState();
return true;
}).negativeText(R.string.cancel_label).cancelable(true).build();
dialog.show();
}
use of android.os.Build in project AntennaPod by AntennaPod.
the class PreferenceController method showChooseDataFolderDialog.
private void showChooseDataFolderDialog() {
Context context = ui.getActivity();
File dataFolder = UserPreferences.getDataFolder(null);
if (dataFolder == null) {
new MaterialDialog.Builder(ui.getActivity()).title(R.string.error_label).content(R.string.external_storage_error_msg).neutralText(android.R.string.ok).show();
return;
}
String dataFolderPath = dataFolder.getAbsolutePath();
int selectedIndex = -1;
File[] mediaDirs = ContextCompat.getExternalFilesDirs(context, null);
List<String> folders = new ArrayList<>(mediaDirs.length);
List<CharSequence> choices = new ArrayList<>(mediaDirs.length);
for (int i = 0; i < mediaDirs.length; i++) {
File dir = mediaDirs[i];
if (dir == null || !dir.exists() || !dir.canRead() || !dir.canWrite()) {
continue;
}
String path = mediaDirs[i].getAbsolutePath();
folders.add(path);
if (dataFolderPath.equals(path)) {
selectedIndex = i;
}
int index = path.indexOf("Android");
String choice;
if (index >= 0) {
choice = path.substring(0, index);
} else {
choice = path;
}
long bytes = StorageUtils.getFreeSpaceAvailable(path);
String freeSpace = String.format(context.getString(R.string.free_space_label), Converter.byteToString(bytes));
choices.add(Html.fromHtml("<html><small>" + choice + " [" + freeSpace + "]" + "</small></html>"));
}
if (choices.size() == 0) {
new MaterialDialog.Builder(ui.getActivity()).title(R.string.error_label).content(R.string.external_storage_error_msg).neutralText(android.R.string.ok).show();
return;
}
MaterialDialog dialog = new MaterialDialog.Builder(ui.getActivity()).title(R.string.choose_data_directory).content(R.string.choose_data_directory_message).items(choices.toArray(new CharSequence[choices.size()])).itemsCallbackSingleChoice(selectedIndex, (dialog1, itemView, which, text) -> {
String folder = folders.get(which);
Log.d(TAG, "data folder: " + folder);
UserPreferences.setDataFolder(folder);
setDataFolderText();
return true;
}).negativeText(R.string.cancel_label).cancelable(true).build();
dialog.show();
}
Aggregations