Version 4.0.0k - Added userId and username to authenticate activity so user knows who they are logged in as.

This commit is contained in:
Graham Jones
2022-02-25 21:42:10 +00:00
parent 070fdd9aec
commit 26690efa11
8 changed files with 135 additions and 16 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -3,7 +3,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
package="uk.org.openseizuredetector" package="uk.org.openseizuredetector"
android:versionCode="92" android:versionCode="92"
android:versionName="4.0.0j"> android:versionName="4.0.0k">
<!-- android:allowBackup="false" --> <!-- android:allowBackup="false" -->
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

View File

@@ -16,8 +16,13 @@ import android.widget.EditText;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class AuthenticateActivity extends AppCompatActivity { public class AuthenticateActivity extends AppCompatActivity {
private String TAG = "AuthenticateActivity"; private String TAG = "AuthenticateActivity";
private EditText mUnameEt; private EditText mUnameEt;
@@ -71,7 +76,6 @@ public class AuthenticateActivity extends AppCompatActivity {
super.onStart(); super.onStart();
mUtil.bindToServer(getApplicationContext(), mConnection); mUtil.bindToServer(getApplicationContext(), mConnection);
waitForConnection(); waitForConnection();
updateUi();
} }
@Override @Override
@@ -103,6 +107,7 @@ public class AuthenticateActivity extends AppCompatActivity {
private void initialiseServiceConnection() { private void initialiseServiceConnection() {
mLm = mConnection.mSdServer.mLm; mLm = mConnection.mSdServer.mLm;
mWac = mConnection.mSdServer.mLm.mWac; mWac = mConnection.mSdServer.mLm.mWac;
updateUi();
} }
private void updateUi() { private void updateUi() {
@@ -125,8 +130,25 @@ public class AuthenticateActivity extends AppCompatActivity {
Log.v(TAG, "Already Logged in - showing Log Out prompt"); Log.v(TAG, "Already Logged in - showing Log Out prompt");
loginLl.setVisibility(View.GONE); loginLl.setVisibility(View.GONE);
logoutLl.setVisibility(View.VISIBLE); logoutLl.setVisibility(View.VISIBLE);
TextView tv = (TextView)findViewById(R.id.tokenTv); //TextView tv = (TextView)findViewById(R.id.tokenTv);
tv.setText("Logged in with Token:"+storedAuthToken); //tv.setText("Logged in with Token: "+storedAuthToken);
if (mWac != null) {
mWac.getUserProfile((JSONObject profileObj) -> {
try {
Long userId = profileObj.getLong("id");
String userName = profileObj.getString("username");
TextView tv2 = (TextView) findViewById(R.id.userIdTv);
tv2.setText(userId.toString());
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.i(TAG,"UpdateUI - not retrieving profile because mWac is null");
}
} }
} }

View File

@@ -532,6 +532,66 @@ public class WebApiConnection {
} }
/**
* Retieve the user profile of the authenticated user from the server, and return it to the callback function.
* @param callback - function to be called with a JSONObject as a parameter that contains the user profile data.
* @return true if request sent successfully, or else false.
*/
public boolean getUserProfile(Consumer<JSONObject> callback) {
Log.v(TAG, "getUserProfile()");
String urlStr = mUrlBase + "/api/accounts/profile/";
Log.v(TAG, "getUserProfile(): urlStr=" + urlStr);
final String authtoken = getStoredToken();
if (!isLoggedIn()) {
Log.v(TAG, "not logged in - doing nothing");
return (false);
}
StringRequest req = new StringRequest(Request.Method.GET, urlStr,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.v(TAG, "Response is: " + response);
try {
JSONObject retObj = new JSONObject(response);
callback.accept(retObj);
} catch (JSONException e) {
Log.e(TAG, "getUserProfile.onRespons(): Error: " + e.getMessage() + "," + e.toString());
callback.accept(null);
}
mServerConnectionOk = true;
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error != null) {
String responseBody = new String(error.networkResponse.data);
Log.e(TAG, "Create Event Error: " + error.toString() + ", message:" + error.getMessage() + ", Response Code:" + error.networkResponse.statusCode + ", Response: " + responseBody);
} else {
Log.e(TAG, "Create Event Error: returned null response");
}
mServerConnectionOk = false;
callback.accept(null);
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("Authorization", "Token " + getStoredToken());
return params;
}
};
mQueue.add(req);
return (true);
}
/** /**
* Retrieve the file containing the standard event types from the server. * Retrieve the file containing the standard event types from the server.
* Calls the specified callback function, passing a JSONObject as a parameter when the data has been received and parsed. * Calls the specified callback function, passing a JSONObject as a parameter when the data has been received and parsed.

View File

@@ -20,8 +20,8 @@
<TextView <TextView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/login_to_osdapi"
android:text="@string/login_to_osdapi" /> android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout> </LinearLayout>
<LinearLayout <LinearLayout
@@ -99,13 +99,51 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/logged_in_as_user_id" />
<TextView
android:id="@+id/userIdTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="userId" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=" (" />
<TextView
android:id="@+id/usernameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="username" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text=")" />
</LinearLayout>
<!--<TextView
android:id="@+id/tokenTv" android:id="@+id/tokenTv"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/logged_in_with_token" /> android:text="@string/logged_in_with_token" />
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"

View File

@@ -2,13 +2,11 @@
<resources> <resources>
<string name="app_name">OpenSeizureDetector</string> <string name="app_name">OpenSeizureDetector</string>
<string name="changelog"> <string name="changelog">
"V4.0 - Added logging of data to remote server "\nV4.0.0k - Added user name and user ID to login activity so the user knows who they are logged in as.
\nV3.7.0 - Added support for Garmin Blood Oxygen Saturation measurements \nV4.0.0j - Added event ID to list in data log manager so users can refer to it when asking for assistance.
\nV3.6.2 - Fix of issue with log file permissions on some Android 10 devices and added more translatable strings with polish translation. \n Moved system log to sqlite database and added list of it to data log manager activity.
\nV3.6.1 - Possible fix for issue with shutting down system and expanded Polish translation to all settings screens. \nV4.0.0i - Tidied up user settings and (hopefully) fixed issue with server not shutting down correctly sometimes.
\nV3.6 - Added phone sensor data source for testing without a watches "</string>
\nV3.5 - Added support for SMS Annunciator App
\nV3.4 - Added support for BLE Data Source "</string>
<string name="UpgradeMsg"> <string name="UpgradeMsg">
OpenSeizureDetector does not collect any personal data. OpenSeizureDetector does not collect any personal data.
This does mean that it is not possible for me to contact users if I find an This does mean that it is not possible for me to contact users if I find an
@@ -349,4 +347,5 @@
<string name="check_seizures_message">Please select the events highlighted in pink to say if they are real seizures or false alarms</string> <string name="check_seizures_message">Please select the events highlighted in pink to say if they are real seizures or false alarms</string>
<string name="error_server_not_running">ERROR: OpenSeizureDetector Server is not running - please re-start it</string> <string name="error_server_not_running">ERROR: OpenSeizureDetector Server is not running - please re-start it</string>
<string name="system_logs">System Logs</string> <string name="system_logs">System Logs</string>
<string name="logged_in_as_user_id">Logged in as User Id:</string>
</resources> </resources>