AntennaPod/src/de/danoeh/antennapod/PodcastApp.java

103 lines
3.4 KiB
Java
Raw Normal View History

2012-07-13 12:23:47 +02:00
package de.danoeh.antennapod;
2011-12-23 19:22:06 +01:00
import java.io.File;
import java.util.concurrent.TimeUnit;
import android.app.AlarmManager;
2011-12-23 19:22:06 +01:00
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
2012-07-06 15:55:20 +02:00
import android.util.Log;
import de.danoeh.antennapod.activity.OpmlImportActivity;
2012-07-13 12:23:47 +02:00
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.receiver.FeedUpdateReceiver;
public class PodcastApp extends Application implements
SharedPreferences.OnSharedPreferenceChangeListener {
2011-12-23 19:22:06 +01:00
2012-07-07 23:37:52 +02:00
private static final String TAG = "PodcastApp";
2012-07-13 12:23:47 +02:00
public static final String PREF_NAME = "AntennapodPrefs";
2012-07-07 23:37:52 +02:00
public static final String PREF_PAUSE_ON_HEADSET_DISCONNECT = "prefPauseOnHeadsetDisconnect";
public static final String PREF_FOLLOW_QUEUE = "prefFollowQueue";
public static final String PREF_DOWNLOAD_MEDIA_ON_WIFI_ONLY = "prefDownloadMediaOnWifiOnly";
public static final String PREF_UPDATE_INTERVALL = "prefAutoUpdateIntervall";
public static final String PREF_MOBILE_UPDATE = "prefMobileUpdate";
private static float LOGICAL_DENSITY;
2012-07-07 23:37:52 +02:00
private static PodcastApp singleton;
2011-12-23 19:22:06 +01:00
public static PodcastApp getInstance() {
return singleton;
}
@Override
public void onCreate() {
super.onCreate();
singleton = this;
LOGICAL_DENSITY = getResources().getDisplayMetrics().density;
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
createImportDirectory();
prefs.registerOnSharedPreferenceChangeListener(this);
FeedManager manager = FeedManager.getInstance();
manager.loadDBData(getApplicationContext());
2011-12-23 19:22:06 +01:00
}
/** Creates the import directory if it doesn't exist and if storage is available */
private void createImportDirectory() {
File importDir = getExternalFilesDir(OpmlImportActivity.IMPORT_DIR);
if (importDir != null) {
if (importDir.exists()) {
if (AppConfig.DEBUG) Log.d(TAG, "Import directory already exists");
} else {
if (AppConfig.DEBUG) Log.d(TAG, "Creating import directory");
importDir.mkdir();
}
} else {
if (AppConfig.DEBUG) Log.d(TAG, "Could not access external storage.");
}
}
2012-07-06 15:55:20 +02:00
@Override
public void onLowMemory() {
super.onLowMemory();
Log.w(TAG, "Received onLowOnMemory warning. Cleaning image cache...");
FeedImageLoader.getInstance().wipeImageCache();
}
2011-12-23 19:22:06 +01:00
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (AppConfig.DEBUG)
Log.d(TAG, "Registered change of application preferences");
if (key.equals(PREF_UPDATE_INTERVALL)) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int hours = Integer.parseInt(sharedPreferences.getString(
PREF_UPDATE_INTERVALL, "0"));
PendingIntent updateIntent = PendingIntent.getBroadcast(this, 0,
new Intent(FeedUpdateReceiver.ACTION_REFRESH_FEEDS), 0);
alarmManager.cancel(updateIntent);
if (hours != 0) {
long newIntervall = TimeUnit.HOURS.toMillis(hours);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
newIntervall, newIntervall, updateIntent);
if (AppConfig.DEBUG)
Log.d(TAG, "Changed alarm to new intervall");
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "Automatic update was deactivated");
}
}
}
public static float getLogicalDensity() {
return LOGICAL_DENSITY;
}
2011-12-23 19:22:06 +01:00
}