Commit 712467d9 authored by rhi's avatar rhi 🐑
Browse files

AccountSettingsActivity: use loader

* use Loader for AccountSettingsActivity sync intervals (fixes Android 7 display "issues")
* SyncManager: allow prepare() to skip synchronization
parent cb0041bf
......@@ -80,9 +80,10 @@ public class CalendarSyncManager extends SyncManager {
@Override
protected void prepare() {
protected boolean prepare() {
collectionURL = HttpUrl.parse(localCalendar().getName());
davCollection = new DavCalendar(httpClient, collectionURL);
return true;
}
@Override
......
......@@ -11,6 +11,7 @@ package at.bitfire.davdroid.syncadapter;
import android.accounts.Account;
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
......@@ -141,7 +142,7 @@ public class ContactsSyncManager extends SyncManager {
@Override
protected void prepare() throws ContactsStorageException {
protected boolean prepare() throws ContactsStorageException {
// prepare local address book
localCollection = new LocalAddressBook(account, provider);
LocalAddressBook localAddressBook = localAddressBook();
......@@ -162,6 +163,8 @@ public class ContactsSyncManager extends SyncManager {
collectionURL = HttpUrl.parse(url);
davCollection = new DavAddressBook(httpClient, collectionURL);
return true;
}
@Override
......
......@@ -131,7 +131,10 @@ abstract public class SyncManager {
int syncPhase = SYNC_PHASE_PREPARE;
try {
App.log.info("Preparing synchronization");
prepare();
if (!prepare()) {
App.log.info("No reason to synchronize, aborting");
return;
}
if (Thread.interrupted())
return;
......@@ -258,7 +261,10 @@ abstract public class SyncManager {
}
abstract protected void prepare() throws ContactsStorageException;
/** Prepares synchronization (for instance, allocates necessary resources).
* @return whether actual synchronization is required / can be made. true = synchronization
* shall be continued, false = synchronization can be skipped */
abstract protected boolean prepare() throws ContactsStorageException;
abstract protected void queryCapabilities() throws IOException, HttpException, DavException, CalendarStorageException, ContactsStorageException;
......
......@@ -78,9 +78,10 @@ public class TasksSyncManager extends SyncManager {
@Override
protected void prepare() {
protected boolean prepare() {
collectionURL = HttpUrl.parse(localTaskList().getSyncId());
davCollection = new DavCalendar(httpClient, collectionURL);
return true;
}
@Override
......
......@@ -9,11 +9,17 @@
package at.bitfire.davdroid.ui;
import android.accounts.Account;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.ContactsContract;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.NavUtils;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
......@@ -23,10 +29,7 @@ import android.support.v7.preference.SwitchPreferenceCompat;
import android.text.TextUtils;
import android.view.MenuItem;
import java.util.logging.Level;
import at.bitfire.davdroid.AccountSettings;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.InvalidAccountException;
import at.bitfire.davdroid.R;
import at.bitfire.ical4android.TaskProvider;
......@@ -64,7 +67,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
}
public static class AccountSettingsFragment extends PreferenceFragmentCompat {
public static class AccountSettingsFragment extends PreferenceFragmentCompat implements LoaderManager.LoaderCallbacks<AccountSettings> {
Account account;
@Override
......@@ -72,7 +75,8 @@ public class AccountSettingsActivity extends AppCompatActivity {
super.onCreate(savedInstanceState);
account = getArguments().getParcelable(EXTRA_ACCOUNT);
refresh();
getLoaderManager().initLoader(0, getArguments(), this);
}
@Override
......@@ -80,13 +84,14 @@ public class AccountSettingsActivity extends AppCompatActivity {
addPreferencesFromResource(R.xml.settings_account);
}
public void refresh() {
final AccountSettings settings;
@Override
public Loader<AccountSettings> onCreateLoader(int id, Bundle args) {
return new AccountSettingsLoader(getContext(), (Account)args.getParcelable(EXTRA_ACCOUNT));
}
try {
settings = new AccountSettings(getActivity(), account);
} catch(InvalidAccountException e) {
App.log.log(Level.INFO, "Account is invalid or doesn't exist (anymore)", e);
@Override
public void onLoadFinished(Loader<AccountSettings> loader, final AccountSettings settings) {
if (settings == null) {
getActivity().finish();
return;
}
......@@ -99,7 +104,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.username((String)newValue);
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -109,7 +114,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.password((String)newValue);
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -127,7 +132,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.setSyncInterval(ContactsContract.AUTHORITY, Long.parseLong((String)newValue));
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -148,7 +153,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.setSyncInterval(CalendarContract.AUTHORITY, Long.parseLong((String)newValue));
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -169,7 +174,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority, Long.parseLong((String)newValue));
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -184,7 +189,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object wifiOnly) {
settings.setSyncWiFiOnly((Boolean)wifiOnly);
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -201,7 +206,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
public boolean onPreferenceChange(Preference preference, Object newValue) {
String ssid = (String)newValue;
settings.setSyncWifiOnlySSID(!TextUtils.isEmpty(ssid) ? ssid : null);
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -216,7 +221,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
public boolean onPreferenceChange(Preference preference, Object o) {
String name = (String)o;
settings.setGroupMethod(GroupMethod.valueOf(name));
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -244,7 +249,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
days = -1;
}
settings.setTimeRangePastDays(days < 0 ? null : days);
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -258,7 +263,7 @@ public class AccountSettingsActivity extends AppCompatActivity {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.setManageCalendarColors((Boolean)newValue);
refresh();
getLoaderManager().restartLoader(0, getArguments(), AccountSettingsFragment.this);
return false;
}
});
......@@ -266,6 +271,52 @@ public class AccountSettingsActivity extends AppCompatActivity {
prefManageColors.setEnabled(false);
}
@Override
public void onLoaderReset(Loader<AccountSettings> loader) {
}
}
private static class AccountSettingsLoader extends AsyncTaskLoader<AccountSettings> implements SyncStatusObserver {
final Account account;
Object listenerHandle;
public AccountSettingsLoader(Context context, Account account) {
super(context);
this.account = account;
}
@Override
protected void onStartLoading() {
forceLoad();
listenerHandle = ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, this);
}
@Override
protected void onStopLoading() {
ContentResolver.removeStatusChangeListener(listenerHandle);
}
@Override
public AccountSettings loadInBackground() {
AccountSettings settings;
try {
settings = new AccountSettings(getContext(), account);
} catch(InvalidAccountException e) {
return null;
}
return settings;
}
@Override
public void onStatusChanged(int which) {
forceLoad();
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment