Detect version + automatically clear cache if the version changed

This commit is contained in:
Thomas 2020-10-12 18:07:10 +02:00
parent cbab3fdc20
commit 11fec8c4ac
4 changed files with 58 additions and 16 deletions

View File

@ -7,7 +7,6 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application <application

View File

@ -36,16 +36,15 @@ import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout; import androidx.drawerlayout.widget.DrawerLayout;
import com.google.android.material.navigation.NavigationView; import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.karumi.dexter.Dexter; import com.karumi.dexter.Dexter;
import com.karumi.dexter.MultiplePermissionsReport;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.multi.MultiplePermissionsListener; import com.karumi.dexter.listener.multi.MultiplePermissionsListener;
import com.karumi.dexter.listener.multi.SnackbarOnAnyDeniedMultiplePermissionsListener;
import java.io.File;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List;
import app.fedilab.mobilizon.client.RetrofitMobilizonAPI; import app.fedilab.mobilizon.client.RetrofitMobilizonAPI;
import app.fedilab.mobilizon.client.entities.WellKnownNodeinfo; import app.fedilab.mobilizon.client.entities.WellKnownNodeinfo;
@ -97,7 +96,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
webview_container = findViewById(R.id.webview_container); webview_container = findViewById(R.id.webview_container);
final ViewGroup videoLayout = findViewById(R.id.videoLayout); final ViewGroup videoLayout = findViewById(R.id.videoLayout);
String instance = Helper.getLiveInstance(MainActivity.this); String instance = Helper.getLiveInstance(MainActivity.this);
Helper.initializeWebview(MainActivity.this, main_webview); Helper.initializeWebview(MainActivity.this, main_webview, instance);
MobilizonWebChromeClient mobilizonWebChromeClient = new MobilizonWebChromeClient(MainActivity.this, main_webview, webview_container, videoLayout); MobilizonWebChromeClient mobilizonWebChromeClient = new MobilizonWebChromeClient(MainActivity.this, main_webview, webview_container, videoLayout);
main_webview.setWebChromeClient(mobilizonWebChromeClient); main_webview.setWebChromeClient(mobilizonWebChromeClient);
main_webview.setWebViewClient(new MobilizonWebViewClient(MainActivity.this)); main_webview.setWebViewClient(new MobilizonWebViewClient(MainActivity.this));
@ -105,20 +104,60 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
showProgressDialog(); showProgressDialog();
main_webview.loadUrl("https://" + instance); main_webview.loadUrl("https://" + instance);
MultiplePermissionsListener snackbarMultiplePermissionsListener =
SnackbarOnAnyDeniedMultiplePermissionsListener.Builder
.with(main_webview, R.string.permissions_message)
.withOpenSettingsButton(R.string.settings)
.withCallback(new Snackbar.Callback() {
@Override
public void onShown(Snackbar snackbar) {
// Event handler for when the given Snackbar is visible
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
// Event handler for when the given Snackbar has been dismissed
}
})
.build();
Dexter.withContext(this) Dexter.withContext(this)
.withPermissions( .withPermissions(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION Manifest.permission.ACCESS_FINE_LOCATION
) )
.withListener(new MultiplePermissionsListener() { .withListener(snackbarMultiplePermissionsListener).check();
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {/* ... */}
@Override new Thread(() -> {
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {/* ... */} try {
}).check(); WellKnownNodeinfo.NodeInfo instanceNodeInfo = new RetrofitMobilizonAPI(instance).getNodeInfo();
if (instanceNodeInfo.getSoftware() != null && instanceNodeInfo.getSoftware().getName().trim().toLowerCase().compareTo("mobilizon") == 0) {
runOnUiThread(() -> {
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String previousVersion = sharedpreferences.getString(Helper.PREF_VERSION+instance, null);
if( previousVersion != null && previousVersion.trim().compareTo(instanceNodeInfo.getSoftware().getVersion()) != 0) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_VERSION + instance.trim(), instanceNodeInfo.getSoftware().getVersion().trim());
editor.apply();
File dir = new File(getFilesDir().getPath()+"/" + instance);
if (dir.isDirectory()) {
String[] children = dir.list();
if( children != null) {
for (String child : children) {
//noinspection ResultOfMethodCallIgnored
new File(dir, child).delete();
}
}
}
main_webview.reload();
}else if(previousVersion == null) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Helper.PREF_VERSION + instance.trim(), instanceNodeInfo.getSoftware().getVersion().trim());
editor.apply();
}
});
}
} catch (Exception ignored) {}
}).start();
} }

View File

@ -29,9 +29,11 @@ import java.io.InputStream;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
public class Helper { public class Helper {
@SuppressWarnings("unused")
public static final String TAG = "mobilizon_app";
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_";
/** /**
* Returns the preferred instance * Returns the preferred instance
* *
@ -50,7 +52,7 @@ public class Helper {
* @param webView WebView * @param webView WebView
*/ */
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
public static void initializeWebview(Context context, WebView webView) { public static void initializeWebview(Context context, WebView webView, String instance) {
webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true); webView.getSettings().setUseWideViewPort(true);
@ -60,7 +62,7 @@ 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()); 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);

View File

@ -37,4 +37,6 @@
<string name="terms">Terms</string> <string name="terms">Terms</string>
<string name="license">License</string> <string name="license">License</string>
<string name="about_the_app">About the app (Release %1$s)</string> <string name="about_the_app">About the app (Release %1$s)</string>
<string name="permissions_message">This permission is used for searching events next to your location.</string>
<string name="settings">Settings</string>
</resources> </resources>