Restore subscriptions when the app is first started

Perform a subscription list restore when the app is started for the
first time. Use a preference setting to figure out if the app is freshly
installed or not.
This commit is contained in:
Johan Liesén 2014-03-21 16:55:06 +01:00
parent 299a6e4789
commit ffac53f9ed
3 changed files with 44 additions and 0 deletions

View File

@ -354,6 +354,9 @@
<string name="new_episodes_count_label">Number of new episodes</string>
<string name="in_progress_episodes_count_label">Number of episodes you have started listening to</string>
<!-- OPML backup -->
<string name="backup_restored">"Restored feed subscriptions from backup"</string>
<!-- AntennaPodSP -->
<string name="sp_apps_importing_feeds_msg">Importing subscriptions from single-purpose apps&#8230;</string>

View File

@ -4,6 +4,8 @@ import java.util.ArrayList;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.app.backup.BackupManager;
import android.app.backup.RestoreObserver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
@ -22,6 +24,8 @@ import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.feed.EventDistributor;
@ -114,6 +118,26 @@ public class MainActivity extends ActionBarActivity {
updateProgressBarVisibility();
EventDistributor.getInstance().register(contentUpdate);
// Possibly restore feed subscriptions from backup
if (UserPreferences.isFreshInstall()) {
new BackupManager(this).requestRestore(new BackupRestoreObserver(this));
UserPreferences.setIsFreshInstall(false);
}
}
private static class BackupRestoreObserver extends RestoreObserver {
private final Context mContext;
public BackupRestoreObserver(final Context context) {
mContext = context;
}
@Override
public void restoreFinished(int error) {
if (error == 0) {
Toast.makeText(mContext, R.string.backup_restored, Toast.LENGTH_SHORT).show();
}
}
}
private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

View File

@ -50,6 +50,7 @@ public class UserPreferences implements
private static final String PREF_PLAYBACK_SPEED = "prefPlaybackSpeed";
private static final String PREF_PLAYBACK_SPEED_ARRAY = "prefPlaybackSpeedArray";
public static final String PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS = "prefPauseForFocusLoss";
private static final String PREF_IS_FRESH_INSTALL = "prefIsFreshInstall";
// TODO: Make this value configurable
private static final double PLAYED_DURATION_AUTOFLATTR_THRESHOLD = 0.8;
@ -76,6 +77,7 @@ public class UserPreferences implements
private String playbackSpeed;
private String[] playbackSpeedArray;
private boolean pauseForFocusLoss;
private boolean isFreshInstall;
private UserPreferences(Context context) {
this.context = context;
@ -130,6 +132,7 @@ public class UserPreferences implements
playbackSpeedArray = readPlaybackSpeedArray(sp.getString(
PREF_PLAYBACK_SPEED_ARRAY, null));
pauseForFocusLoss = sp.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false);
isFreshInstall = sp.getBoolean(PREF_IS_FRESH_INSTALL, true);
}
private int readThemeValue(String valueFromPrefs) {
@ -284,6 +287,11 @@ public class UserPreferences implements
return instance.pauseForFocusLoss;
}
public static boolean isFreshInstall() {
instanceAvailable();
return instance.isFreshInstall;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sp, String key) {
if (AppConfig.DEBUG)
@ -334,6 +342,8 @@ public class UserPreferences implements
pauseForFocusLoss = sp.getBoolean(PREF_PAUSE_PLAYBACK_FOR_FOCUS_LOSS, false);
} else if (key.equals(PREF_PAUSE_ON_HEADSET_DISCONNECT)) {
pauseOnHeadsetDisconnect = sp.getBoolean(PREF_PAUSE_ON_HEADSET_DISCONNECT, true);
} else if (key.equals(PREF_IS_FRESH_INSTALL)) {
isFreshInstall = sp.getBoolean(PREF_IS_FRESH_INSTALL, true);
}
}
@ -526,4 +536,11 @@ public class UserPreferences implements
instanceAvailable();
return PLAYED_DURATION_AUTOFLATTR_THRESHOLD;
}
public static void setIsFreshInstall(boolean isFreshInstall) {
instanceAvailable();
PreferenceManager.getDefaultSharedPreferences(instance.context).edit()
.putBoolean(PREF_IS_FRESH_INSTALL, isFreshInstall)
.apply();
}
}