Search in sources :

Example 6 with Build

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));
}
Also used : Locale(java.util.Locale) Context(android.content.Context) PackageInfo(android.content.pm.PackageInfo) Device(com.microsoft.azure.mobile.ingestion.models.Device) Mockito.anyString(org.mockito.Mockito.anyString) Point(android.graphics.Point) WindowManager(android.view.WindowManager) TimeZone(java.util.TimeZone) PackageManager(android.content.pm.PackageManager) TelephonyManager(android.telephony.TelephonyManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Build(android.os.Build) WrapperSdk(com.microsoft.azure.mobile.ingestion.models.WrapperSdk) Display(android.view.Display) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with Build

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();
}
Also used : Converter(de.danoeh.antennapod.core.util.Converter) Context(android.content.Context) Bundle(android.os.Bundle) PackageManager(android.content.pm.PackageManager) Intent(android.content.Intent) ArrayList(java.util.ArrayList) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) Manifest(android.Manifest) Button(android.widget.Button) StorageUtils(de.danoeh.antennapod.core.util.StorageUtils) Build(android.os.Build) Log(android.util.Log) R(de.danoeh.antennapod.R) IntentFilter(android.content.IntentFilter) ContextCompat(android.support.v4.content.ContextCompat) TextUtils(android.text.TextUtils) BroadcastReceiver(android.content.BroadcastReceiver) ActivityCompat(android.support.v4.app.ActivityCompat) AppCompatActivity(android.support.v7.app.AppCompatActivity) File(java.io.File) List(java.util.List) AlertDialog(android.support.v7.app.AlertDialog) Html(android.text.Html) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Activity(android.app.Activity) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArrayList(java.util.ArrayList) File(java.io.File)

Example 8 with Build

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();
}
Also used : Context(android.content.Context) Arrays(java.util.Arrays) DateUtils(android.text.format.DateUtils) PackageManager(android.content.pm.PackageManager) Uri(android.net.Uri) AndroidSchedulers(rx.android.schedulers.AndroidSchedulers) FlattrUtils(de.danoeh.antennapod.core.util.flattr.FlattrUtils) PreferenceScreen(android.preference.PreferenceScreen) EditTextPreference(android.preference.EditTextPreference) UserPreferences(de.danoeh.antennapod.core.preferences.UserPreferences) Manifest(android.Manifest) ExportWorker(de.danoeh.antennapod.asynctask.ExportWorker) Schedulers(rx.schedulers.Schedulers) PreferenceManager(android.preference.PreferenceManager) GpodnetSyncService(de.danoeh.antennapod.core.service.GpodnetSyncService) Log(android.util.Log) WifiConfiguration(android.net.wifi.WifiConfiguration) GpodnetSetHostnameDialog(de.danoeh.antennapod.dialog.GpodnetSetHostnameDialog) GregorianCalendar(java.util.GregorianCalendar) ContextCompat(android.support.v4.content.ContextCompat) ActivityCompat(android.support.v4.app.ActivityCompat) DateFormat(android.text.format.DateFormat) ListPreference(android.preference.ListPreference) List(java.util.List) MainActivity(de.danoeh.antennapod.activity.MainActivity) HtmlWriter(de.danoeh.antennapod.core.export.html.HtmlWriter) ActivityNotFoundException(android.content.ActivityNotFoundException) Html(android.text.Html) AutoFlattrPreferenceDialog(de.danoeh.antennapod.dialog.AutoFlattrPreferenceDialog) ListView(android.widget.ListView) Snackbar(android.support.design.widget.Snackbar) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) Subscription(rx.Subscription) TextWatcher(android.text.TextWatcher) Converter(de.danoeh.antennapod.core.util.Converter) Context(android.content.Context) TimePickerDialog(android.app.TimePickerDialog) AboutActivity(de.danoeh.antennapod.activity.AboutActivity) AuthenticationDialog(de.danoeh.antennapod.dialog.AuthenticationDialog) ExportWriter(de.danoeh.antennapod.core.export.ExportWriter) Intent(android.content.Intent) CheckBoxPreference(android.preference.CheckBoxPreference) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Editable(android.text.Editable) ArrayList(java.util.ArrayList) Observable(rx.Observable) StatisticsActivity(de.danoeh.antennapod.activity.StatisticsActivity) SuppressLint(android.annotation.SuppressLint) OpmlWriter(de.danoeh.antennapod.core.export.opml.OpmlWriter) Calendar(java.util.Calendar) PreferenceActivity(de.danoeh.antennapod.activity.PreferenceActivity) Toast(android.widget.Toast) CrashReportWriter(de.danoeh.antennapod.CrashReportWriter) StorageUtils(de.danoeh.antennapod.core.util.StorageUtils) Build(android.os.Build) ProxyDialog(de.danoeh.antennapod.dialog.ProxyDialog) R(de.danoeh.antennapod.R) PreferenceActivityGingerbread(de.danoeh.antennapod.activity.PreferenceActivityGingerbread) VariableSpeedDialog(de.danoeh.antennapod.dialog.VariableSpeedDialog) ProgressDialog(android.app.ProgressDialog) File(java.io.File) DirectoryChooserActivity(de.danoeh.antennapod.activity.DirectoryChooserActivity) TimeUnit(java.util.concurrent.TimeUnit) WifiManager(android.net.wifi.WifiManager) AlertDialog(android.support.v7.app.AlertDialog) SharedPreferences(android.content.SharedPreferences) Preference(android.preference.Preference) GpodnetPreferences(de.danoeh.antennapod.core.preferences.GpodnetPreferences) Comparator(java.util.Comparator) Activity(android.app.Activity) Collections(java.util.Collections) EditText(android.widget.EditText) Resources(android.content.res.Resources) MaterialDialog(com.afollestad.materialdialogs.MaterialDialog) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) File(java.io.File)

Aggregations

Build (android.os.Build)8 Intent (android.content.Intent)5 Context (android.content.Context)4 PackageManager (android.content.pm.PackageManager)4 Manifest (android.Manifest)3 Activity (android.app.Activity)3 ActivityCompat (android.support.v4.app.ActivityCompat)3 ContextCompat (android.support.v4.content.ContextCompat)3 Html (android.text.Html)3 Log (android.util.Log)3 Toast (android.widget.Toast)3 MaterialDialog (com.afollestad.materialdialogs.MaterialDialog)3 File (java.io.File)3 SuppressLint (android.annotation.SuppressLint)2 TargetApi (android.annotation.TargetApi)2 SharedPreferences (android.content.SharedPreferences)2 Cursor (android.database.Cursor)2 Uri (android.net.Uri)2 Bundle (android.os.Bundle)2 AlertDialog (android.support.v7.app.AlertDialog)2