2012-07-13 12:23:47 +02:00
|
|
|
package de.danoeh.antennapod;
|
2011-12-23 19:22:06 +01:00
|
|
|
|
2012-07-25 12:45:15 +02:00
|
|
|
import java.io.File;
|
2012-08-12 14:21:36 +02:00
|
|
|
import java.io.IOException;
|
2012-07-08 13:40:10 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
import android.app.AlarmManager;
|
2011-12-23 19:22:06 +01:00
|
|
|
import android.app.Application;
|
2012-07-08 13:40:10 +02:00
|
|
|
import android.app.PendingIntent;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
2012-08-11 15:45:30 +02:00
|
|
|
import android.content.res.Configuration;
|
2012-07-08 13:40:10 +02:00
|
|
|
import android.preference.PreferenceManager;
|
2012-07-06 15:55:20 +02:00
|
|
|
import android.util.Log;
|
2012-07-25 12:45:15 +02:00
|
|
|
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;
|
2012-07-08 13:40:10 +02:00
|
|
|
|
2012-08-07 13:56:33 +02:00
|
|
|
/** Main application class. */
|
2012-07-08 13:40:10 +02:00
|
|
|
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-26 19:19:35 +02:00
|
|
|
public static final String EXPORT_DIR = "export/";
|
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";
|
2012-07-09 12:05:09 +02:00
|
|
|
public static final String PREF_MOBILE_UPDATE = "prefMobileUpdate";
|
2012-08-06 10:54:49 +02:00
|
|
|
public static final String PREF_AUTO_QUEUE = "prefAutoQueue";
|
2012-08-10 14:30:54 +02:00
|
|
|
public static final String PREF_DISPLAY_ONLY_EPISODES = "prefDisplayOnlyEpisodes";
|
2012-07-22 18:25:15 +02:00
|
|
|
|
2012-07-21 19:35:07 +02:00
|
|
|
private static float LOGICAL_DENSITY;
|
2012-07-08 13:40:10 +02:00
|
|
|
|
2012-07-07 23:37:52 +02:00
|
|
|
private static PodcastApp singleton;
|
2012-08-19 16:27:37 +02:00
|
|
|
|
|
|
|
private boolean displayOnlyEpisodes;
|
2012-07-07 23:37:52 +02:00
|
|
|
|
2011-12-23 19:22:06 +01:00
|
|
|
public static PodcastApp getInstance() {
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate() {
|
|
|
|
super.onCreate();
|
|
|
|
singleton = this;
|
2012-07-21 19:35:07 +02:00
|
|
|
LOGICAL_DENSITY = getResources().getDisplayMetrics().density;
|
2012-07-08 13:40:10 +02:00
|
|
|
SharedPreferences prefs = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(this);
|
2012-08-19 16:27:37 +02:00
|
|
|
displayOnlyEpisodes = prefs.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false);
|
2012-07-25 12:45:15 +02:00
|
|
|
createImportDirectory();
|
2012-08-12 14:21:36 +02:00
|
|
|
createNoMediaFile();
|
2012-07-08 13:40:10 +02:00
|
|
|
prefs.registerOnSharedPreferenceChangeListener(this);
|
2012-07-22 18:25:15 +02:00
|
|
|
FeedManager manager = FeedManager.getInstance();
|
|
|
|
manager.loadDBData(getApplicationContext());
|
2011-12-23 19:22:06 +01:00
|
|
|
}
|
2012-08-07 13:56:33 +02:00
|
|
|
|
2012-08-12 14:21:36 +02:00
|
|
|
/** Create a .nomedia file to prevent scanning by the media scanner. */
|
|
|
|
private void createNoMediaFile() {
|
|
|
|
File f = new File(getExternalFilesDir(null), ".nomedia");
|
|
|
|
if (!f.exists()) {
|
|
|
|
try {
|
|
|
|
f.createNewFile();
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.e(TAG, "Could not create .nomedia file");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (AppConfig.DEBUG) Log.d(TAG, ".nomedia file created");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-07 13:56:33 +02:00
|
|
|
/**
|
|
|
|
* Creates the import directory if it doesn't exist and if storage is
|
|
|
|
* available
|
|
|
|
*/
|
2012-07-25 12:45:15 +02:00
|
|
|
private void createImportDirectory() {
|
|
|
|
File importDir = getExternalFilesDir(OpmlImportActivity.IMPORT_DIR);
|
|
|
|
if (importDir != null) {
|
|
|
|
if (importDir.exists()) {
|
2012-08-07 13:56:33 +02:00
|
|
|
if (AppConfig.DEBUG)
|
|
|
|
Log.d(TAG, "Import directory already exists");
|
2012-07-25 12:45:15 +02:00
|
|
|
} else {
|
2012-08-07 13:56:33 +02:00
|
|
|
if (AppConfig.DEBUG)
|
|
|
|
Log.d(TAG, "Creating import directory");
|
2012-07-25 12:45:15 +02:00
|
|
|
importDir.mkdir();
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-07 13:56:33 +02:00
|
|
|
if (AppConfig.DEBUG)
|
|
|
|
Log.d(TAG, "Could not access external storage.");
|
2012-07-25 12:45:15 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2012-08-07 13:56:33 +02:00
|
|
|
/**
|
|
|
|
* Listens for changes in the 'update intervall'-preference and changes the
|
|
|
|
* alarm if necessary.
|
|
|
|
*/
|
2012-07-08 13:40:10 +02:00
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
|
|
|
String key) {
|
2012-07-22 18:25:15 +02:00
|
|
|
if (AppConfig.DEBUG)
|
|
|
|
Log.d(TAG, "Registered change of application preferences");
|
2012-07-08 13:40:10 +02:00
|
|
|
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);
|
2012-07-09 19:52:47 +02:00
|
|
|
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
|
|
|
|
newIntervall, newIntervall, updateIntent);
|
2012-07-22 18:25:15 +02:00
|
|
|
if (AppConfig.DEBUG)
|
|
|
|
Log.d(TAG, "Changed alarm to new intervall");
|
2012-07-08 13:40:10 +02:00
|
|
|
} else {
|
2012-07-22 18:25:15 +02:00
|
|
|
if (AppConfig.DEBUG)
|
|
|
|
Log.d(TAG, "Automatic update was deactivated");
|
2012-07-08 13:40:10 +02:00
|
|
|
}
|
2012-08-19 16:27:37 +02:00
|
|
|
} else if (key.equals(PREF_DISPLAY_ONLY_EPISODES)) {
|
|
|
|
if (AppConfig.DEBUG) Log.d(TAG, "PREF_DISPLAY_ONLY_EPISODES changed");
|
|
|
|
displayOnlyEpisodes = sharedPreferences.getBoolean(PREF_DISPLAY_ONLY_EPISODES, false);
|
2012-07-08 13:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-22 18:25:15 +02:00
|
|
|
|
2012-07-21 19:35:07 +02:00
|
|
|
public static float getLogicalDensity() {
|
|
|
|
return LOGICAL_DENSITY;
|
|
|
|
}
|
2012-08-19 16:27:37 +02:00
|
|
|
|
|
|
|
public boolean displayOnlyEpisodes() {
|
|
|
|
return displayOnlyEpisodes;
|
|
|
|
}
|
2012-08-11 15:45:30 +02:00
|
|
|
|
|
|
|
public boolean isLargeScreen() {
|
|
|
|
return (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE || (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE;
|
|
|
|
|
|
|
|
}
|
2011-12-23 19:22:06 +01:00
|
|
|
}
|