Skip to content

Commit

Permalink
Use view binding VersioningDialogActivity and WebGuiActivity (#2051)
Browse files Browse the repository at this point in the history
Use view binding VersioningDialogActivity and WebGuiActivity πŸ™‚
  • Loading branch information
adamszewe authored Feb 10, 2024
1 parent 6611404 commit 03f7b8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.Spinner;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ActivityVersioningDialogBinding;
import com.nutomic.syncthingandroid.fragments.dialog.ExternalVersioningFragment;
import com.nutomic.syncthingandroid.fragments.dialog.NoVersioningFragment;
import com.nutomic.syncthingandroid.fragments.dialog.SimpleVersioningFragment;
Expand All @@ -27,10 +26,13 @@ public class VersioningDialogActivity extends ThemedAppCompatActivity {

private Bundle mArguments;

private ActivityVersioningDialogBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_versioning_dialog);
binding = ActivityVersioningDialogBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

if (savedInstanceState != null) {
mArguments = savedInstanceState.getBundle("arguments");
Expand All @@ -44,8 +46,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

private void initiateFinishBtn() {
Button finishBtn = findViewById(R.id.finish_btn);
finishBtn.setOnClickListener(v -> {
binding.finishBtn.setOnClickListener(v -> {
saveConfiguration();
finish();
});
Expand All @@ -58,9 +59,8 @@ private void saveConfiguration() {
}

private void initiateSpinner() {
Spinner versioningTypeSpinner = findViewById(R.id.versioningTypeSpinner);
versioningTypeSpinner.setSelection(mTypes.indexOf(getIntent().getExtras().getString("type")));
versioningTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
binding.versioningTypeSpinner.setSelection(mTypes.indexOf(getIntent().getExtras().getString("type")));
binding.versioningTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position != mTypes.indexOf(getIntent().getExtras().getString("type"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Proxy;
import android.net.Uri;
import android.net.http.SslCertificate;
import android.net.http.SslError;
import android.net.Proxy;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
Expand All @@ -18,13 +18,13 @@
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.nutomic.syncthingandroid.R;
import com.nutomic.syncthingandroid.databinding.ActivityWebGuiBinding;
import com.nutomic.syncthingandroid.service.Constants;
import com.nutomic.syncthingandroid.service.SyncthingService;
import com.nutomic.syncthingandroid.service.SyncthingServiceBinder;
Expand All @@ -37,7 +37,6 @@
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -58,14 +57,12 @@ public class WebGuiActivity extends StateDialogActivity

private static final String TAG = "WebGuiActivity";

private WebView mWebView;

private View mLoadingView;

private X509Certificate mCaCert;

private ConfigXml mConfig;

private ActivityWebGuiBinding binding;

/**
* Hides the loading screen and shows the WebView once it is fully loaded.
*/
Expand Down Expand Up @@ -110,8 +107,8 @@ public boolean shouldOverrideUrlLoading(WebView view, String url) {

@Override
public void onPageFinished(WebView view, String url) {
mWebView.setVisibility(View.VISIBLE);
mLoadingView.setVisibility(View.GONE);
binding.webview.setVisibility(View.VISIBLE);
binding.loading.setVisibility(View.GONE);
}
};

Expand All @@ -124,18 +121,16 @@ public void onPageFinished(WebView view, String url) {
@SuppressLint("SetJavaScriptEnabled")
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityWebGuiBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

setContentView(R.layout.activity_web_gui);

mLoadingView = findViewById(R.id.loading);
mConfig = new ConfigXml(this);
loadCaCert();

mWebView = findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.setWebViewClient(mWebViewClient);
mWebView.clearCache(true);
binding.webview.getSettings().setJavaScriptEnabled(true);
binding.webview.getSettings().setDomStorageEnabled(true);
binding.webview.setWebViewClient(mWebViewClient);
binding.webview.clearCache(true);

// SyncthingService needs to be started from this activity as the user
// can directly launch this activity from the recent activity switcher.
Expand All @@ -158,26 +153,26 @@ public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
public void onServiceStateChange(SyncthingService.State newState) {
Log.v(TAG, "onServiceStateChange(" + newState + ")");
if (newState == SyncthingService.State.ACTIVE) {
if (mWebView == null) {
if (binding.webview == null) {
Log.v(TAG, "onWebGuiAvailable: Skipped event due to mWebView == null");
return;
}
if (mWebView.getUrl() == null) {
mWebView.stopLoading();
setWebViewProxy(mWebView.getContext().getApplicationContext(), "", 0, "localhost|0.0.0.0|127.*|[::1]");
if (binding.webview.getUrl() == null) {
binding.webview.stopLoading();
setWebViewProxy(binding.webview.getContext().getApplicationContext(), "", 0, "localhost|0.0.0.0|127.*|[::1]");
String credentials = mConfig.getUserName() + ":" + mConfig.getApiKey();
String b64Credentials = Base64.encodeToString(credentials.getBytes(UTF_8), Base64.NO_WRAP);
Map<String,String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + b64Credentials);
mWebView.loadUrl(getService().getWebGuiUrl().toString(), headers);
binding.webview.loadUrl(getService().getWebGuiUrl().toString(), headers);
}
}
}

@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
if (binding.webview.canGoBack()) {
binding.webview.goBack();
} else {
finish();
super.onBackPressed();
Expand All @@ -186,16 +181,16 @@ public void onBackPressed() {

@Override
public void onPause() {
mWebView.onPause();
mWebView.pauseTimers();
binding.webview.onPause();
binding.webview.pauseTimers();
super.onPause();
}

@Override
public void onResume() {
super.onResume();
mWebView.resumeTimers();
mWebView.onResume();
binding.webview.resumeTimers();
binding.webview.onResume();
}

@Override
Expand All @@ -204,8 +199,7 @@ protected void onDestroy() {
if (mSyncthingService != null) {
mSyncthingService.unregisterOnServiceStateChangeListener(this);
}
mWebView.destroy();
mWebView = null;
binding.webview.destroy();
super.onDestroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
android:padding="?dialogPreferredPadding"
style="@style/Theme.Syncthing.Dialog">


<Spinner
android:id="@+id/versioningTypeSpinner"
android:layout_width="match_parent"
Expand Down Expand Up @@ -38,4 +37,4 @@
</LinearLayout>
</ScrollView>

</LinearLayout>
</LinearLayout>

0 comments on commit 03f7b8c

Please sign in to comment.