Skip to content

Commit

Permalink
Better logging behaviour; read only switch; transmit prefs by hack
Browse files Browse the repository at this point in the history
  • Loading branch information
larkery committed Jul 18, 2022
1 parent cd7eae6 commit 5556012
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 382 deletions.
20 changes: 6 additions & 14 deletions app/src/main/java/com/larkery/simpleorgsync/PrefsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,20 @@
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import com.larkery.simpleorgsync.lib.Application;
import com.larkery.simpleorgsync.lib.FileUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

Expand All @@ -37,8 +32,6 @@ protected void onCreate(Bundle savedInstanceState) {
getFragmentManager()
.beginTransaction()
.replace(android.R.id.content, frag).commit();


}

@Override
Expand Down Expand Up @@ -149,7 +142,7 @@ public boolean onPreferenceClick(Preference preference) {
public boolean onPreferenceClick(Preference preference) {
final Application a = (Application)
OrgPreferenceFragment.this.getActivity().getApplication();
a.requestSync();
a.requestSync("user pressed button in prefs");
return true;
}
}
Expand All @@ -174,7 +167,6 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String path = null;
String pref = null;
Log.i("ORGSYNC/PREF", "got uri " + data.getData());
switch (requestCode) {
case SELECT_AGENDA_FILE:
getContext().getContentResolver().takePersistableUriPermission(
Expand Down
179 changes: 107 additions & 72 deletions app/src/main/java/com/larkery/simpleorgsync/cal/CalSyncAdapter.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public void addProperty(Property property) {
this.endOfLastProperty = Math.max(property.getEndPosition(), endOfLastProperty);
}

public String ensureID() {
private String ensureID() {
if (!hasProperty("ID")) {
setProperty("ID", UUID.randomUUID().toString());
}
Expand Down Expand Up @@ -248,4 +248,9 @@ public void addInheritedTags(Set<String> filetags) {
public Set<String> getTags() {
return tags;
}

public String syncID(boolean readOnly) {
if (readOnly) return checksum();
else return ensureID();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.larkery.simpleorgsync.con;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation;
Expand All @@ -23,23 +24,22 @@
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.provider.ContactsContract.CommonDataKinds.StructuredPostal;
import android.provider.ContactsContract.RawContacts;
import android.util.Log;

import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import com.larkery.simpleorgsync.lib.Application;
import com.larkery.simpleorgsync.lib.JSONPrefs;
import com.larkery.simpleorgsync.lib.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -48,7 +48,7 @@
import java.util.UUID;

public class ConSyncAdapter extends AbstractThreadedSyncAdapter {
static final String TAG = Application.TAG +"/CON";
static final String TAG = "ConSyncAdapter";
private Uri rawContactsURI;
private Uri dataURI;
private Account account;
Expand Down Expand Up @@ -89,9 +89,12 @@ private Uri syncAdapterURI(Uri contentUri, Account account) {

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
JSONPrefs prefs = JSONPrefs.fromContext(getContext(), account);

Log.i(TAG, "Prefs: " + prefs);
final String contactsFile = prefs.getString("contacts_file", null);
Log.i(TAG, "Sync Contacts File " + contactsFile);
if (contactsFile == null) return;

this.rawContactsURI = syncAdapterURI(RawContacts.CONTENT_URI, account);
this.dataURI = syncAdapterURI(ContactsContract.Data.CONTENT_URI, account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,9 @@
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
* Created by hinton on 10/04/20.
Expand Down
50 changes: 37 additions & 13 deletions app/src/main/java/com/larkery/simpleorgsync/lib/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,31 @@

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.util.Log;
import android.support.v4.app.ShareCompat;

import com.larkery.simpleorgsync.R;
import com.larkery.simpleorgsync.cal.CalSyncService;
import com.larkery.simpleorgsync.con.ConSyncService;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Map;

public class Application extends android.app.Application implements SharedPreferences.OnSharedPreferenceChangeListener {
public static final String TAG = "ORGSYNC";
private final static String TAG = "Application";
private AccountManager accountManager;
private Account account;
@Override
Expand All @@ -34,10 +39,13 @@ public void onCreate() {
preferences.registerOnSharedPreferenceChangeListener(this);

FileJobService.register(getApplicationContext());

transmitPreferences(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));
}

public Account getAccount() {
accountManager.addAccountExplicitly(account, null, null);

return account;
}

Expand All @@ -60,21 +68,24 @@ private void informSystemSyncable() {
}
}

public void requestSync() {
public void requestSync(final String cause) {
informSystemSyncable();

transmitPreferences(PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));

boolean contacts = isContactsSyncable();
boolean calendar = isCalendarSyncable();

if (contacts) {
Log.i(TAG, "Request contacts sync");
Log.i(TAG, "Request contacts sync due to " + cause);
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
ContentResolver.requestSync(getAccount(), ContactsContract.AUTHORITY, settingsBundle);
}

if (calendar) {
Log.i(TAG, "Request calendar sync");
Log.i(TAG, "Request calendar sync due to " + cause);
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
Expand Down Expand Up @@ -129,25 +140,38 @@ private boolean isCalendarSyncable() {
.getString("agenda_files", null) != null;
}

void transmitPreferences(SharedPreferences sharedPreferences) {
final JSONObject obj = new JSONObject();
for (final Map.Entry<String, ?> e : sharedPreferences.getAll().entrySet()) {
try {
obj.put(e.getKey(), (Object)e.getValue());
} catch (JSONException jsonException) {
jsonException.printStackTrace();
}
}
getApplicationContext().getSystemService(AccountManager.class)
.setUserData(getAccount(), "prefs", obj.toString());
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
final String cf = sharedPreferences.getString("contacts_file", null);
final String af = sharedPreferences.getString("agenda_files", null);
Log.i(TAG, "Pref change " + cf + " " + af);
transmitPreferences(sharedPreferences);
switch (s) {
case "contacts_file":
case "agenda_files":
case "date_type":
case "ignore_syncthing_conflicts":
{
requestSync();
case "read_only": {
requestSync("a preference was changed: " + s);
}
break;
break;
case "inotify":
FileJobService.register(getApplicationContext());
break;
case "sync_frequency":
{
case "sync_frequency": {
int syncFrequency = Integer.parseInt(sharedPreferences.getString("sync_frequency", "-1"));
if (af != null || cf != null) {
if (af != null) {
Expand Down Expand Up @@ -181,7 +205,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
}
}
}
break;
break;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
package com.larkery.simpleorgsync.lib;

import android.app.Notification;
import android.app.Service;
import android.app.job.JobInfo;
import android.app.job.JobParameters;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.provider.DocumentFile;
import android.util.Log;
import android.widget.Toast;

import com.larkery.simpleorgsync.R;
import com.larkery.simpleorgsync.cal.CalSyncAdapter;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

public class FileJobService extends JobService {
private static final String TAG = "ORGSYNC/FILEJOBSERVICE";
private static final String TAG = "FileJobService";

public FileJobService() {
}
Expand Down Expand Up @@ -67,6 +52,7 @@ public static void register(Context context) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
final String contactsFile = prefs.getString("contacts_file", null);
final String agendaFiles = prefs.getString("agenda_files", null);
final boolean ignoreConflict = prefs.getBoolean("ignore_syncthing_conflicts", true);

final Set<DocumentFile> toWatch = new HashSet<>();
if (contactsFile != null) {
Expand All @@ -76,15 +62,15 @@ public static void register(Context context) {
if (agendaFiles != null) {
CalSyncAdapter.collectOrgFiles(
CalSyncAdapter.docFile(context, agendaFiles),
toWatch);
toWatch,
ignoreConflict);
}

final ArrayList<String> paths = new ArrayList<>();
StringBuffer sb = new StringBuffer();

int i = 0;
for (DocumentFile f : toWatch) {
i++;
Log.i(TAG, "Watching " + f.getName());

b.addTriggerContentUri(
new JobInfo.TriggerContentUri(
f.getUri(),
Expand All @@ -93,7 +79,6 @@ public static void register(Context context) {
);
}

Log.i(TAG, "Watching " + i + " contenturi for " + paths.size() + " files...");
b.setTriggerContentUpdateDelay(100);
b.setTriggerContentMaxDelay(3000);
scheduler.schedule(b.build());
Expand All @@ -103,8 +88,9 @@ public static void register(Context context) {

@Override
public boolean onStartJob(JobParameters jobParameters) {
Log.i(TAG, "Agenda sync triggered!");
((Application)getApplication()).requestSync();
((Application)getApplication()).requestSync("a file was changed: " +
Arrays.toString(jobParameters.getTriggeredContentUris())
);
this.jobFinished(jobParameters, false);
register(getApplicationContext());
return false;
Expand Down
Loading

0 comments on commit 5556012

Please sign in to comment.