Progressing with making app compatible with either osdapi or firebase backend - still crashing sometimes, so need to fix that...and reported IDs of events look wrong for the osdapi backend.
This commit is contained in:
@@ -24,6 +24,9 @@ import com.google.android.gms.tasks.OnCompleteListener;
|
||||
import com.google.android.gms.tasks.Task;
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class AuthenticateActivity extends AppCompatActivity {
|
||||
@@ -35,6 +38,7 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
final Handler serverStatusHandler = new Handler();
|
||||
private WebApiConnection mWac;
|
||||
private LogManager mLm;
|
||||
private static final String TOKEN_ID = "webApiAuthToken";
|
||||
|
||||
|
||||
@Override
|
||||
@@ -146,6 +150,7 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void initialiseServiceConnection() {
|
||||
Log.v(TAG,"initialiseServiceConnection()");
|
||||
mLm = mConnection.mSdServer.mLm;
|
||||
mWac = mConnection.mSdServer.mLm.mWac;
|
||||
updateUi();
|
||||
@@ -165,25 +170,45 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
|
||||
|
||||
private void updateUi() {
|
||||
Log.v(TAG,"updateUi()");
|
||||
LinearLayout loginLl = (LinearLayout) findViewById(R.id.login_ui);
|
||||
LinearLayout osdApiLoginLl = (LinearLayout) findViewById(R.id.login_osdapi_ui);
|
||||
LinearLayout logoutLl = (LinearLayout) findViewById(R.id.logout_ui);
|
||||
|
||||
// Check if we are already logged in
|
||||
FirebaseAuth auth = FirebaseAuth.getInstance();
|
||||
if (auth.getCurrentUser() == null) {
|
||||
Log.i(TAG, "Not Logged in - showing log in UI");
|
||||
loginLl.setVisibility(View.VISIBLE);
|
||||
logoutLl.setVisibility(View.GONE);
|
||||
} else {
|
||||
Log.i(TAG, "Already Logged in - showing Log Out prompt - " + auth.getCurrentUser().toString());
|
||||
loginLl.setVisibility(View.GONE);
|
||||
logoutLl.setVisibility(View.VISIBLE);
|
||||
TextView tv2 = (TextView) findViewById(R.id.userIdTv);
|
||||
tv2.setText(auth.getCurrentUser().getDisplayName());
|
||||
tv2 = (TextView) findViewById(R.id.usernameTv);
|
||||
tv2.setText(auth.getCurrentUser().getEmail());
|
||||
if (mWac == null) {
|
||||
Log.i(TAG,"mWac is null - not updating UI");
|
||||
return;
|
||||
}
|
||||
|
||||
if (mWac.isLoggedIn()) {
|
||||
Log.v(TAG, "Already Logged in - showing Log Out prompt");
|
||||
loginLl.setVisibility(View.GONE);
|
||||
logoutLl.setVisibility(View.VISIBLE);
|
||||
if (!LogManager.USE_FIREBASE_BACKEND) {
|
||||
osdApiLoginLl.setVisibility(View.GONE);
|
||||
}
|
||||
mWac.getUserProfile((JSONObject profileObj) -> {
|
||||
try {
|
||||
String userId = profileObj.getString("id");
|
||||
String userName = profileObj.getString("username");
|
||||
TextView tv2 = (TextView) findViewById(R.id.userIdTv);
|
||||
tv2.setText(userId);
|
||||
tv2 = (TextView) findViewById(R.id.usernameTv);
|
||||
tv2.setText(userName);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error Parsing profileObj: " + e.getMessage());
|
||||
mUtil.showToast("Error Parsing profileObj - this should not happen!!!");
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Log.v(TAG,"updateUi() - not logged in..");
|
||||
loginLl.setVisibility(View.VISIBLE);
|
||||
logoutLl.setVisibility(View.GONE);
|
||||
if (!LogManager.USE_FIREBASE_BACKEND) {
|
||||
osdApiLoginLl.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
View.OnClickListener onCancel =
|
||||
@@ -201,6 +226,7 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
//m_status=true;
|
||||
if (LogManager.USE_FIREBASE_BACKEND) {
|
||||
Log.v(TAG, "onLogin() - using Firebase Login");
|
||||
Intent signInIntent = AuthUI.getInstance()
|
||||
.createSignInIntentBuilder()
|
||||
@@ -218,6 +244,28 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
// ... options ...
|
||||
.build();
|
||||
signInLauncher.launch(signInIntent);
|
||||
} else {
|
||||
// Use Username and password authentication for OSDAPI.
|
||||
// FIXME - make this work with Google Authentication like we do for Firebase.
|
||||
String uname = mUnameEt.getText().toString();
|
||||
String passwd = mPasswdEt.getText().toString();
|
||||
Log.v(TAG,"onOK() - uname="+uname+", passwd="+passwd);
|
||||
mWac.authenticate(uname, passwd, new WebApiConnection.StringCallback() {
|
||||
@Override
|
||||
public void accept(String retVal) {
|
||||
if (retVal != null) {
|
||||
Log.d(TAG,"Authentication Success - token is "+retVal);
|
||||
mUtil.showToast("Login Successful");
|
||||
saveAuthToken(retVal);
|
||||
updateUi();
|
||||
} else {
|
||||
Log.e(TAG,"onOk: Authentication failure for "+uname+", "+passwd);
|
||||
mUtil.showToast("ERROR: Authentication Failed - Please Try Again");
|
||||
mUtil.writeToSysLogFile("AuthActivity - Authorisation failed for "+uname+", "+passwd);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -225,6 +273,7 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onLogout");
|
||||
if (LogManager.USE_FIREBASE_BACKEND) {
|
||||
AuthUI.getInstance()
|
||||
.signOut(getApplicationContext())
|
||||
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@@ -233,6 +282,15 @@ public class AuthenticateActivity extends AppCompatActivity {
|
||||
updateUi();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
if (mWac != null) {
|
||||
mWac.logout();
|
||||
saveAuthToken(null);
|
||||
} else {
|
||||
Log.e(TAG,"logout() - mWac is null - not doing anything");
|
||||
}
|
||||
}
|
||||
updateUi();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -130,7 +130,7 @@ public class LogManager {
|
||||
mWac = new WebApiConnection_osdapi(mContext);
|
||||
}
|
||||
|
||||
//mWac.setStoredToken(mAuthToken);
|
||||
mWac.setStoredToken(mAuthToken);
|
||||
|
||||
if (mLogRemote) {
|
||||
Log.i(TAG, "Starting Remote Log Timer");
|
||||
|
||||
@@ -624,24 +624,27 @@ public class LogManagerControlActivity extends AppCompatActivity {
|
||||
|
||||
// Convert date format to something more readable.
|
||||
TextView tv = (TextView) v.findViewById(R.id.event_date_remote_tv);
|
||||
Date dataTime = null;
|
||||
try {
|
||||
//SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
//Date dataTime = dateFormat.parse(dataItem.get("dataTime").toString());
|
||||
Long tstamp = Long.parseLong((String) dataItem.get("dataTime"));
|
||||
Date dataTime = new Date(tstamp);
|
||||
dataTime = new Date(tstamp);
|
||||
} catch (NumberFormatException e) {
|
||||
Log.v(TAG, "remoteEventsAdapter.getView: Error Parsing dataDate as Long: " + e.getLocalizedMessage()+" trying as string");
|
||||
try {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
|
||||
dataTime = dateFormat.parse(dataItem.get("dataTime").toString());
|
||||
} catch (ParseException e2) {
|
||||
Log.e(TAG, "remoteEventsAdapter.getView: Error Parsing dataDate " + e2.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
if (dataTime != null) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
|
||||
tv.setText(dateFormat.format(dataTime));
|
||||
} catch (NumberFormatException e) {
|
||||
Log.e(TAG, "remoteEventsAdapter.getView: Error Parsing dataDate " + e.getLocalizedMessage());
|
||||
} else {
|
||||
tv.setText("---");
|
||||
}
|
||||
|
||||
|
||||
return (v);
|
||||
}
|
||||
}
|
||||
|
||||
;
|
||||
|
||||
|
||||
}
|
||||
@@ -46,6 +46,8 @@ public abstract class WebApiConnection {
|
||||
private Context mContext;
|
||||
private OsdUtil mUtil;
|
||||
private String TAG = "WebApiConnection";
|
||||
private String mAuthToken;
|
||||
|
||||
|
||||
|
||||
public interface JSONObjectCallback {
|
||||
@@ -109,4 +111,26 @@ public abstract class WebApiConnection {
|
||||
*/
|
||||
public abstract boolean checkServerConnection();
|
||||
|
||||
public abstract boolean getUserProfile(JSONObjectCallback callback);
|
||||
|
||||
|
||||
public boolean authenticate(final String uname, final String passwd, StringCallback callback) {
|
||||
Log.e(TAG,"WebApiConnection.authenticate(username, password, callback) Not Implemented");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Remove the stored token so future calls are not authenticated.
|
||||
public void logout() {
|
||||
Log.v(TAG, "logout()");
|
||||
setStoredToken(null);
|
||||
}
|
||||
|
||||
protected void setStoredToken(String authToken) {
|
||||
mAuthToken = authToken;
|
||||
}
|
||||
|
||||
protected String getStoredToken() {
|
||||
return (mAuthToken);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package uk.org.openseizuredetector;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
@@ -31,7 +32,7 @@ import java.util.Map;
|
||||
|
||||
|
||||
// This class is intended to handle all interactions with the OSD WebAPI
|
||||
public class WebApiConnection_firebase extends WebApiConnection{
|
||||
public class WebApiConnection_firebase extends WebApiConnection {
|
||||
public String retVal;
|
||||
public int retCode;
|
||||
public boolean mServerConnectionOk = false;
|
||||
@@ -85,6 +86,28 @@ public class WebApiConnection_firebase extends WebApiConnection{
|
||||
}
|
||||
}
|
||||
|
||||
public boolean getUserProfile(JSONObjectCallback callback) {
|
||||
Log.v(TAG, "getUserProfile()");
|
||||
FirebaseAuth auth = FirebaseAuth.getInstance();
|
||||
if (!isLoggedIn()) {
|
||||
Log.v(TAG, "not logged in - doing nothing");
|
||||
return (false);
|
||||
} else {
|
||||
try {
|
||||
JSONObject retObj = new JSONObject();
|
||||
retObj.put("id",auth.getCurrentUser().getUid());
|
||||
retObj.put("username", auth.getCurrentUser().getDisplayName());
|
||||
retObj.put("email", auth.getCurrentUser().getEmail());
|
||||
callback.accept(retObj);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "Error Creating retObjObj: " + e.getMessage());
|
||||
mUtil.showToast("Error Creating retObj - this should not happen!!!");
|
||||
return (false);
|
||||
}
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
public String getStoredToken() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -32,7 +32,6 @@ public class WebApiConnection_osdapi extends WebApiConnection {
|
||||
public boolean mServerConnectionOk = false;
|
||||
private String mUrlBase = "https://osdApi.ddns.net";
|
||||
private String TAG = "WebApiConnection_osdapi";
|
||||
private String mAuthToken;
|
||||
private Context mContext;
|
||||
private OsdUtil mUtil;
|
||||
RequestQueue mQueue;
|
||||
@@ -57,6 +56,7 @@ public class WebApiConnection_osdapi extends WebApiConnection {
|
||||
* @param callback - call back function callback(String retVal)
|
||||
* @return true if request sent, or false if failed to send request.
|
||||
*/
|
||||
@Override
|
||||
public boolean authenticate(final String uname, final String passwd, StringCallback callback) {
|
||||
// NOTE: the 'final' keyword is necessary for uname and passwd to be accessible to getParams below - I don't know why!
|
||||
// We know that this command works, so we just need the Java equivalent:
|
||||
@@ -111,28 +111,17 @@ public class WebApiConnection_osdapi extends WebApiConnection {
|
||||
return (true);
|
||||
}
|
||||
|
||||
// Remove the stored token so future calls are not authenticated.
|
||||
public void logout() {
|
||||
Log.v(TAG, "logout()");
|
||||
setStoredToken(null);
|
||||
//saveStoredToken(null);
|
||||
}
|
||||
|
||||
public void setStoredToken(String authToken) {
|
||||
mAuthToken = authToken;
|
||||
}
|
||||
|
||||
private String getStoredToken() {
|
||||
return (mAuthToken);
|
||||
}
|
||||
|
||||
public boolean isLoggedIn() {
|
||||
String authToken = getStoredToken();
|
||||
//Log.v(TAG, "isLoggedIn(): token=" + authToken);
|
||||
Log.v(TAG, "isLoggedIn(): token=" + authToken);
|
||||
if (authToken == null || authToken.length() == 0) {
|
||||
//Log.v(TAG, "isLogged in - not logged in");
|
||||
Log.v(TAG, "isLogged in - not logged in");
|
||||
return (false);
|
||||
} else {
|
||||
Log.v(TAG,"isLoggedIn - logged in ok");
|
||||
return (true);
|
||||
}
|
||||
|
||||
@@ -543,7 +532,7 @@ public class WebApiConnection_osdapi extends WebApiConnection {
|
||||
JSONObject retObj = new JSONObject(response);
|
||||
callback.accept(retObj);
|
||||
} catch (JSONException e) {
|
||||
Log.e(TAG, "getUserProfile.onRespons(): Error: " + e.getMessage() + "," + e.toString());
|
||||
Log.e(TAG, "getUserProfile.onResponse(): Error: " + e.getMessage() + "," + e.toString());
|
||||
callback.accept(null);
|
||||
}
|
||||
mServerConnectionOk = true;
|
||||
|
||||
@@ -55,6 +55,59 @@
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/login_osdapi_ui"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/username"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="4dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginRight="4dp"
|
||||
android:layout_marginBottom="4dp"
|
||||
android:hint="username" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/password"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="4dp"
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_marginRight="4dp"
|
||||
android:layout_marginBottom="16dp"
|
||||
android:fontFamily="sans-serif"
|
||||
android:hint="password"
|
||||
android:inputType="textPassword" />
|
||||
|
||||
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
>
|
||||
|
||||
<Button
|
||||
android:id="@+id/RegisterBtn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/register" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/ResetPasswordBtn"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/reset_password" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/logout_ui"
|
||||
android:layout_width="fill_parent"
|
||||
@@ -146,4 +199,8 @@
|
||||
android:text="@string/privacy_policy" />
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</LinearLayout>
|
||||
Reference in New Issue
Block a user