Clear persistent storage on server deletion.

Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
Yahor Berdnikau 2018-03-12 22:21:07 +01:00
parent 334ffbf5e9
commit 8076956786
2 changed files with 19 additions and 4 deletions

View File

@ -12,7 +12,9 @@ import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import org.moire.ultrasonic.BuildConfig;
import org.moire.ultrasonic.R;
import org.moire.ultrasonic.cache.PermanentFileStorage;
import org.moire.ultrasonic.service.MusicService;
import org.moire.ultrasonic.service.MusicServiceFactory;
import org.moire.ultrasonic.util.Constants;
@ -279,6 +281,15 @@ public class ServerSettingsFragment extends PreferenceFragment
int activeServers = sharedPreferences
.getInt(Constants.PREFERENCES_KEY_ACTIVE_SERVERS, 0);
// Clear permanent storage
final String storageServerId = MusicServiceFactory.getServerId(sharedPreferences, serverId);
final PermanentFileStorage fileStorage = new PermanentFileStorage(
MusicServiceFactory.getDirectories(getActivity()),
storageServerId,
BuildConfig.DEBUG
);
fileStorage.clearAll();
// Reset values to null so when we ask for them again they are new
sharedPreferences.edit()
.remove(Constants.PREFERENCES_KEY_SERVER_NAME + serverId)

View File

@ -116,14 +116,18 @@ public class MusicServiceFactory {
private static PermanentFileStorage getPermanentFileStorage(final Context context) {
final SharedPreferences preferences = Util.getPreferences(context);
int instance = preferences.getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
String serverUrl = preferences.getString(
Constants.PREFERENCES_KEY_SERVER_URL + instance, null);
String serverId = String.valueOf(Math.abs((serverUrl + instance).hashCode()));
final String serverId = getServerId(preferences, instance);
return new PermanentFileStorage(getDirectories(context), serverId, BuildConfig.DEBUG);
}
private static Directories getDirectories(final Context context) {
public static String getServerId(final SharedPreferences sp, final int instance) {
String serverUrl = sp.getString(
Constants.PREFERENCES_KEY_SERVER_URL + instance, null);
return String.valueOf(Math.abs((serverUrl + instance).hashCode()));
}
public static Directories getDirectories(final Context context) {
return new Directories() {
@NotNull
@Override