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

View File

@@ -16,8 +16,13 @@ import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
public class AuthenticateActivity extends AppCompatActivity {
private String TAG = "AuthenticateActivity";
private EditText mUnameEt;
@@ -71,7 +76,6 @@ public class AuthenticateActivity extends AppCompatActivity {
super.onStart();
mUtil.bindToServer(getApplicationContext(), mConnection);
waitForConnection();
updateUi();
}
@Override
@@ -103,6 +107,7 @@ public class AuthenticateActivity extends AppCompatActivity {
private void initialiseServiceConnection() {
mLm = mConnection.mSdServer.mLm;
mWac = mConnection.mSdServer.mLm.mWac;
updateUi();
}
private void updateUi() {
@@ -125,8 +130,25 @@ public class AuthenticateActivity extends AppCompatActivity {
Log.v(TAG, "Already Logged in - showing Log Out prompt");
loginLl.setVisibility(View.GONE);
logoutLl.setVisibility(View.VISIBLE);
TextView tv = (TextView)findViewById(R.id.tokenTv);
tv.setText("Logged in with Token:"+storedAuthToken);
//TextView tv = (TextView)findViewById(R.id.tokenTv);
//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.
* Calls the specified callback function, passing a JSONObject as a parameter when the data has been received and parsed.