diff --git a/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java b/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java index 5ac970d..86745b3 100644 --- a/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java +++ b/app/src/main/java/uk/org/openseizuredetector/AuthenticateActivity.java @@ -12,6 +12,7 @@ import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; +import android.widget.TextView; public class AuthenticateActivity extends AppCompatActivity implements AuthCallbackInterface { private String TAG = "AuthenticateActivity"; @@ -31,6 +32,11 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb cancelBtn.setOnClickListener(onCancel); Button OKBtn = (Button) findViewById(R.id.OKBtn); OKBtn.setOnClickListener(onOK); + Button logoutCancelBtn = + (Button) findViewById(R.id.logoutCancelBtn); + logoutCancelBtn.setOnClickListener(onCancel); + Button logoutBtn = (Button)findViewById(R.id.logoutBtn); + logoutBtn.setOnClickListener(onLogout); mUnameEt = (EditText) findViewById(R.id.username); mPasswdEt = (EditText) findViewById(R.id.password); @@ -40,15 +46,15 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb @Override protected void onStart() { super.onStart(); - switchUi(); + updateUi(); } public void authCallback(boolean authSuccess, String tokenStr) { Log.v(TAG,"authCallback"); - switchUi(); + updateUi(); } - private void switchUi() { + private void updateUi() { SharedPreferences prefs; String storedAuthToken; LinearLayout loginLl = (LinearLayout)findViewById(R.id.login_ui); @@ -67,6 +73,8 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb 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); } } @@ -95,5 +103,15 @@ public class AuthenticateActivity extends AppCompatActivity implements AuthCallb } }; + View.OnClickListener onLogout = + new View.OnClickListener() { + @Override + public void onClick(View view) { + Log.v(TAG, "onLogout"); + //m_status=false; + mWac.logout(); + updateUi(); + } + }; } \ No newline at end of file diff --git a/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java b/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java index 9f64c2a..0c76f66 100644 --- a/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java +++ b/app/src/main/java/uk/org/openseizuredetector/WebApiConnection.java @@ -51,9 +51,16 @@ public class WebApiConnection { new Response.Listener() { @Override public void onResponse(String response) { + String tokenStr; Log.v(TAG, "Response is: " + response); + try { + JSONObject jo = new JSONObject(response); + tokenStr = jo.getString("token"); + } catch (JSONException e) { + tokenStr = "Error Parsing Rsponse"; + } SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); - prefs.edit().putString("webApiAuthToken", response).commit(); + prefs.edit().putString("webApiAuthToken", tokenStr).commit(); mAuthCallback.authCallback(true, response); } }, @@ -82,4 +89,23 @@ public class WebApiConnection { return (true); } + // Remove the stored token so future calls are not authemticated. + public void logout() { + Log.v(TAG, "logout()"); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + prefs.edit().putString("webApiAuthToken", null).commit(); + } + + + // Create a new event in the remote database, based on the provided parameters. + public boolean createEvent() { + Log.v(TAG,"createEvent() - FIXME - This does not do anything!"); + + } + + public boolean createDatapoint() { + Log.v(TAG,"createDatapoint() - FIXME - This does not do anything!"); + + } + } diff --git a/app/src/main/res/layout/activity_authenticate.xml b/app/src/main/res/layout/activity_authenticate.xml index 4d3f5fa..d7b5149 100644 --- a/app/src/main/res/layout/activity_authenticate.xml +++ b/app/src/main/res/layout/activity_authenticate.xml @@ -4,16 +4,10 @@ android:layout_height="fill_parent" android:orientation="vertical"> - -