Added authorisation dialog - doesn't do anything yet, which is a shame considering how much code it took!
This commit is contained in:
78
app/src/main/java/uk/org/openseizuredetector/AuthDialog.java
Normal file
78
app/src/main/java/uk/org/openseizuredetector/AuthDialog.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
public class AuthDialog extends DialogFragment {
|
||||
private String TAG = "AuthDialog";
|
||||
private AuthDialogInterface mListener;
|
||||
private Context mContext;
|
||||
private EditText mUnameEt;
|
||||
private EditText mPasswdEt;
|
||||
|
||||
@Override
|
||||
public View onCreateView(
|
||||
LayoutInflater inflater,
|
||||
ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
Log.v(TAG, "onCreateView()");
|
||||
View v = inflater.inflate(R.layout.dialog_authenticate,
|
||||
container, false);
|
||||
Button cancelBtn =
|
||||
(Button) v.findViewById(R.id.cancelBtn);
|
||||
cancelBtn.setOnClickListener(onCancel);
|
||||
Button OKBtn = (Button) v.findViewById(R.id.OKBtn);
|
||||
OKBtn.setOnClickListener(onOK);
|
||||
|
||||
mUnameEt = (EditText) v.findViewById(R.id.username);
|
||||
mPasswdEt = (EditText) v.findViewById(R.id.password);
|
||||
|
||||
return v;
|
||||
|
||||
}
|
||||
|
||||
View.OnClickListener onCancel =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "onCancel");
|
||||
//m_status=false;
|
||||
mListener.onDialogDone(false);
|
||||
dismiss();
|
||||
}
|
||||
};
|
||||
|
||||
View.OnClickListener onOK =
|
||||
new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
//m_status=true;
|
||||
Log.v(TAG, "onOK()");
|
||||
String uname = mUnameEt.getText().toString();
|
||||
String passwd = mPasswdEt.getText().toString();
|
||||
Log.v(TAG,"onOK() - uname="+uname+", passwd="+passwd);
|
||||
mListener.onDialogDone(true);
|
||||
dismiss();
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
try {
|
||||
mListener = (AuthDialogInterface) context;
|
||||
} catch (ClassCastException e) {
|
||||
throw new ClassCastException(context.toString()
|
||||
+ " must implement dialogDoneistener");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
public interface AuthDialogInterface {
|
||||
void onDialogDone(boolean state);
|
||||
}
|
||||
@@ -175,6 +175,21 @@ public class LogManager {
|
||||
uploadSdData();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Authenticate using the WebAPI to obtain a token for future API requests.
|
||||
* @param uname - user name
|
||||
* @param passwd - password
|
||||
*/
|
||||
public void authenticate(String uname, String passwd) {
|
||||
Log.v(TAG, "authenticate()");
|
||||
// FIXME - this does not work!!!!
|
||||
String dataStr = "data string to upload";
|
||||
//new PostDataTask().execute("http://" + mOSDUrl + ":8080/data", dataStr, mOSDUname, mOSDPasswd);
|
||||
new PostDataTask().execute("http://192.168.43.175:8765/datapoints/add", dataStr, mOSDUname, mOSDPasswd);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload a batch of seizure detector data records to the server..
|
||||
* Uses the UploadSdDataTask class to upload the data in the
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
package uk.org.openseizuredetector;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import uk.org.openseizuredetector.EventLogManager.EventLogListAdapter;
|
||||
import uk.org.openseizuredetector.EventLogManager.EventLogManager;
|
||||
import uk.org.openseizuredetector.EventLogManager.LogEntryModel;
|
||||
|
||||
public class LogManagerActivity extends Activity {
|
||||
|
||||
public class LogManagerActivity extends FragmentActivity
|
||||
implements AuthDialogInterface {
|
||||
private String TAG = "LogManagerActivity";
|
||||
private EventLogListAdapter mEventLogListAdapter;
|
||||
private ListView mEventLogListView;
|
||||
private EventLogManager mElm;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -28,15 +31,32 @@ public class LogManagerActivity extends Activity {
|
||||
lem.setDataJSON("[]");
|
||||
lem.setAlarmState(1);
|
||||
|
||||
mElm = new EventLogManager(this);
|
||||
mElm.addRow(lem);
|
||||
//mElm = new EventLogManager(this);
|
||||
//mElm.addRow(lem);
|
||||
|
||||
mEventLogListAdapter = new EventLogListAdapter(this);
|
||||
mEventLogListView = (ListView) findViewById(R.id.eventLogListView);
|
||||
mEventLogListView.setAdapter(mEventLogListAdapter);
|
||||
//mEventLogListAdapter = new EventLogListAdapter(this);
|
||||
//mEventLogListView = (ListView) findViewById(R.id.eventLogListView);
|
||||
//mEventLogListView.setAdapter(mEventLogListAdapter);
|
||||
|
||||
Button b;
|
||||
|
||||
b = (Button) findViewById(R.id.authenticate_button);
|
||||
b.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
Log.v(TAG, "authenticate button clicked");
|
||||
AuthDialog authDialog = new AuthDialog();
|
||||
authDialog.show(getSupportFragmentManager(),"authDialog");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void updateUi() {
|
||||
Log.v(TAG, "updateUi");
|
||||
}
|
||||
|
||||
}
|
||||
public void onDialogDone(boolean State) {
|
||||
Log.v(TAG,"onDialogDOne()");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -266,6 +266,17 @@ public class MainActivity extends AppCompatActivity {
|
||||
Log.i(TAG, "exception starting log manager activity " + ex.toString());
|
||||
}
|
||||
return true;
|
||||
case R.id.action_logmanager:
|
||||
Log.i(TAG, "action_logmanager");
|
||||
try {
|
||||
Intent intent = new Intent(
|
||||
MainActivity.this,
|
||||
LogManagerActivity.class);
|
||||
this.startActivity(intent);
|
||||
} catch (Exception ex) {
|
||||
Log.i(TAG, "exception starting log manager activity " + ex.toString());
|
||||
}
|
||||
return true;
|
||||
case R.id.action_settings:
|
||||
Log.i(TAG, "action_settings");
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package uk.org.openseizuredetector.data;
|
||||
|
||||
import uk.org.openseizuredetector.data.model.LoggedInUser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Class that handles authentication w/ login credentials and retrieves user information.
|
||||
*/
|
||||
public class LoginDataSource {
|
||||
|
||||
public Result<LoggedInUser> login(String username, String password) {
|
||||
|
||||
try {
|
||||
// TODO: handle loggedInUser authentication
|
||||
LoggedInUser fakeUser =
|
||||
new LoggedInUser(
|
||||
java.util.UUID.randomUUID().toString(),
|
||||
"Jane Doe");
|
||||
return new Result.Success<>(fakeUser);
|
||||
} catch (Exception e) {
|
||||
return new Result.Error(new IOException("Error logging in", e));
|
||||
}
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
// TODO: revoke authentication
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package uk.org.openseizuredetector.data;
|
||||
|
||||
import uk.org.openseizuredetector.data.model.LoggedInUser;
|
||||
|
||||
/**
|
||||
* Class that requests authentication and user information from the remote data source and
|
||||
* maintains an in-memory cache of login status and user credentials information.
|
||||
*/
|
||||
public class LoginRepository {
|
||||
|
||||
private static volatile LoginRepository instance;
|
||||
|
||||
private LoginDataSource dataSource;
|
||||
|
||||
// If user credentials will be cached in local storage, it is recommended it be encrypted
|
||||
// @see https://developer.android.com/training/articles/keystore
|
||||
private LoggedInUser user = null;
|
||||
|
||||
// private constructor : singleton access
|
||||
private LoginRepository(LoginDataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
public static LoginRepository getInstance(LoginDataSource dataSource) {
|
||||
if (instance == null) {
|
||||
instance = new LoginRepository(dataSource);
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public boolean isLoggedIn() {
|
||||
return user != null;
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
user = null;
|
||||
dataSource.logout();
|
||||
}
|
||||
|
||||
private void setLoggedInUser(LoggedInUser user) {
|
||||
this.user = user;
|
||||
// If user credentials will be cached in local storage, it is recommended it be encrypted
|
||||
// @see https://developer.android.com/training/articles/keystore
|
||||
}
|
||||
|
||||
public Result<LoggedInUser> login(String username, String password) {
|
||||
// handle login
|
||||
Result<LoggedInUser> result = dataSource.login(username, password);
|
||||
if (result instanceof Result.Success) {
|
||||
setLoggedInUser(((Result.Success<LoggedInUser>) result).getData());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package uk.org.openseizuredetector.data;
|
||||
|
||||
/**
|
||||
* A generic class that holds a result success w/ data or an error exception.
|
||||
*/
|
||||
public class Result<T> {
|
||||
// hide the private constructor to limit subclass types (Success, Error)
|
||||
private Result() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (this instanceof Result.Success) {
|
||||
Result.Success success = (Result.Success) this;
|
||||
return "Success[data=" + success.getData().toString() + "]";
|
||||
} else if (this instanceof Result.Error) {
|
||||
Result.Error error = (Result.Error) this;
|
||||
return "Error[exception=" + error.getError().toString() + "]";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Success sub-class
|
||||
public final static class Success<T> extends Result {
|
||||
private T data;
|
||||
|
||||
public Success(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public T getData() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
|
||||
// Error sub-class
|
||||
public final static class Error extends Result {
|
||||
private Exception error;
|
||||
|
||||
public Error(Exception error) {
|
||||
this.error = error;
|
||||
}
|
||||
|
||||
public Exception getError() {
|
||||
return this.error;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package uk.org.openseizuredetector.data.model;
|
||||
|
||||
/**
|
||||
* Data class that captures user information for logged in users retrieved from LoginRepository
|
||||
*/
|
||||
public class LoggedInUser {
|
||||
|
||||
private String userId;
|
||||
private String displayName;
|
||||
|
||||
public LoggedInUser(String userId, String displayName) {
|
||||
this.userId = userId;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
public String getUserId() {
|
||||
return userId;
|
||||
}
|
||||
|
||||
public String getDisplayName() {
|
||||
return displayName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user