Added Connectivity change receiver

This commit is contained in:
daniel oeh 2013-03-09 21:08:24 +01:00
parent 485f8905c4
commit 6eb08f6693
2 changed files with 54 additions and 0 deletions

View File

@ -348,6 +348,11 @@
android:configChanges="orientation"
android:label="@string/organize_queue_label" >
</activity>
<receiver android:name=".receiver.ConnectivityActionReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
</application>
</manifest>

View File

@ -0,0 +1,49 @@
package de.danoeh.antennapod.receiver;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.storage.DownloadRequester;
import de.danoeh.antennapod.util.NetworkUtils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
public class ConnectivityActionReceiver extends BroadcastReceiver {
private static final String TAG = "ConnectivityActionReceiver";
@Override
public void onReceive(final Context context, Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
if (AppConfig.DEBUG)
Log.d(TAG, "Received intent");
if (NetworkUtils.autodownloadNetworkAvailable(context)) {
if (AppConfig.DEBUG)
Log.d(TAG,
"auto-dl network available, starting auto-download");
new Thread() {
@Override
public void run() {
FeedManager.getInstance()
.autodownloadUndownloadedItems(context);
}
}.start();
} else { // if new network is Wi-Fi, finish ongoing downloads,
// otherwise cancel all downloads
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null || ni.getType() != ConnectivityManager.TYPE_WIFI) {
if (AppConfig.DEBUG)
Log.i(TAG,
"Device is no longer connected to Wi-Fi. Cancelling ongoing downloads");
DownloadRequester.getInstance().cancelAllDownloads(context);
}
}
}
}
}