Commit 88c4957ae4bb47f124943ed4f01ee142ac5da52a

Authored by Ali B
2 parents 3c1d864a f5727fb6

Merge branch 'login_ali'

# Conflicts:
#	Risiko/app/src/main/java/activity/Incident.java
#	Risiko/app/src/main/java/api/Endpoints.java
#	Risiko/app/src/main/res/values/strings.xml
#	restApi/.idea/workspace.xml
Showing 62 changed files with 2145 additions and 79 deletions   Show diff stats
Risiko/.idea/caches/build_file_checksums.ser
No preview for this file type
Risiko/.idea/misc.xml
... ... @@ -5,11 +5,12 @@
5 5 <option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
6 6 <option name="myNullables">
7 7 <value>
8   - <list size="4">
  8 + <list size="5">
9 9 <item index="0" class="java.lang.String" itemvalue="org.jetbrains.annotations.Nullable" />
10 10 <item index="1" class="java.lang.String" itemvalue="javax.annotation.Nullable" />
11   - <item index="2" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
12   - <item index="3" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
  11 + <item index="2" class="java.lang.String" itemvalue="javax.annotation.CheckForNull" />
  12 + <item index="3" class="java.lang.String" itemvalue="edu.umd.cs.findbugs.annotations.Nullable" />
  13 + <item index="4" class="java.lang.String" itemvalue="android.support.annotation.Nullable" />
13 14 </list>
14 15 </value>
15 16 </option>
... ...
Risiko/app/src/main/AndroidManifest.xml
... ... @@ -18,6 +18,11 @@
18 18 <category android:name="android.intent.category.LAUNCHER" />
19 19 </intent-filter>
20 20 </activity>
  21 +
  22 + <service
  23 + android:name="api.APILoginService"
  24 + android:exported="false" />
  25 +
21 26 <activity android:name="activity.MainMenu" />
22 27 <activity android:name="activity.Activities" />
23 28 <activity android:name="activity.Incident" />
... ...
Risiko/app/src/main/java/activity/Activities.java
... ... @@ -12,10 +12,10 @@ import android.widget.Toast;
12 12  
13 13 import com.risiko.risiko.R;
14 14  
15   -import java.util.ArrayList;
16 15 import java.util.List;
17 16 import adapters.ListAdapterActivities;
18 17 import api.Api;
  18 +import api.TokenEncrypter;
19 19 import dataclasses.Activity;
20 20 import retrofit2.Call;
21 21 import retrofit2.Callback;
... ... @@ -23,6 +23,7 @@ import retrofit2.Response;
23 23  
24 24 public class Activities extends AppCompatActivity {
25 25 ListView lv;
  26 + String token;
26 27  
27 28 @Override
28 29 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -31,7 +32,9 @@ public class Activities extends AppCompatActivity {
31 32 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
32 33 setSupportActionBar(myToolbar);
33 34  
34   - Api.get().getAllActivities()
  35 + token = TokenEncrypter.getToken(this);
  36 +
  37 + Api.getInstance().getAllActivities()
35 38 .enqueue(new Callback<List<Activity>>() {
36 39 @Override
37 40 public void onResponse(Call<List<Activity>> call, Response<List<Activity>> response) {
... ...
Risiko/app/src/main/java/activity/EditIncident.java
... ... @@ -19,6 +19,7 @@ import java.util.HashMap;
19 19 import java.util.List;
20 20  
21 21 import api.Api;
  22 +import api.TokenEncrypter;
22 23 import dataclasses.Evaluation;
23 24 import dataclasses.Incident;
24 25 import retrofit2.Call;
... ... @@ -32,6 +33,7 @@ public class EditIncident extends AppCompatActivity {
32 33 private EditText etDescription;
33 34 private Button editIncident;
34 35 private Spinner spinnerAssociatedRisk;
  36 + private String token;
35 37  
36 38 @Override
37 39 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -40,6 +42,8 @@ public class EditIncident extends AppCompatActivity {
40 42 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
41 43 setSupportActionBar(myToolbar);
42 44  
  45 + token = TokenEncrypter.getToken(this);
  46 +
43 47 Intent intent = getIntent();
44 48 Bundle b = intent.getExtras();
45 49 String id = b.getString("chosenEventId");
... ... @@ -63,7 +67,7 @@ public class EditIncident extends AppCompatActivity {
63 67  
64 68 spinnerAssociatedRisk = findViewById(R.id.spinner_associated_risk);
65 69  
66   - Api.get().getAllEvaluations()
  70 + Api.getInstance().getAllEvaluations()
67 71 .enqueue(new Callback<List<Evaluation>>() {
68 72  
69 73 @Override
... ... @@ -86,7 +90,7 @@ public class EditIncident extends AppCompatActivity {
86 90 String newlocation = etLocation.getText().toString();
87 91 String newdescription = etDescription.getText().toString();
88 92  
89   - Api.get().editIncident(id, newdate, newlocation, newtitle, newdescription, associatedRisk).enqueue(new Callback<dataclasses.Incident>() {
  93 + Api.getInstance().editIncident(id, newdate, newlocation, newtitle, newdescription, associatedRisk).enqueue(new Callback<dataclasses.Incident>() {
90 94 @Override
91 95 public void onResponse(Call<Incident> call, Response<Incident> response) {
92 96 System.out.println(response);
... ...
Risiko/app/src/main/java/activity/Evaluations.java
1 1 package activity;
2 2  
3   -import android.content.DialogInterface;
4 3 import android.content.Intent;
5 4 import android.graphics.Typeface;
6 5 import android.support.v7.app.AlertDialog;
... ... @@ -25,6 +24,7 @@ import java.util.List;
25 24 import adapters.ListAdapterEvaluations;
26 25  
27 26 import api.Api;
  27 +import api.TokenEncrypter;
28 28 import database.DatabaseSource;
29 29 import dataclasses.Evaluation;
30 30 import retrofit2.Call;
... ... @@ -37,6 +37,7 @@ public class Evaluations extends AppCompatActivity {
37 37 Button btnNewEvaluation;
38 38 DatabaseSource dbs;
39 39 String id;
  40 + String token;
40 41  
41 42 @Override
42 43 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -45,6 +46,7 @@ public class Evaluations extends AppCompatActivity {
45 46 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
46 47 setSupportActionBar(myToolbar);
47 48  
  49 + token = TokenEncrypter.getToken(this);
48 50  
49 51 dbs = new DatabaseSource(this);
50 52  
... ... @@ -54,7 +56,7 @@ public class Evaluations extends AppCompatActivity {
54 56 System.out.println(activityId);
55 57  
56 58  
57   - Api.get().getAllEvaluations()
  59 + Api.getInstance().getAllEvaluations()
58 60 .enqueue(new Callback<List<Evaluation>>() {
59 61 @Override
60 62 public void onResponse(Call<List<Evaluation>> call, Response<List<Evaluation>> response) {
... ... @@ -205,7 +207,7 @@ public class Evaluations extends AppCompatActivity {
205 207 public void onClick(DialogInterface dialogInterface, int i) {
206 208 finish();
207 209  
208   - Api.get().deleteEvaluation(id).enqueue(new Callback<String>() {
  210 + Api.getInstance().deleteEvaluation(id).enqueue(new Callback<String>() {
209 211 @Override
210 212 public void onResponse(Call<String> call, Response<String> response) {
211 213 finish();
... ...
Risiko/app/src/main/java/activity/Incident.java
... ... @@ -12,7 +12,6 @@ import android.view.Menu;
12 12 import android.view.MenuItem;
13 13 import android.view.View;
14 14 import android.widget.Button;
15   -import android.widget.EditText;
16 15 import android.widget.ListView;
17 16 import android.widget.TextView;
18 17 import android.widget.Toast;
... ... @@ -30,6 +29,7 @@ import java.util.List;
30 29  
31 30 import adapters.ListAdapterIncidents;
32 31 import api.Api;
  32 +import api.TokenEncrypter;
33 33 import database.DatabaseSource;
34 34 import dataclasses.Evaluation;
35 35 import retrofit2.Call;
... ... @@ -41,6 +41,7 @@ public class Incident extends AppCompatActivity {
41 41 String chosenEventId;
42 42 DatabaseSource dbs;
43 43 String id;
  44 + String token;
44 45  
45 46 @Override
46 47 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -50,6 +51,7 @@ public class Incident extends AppCompatActivity {
50 51 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
51 52 setSupportActionBar(myToolbar);
52 53  
  54 + token = TokenEncrypter.getToken(this);
53 55  
54 56 dbs = new DatabaseSource(this);
55 57  
... ... @@ -58,7 +60,7 @@ public class Incident extends AppCompatActivity {
58 60 // insertEvents(incidentList);
59 61  
60 62  
61   - Api.get().getAllIncidents()
  63 + Api.getInstance().getAllIncidents()
62 64 .enqueue(new Callback<List<dataclasses.Incident>>() {
63 65  
64 66 @Override
... ... @@ -127,7 +129,7 @@ public class Incident extends AppCompatActivity {
127 129 editEvent.setOnClickListener(view -> editIncident(chosenItem.getId(), chosenItem.getTitle(), chosenItem.getDate(), chosenItem.getDescription(), chosenItem.getLocation(), chosenItem.getAssociatedeval()));
128 130 seeLog.setOnClickListener(view -> goToAccesslog(chosenEventId));
129 131  
130   - Api.get().getEvaluation(chosenItem.getAssociatedeval()).enqueue(new Callback<Evaluation>() {
  132 + Api.getInstance().getEvaluation(chosenItem.getAssociatedeval()).enqueue(new Callback<Evaluation>() {
131 133 @Override
132 134 public void onResponse(Call<Evaluation> call, Response<Evaluation> response) {
133 135 Evaluation evaluation = response.body();
... ... @@ -186,7 +188,7 @@ public class Incident extends AppCompatActivity {
186 188 @Override
187 189 public void onClick(DialogInterface dialogInterface, int i) {
188 190  
189   - Api.get().deleteIncident(chosenEventId).enqueue(new Callback<String>() {
  191 + Api.getInstance().deleteIncident(chosenEventId).enqueue(new Callback<String>() {
190 192 @Override
191 193 public void onResponse(Call<String> call, Response<String> response) {
192 194 finish();
... ...
Risiko/app/src/main/java/activity/MainPage.java
1 1 package activity;
2 2  
  3 +import android.content.BroadcastReceiver;
  4 +import android.content.Context;
3 5 import android.content.Intent;
  6 +import android.content.IntentFilter;
  7 +import android.support.v4.content.LocalBroadcastManager;
4 8 import android.support.v7.app.AppCompatActivity;
5 9 import android.os.Bundle;
  10 +import android.text.TextUtils;
6 11 import android.view.View;
7 12 import android.widget.Button;
  13 +import android.widget.EditText;
  14 +import android.widget.TextView;
  15 +import android.widget.Toast;
8 16  
9 17 import com.risiko.risiko.R;
10 18  
  19 +import api.APILoginService;
  20 +import api.Api;
  21 +import api.TokenEncrypter;
  22 +import dataclasses.User;
  23 +import retrofit2.Call;
  24 +import retrofit2.Callback;
  25 +import retrofit2.Response;
  26 +
11 27 public class MainPage extends AppCompatActivity {
12 28 Button toMainMenu;
  29 + private EditText etUsernameView;
  30 + private EditText etPasswordView;
  31 + private BroadCastReceiverLogin broadCastReceiverLogin;
13 32  
14 33 @Override
15 34 protected void onCreate(Bundle savedInstanceState) {
16 35 super.onCreate(savedInstanceState);
17 36 setContentView(R.layout.activity_main_page);
18 37 toMainMenu = findViewById(R.id.btn_to_main_menu);
19   - toMainMenu.setOnClickListener(view -> toMainMenuEvent());
  38 + toMainMenu.setOnClickListener(view -> attemptLogin());
  39 + etUsernameView = findViewById(R.id.et_username);
  40 + etPasswordView = findViewById(R.id.et_password);
  41 + setBroadCastReceiver();
20 42 }
21 43  
22 44 public void toMainMenuEvent(){
... ... @@ -24,4 +46,128 @@ public class MainPage extends AppCompatActivity {
24 46 startActivity(intent);
25 47 }
26 48  
  49 + /**
  50 + * This it the "login" method. It has a simple input check.
  51 + *
  52 + * First it checks the string values in password and username. If they pass the test the progress "loading" icon will be shown and the information will be sent to the LoginService.
  53 + *
  54 + */
  55 + private void attemptLogin() {
  56 + System.out.println("0");
  57 +
  58 + etUsernameView.setError(null);
  59 + etPasswordView.setError(null);
  60 +
  61 + String username = etUsernameView.getText().toString();
  62 + String password = etPasswordView.getText().toString();
  63 +
  64 + boolean cancel = false;
  65 + View focusView = null;
  66 +
  67 + // Checking password input
  68 + if (TextUtils.isEmpty(password) && !isPasswordValid(password)) {
  69 + System.out.println("1");
  70 + etPasswordView.setError(getString(R.string.error_invalid_password));
  71 + focusView = etPasswordView;
  72 + cancel = true;
  73 + }
  74 +
  75 + // Checking username input.
  76 + if (TextUtils.isEmpty(username)) {
  77 + System.out.println("2");
  78 + etUsernameView.setError(getString(R.string.error_field_required));
  79 + focusView = etUsernameView;
  80 + cancel = true;
  81 + } else if (!isUsernameValid(username)) {
  82 + System.out.println("2,1");
  83 + etUsernameView.setError(getString(R.string.error_invalid_username));
  84 + focusView = etUsernameView;
  85 + cancel = true;
  86 + }
  87 +
  88 + if (cancel) {
  89 + focusView.requestFocus();
  90 + } else {
  91 + System.out.println("3");
  92 + //Connecting to server and checking login credentials.
  93 + APILoginService.startActionLogin(this, etPasswordView.getText().toString(), etUsernameView.getText().toString());
  94 + }
  95 + }
  96 +
  97 + /**
  98 + * Set the broadcast receiver for login.
  99 + */
  100 + private void setBroadCastReceiver() {
  101 + broadCastReceiverLogin = new BroadCastReceiverLogin();
  102 + IntentFilter filter = new IntentFilter(BroadCastReceiverLogin.USER_LOGIN);
  103 + LocalBroadcastManager.getInstance(this).registerReceiver(broadCastReceiverLogin, filter);
  104 + }
  105 +
  106 + /**
  107 + * This is the Broadcast receiver for login. The LoginService will send a broadcast when the login credentials has been checked.'
  108 + * The onReceive will check the response and act accordingly.
  109 + */
  110 + public class BroadCastReceiverLogin extends BroadcastReceiver {
  111 + public static final String USER_LOGIN = "com.risiko.risiko.USER_LOGIN";
  112 +
  113 + @Override
  114 + public void onReceive(Context context, Intent intent) {
  115 + System.out.println("broadcast received");
  116 + onResponse(intent.getIntExtra("responseCode", 400));
  117 + }
  118 +
  119 + // Reacting on the server answer.
  120 + private void onResponse(int responseCode){
  121 + TextView tvSomethingWentWrong = findViewById(R.id.tv_wrongMessageLogin);
  122 + switch (responseCode) {
  123 +
  124 + // Something went wrong with login
  125 + case 400:
  126 + tvSomethingWentWrong.setVisibility(View.VISIBLE);
  127 + tvSomethingWentWrong.setText(R.string.something_went_wrong_with_login);
  128 + break;
  129 +
  130 + // Code for success
  131 + case 202:
  132 + toMainMenuEvent();
  133 + break;
  134 +
  135 + // Error from php server.
  136 + case 500:
  137 + tvSomethingWentWrong.setVisibility(View.VISIBLE);
  138 + tvSomethingWentWrong.setText(R.string.server_error);
  139 +
  140 + // Wrong password or username.
  141 + case 403:
  142 + tvSomethingWentWrong.setVisibility(View.VISIBLE);
  143 + tvSomethingWentWrong.setText(R.string.wrong_password_or_username);
  144 + break;
  145 + }
  146 + }
  147 + }
  148 +
  149 +
  150 +
  151 + /**
  152 + * @param password String password to check
  153 + *
  154 + * Simple password validation check
  155 + *
  156 + * @return true if password is valid.
  157 + */
  158 + public static boolean isPasswordValid(String password) {
  159 + return password.length() > 5;
  160 + }
  161 +
  162 + /**
  163 + * @param username
  164 + *
  165 + * Checks length of username, other checks can be added later if needed
  166 + *
  167 + * @return true if it is valid.
  168 + */
  169 + public static boolean isUsernameValid(String username) {
  170 + return username.length() > 3;
  171 + }
  172 +
27 173 }
28 174 \ No newline at end of file
... ...
Risiko/app/src/main/java/activity/NewEvaluation.java
... ... @@ -14,12 +14,11 @@ import android.widget.Toast;
14 14  
15 15 import com.risiko.risiko.R;
16 16  
17   -import java.util.HashMap;
18 17 import java.util.UUID;
19 18  
20 19 import api.Api;
  20 +import api.TokenEncrypter;
21 21 import database.DatabaseSource;
22   -import dataclasses.Evaluation;
23 22 import retrofit2.Call;
24 23 import retrofit2.Callback;
25 24 import retrofit2.Response;
... ... @@ -30,6 +29,7 @@ public class NewEvaluation extends AppCompatActivity {
30 29 private Spinner spinnerConsequence, spinnerProbability;
31 30 private Button btnNewEval;
32 31 DatabaseSource dbs;
  32 + String token;
33 33  
34 34 @Override
35 35 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -38,6 +38,7 @@ public class NewEvaluation extends AppCompatActivity {
38 38 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
39 39 setSupportActionBar(myToolbar);
40 40  
  41 + token = TokenEncrypter.getToken(this);
41 42  
42 43 Intent intent = getIntent();
43 44 String activityId = intent.getStringExtra("ID_OF_CHOSEN_ACTIVITY");
... ... @@ -61,7 +62,7 @@ public class NewEvaluation extends AppCompatActivity {
61 62 int consequenceValue = getConsequenceValue(consequence);
62 63 int probabilityValue = getProbabilityValue(probability);
63 64  
64   - Api.get().newEvaluation(title, UUID.randomUUID().toString() ,activityId,consequenceValue,action,outcome,probabilityValue).enqueue(new Callback<String>() {
  65 + Api.getInstance().newEvaluation(title, UUID.randomUUID().toString() ,activityId,consequenceValue,action,outcome,probabilityValue).enqueue(new Callback<String>() {
65 66 @Override
66 67 public void onResponse(Call<String> call, Response<String> response) {
67 68 System.out.println(response);
... ...
Risiko/app/src/main/java/activity/NewIncident.java
... ... @@ -21,6 +21,7 @@ import java.util.List;
21 21 import java.util.UUID;
22 22  
23 23 import api.Api;
  24 +import api.TokenEncrypter;
24 25 import database.DatabaseSource;
25 26 import dataclasses.Evaluation;
26 27 import dataclasses.Incident;
... ... @@ -35,6 +36,7 @@ public class NewIncident extends AppCompatActivity {
35 36 private Button btnNewIncident;
36 37 private DatabaseSource dbs;
37 38 private Spinner spinnerReadAccess;
  39 + private String token;
38 40  
39 41 @Override
40 42 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -43,6 +45,7 @@ public class NewIncident extends AppCompatActivity {
43 45 Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
44 46 setSupportActionBar(myToolbar);
45 47  
  48 + token = TokenEncrypter.getToken(this);
46 49  
47 50 etDate = findViewById(R.id.et_newIncident_date);
48 51 etLocation = findViewById(R.id.et_newIncident_location);
... ... @@ -67,7 +70,7 @@ public class NewIncident extends AppCompatActivity {
67 70  
68 71 // dbs.getAllEvaluation(evaluations);
69 72  
70   - Api.get().getAllEvaluations()
  73 + Api.getInstance().getAllEvaluations()
71 74 .enqueue(new Callback<List<Evaluation>>() {
72 75  
73 76 @Override
... ... @@ -94,7 +97,7 @@ public class NewIncident extends AppCompatActivity {
94 97 dataclasses.Incident newIncident = new Incident(id, date, location, title, description, associatedRisk);
95 98  
96 99  
97   - Api.get().newIncident(date, title, id, associatedRisk, description, location).enqueue(new Callback<String>() {
  100 + Api.getInstance().newIncident(date, title, id, associatedRisk, description, location).enqueue(new Callback<String>() {
98 101 @Override
99 102 public void onResponse(Call<String> call, Response<String> response) {
100 103 System.out.println(response);
... ...
Risiko/app/src/main/java/api/APILoginService.java 0 → 100644
  1 +package api;
  2 +
  3 +import android.app.IntentService;
  4 +import android.content.Context;
  5 +import android.content.Intent;
  6 +import android.content.SharedPreferences;
  7 +import android.support.v4.content.LocalBroadcastManager;
  8 +
  9 +import com.risiko.risiko.R;
  10 +
  11 +import activity.MainPage;
  12 +import retrofit2.Call;
  13 +import retrofit2.Callback;
  14 +import retrofit2.Response;
  15 +
  16 +
  17 +/**
  18 + * Service for login.
  19 + * Makes contact with API on stored URL. API checks for correct password and username.
  20 + * Sends broadcast with response. Does not handle repsonse in class.
  21 + */
  22 +public class APILoginService extends IntentService {
  23 +
  24 + private static final String ACTION_LOGIN = "risk.api.action.LOGIN";
  25 + private static final String EXTRA_PASSWORD = "risk.api.extra.PASSWORD";
  26 + private static final String EXTRA_USERNAME = "risk.api.extra.EMAIL";
  27 +
  28 + public APILoginService() {
  29 + super("APIServiceLogin");
  30 + }
  31 +
  32 + public static void startActionLogin(Context context, String password, String username) {
  33 + Intent intent = new Intent(context, APILoginService.class);
  34 + intent.setAction(ACTION_LOGIN);
  35 + intent.putExtra(EXTRA_PASSWORD, password);
  36 + intent.putExtra(EXTRA_USERNAME, username);
  37 + context.startService(intent);
  38 + }
  39 +
  40 + @Override
  41 + protected void onHandleIntent(Intent intent) {
  42 + if (intent != null) {
  43 + final String action = intent.getAction();
  44 + if (ACTION_LOGIN.equals(action)) {
  45 + final String password = intent.getStringExtra(EXTRA_PASSWORD);
  46 + final String username = intent.getStringExtra(EXTRA_USERNAME);
  47 + handleActionLogin(password, username);
  48 + }
  49 + }
  50 + }
  51 +
  52 + /**
  53 + * @param password Password to account to check
  54 + * @param username Username.
  55 + * @throws NullPointerException
  56 + *
  57 + * This method makes connection to API and sends request to check password and email.
  58 + * If correct it will send back a token, and response cod 201.
  59 + * If not correct it will only send back a response code.
  60 + */
  61 + private void handleActionLogin(String password, String username) throws NullPointerException {
  62 +
  63 +
  64 + Api.getInstance().login(username, password).enqueue(new Callback<String>() {
  65 + @Override
  66 + public void onResponse(Call<String> call, Response<String> response) {
  67 + if (response.code() == 202) {
  68 + String token = response.body();
  69 +
  70 + SharedPreferences sharedPref = getSharedPreferences(getString(R.string.packageName), Context.MODE_PRIVATE);
  71 + SharedPreferences.Editor edit = sharedPref.edit();
  72 + edit.clear();
  73 + edit.putString(getString(R.string.preference_name_token), encryptToken(token));
  74 + edit.commit();
  75 + sendBroadcast(response.code());
  76 + } else
  77 + sendBroadcast(response.code());
  78 + }
  79 +
  80 + @Override
  81 + public void onFailure(Call<String> call, Throwable t) {
  82 + System.out.println("Something went wrong");
  83 + System.out.println(t.getMessage());
  84 + }
  85 + });
  86 +
  87 +// HttpURLConnection urlConnection = null;
  88 +// URL url = null;
  89 +// BufferedReader reader;
  90 +// OutputStreamWriter writer = null;
  91 +//
  92 +// try {
  93 +//
  94 +// // Sending the post request with email and password to the server.
  95 +// url = new URL(getString(R.string.api_login_link));
  96 +// urlConnection = (HttpURLConnection) url.openConnection();
  97 +//
  98 +// urlConnection.setRequestMethod("POST");
  99 +// urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  100 +// urlConnection.setRequestProperty("charset", "utf-8");
  101 +// urlConnection.setDoOutput(true);
  102 +// writer = new OutputStreamWriter(urlConnection.getOutputStream());
  103 +// writer.write("email="+username+"&password="+password);
  104 +// writer.flush();
  105 +// writer.close();
  106 +//
  107 +// // The response code from the server.
  108 +// int responseCode = urlConnection.getResponseCode();
  109 +//
  110 +// // If correct email and password. Get the token from the server, encrypt it and store it in shared preferences.
  111 +// if(responseCode == 201) {
  112 +// reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
  113 +// String token = reader.readLine();
  114 +// reader.close();
  115 +//
  116 +// SharedPreferences sharedPref = getSharedPreferences(getString(R.string.packageName), Context.MODE_PRIVATE);
  117 +// SharedPreferences.Editor edit = sharedPref.edit();
  118 +// edit.clear();
  119 +// edit.putString(getString(R.string.preference_name_token), encryptToken(token));
  120 +// edit.commit();
  121 +// sendBroadcast(responseCode);
  122 +// }else{
  123 +// // In case of other answer than 201. Will be handled in the broadcast receiver.
  124 +// sendBroadcast(responseCode);
  125 +// }
  126 +//
  127 +// } catch (IOException e) {
  128 +// e.printStackTrace();
  129 +// }
  130 + }
  131 +
  132 + /**
  133 + * @param responseCode Response code from server
  134 + * Code 202: Correct password and Username
  135 + * Code 403: Wrong username or password
  136 + * Code 401: Unautorhized user (user not activated)
  137 + * Code 500: Error from server
  138 + * Code 400: Something went wrong with login
  139 + *
  140 + * Sends broadcast when connection finished.
  141 + */
  142 + private void sendBroadcast(int responseCode){
  143 + Intent broadCastIntent = new Intent(MainPage.BroadCastReceiverLogin.USER_LOGIN);
  144 + broadCastIntent.putExtra("responseCode", responseCode);
  145 + LocalBroadcastManager.getInstance(this).sendBroadcast(broadCastIntent);
  146 + }
  147 +
  148 + /**
  149 + * @param token String token to encrypt
  150 + * @return returns encrypted string.
  151 + *
  152 + * This method encrypts given string with DESede algorithm. With key stored in strings value.
  153 + */
  154 + private String encryptToken(String token){
  155 + TokenEncrypter encrypter = new TokenEncrypter();
  156 + return encrypter.encrypt(token, getString(R.string.encryption_key));
  157 + }
  158 +}
  159 +
... ...
Risiko/app/src/main/java/api/Api.java
1 1 package api;
2 2  
  3 +import com.google.gson.Gson;
  4 +import com.google.gson.GsonBuilder;
  5 +
3 6 import retrofit2.Retrofit;
4 7 import retrofit2.converter.gson.GsonConverterFactory;
5 8  
... ... @@ -11,11 +14,15 @@ public class Api {
11 14  
12 15 private static Endpoints instance;
13 16  
14   - public static Endpoints get(){
  17 + public static Endpoints getInstance(){
15 18 if (instance == null){
  19 + Gson gson = new GsonBuilder()
  20 + .setLenient()
  21 + .create();
  22 +
16 23 Retrofit retrofit = new Retrofit.Builder()
17 24 .baseUrl(Endpoints.BASEURL)
18   - .addConverterFactory(GsonConverterFactory.create())
  25 + .addConverterFactory(GsonConverterFactory.create(gson))
19 26 .build();
20 27 instance = retrofit.create(Endpoints.class);
21 28 }
... ...
Risiko/app/src/main/java/api/Endpoints.java
... ... @@ -10,6 +10,7 @@ import dataclasses.Activity;
10 10 import dataclasses.Evaluation;
11 11 import dataclasses.EvaluationLog;
12 12 import dataclasses.Incident;
  13 +import dataclasses.User;
13 14 import retrofit2.Call;
14 15 import retrofit2.http.Body;
15 16 import retrofit2.http.DELETE;
... ... @@ -27,7 +28,11 @@ public interface Endpoints {
27 28  
28 29  
29 30 // THE BASEURL HAS TO BE CHANGE TO THE IP-ADDRESS YOU ARE CONNECTED TO AT LOCALHOST
30   - //String BASEURL = "http://172.20.10.4:3000/";
  31 +
  32 + //HOS ALI
  33 + String BASEURL = "http://172.20.10.4:3000/";
  34 +// String BASEURL = "http://172.20.10.2:3000/";
  35 +// String BASEURL = "http://192.168.38.200:3000/";
31 36  
32 37 //HOS KRISTIN
33 38 String BASEURL = "http://192.168.84.71:3000/";
... ... @@ -36,19 +41,30 @@ public interface Endpoints {
36 41 //ØYVIN FIX
37 42 //String BASEURL ="http://risikoapi.harm.no/";
38 43  
  44 + @FormUrlEncoded
  45 + @POST("login")
  46 + Call<String> login(@Field("username") String username, @Field("password") String password);
  47 +
  48 + @GET("user/{apitoken}")
  49 + Call<User> getUser(@Path("apitoken") String apiToken);
  50 +
39 51  
40 52 @GET("evaluation")
41 53 Call<List<Evaluation>> getAllEvaluations();
42 54  
  55 +
43 56 @GET("evaluation/{id}")
44 57 Call<Evaluation> getEvaluation(@Path("id") String id);
45 58  
  59 +
46 60 @GET("activity")
47 61 Call<List<Activity>> getAllActivities();
48 62  
  63 +
49 64 @GET("incident")
50 65 Call<List<Incident>> getAllIncidents();
51 66  
  67 +
52 68 @GET("incident/{id}")
53 69 Call<List<Incident>> getIncident(@Path("id") String id);
54 70  
... ... @@ -62,10 +78,11 @@ public interface Endpoints {
62 78 Call<String> newEvaluation(@Field("title") String title, @Field("id") String id, @Field("associatedActivity") String associatedactivity,
63 79 @Field("consequence") int consequence, @Field("measures") String measures, @Field("outcome") String outcome, @Field("probability") int probability);
64 80  
  81 +
65 82 @DELETE("incident/{id}")
66 83 Call<String> deleteIncident(@Path("id") String id);
67 84  
68   - @FormUrlEncoded
  85 +
69 86 @DELETE("evaluation/{id}")
70 87 Call<String> deleteEvaluation(@Path("id") String id);
71 88  
... ...
Risiko/app/src/main/java/api/TokenEncrypter.java 0 → 100644
  1 +package api;
  2 +
  3 +import android.app.Activity;
  4 +import android.content.Context;
  5 +import android.content.Intent;
  6 +import android.content.SharedPreferences;
  7 +import android.util.Base64;
  8 +
  9 +import com.risiko.risiko.R;
  10 +
  11 +import java.io.UnsupportedEncodingException;
  12 +import java.security.GeneralSecurityException;
  13 +import java.security.spec.KeySpec;
  14 +
  15 +import javax.crypto.Cipher;
  16 +import javax.crypto.SecretKey;
  17 +import javax.crypto.SecretKeyFactory;
  18 +import javax.crypto.spec.DESedeKeySpec;
  19 +
  20 +import activity.MainPage;
  21 +import dataclasses.User;
  22 +import retrofit2.Call;
  23 +import retrofit2.Callback;
  24 +import retrofit2.Response;
  25 +
  26 +/**
  27 + * Class for encrypting and decrypting login token. With given key.
  28 + */
  29 +public class TokenEncrypter {
  30 +
  31 + // Empty constructor
  32 + public TokenEncrypter(){}
  33 +
  34 + public static String getToken(Activity activity) {
  35 + SharedPreferences sharedPref = activity.getSharedPreferences(activity.getString(R.string.packageName), Context.MODE_PRIVATE);
  36 + String token = sharedPref.getString(activity.getString(R.string.preference_name_token),"");
  37 + if (!token.equals(""))
  38 + return new TokenEncrypter().decrypt(token, activity.getString(R.string.encryption_key));
  39 +
  40 + logOut(activity);
  41 + return "";
  42 + }
  43 +
  44 + public static User getUserByToken(String token){
  45 + System.out.println(token);
  46 + final User[] user = new User[1];
  47 + Api.getInstance().getUser(token).enqueue(new Callback<User>() {
  48 + @Override
  49 + public void onResponse(Call<User> call, Response<User> response) {
  50 + user[0] = response.body();
  51 + }
  52 +
  53 + @Override
  54 + public void onFailure(Call<User> call, Throwable t) {
  55 + System.out.println(t.getMessage());
  56 + }
  57 + });
  58 + return user[0];
  59 + }
  60 +
  61 + /**
  62 + * Deletes token, finishes all activities and starts the loginActivity.
  63 + */
  64 + public static void logOut(Activity activity) {
  65 + deleteToken(activity);
  66 + Intent intent = new Intent(activity, MainPage.class);
  67 + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
  68 + activity.startActivity(intent);
  69 + activity.finishAffinity();
  70 + }
  71 +
  72 + /**
  73 + *
  74 + * @param context Activity wich the token is stored.
  75 + *
  76 + * This method deletes the token which is stored in shared preferences.
  77 + */
  78 + public static void deleteToken(Context context){
  79 + SharedPreferences sharedPref = context.getSharedPreferences(context.getString(R.string.packageName), Context.MODE_PRIVATE);
  80 + SharedPreferences.Editor edit = sharedPref.edit();
  81 + edit.clear();
  82 + edit.remove(context.getString(R.string.preference_name_token));
  83 + edit.apply();
  84 + }
  85 +
  86 + /**
  87 + * @param stringToEncrypt The string you want to encrypt.
  88 + * @param key The encryption key.
  89 + * @return Returns encrypted string
  90 + *
  91 + * This method encrypts given string with DESede algorithm and with given key.
  92 + */
  93 + public String encrypt(String stringToEncrypt, String key) {
  94 + try {
  95 + KeySpec keySpec = new DESedeKeySpec(key.getBytes());
  96 + SecretKey secretKey = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
  97 +
  98 + Cipher ecipher = Cipher.getInstance(secretKey.getAlgorithm());
  99 + ecipher.init(Cipher.ENCRYPT_MODE, secretKey);
  100 +
  101 + String charset = "UTF-8";
  102 + byte[] in = stringToEncrypt.getBytes(charset);
  103 + byte[] out = ecipher.doFinal(in);
  104 +
  105 + return new String(Base64.encode(out, Base64.NO_WRAP));
  106 + } catch (GeneralSecurityException | UnsupportedEncodingException e) {
  107 + e.printStackTrace();
  108 + }
  109 + return "";
  110 + }
  111 +
  112 + /**
  113 + * @param stringToDecrypt The string you want to decrypt
  114 + * @param key The decryption key. Stored in strings.
  115 + * @return Returns decrypted string.
  116 + *
  117 + * This decrypts a string with given key. The algorithm used is DESede.
  118 + */
  119 + public String decrypt(String stringToDecrypt, String key){
  120 + try{
  121 + KeySpec keySpec = new DESedeKeySpec(key.getBytes());
  122 + SecretKey secretKey = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
  123 +
  124 + Cipher dcipher = Cipher.getInstance(secretKey.getAlgorithm());
  125 + dcipher.init(Cipher.DECRYPT_MODE, secretKey);
  126 +
  127 + byte[] encryptedTextBytes = Base64.decode(stringToDecrypt, Base64.NO_WRAP);
  128 + byte[] deCryptedBytes = dcipher.doFinal(encryptedTextBytes);
  129 + String charSet = "UTF-8";
  130 + return new String(deCryptedBytes, charSet);
  131 + } catch (GeneralSecurityException | UnsupportedEncodingException e) {
  132 + e.printStackTrace();
  133 + }
  134 + return "";
  135 + }
  136 +
  137 +
  138 +}
... ...
Risiko/app/src/main/java/dataclasses/User.java
... ... @@ -9,65 +9,42 @@ public class User {
9 9 public static final int READACCESS_ALL = 1, READACESS_NURSE_ANESTHETIST = 2, READACCESS_CHILD_NURSE = 3,
10 10 READACCESS_ALL_TYPES_NURSE = 4, READACCESS_DOCTOR = 5, READACCESS_CONSULTANT = 6, READACCESS_PARAMEDICS = 7;
11 11  
12   - String userId;
13   - String userName;
14   - String firstname;
15   - String lastname;
16   - int groupNumber; //TODO: Gruppenummer er bare en lett måte å kunne implementere dette med begrenset innsyn
17   - Boolean isOwner; //TODO: Tenker vi må lage høyde for en sjekk på om bruker er eier av en sak, slik at vi kan bruke det for å se om de har rettigheter til å endre/slette en sak
18   -
19   - public User(String userId, String userName, String firstname, String lastname, int groupNumber, Boolean isOwner) {
20   - this.userId = userId;
21   - this.userName = userName;
22   - this.firstname = firstname;
23   - this.lastname = lastname;
24   - this.groupNumber = groupNumber;
25   - this.isOwner = isOwner;
  12 + String username;
  13 + String name;
  14 + int groupnumber; //TODO: Gruppenummer er bare en lett måte å kunne implementere dette med begrenset innsyn
  15 +
  16 + public User(String userame, String name,int groupnumber) {
  17 + this.username = userame;
  18 + this.groupnumber = groupnumber;
  19 + this.name = name;
26 20 }
27 21  
28   - public String getUserId() {
29   - return userId;
  22 + public String getUsername() {
  23 + return username;
30 24 }
31 25  
32   - public void setUserId(String userId) {
33   - this.userId = userId;
  26 + public void setUsername(String username) {
  27 + this.username = username;
34 28 }
35 29  
36   - public String getFirstname() {
37   - return firstname;
  30 + public String getName() {
  31 + return name;
38 32 }
39 33  
40   - public void setFirstname(String firstname) {
41   - this.firstname = firstname;
  34 + public void setName(String name) {
  35 + this.name = name;
42 36 }
43 37  
44   - public String getLastname() {
45   - return lastname;
  38 + public int getGroupnumber() {
  39 + return groupnumber;
46 40 }
47 41  
48   - public void setLastname(String lastname) {
49   - this.lastname = lastname;
  42 + public void setGroupnumber(int groupnumber) {
  43 + this.groupnumber = groupnumber;
50 44 }
51 45  
52   - public int getGroupNumber() {
53   - return groupNumber;
54   - }
55   -
56   - public void setGroupNumber(int groupNumber) {
57   - this.groupNumber = groupNumber;
58   - }
59   -
60   - public Boolean getOwner() {
61   - return isOwner;
62   - }
63   -
64   - public void setOwner(Boolean owner) {
65   - isOwner = owner;
66   - }
67   -
68   -
69 46 public String getReadAccessString() {
70   - switch (groupNumber) {
  47 + switch (groupnumber) {
71 48 case READACCESS_ALL: return "All helsepersonell";
72 49 case READACESS_NURSE_ANESTHETIST: return "Anestiesisykepleier";
73 50 case READACCESS_CHILD_NURSE: return "Barnesykepleier";
... ...
Risiko/app/src/main/res/layout/activity_main_page.xml
... ... @@ -7,6 +7,21 @@
7 7 tools:context="com.risiko.risiko.activity.Startside">
8 8  
9 9 <TextView
  10 + android:id="@+id/tv_wrongMessageLogin"
  11 + android:layout_width="wrap_content"
  12 + android:layout_height="wrap_content"
  13 + android:layout_marginBottom="8dp"
  14 + android:layout_marginEnd="8dp"
  15 + android:layout_marginStart="8dp"
  16 + android:layout_marginTop="8dp"
  17 + android:textColor="@color/red"
  18 + android:visibility="invisible"
  19 + app:layout_constraintBottom_toBottomOf="parent"
  20 + app:layout_constraintEnd_toEndOf="parent"
  21 + app:layout_constraintStart_toStartOf="parent"
  22 + app:layout_constraintTop_toBottomOf="@+id/btn_to_main_menu" />
  23 +
  24 + <TextView
10 25 android:id="@+id/app_name"
11 26 android:layout_marginTop="50dp"
12 27 android:layout_width="wrap_content"
... ...
Risiko/app/src/main/res/values/colors.xml
... ... @@ -8,4 +8,5 @@
8 8 <color name="colorLightGray">#E0E0E0</color>
9 9 <color name="colorWhite">#ffffff</color>
10 10 <color name="background_material_light">#e3e8e8e8</color>
  11 + <color name="red">#ff0000</color>
11 12 </resources>
... ...
Risiko/app/src/main/res/values/strings.xml
... ... @@ -39,10 +39,19 @@
39 39 <string name="no">Nei</string>
40 40 <string name="areYouSureEvent">Er du sikker på at du vil slette denne hendelsen?</string>
41 41 <string name="added_by">Lagt til av:</string>
  42 + <string name="api_login_link">http://192.168.84.67:3000/login</string>
  43 + <string name="encryption_key">0123456789abcdef0123456789abcdef0123456789abcdef</string>
  44 + <string name="preference_name_token" translatable="false">api-token</string>
  45 + <string name="packageName">api</string>
  46 + <string name="something_went_wrong_with_login">Noe gikk galt</string>
  47 + <string name="server_error">Server feil</string>
  48 + <string name="wrong_password_or_username">Feil brukernavn eller passord</string>
  49 + <string name="error_invalid_password">Feil passord</string>
  50 + <string name="error_field_required">Dette feltet er påkrevd</string>
  51 + <string name="error_invalid_username">Uvalid brukernavn</string>
42 52 <string name="see_log">Se hendelseslogg</string>
43 53 <string name="aksesslogg">Aksesslogg</string>
44 54 <string name="accesss_log">Se tilgangslogg</string>
45 55 <string name="last_edited_by">Sist endret av:</string>