Skip to content

Commit

Permalink
Crash fixes, permissions cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sevilaa committed Apr 17, 2024
1 parent c557d3d commit 6d33de4
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 78 deletions.
8 changes: 0 additions & 8 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />

<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />

<uses-permission android:name="android.permission.READ_SYNC_SETTINGS"/>
<uses-permission android:name="android.permission.READ_SYNC_STATS"/>
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS"/>

<application
android:name=".SwissOApplication"
android:allowBackup="true"
Expand Down
Binary file modified app/src/main/ic_launcher-playstore.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 24 additions & 28 deletions app/src/main/java/ch/swisso/CalendarManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,40 +37,34 @@ public class CalendarManager {
private static final int IDX_ALL_EVENTS = 0;
private static final int IDX_FAV_EVENTS = 1;

private boolean updating = false;
private final ActivityResultLauncher<String> permissionRequester;

public CalendarManager(MyActivity activity) {
act = activity;
permissionRequester = act.registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
checkPermissions();
} else {
Toast.makeText(act, R.string.cal_permission_denied, Toast.LENGTH_SHORT).show();
}
});
checkPermissions();
}

private void checkPermissions() {
if (!(ContextCompat.checkSelfPermission(act, Manifest.permission.READ_CALENDAR) == PackageManager.PERMISSION_GRANTED)) {
ActivityResultLauncher<String> resultLauncher = act.registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
checkPermissions();
} else {
Toast.makeText(act, "Events werden nicht im Kalender angezeigt", Toast.LENGTH_SHORT).show(); //TODO
}
});
resultLauncher.launch(Manifest.permission.READ_CALENDAR);
permissionRequester.launch(Manifest.permission.READ_CALENDAR);
return;
}
if (!(ContextCompat.checkSelfPermission(act, Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED)) {
ActivityResultLauncher<String> resultLauncher = act.registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
checkPermissions();
} else {
Toast.makeText(act, "Events werden nicht im Kalender angezeigt", Toast.LENGTH_SHORT).show(); //TODO
}
});
resultLauncher.launch(Manifest.permission.WRITE_CALENDAR);
permissionRequester.launch(Manifest.permission.WRITE_CALENDAR);
return;
}
setupCals();
}

private void setupCals() {
boolean initialSetup = false;
AccountManager accountManager = AccountManager.get(act);
Account[] accounts = accountManager.getAccountsByType(act.getString(R.string.account_type));
if (accounts.length == 0) {
Expand Down Expand Up @@ -106,12 +100,16 @@ private void setupCals() {
values.put(Calendars.SYNC_EVENTS, 1);
Uri result = cr.insert(asSyncAdapter(Calendars.CONTENT_URI), values);
calIds[i] = Long.parseLong(result.getLastPathSegment());
initialSetup = true;
} else {
cursor.moveToFirst();
calIds[i] = cursor.getLong(0);
}
cursor.close();
}
if (initialSetup && act instanceof MainActivity) {
updateEvents(((MainActivity) act).getEvents());
}
}

private Uri asSyncAdapter(@NonNull Uri uri) {
Expand All @@ -121,29 +119,27 @@ private Uri asSyncAdapter(@NonNull Uri uri) {
.appendQueryParameter(Calendars.ACCOUNT_TYPE, account.type).build();
}

public void updateEvents(ArrayList<Event> events) {
if (!updating) {
updating = true;
public void updateEvents(@NonNull ArrayList<Event> events) {
if (!events.isEmpty()) {
final Helper.ProcessedListener listener = successful -> handler.post(() -> {
if (successful) {
updating = false;
}
});

Runnable background = () -> {
asyncUpdateEvents(events);
asyncUpdateEvents(new ArrayList<>(events));
listener.onProcessed(true);
};

executor.execute(background);
}
}

public void updateFavEvent(Event event){
updateEvent(event, calIds[IDX_FAV_EVENTS]);
public void updateFavEvent(Event event) {
if (calIds != null) {
updateEvent(event, calIds[IDX_FAV_EVENTS]);
}
}

private void asyncUpdateEvents(ArrayList<Event> events){
private void asyncUpdateEvents(ArrayList<Event> events) {
if (calIds != null) {
ArrayList<String> checkedIds = new ArrayList<>();
ArrayList<String> checkedFavIds = new ArrayList<>();
Expand All @@ -158,7 +154,7 @@ private void asyncUpdateEvents(ArrayList<Event> events){
String[] ids = new String[2];
ids[IDX_ALL_EVENTS] = String.join(", ", checkedIds);
ids[IDX_FAV_EVENTS] = String.join(", ", checkedFavIds);
for(int i = 0; i < calIds.length; i++){
for (int i = 0; i < calIds.length; i++) {
act.getContentResolver().delete(asSyncAdapter(Events.CONTENT_URI), Events.CALENDAR_ID + " = " + calIds[i] + " AND " + Events._SYNC_ID + " NOT IN (" + ids[i] + ")", null);
}
}
Expand Down
21 changes: 12 additions & 9 deletions app/src/main/java/ch/swisso/Daten.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ public void close() {
dbHelper.close();
}

public boolean isOpen() {
return database.isOpen();
}

//Table Fruende, Clubs, Kats

public static String getTable(@NonNull ProfilFragment.ProfilList list){
switch (list){
public static String getTable(@NonNull ProfilFragment.ProfilList list) {
switch (list) {
case Club:
return SQLiteHelper.TABLE_Clubs;
case Freund:
Expand Down Expand Up @@ -78,20 +82,20 @@ public ArrayList<String> getProfilListList(ProfilFragment.ProfilList type) {

//Table Lists and Runners

public void insertList(ContentValues contentValues){
public void insertList(ContentValues contentValues) {
database.insert(SQLiteHelper.TABLE_Lists, null, contentValues);
}

public void deleteList(int listID){
public void deleteList(int listID) {
database.delete(SQLiteHelper.TABLE_Runners, SQLiteHelper.COLUMN_LIST + " = " + listID, null);
database.delete(SQLiteHelper.TABLE_Lists, SQLiteHelper.COLUMN_ID + " = " + listID, null);
}

public ArrayList<List> createListsByEvent(Event event){
public ArrayList<List> createListsByEvent(@NonNull Event event) {
Cursor cursor = database.query(SQLiteHelper.TABLE_Lists, null, SQLiteHelper.COLUMN_EVENT + " = " + event.getId(), null, null, null, null);
cursor.moveToFirst();
ArrayList<List> lists = new ArrayList<>();
while (!cursor.isAfterLast()){
while (!cursor.isAfterLast()) {
lists.add(new List(Helper.getInt(cursor, SQLiteHelper.COLUMN_ID), event, Helper.getInt(cursor, SQLiteHelper.COLUMN_LISTTYPE)));
cursor.moveToNext();
}
Expand All @@ -108,7 +112,7 @@ public boolean updateRunnersFromJson(String event_details) {
try {
JSONObject event = new JSONObject(event_details);
JSONArray lists = event.getJSONArray("lists");
for (int i = 0; i < lists.length(); i++){
for (int i = 0; i < lists.length(); i++) {
JSONObject list = lists.getJSONObject(i);
int listID = list.getInt(SQLiteHelper.COLUMN_ID);
deleteList(listID);
Expand Down Expand Up @@ -172,8 +176,7 @@ public Cursor getFilteredRunnersByList(int listId, String content, String filter
} else {
return null;
}
}
else if (!content.equals(Helper.SingleListTab.tabAlle)){
} else if (!content.equals(Helper.SingleListTab.tabAlle)) {
where += " AND " + SQLiteHelper.COLUMN_KATEGORIE + " = '" + content + "'";
}
return database.query(SQLiteHelper.TABLE_Runners, null, where, null, null, null, order);
Expand Down
78 changes: 45 additions & 33 deletions app/src/main/java/ch/swisso/SwissOParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ private void loadEvents(String json, OnParserResult onParserResult) {
});

Runnable background = () -> {
boolean result = act.getDaten().updateEventsFromJson(json);
listener.onProcessed(result);
if(act.getDaten().isOpen()) {
boolean result = act.getDaten().updateEventsFromJson(json);
listener.onProcessed(result);
}
else{
listener.onProcessed(false);
}
};

executor.execute(background);
Expand All @@ -95,8 +100,13 @@ private void loadEventDetails(String json, OnParserResult onParserResult) {
});

Runnable background = () -> {
boolean result = act.getDaten().updateRunnersFromJson(json);
listener.onProcessed(result);
if(act.getDaten().isOpen()) {
boolean result = act.getDaten().updateRunnersFromJson(json);
listener.onProcessed(result);
}
else{
listener.onProcessed(false);
}
};

executor.execute(background);
Expand All @@ -107,38 +117,40 @@ private void loadMessages(String json) {
try {
JSONArray array = new JSONArray(json);
Daten daten = act.getDaten();
Cursor c = daten.getMessages();
ArrayList<Integer> ids = new ArrayList<>();
c.moveToFirst();
while (!c.isAfterLast()) {
ids.add(Helper.getInt(c, SQLiteHelper.COLUMN_ID));
c.moveToNext();
}
c.close();
for (int black : Helper.blacklistMessages) {
if (!ids.contains(black)) {
ids.add(black);
} else {
daten.deleteMessage(black);
if (daten.isOpen()) {
Cursor c = daten.getMessages();
ArrayList<Integer> ids = new ArrayList<>();
c.moveToFirst();
while (!c.isAfterLast()) {
ids.add(Helper.getInt(c, SQLiteHelper.COLUMN_ID));
c.moveToNext();
}
}
for (int i = 0; i < array.length(); i++) {
JSONObject jsonMessage = array.getJSONObject(i);
int id = jsonMessage.getInt(SQLiteHelper.COLUMN_ID);
if (!ids.contains(id)) {
ContentValues contentValues = new ContentValues();
contentValues.put(SQLiteHelper.COLUMN_ID, id);
contentValues.put(SQLiteHelper.COLUMN_VIEWED, 0);
contentValues.put(SQLiteHelper.COLUMN_MESSAGE, jsonMessage.getString("content"));
daten.insertMessage(contentValues);
} else {
ids.remove((Integer) id);
c.close();
for (int black : Helper.blacklistMessages) {
if (!ids.contains(black)) {
ids.add(black);
} else {
daten.deleteMessage(black);
}
}
for (int i = 0; i < array.length(); i++) {
JSONObject jsonMessage = array.getJSONObject(i);
int id = jsonMessage.getInt(SQLiteHelper.COLUMN_ID);
if (!ids.contains(id)) {
ContentValues contentValues = new ContentValues();
contentValues.put(SQLiteHelper.COLUMN_ID, id);
contentValues.put(SQLiteHelper.COLUMN_VIEWED, 0);
contentValues.put(SQLiteHelper.COLUMN_MESSAGE, jsonMessage.getString("content"));
daten.insertMessage(contentValues);
} else {
ids.remove((Integer) id);
}
}
for (int id : ids) {
daten.deleteMessage(id);
}
((MainActivity) act).showMessages();
}
for (int id : ids) {
daten.deleteMessage(id);
}
((MainActivity) act).showMessages();
} catch (JSONException e) {
Log.e("SwissO", "Messages loading failed", e);
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
<string name="startnummer">Startnummer</string>
<string name="rang">Rang</string>

<string name="cal_permission_denied">Events werden nicht im Kalender angezeigt</string>

<string name="empty" />

<string-array name="sorting_startlist_entires">
Expand Down

0 comments on commit 6d33de4

Please sign in to comment.