ConnectivityActionReceiver.java 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  66. * There is an interesting mess to process WifiManager.NETWORK_STATE_CHANGED_ACTION and
  67. * ConnectivityManager.CONNECTIVITY_ACTION in a simple and reliable way.
  68. *
  69. * The former triggers much more events than what we really need to know about Wifi connection.
  70. *
  71. * But there are annoying uncertainties about ConnectivityManager.CONNECTIVITY_ACTION due
  72. * to the deprecation of ConnectivityManager.EXTRA_NETWORK_INFO in API level 14, and the absence
  73. * of ConnectivityManager.EXTRA_NETWORK_TYPE until API level 17. Dear Google, how should we
  74. * handle API levels 14 to 16?
  75. *
  76. * In the end maybe we need to keep in memory the current knowledge about connectivity
  77. * and update it taking into account several Intents received in a row
  78. *
  79. * But first let's try something "simple" to keep a basic retry of instant uploads in
  80. * version 1.9.2, similar to the existent until 1.9.1. To be improved.
  81. */
  82. if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
  83. NetworkInfo networkInfo =
  84. intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
  85. WifiInfo wifiInfo =
  86. intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
  87. String bssid =
  88. intent.getStringExtra(WifiManager.EXTRA_BSSID);
  89. if(networkInfo.isConnected() && // not enough; see (*) right below
  90. wifiInfo != null &&
  91. !UNKNOWN_SSID.equals(wifiInfo.getSSID().toLowerCase()) &&
  92. bssid != null
  93. ) {
  94. Log_OC.d(TAG, "WiFi connected");
  95. wifiConnected(context);
  96. } else {
  97. // TODO tons of things to check to conclude disconnection;
  98. // TODO maybe alternative commented below, based on CONNECTIVITY_ACTION is better
  99. Log_OC.d(TAG, "WiFi disconnected ... but don't know if right now");
  100. }
  101. }
  102. // (*) When WiFi is lost, an Intent with network state CONNECTED and SSID "<unknown ssid>" is
  103. // received right before another Intent with network state DISCONNECTED; needs to
  104. // be differentiated of a new Wifi connection.
  105. //
  106. // Besides, with a new connection two Intents are received, having only the second the extra
  107. // WifiManager.EXTRA_BSSID, with the BSSID of the access point accessed.
  108. //
  109. // Not sure if this protocol is exact, since it's not documented. Only found mild references in
  110. // - http://developer.android.com/intl/es/reference/android/net/wifi/WifiInfo.html#getSSID()
  111. // - http://developer.android.com/intl/es/reference/android/net/wifi/WifiManager.html#EXTRA_BSSID
  112. // and reproduced in Nexus 5 with Android 6.
  113. /**
  114. * Possible alternative attending ConnectivityManager.CONNECTIVITY_ACTION.
  115. *
  116. * Let's see what QA has to say
  117. *
  118. if(intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
  119. NetworkInfo networkInfo = intent.getParcelableExtra(
  120. ConnectivityManager.EXTRA_NETWORK_INFO // deprecated in API 14
  121. );
  122. int networkType = intent.getIntExtra(
  123. ConnectivityManager.EXTRA_NETWORK_TYPE, // only from API level 17
  124. -1
  125. );
  126. boolean couldBeWifiAction =
  127. (networkInfo == null && networkType < 0) || // cases of lack of info
  128. networkInfo.getType() == ConnectivityManager.TYPE_WIFI ||
  129. networkType == ConnectivityManager.TYPE_WIFI;
  130. if (couldBeWifiAction) {
  131. if (ConnectivityUtils.isAppConnectedViaWiFi(context)) {
  132. Log_OC.d(TAG, "WiFi connected");
  133. wifiConnected(context);
  134. } else {
  135. Log_OC.d(TAG, "WiFi disconnected");
  136. wifiDisconnected(context);
  137. }
  138. } /* else, CONNECTIVIY_ACTION is (probably) about other network interface (mobile, bluetooth, ...)
  139. }
  140. */
  141. }
  142. private void wifiConnected(Context context) {
  143. // for the moment, only recovery of instant uploads, similar to behaviour in release 1.9.1
  144. if (
  145. (PreferenceManager.instantPictureUploadEnabled(context) &&
  146. PreferenceManager.instantPictureUploadViaWiFiOnly(context)) ||
  147. (PreferenceManager.instantVideoUploadEnabled(context) &&
  148. PreferenceManager.instantVideoUploadViaWiFiOnly(context))
  149. ) {
  150. Log_OC.d(TAG, "Requesting retry of instant uploads (& friends)");
  151. FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
  152. requester.retryFailedUploads(
  153. context,
  154. null,
  155. UploadResult.NETWORK_CONNECTION // for the interrupted when Wifi fell, if any
  156. // (side effect: any upload failed due to network error will be retried too, instant or not)
  157. );
  158. requester.retryFailedUploads(
  159. context,
  160. null,
  161. UploadResult.DELAYED_FOR_WIFI // for the rest of enqueued when Wifi fell
  162. );
  163. }
  164. }
  165. private void wifiDisconnected(Context context) {
  166. // TODO something smart
  167. // NOTE: explicit cancellation of only-wifi instant uploads is not needed anymore, since currently:
  168. // - any upload in progress will be interrupted due to the lack of connectivity while the device
  169. // reconnects through other network interface;
  170. // - FileUploader checks instant upload settings and connection state before executing each
  171. // upload operation, so other pending instant uploads after the current one will not be run
  172. // (currently are silently moved to FAILED state)
  173. }
  174. static public void enableActionReceiver(Context context) {
  175. PackageManager pm = context.getPackageManager();
  176. ComponentName compName = new ComponentName(context.getApplicationContext(), ConnectivityActionReceiver.class);
  177. pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
  178. PackageManager.DONT_KILL_APP);
  179. }
  180. static public void disableActionReceiver(Context context) {
  181. PackageManager pm = context.getPackageManager();
  182. ComponentName compName = new ComponentName(context.getApplicationContext(), ConnectivityActionReceiver.class);
  183. pm.setComponentEnabledSetting(compName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
  184. PackageManager.DONT_KILL_APP);
  185. }
  186. }