Java Code Examples for android.net.ConnectivityManager#getRestrictBackgroundStatus()

The following examples show how to use android.net.ConnectivityManager#getRestrictBackgroundStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FragmentSetup.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onResume() {
    super.onResume();

    // Doze
    Boolean ignoring = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
        PackageManager pm = getContext().getPackageManager();
        if (intent.resolveActivity(pm) != null) { // system whitelisted
            ignoring = Helper.isIgnoringOptimizations(getContext());
            if (ignoring == null)
                ignoring = true;
        }
    }

    btnDoze.setEnabled(!ignoring);

    // https://issuetracker.google.com/issues/37070074
    //ignoring = (ignoring || Build.VERSION.SDK_INT != Build.VERSION_CODES.M);

    tvDozeDone.setText(ignoring ? R.string.title_setup_done : R.string.title_setup_to_do);
    tvDozeDone.setTextColor(ignoring ? textColorPrimary : colorWarning);
    tvDozeDone.setCompoundDrawablesWithIntrinsicBounds(ignoring ? check : null, null, null, null);

    // https://developer.android.com/training/basics/network-ops/data-saver.html
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm != null) {
            int status = cm.getRestrictBackgroundStatus();
            grpDataSaver.setVisibility(
                    status == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED
                            ? View.VISIBLE : View.GONE);
        }
    }
}
 
Example 2
Source File: XmppActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
protected boolean isAffectedByDataSaver() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm != null
                && cm.isActiveNetworkMetered()
                && cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
    } else {
        return false;
    }
}
 
Example 3
Source File: XmppActivity.java    From Conversations with GNU General Public License v3.0 5 votes vote down vote up
protected boolean isAffectedByDataSaver() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        final ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm != null
                && cm.isActiveNetworkMetered()
                && cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
    } else {
        return false;
    }
}
 
Example 4
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
public static boolean dataSaving(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
}
 
Example 5
Source File: Log.java    From FairEmail with GNU General Public License v3.0 4 votes vote down vote up
private static StringBuilder getAppInfo(Context context) {
    StringBuilder sb = new StringBuilder();

    // Get version info
    String installer = context.getPackageManager().getInstallerPackageName(BuildConfig.APPLICATION_ID);
    sb.append(String.format("%s: %s/%s %s/%s%s%s%s\r\n",
            context.getString(R.string.app_name),
            BuildConfig.APPLICATION_ID,
            installer,
            BuildConfig.VERSION_NAME,
            Helper.hasValidFingerprint(context) ? "1" : "3",
            BuildConfig.PLAY_STORE_RELEASE ? "p" : "",
            BuildConfig.DEBUG ? "d" : "",
            ActivityBilling.isPro(context) ? "+" : ""));
    sb.append(String.format("Android: %s (SDK %d)\r\n", Build.VERSION.RELEASE, Build.VERSION.SDK_INT));
    sb.append("\r\n");

    // Get device info
    sb.append(String.format("uid: %s\r\n", android.os.Process.myUid()));
    sb.append(String.format("Brand: %s\r\n", Build.BRAND));
    sb.append(String.format("Manufacturer: %s\r\n", Build.MANUFACTURER));
    sb.append(String.format("Model: %s\r\n", Build.MODEL));
    sb.append(String.format("Product: %s\r\n", Build.PRODUCT));
    sb.append(String.format("Device: %s\r\n", Build.DEVICE));
    sb.append(String.format("Host: %s\r\n", Build.HOST));
    sb.append(String.format("Display: %s\r\n", Build.DISPLAY));
    sb.append(String.format("Id: %s\r\n", Build.ID));
    sb.append("\r\n");

    sb.append(String.format("Processors: %d\r\n", Runtime.getRuntime().availableProcessors()));

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    sb.append(String.format("Memory class: %d\r\n", am.getMemoryClass()));

    sb.append(String.format("Storage space: %s/%s\r\n",
            Helper.humanReadableByteCount(Helper.getAvailableStorageSpace(), true),
            Helper.humanReadableByteCount(Helper.getTotalStorageSpace(), true)));

    Runtime rt = Runtime.getRuntime();
    long hused = (rt.totalMemory() - rt.freeMemory()) / 1024L;
    long hmax = rt.maxMemory() / 1024L;
    long nheap = Debug.getNativeHeapAllocatedSize() / 1024L;
    sb.append(String.format("Heap usage: %s/%s KiB native: %s KiB\r\n", hused, hmax, nheap));

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    float density = context.getResources().getDisplayMetrics().density;
    sb.append(String.format("Density %f resolution: %.2f x %.2f dp %b\r\n",
            density,
            size.x / density, size.y / density,
            context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL)));

    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    boolean ignoring = true;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        ignoring = pm.isIgnoringBatteryOptimizations(BuildConfig.APPLICATION_ID);
    sb.append(String.format("Battery optimizations: %b\r\n", !ignoring));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        UsageStatsManager usm = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);
        int bucket = usm.getAppStandbyBucket();
        sb.append(String.format("Standby bucket: %d\r\n", bucket));
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        boolean saving = (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
        sb.append(String.format("Data saving: %b\r\n", saving));
    }

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    boolean reporting = prefs.getBoolean("crash_reports", false);
    if (reporting) {
        String uuid = prefs.getString("uuid", null);
        sb.append(String.format("UUID: %s\r\n", uuid == null ? "-" : uuid));
    }

    sb.append("\r\n");

    sb.append(new Date(Helper.getInstallTime(context))).append("\r\n");
    sb.append(new Date()).append("\r\n");

    sb.append("\r\n");

    return sb;
}
 
Example 6
Source File: Util.java    From kcanotify with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
public static boolean dataSaving(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
}
 
Example 7
Source File: Util.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.N)
public static boolean dataSaving(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return (cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED);
}
 
Example 8
Source File: NetworkStateUtil.java    From earth with GNU General Public License v3.0 4 votes vote down vote up
public static boolean shouldConsiderSavingData(ConnectivityManager cm) {
    // Do not trust ConnectivityManagerCompat, since it will always return ENABLED in API < N
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
            cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED;
}