Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pomodoro integration #71

Merged
merged 17 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.secuso.privacyfriendlytodolist">

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="org.secuso.privacyfriendlytodolist.TODO_PERMISSION"/>
<uses-permission android:name="org.secuso.privacyfriendlyproductivitytimer.TODO_RE_PERMISSION"/>

<!-- Permission for TodoReceiver: -->
<permission android:name="org.secuso.privacyfriendlytodolist.TODO_PERMISSION"
android:protectionLevel="signature"
android:label="@string/permission_todo_name"
android:description="@string/permission_todo_description" />

<supports-screens
android:anyDensity="true"
Expand Down Expand Up @@ -82,6 +90,15 @@
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>


<receiver android:name="org.secuso.privacyfriendlytodolist.receiver.TodoReReceiver"
android:exported="true"
android:permission="org.secuso.privacyfriendlyproductivitytimer.TODO_RE_PERMISSION">
<intent-filter>
<action android:name="org.secuso.privacyfriendlyproductivitytimer.TODO_RE_ACTION" />
</intent-filter>
</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public abstract class BaseTodo {

protected int id;

protected int progress;

public int getProgress(){
return progress;
}

protected String name, description;
protected DBQueryHandler.ObjectStates dbState;

Expand All @@ -43,6 +49,12 @@ public void setChanged() {
this.dbState = DBQueryHandler.ObjectStates.UPDATE_DB;
}

public void setChangedFromPomodoro() {
this.dbState = DBQueryHandler.ObjectStates.UPDATE_FROM_POMODORO;
}



public void setUnchanged() {
this.dbState = DBQueryHandler.ObjectStates.NO_DB_ACTION;
}
Expand All @@ -51,6 +63,10 @@ public void setName(String name) {
this.name = name;
}

public void setProgress(int progress) {
this.progress = progress;
}

public void setId(int id) {
this.id = id;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ public class TodoSubTask extends BaseTodo implements Parcelable {
private boolean inTrash;
private long taskIdForeignKey;

private int progress = -1;

@Override
public int getProgress() {
return progress;
}

public TodoSubTask() {
super();
done = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private static TodoTask extractTodoTask(Cursor cursor) {
public enum ObjectStates {
INSERT_TO_DB,
UPDATE_DB,
UPDATE_FROM_POMODORO,
NO_DB_ACTION
}

Expand Down Expand Up @@ -404,7 +405,7 @@ public static int saveTodoTaskInDb(SQLiteDatabase db, TodoTask todoTask) {

int returnCode;

if(todoTask.getDBState() != ObjectStates.NO_DB_ACTION) {
if((todoTask.getDBState() != ObjectStates.NO_DB_ACTION) && (todoTask.getDBState() != ObjectStates.UPDATE_FROM_POMODORO)) {

ContentValues values = new ContentValues();
values.put(TTodoTask.COLUMN_NAME, todoTask.getName());
Expand All @@ -421,21 +422,39 @@ public static int saveTodoTaskInDb(SQLiteDatabase db, TodoTask todoTask) {
if(todoTask.getDBState() == ObjectStates.INSERT_TO_DB) {
returnCode = (int) db.insert(TTodoTask.TABLE_NAME, null, values);
Log.d(TAG, "Todo task " + todoTask.getName() + " was inserted into the database (return code: "+returnCode+").");

} else if(todoTask.getDBState() == ObjectStates.UPDATE_DB) {
String whereClause = TTodoTask.COLUMN_ID + "=?";
String[] whereArgs = {String.valueOf(todoTask.getId())};
db.update(TTodoTask.TABLE_NAME, values, whereClause, whereArgs);
returnCode = todoTask.getId();
Log.d(TAG, "Todo task " + todoTask.getName() + " was updated (return code: "+returnCode+").");

} else
returnCode = NO_CHANGES;


todoTask.setUnchanged();
//todoTask.setDbState(ObjectStates.NO_DB_ACTION);

} else
returnCode = NO_CHANGES;
} else if (todoTask.getDBState() == ObjectStates.UPDATE_FROM_POMODORO) {
//Only update values given by pomodoro
ContentValues values = new ContentValues();
values.put(TTodoTask.COLUMN_NAME, todoTask.getName());
values.put(TTodoTask.COLUMN_PROGRESS, todoTask.getProgress());
values.put(TTodoTask.COLUMN_DONE, todoTask.getDone());
String whereClause = TTodoTask.COLUMN_ID + "=?";
String[] whereArgs = {String.valueOf(todoTask.getId())};
db.update(TTodoTask.TABLE_NAME, values, whereClause, whereArgs);
returnCode = todoTask.getId();
Log.d(TAG, "Todo task " + todoTask.getName() + " was updated (return code: "+returnCode+").");
todoTask.setUnchanged();

} else {
Log.d("DB", "5");
returnCode = NO_CHANGES;
}
Log.d("DB", "return code:"+returnCode);
return returnCode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
This file is part of Privacy Friendly To-Do List.

Privacy Friendly To-Do List is free software:
you can redistribute it and/or modify it under the terms of the
GNU General Public License as published by the Free Software Foundation,
either version 3 of the License, or any later version.

Privacy Friendly To-Do List is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Privacy Friendly To-Do List. If not, see <http://www.gnu.org/licenses/>.
*/
package org.secuso.privacyfriendlytodolist.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.secuso.privacyfriendlytodolist.view.MainActivity;

public class TodoReReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
int id = intent.getIntExtra("todo_id", -1);
String name = intent.getStringExtra("todo_name");
int progress = intent.getIntExtra("todo_progress", -1);

if (id == -1 || name == null) {
Log.e("MainActivity", "Todo Intent is not complete");
return;
}



Intent runIntent = new Intent(context, MainActivity.class);
runIntent.putExtra(MainActivity.COMMAND, MainActivity.COMMAND_UPDATE)
.putExtra("todo_id", id)
.putExtra("todo_name", name)
.putExtra("todo_progress", progress)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(runIntent);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -83,6 +84,12 @@

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {


public static final String COMMAND = "command";
public static final int COMMAND_UPDATE = 3;
public static final int COMMAND_RUN_TODO = 2;


private static final String TAG = MainActivity.class.getSimpleName();

// Keys
Expand All @@ -94,6 +101,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public static final String KEY_SELECTED_FRAGMENT_BY_NOTIFICATION = "fragment_choice";
private static final String KEY_FRAGMENT_CONFIG_CHANGE_SAVE = "current_fragment";
private static final String KEY_ACTIVE_LIST = "KEY_ACTIVE_LIST";
private static final String POMODORO_ACTION = "org.secuso.privacyfriendlytodolist.TODO_ACTION";



// Fragment administration
Expand Down Expand Up @@ -142,6 +151,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
int notificationDone;
private int activeList = -1;

//Pomodoro
private boolean pomodoroInstalled = false;


@Override
public boolean onCreateOptionsMenu(Menu menu) {
Expand Down Expand Up @@ -251,6 +263,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
Expand Down Expand Up @@ -296,9 +309,13 @@ protected void onCreate(Bundle savedInstanceState) {
TodoTask currentTask = tasks.getTasks().get(taskID);
currentTask.setReminderTime(System.currentTimeMillis() + notificationDone);
sendToDatabase(currentTask);

}
} */

if (getIntent().getIntExtra(COMMAND, -1) == COMMAND_UPDATE) {
updateTodoFromPomodoro();
}

authAndGuiInit(savedInstanceState);
TodoList defaultList = new TodoList();
Expand All @@ -308,6 +325,8 @@ protected void onCreate(Bundle savedInstanceState) {
if(activeList != -1) {
showTasksOfList(activeList);
}


}


Expand Down Expand Up @@ -567,6 +586,9 @@ protected void onRestart() {
@Override
protected void onResume() {
super.onResume();
Log.d("RESUME","resume");
// Check if Pomodoro is installed
pomodoroInstalled = checkIfPomodoroInstalled();

if (this.isInitialized && !this.isUnlocked && (this.unlockUntil == -1 || System.currentTimeMillis() > this.unlockUntil)) {
// restart activity to show pin dialog again
Expand Down Expand Up @@ -1027,11 +1049,18 @@ public void onCreateContextMenu(ContextMenu menu, View v,
MenuInflater inflater = this.getMenuInflater();
menu.setHeaderView(Helper.getMenuHeader(getBaseContext(), getBaseContext().getString(R.string.select_option)));

int workItemId;
// context menu for child items
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
inflater.inflate(R.menu.todo_subtask_long_click, menu);
workItemId = R.id.work_subtask;
} else { // context menu for group items
inflater.inflate(R.menu.todo_task_long_click, menu);
workItemId = R.id.work_task;
}

if (pomodoroInstalled) {
menu.findItem(workItemId).setVisible(true);
}
}

Expand Down Expand Up @@ -1129,14 +1158,64 @@ public void onClick(View v) {
snackbar.show();
break;

case R.id.work_task:

Log.i(MainActivity.class.getSimpleName(), "START TASK");
sendToPomodoro(longClickedTodo.getLeft());
break;

case R.id.work_subtask:

Log.i(MainActivity.class.getSimpleName(), "START SUBTASK");
sendToPomodoro(longClickedTodo.getRight());
break;

default:
throw new IllegalArgumentException("Invalid menu item selected.");
}

return super.onContextItemSelected(item);
}

private void sendToPomodoro(BaseTodo todo) {
Intent pomodoro = new Intent(POMODORO_ACTION);
int todoId = todo.getId();
String todoDescription;
if (todo.getDescription() == null) {
todoDescription = "";
} else {
todoDescription = todo.getDescription();
}

String todoName = todo.getName();
pomodoro.putExtra("todo_id", todoId)
.putExtra("todo_name", todoName)
.putExtra("todo_description", todoDescription)
.putExtra("todo_progress", todo.getProgress())
.setPackage("org.secuso.privacyfriendlyproductivitytimer")
.setFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
sendBroadcast(pomodoro, "org.secuso.privacyfriendlytodolist.TODO_PERMISSION");
finish();
}

private void updateTodoFromPomodoro() {
TodoTask todoRe = new TodoTask();
todoRe.setChangedFromPomodoro(); //Change the dbState to UPDATE_FROM_POMODORO
//todoRe.setPriority(TodoTask.Priority.HIGH);
todoRe.setName(getIntent().getStringExtra("todo_name"));
todoRe.setId(getIntent().getIntExtra("todo_id", -1));
todoRe.setProgress(getIntent().getIntExtra("todo_progress", -1));
if (todoRe.getProgress() == 100) {
// Set task as done
todoRe.setDone(true);
//todoRe.doneStatusChanged();
}
if (todoRe.getProgress() != -1) {
sendToDatabase(todoRe); //Update the existing entry, if no subtask
}

//super.onResume();
}

public void hints() {

Expand Down Expand Up @@ -1170,5 +1249,16 @@ public void hints() {
secondAlert.clearAnimation();
}
}

private boolean checkIfPomodoroInstalled() {
try {
getPackageManager().getPackageInfo("org.secuso.privacyfriendlyproductivitytimer", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}


}

1 change: 1 addition & 0 deletions app/src/main/res/menu/todo_subtask_long_click.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/change_subtask" android:title="@string/edit_subtask"/>
<item android:id="@+id/delete_subtask" android:title="@string/delete_subtask"/>
<item android:id="@+id/work_subtask" android:title="@string/work_subtask" android:visible="false"/>
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/menu/todo_task_long_click.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/change_task" android:title="@string/edit_task"/>
<item android:id="@+id/delete_task" android:title="@string/delete_task"/>
<item android:id="@+id/work_task" android:title="@string/work_task" android:visible="false"/>
</menu>
Loading