This commit is contained in:
rusefi 2020-08-16 00:32:10 -04:00
parent c8cab80b79
commit 5e34753d5f
9 changed files with 44 additions and 29 deletions

View File

@ -31,6 +31,7 @@ import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.util.Linkify;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
@ -41,7 +42,7 @@ import com.google.android.material.snackbar.BaseTransientBottomBar;
import com.google.android.material.snackbar.Snackbar;
import com.rusefi.Callable;
import com.rusefi.app.serial.AndroidSerial;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.dfu.DfuConnection;
import com.rusefi.dfu.DfuImage;
import com.rusefi.dfu.DfuLogic;
@ -69,6 +70,7 @@ public class rusEFI extends Activity {
private TextView mStatusView;
private TextView mResultView;
private EditText authToken;
private TextView myClickableUrl;
private UsbManager usbManager;
private DfuUpload dfuUpload;
@ -102,15 +104,21 @@ public class rusEFI extends Activity {
@Override
public void afterTextChanged(Editable editable) {
String text = authToken.getText().toString();
if (AutoTokenUtil.isToken(text)) {
if (AuthTokenUtil.isToken(text)) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(rusEFI.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(AutoTokenUtil.AUTH_TOKEN, text);
editor.putString(AuthTokenUtil.AUTH_TOKEN, text);
editor.commit();
myClickableUrl.setVisibility(View.GONE);
}
}
});
myClickableUrl = (TextView) findViewById(R.id.authStatus);
myClickableUrl.setText(AuthTokenUtil.TOKEN_PROFILE_URL);
Linkify.addLinks(myClickableUrl, Linkify.WEB_URLS);
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
@ -119,7 +127,9 @@ public class rusEFI extends Activity {
dfuUpload = new DfuUpload(this);
dfuUpload.fileOperation(mResultView);
authToken.setText(getAuthToken());
String authToken = getAuthToken();
this.authToken.setText(authToken);
myClickableUrl.setVisibility(AuthTokenUtil.isToken(authToken) ? View.VISIBLE : View.GONE);
// switchOrProgramDfu();
@ -128,7 +138,7 @@ public class rusEFI extends Activity {
private String getAuthToken() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(rusEFI.this);
return preferences.getString(AutoTokenUtil.AUTH_TOKEN, AutoTokenUtil.TOKEN_WARNING);
return preferences.getString(AuthTokenUtil.AUTH_TOKEN, "");
}
@Override

View File

@ -2,12 +2,12 @@
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -34,6 +34,12 @@
android:inputType="text"
android:text="Auth Token" />
<TextView
android:id="@+id/authStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/buttonSound"
android:layout_width="match_parent"

View File

@ -1,6 +1,6 @@
package com.rusefi.auth;
public class AutoTokenUtil {
public class AuthTokenUtil {
public static final int TOKEN_LENGTH = 8 + 1 + 4 + 1 + 4 + 1 + 4 + 1 + 12;
public static final String TOKEN_PROFILE_URL = "https://rusefi.com/forum/ucp.php?i=254";

View File

@ -1,6 +1,6 @@
package com.rusefi.ui.test;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.auth.AuthTokenUtil;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
@ -9,11 +9,11 @@ import static org.junit.Assert.assertTrue;
public class AuthTokenUtilTest {
@Test
public void test() {
assertFalse(AutoTokenUtil.isToken(null));
assertTrue(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 "));
assertFalse(AuthTokenUtil.isToken(null));
assertTrue(AuthTokenUtil.isToken(" 11112222-5719-4444-84d4-111122223333 "));
assertFalse(AutoTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 "));
assertFalse(AuthTokenUtil.isToken(" 11112222x5719-4444-84d4-111122223333 "));
assertFalse(AutoTokenUtil.isToken(" 11112222-5719-4444-84d4-11112222333 "));
assertFalse(AuthTokenUtil.isToken(" 11112222-5719-4444-84d4-11112222333 "));
}
}

View File

@ -1,6 +1,6 @@
package com.rusefi.ui;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.ui.storage.PersistentConfiguration;
import com.rusefi.ui.util.URLLabel;
import org.jetbrains.annotations.NotNull;
@ -56,7 +56,7 @@ public class AuthTokenPanel {
public void actionPerformed(ActionEvent e) {
try {
String data = (String) clipboard.getData(DataFlavor.stringFlavor);
if (AutoTokenUtil.isToken(data)) {
if (AuthTokenUtil.isToken(data)) {
authTokenTestField.setText(data);
}
} catch (IOException | UnsupportedFlavorException ex) {
@ -85,25 +85,25 @@ public class AuthTokenPanel {
*/
content.add(top);
if (authToken.trim().isEmpty()) {
authToken = AutoTokenUtil.TOKEN_WARNING;
authToken = AuthTokenUtil.TOKEN_WARNING;
}
content.add(new URLLabel("Manage authentication token at your forum profile", AutoTokenUtil.TOKEN_PROFILE_URL), BorderLayout.SOUTH);
content.add(new URLLabel("Manage authentication token at your forum profile", AuthTokenUtil.TOKEN_PROFILE_URL), BorderLayout.SOUTH);
authTokenTestField.setText(authToken);
}
public static void setAuthToken(String value) {
getConfig().getRoot().setProperty(AutoTokenUtil.AUTH_TOKEN, value);
getConfig().getRoot().setProperty(AuthTokenUtil.AUTH_TOKEN, value);
}
@NotNull
public static String getAuthToken() {
return getConfig().getRoot().getProperty(AutoTokenUtil.AUTH_TOKEN);
return getConfig().getRoot().getProperty(AuthTokenUtil.AUTH_TOKEN);
}
private void setPasteButtonEnabledBasedOnClipboardContent(Clipboard clipboard, JButton paste) {
try {
String data = (String) clipboard.getData(DataFlavor.stringFlavor);
paste.setEnabled(AutoTokenUtil.isToken(data));
paste.setEnabled(AuthTokenUtil.isToken(data));
} catch (IOException | IllegalStateException | UnsupportedFlavorException ex) {
// ignoring this exception
}
@ -115,7 +115,7 @@ public class AuthTokenPanel {
}
private void onTextChange() {
if (AutoTokenUtil.isToken(authTokenTestField.getText())) {
if (AuthTokenUtil.isToken(authTokenTestField.getText())) {
grabText();
}
}
@ -125,7 +125,7 @@ public class AuthTokenPanel {
}
public static boolean hasToken() {
return AutoTokenUtil.isToken(getAuthToken());
return AuthTokenUtil.isToken(getAuthToken());
}
public String getToken() {

View File

@ -1,7 +1,7 @@
package com.rusefi.tools;
import com.devexperts.logging.Logging;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.autodetect.PortDetector;
import com.rusefi.proxy.NetworkConnector;
import com.rusefi.proxy.NetworkConnectorContext;
@ -11,7 +11,7 @@ public class NetworkConnectorStartup {
private final static Logging log = Logging.getLogging(NetworkConnectorStartup.class);
public static void start() {
String authToken = AuthTokenPanel.getAuthToken();
if (!AutoTokenUtil.isToken(authToken)) {
if (!AuthTokenUtil.isToken(authToken)) {
System.err.println("Please configure authentication token using 'set_auth_token' command");
return;
}

View File

@ -1,7 +1,7 @@
package com.rusefi.server;
import com.devexperts.logging.Logging;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.binaryprotocol.IncomingDataBuffer;
import com.rusefi.config.generated.Fields;
import com.rusefi.core.SensorsHolder;
@ -93,7 +93,7 @@ public class ControllerConnectionState {
if (jsonString == null)
return;
sessionDetails = SessionDetails.valueOf(jsonString);
if (!AutoTokenUtil.isToken(sessionDetails.getAuthToken()))
if (!AuthTokenUtil.isToken(sessionDetails.getAuthToken()))
throw new IOException("Invalid token in " + jsonString);
log.info(sessionDetails.getAuthToken() + " New client: " + sessionDetails.getControllerInfo());

View File

@ -1,6 +1,6 @@
package com.rusefi.ts_plugin;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.auth.AuthTokenUtil;
import com.rusefi.autodetect.PortDetector;
import com.rusefi.autoupdate.AutoupdateUtil;
import com.rusefi.proxy.NetworkConnector;
@ -33,7 +33,7 @@ public class BroadcastTab {
broadcast.addActionListener(e -> {
String authToken = AuthTokenPanel.getAuthToken();
if (!AutoTokenUtil.isToken(authToken)) {
if (!AuthTokenUtil.isToken(authToken)) {
status.setText("Auth token is required to broadcast ECU");
return;
}

View File

@ -3,7 +3,6 @@ package com.rusefi.ts_plugin;
import com.rusefi.NamedThreadFactory;
import com.rusefi.SignatureHelper;
import com.rusefi.Timeouts;
import com.rusefi.auth.AutoTokenUtil;
import com.rusefi.autoupdate.AutoupdateUtil;
import com.rusefi.io.serial.StreamStatistics;
import com.rusefi.io.tcp.ServerSocketReference;