Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
New Alpha release 1.0.3
  • Loading branch information
sleidig committed Sep 3, 2015
2 parents 32d6b4f + a86d3c9 commit 3e21ba5
Show file tree
Hide file tree
Showing 35 changed files with 330 additions and 66 deletions.
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="iiitd.mc.timetracker"
android:versionCode="3"
android:versionName="1.0.2">
android:versionCode="4"
android:versionName="1.0.3">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
Expand All @@ -13,7 +13,7 @@
<application
android:name=".ApplicationHelper"
android:allowBackup="true"
android:icon="@drawable/ic_launchertimeturner"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
Expand Down
Binary file added app/src/main/ic_launcher-web.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 19 additions & 4 deletions app/src/main/java/iiitd/mc/timetracker/ApplicationHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import android.app.Application;
import android.content.Context;

import iiitd.mc.timetracker.database.DatabaseController;
import iiitd.mc.timetracker.database.CachedDatabaseController;
import iiitd.mc.timetracker.database.IDatabaseController;
import iiitd.mc.timetracker.suggestor.ITaskSuggestor;
import iiitd.mc.timetracker.suggestor.MainTaskSuggestor;

/**
* Helper class to get the application Context in static functions.
Expand All @@ -13,6 +15,8 @@
*/
public class ApplicationHelper extends Application {
private static Context context;
private static IDatabaseController databaseController;
private static ITaskSuggestor mainTaskSuggestor;

public void onCreate() {
super.onCreate();
Expand All @@ -34,8 +38,19 @@ public static Context getAppContext() {
*
* @return An instance implementing the IDatabaseController interface.
*/
public static IDatabaseController createDatabaseController() {
//return new MockupDatabaseController();
return new DatabaseController(ApplicationHelper.context);
public static IDatabaseController getDatabaseController() {
if (databaseController == null) {
databaseController = new CachedDatabaseController(context);
}

return databaseController;
}

public static ITaskSuggestor getMainTaskSuggestor() {
if (mainTaskSuggestor == null) {
mainTaskSuggestor = new MainTaskSuggestor();
}

return mainTaskSuggestor;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class TaskRecorderService extends Service {
private final int ONGOING_NOTIFICATION_ID = 1;
private Recording currentRecording = null;
private Recording lastRecording = null;
private IDatabaseController db = ApplicationHelper.createDatabaseController();
private IDatabaseController db = ApplicationHelper.getDatabaseController();
private Task breakTask;
private static final String SETTINGS_BREAK_TASK_ID = "breakTaskId";
private static final String BREAK_TASK_NAME = "Break";
Expand Down Expand Up @@ -233,7 +233,7 @@ public Task getBreakTask() {
*/
public static Task getTaskFromString(String taskString) {
Task task = null;
IDatabaseController db = ApplicationHelper.createDatabaseController();
IDatabaseController db = ApplicationHelper.getDatabaseController();
db.open();
String[] taskStringParts = taskString.split(Pattern.quote(Task.THS));
// get all tasks with name like the lowest hierarchy part of the string
Expand Down Expand Up @@ -276,7 +276,7 @@ public static Task createTaskFromString(String taskString) {
taskParent = createTaskFromString(parentName);
}
Task newTask = new Task(taskName, taskParent);
IDatabaseController db = ApplicationHelper.createDatabaseController();
IDatabaseController db = ApplicationHelper.getDatabaseController();
db.open();
db.insertTask(newTask);
db.close();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
package iiitd.mc.timetracker.database;

import android.content.Context;
import android.database.SQLException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import iiitd.mc.timetracker.model.Recording;
import iiitd.mc.timetracker.model.Task;

/**
* Wrapper for DatabaseController that caches the Records and Tasks list in memory for faster access.
*/
public class CachedDatabaseController implements IDatabaseController {
private IDatabaseController db;
/* in-memory caches of data. to invalidate clear the map. */
private boolean cachedTasksComplete = false;
private HashMap<Long, Task> cachedTasks = new HashMap<>();
private boolean cachedRecordingsComplete = false;
private HashMap<Long, Recording> cachedRecordings = new HashMap<>();

public CachedDatabaseController(Context context) {
db = new DatabaseController(context);
}

@Override
public IDatabaseController open() throws SQLException {
db.open();
return this;
}

@Override
public void close() {
db.close();
}

@Override
public void insertTask(Task newTask) {
db.insertTask(newTask);

cachedTasks.put(newTask.getId(), newTask);
}

@Override
public Task getTask(long id) {
Task task = cachedTasks.get(id);

if (task == null && !cachedTasksComplete) {
task = db.getTask(id);
cachedTasks.put(id, task);
}

return task;
}

@Override
public List<Task> getTasks() {
if (!cachedTasksComplete) {
List<Task> tasks = db.getTasks();
for (Task t : tasks) {
cachedTasks.put(t.getId(), t);
}
cachedTasksComplete = true;
}

return new ArrayList<>(cachedTasks.values());
}

@Override
public List<Task> getTasks(String name) {
//TODO: Cache name searches?
return db.getTasks(name);
}

@Override
public void updateTask(Task updatedTask) {
db.updateTask(updatedTask);

cachedTasks.put(updatedTask.getId(), updatedTask);
}

@Override
public void deleteTask(long id) {
db.deleteTask(id);

cachedTasks.remove(id);
}

@Override
public void deleteTask(Task removedTask) {
deleteTask(removedTask.getId());
}


@Override
public void insertRecording(Recording newRecording) {
db.insertRecording(newRecording);

cachedRecordings.put(newRecording.getRecordingId(), newRecording);
}

@Override
public Recording getRecording(long recordingId) {
Recording recording = cachedRecordings.get(recordingId);

if (recording == null && !cachedRecordingsComplete) {
recording = db.getRecording(recordingId);
cachedRecordings.put(recordingId, recording);
}

return recording;
}

@Override
public List<Recording> getRecordings() {
if (cachedRecordingsComplete) {
return convertToSortedList(cachedRecordings.values());
} else {
List<Recording> recordings = db.getRecordings();
for (Recording r : recordings) {
cachedRecordings.put(r.getRecordingId(), r);
}
cachedRecordingsComplete = true;
return recordings;
}
}

private List<Recording> convertToSortedList(Collection<Recording> recordingValues) {
List<Recording> recordings = new ArrayList<>(recordingValues);
Collections.sort(recordings);
return recordings;
}

@Override
public List<Recording> getRecordings(int limit) {
//TODO: Cache limit recordings search?
return db.getRecordings(limit);
}

@Override
public List<Recording> getRecordings(long date) {
//TODO: Cache date recordings search?
return db.getRecordings(date);
}

@Override
public List<Recording> getRecordings(long start, long end) {
//TODO: Cache limit recordings search?
return db.getRecordings(start, end);
}

@Override
public List<Recording> getRecordings(long taskid, long start, long end) {
//TODO: Cache task and time recordings search?
return db.getRecordings(taskid, start, end);
}

@Override
public void updateRecording(Recording updatedRecording) {
db.updateRecording(updatedRecording);

cachedRecordings.put(updatedRecording.getRecordingId(), updatedRecording);
}

@Override
public void deleteRecording(long id) {
db.deleteRecording(id);

cachedRecordings.remove(id);
}

@Override
public void deleteRecording(Recording removedRecording) {
deleteRecording(removedRecording.getRecordingId());
}

@Override
public List<Task> getSubTasks(long id) {
//TODO: Cache subtask requests?
return db.getSubTasks(id);
}

@Override
public void resetDatabase() {
db.resetDatabase();

cachedTasks.clear();
cachedTasksComplete = false;
cachedRecordings.clear();
cachedRecordingsComplete = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static String getDefaultExportFileName() {
public static List<String> exportCsv() {
List<String> lines = new ArrayList<>();

IDatabaseController db = ApplicationHelper.createDatabaseController();
IDatabaseController db = ApplicationHelper.getDatabaseController();
db.open();
for (Recording recording : db.getRecordings()) {
lines.add(recording2csv(recording));
Expand Down Expand Up @@ -78,7 +78,7 @@ private static String unescapeString(String s) {
* @param csvLines A list of Strings, each representing csv data of one recording.
*/
public static void importCsv(List<String> csvLines) {
IDatabaseController db = ApplicationHelper.createDatabaseController();
IDatabaseController db = ApplicationHelper.getDatabaseController();
db.open();
for (String line : csvLines) {
Recording recording = csv2recording(line);
Expand Down Expand Up @@ -134,7 +134,7 @@ private static Recording csv2recording(String csvLine) {
* Use with care!
*/
public static void resetDatabase() {
IDatabaseController db = ApplicationHelper.createDatabaseController();
IDatabaseController db = ApplicationHelper.getDatabaseController();
db.open();
db.resetDatabase();
db.close();
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/java/iiitd/mc/timetracker/model/Recording.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* A Recording describes a specific time period spent working on a Task.
*/
public class Recording {
public class Recording implements Comparable<Recording> {

private long recordingId;
private Date start;
Expand Down Expand Up @@ -175,4 +175,9 @@ public String toString() {
eTime = dateFormatter.format(end) + " " + timeFormatter.format(end);
return getTask().toString() + " [" + sTime + " - " + eTime + "]";
}

@Override
public int compareTo(Recording recording) {
return recording.getStart().compareTo(this.getStart());
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/iiitd/mc/timetracker/model/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public List<Task> getSubtasks() {
if (subtasks == null)
subtasks = new ArrayList<>();

IDatabaseController db = ApplicationHelper.createDatabaseController();
IDatabaseController db = ApplicationHelper.getDatabaseController();
db.open();
List<Task> subtasks = db.getSubTasks(id);
db.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class LocationTaskSuggestor implements ITaskSuggestor {

public LocationTaskSuggestor() {
appContext = ApplicationHelper.getAppContext();
db = ApplicationHelper.createDatabaseController();
db = ApplicationHelper.getDatabaseController();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

/**
* Merges different tasks into one Task list
*
* @author gullal
*/
public class MainTaskSuggestor implements ITaskSuggestor {
private ITaskSuggestor[] suggestors = new ITaskSuggestor[]{
Expand All @@ -17,7 +15,7 @@ public class MainTaskSuggestor implements ITaskSuggestor {
new LocationTaskSuggestor(),
};
private double[] suggestorWeights = new double[]{
0, // top hierarchy
0, // top hierarchy
0.1, // recent tasks
0.4, // time
0.5, // location
Expand Down
Loading

0 comments on commit 3e21ba5

Please sign in to comment.