Starting on an Autenticate activity to obtain access token - the POST parameters are not working yet....

This commit is contained in:
Graham Jones
2021-12-12 06:21:19 +00:00
parent 276c9d7d1c
commit 0a405af092
8 changed files with 304 additions and 9 deletions

View File

@@ -32,6 +32,14 @@ dependencies {
implementation 'com.getpebble:pebblekit:3.1.0@aar' implementation 'com.getpebble:pebblekit:3.1.0@aar'
// Unit testing dependencies // Unit testing dependencies
implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support.constraint:constraint-layout:1.1.3'
//implementation 'androidx.appcompat:appcompat:1.4.0'
//implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
//implementation 'androidx.appcompat:appcompat:1.2.0'
//implementation 'com.google.android.material:material:1.1.0'
//implementation 'androidx.annotation:annotation:1.1.0'
//implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
//implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'
//implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
// Set this dependency if you want to use Mockito // Set this dependency if you want to use Mockito
testImplementation 'org.mockito:mockito-core:1.10.19' testImplementation 'org.mockito:mockito-core:1.10.19'
@@ -47,6 +55,7 @@ dependencies {
implementation 'com.google.android.gms:play-services-location:10.0.0' implementation 'com.google.android.gms:play-services-location:10.0.0'
//implementation 'com.github.RohitSurwase.UCE-Handler:uce_handler:1.3' //implementation 'com.github.RohitSurwase.UCE-Handler:uce_handler:1.3'
testImplementation 'org.robolectric:robolectric:4.3' testImplementation 'org.robolectric:robolectric:4.3'
implementation 'com.android.volley:volley:1.2.1'
} }

View File

@@ -28,19 +28,19 @@
<uses-feature <uses-feature
android:name="android.hardware.telephony" android:name="android.hardware.telephony"
android:required="false" /> android:required="false" />
<uses-feature
<uses-feature android:name="android.hardware.bluetooth_le" android:required="false" /> android:name="android.hardware.bluetooth_le"
android:required="false" />
<application <application
android:icon="@drawable/star_of_life_48x48" android:icon="@drawable/star_of_life_48x48"
android:label="@string/app_name" android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config" android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme" android:theme="@style/AppTheme">
> <!--@android:style/Theme.Holo.Light"--> <activity android:name=".AuthenticateActivity"></activity>
<activity android:name=".BLEScanActivity"></activity> <!-- @android:style/Theme.Holo.Light" -->
<activity android:name=".DBQueryActivity"></activity> <activity android:name=".BLEScanActivity" />
<!-- android:usesCleartextTraffic="true" --> <activity android:name=".DBQueryActivity" /> <!-- android:usesCleartextTraffic="true" -->
<activity android:name=".StartupActivity"> <activity android:name=".StartupActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@@ -0,0 +1,94 @@
package uk.org.openseizuredetector;
//import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class AuthenticateActivity extends AppCompatActivity {
private String TAG = "AuthenticateActivity";
private Context mContext;
private EditText mUnameEt;
private EditText mPasswdEt;
private boolean mIsLoggedIn;
private WebApiConnection mWac;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v(TAG, "onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_authenticate);
Button cancelBtn =
(Button) findViewById(R.id.cancelBtn);
cancelBtn.setOnClickListener(onCancel);
Button OKBtn = (Button) findViewById(R.id.OKBtn);
OKBtn.setOnClickListener(onOK);
mUnameEt = (EditText) findViewById(R.id.username);
mPasswdEt = (EditText) findViewById(R.id.password);
mWac = new WebApiConnection(this);
}
@Override
protected void onStart() {
super.onStart();
switchUi();
}
private void switchUi() {
SharedPreferences prefs;
String storedAuthToken;
LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui);
LinearLayout logoutLl = (LinearLayout)findViewById(R.id.logout_ui);
Log.i(TAG, "switchUi()");
prefs = PreferenceManager.getDefaultSharedPreferences(this);
storedAuthToken = (prefs.getString("webApiAuthToken", null));
Log.v(TAG, "storedAuthToken=" + storedAuthToken);
// Check if we are already logged in
if (storedAuthToken == null || storedAuthToken.length() == 0) {
Log.v(TAG, "Not Logged in - showing log in UI");
loginLl.setVisibility(View.VISIBLE);
logoutLl.setVisibility(View.GONE);
} else {
Log.v(TAG, "Already Logged in - showing Log Out prompt");
loginLl.setVisibility(View.GONE);
logoutLl.setVisibility(View.VISIBLE);
}
}
View.OnClickListener onCancel =
new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.v(TAG, "onCancel");
//m_status=false;
finish();
}
};
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);
mWac.authenticate(uname,passwd);
//finish();
}
};
}

View File

@@ -244,6 +244,17 @@ public class MainActivity extends AppCompatActivity {
mConnection.mSdServer.sendPhoneAlarm(); mConnection.mSdServer.sendPhoneAlarm();
} }
return true; return true;
case R.id.action_authenticate_api:
Log.i(TAG, "action_autheticate_api");
try {
Intent i = new Intent(
MainActivity.this,
AuthenticateActivity.class);
this.startActivity(i);
} catch (Exception ex) {
Log.i(TAG, "exception starting export activity " + ex.toString());
}
return true;
case R.id.action_export: case R.id.action_export:
Log.i(TAG, "action_export"); Log.i(TAG, "action_export");
try { try {

View File

@@ -0,0 +1,66 @@
package uk.org.openseizuredetector;
import android.content.Context;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
// This class is intended to handle all interactions with the OSD WebAPI
public class WebApiConnection {
public String retVal;
public int retCode;
private String mUrlBase = "https://osdApi.ddns.net";
private String TAG = "WebApiConnection";
RequestQueue mQueue;
public WebApiConnection(Context context) {
mQueue = Volley.newRequestQueue(context);
}
public boolean authenticate(String uname, String passwd) {
// We know that this command works, so we just need the Java equivalent:
// curl -X POST -d 'login=graham4&password=testpwd1' https://osdapi.ddns.net/api/accounts/login/
String urlStr = mUrlBase + "/api/accounts/login/";
Log.v(TAG, "urlStr=" + urlStr);
JSONObject postData = new JSONObject();
try {
postData.put("login", uname);
postData.put("password", passwd);
} catch (JSONException e) {
Log.e(TAG,e.getMessage());
}
Log.v(TAG,postData.toString());
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, urlStr, postData,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Display the first 500 characters of the response string.
Log.v(TAG, "Response is: " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String responseBody = new String(error.networkResponse.data);
Log.v(TAG, "Login Error" + error.toString()+","+error.getMessage()+error.networkResponse.statusCode+","+responseBody);
}
}
);
mQueue.add(req);
return (true);
}
}

View File

@@ -0,0 +1,104 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/versionTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android OpenSeizureDetector Version x.xx" />
<ImageView
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name"
android:scaleType="center"
android:src="@drawable/star_of_life_24x24" />
<LinearLayout
android:id="@+id/login_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/cancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cancel" />
<Button
android:id="@+id/OKBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/authenticate" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/logout_ui"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tokenTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/logged_in_with_token" />
<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/logoutCancelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/cancel" />
<Button
android:id="@+id/logoutBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/logout" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

View File

@@ -61,6 +61,14 @@
android:title="@string/data_log_manager" android:title="@string/data_log_manager"
/> />
<item
android:enabled="true"
android:id="@+id/action_authenticate_api"
android:icon="@drawable/ic_action_settings"
android:showAsAction="never|withText"
android:title="@string/authenticate"
/>
<item <item
android:id="@+id/action_logs" android:id="@+id/action_logs"
android:icon="@drawable/ic_action_settings" android:icon="@drawable/ic_action_settings"

View File

@@ -291,4 +291,7 @@
<string name="O2SatNullAlarmTitle">Treat Null Value as Alarm</string> <string name="O2SatNullAlarmTitle">Treat Null Value as Alarm</string>
<string name="O2SatThreshMinTitle">Oxygen Saturation Low Alarm Level (%)</string> <string name="O2SatThreshMinTitle">Oxygen Saturation Low Alarm Level (%)</string>
<string name="O2SatThreshMinSummary">O2 Saturation Low Alarm Level (%)</string> <string name="O2SatThreshMinSummary">O2 Saturation Low Alarm Level (%)</string>
<string name="title_activity_authenticate">AuthenticateActivity</string>
<string name="logout">Log Out</string>
<string name="logged_in_with_token">Logged in with Token:</string>
</resources> </resources>