|
@@ -1,64 +1,102 @@
|
|
|
-/**
|
|
|
- * 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
|
|
|
- * 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,
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
* 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/>.
|
|
|
+ *
|
|
|
+ * Inspired by: https://stackoverflow.com/questions/6493517/detect-if-android-device-has-internet-connection
|
|
|
*/
|
|
|
|
|
|
package com.owncloud.android.utils;
|
|
|
|
|
|
+import android.accounts.Account;
|
|
|
+import android.accounts.AuthenticatorException;
|
|
|
+import android.accounts.OperationCanceledException;
|
|
|
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 com.evernote.android.job.JobRequest;
|
|
|
+import com.evernote.android.job.util.Device;
|
|
|
+import com.owncloud.android.authentication.AccountUtils;
|
|
|
+import com.owncloud.android.lib.common.OwnCloudAccount;
|
|
|
+import com.owncloud.android.lib.common.OwnCloudClient;
|
|
|
+import com.owncloud.android.lib.common.OwnCloudClientFactory;
|
|
|
import com.owncloud.android.lib.common.utils.Log_OC;
|
|
|
+import com.owncloud.android.lib.resources.status.OwnCloudVersion;
|
|
|
+
|
|
|
+import org.apache.commons.httpclient.methods.GetMethod;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
|
|
|
public class ConnectivityUtils {
|
|
|
|
|
|
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;
|
|
|
- }
|
|
|
+ public static boolean isInternetWalled(Context context) {
|
|
|
+ if (!Device.getNetworkType(context).equals(JobRequest.NetworkType.ANY)) {
|
|
|
+ try {
|
|
|
+ Account account = AccountUtils.getCurrentOwnCloudAccount(context);
|
|
|
+ if (account != null) {
|
|
|
+ OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
|
|
|
+ OwnCloudVersion serverVersion = AccountUtils.getServerVersion(account);
|
|
|
|
|
|
- public static boolean isAppConnected(Context context) {
|
|
|
- ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
- return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
|
|
|
- }
|
|
|
+ String url;
|
|
|
+ if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
|
|
|
+ url = ocAccount.getBaseUri() + "/index.php/204";
|
|
|
+ } else {
|
|
|
+ url = ocAccount.getBaseUri() + "/status.php";
|
|
|
+ }
|
|
|
|
|
|
- public static boolean isCharging(Context context) {
|
|
|
- IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
|
|
- Intent batteryStatus = context.registerReceiver(null, ifilter);
|
|
|
+ GetMethod get = new GetMethod(url);
|
|
|
+ OwnCloudClient client = OwnCloudClientFactory.createOwnCloudClient(account, context);
|
|
|
|
|
|
- int status = 0;
|
|
|
- if (batteryStatus != null) {
|
|
|
- status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
|
|
|
+ int status = client.executeMethod(get);
|
|
|
+
|
|
|
+ if (serverVersion.compareTo(OwnCloudVersion.nextcloud_13) > 0) {
|
|
|
+ return !(status == 204 && get.getResponseContentLength() == -1);
|
|
|
+ } else {
|
|
|
+ if (status == 200) {
|
|
|
+ try {
|
|
|
+ // try parsing json to verify response
|
|
|
+ // check if json contains maintenance and it should be false
|
|
|
+
|
|
|
+ String json = get.getResponseBodyAsString();
|
|
|
+ return new JSONObject(json).getBoolean("maintenance");
|
|
|
+ } catch (JSONException e) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ Log_OC.e(TAG, "Error checking internet connection", e);
|
|
|
+ } catch (com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException e) {
|
|
|
+ Log_OC.e(TAG, "Account not found", e);
|
|
|
+ } catch (OperationCanceledException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (AuthenticatorException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
}
|
|
|
- return status == BatteryManager.BATTERY_STATUS_CHARGING ||
|
|
|
- status == BatteryManager.BATTERY_STATUS_FULL;
|
|
|
- }
|
|
|
|
|
|
+ return true;
|
|
|
+
|
|
|
+ }
|
|
|
}
|