1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-28 19:45:50 +01:00

fix calendar blacklist, view and storage

- view: unselect calendar that aren't blacklisted
- use more unique string to identify and store blacklisted calendars
This commit is contained in:
Ludovic Jozeau 2022-05-27 00:49:50 +02:00 committed by Gitea
parent 90145ce977
commit b07dc6f2b2
5 changed files with 77 additions and 23 deletions

View File

@ -559,11 +559,11 @@ public class GBApplication extends Application {
private static HashSet<String> calendars_blacklist = null;
public static boolean calendarIsBlacklisted(String calendarDisplayName) {
public static boolean calendarIsBlacklisted(String calendarUniqueName) {
if (calendars_blacklist == null) {
GB.log("calendarIsBlacklisted: calendars_blacklist is null!", GB.INFO, null);
}
return calendars_blacklist != null && calendars_blacklist.contains(calendarDisplayName);
return calendars_blacklist != null && calendars_blacklist.contains(calendarUniqueName);
}
public static void setCalendarsBlackList(Set<String> calendarNames) {
@ -577,14 +577,18 @@ public class GBApplication extends Application {
saveCalendarsBlackList();
}
public static void addCalendarToBlacklist(String calendarDisplayName) {
if (calendars_blacklist.add(calendarDisplayName)) {
public static void addCalendarToBlacklist(String calendarUniqueName) {
if (calendars_blacklist.add(calendarUniqueName)) {
GB.log("Blacklisted calendar " + calendarUniqueName, GB.INFO, null);
saveCalendarsBlackList();
} else {
GB.log("Calendar " + calendarUniqueName + " already blacklisted!", GB.WARN, null);
}
}
public static void removeFromCalendarBlacklist(String calendarDisplayName) {
calendars_blacklist.remove(calendarDisplayName);
public static void removeFromCalendarBlacklist(String calendarUniqueName) {
calendars_blacklist.remove(calendarUniqueName);
GB.log("Unblacklisted calendar " + calendarUniqueName, GB.INFO, null);
saveCalendarsBlackList();
}

View File

@ -51,6 +51,7 @@ public class CalBlacklistActivity extends AbstractGBActivity {
private final String[] EVENT_PROJECTION = new String[]{
CalendarContract.Calendars.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME,
CalendarContract.Calendars.CALENDAR_COLOR
};
private ArrayList<Calendar> calendarsArrayList;
@ -69,7 +70,7 @@ public class CalBlacklistActivity extends AbstractGBActivity {
try (Cursor cur = getContentResolver().query(uri, EVENT_PROJECTION, null, null, null)) {
calendarsArrayList = new ArrayList<>();
while (cur != null && cur.moveToNext()) {
calendarsArrayList.add(new Calendar(cur.getString(0), cur.getInt(1)));
calendarsArrayList.add(new Calendar(cur.getString(0), cur.getString(1), cur.getInt(2)));
}
}
@ -82,9 +83,9 @@ public class CalBlacklistActivity extends AbstractGBActivity {
CheckBox selected = (CheckBox) view.findViewById(R.id.item_checkbox);
toggleEntry(view);
if (selected.isChecked()) {
GBApplication.addCalendarToBlacklist(item.displayName);
GBApplication.addCalendarToBlacklist(item.getUniqueString());
} else {
GBApplication.removeFromCalendarBlacklist(item.displayName);
GBApplication.removeFromCalendarBlacklist(item.getUniqueString());
}
}
});
@ -112,12 +113,18 @@ public class CalBlacklistActivity extends AbstractGBActivity {
class Calendar {
private final String displayName;
private final String accountName;
private final int color;
public Calendar(String displayName, int color) {
public Calendar(String displayName, String accountName, int color) {
this.displayName = displayName;
this.accountName = accountName;
this.color = color;
}
public String getUniqueString() {
return accountName + '/' + displayName;
}
}
private class CalendarListAdapter extends ArrayAdapter<Calendar> {
@ -138,13 +145,16 @@ public class CalBlacklistActivity extends AbstractGBActivity {
View color = view.findViewById(R.id.calendar_color);
TextView name = (TextView) view.findViewById(R.id.calendar_name);
TextView ownerAccount = (TextView) view.findViewById(R.id.calendar_owner_account);
CheckBox checked = (CheckBox) view.findViewById(R.id.item_checkbox);
if (GBApplication.calendarIsBlacklisted(item.displayName) && !checked.isChecked()) {
if (GBApplication.calendarIsBlacklisted(item.getUniqueString()) && !checked.isChecked() ||
!GBApplication.calendarIsBlacklisted(item.getUniqueString()) && checked.isChecked()) {
toggleEntry(view);
}
color.setBackgroundColor(item.color);
name.setText(item.displayName);
ownerAccount.setText(item.accountName);
return view;
}

View File

@ -21,6 +21,7 @@ import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Instances;
import android.text.format.Time;
@ -56,6 +57,8 @@ public class CalendarEvents {
Instances.DESCRIPTION,
Instances.EVENT_LOCATION,
Instances.CALENDAR_DISPLAY_NAME,
CalendarContract.Calendars.ACCOUNT_NAME,
Instances.CALENDAR_COLOR,
Instances.ALL_DAY
};
@ -101,12 +104,14 @@ public class CalendarEvents {
evtCursor.getString(5),
evtCursor.getString(6),
evtCursor.getString(7),
!evtCursor.getString(8).equals("0")
evtCursor.getString(8),
evtCursor.getInt(9),
!evtCursor.getString(10).equals("0")
);
if (!GBApplication.calendarIsBlacklisted(calEvent.getCalName())) {
if (!GBApplication.calendarIsBlacklisted(calEvent.getUniqueCalName())) {
calendarEventList.add(calEvent);
} else {
LOG.debug("calendar " + calEvent.getCalName() + " skipped because it's blacklisted");
LOG.debug("calendar " + calEvent.getUniqueCalName() + " skipped because it's blacklisted");
}
}
return true;
@ -124,9 +129,11 @@ public class CalendarEvents {
private String description;
private String location;
private String calName;
private String calAccountName;
private int color;
private boolean allDay;
public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName, boolean allDay) {
public CalendarEvent(long begin, long end, long id, String title, String description, String location, String calName, String calAccountName, int color, boolean allDay) {
this.begin = begin;
this.end = end;
this.id = id;
@ -134,6 +141,8 @@ public class CalendarEvents {
this.description = description;
this.location = location;
this.calName = calName;
this.calAccountName = calAccountName;
this.color = color;
this.allDay = allDay;
}
@ -182,6 +191,18 @@ public class CalendarEvents {
return calName;
}
public String getCalAccountName() {
return calAccountName;
}
public String getUniqueCalName() {
return getCalAccountName() + '/' + getCalName();
}
public int getColor() {
return color;
}
public boolean isAllDay() {
return allDay;
}
@ -197,6 +218,8 @@ public class CalendarEvents {
Objects.equals(this.getDescription(), e.getDescription()) &&
(this.getEnd() == e.getEnd()) &&
Objects.equals(this.getCalName(), e.getCalName()) &&
Objects.equals(this.getCalAccountName(), e.getCalAccountName()) &&
(this.getColor() == e.getColor()) &&
(this.isAllDay() == e.isAllDay());
} else {
return false;
@ -212,6 +235,8 @@ public class CalendarEvents {
result = 31 * result + Objects.hash(description);
result = 31 * result + Long.valueOf(end).hashCode();
result = 31 * result + Objects.hash(calName);
result = 31 * result + Objects.hash(calAccountName);
result = 31 * result + Integer.valueOf(color).hashCode();
result = 31 * result + Boolean.valueOf(allDay).hashCode();
return result;
}

View File

@ -25,8 +25,8 @@
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_toEndOf="@+id/item_checkbox"
android:paddingBottom="8dp"
android:paddingTop="8dp" />
android:paddingTop="8dp"
android:paddingBottom="8dp" />
<LinearLayout
android:layout_width="fill_parent"
@ -36,6 +36,16 @@
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/calendar_owner_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:maxLines="1"
android:text="TextView"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"
android:textSize="12sp" />
<TextView
android:id="@+id/calendar_name"
android:textAppearance="@style/TextAppearance.AppCompat.Subhead"

View File

@ -19,12 +19,17 @@ public class CalendarEventTest extends TestBase {
private static final long ID_1 = 100;
private static final long ID_2 = 101;
private static final String CALNAME_1 = "cal1";
private static final String CALACCOUNTNAME_1 = "account1";
private static final int COLOR_1 = 185489;
@Test
public void testHashCode() {
CalendarEvents.CalendarEvent c1 = new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, "something", null, null, CALNAME_1, false);
CalendarEvents.CalendarEvent c2 = new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, false);
CalendarEvents.CalendarEvent c3 = new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, null, null, "something", CALNAME_1, false);
public void testHashCode() {
CalendarEvents.CalendarEvent c1 =
new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, "something", null, null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false);
CalendarEvents.CalendarEvent c2 =
new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false);
CalendarEvents.CalendarEvent c3 =
new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, null, null, "something", CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false);
assertEquals(c1.hashCode(), c1.hashCode());
assertNotEquals(c1.hashCode(), c2.hashCode());
@ -35,7 +40,7 @@ public class CalendarEventTest extends TestBase {
@Test
public void testSync() {
List<CalendarEvents.CalendarEvent> eventList = new ArrayList<>();
eventList.add(new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, false));
eventList.add(new CalendarEvents.CalendarEvent(BEGIN, END, ID_1, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false));
GBDevice dummyGBDevice = createDummyGDevice("00:00:01:00:03");
dummyGBDevice.setState(GBDevice.State.INITIALIZED);
@ -44,7 +49,7 @@ public class CalendarEventTest extends TestBase {
testCR.syncCalendar(eventList);
eventList.add(new CalendarEvents.CalendarEvent(BEGIN, END, ID_2, null, "something", null, CALNAME_1, false));
eventList.add(new CalendarEvents.CalendarEvent(BEGIN, END, ID_2, null, "something", null, CALNAME_1, CALACCOUNTNAME_1, COLOR_1, false));
testCR.syncCalendar(eventList);
CalendarSyncStateDao calendarSyncStateDao = daoSession.getCalendarSyncStateDao();