diff --git a/app/build.gradle b/app/build.gradle index f4afe28..889bef4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -46,6 +46,9 @@ dependencies { implementation 'com.android.support:cardview-v7:27.0.2' implementation 'com.android.support:support-v4:27.0.2' compile 'com.android.support.constraint:constraint-layout:1.1.0-beta4' + compile 'com.squareup.retrofit2:retrofit:2.1.0' + compile 'com.google.code.gson:gson:2.6.2' + compile 'com.squareup.retrofit2:converter-gson:2.1.0' implementation 'com.android.support:design:27.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 75f64a6..5d62988 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,8 @@ + + call = apiInterface.createStudentAccount(id, username, SAPId, department, year, batch); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { +// Log.i("student", response.body().toString()); + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + + private void hash_password (String password) { + + HashingPassword hashingPassword = new HashingPassword(); + salt = hashingPassword.generate_salt(); + intermediate_password = password + salt; + hashed_password = hashingPassword.hash_the_password(intermediate_password); + } + + private void getIdForStudentFromUser () { + Call call = apiInterface.getId(username, hashed_password); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + Log.i("idFromServer", Integer.toString(response.body().getId())); + sendDataToServer(response.body().getId()); + + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + + } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/djunicode/queuingapp/customClasses/HashingPassword.java b/app/src/main/java/com/djunicode/queuingapp/customClasses/HashingPassword.java new file mode 100644 index 0000000..9ef5347 --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/customClasses/HashingPassword.java @@ -0,0 +1,52 @@ +package com.djunicode.queuingapp.customClasses; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Created by DELL_PC on 23-01-2018. + */ + +public class HashingPassword { + + private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + public String generate_salt() { + int count = 16; + StringBuilder builder = new StringBuilder(); + while (count-- != 0) { + int character = (int)(Math.random()*ALPHA_NUMERIC_STRING.length()); + builder.append(ALPHA_NUMERIC_STRING.charAt(character)); + } + return builder.toString(); + } + + public String hash_the_password(String input) { + StringBuilder hash = new StringBuilder(); + + try { + MessageDigest sha = MessageDigest.getInstance("SHA-1"); + byte[] hashedBytes = sha.digest(input.getBytes()); + char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'a', 'b', 'c', 'd', 'e', 'f' }; + for (int idx = 0; idx < hashedBytes.length; ++idx) { + byte b = hashedBytes[idx]; + hash.append(digits[(b & 0xf0) >> 4]); + hash.append(digits[b & 0x0f]); + } + } catch (NoSuchAlgorithmException e) { + // handle error here. + } + + return hash.toString(); + } + + public boolean isExpectedPassword(String password, String salt, String expectedHash) { + String fina = password + salt; + String s = hash_the_password(fina); + if(s.equals(expectedHash)) + return true; + else + return false; + } + +} diff --git a/app/src/main/java/com/djunicode/queuingapp/customClasses/TeacherTimerTask.java b/app/src/main/java/com/djunicode/queuingapp/customClasses/TeacherTimerTask.java new file mode 100644 index 0000000..7f5dd9b --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/customClasses/TeacherTimerTask.java @@ -0,0 +1,38 @@ +package com.djunicode.queuingapp.customClasses; + +import android.util.Log; +import com.djunicode.queuingapp.model.LocationTeacher; +import com.djunicode.queuingapp.rest.ApiClient; +import com.djunicode.queuingapp.rest.ApiInterface; +import java.util.TimerTask; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; + +/** + * Created by DELL_PC on 23-01-2018. + */ + +public class TeacherTimerTask extends TimerTask{ + + ApiInterface apiInterface = ApiClient.getClient().create(ApiInterface.class); + int i = 19; + + @Override + public void run() { + + Call call = apiInterface.deleteTeacherLocation(i); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + Log.i("Info", "Deleted"); + } + + @Override + public void onFailure(Call call, Throwable t) { + Log.i("Info", "Deletion failed"); + } + }); + + } +} diff --git a/app/src/main/java/com/djunicode/queuingapp/fragment/FindTeacherFragment.java b/app/src/main/java/com/djunicode/queuingapp/fragment/FindTeacherFragment.java index 5c51ee5..6514bc1 100644 --- a/app/src/main/java/com/djunicode/queuingapp/fragment/FindTeacherFragment.java +++ b/app/src/main/java/com/djunicode/queuingapp/fragment/FindTeacherFragment.java @@ -7,6 +7,7 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.CardView; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -19,6 +20,12 @@ import android.widget.Toast; import com.djunicode.queuingapp.R; import com.djunicode.queuingapp.activity.SightedTeacherActivity; +import com.djunicode.queuingapp.model.LocationTeacher; +import com.djunicode.queuingapp.rest.ApiClient; +import com.djunicode.queuingapp.rest.ApiInterface; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; /** * A simple {@link Fragment} subclass. @@ -27,6 +34,8 @@ public class FindTeacherFragment extends Fragment { private Spinner subjectSpinner, teacherSpinner; private CardView findTeacherButton, sightedTeacherButton; + ApiInterface apiInterface; + public FindTeacherFragment() { // Required empty public constructor @@ -46,6 +55,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, teacherSpinner = (Spinner) view.findViewById(R.id.teacherSpinner); findTeacherButton = (CardView) view.findViewById(R.id.findTeacherButton); sightedTeacherButton = (CardView) view.findViewById(R.id.sightedTeacherButton); + apiInterface = ApiClient.getClient().create(ApiInterface.class); ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_dropdown_item, array); @@ -74,7 +84,7 @@ public void onNothingSelected(AdapterView parent) { findTeacherButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - if(teacherSpinner.getSelectedItemPosition() != 0){ + /*if(teacherSpinner.getSelectedItemPosition() != 0){ String location = "Prof. " + teacherSpinner.getSelectedItem().toString() + " is in the staff lounge."; AlertDialog.Builder builder = new Builder(getActivity()); @@ -82,8 +92,44 @@ public void onClick(View v) { .setPositiveButton("OK", null) .show(); } + else + Toast.makeText(getContext(), "Please Select a teacher!", Toast.LENGTH_SHORT).show();*/ + if(teacherSpinner.getSelectedItemPosition() != 0) { + int i = 22; + Call call = apiInterface.getTeacherLocation(i); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + /*Log.i("Id", response.body().getId().toString()); + Log.i("Floor", response.body().getFloor().toString()); + Log.i("Department", response.body().getDepartment()); + Log.i("Room", response.body().getRoom()); + Log.i("Updated At", response.body().getUpdated_at());*/ + String location = "Prof. " + teacherSpinner.getSelectedItem().toString() + + "is at " + response.body().getFloor().toString() + "floor's " + response.body().getDepartment() + + "department and " + response.body().getRoom() + "room."; + AlertDialog.Builder builder = new Builder(getActivity()); + builder.setMessage(location) + .setPositiveButton("OK", null) + .show(); + } + + @Override + public void onFailure(Call call, Throwable t) { + if(t != null) { + Log.i("info: ", t.getMessage()); + } + else { + Log.i("info: ", "failed"); + } + } + }); + + } + else Toast.makeText(getContext(), "Please Select a teacher!", Toast.LENGTH_SHORT).show(); + } }); @@ -97,4 +143,4 @@ public void onClick(View v) { return view; } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/djunicode/queuingapp/fragment/LogInStudentFragment.java b/app/src/main/java/com/djunicode/queuingapp/fragment/LogInStudentFragment.java index 0e739b2..7c81824 100644 --- a/app/src/main/java/com/djunicode/queuingapp/fragment/LogInStudentFragment.java +++ b/app/src/main/java/com/djunicode/queuingapp/fragment/LogInStudentFragment.java @@ -126,7 +126,7 @@ private boolean validatePassword() { } private boolean validMatch() { - if(sapIdLogInEditText.getText().toString().equals("dhruv") && + if(sapIdLogInEditText.getText().toString().equals("60004160006") && passwordLogInEditText.getText().toString().equals("demopass")) { return true; diff --git a/app/src/main/java/com/djunicode/queuingapp/fragment/LogInTeacherFragment.java b/app/src/main/java/com/djunicode/queuingapp/fragment/LogInTeacherFragment.java index c4b7871..bcdfb3c 100644 --- a/app/src/main/java/com/djunicode/queuingapp/fragment/LogInTeacherFragment.java +++ b/app/src/main/java/com/djunicode/queuingapp/fragment/LogInTeacherFragment.java @@ -120,7 +120,7 @@ private boolean validatePassword() { } private boolean validMatch() { - if(sapIdLogInTeacherEditText.getText().toString().equals("dhruv") && + if(sapIdLogInTeacherEditText.getText().toString().equals("60004160006") && passwordLogInTeacherEditText.getText().toString().equals("demopass")) { return true; diff --git a/app/src/main/java/com/djunicode/queuingapp/fragment/TeacherLocationFragment.java b/app/src/main/java/com/djunicode/queuingapp/fragment/TeacherLocationFragment.java index a1fbbcd..f93b047 100644 --- a/app/src/main/java/com/djunicode/queuingapp/fragment/TeacherLocationFragment.java +++ b/app/src/main/java/com/djunicode/queuingapp/fragment/TeacherLocationFragment.java @@ -5,6 +5,7 @@ import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.v4.app.Fragment; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; @@ -16,7 +17,16 @@ import android.widget.Spinner; import android.widget.Toast; import com.djunicode.queuingapp.R; +import com.djunicode.queuingapp.customClasses.TeacherTimerTask; +import com.djunicode.queuingapp.model.LocationTeacher; import com.djunicode.queuingapp.model.RecentEvents; +import com.djunicode.queuingapp.rest.ApiClient; +import com.djunicode.queuingapp.rest.ApiInterface; + +import java.util.Timer; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; /** * A simple {@link Fragment} subclass. @@ -27,6 +37,7 @@ public class TeacherLocationFragment extends Fragment { private FloatingActionButton locationUpdateFab; public static boolean locationUpdated = false; public static String locationString; + ApiInterface apiInterface; public TeacherLocationFragment() { // Required empty public constructor @@ -38,6 +49,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_teacher_location, container, false); + apiInterface = ApiClient.getClient().create(ApiInterface.class); + + TeacherTimerTask timerTask = new TeacherTimerTask(); + Timer t = new Timer(); + t.schedule(timerTask, 6000,360000); String[] array = {"Select", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}; @@ -134,10 +150,32 @@ public void onClick(View v) { Toast.makeText(getContext(), "Created new event!", Toast.LENGTH_SHORT).show(); } } + Toast.makeText(getContext(), "Clicked!", Toast.LENGTH_LONG).show(); + Call call = apiInterface.sendTeacherLocation(6, "Comps", "60"); + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) { + Log.i("Id", response.body().getId().toString()); + Log.i("Floor", response.body().getFloor().toString()); + Log.i("Department", response.body().getDepartment().toString()); + Log.i("Room", response.body().getRoom().toString()); + Log.i("Updated At", response.body().getUpdated_at().toString()); + } + + @Override + public void onFailure(Call call, Throwable t) { + if(t != null) { + int i = Log.i("info: ", t.getMessage()); + } + else { + Log.i("info: ", "failed"); + } + } + }); } }); return view; } -} +} \ No newline at end of file diff --git a/app/src/main/java/com/djunicode/queuingapp/model/LocationTeacher.java b/app/src/main/java/com/djunicode/queuingapp/model/LocationTeacher.java new file mode 100644 index 0000000..01d9737 --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/model/LocationTeacher.java @@ -0,0 +1,55 @@ +package com.djunicode.queuingapp.model; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +/** + * Created by DELL_PC on 13-01-2018. + */ + +public class LocationTeacher { + + @SerializedName("id") + public Integer id; + + @SerializedName("floor") + public Integer floor; + + @SerializedName("department") + public String department; + + @SerializedName("room") + public String room; + + @SerializedName("updated_at") + public String updated_at; + + + public LocationTeacher(Integer id, Integer floor, String department, String room, String updated_at) { + this.id = id; + this.floor = floor; + this.department = department; + this.room = room; + this.updated_at = updated_at; + } + + public Integer getId() { + return id; + } + + public Integer getFloor() { + return floor; + } + + public String getDepartment() { + return department; + } + + public String getRoom() { + return room; + } + + public String getUpdated_at() { + return updated_at; + } +} diff --git a/app/src/main/java/com/djunicode/queuingapp/model/Student.java b/app/src/main/java/com/djunicode/queuingapp/model/Student.java new file mode 100644 index 0000000..657b308 --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/model/Student.java @@ -0,0 +1,39 @@ +package com.djunicode.queuingapp.model; + +/** + * Created by DELL_PC on 09-01-2018. + */ + +import com.google.gson.annotations.SerializedName; + + +public class Student { + + @SerializedName("user") + public int user; + @SerializedName("name") + public String name; + @SerializedName("department") + public String department; + @SerializedName("year") + public String year; + @SerializedName("batch") + public String batch; + @SerializedName("sapID") + public String sapID; +// @SerializedName("password") +// public String password; + + public Student(int user, String name, String department, String year, String batch, + String sapID, String password) { + this.user = user; + this.name = name; + this.department = department; + this.year = year; + this.batch = batch; + this.sapID = sapID; +// this.password = password; + } + +} + diff --git a/app/src/main/java/com/djunicode/queuingapp/model/StudentForId.java b/app/src/main/java/com/djunicode/queuingapp/model/StudentForId.java new file mode 100644 index 0000000..c85810d --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/model/StudentForId.java @@ -0,0 +1,51 @@ +package com.djunicode.queuingapp.model; + +import com.google.gson.annotations.SerializedName; + +/** + * Created by DELL_PC on 24-01-2018. + */ + +public class StudentForId { + + @SerializedName("username") + public String username; + @SerializedName("password") + public String password; + // @SerializedName("salt") +// public String salt; + @SerializedName("id") + public Integer id; +// @SerializedName("created_at") +// public String updated_at; + + + public StudentForId(String username, String password, String salt, Integer id, String created_at) { + this.id = id; + this.username = username; + this.password = password; +// this.salt = salt; +// this.updated_at = created_at; + } + + public Integer getId() { + return id; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + +// public String getSalt() { +// return salt; +// } +// +// public String getCreated_at() { +// return updated_at; +// } + +} diff --git a/app/src/main/java/com/djunicode/queuingapp/rest/ApiClient.java b/app/src/main/java/com/djunicode/queuingapp/rest/ApiClient.java new file mode 100644 index 0000000..897954c --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/rest/ApiClient.java @@ -0,0 +1,22 @@ +package com.djunicode.queuingapp.rest; + + +import retrofit2.Retrofit; +import retrofit2.converter.gson.GsonConverterFactory; + +/** + * Created by DELL_PC on 09-01-2018. + */ + +public class ApiClient { + + private static Retrofit retrofit = null; + + public static Retrofit getClient () { + retrofit = new Retrofit.Builder() + .baseUrl("http://192.168.0.100:8000/") + .addConverterFactory(GsonConverterFactory.create()) + .build(); + return retrofit; + } +} diff --git a/app/src/main/java/com/djunicode/queuingapp/rest/ApiInterface.java b/app/src/main/java/com/djunicode/queuingapp/rest/ApiInterface.java new file mode 100644 index 0000000..ac3053f --- /dev/null +++ b/app/src/main/java/com/djunicode/queuingapp/rest/ApiInterface.java @@ -0,0 +1,44 @@ +package com.djunicode.queuingapp.rest; + +import com.djunicode.queuingapp.model.LocationTeacher; +import com.djunicode.queuingapp.model.Student; +import com.djunicode.queuingapp.model.StudentForId; +import retrofit2.Call; +import retrofit2.http.Body; +import retrofit2.http.DELETE; +import retrofit2.http.Field; +import retrofit2.http.FormUrlEncoded; +import retrofit2.http.GET; +import retrofit2.http.POST; +import retrofit2.http.PUT; +import retrofit2.http.Path; +import retrofit2.http.Query; + +/** + * Created by DELL_PC on 09-01-2018. + */ + +public interface ApiInterface { + + @FormUrlEncoded + @POST("queues/student/") + Call createStudentAccount (@Field("user") int user_id, @Field("name") String username, + @Field("sapID") String sapId, @Field("department") String department, + @Field("year") String year, @Field("batch") String batch); + + @FormUrlEncoded + @POST("queues/users/") + Call getId (@Field("username") String username, @Field("password") String password); + + @FormUrlEncoded + @POST("queues/") + Call sendTeacherLocation (@Field("floor") Integer floor, + @Field("department") String department, @Field("room") String room); + + @GET("queues/{id}/") + Call getTeacherLocation (@Path("id") int id); + + @DELETE("queues/{id}/") + Call deleteTeacherLocation (@Path("id") int id); + +} diff --git a/app/src/main/res/layout-sw320dp/activity_log_in.xml b/app/src/main/res/layout-sw320dp/activity_log_in.xml index 09d7c3c..440c090 100644 --- a/app/src/main/res/layout-sw320dp/activity_log_in.xml +++ b/app/src/main/res/layout-sw320dp/activity_log_in.xml @@ -101,7 +101,6 @@ android:layout_marginTop="30dp" android:entries="@array/batch_array" android:theme="@style/Base.ThemeOverlay.AppCompat.Dark"/> - Enter the password Password should be at least 5 characters Username and password does not match + Enter your batch Enter your department Enter your year