Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
fm-sys committed Oct 1, 2022
1 parent 140ae91 commit 5413a14
Showing 6 changed files with 82 additions and 77 deletions.
28 changes: 15 additions & 13 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions app/src/main/assets/init.js
Original file line number Diff line number Diff line change
@@ -73,15 +73,6 @@ let aboutBackground = document.querySelector('#about>x-background');
aboutBackground.style.left = 'calc(32px - 200px)';
aboutBackground.style.right = null;

//change ServerConnection.send(message) to connect to JavaScriptInterface
ServerConnection.prototype.s = ServerConnection.prototype.send;
ServerConnection.prototype.send = function(message){
this.s(message);
if (message.type == 'pong') {
SnapdropAndroid.updateLastOnlineTime();
}
};

//change PeerUI._onTouchEnd(e) to connect to JavaScriptInterface
PeerUI.prototype._oTE = PeerUI.prototype._onTouchEnd;
PeerUI.prototype._onTouchEnd = function(e){
5 changes: 0 additions & 5 deletions app/src/main/java/com/fmsys/snapdrop/JavaScriptInterface.java
Original file line number Diff line number Diff line change
@@ -76,11 +76,6 @@ public int getVersionId() {
return BuildConfig.VERSION_CODE;
}

@JavascriptInterface
public void updateLastOnlineTime() {
context.setLastServerConnection(System.currentTimeMillis());
}

@JavascriptInterface
public boolean shouldOpenSendTextDialog() {
return context.onlyText;
85 changes: 36 additions & 49 deletions app/src/main/java/com/fmsys/snapdrop/MainActivity.java
Original file line number Diff line number Diff line change
@@ -65,6 +65,7 @@
import com.fmsys.snapdrop.databinding.ActivityMainBinding;
import com.fmsys.snapdrop.utils.NetworkUtils;
import com.fmsys.snapdrop.utils.ShareUtils;
import com.fmsys.snapdrop.utils.StateHandler;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
@@ -86,9 +87,7 @@ public class MainActivity extends AppCompatActivity {

public ValueCallback<Uri[]> uploadMessage;

private boolean currentlyOffline = true;
private boolean currentlyAtAboutPage = false;
private boolean currentlyLoading = true;
private final StateHandler state = new StateHandler();
public boolean forceRefresh = false;
public ObservableProperty<Boolean> transfer = new ObservableProperty<>(false);
public boolean onlyText = false;
@@ -104,7 +103,7 @@ public class MainActivity extends AppCompatActivity {

@Override
public void onReceive(final Context context, final Intent intent) {
if ((!isInitialStickyBroadcast()) && currentlyOffline) {
if (state.isCurrentlyOffline()) {
final Handler handler = new Handler();
handler.postDelayed(() -> {
if (NetworkUtils.isWifiAvailable()) {
@@ -219,7 +218,7 @@ protected void onCreate(final Bundle savedInstanceState) {

binding.pullToRefresh.setOnRefreshListener(() -> refreshWebsite(true));

splashScreen.setKeepOnScreenCondition(() -> currentlyLoading);
splashScreen.setKeepOnScreenCondition(state::isCurrentlyStarting);
}

@Override
@@ -243,12 +242,11 @@ public boolean onOptionsItemSelected(@NonNull final MenuItem item) {
}

private void toggleAbout() {
if (currentlyAtAboutPage) {
if (binding.webview.getUrl() != null && binding.webview.getUrl().endsWith("#about")) {
binding.webview.loadUrl(baseURL + "#");
} else {
binding.webview.loadUrl(baseURL + "#about");
}
currentlyAtAboutPage = !currentlyAtAboutPage;
}

private void refreshWebsite(final boolean pulled) {
@@ -260,6 +258,7 @@ private void refreshWebsite(final boolean pulled) {
binding.pullToRefresh.setRefreshing(false);
forceRefresh = pulled; //reset forceRefresh if after pullToRefresh the refresh request did come from another source eg onResume, so pullToRefresh doesn't unexpectedly force refreshes by "first time"
} else {
state.setCurrentlyLoading(false);
showScreenNoConnection();
}
}
@@ -269,9 +268,8 @@ private void refreshWebsite() {
}

private void showScreenNoConnection() {
state.setCurrentlyOffline(true);
binding.webview.loadUrl("file:///android_asset/offline.html?text=" + getString(R.string.error_network) + "&button=" + getString(R.string.ignore_error_network));
currentlyOffline = true;
currentlyLoading = false;
}

public static boolean isTablet(final Context ctx) {
@@ -336,25 +334,13 @@ public void onActivityResult(final int requestCode, final int resultCode, final
public void onBackPressed() {
if (binding.webview.getUrl() != null && binding.webview.getUrl().endsWith("#about")) {
binding.webview.loadUrl(baseURL + "#");
currentlyAtAboutPage = false;
} else if (dialogVisible) {
binding.webview.loadUrl(JavaScriptInterface.getAssetsJS(this, "closeDialogs.js"));
} else {
super.onBackPressed();
}
}

/**
* @return true if there was no server connection for more than a minute
*/
private boolean isServerConnectionLost() {
return System.currentTimeMillis() - prefs.getLong(getString(R.string.pref_last_server_connection), 0) > 1000 * 60;
}

public void setLastServerConnection(final long timestamp) {
prefs.edit().putLong(getString(R.string.pref_last_server_connection), timestamp).apply();
}

@Override
public void onResume() {
super.onResume();
@@ -367,7 +353,7 @@ public void onResume() {
@Override
public void onRestart() {
super.onRestart();
if (isServerConnectionLost()) {
if (binding.webview.getUrl() == null || !binding.webview.getUrl().startsWith(baseURL)) {
refreshWebsite();
}
}
@@ -382,7 +368,6 @@ protected void onStop() {
}
if (!transfer.get() && !dialogVisible && uploadMessage == null) {
binding.webview.loadUrl("about:blank");
setLastServerConnection(0);
}
}

@@ -502,12 +487,12 @@ private class CustomWebViewClient extends WebViewClient {
@Override
public void onPageFinished(final WebView view, final String url) {
Log.w("SnapdropAndroid", "refresh finished");
currentlyLoading = false;
binding.pullToRefresh.setRefreshing(false);

if (url.startsWith(baseURL) || currentlyOffline) {
currentlyOffline = !url.startsWith(baseURL);
state.setCurrentlyLoading(false);
binding.pullToRefresh.setRefreshing(false);

if (url.startsWith(baseURL)) {
state.setCurrentlyOffline(false);
initSnapdrop();

}
@@ -519,23 +504,21 @@ private void initSnapdrop() {
return; // too late to do anything at this point in time...
}
//website initialisation
if (!currentlyOffline) {
binding.webview.loadUrl(JavaScriptInterface.getAssetsJS(MainActivity.this, "init.js"));
binding.webview.loadUrl(JavaScriptInterface.getSendTextDialogWithPreInsertedString(getTextFromUploadIntent()));
WebsiteLocalizer.localize(binding.webview);

// welcome dialog
if (prefs.getBoolean(getString(R.string.pref_first_use), true)) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(false)
.setTitle(R.string.app_welcome)
.setMessage(R.string.app_welcome_summary)
.setPositiveButton(android.R.string.ok, null)
.create()
.show();

prefs.edit().putBoolean(getString(R.string.pref_first_use), false).apply();
}
binding.webview.loadUrl(JavaScriptInterface.getAssetsJS(MainActivity.this, "init.js"));
binding.webview.loadUrl(JavaScriptInterface.getSendTextDialogWithPreInsertedString(getTextFromUploadIntent()));
WebsiteLocalizer.localize(binding.webview);

// welcome dialog
if (prefs.getBoolean(getString(R.string.pref_first_use), true)) {
new AlertDialog.Builder(MainActivity.this)
.setCancelable(false)
.setTitle(R.string.app_welcome)
.setMessage(R.string.app_welcome_summary)
.setPositiveButton(android.R.string.ok, null)
.create()
.show();

prefs.edit().putBoolean(getString(R.string.pref_first_use), false).apply();
}
}

@@ -545,12 +528,12 @@ public void onPageStarted(final WebView view, final String url, final Bitmap fav

final Handler handler = new Handler();
final int delay = 500; // milliseconds
currentlyLoading = true;
state.setCurrentlyLoading(true);

handler.postDelayed(new Runnable() {
public void run() {
//do something
if (currentlyLoading) {
if (state.isCurrentlyLoading()) {
if (NetworkUtils.isInternetAvailable()) {
handler.postDelayed(this, delay);
} else {
@@ -565,15 +548,19 @@ public void run() {
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onReceivedError(final WebView view, final WebResourceRequest request, final WebResourceError error) {
if (error.getErrorCode() == -6) {

state.setCurrentlyLoading(false);

Log.e("WebViewError", "Error on accessing " + request.getUrl() + ", " + error.getDescription() + " (ErrorCode " + error.getErrorCode() + ")");
showScreenNoConnection();

if (error.getErrorCode() == ERROR_CONNECT || error.getErrorCode() == ERROR_TIMEOUT) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(R.string.error_not_reachable_title)
.setMessage(R.string.error_not_reachable)
.setPositiveButton(android.R.string.ok, null)
.show();
}
Log.e("WebViewError", "Cannot reach " + request.getUrl() + " (ErrorCode " + error.getErrorCode() + ")");
showScreenNoConnection();
}
}

31 changes: 31 additions & 0 deletions app/src/main/java/com/fmsys/snapdrop/utils/StateHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.fmsys.snapdrop.utils;

public class StateHandler {
private boolean currentlyOffline = false;
private boolean currentlyLoading = true;
private boolean currentlyStarting = true;

public boolean isCurrentlyOffline() {
return currentlyOffline;
}

public void setCurrentlyOffline(final boolean currentlyOffline) {
this.currentlyOffline = currentlyOffline;
}

public boolean isCurrentlyLoading() {
return currentlyLoading;
}

public void setCurrentlyLoading(final boolean currentlyLoading) {
this.currentlyLoading = currentlyLoading;

if (!currentlyLoading) {
currentlyStarting = false;
}
}

public boolean isCurrentlyStarting() {
return currentlyStarting;
}
}
1 change: 0 additions & 1 deletion app/src/main/res/values/preference_keys.xml
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@
<resources>
<string name="pref_switch_keep_on" translatable="false">keep_screen_on</string>
<string name="pref_floating_text_selection" translatable="false">floating_text_selection</string>
<string name="pref_last_server_connection" translatable="false">last_server_connection</string>
<string name="pref_device_name" translatable="false">device_name</string>
<string name="pref_first_use" translatable="false">first_use</string>
<string name="pref_baseurl" translatable="false">baseurl</string>

0 comments on commit 5413a14

Please sign in to comment.