Skip to content

Commit

Permalink
Add insert session (ProjektMedInf#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
bgeVam committed May 31, 2017
1 parent 2b54fc4 commit 53ef2df
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface SessionDao {
public List<Session> getAllSessions();

public Session getSessionBySessionId(long sessionId);

public long insertSession(Session session);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.projektmedinf.wifisdcryptolocker.data.dao.impl;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
Expand Down Expand Up @@ -33,15 +34,16 @@ public List<Session> getAllSessions() {

if (cursor != null) {
if (cursor.moveToFirst()) {
// TODO change date to decrypted
sessionList.add(new Session(
cursor.getLong(cursor.getColumnIndex(COLUMN_NAME_SESSION_ID)),
cursor.getBlob(cursor.getColumnIndex(COLUMN_NAME_ENCRYPTED_DATE)),
new Date(),
cursor.getString(cursor.getColumnIndex(COLUMN_NAME_LOCATION)),
cursor.getBlob(cursor.getColumnIndex(COLUMN_NAME_INITIALISATION_VECTOR)))
);

do {
// TODO change date to decrypted
sessionList.add(new Session(
cursor.getLong(cursor.getColumnIndex(COLUMN_NAME_SESSION_ID)),
cursor.getBlob(cursor.getColumnIndex(COLUMN_NAME_ENCRYPTED_DATE)),
new Date(),
cursor.getString(cursor.getColumnIndex(COLUMN_NAME_LOCATION)),
cursor.getBlob(cursor.getColumnIndex(COLUMN_NAME_INITIALISATION_VECTOR)))
);
} while (cursor.moveToNext());
}
cursor.close();
}
Expand Down Expand Up @@ -71,4 +73,13 @@ public Session getSessionBySessionId(long sessionId) {

return toReturn;
}

@Override
public long insertSession(Session session) {
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_NAME_ENCRYPTED_DATE, session.getEncryptedDate());
contentValues.put(COLUMN_NAME_LOCATION, session.getLocation());
contentValues.put(COLUMN_NAME_INITIALISATION_VECTOR, session.getIv());
return sqLiteDatabase.insert(TABLE_NAME_SESSION, null, contentValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,24 @@

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import io.github.projektmedinf.wifisdcryptolocker.R;
import io.github.projektmedinf.wifisdcryptolocker.model.Session;
import io.github.projektmedinf.wifisdcryptolocker.model.User;
import io.github.projektmedinf.wifisdcryptolocker.service.SessionService;
import io.github.projektmedinf.wifisdcryptolocker.service.impl.SessionServiceImpl;
import io.github.projektmedinf.wifisdcryptolocker.service.impl.UserServiceImpl;
import io.github.projektmedinf.wifisdcryptolocker.utils.CryptoUtils;

import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.util.Date;

import static io.github.projektmedinf.wifisdcryptolocker.utils.Constansts.USER_NAME;

public class HomeFragment extends android.support.v4.app.Fragment {

Expand All @@ -17,7 +31,28 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
View view = inflater.inflate(R.layout.fragment_home, container, false);
Button insertSessionButton = (Button) view.findViewById(R.id.insertSessionButton);
insertSessionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
insertRandomSession();
}
});
return view;
}

private void insertRandomSession() {
byte[] date = ByteBuffer.allocate(Long.SIZE / Byte.SIZE).putLong(new Date().getTime()).array();
try {
User user = new UserServiceImpl(getActivity()).getUserByUserName(USER_NAME);
Session randomSession = new Session(0l, CryptoUtils.handleByteArrayCrypto(date, user, 1), null, "Vienna", user.getCryptoKeyIV());
SessionService sessionService = new SessionServiceImpl(getActivity());
sessionService.insertSession(randomSession);
Log.d("HomeFragment", sessionService.getAllSessions().toString());
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,18 @@
*/
public interface SessionService {

/**
* Get all {@code Session}s
*
* @return list of all sessions
*/
public List<Session> getAllSessions();

/**
* Insert a new {@code Session} given a {@code session}
*
* @param session session to insert
* @return the id of the newly inserted session
*/
long insertSession(Session session);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@
/**
* Created by stiefel40k on 20.04.17.
*/
public class SessionServiceImpl implements SessionService{
public class SessionServiceImpl implements SessionService {

private SessionDao sessionDao;

public SessionServiceImpl(Context context){
public SessionServiceImpl(Context context) {
sessionDao = new SessionDaoImpl(context);
}

@Override
public List<Session> getAllSessions() {
return sessionDao.getAllSessions();
}

@Override
public long insertSession(Session session) {
return sessionDao.insertSession(session);
}
}
9 changes: 4 additions & 5 deletions app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
android:layout_height="match_parent"
tools:context="io.github.projektmedinf.wifisdcryptolocker.fragments.HomeFragment">

<TextView
android:text="This is the home fragment"
android:layout_width="227dp"
android:layout_gravity="center"
android:layout_height="wrap_content" android:id="@+id/textView"/>
<Button
android:text="Insert Session"
android:layout_width="300dp"
android:layout_height="wrap_content" android:id="@+id/insertSessionButton"/>

</FrameLayout>

0 comments on commit 53ef2df

Please sign in to comment.