ConnectivityUtils.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2016 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.owncloud.android.utils;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.content.IntentFilter;
  23. import android.net.ConnectivityManager;
  24. import android.net.NetworkInfo;
  25. import android.os.BatteryManager;
  26. import com.owncloud.android.lib.common.utils.Log_OC;
  27. public class ConnectivityUtils {
  28. private final static String TAG = ConnectivityUtils.class.getName();
  29. public static boolean isAppConnectedViaWiFi(Context context) {
  30. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  31. boolean result =
  32. cm != null && cm.getActiveNetworkInfo() != null
  33. && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
  34. && cm.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED;
  35. Log_OC.d(TAG, "is AppConnectedViaWifi returns " + result);
  36. return result;
  37. }
  38. public static boolean isAppConnected(Context context) {
  39. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  40. return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
  41. }
  42. public static boolean isCharging(Context context){
  43. IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  44. Intent batteryStatus = context.registerReceiver(null, ifilter);
  45. int status = 0;
  46. if (batteryStatus != null) {
  47. status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  48. }
  49. return status == BatteryManager.BATTERY_STATUS_CHARGING ||
  50. status == BatteryManager.BATTERY_STATUS_FULL;
  51. }
  52. }