Fixed bug on retrieving students list. Closes #72

This commit is contained in:
Juan Miguel Boyero Corral 2013-12-04 21:22:04 +01:00
parent fc63bd346c
commit 63c970289f
12 changed files with 118 additions and 99 deletions

View File

@ -19,9 +19,7 @@
package es.ugr.swad.swadroid; package es.ugr.swad.swadroid;
import android.accounts.Account;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
@ -591,6 +589,14 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
Log.i(TAG, "Database has been cleaned"); Log.i(TAG, "Database has been cleaned");
} }
private void logoutClean(String key) {
Constants.setLogged(false);
Log.i(TAG, "Forced logout due to " + key + " change in preferences");
cleanDatabase();
Constants.setPreferencesChanged();
}
/** /**
* Initializes preferences of activity. * Initializes preferences of activity.
* *
@ -657,12 +663,8 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
//Restore preferences //Restore preferences
addPreferencesFromResource(R.xml.preferences); addPreferencesFromResource(R.xml.preferences);
this.ctx = getBaseContext(); ctx = getBaseContext();
getPreferences(ctx);
if(prefs == null) {
prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
editor = prefs.edit();
}
//Initialize database //Initialize database
try { try {
@ -674,18 +676,6 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
error(TAG, ex.getMessage(), ex, true); error(TAG, ex.getMessage(), ex, true);
} }
userID = prefs.getString(USERIDPREF, "");
userPassword = prefs.getString(USERPASSWORDPREF, "");
server = prefs.getString(SERVERPREF, Constants.DEFAULT_SERVER);
lastVersion = prefs.getInt(LASTVERSIONPREF, 0);
lastCourseSelected = prefs.getInt(LASTCOURSESELECTEDPREF, 0);
syncEnabled = prefs.getBoolean(SYNCENABLEPREF, true);
lastSyncTime = prefs.getLong(LASTSYNCTIMEPREF, 0);
notifLimit = prefs.getInt(NOTIFLIMITPREF, 25);
notifSoundEnabled = prefs.getBoolean(NOTIFSOUNDENABLEPREF, true);
notifVibrateEnabled = prefs.getBoolean(NOTIFVIBRATEENABLEPREF, true);
notifLightsEnabled = prefs.getBoolean(NOTIFLIGHTSENABLEPREF, true);
userIDPref = findPreference(USERIDPREF); userIDPref = findPreference(USERIDPREF);
userPasswordPref = findPreference(USERPASSWORDPREF); userPasswordPref = findPreference(USERPASSWORDPREF);
currentVersionPref = findPreference(CURRENTVERSIONPREF); currentVersionPref = findPreference(CURRENTVERSIONPREF);
@ -866,26 +856,41 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
setUserPassword(""); setUserPassword("");
userPasswordPref.setSummary(""); userPasswordPref.setSummary("");
Log.i(TAG, "Resetted user password due to userid change"); Log.i(TAG, "Resetted user password due to userid change");
//If preferences have changed, logout
logoutClean(key);
} else if (USERPASSWORDPREF.equals(key)) { } else if (USERPASSWORDPREF.equals(key)) {
try { try {
userPassword = Crypto.encryptPassword((String) newValue); userPassword = Crypto.encryptPassword((String) newValue);
preference.setSummary(getStarsSequence(STARS_LENGTH)); preference.setSummary(getStarsSequence(STARS_LENGTH));
//If preferences have changed, logout
Constants.setLogged(false);
Log.i(TAG, "Forced logout due to " + key + " change in preferences");
} catch (NoSuchAlgorithmException ex) { } catch (NoSuchAlgorithmException ex) {
error(TAG, ex.getMessage(), ex, true); error(TAG, ex.getMessage(), ex, true);
} }
} else if (SERVERPREF.equals(key)) { } else if (SERVERPREF.equals(key)) {
server = (String) newValue; server = (String) newValue;
//If preferences have changed, logout
logoutClean(key);
} else if(SYNCENABLEPREF.equals(key)) { } else if(SYNCENABLEPREF.equals(key)) {
//boolean masterSyncEnabled = ContentResolver.getMasterSyncAutomatically(); //boolean masterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
syncEnabled = (Boolean) newValue; syncEnabled = (Boolean) newValue;
Account account = new Account(getString(R.string.app_name), Constants.ACCOUNT_TYPE); //Account account = new Account(getString(R.string.app_name), Constants.ACCOUNT_TYPE);
//Configure automatic synchronization //Configure automatic synchronization
/*if(syncEnabled && !masterSyncEnabled) { /*if(syncEnabled && !masterSyncEnabled) {
ContentResolver.setMasterSyncAutomatically(syncEnabled); ContentResolver.setMasterSyncAutomatically(syncEnabled);
}*/ }
ContentResolver.setSyncAutomatically(account, Constants.AUTHORITY, syncEnabled); ContentResolver.setSyncAutomatically(account, Constants.AUTHORITY, syncEnabled);*/
if(syncEnabled) {
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.valueOf(syncTime), ctx);
} else {
SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, ctx);
}
syncEnablePref.setChecked(syncEnabled); syncEnablePref.setChecked(syncEnabled);
} else if(SYNCTIMEPREF.equals(key)) { } else if(SYNCTIMEPREF.equals(key)) {
@ -896,10 +901,10 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
int prefSyncTimeIndex = prefSyncTimeValues.indexOf(syncTime); int prefSyncTimeIndex = prefSyncTimeValues.indexOf(syncTime);
String prefSyncTimeEntry = prefSyncTimeEntries.get(prefSyncTimeIndex); String prefSyncTimeEntry = prefSyncTimeEntries.get(prefSyncTimeIndex);
SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, getApplicationContext()); SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, ctx);
if (!syncTime.equals("0")) { if (!syncTime.equals("0") && syncEnabled) {
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.parseLong(syncTime), getApplicationContext()); SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.parseLong(syncTime), ctx);
} }
if(lastSyncTime == 0) { if(lastSyncTime == 0) {
@ -930,18 +935,6 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
notifLightsEnablePref.setChecked(notifLightsEnabled); notifLightsEnablePref.setChecked(notifLightsEnabled);
} }
//If preferences have changed, logout
if (USERIDPREF.equals(key) || SERVERPREF.equals(key)) {
Constants.setLogged(false);
Log.i(TAG, "Forced logout due to userid or server change in preferences");
cleanDatabase();
Constants.setPreferencesChanged();
} else if (USERPASSWORDPREF.equals(key)) {
Constants.setLogged(false);
Log.i(TAG, "Forced logout due to user password change in preferences");
}
//Refresh preferences screen //Refresh preferences screen
//((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged(); //((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();
//getListView().invalidate(); //getListView().invalidate();

View File

@ -66,7 +66,6 @@ import es.ugr.swad.swadroid.modules.rollcall.Rollcall;
import es.ugr.swad.swadroid.modules.tests.Tests; import es.ugr.swad.swadroid.modules.tests.Tests;
import es.ugr.swad.swadroid.ssl.SecureConnection; import es.ugr.swad.swadroid.ssl.SecureConnection;
import es.ugr.swad.swadroid.sync.AccountAuthenticator; import es.ugr.swad.swadroid.sync.AccountAuthenticator;
import es.ugr.swad.swadroid.sync.SyncUtils;
import es.ugr.swad.swadroid.utils.Utils; import es.ugr.swad.swadroid.utils.Utils;
/** /**
@ -286,10 +285,9 @@ public class SWADMain extends MenuExpandableListActivity {
showConfigurationDialog(); showConfigurationDialog();
//Configure automatic synchronization //Configure automatic synchronization
prefs.setSyncTime(String.valueOf(Constants.DEFAULT_SYNC_TIME));
activity = new Intent(getBaseContext(), AccountAuthenticator.class); activity = new Intent(getBaseContext(), AccountAuthenticator.class);
startActivity(activity); startActivity(activity);
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Constants.DEFAULT_SYNC_TIME, this);
prefs.setSyncTime(String.valueOf(Constants.DEFAULT_SYNC_TIME));
prefs.setLastVersion(currentVersion); prefs.setLastVersion(currentVersion);
firstRun = true; firstRun = true;
@ -316,12 +314,10 @@ public class SWADMain extends MenuExpandableListActivity {
} }
//Configure automatic synchronization //Configure automatic synchronization
if(Preferences.isSyncEnabled()) { /*if(Preferences.isSyncEnabled()) {
activity = new Intent(getBaseContext(), AccountAuthenticator.class);
startActivity(activity);
SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, this); SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, this);
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.valueOf(prefs.getSyncTime()), this); SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.valueOf(prefs.getSyncTime()), this);
} }*/
prefs.setLastVersion(currentVersion); prefs.setLastVersion(currentVersion);
} }

View File

@ -47,7 +47,7 @@ public class MenuActivity extends Activity {
/** /**
* Application preferences. * Application preferences.
*/ */
private static final Preferences prefs = new Preferences(); protected static final Preferences prefs = new Preferences();
/** /**
* Database Helper. * Database Helper.
*/ */

View File

@ -385,11 +385,17 @@ public class DataBaseHelper {
* @return A Model's subclass object * @return A Model's subclass object
* or null if the row does not exist in the specified table * or null if the row does not exist in the specified table
*/ */
public Model getRow(String table, String fieldName, String fieldValue) { public Model getRow(String table, String fieldName, Object fieldValue) {
List<Entity> rows = db.getEntityList(table, fieldName + " = '" + fieldValue + "'"); List<Entity> rows;
Entity ent; Entity ent;
Model row = null; Model row = null;
if(fieldValue instanceof String) {
rows = db.getEntityList(table, fieldName + " = '" + fieldValue + "'");
} else {
rows = db.getEntityList(table, fieldName + " = " + fieldValue + "");
}
if (rows.size() > 0) { if (rows.size() > 0) {
ent = rows.get(0); ent = rows.get(0);
row = createObjectByTable(table, ent); row = createObjectByTable(table, ent);
@ -406,11 +412,17 @@ public class DataBaseHelper {
* @return The user found * @return The user found
* or null if the user does not exist * or null if the user does not exist
*/ */
public User getUser(String fieldName, String fieldValue) { public User getUser(String fieldName, Object fieldValue) {
List<Entity> rows = db.getEntityList(Constants.DB_TABLE_USERS, fieldName + " = '" + fieldValue + "'"); List<Entity> rows;
Entity ent; Entity ent;
User user = null; User user = null;
if(fieldValue instanceof String) {
rows = db.getEntityList(Constants.DB_TABLE_USERS, fieldName + " = '" + fieldValue + "'");
} else {
rows = db.getEntityList(Constants.DB_TABLE_USERS, fieldName + " = " + fieldValue + "");
}
if (rows.size() > 0) { if (rows.size() > 0) {
ent = rows.get(0); ent = rows.get(0);

View File

@ -43,7 +43,6 @@ import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import es.ugr.swad.swadroid.Constants; import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.Preferences;
import es.ugr.swad.swadroid.R; import es.ugr.swad.swadroid.R;
import es.ugr.swad.swadroid.gui.MenuActivity; import es.ugr.swad.swadroid.gui.MenuActivity;
import es.ugr.swad.swadroid.utils.Utils; import es.ugr.swad.swadroid.utils.Utils;
@ -70,10 +69,6 @@ public abstract class Module extends MenuActivity {
* SERVER param for webservice request. * SERVER param for webservice request.
*/ */
private String SERVER; // = "swad.ugr.es"; private String SERVER; // = "swad.ugr.es";
/**
* Preferences of the activity.
*/
protected static Preferences prefs = new Preferences();
/** /**
* Async Task for background jobs * Async Task for background jobs
*/ */
@ -102,10 +97,6 @@ public abstract class Module extends MenuActivity {
* Connection available flag * Connection available flag
*/ */
protected static boolean isConnected; protected static boolean isConnected;
/**
* Application debuggable flag
*/
protected static boolean isDebuggable;
/** /**
* Class Module's tag name for Logcat * Class Module's tag name for Logcat
*/ */
@ -194,24 +185,6 @@ public abstract class Module extends MenuActivity {
this.SOAP_ACTION = SOAP_ACTION; this.SOAP_ACTION = SOAP_ACTION;
} }
/**
* Gets preferences of activity.
*
* @return Preferences of activity.
*/
public Preferences getPrefs() {
return prefs;
}
/**
* Sets preferences of activity.
*
* @param prefs Preferences of activity.
*/
public void setPrefs(Preferences prefs) {
Module.prefs = prefs;
}
/** /**
* Gets webservice request. * Gets webservice request.
* *

View File

@ -322,28 +322,30 @@ public class Notifications extends Module {
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see es.ugr.swad.swadroid.modules.Module#onResume() * @see es.ugr.swad.swadroid.modules.Module#onStart()
*/ */
@Override @Override
protected void onResume() { protected void onStart() {
super.onResume(); super.onStart();
IntentFilter intentFilter = new IntentFilter(); IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(NotificationsSyncAdapterService.START_SYNC); intentFilter.addAction(NotificationsSyncAdapterService.START_SYNC);
intentFilter.addAction(NotificationsSyncAdapterService.STOP_SYNC); intentFilter.addAction(NotificationsSyncAdapterService.STOP_SYNC);
intentFilter.addAction(Intent.CATEGORY_DEFAULT); intentFilter.addAction(Intent.CATEGORY_DEFAULT);
registerReceiver(receiver, intentFilter); registerReceiver(receiver, intentFilter);
Log.i(TAG, "Registered receiver for automatic synchronization");
refreshScreen(); refreshScreen();
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see es.ugr.swad.swadroid.modules.Module#onPause() * @see es.ugr.swad.swadroid.modules.Module#onStop()
*/ */
@Override @Override
protected void onPause() { protected void onStop() {
super.onPause(); super.onStop();
unregisterReceiver(receiver); unregisterReceiver(receiver);
Log.i(TAG, "Unregistered receiver for automatic synchronization");
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -356,11 +358,14 @@ public class Notifications extends Module {
//Download new notifications from the server //Download new notifications from the server
SIZE_LIMIT = prefs.getNotifLimit(); SIZE_LIMIT = prefs.getNotifLimit();
account = new Account(getString(R.string.app_name), accountType);
if (ContentResolver.getSyncAutomatically(account, authority)) { if (ContentResolver.getSyncAutomatically(account, authority)) {
Log.i(TAG, "Automatic synchronization is enabled. Requesting asynchronous sync operation");
//Call synchronization service //Call synchronization service
ContentResolver.requestSync(account, authority, new Bundle()); ContentResolver.requestSync(account, authority, new Bundle());
} else { } else {
Log.i(TAG, "Automatic synchronization is disabled. Requesting manual sync operation");
//Calculates next timestamp to be requested //Calculates next timestamp to be requested
Long timestamp = Long.valueOf(dbHelper.getFieldOfLastNotification("eventTime")); Long timestamp = Long.valueOf(dbHelper.getFieldOfLastNotification("eventTime"));
timestamp++; timestamp++;

View File

@ -316,6 +316,10 @@ public class NotificationsSyncAdapterService extends Service {
createRequest(); createRequest();
addParam("userID", prefs.getUserID()); addParam("userID", prefs.getUserID());
addParam("userPassword", prefs.getUserPassword()); addParam("userPassword", prefs.getUserPassword());
Log.d(TAG, "[logUser] SERVER="+SERVER);
Log.d(TAG, "[logUser] SIZE_LIMIT="+SIZE_LIMIT);
Log.d(TAG, "[logUser] userID="+prefs.getUserID());
Log.d(TAG, "[logUser] userPassword="+prefs.getUserPassword());
addParam("appKey", Constants.SWAD_APP_KEY); addParam("appKey", Constants.SWAD_APP_KEY);
sendRequest(User.class, true); sendRequest(User.class, true);

View File

@ -43,6 +43,7 @@ import java.util.List;
* Students history module. * Students history module.
* *
* @author Antonio Aguilera Malagon <aguilerin@gmail.com> * @author Antonio Aguilera Malagon <aguilerin@gmail.com>
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/ */
public class StudentsHistory extends Module { public class StudentsHistory extends Module {
private List<StudentItemModel> studentsList; private List<StudentItemModel> studentsList;
@ -84,7 +85,7 @@ public class StudentsHistory extends Module {
studentsList = new ArrayList<StudentItemModel>(); studentsList = new ArrayList<StudentItemModel>();
for (Long userCode : idList) { for (Long userCode : idList) {
User u = dbHelper.getUser("userCode", String.valueOf(userCode)); User u = dbHelper.getUser("userCode", userCode);
studentsList.add(new StudentItemModel(u)); studentsList.add(new StudentItemModel(u));
} }
// Arrange the list alphabetically // Arrange the list alphabetically

View File

@ -43,6 +43,7 @@ import java.util.List;
* Students list module. * Students list module.
* *
* @author Antonio Aguilera Malagon <aguilerin@gmail.com> * @author Antonio Aguilera Malagon <aguilerin@gmail.com>
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/ */
public class StudentsList extends Module { public class StudentsList extends Module {
private Dialog studentsDialog; private Dialog studentsDialog;
@ -88,7 +89,7 @@ public class StudentsList extends Module {
List<StudentItemModel> studentsList = new ArrayList<StudentItemModel>(); List<StudentItemModel> studentsList = new ArrayList<StudentItemModel>();
for (long userCode : userIds) { for (long userCode : userIds) {
User u = dbHelper.getUser("userCode", String.valueOf(userCode)); User u = dbHelper.getUser("userCode", userCode);
studentsList.add(new StudentItemModel(u)); studentsList.add(new StudentItemModel(u));
} }
// Arrange the list alphabetically // Arrange the list alphabetically

View File

@ -6,11 +6,17 @@ import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import es.ugr.swad.swadroid.Constants; import es.ugr.swad.swadroid.Constants;
import es.ugr.swad.swadroid.Preferences; import es.ugr.swad.swadroid.Preferences;
import es.ugr.swad.swadroid.R; import es.ugr.swad.swadroid.R;
public class AccountAuthenticator extends AccountAuthenticatorActivity { public class AccountAuthenticator extends AccountAuthenticatorActivity {
/**
* Login tag name for Logcat
*/
public static final String TAG = Constants.APP_TAG + " AccountAuthenticator";
@Override @Override
protected void onCreate(Bundle icicle) { protected void onCreate(Bundle icicle) {
super.onCreate(icicle); super.onCreate(icicle);
@ -21,20 +27,32 @@ public class AccountAuthenticator extends AccountAuthenticatorActivity {
boolean accountCreated = am.addAccountExplicitly(account, getString(R.string.app_name), null); boolean accountCreated = am.addAccountExplicitly(account, getString(R.string.app_name), null);
Bundle extras = getIntent().getExtras(); Bundle extras = getIntent().getExtras();
if (extras != null) {
Log.d(TAG, "accountCreated=" + accountCreated);
Log.d(TAG, "extras=" + extras);
if (accountCreated) { //Pass the new account back to the account manager if (accountCreated) { //Pass the new account back to the account manager
if (extras != null) {
AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE); AccountAuthenticatorResponse response = extras.getParcelable(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE);
Bundle result = new Bundle(); Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
response.onResult(result); response.onResult(result);
} }
Log.i(TAG, "Account for automatic synchronization created successfully");
} else {
Log.w(TAG, "Account for automatic synchronization was not created");
} }
//Configure automatic synchronization //Configure automatic synchronization
ContentResolver.setIsSyncable(account, Constants.AUTHORITY, 1); ContentResolver.setIsSyncable(account, Constants.AUTHORITY, 1);
Log.i(TAG, "Account setted as syncable");
ContentResolver.setMasterSyncAutomatically(true); ContentResolver.setMasterSyncAutomatically(true);
ContentResolver.setSyncAutomatically(account, Constants.AUTHORITY, true); Log.i(TAG, "Master auto-sync setting enabled");
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.valueOf(prefs.getSyncTime()), this);
finish(); finish();
} }

View File

@ -20,13 +20,13 @@ package es.ugr.swad.swadroid.sync;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.Log;
import es.ugr.swad.swadroid.Constants; import es.ugr.swad.swadroid.Constants;
/** /**
@ -34,8 +34,11 @@ import es.ugr.swad.swadroid.Constants;
* *
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com> * @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/ */
@SuppressLint("NewApi")
public class SyncUtils { public class SyncUtils {
/**
* Login tag name for Logcat
*/
public static final String TAG = Constants.APP_TAG + " SyncUtils";
public static void addPeriodicSync(String authority, Bundle extras, long frequency, Context context) { public static void addPeriodicSync(String authority, Bundle extras, long frequency, Context context) {
long pollFrequencyMsec = frequency * 60000; long pollFrequencyMsec = frequency * 60000;
@ -48,12 +51,19 @@ public class SyncUtils {
PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras); PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
manager.setInexactRepeating(type, triggerAtTime, pollFrequencyMsec, operation); manager.setInexactRepeating(type, triggerAtTime, pollFrequencyMsec, operation);
Log.i(TAG, "Added periodic alarm with pollFrequency=" + pollFrequencyMsec);
} else { } else {
AccountManager am = AccountManager.get(context); AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE); Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE);
Log.d(TAG, "[addPeriodicSync] Number of accounts with type " + Constants.ACCOUNT_TYPE + " = " + accounts.length);
for (Account a : accounts) { for (Account a : accounts) {
ContentResolver.setSyncAutomatically(a, Constants.AUTHORITY, true);
ContentResolver.addPeriodicSync(a, authority, extras, frequency * 60); ContentResolver.addPeriodicSync(a, authority, extras, frequency * 60);
Log.i(TAG, "Added periodic synchronization with pollFrequency=" + (frequency * 60)
+ " for account " + a.toString());
} }
} }
} }
@ -63,12 +73,18 @@ public class SyncUtils {
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras); PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
manager.cancel(operation); manager.cancel(operation);
Log.i(TAG, "Removed periodic alarm");
} else { } else {
AccountManager am = AccountManager.get(context); AccountManager am = AccountManager.get(context);
Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE); Account[] accounts = am.getAccountsByType(Constants.ACCOUNT_TYPE);
Log.d(TAG, "[removePeriodicSync] Number of accounts with type " + Constants.ACCOUNT_TYPE + " = " + accounts.length);
for (Account a : accounts) { for (Account a : accounts) {
ContentResolver.setSyncAutomatically(a, Constants.AUTHORITY, false);
ContentResolver.removePeriodicSync(a, authority, extras); ContentResolver.removePeriodicSync(a, authority, extras);
Log.i(TAG, "Removed periodic synchronization for account " + a.toString());
} }
} }
} }