Merge pull request #1696 from mfietz/issue/1695-npe-context-null

Ensure initialization
This commit is contained in:
Martin Fietz 2016-03-16 20:56:51 +01:00
commit 0d114d172b
9 changed files with 64 additions and 43 deletions

View File

@ -8,11 +8,8 @@ import com.joanzapata.iconify.Iconify;
import com.joanzapata.iconify.fonts.FontAwesomeModule;
import com.joanzapata.iconify.fonts.MaterialModule;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.spa.SPAUtil;
/** Main application class. */
@ -56,11 +53,8 @@ public class PodcastApp extends Application {
singleton = this;
PodDBAdapter.init(this);
UserPreferences.init(this);
UpdateManager.init(this);
PlaybackPreferences.init(this);
NetworkUtils.init(this);
ClientConfig.initialize(this);
EventDistributor.getInstance();
Iconify.with(new FontAwesomeModule());
Iconify.with(new MaterialModule());

View File

@ -8,6 +8,7 @@ import android.net.NetworkInfo;
import android.text.TextUtils;
import android.util.Log;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.NetworkUtils;
@ -20,7 +21,8 @@ public class ConnectivityActionReceiver extends BroadcastReceiver {
if (TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) {
Log.d(TAG, "Received intent");
if (NetworkUtils.autodownloadNetworkAvailable()) {
ClientConfig.initialize(context);
if (NetworkUtils.autodownloadNetworkAvailable()) {
Log.d(TAG, "auto-dl network available, starting auto-download");
DBTasks.autodownloadUndownloadedItems(context);
} else { // if new network is Wi-Fi, finish ongoing downloads,

View File

@ -3,10 +3,9 @@ package de.danoeh.antennapod.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DownloadRequester;
@ -25,6 +24,7 @@ public class PowerConnectionReceiver extends BroadcastReceiver {
Log.d(TAG, "charging intent: " + action);
ClientConfig.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

View File

@ -9,8 +9,8 @@ import android.widget.Toast;
import java.util.Arrays;
import de.danoeh.antennapod.BuildConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.feed.Feed;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
import de.danoeh.antennapod.core.storage.DownloadRequester;
@ -27,29 +27,30 @@ public class SPAReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
if (TextUtils.equals(intent.getAction(), ACTION_SP_APPS_QUERY_FEEDS_REPSONSE)) {
if (BuildConfig.DEBUG) Log.d(TAG, "Received SP_APPS_QUERY_RESPONSE");
if (intent.hasExtra(ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA)) {
String[] feedUrls = intent.getStringArrayExtra(ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA);
if (feedUrls != null) {
if (BuildConfig.DEBUG) Log.d(TAG, "Received feeds list: " + Arrays.toString(feedUrls));
for (String url : feedUrls) {
Feed f = new Feed(url, null);
try {
DownloadRequester.getInstance().downloadFeed(context, f);
} catch (DownloadRequestException e) {
Log.e(TAG, "Error while trying to add feed " + url);
e.printStackTrace();
}
}
Toast.makeText(context, R.string.sp_apps_importing_feeds_msg, Toast.LENGTH_LONG).show();
} else {
Log.e(TAG, "Received invalid SP_APPS_QUERY_REPSONSE: extra was null");
}
} else {
Log.e(TAG, "Received invalid SP_APPS_QUERY_RESPONSE: Contains no extra");
if (!TextUtils.equals(intent.getAction(), ACTION_SP_APPS_QUERY_FEEDS_REPSONSE)) {
return;
}
Log.d(TAG, "Received SP_APPS_QUERY_RESPONSE");
if (!intent.hasExtra(ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA)) {
Log.e(TAG, "Received invalid SP_APPS_QUERY_RESPONSE: Contains no extra");
return;
}
String[] feedUrls = intent.getStringArrayExtra(ACTION_SP_APPS_QUERY_FEEDS_REPSONSE_FEEDS_EXTRA);
if (feedUrls == null) {
Log.e(TAG, "Received invalid SP_APPS_QUERY_REPSONSE: extra was null");
return;
}
Log.d(TAG, "Received feeds list: " + Arrays.toString(feedUrls));
ClientConfig.initialize(context);
for (String url : feedUrls) {
Feed f = new Feed(url, null);
try {
DownloadRequester.getInstance().downloadFeed(context, f);
} catch (DownloadRequestException e) {
Log.e(TAG, "Error while trying to add feed " + url);
e.printStackTrace();
}
}
Toast.makeText(context, R.string.sp_apps_importing_feeds_msg, Toast.LENGTH_LONG).show();
}
}

View File

@ -1,5 +1,12 @@
package de.danoeh.antennapod.core;
import android.content.Context;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.PodDBAdapter;
import de.danoeh.antennapod.core.util.NetworkUtils;
/**
* Stores callbacks for core classes like Services, DB classes etc. and other configuration variables.
* Apps using the core module of AntennaPod should register implementations of all interfaces here.
@ -22,4 +29,19 @@ public class ClientConfig {
public static FlattrCallbacks flattrCallbacks;
public static DBTasksCallbacks dbTasksCallbacks;
private static boolean initialized = false;
public static synchronized void initialize(Context context) {
if(initialized) {
return;
}
PodDBAdapter.init(context);
UserPreferences.init(context);
UpdateManager.init(context);
PlaybackPreferences.init(context);
NetworkUtils.init(context);
initialized = true;
}
}

View File

@ -1,4 +1,4 @@
package de.danoeh.antennapod;
package de.danoeh.antennapod.core;
import android.content.Context;

View File

@ -6,7 +6,7 @@ import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
/** Listens for events that make it necessary to reset the update alarm. */
@ -22,8 +22,7 @@ public class AlarmUpdateReceiver extends BroadcastReceiver {
} else if (TextUtils.equals(intent.getAction(), Intent.ACTION_PACKAGE_REPLACED)) {
Log.d(TAG, "Resetting update alarm after app upgrade");
}
PlaybackPreferences.init(context);
UserPreferences.init(context);
ClientConfig.initialize(context);
UserPreferences.restartUpdateAlarm(false);
}

View File

@ -5,6 +5,7 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.util.NetworkUtils;
@ -19,6 +20,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received intent");
ClientConfig.initialize(context);
if (NetworkUtils.isDownloadAllowed()) {
DBTasks.refreshAllFeeds(context, null);
} else {

View File

@ -5,7 +5,8 @@ import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.ClientConfig;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
/** Receives media button events. */
@ -17,10 +18,10 @@ public class MediaButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (BuildConfig.DEBUG) Log.d(TAG, "Received intent");
KeyEvent event = (KeyEvent) intent.getExtras().get(
Intent.EXTRA_KEY_EVENT);
Log.d(TAG, "Received intent");
KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
if (event.getAction() == KeyEvent.ACTION_DOWN) {
ClientConfig.initialize(context);
Intent serviceIntent = new Intent(context, PlaybackService.class);
int keycode = event.getKeyCode();
serviceIntent.putExtra(EXTRA_KEYCODE, keycode);