summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/receiver/PowerConnectionReceiver.java
blob: 641355d5cc78f3dad2bc11222361ceeabb19b89e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package de.danoeh.antennapod.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import de.danoeh.antennapod.core.ClientConfigurator;
import de.danoeh.antennapod.net.download.serviceinterface.AutoDownloadManager;
import de.danoeh.antennapod.storage.preferences.UserPreferences;
import de.danoeh.antennapod.net.download.serviceinterface.DownloadServiceInterface;

// modified from http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
// and ConnectivityActionReceiver.java
// Updated based on http://stackoverflow.com/questions/20833241/android-charge-intent-has-no-extra-data
// Since the intent doesn't have the EXTRA_STATUS like the android.com article says it does
// (though it used to)
public class PowerConnectionReceiver extends BroadcastReceiver {
    private static final String TAG = "PowerConnectionReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        Log.d(TAG, "charging intent: " + action);

        ClientConfigurator.initialize(context);
        if (Intent.ACTION_POWER_CONNECTED.equals(action)) {
            Log.d(TAG, "charging, starting auto-download");
            // we're plugged in, this is a great time to auto-download if everything else is
            // right. So, even if the user allows auto-dl on battery, let's still start
            // downloading now. They shouldn't mind.
            // autodownloadUndownloadedItems will make sure we're on the right wifi networks,
            // etc... so we don't have to worry about it.
            AutoDownloadManager.getInstance().autodownloadUndownloadedItems(context);
        } else {
            // if we're not supposed to be auto-downloading when we're not charging, stop it
            if (!UserPreferences.isEnableAutodownloadOnBattery()) {
                Log.d(TAG, "not charging anymore, canceling auto-download");
                DownloadServiceInterface.get().cancelAll(context);
            } else {
                Log.d(TAG, "not charging anymore, but the user allows auto-download " +
                           "when on battery so we'll keep going");
            }
        }

    }
}