diff --git a/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java b/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java index 08021d908..2c20f301b 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java +++ b/app/src/main/java/com/simplemobiletools/calendar/CalendarImpl.java @@ -1,8 +1,5 @@ package com.simplemobiletools.calendar; -import android.content.Context; -import android.util.SparseBooleanArray; - import com.simplemobiletools.calendar.models.Day; import org.joda.time.DateTime; @@ -18,15 +15,11 @@ public class CalendarImpl { private final Calendar mCallback; private final String mToday; - private final Context mContext; private DateTime mTargetDate; - private SparseBooleanArray mEvents; - public CalendarImpl(Calendar callback, Context context) { + public CalendarImpl(Calendar callback) { this.mCallback = callback; mToday = new DateTime().toString(Constants.DATE_PATTERN); - mContext = context; - mEvents = Config.newInstance(context).getEvents(); } public void updateCalendar(DateTime targetDate) { @@ -81,7 +74,7 @@ public class CalendarImpl { } private boolean hasEvent(String dayCode) { - return mEvents.get(Integer.parseInt(dayCode)); + return false; } private boolean isToday(DateTime targetDate, int curDayInMonth) { diff --git a/app/src/main/java/com/simplemobiletools/calendar/Config.java b/app/src/main/java/com/simplemobiletools/calendar/Config.java index 2b7163710..6b333c0ec 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Config.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Config.java @@ -2,7 +2,6 @@ package com.simplemobiletools.calendar; import android.content.Context; import android.content.SharedPreferences; -import android.util.SparseBooleanArray; public class Config { private SharedPreferences mPrefs; @@ -22,13 +21,4 @@ public class Config { public void setIsFirstRun(boolean firstRun) { mPrefs.edit().putBoolean(Constants.IS_FIRST_RUN, firstRun).apply(); } - - public SparseBooleanArray getEvents() { - final String json = mPrefs.getString(Constants.EVENTS, "{}"); - return Utils.deserializeJson(json); - } - - public void setEvents(SparseBooleanArray events) { - mPrefs.edit().putString(Constants.EVENTS, Utils.serializeArray(events)).apply(); - } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java new file mode 100644 index 000000000..e3f4400a3 --- /dev/null +++ b/app/src/main/java/com/simplemobiletools/calendar/DBHelper.java @@ -0,0 +1,83 @@ +package com.simplemobiletools.calendar; + +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.database.sqlite.SQLiteOpenHelper; + +import com.simplemobiletools.calendar.models.Event; + +import java.util.ArrayList; +import java.util.List; + +public class DBHelper extends SQLiteOpenHelper { + private static SQLiteDatabase mDb; + private static String[] mProjection; + + private static final String DB_NAME = "events.db"; + private static final int DB_VERSION = 1; + + private static final String TABLE_NAME = "events"; + private static final String COL_ID = "id"; + private static final String COL_START_TS = "start_ts"; + private static final String COL_END_TS = "end_ts"; + private static final String COL_TITLE = "title"; + private static final String COL_DESCRIPTION = "description"; + + public static DBHelper newInstance(Context context) { + return new DBHelper(context); + } + + public DBHelper(Context context) { + super(context, DB_NAME, null, DB_VERSION); + mDb = getWritableDatabase(); + mProjection = new String[]{COL_ID, COL_START_TS, COL_END_TS, COL_TITLE, COL_DESCRIPTION}; + } + + @Override + public void onCreate(SQLiteDatabase db) { + db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + + COL_ID + " INTEGER PRIMARY KEY, " + + COL_START_TS + " INTEGER," + + COL_END_TS + " INTEGER," + + COL_TITLE + " TEXT," + + COL_DESCRIPTION + " TEXT" + + ")"); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { + + } + + public void insert(Event event) { + final ContentValues values = new ContentValues(); + values.put(COL_START_TS, event.getStartTS()); + values.put(COL_END_TS, event.getEndTS()); + values.put(COL_TITLE, event.getTitle()); + values.put(COL_DESCRIPTION, event.getDescription()); + mDb.insert(TABLE_NAME, null, values); + } + + public List getEvents(int fromTS, int toTS) { + List events = new ArrayList<>(); + final String selection = COL_START_TS + " >= ? AND " + COL_START_TS + " <= ?"; + final String[] selectionArgs = {String.valueOf(fromTS), String.valueOf(toTS)}; + final Cursor cursor = mDb.query(TABLE_NAME, mProjection, selection, selectionArgs, null, null, null); + if (cursor != null) { + if (cursor.moveToFirst()) { + do { + final int id = cursor.getInt(cursor.getColumnIndex(COL_ID)); + final int startTS = cursor.getInt(cursor.getColumnIndex(COL_START_TS)); + final int endTS = cursor.getInt(cursor.getColumnIndex(COL_END_TS)); + final String title = cursor.getString(cursor.getColumnIndex(COL_TITLE)); + final String description = cursor.getString(cursor.getColumnIndex(COL_DESCRIPTION)); + events.add(new Event(id, startTS, endTS, title, description)); + } while (cursor.moveToNext()); + } + cursor.close(); + } + return events; + } +} diff --git a/app/src/main/java/com/simplemobiletools/calendar/MyWidgetProvider.java b/app/src/main/java/com/simplemobiletools/calendar/MyWidgetProvider.java index 5ec87cb1f..02577e350 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/MyWidgetProvider.java +++ b/app/src/main/java/com/simplemobiletools/calendar/MyWidgetProvider.java @@ -69,7 +69,7 @@ public class MyWidgetProvider extends AppWidgetProvider implements Calendar { final int bgColor = prefs.getInt(Constants.WIDGET_BG_COLOR, Color.BLACK); mRemoteViews.setInt(R.id.calendar_holder, "setBackgroundColor", bgColor); - mCalendar = new CalendarImpl(this, mContext); + mCalendar = new CalendarImpl(this); mCalendar.updateCalendar(new DateTime()); } diff --git a/app/src/main/java/com/simplemobiletools/calendar/Utils.java b/app/src/main/java/com/simplemobiletools/calendar/Utils.java index 4aa27d8f5..b12113d67 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/Utils.java +++ b/app/src/main/java/com/simplemobiletools/calendar/Utils.java @@ -1,16 +1,8 @@ package com.simplemobiletools.calendar; import android.graphics.Color; -import android.util.Log; -import android.util.SparseBooleanArray; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.Iterator; public class Utils { - private static final String TAG = Utils.class.getSimpleName(); public static int adjustAlpha(int color, float factor) { final int alpha = Math.round(Color.alpha(color) * factor); @@ -19,33 +11,4 @@ public class Utils { final int blue = Color.blue(color); return Color.argb(alpha, red, green, blue); } - - public static String serializeArray(SparseBooleanArray arr) { - JSONObject json = new JSONObject(); - try { - for (int i = 0; i < arr.size(); i++) { - int key = arr.keyAt(i); - json.put(String.valueOf(key), arr.get(key)); - } - } catch (JSONException e) { - Log.e(TAG, "serializeArray " + e.getMessage()); - } - return json.toString(); - } - - public static SparseBooleanArray deserializeJson(String string) { - final SparseBooleanArray sparseBooleanArray = new SparseBooleanArray(); - try { - final JSONObject json = new JSONObject(string); - final Iterator iter = json.keys(); - while (iter.hasNext()) { - String key = iter.next(); - Boolean value = (Boolean) json.get(key); - sparseBooleanArray.put(Integer.parseInt(key), value); - } - } catch (JSONException e) { - Log.e(TAG, "deserializeJson " + e.getMessage()); - } - return sparseBooleanArray; - } } diff --git a/app/src/main/java/com/simplemobiletools/calendar/activities/MainActivity.java b/app/src/main/java/com/simplemobiletools/calendar/activities/MainActivity.java index 19b264166..8a67a11a0 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/MainActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/MainActivity.java @@ -77,7 +77,7 @@ public class MainActivity extends AppCompatActivity implements Calendar { final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mCalendarHolder.getLayoutParams(); params.setMargins(mActivityMargin, mActivityMargin, mActivityMargin, mActivityMargin); - mCalendar = new CalendarImpl(this, getApplicationContext()); + mCalendar = new CalendarImpl(this); mCalendar.updateCalendar(new DateTime()); } diff --git a/app/src/main/java/com/simplemobiletools/calendar/activities/WidgetConfigureActivity.java b/app/src/main/java/com/simplemobiletools/calendar/activities/WidgetConfigureActivity.java index 2762d0a27..d5a690e6b 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/activities/WidgetConfigureActivity.java +++ b/app/src/main/java/com/simplemobiletools/calendar/activities/WidgetConfigureActivity.java @@ -18,10 +18,10 @@ import android.widget.TextView; import com.simplemobiletools.calendar.Calendar; import com.simplemobiletools.calendar.CalendarImpl; import com.simplemobiletools.calendar.Constants; -import com.simplemobiletools.calendar.models.Day; -import com.simplemobiletools.calendar.Utils; import com.simplemobiletools.calendar.MyWidgetProvider; import com.simplemobiletools.calendar.R; +import com.simplemobiletools.calendar.Utils; +import com.simplemobiletools.calendar.models.Day; import org.joda.time.DateTime; @@ -99,7 +99,7 @@ public class WidgetConfigureActivity extends AppCompatActivity implements Calend mBgSeekBar.setProgress((int) (mBgAlpha * 100)); updateBgColor(); - mCalendar = new CalendarImpl(this, getApplicationContext()); + mCalendar = new CalendarImpl(this); mCalendar.updateCalendar(new DateTime()); } diff --git a/app/src/main/java/com/simplemobiletools/calendar/models/Event.java b/app/src/main/java/com/simplemobiletools/calendar/models/Event.java index 46236b481..7046520be 100644 --- a/app/src/main/java/com/simplemobiletools/calendar/models/Event.java +++ b/app/src/main/java/com/simplemobiletools/calendar/models/Event.java @@ -1,16 +1,24 @@ package com.simplemobiletools.calendar.models; public class Event { + private final int mId; private final int mStartTS; private final int mEndTS; + private final String mTitle; private final String mDescription; - public Event(int startTS, int endTS, String description) { + public Event(int id, int startTS, int endTS, String title, String description) { + mId = id; mStartTS = startTS; mEndTS = endTS; + mTitle = title; mDescription = description; } + public int getId() { + return mId; + } + public int getStartTS() { return mStartTS; } @@ -19,7 +27,22 @@ public class Event { return mEndTS; } + public String getTitle() { + return mTitle; + } + public String getDescription() { return mDescription; } + + @Override + public String toString() { + return "Event {" + + "id=" + getId() + + ", startTS=" + getStartTS() + + ", endTS=" + getEndTS() + + ", title=" + getTitle() + + ", description=" + getDescription() + + "}"; + } } diff --git a/app/src/main/res/layout/activity_event.xml b/app/src/main/res/layout/activity_event.xml index 20f2bac21..ccd7b6b4e 100644 --- a/app/src/main/res/layout/activity_event.xml +++ b/app/src/main/res/layout/activity_event.xml @@ -96,10 +96,11 @@