mirror of https://github.com/iSoron/uhabits.git
parent
dfe5c4954e
commit
581197be03
@ -0,0 +1,175 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.dialogs;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.app.Dialog;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.view.WindowManager.LayoutParams;
|
||||||
|
import android.widget.AdapterView;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.ListView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileFilter;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class FilePickerDialog implements AdapterView.OnItemClickListener
|
||||||
|
{
|
||||||
|
private static final String PARENT_DIR = "..";
|
||||||
|
|
||||||
|
private final Activity activity;
|
||||||
|
private ListView list;
|
||||||
|
private Dialog dialog;
|
||||||
|
private File currentPath;
|
||||||
|
|
||||||
|
public interface OnFileSelectedListener
|
||||||
|
{
|
||||||
|
void onFileSelected(File file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private OnFileSelectedListener fileListener;
|
||||||
|
|
||||||
|
public FilePickerDialog(Activity activity, File initialDirectory)
|
||||||
|
{
|
||||||
|
this.activity = activity;
|
||||||
|
|
||||||
|
list = new ListView(activity);
|
||||||
|
list.setOnItemClickListener(this);
|
||||||
|
|
||||||
|
dialog = new Dialog(activity);
|
||||||
|
dialog.setContentView(list);
|
||||||
|
dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
|
||||||
|
|
||||||
|
navigateTo(initialDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onItemClick(AdapterView<?> parent, View view, int which, long id)
|
||||||
|
{
|
||||||
|
String filename = (String) list.getItemAtPosition(which);
|
||||||
|
File file;
|
||||||
|
|
||||||
|
if (filename.equals(PARENT_DIR))
|
||||||
|
file = currentPath.getParentFile();
|
||||||
|
else
|
||||||
|
file = new File(currentPath, filename);
|
||||||
|
|
||||||
|
if (file.isDirectory())
|
||||||
|
{
|
||||||
|
navigateTo(file);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (fileListener != null) fileListener.onFileSelected(file);
|
||||||
|
dialog.dismiss();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void show()
|
||||||
|
{
|
||||||
|
dialog.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFileListener(OnFileSelectedListener fileListener)
|
||||||
|
{
|
||||||
|
this.fileListener = fileListener;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void navigateTo(File path)
|
||||||
|
{
|
||||||
|
if (!path.exists()) return;
|
||||||
|
|
||||||
|
File[] dirs = path.listFiles(new ReadableDirFilter());
|
||||||
|
File[] files = path.listFiles(new RegularReadableFileFilter());
|
||||||
|
if(dirs == null || files == null) return;
|
||||||
|
|
||||||
|
this.currentPath = path;
|
||||||
|
dialog.setTitle(currentPath.getPath());
|
||||||
|
list.setAdapter(new FilePickerAdapter(getFileList(path, dirs, files)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private String[] getFileList(File path, File[] dirs, File[] files)
|
||||||
|
{
|
||||||
|
int count = 0;
|
||||||
|
int length = dirs.length + files.length;
|
||||||
|
String[] fileList;
|
||||||
|
|
||||||
|
if (path.getParentFile() == null || !path.getParentFile().canRead())
|
||||||
|
{
|
||||||
|
fileList = new String[length];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
fileList = new String[length + 1];
|
||||||
|
fileList[count++] = PARENT_DIR;
|
||||||
|
}
|
||||||
|
|
||||||
|
Arrays.sort(dirs);
|
||||||
|
Arrays.sort(files);
|
||||||
|
|
||||||
|
for (File dir : dirs)
|
||||||
|
fileList[count++] = dir.getName();
|
||||||
|
|
||||||
|
for (File file : files)
|
||||||
|
fileList[count++] = file.getName();
|
||||||
|
|
||||||
|
return fileList;
|
||||||
|
}
|
||||||
|
|
||||||
|
private class FilePickerAdapter extends ArrayAdapter<String>
|
||||||
|
{
|
||||||
|
public FilePickerAdapter(@NonNull String[] fileList)
|
||||||
|
{
|
||||||
|
super(FilePickerDialog.this.activity, android.R.layout.simple_list_item_1, fileList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public View getView(int pos, View view, ViewGroup parent)
|
||||||
|
{
|
||||||
|
view = super.getView(pos, view, parent);
|
||||||
|
TextView tv = (TextView) view;
|
||||||
|
tv.setSingleLine(true);
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ReadableDirFilter implements FileFilter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file)
|
||||||
|
{
|
||||||
|
return (file.isDirectory() && file.canRead());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class RegularReadableFileFilter implements FileFilter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean accept(File file)
|
||||||
|
{
|
||||||
|
return !file.isDirectory() && file.canRead();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,109 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.fragments;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ProgressBar;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.io.GenericImporter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class ImportHabitsAsyncTask extends AsyncTask<Void, Void, Void>
|
||||||
|
{
|
||||||
|
public static final int SUCCESS = 1;
|
||||||
|
public static final int NOT_RECOGNIZED = 2;
|
||||||
|
public static final int FAILED = 3;
|
||||||
|
|
||||||
|
public interface Listener
|
||||||
|
{
|
||||||
|
void onImportFinished(int result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private final ProgressBar progressBar;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final File file;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Listener listener;
|
||||||
|
|
||||||
|
int result;
|
||||||
|
|
||||||
|
public ImportHabitsAsyncTask(@NonNull File file, @Nullable ProgressBar progressBar)
|
||||||
|
{
|
||||||
|
this.file = file;
|
||||||
|
this.progressBar = progressBar;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(@Nullable Listener listener)
|
||||||
|
{
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPreExecute()
|
||||||
|
{
|
||||||
|
if(progressBar != null)
|
||||||
|
{
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
progressBar.setVisibility(View.VISIBLE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onPostExecute(Void aVoid)
|
||||||
|
{
|
||||||
|
if(progressBar != null)
|
||||||
|
progressBar.setVisibility(View.GONE);
|
||||||
|
|
||||||
|
if(listener != null) listener.onImportFinished(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Void doInBackground(Void... params)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
GenericImporter importer = new GenericImporter();
|
||||||
|
if(importer.canHandle(file))
|
||||||
|
{
|
||||||
|
importer.importHabitsFromFile(file);
|
||||||
|
result = SUCCESS;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = NOT_RECOGNIZED;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
result = FAILED;
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.io;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public abstract class AbstractImporter
|
||||||
|
{
|
||||||
|
public abstract boolean canHandle(@NonNull File file) throws IOException;
|
||||||
|
|
||||||
|
public abstract void importHabitsFromFile(@NonNull File file) throws IOException;
|
||||||
|
|
||||||
|
public static boolean isSQLite3File(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
FileInputStream fis = new FileInputStream(file);
|
||||||
|
|
||||||
|
byte[] sqliteHeader = "SQLite format 3".getBytes();
|
||||||
|
byte[] buffer = new byte[sqliteHeader.length];
|
||||||
|
|
||||||
|
|
||||||
|
int count = fis.read(buffer);
|
||||||
|
if(count < sqliteHeader.length) return false;
|
||||||
|
|
||||||
|
return Arrays.equals(buffer, sqliteHeader);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.io;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GenericImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
List<AbstractImporter> importers;
|
||||||
|
|
||||||
|
public GenericImporter()
|
||||||
|
{
|
||||||
|
importers = new LinkedList<>();
|
||||||
|
importers.add(new RewireDBImporter());
|
||||||
|
importers.add(new TickmateDBImporter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
for(AbstractImporter importer : importers)
|
||||||
|
if(importer.canHandle(file)) return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
for(AbstractImporter importer : importers)
|
||||||
|
if(importer.canHandle(file))
|
||||||
|
importer.importHabitsFromFile(file);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,190 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.io;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.isoron.helpers.ActiveAndroidHelper;
|
||||||
|
import org.isoron.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
public class RewireDBImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
if(!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
|
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
Cursor c = db.rawQuery("select count(*) from SQLITE_MASTER where name=? or name=?",
|
||||||
|
new String[]{"CHECKINS", "UNIT"});
|
||||||
|
|
||||||
|
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
||||||
|
|
||||||
|
c.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
final SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
ActiveAndroidHelper.executeAsTransaction(new ActiveAndroidHelper.Command()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void execute()
|
||||||
|
{
|
||||||
|
createHabits(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createHabits(SQLiteDatabase db)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = db.rawQuery("select _id, name, description, schedule, active_days, " +
|
||||||
|
"repeating_count, days, period from habits", new String[0]);
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int id = c.getInt(0);
|
||||||
|
String name = c.getString(1);
|
||||||
|
String description = c.getString(2);
|
||||||
|
int schedule = c.getInt(3);
|
||||||
|
String activeDays = c.getString(4);
|
||||||
|
int repeatingCount = c.getInt(5);
|
||||||
|
int days = c.getInt(6);
|
||||||
|
int periodIndex = c.getInt(7);
|
||||||
|
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habit.name = name;
|
||||||
|
habit.description = description;
|
||||||
|
|
||||||
|
int periods[] = { 7, 31, 365 };
|
||||||
|
|
||||||
|
switch (schedule)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
habit.freqNum = activeDays.split(",").length;
|
||||||
|
habit.freqDen = 7;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 1:
|
||||||
|
habit.freqNum = days;
|
||||||
|
habit.freqDen = periods[periodIndex];
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
habit.freqNum = 1;
|
||||||
|
habit.freqDen = repeatingCount;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
habit.save();
|
||||||
|
|
||||||
|
createReminder(db, habit, id);
|
||||||
|
createCheckmarks(db, habit, id);
|
||||||
|
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createReminder(SQLiteDatabase db, Habit habit, int rewireHabitId)
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(rewireHabitId) };
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = db.rawQuery("select time, active_days from reminders where habit_id=? limit 1", params);
|
||||||
|
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
int rewireReminder = Integer.parseInt(c.getString(0));
|
||||||
|
if (rewireReminder <= 0 || rewireReminder >= 1440) return;
|
||||||
|
|
||||||
|
boolean reminderDays[] = new boolean[7];
|
||||||
|
|
||||||
|
String activeDays[] = c.getString(1).split(",");
|
||||||
|
for(String d : activeDays)
|
||||||
|
{
|
||||||
|
int idx = (Integer.parseInt(d) + 1) % 7;
|
||||||
|
reminderDays[idx] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
habit.reminderDays = DateHelper.packWeekdayList(reminderDays);
|
||||||
|
habit.reminderHour = rewireReminder / 60;
|
||||||
|
habit.reminderMin = rewireReminder % 60;
|
||||||
|
habit.save();
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if(c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createCheckmarks(@NonNull SQLiteDatabase db, @NonNull Habit habit, int rewireHabitId)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(rewireHabitId) };
|
||||||
|
c = db.rawQuery("select distinct date from checkins where habit_id=? and type=2", params);
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
String date = c.getString(0);
|
||||||
|
int year = Integer.parseInt(date.substring(0, 4));
|
||||||
|
int month = Integer.parseInt(date.substring(4, 6));
|
||||||
|
int day = Integer.parseInt(date.substring(6, 8));
|
||||||
|
|
||||||
|
GregorianCalendar cal = DateHelper.getStartOfTodayCalendar();
|
||||||
|
cal.set(year, month - 1, day);
|
||||||
|
|
||||||
|
habit.repetitions.toggle(cal.getTimeInMillis());
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,130 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.io;
|
||||||
|
|
||||||
|
import android.database.Cursor;
|
||||||
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
|
||||||
|
import org.isoron.helpers.ActiveAndroidHelper;
|
||||||
|
import org.isoron.helpers.DateHelper;
|
||||||
|
import org.isoron.uhabits.models.Habit;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
|
||||||
|
public class TickmateDBImporter extends AbstractImporter
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public boolean canHandle(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
if(!isSQLite3File(file)) return false;
|
||||||
|
|
||||||
|
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
Cursor c = db.rawQuery("select count(*) from SQLITE_MASTER where name=? or name=?",
|
||||||
|
new String[]{"tracks", "track2groups"});
|
||||||
|
|
||||||
|
boolean result = (c.moveToFirst() && c.getInt(0) == 2);
|
||||||
|
|
||||||
|
c.close();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void importHabitsFromFile(@NonNull File file) throws IOException
|
||||||
|
{
|
||||||
|
final SQLiteDatabase db = SQLiteDatabase.openDatabase(file.getPath(), null,
|
||||||
|
SQLiteDatabase.OPEN_READONLY);
|
||||||
|
|
||||||
|
ActiveAndroidHelper.executeAsTransaction(new ActiveAndroidHelper.Command()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void execute()
|
||||||
|
{
|
||||||
|
createHabits(db);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createHabits(SQLiteDatabase db)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
c = db.rawQuery("select _id, name, description from tracks", new String[0]);
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int id = c.getInt(0);
|
||||||
|
String name = c.getString(1);
|
||||||
|
String description = c.getString(2);
|
||||||
|
|
||||||
|
Habit habit = new Habit();
|
||||||
|
habit.name = name;
|
||||||
|
habit.description = description;
|
||||||
|
habit.freqNum = 1;
|
||||||
|
habit.freqDen = 1;
|
||||||
|
habit.save();
|
||||||
|
|
||||||
|
createCheckmarks(db, habit, id);
|
||||||
|
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createCheckmarks(@NonNull SQLiteDatabase db, @NonNull Habit habit, int tickmateTrackId)
|
||||||
|
{
|
||||||
|
Cursor c = null;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
String[] params = { Integer.toString(tickmateTrackId) };
|
||||||
|
c = db.rawQuery("select distinct year, month, day from ticks where _track_id=?", params);
|
||||||
|
if (!c.moveToFirst()) return;
|
||||||
|
|
||||||
|
do
|
||||||
|
{
|
||||||
|
int year = c.getInt(0);
|
||||||
|
int month = c.getInt(1);
|
||||||
|
int day = c.getInt(2);
|
||||||
|
|
||||||
|
GregorianCalendar cal = DateHelper.getStartOfTodayCalendar();
|
||||||
|
cal.set(year, month, day);
|
||||||
|
|
||||||
|
habit.repetitions.toggle(cal.getTimeInMillis());
|
||||||
|
}
|
||||||
|
while (c.moveToNext());
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
if (c != null) c.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue