Example usage for android.net ConnectivityManager getRestrictBackgroundStatus

List of usage examples for android.net ConnectivityManager getRestrictBackgroundStatus

Introduction

In this page you can find the example usage for android.net ConnectivityManager getRestrictBackgroundStatus.

Prototype

public @RestrictBackgroundStatus int getRestrictBackgroundStatus() 

Source Link

Document

Determines if the calling application is subject to metered network restrictions while running on background.

Usage

From source file:android_network.hetnet.vpn_service.Util.java

@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);
}

From source file:org.telegram.messenger.GcmPushListenerService.java

@Override
public void onMessageReceived(String from, final Bundle bundle) {
    FileLog.d("GCM received bundle: " + bundle + " from: " + from);
    AndroidUtilities.runOnUIThread(new Runnable() {
        @Override/*from  www.  j  ava2s .  c  o m*/
        public void run() {
            ApplicationLoader.postInitApplication();

            try {
                String key = bundle.getString("loc_key");
                if ("DC_UPDATE".equals(key)) {
                    String data = bundle.getString("custom");
                    JSONObject object = new JSONObject(data);
                    int dc = object.getInt("dc");
                    String addr = object.getString("addr");
                    String[] parts = addr.split(":");
                    if (parts.length != 2) {
                        return;
                    }
                    String ip = parts[0];
                    int port = Integer.parseInt(parts[1]);
                    ConnectionsManager.getInstance().applyDatacenterAddress(dc, ip, port);
                } else if ("MESSAGE_ANNOUNCEMENT".equals(key)) {
                    Object obj = bundle.get("google.sent_time");
                    long time;
                    try {
                        if (obj instanceof String) {
                            time = Utilities.parseLong((String) obj);
                        } else if (obj instanceof Long) {
                            time = (Long) obj;
                        } else {
                            time = System.currentTimeMillis();
                        }
                    } catch (Exception ignore) {
                        time = System.currentTimeMillis();
                    }

                    TLRPC.TL_updateServiceNotification update = new TLRPC.TL_updateServiceNotification();
                    update.popup = false;
                    update.flags = 2;
                    update.inbox_date = (int) (time / 1000);
                    update.message = bundle.getString("message");
                    update.type = "announcement";
                    update.media = new TLRPC.TL_messageMediaEmpty();
                    final TLRPC.TL_updates updates = new TLRPC.TL_updates();
                    updates.updates.add(update);
                    Utilities.stageQueue.postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            MessagesController.getInstance().processUpdates(updates, false);
                        }
                    });
                } else if (Build.VERSION.SDK_INT >= 24 && ApplicationLoader.mainInterfacePaused
                        && UserConfig.isClientActivated()) {
                    Object value = bundle.get("badge");
                    if (value == null) {
                        Object obj = bundle.get("google.sent_time");
                        long time;
                        if (obj instanceof String) {
                            time = Utilities.parseLong((String) obj);
                        } else if (obj instanceof Long) {
                            time = (Long) obj;
                        } else {
                            time = -1;
                        }
                        if (time == -1 || UserConfig.lastAppPauseTime < time) {
                            ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext
                                    .getSystemService(Context.CONNECTIVITY_SERVICE);
                            NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
                            if (connectivityManager
                                    .getRestrictBackgroundStatus() == RESTRICT_BACKGROUND_STATUS_ENABLED
                                    && netInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                                NotificationsController.getInstance().showSingleBackgroundNotification();
                            }
                        }
                    }
                }
            } catch (Exception e) {
                FileLog.e(e);
            }
            ConnectionsManager.onInternalPushReceived();
            ConnectionsManager.getInstance().resumeNetworkMaybe();
        }
    });
}

From source file:com.irccloud.android.IRCCloudApplicationBase.java

public void onPause(final Activity context) {
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (notifierSockerTimerTask != null)
        notifierSockerTimerTask.cancel();
    notifierSockerTimerTask = new TimerTask() {
        @Override/*from w  ww  . j a  v a  2 s. c  om*/
        public void run() {
            notifierSockerTimerTask = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
                for (ActivityManager.RunningAppProcessInfo info : am.getRunningAppProcesses()) {
                    if (info.processName.equals(context.getPackageName())
                            && info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
                        return;
                }
            }
            if (!conn.notifier && conn.getState() == NetworkConnection.STATE_CONNECTED) {
                conn.disconnect();
                if (ServersList.getInstance().count() < 1) {
                    Crashlytics.log(Log.DEBUG, "IRCCloud",
                            "No servers configured, not connecting notifier socket");
                    return;
                }
                if (!(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cm.isActiveNetworkMetered() && cm
                        .getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED)) {
                    try {
                        Thread.sleep(1000);
                        conn.notifier = true;
                        conn.connect();
                    } catch (Exception e) {
                    }
                }
            }
        }
    };

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && cm.isActiveNetworkMetered()
            && cm.getRestrictBackgroundStatus() == ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED) {
        notifierTimer.schedule(notifierSockerTimerTask, 5000);
    } else {
        notifierTimer.schedule(notifierSockerTimerTask, 300000);
    }
}