ConnectivityActionReceiver.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author LukeOwncloud
  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. */
  20. package com.owncloud.android.files.services;
  21. import android.content.BroadcastReceiver;
  22. import android.content.ComponentName;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.content.pm.PackageManager;
  26. import android.net.NetworkInfo;
  27. import android.net.wifi.WifiInfo;
  28. import android.net.wifi.WifiManager;
  29. import android.os.Bundle;
  30. import com.owncloud.android.db.PreferenceManager;
  31. import com.owncloud.android.db.UploadResult;
  32. import com.owncloud.android.lib.common.utils.Log_OC;
  33. /**
  34. * Receives all connectivity action from Android OS at all times and performs
  35. * required OC actions. For now that are: - Signal connectivity to
  36. * {@link FileUploader}.
  37. *
  38. * Later can be added: - Signal connectivity to download service, deletion
  39. * service, ... - Handle offline mode (cf.
  40. * https://github.com/owncloud/android/issues/162)
  41. *
  42. * Have fun with the comments :S
  43. */
  44. public class ConnectivityActionReceiver extends BroadcastReceiver {
  45. private static final String TAG = ConnectivityActionReceiver.class.getSimpleName();
  46. /**
  47. * Magic keyword, by Google.
  48. *
  49. * {@See http://developer.android.com/intl/es/reference/android/net/wifi/WifiInfo.html#getSSID()}
  50. */
  51. private static final String UNKNOWN_SSID = "<unknown ssid>";
  52. @Override
  53. public void onReceive(final Context context, Intent intent) {
  54. // LOG ALL EVENTS:
  55. Log_OC.v(TAG, "action: " + intent.getAction());
  56. Log_OC.v(TAG, "component: " + intent.getComponent());
  57. Bundle extras = intent.getExtras();
  58. if (extras != null) {
  59. for (String key : extras.keySet()) {
  60. Log_OC.v(TAG, "key [" + key + "]: " + extras.get(key));
  61. }
  62. } else {
  63. Log_OC.v(TAG, "no extras");
  64. }
  65. if (intent.getAction().equals(Intent.ACTION_POWER_CONNECTED) &&
  66. (PreferenceManager.instantPictureUploadEnabled(context) &&
  67. PreferenceManager.instantPictureUploadWhenChargingOnly(context)) ||
  68. (PreferenceManager.instantVideoUploadEnabled(context) &&
  69. PreferenceManager.instantVideoUploadWhenChargingOnly(context))
  70. ) {
  71. // for the moment, only recovery of instant uploads, similar to behaviour in release 1.9.1
  72. Log_OC.d(TAG, "Requesting retry of instant uploads (& friends) due to charging");
  73. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  74. requester.retryFailedUploads(
  75. context,
  76. null,
  77. UploadResult.DELAYED_FOR_CHARGING // for the rest of enqueued when Wifi fell
  78. );
  79. }
  80. /**
  81. * There is an interesting mess to process WifiManager.NETWORK_STATE_CHANGED_ACTION and
  82. * ConnectivityManager.CONNECTIVITY_ACTION in a simple and reliable way.
  83. *
  84. * The former triggers much more events than what we really need to know about Wifi connection.
  85. *
  86. * But there are annoying uncertainties about ConnectivityManager.CONNECTIVITY_ACTION due
  87. * to the deprecation of ConnectivityManager.EXTRA_NETWORK_INFO in API level 14, and the absence
  88. * of ConnectivityManager.EXTRA_NETWORK_TYPE until API level 17. Dear Google, how should we
  89. * handle API levels 14 to 16?
  90. *
  91. * In the end maybe we need to keep in memory the current knowledge about connectivity
  92. * and update it taking into account several Intents received in a row
  93. *
  94. * But first let's try something "simple" to keep a basic retry of instant uploads in
  95. * version 1.9.2, similar to the existent until 1.9.1. To be improved.
  96. */
  97. if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
  98. NetworkInfo networkInfo =
  99. intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  100. WifiInfo wifiInfo =
  101. intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
  102. String bssid =
  103. intent.getStringExtra(WifiManager.EXTRA_BSSID);
  104. if(networkInfo.isConnected() && // not enough; see (*) right below
  105. wifiInfo != null &&
  106. !UNKNOWN_SSID.equals(wifiInfo.getSSID().toLowerCase()) &&
  107. bssid != null
  108. ) {
  109. Log_OC.d(TAG, "WiFi connected");
  110. wifiConnected(context);
  111. } else {
  112. // TODO tons of things to check to conclude disconnection;
  113. // TODO maybe alternative commented below, based on CONNECTIVITY_ACTION is better
  114. Log_OC.d(TAG, "WiFi disconnected ... but don't know if right now");
  115. }
  116. }
  117. // (*) When WiFi is lost, an Intent with network state CONNECTED and SSID "<unknown ssid>" is
  118. // received right before another Intent with network state DISCONNECTED; needs to
  119. // be differentiated of a new Wifi connection.
  120. //
  121. // Besides, with a new connection two Intents are received, having only the second the extra
  122. // WifiManager.EXTRA_BSSID, with the BSSID of the access point accessed.
  123. //
  124. // Not sure if this protocol is exact, since it's not documented. Only found mild references in
  125. // - http://developer.android.com/intl/es/reference/android/net/wifi/WifiInfo.html#getSSID()
  126. // - http://developer.android.com/intl/es/reference/android/net/wifi/WifiManager.html#EXTRA_BSSID
  127. // and reproduced in Nexus 5 with Android 6.
  128. /**
  129. * Possible alternative attending ConnectivityManager.CONNECTIVITY_ACTION.
  130. *
  131. * Let's see what QA has to say
  132. *
  133. if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
  134. NetworkInfo networkInfo = intent.getParcelableExtra(
  135. ConnectivityManager.EXTRA_NETWORK_INFO // deprecated in API 14
  136. );
  137. int networkType = intent.getIntExtra(
  138. ConnectivityManager.EXTRA_NETWORK_TYPE, // only from API level 17
  139. -1
  140. );
  141. boolean couldBeWifiAction =
  142. (networkInfo == null && networkType < 0) || // cases of lack of info
  143. networkInfo.getType() == ConnectivityManager.TYPE_WIFI ||
  144. networkType == ConnectivityManager.TYPE_WIFI;
  145. if (couldBeWifiAction) {
  146. if (ConnectivityUtils.isAppConnectedViaUnmeteredWiFi(context)) {
  147. Log_OC.d(TAG, "WiFi connected");
  148. wifiConnected(context);
  149. } else {
  150. Log_OC.d(TAG, "WiFi disconnected");
  151. wifiDisconnected(context);
  152. }
  153. } /* else, CONNECTIVIY_ACTION is (probably) about other network interface (mobile, bluetooth, ...)
  154. }
  155. */
  156. }
  157. private void wifiConnected(Context context) {
  158. // for the moment, only recovery of instant uploads, similar to behaviour in release 1.9.1
  159. if (
  160. (PreferenceManager.instantPictureUploadEnabled(context) &&
  161. PreferenceManager.instantPictureUploadViaWiFiOnly(context)) ||
  162. (PreferenceManager.instantVideoUploadEnabled(context) &&
  163. PreferenceManager.instantVideoUploadViaWiFiOnly(context))
  164. ) {
  165. Log_OC.d(TAG, "Requesting retry of instant uploads (& friends)");
  166. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  167. requester.retryFailedUploads(
  168. context,
  169. null,
  170. UploadResult.NETWORK_CONNECTION // for the interrupted when Wifi fell, if any
  171. // (side effect: any upload failed due to network error will be retried too, instant or not)
  172. );
  173. requester.retryFailedUploads(
  174. context,
  175. null,
  176. UploadResult.DELAYED_FOR_WIFI // for the rest of enqueued when Wifi fell
  177. );
  178. }
  179. }
  180. private void wifiDisconnected(Context context) {
  181. // TODO something smart
  182. // NOTE: explicit cancellation of only-wifi instant uploads is not needed anymore, since currently:
  183. // - any upload in progress will be interrupted due to the lack of connectivity while the device
  184. // reconnects through other network interface;
  185. // - FileUploader checks instant upload settings and connection state before executing each
  186. // upload operation, so other pending instant uploads after the current one will not be run
  187. // (currently are silently moved to FAILED state)
  188. }
  189. static public void enableActionReceiver(Context context) {
  190. PackageManager pm = context.getPackageManager();
  191. ComponentName compName = new ComponentName(context.getApplicationContext(), ConnectivityActionReceiver.class);
  192. pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  193. PackageManager.DONT_KILL_APP);
  194. }
  195. static public void disableActionReceiver(Context context) {
  196. PackageManager pm = context.getPackageManager();
  197. ComponentName compName = new ComponentName(context.getApplicationContext(), ConnectivityActionReceiver.class);
  198. pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  199. PackageManager.DONT_KILL_APP);
  200. }
  201. }