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

merge activity #287

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions app/res/menu/detail_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
android:orderInCategory="100"
android:title="@string/Delete_activity"/>
<item
android:id="@+id/menu_recompute_activity"
android:id="@+id/menu_merge_activity"
android:orderInCategory="100"
android:title="@string/Recompute_activity"
android:title="@string/Merge_activity"
android:enabled="false"/>
</menu>
125 changes: 125 additions & 0 deletions app/src/org/runnerup/db/ActivityMerger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package org.runnerup.db;

/*
* Copyright (C) 2013 [email protected]
*
* This program 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
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import org.runnerup.common.util.Constants;


public class ActivityMerger implements Constants {
public static boolean canMerge(SQLiteDatabase db, long activityId) {
// previous activity must exist and be the same activity type
// there is a deleted flag in the DB, but it doesn't seem to be used?
// I deleted a previous activity (id = 50) and I successfully get the
// (undeleted) previous activity (id = 49)...
try {
long previousActivityId = previousActivityId(db, activityId);
long type = activityType(db, activityId);
long previousType = activityType(db, previousActivityId);
return type == previousType;
} catch (Exception e) {
return false;
}
}

public static void merge(SQLiteDatabase db, long activityId) {
long previousActivityId = previousActivityId(db, activityId);

// update the 3 activity tables - LOCATION, LAP and ACTIVITY
// through this operation we re-order the new laps based on the older laps
// and move everything to the current activity ID
updateLocation(db, activityId, previousActivityId);
updateLap(db, activityId, previousActivityId);
updateActivity(db, activityId, previousActivityId);

// cleanup
new ActivityCleaner().recompute(db, activityId);
DBHelper.deleteActivity(db, previousActivityId);
}

private static void updateActivity(SQLiteDatabase db, long activityId, long previousActivityId) {
// update current activity start time with older activity start time
db.execSQL("UPDATE " + DB.ACTIVITY.TABLE + " SET " + DB.ACTIVITY.START_TIME + " = " + startTime(db, previousActivityId) +
" WHERE _id = " + activityId);
}

private static void updateLap(SQLiteDatabase db, long activityId, long previousActivityId) {
// # VERIFY THAT time IS UPDATED BY recompute IF NECESSARY

// newer activity lap numbers should continue where previousActivity laps stopped
// lap: old min = 0, new min = 0, old max = x, new max = y
long nextLap = lapMinMax(db, DB.LAP.TABLE, "MAX", previousActivityId) + 1;
// has this code already run? if so, don't re-run it.
if (lapMinMax(db, DB.LAP.TABLE, "MIN", activityId) == 0 && nextLap !=0)
db.execSQL("UPDATE " + DB.LAP.TABLE + " SET " + DB.LAP.LAP + "=" + DB.LAP.LAP + "+" + nextLap +
" WHERE " + DB.LAP.ACTIVITY + "=" + activityId);

// assign all older laps to the current activity
db.execSQL("UPDATE " + DB.LAP.TABLE + " SET " + DB.LAP.ACTIVITY + " = " + activityId +
" WHERE " + DB.LAP.ACTIVITY + " = " + previousActivityId);
}

private static void updateLocation(SQLiteDatabase db, long activityId, long previousActivityId) {
// location: set middle start/end type to pause/resume
ContentValues values = new ContentValues();
values.put(DB.LOCATION.TYPE, DB.LOCATION.TYPE_PAUSE);
db.update(DB.LOCATION.TABLE, values, DB.LOCATION.TYPE + "=" + DB.LOCATION.TYPE_END + " AND " + DB.LOCATION.ACTIVITY + "=" + previousActivityId, null);
values = new ContentValues();
values.put(DB.LOCATION.TYPE, DB.LOCATION.TYPE_RESUME);
db.update(DB.LOCATION.TABLE, values, DB.LOCATION.TYPE + "=" + DB.LOCATION.TYPE_START + " AND " + DB.LOCATION.ACTIVITY + "=" + activityId, null);

// location: newer lap numbers should continue where previousActivity laps stopped
long nextLap = lapMinMax(db, DB.LOCATION.TABLE, "MAX", previousActivityId) + 1;
// has this code already run? if so, don't re-run it.
if (lapMinMax(db, DB.LOCATION.TABLE, "MIN", activityId) == 0 && nextLap !=0)
db.execSQL("UPDATE " + DB.LOCATION.TABLE + " SET " + DB.LOCATION.LAP + "=" + DB.LOCATION.LAP + "+" + nextLap +
" WHERE " + DB.LOCATION.ACTIVITY + "=" + activityId);

// location: assign newer activityId to older locations
values = new ContentValues();
values.put(DB.LOCATION.ACTIVITY, activityId);
db.update(DB.LOCATION.TABLE, values, DB.LOCATION.ACTIVITY + "=" + previousActivityId, null);
}

private static long activityType(SQLiteDatabase db, long activityId) {
return db.compileStatement("SELECT type FROM " + DB.ACTIVITY.TABLE +
" WHERE _id = " + activityId).simpleQueryForLong();
}

private static long previousActivityId(SQLiteDatabase db, long activityId) {
return db.compileStatement("SELECT MAX(_id) FROM " + DB.ACTIVITY.TABLE +
" WHERE _id < " + activityId).simpleQueryForLong();
}

private static long startTime(SQLiteDatabase db, long activityId) {
return db.compileStatement("SELECT " + DB.ACTIVITY.START_TIME + " FROM " + DB.ACTIVITY.TABLE +
" WHERE _id = " + activityId).simpleQueryForLong();
}

private static long lapMinMax(SQLiteDatabase db, String table, String minMax, long activityId) {
try {
return db.compileStatement("SELECT " + minMax + "(" + DB.LAP.LAP + ") FROM " + table +
" WHERE " + DB.LAP.ACTIVITY + "=" + activityId).simpleQueryForLong();
} catch (Exception e) {
// no laps, return -1
return -1;
}
}
}
34 changes: 26 additions & 8 deletions app/src/org/runnerup/view/DetailActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
import org.runnerup.content.ActivityProvider;
import org.runnerup.content.WorkoutFileProvider;
import org.runnerup.db.ActivityCleaner;
import org.runnerup.db.ActivityMerger;
import org.runnerup.db.DBHelper;
import org.runnerup.db.entities.ActivityEntity;
import org.runnerup.db.entities.LocationEntity;
import org.runnerup.export.SyncManager;
import org.runnerup.export.Synchronizer;
Expand Down Expand Up @@ -122,7 +122,7 @@ public class DetailActivity extends FragmentActivity implements Constants {

TitleSpinner sport = null;
EditText notes = null;
MenuItem recomputeMenuItem = null;
MenuItem mergeMenuItem = null;

MapView map = null;
AsyncTask<String, String, Route> loadRouteTask = null;
Expand Down Expand Up @@ -269,15 +269,16 @@ private void setEdit(boolean value) {
uploadButton.setEnabled(value);
WidgetUtil.setEditable(notes, value);
sport.setEnabled(value);
if (recomputeMenuItem != null)
recomputeMenuItem.setEnabled(value);
if (mergeMenuItem != null) {
mergeMenuItem.setEnabled(value);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (mode == MODE_DETAILS) {
getMenuInflater().inflate(R.menu.detail_menu, menu);
recomputeMenuItem = menu.findItem(R.id.menu_recompute_activity);
mergeMenuItem = menu.findItem(R.id.menu_merge_activity);
}
return true;
}
Expand All @@ -295,9 +296,26 @@ public boolean onOptionsItemSelected(MenuItem item) {
requery();
}
break;
case R.id.menu_recompute_activity:
new ActivityCleaner().recompute(mDB, mID);
requery();
case R.id.menu_merge_activity:
if (ActivityMerger.canMerge(mDB, mID)) {
ActivityMerger.merge(mDB, mID);
requery();
fillHeaderData();
loadRoute();
} else {
AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
builder1.setMessage("Previous activity must be of the same type to merge.");
builder1.setCancelable(true);
builder1.setNeutralButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});

AlertDialog alert11 = builder1.create();
alert11.show();
}
break;
case R.id.menu_share_activity:
shareActivity();
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<string name="Edit_activity">Aktivität bearbeiten</string>
<string name="Share_activity">Aktivität teilen</string>
<string name="Delete_activity">Aktivität löschen</string>
<string name="Recompute_activity">Aktivität neu berechnen</string>
<string name="Merge_activity">Aktivität neu berechnen</string>
<string name="Configure_heart_rate_zones">Herzfrequenzzonen konfigurieren</string>
<string name="Clear_heart_rate_zone_settings">Herzfrequenzzoneneinstellungen zurücksetzen</string>
<string name="Manage_accounts">Benutzerkonten verwalten</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-en-rNL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<string name="Edit_activity">Activiteit bewerken</string>
<string name="Share_activity">Activiteit delen</string>
<string name="Delete_activity">Activiteit verwijderen</string>
<string name="Recompute_activity">Activiteit herberekenen</string>
<string name="Merge_activity">Activiteit herberekenen</string>
<string name="Configure_heart_rate_zones">Hartslagzones instellen</string>
<string name="Clear_heart_rate_zone_settings">Hartslagzone instellingen verwijderen</string>
<string name="Manage_accounts">Accounts beheren</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<string name="Edit_activity">Éditer la séance</string>
<string name="Share_activity">Partager la séance</string>
<string name="Delete_activity">Supprimer la séance</string>
<string name="Recompute_activity">Recalculer la séance</string>
<string name="Merge_activity">Recalculer la séance</string>
<string name="Configure_heart_rate_zones">Configurer les zones de fréq. cardiaque</string>
<string name="Clear_heart_rate_zone_settings">Supprimer les paramètres de zone de fréq. cardiaque</string>
<string name="Manage_accounts">Gérer les comptes</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<string name="Edit_activity">Modifica attività</string>
<string name="Share_activity">Condividi attività</string>
<string name="Delete_activity">Elimina attività</string>
<string name="Recompute_activity">Ricalcola attività</string>
<string name="Merge_activity">Ricalcola attività</string>
<string name="Configure_heart_rate_zones">Configura zone BPM</string>
<string name="Clear_heart_rate_zone_settings">Elimina impostazioni zona BPM</string>
<string name="Manage_accounts">Gestisci account</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-ja/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<string name="Edit_activity">アクティビティを編集</string>
<string name="Share_activity">アクティビティを共有</string>
<string name="Delete_activity">アクティビティを削除</string>
<string name="Recompute_activity">アクティビティを再計算</string>
<string name="Merge_activity">アクティビティを再計算</string>
<string name="Configure_heart_rate_zones">心拍数ゾーンを設定</string>
<string name="Clear_heart_rate_zone_settings">心拍数ゾーンの設定をクリア</string>
<string name="Manage_accounts">アカウントの管理</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-nl-rNL/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<string name="Edit_activity">Activiteit bewerken</string>
<string name="Share_activity">Activiteit delen</string>
<string name="Delete_activity">Activiteit verwijderen</string>
<string name="Recompute_activity">Activiteit herberekenen</string>
<string name="Merge_activity">Activiteit herberekenen</string>
<string name="Configure_heart_rate_zones">Hartslagzones instellen</string>
<string name="Clear_heart_rate_zone_settings">Hartslagzone instellingen verwijderen</string>
<string name="Manage_accounts">Accounts beheren</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-nl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
<string name="Edit_activity">Activiteit bewerken</string>
<string name="Share_activity">Activiteit delen</string>
<string name="Delete_activity">Activiteit verwijderen</string>
<string name="Recompute_activity">Activiteit herberekenen</string>
<string name="Merge_activity">Activiteit herberekenen</string>
<string name="Configure_heart_rate_zones">Hartslagzones instellen</string>
<string name="Clear_heart_rate_zone_settings">Hartslagzone instellingen verwijderen</string>
<string name="Manage_accounts">Accounts beheren</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<string name="Edit_activity">Edytuj aktywność</string>
<string name="Share_activity">Udostępnij aktywność</string>
<string name="Delete_activity">Usuń aktywność</string>
<string name="Recompute_activity">Przelicz aktywność</string>
<string name="Merge_activity">Przelicz aktywność</string>
<string name="Configure_heart_rate_zones">Ustaw strefy tętna</string>
<string name="Clear_heart_rate_zone_settings">Wyczyść ustawienia stref tętna</string>
<string name="Manage_accounts">Zarządzaj kontami</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-pt-rBR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<string name="Edit_activity">Editar atividade</string>
<string name="Share_activity">Compartilhar atividade</string>
<string name="Delete_activity">Excluir atividade</string>
<string name="Recompute_activity">Recalcular atividade</string>
<string name="Merge_activity">Recalcular atividade</string>
<string name="Configure_heart_rate_zones">Configurar zonas de frequência cardíaca</string>
<string name="Clear_heart_rate_zone_settings">Apagar configurações da zona de frequência cardíaca</string>
<string name="Manage_accounts">Gerenciar contas</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
<string name="Edit_activity">Редактировать</string>
<string name="Share_activity">Опубликовать</string>
<string name="Delete_activity">Удалить</string>
<string name="Recompute_activity">Пересчитать</string>
<string name="Merge_activity">Пересчитать</string>
<string name="Configure_heart_rate_zones">Настройка зон ЧСС</string>
<string name="Clear_heart_rate_zone_settings">Очистить зоны ЧСС</string>
<string name="Manage_accounts">Управление аккаунтами</string>
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@
<string name="Edit_activity">Edit activity</string>
<string name="Share_activity">Share activity</string>
<string name="Delete_activity">Delete activity</string>
<string name="Recompute_activity">Recompute activity</string>
<string name="Merge_activity">Merge activity</string>
<string name="Configure_heart_rate_zones">Configure heart rate zones</string>
<string name="Clear_heart_rate_zone_settings">Clear heart rate zone settings</string>
<string name="Manage_accounts">Manage accounts</string>
Expand Down