Fix issue #3 - Cache not cleared after update

This commit is contained in:
Thomas 2020-11-23 09:15:48 +01:00
parent 3485d6d183
commit 3abcc17fc6
3 changed files with 38 additions and 19 deletions

View File

@ -8,8 +8,8 @@ android {
applicationId "app.fedilab.mobilizon" applicationId "app.fedilab.mobilizon"
minSdkVersion 23 minSdkVersion 23
targetSdkVersion 30 targetSdkVersion 30
versionCode 2 versionCode 3
versionName "1.0.1" versionName "1.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
} }
@ -47,7 +47,7 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1' implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.3' implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.navigation:navigation-fragment:2.3.1' implementation 'androidx.navigation:navigation-fragment:2.3.1'
implementation 'androidx.navigation:navigation-ui:2.3.1' implementation 'androidx.navigation:navigation-ui:2.3.1'
testImplementation 'junit:junit:4.13' testImplementation 'junit:junit:4.13'

View File

@ -133,21 +133,17 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("mobilizon") == 0) { if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("mobilizon") == 0) {
runOnUiThread(() -> { runOnUiThread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
boolean forcedCacheDeletedDone = sharedpreferences.getBoolean(Helper.FORCED_CACHE_DELETE_DONE, false);
String previousVersion = sharedpreferences.getString(Helper.PREF_VERSION+instance, null); String previousVersion = sharedpreferences.getString(Helper.PREF_VERSION+instance, null);
if( previousVersion != null && previousVersion.trim().compareTo(instanceNodeInfo.getSoftware().getVersion()) != 0) { if( !forcedCacheDeletedDone || (previousVersion != null && previousVersion.trim().compareTo(instanceNodeInfo.getSoftware().getVersion()) != 0)) {
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_VERSION + instance.trim(), instanceNodeInfo.getSoftware().getVersion().trim()); editor.putString(Helper.PREF_VERSION + instance.trim(), instanceNodeInfo.getSoftware().getVersion().trim());
editor.putBoolean(Helper.FORCED_CACHE_DELETE_DONE, true);
editor.apply(); editor.apply();
File dir = new File(getFilesDir().getPath()+"/" + instance); File dir = new File(getCacheDir().getPath());
if (dir.isDirectory()) { try {
String[] children = dir.list(); Helper.deleteDir(dir);
if( children != null) { }catch (Exception ignored){}
for (String child : children) {
//noinspection ResultOfMethodCallIgnored
new File(dir, child).delete();
}
}
}
main_webview.reload(); main_webview.reload();
}else if(previousVersion == null) { }else if(previousVersion == null) {
SharedPreferences.Editor editor = sharedpreferences.edit(); SharedPreferences.Editor editor = sharedpreferences.edit();
@ -159,11 +155,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} catch (Exception ignored) {} } catch (Exception ignored) {}
}).start(); }).start();
} }
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. // Inflate the menu; this adds items to the action bar if it is present.
@ -201,7 +195,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
// as you specify a parent activity in AndroidManifest.xml. // as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId(); int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_instance) { if (id == R.id.action_instance) {
showRadioButtonDialogFullInstances(); showRadioButtonDialogFullInstances();
return true; return true;
@ -360,7 +353,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
} }
}).start()); }).start());
//TODO: uncomment and add support for picking instances when available
alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss()); alt_bld.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
alt_bld.setNeutralButton(R.string.help, (dialog, which) -> { alt_bld.setNeutralButton(R.string.help, (dialog, which) -> {
Intent intent = new Intent(MainActivity.this, InstancePickerActivity.class); Intent intent = new Intent(MainActivity.this, InstancePickerActivity.class);

View File

@ -11,6 +11,7 @@ import android.webkit.WebSettings;
import android.webkit.WebStorage; import android.webkit.WebStorage;
import android.webkit.WebView; import android.webkit.WebView;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
/* Copyright 2020 Thomas Schneider /* Copyright 2020 Thomas Schneider
@ -34,6 +35,9 @@ public class Helper {
public static final String APP_PREFS = "app_prefs"; public static final String APP_PREFS = "app_prefs";
public static final String PREF_INSTANCE = "instance"; public static final String PREF_INSTANCE = "instance";
public static final String PREF_VERSION = "version_"; public static final String PREF_VERSION = "version_";
public static final String FORCED_CACHE_DELETE_DONE = "forced_cache_delete_done";
/** /**
* Returns the preferred instance * Returns the preferred instance
* *
@ -62,7 +66,6 @@ public class Helper {
webView.getSettings().setBuiltInZoomControls(true); webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadsImagesAutomatically(true); webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setSupportMultipleWindows(false); webView.getSettings().setSupportMultipleWindows(false);
webView.getSettings().setGeolocationDatabasePath(context.getFilesDir().getPath()+"/" + instance);
webView.getSettings().setGeolocationEnabled(true); webView.getSettings().setGeolocationEnabled(true);
webView.getSettings().setAppCachePath(context.getCacheDir().getPath()); webView.getSettings().setAppCachePath(context.getCacheDir().getPath());
webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setAppCacheEnabled(true);
@ -82,6 +85,30 @@ public class Helper {
} }
/**
* Delete a directory
* @param dir File
* @return boolean
*/
public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
String[] children = dir.list();
for (String child : children) {
boolean success = deleteDir(new File(dir, child));
if (!success) {
return false;
}
}
return dir.delete();
} else if(dir!= null && dir.isFile()) {
return dir.delete();
} else {
return false;
}
}
@SuppressWarnings({"ResultOfMethodCallIgnored"}) @SuppressWarnings({"ResultOfMethodCallIgnored"})
public static void injectCSS(Activity activity, WebView view, String cssFile) { public static void injectCSS(Activity activity, WebView view, String cssFile) {
try { try {