summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/PodcastApp.java
blob: 58dbf2a2370d29f5b937709096579b3e9c531592 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package de.danoeh.antennapod;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.util.Log;
import de.danoeh.antennapod.activity.OpmlImportActivity;
import de.danoeh.antennapod.asynctask.FeedImageLoader;
import de.danoeh.antennapod.feed.FeedManager;
import de.danoeh.antennapod.feed.FeedMedia;
import de.danoeh.antennapod.receiver.FeedUpdateReceiver;
import de.danoeh.antennapod.service.PlaybackService;

/** Main application class. */
public class PodcastApp extends Application implements
		SharedPreferences.OnSharedPreferenceChangeListener {

	private static final String TAG = "PodcastApp";
	public static final String EXPORT_DIR = "export/";

	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";
	public static final String PREF_AUTO_QUEUE = "prefAutoQueue";
	public static final String PREF_DISPLAY_ONLY_EPISODES = "prefDisplayOnlyEpisodes";
	public static final String PREF_AUTO_DELETE = "prefAutoDelete";
	public static final String PREF_THEME = "prefTheme";
	public static final String PREF_DATA_FOLDER = "prefDataFolder";

	private static float LOGICAL_DENSITY;

	private static PodcastApp singleton;

	private boolean displayOnlyEpisodes;

	private static long currentlyPlayingMediaId;

	/** Resource id of the currently selected theme. */
	private static int theme;

	public static PodcastApp getInstance() {
		return singleton;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		singleton = this;
		LOGICAL_DENSITY = getResources().getDisplayMetrics().density;
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		displayOnlyEpisodes = prefs.getBoolean(PREF_DISPLAY_ONLY_EPISODES,
				false);
		currentlyPlayingMediaId = prefs.getLong(
				PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA,
				PlaybackService.NO_MEDIA_PLAYING);
		readThemeValue();
		createImportDirectory();
		createNoMediaFile();
		prefs.registerOnSharedPreferenceChangeListener(this);
		FeedManager manager = FeedManager.getInstance();
		manager.loadDBData(getApplicationContext());
	}

	/** 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");
		}
	}

	/**
	 * Creates the import directory if it doesn't exist and if storage is
	 * available
	 */
	private void createImportDirectory() {
		File importDir = getDataFolder(this, 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.");
		}
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		Log.w(TAG, "Received onLowOnMemory warning. Cleaning image cache...");
		FeedImageLoader.getInstance().wipeImageCache();
	}

	/**
	 * Listens for changes in the 'update intervall'-preference and changes the
	 * alarm if necessary.
	 */
	@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");
			}
		} 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);
		} else if (key.equals(PlaybackService.PREF_LAST_PLAYED_ID)) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "PREF_LAST_PLAYED_ID changed");
			long mediaId = sharedPreferences.getLong(
					PlaybackService.PREF_AUTODELETE_MEDIA_ID, -1);
			if (mediaId != -1) {
				FeedManager manager = FeedManager.getInstance();
				FeedMedia media = manager.getFeedMedia(mediaId);
				if (media != null) {
					manager.autoDeleteIfPossible(this, media);
				}
			}
		} else if (key.equals(PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA)) {
			long id = sharedPreferences.getLong(
					PlaybackService.PREF_CURRENTLY_PLAYING_MEDIA,
					PlaybackService.NO_MEDIA_PLAYING);
			if (AppConfig.DEBUG)
				Log.d(TAG, "Currently playing media set to " + id);
			if (id != currentlyPlayingMediaId) {
				currentlyPlayingMediaId = id;
			}
		} else if (key.equals(PREF_THEME)) {
			readThemeValue();
		}
	}

	public static float getLogicalDensity() {
		return LOGICAL_DENSITY;
	}

	public boolean displayOnlyEpisodes() {
		return displayOnlyEpisodes;
	}

	public static long getCurrentlyPlayingMediaId() {
		return currentlyPlayingMediaId;
	}

	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;

	}

	public static int getThemeResourceId() {
		return theme;
	}

	/** Read value of prefTheme and determine the correct resource id. */
	private void readThemeValue() {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		int prefTheme = Integer.parseInt(prefs.getString(PREF_THEME, "0"));
		switch (prefTheme) {
		case 0:
			theme = R.style.Theme_AntennaPod_Light;
			break;
		case 1:
			theme = R.style.Theme_AntennaPod_Dark;
			break;
		}
	}

	/**
	 * Return the folder where the app stores all of its data. This method will
	 * return the standard data folder if none has been set by the user.
	 * 
	 * @param type
	 *            The name of the folder inside the data folder. May be null
	 *            when accessing the root of the data folder.
	 * @return The data folder that has been requested or null if the folder
	 *         could not be created.
	 */
	public static File getDataFolder(Context context, String type) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context.getApplicationContext());
		String strDir = prefs.getString(PREF_DATA_FOLDER, null);
		if (strDir == null) {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Using default data folder");
			return context.getExternalFilesDir(type);
		} else {
			File dataDir = new File(strDir);
			if (!dataDir.exists()) {
				if (!dataDir.mkdir()) {
					Log.w(TAG, "Could not create data folder");
					return null;
				}
			}

			if (type == null) {
				return dataDir;
			} else {
				// handle path separators
				String[] dirs = type.split("/");
				for (int i = 0; i < dirs.length; i++) {
					if (dirs.length > 0) {
						if (i < dirs.length - 1) {
							dataDir = getDataFolder(context, dirs[i]);
							if (dataDir == null) {
								return null;
							}
						}
						type = dirs[i];
					}
				}
				File typeDir = new File(dataDir, type);
				if (!typeDir.exists()) {
					if (dataDir.canWrite()) {
						if (!typeDir.mkdir()) {
							Log.e(TAG, "Could not create data folder named "
									+ type);
							return null;
						}
					}
				}
				return typeDir;
			}

		}
	}

	public void setDataFolder(String dir) {
		if (AppConfig.DEBUG)
			Log.d(TAG, "Result from DirectoryChooser: " + dir);
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(this);
		SharedPreferences.Editor editor = prefs.edit();
		editor.putString(PodcastApp.PREF_DATA_FOLDER, dir);
		editor.commit();
		createImportDirectory();
	}
}