|
@@ -1,64 +1,62 @@
|
|
-/**
|
|
|
|
- * ownCloud Android client application
|
|
|
|
|
|
+/*
|
|
|
|
+ * Nextcloud Android client application
|
|
*
|
|
*
|
|
- * @author David A. Velasco
|
|
|
|
- * Copyright (C) 2016 ownCloud Inc.
|
|
|
|
|
|
+ * @author Mario Danic
|
|
|
|
+ * Copyright (C) 2017 Mario Danic
|
|
|
|
+ * Copyright (C) 2017 Nextcloud GmbH.
|
|
*
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* This program is free software: you can redistribute it and/or modify
|
|
- * it under the terms of the GNU General Public License version 2,
|
|
|
|
- * as published by the Free Software Foundation.
|
|
|
|
|
|
+ * it under the terms of the GNU Affero General Public License as published by
|
|
|
|
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
|
+ * at your option) any later version.
|
|
*
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
- * GNU General Public License for more details.
|
|
|
|
|
|
+ * GNU Affero General Public License for more details.
|
|
*
|
|
*
|
|
- * You should have received a copy of the GNU General Public License
|
|
|
|
|
|
+ * You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
+ *
|
|
|
|
+ * Inspired by: https://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection
|
|
*/
|
|
*/
|
|
|
|
|
|
package com.owncloud.android.utils;
|
|
package com.owncloud.android.utils;
|
|
|
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
-import android.content.Intent;
|
|
|
|
-import android.content.IntentFilter;
|
|
|
|
-import android.net.ConnectivityManager;
|
|
|
|
-import android.net.NetworkInfo;
|
|
|
|
-import android.os.BatteryManager;
|
|
|
|
-import android.support.v4.net.ConnectivityManagerCompat;
|
|
|
|
|
|
+import android.util.Log;
|
|
|
|
|
|
-import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
|
|
|
+import com.evernote.android.job.JobRequest;
|
|
|
|
+import com.evernote.android.job.util.Device;
|
|
|
|
|
|
-public class ConnectivityUtils {
|
|
|
|
|
|
+import org.apache.commons.httpclient.util.HttpURLConnection;
|
|
|
|
|
|
- private final static String TAG = ConnectivityUtils.class.getName();
|
|
|
|
-
|
|
|
|
- public static boolean isAppConnectedViaUnmeteredWiFi(Context context) {
|
|
|
|
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
- boolean result =
|
|
|
|
- cm != null && cm.getActiveNetworkInfo() != null
|
|
|
|
- && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI
|
|
|
|
- && cm.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED
|
|
|
|
- && !ConnectivityManagerCompat.isActiveNetworkMetered(cm);
|
|
|
|
- Log_OC.d(TAG, "is AppConnectedViaWifi returns " + result);
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.net.URL;
|
|
|
|
|
|
- public static boolean isAppConnected(Context context) {
|
|
|
|
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
- return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
|
|
|
|
- }
|
|
|
|
|
|
+public class ConnectivityUtils {
|
|
|
|
|
|
- public static boolean isCharging(Context context) {
|
|
|
|
- IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
|
|
|
- Intent batteryStatus = context.registerReceiver(null, ifilter);
|
|
|
|
|
|
+ private final static String TAG = ConnectivityUtils.class.getName();
|
|
|
|
|
|
- int status = 0;
|
|
|
|
- if (batteryStatus != null) {
|
|
|
|
- status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
|
|
|
|
|
+ public static boolean isInternetWalled(Context context) {
|
|
|
|
+ if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
|
|
|
|
+ try {
|
|
|
|
+ HttpURLConnection urlc = (HttpURLConnection)
|
|
|
|
+ (new URL("http://clients3.google.com/generate_204")
|
|
|
|
+ .openConnection());
|
|
|
|
+ urlc.setRequestProperty("User-Agent", "Android");
|
|
|
|
+ urlc.setRequestProperty("Connection", "close");
|
|
|
|
+ urlc.setConnectTimeout(1500);
|
|
|
|
+ urlc.connect();
|
|
|
|
+ return !(urlc.getResponseCode() == 204 &&
|
|
|
|
+ urlc.getContentLength() == 0);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ Log.e(TAG, "Error checking internet connection", e);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ Log.d(TAG, "No network available!");
|
|
}
|
|
}
|
|
- return status == BatteryManager.BATTERY_STATUS_CHARGING ||
|
|
|
|
- status == BatteryManager.BATTERY_STATUS_FULL;
|
|
|
|
- }
|
|
|
|
|
|
+ return true;
|
|
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|