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;
import android.accounts.Account;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
@ -590,6 +588,14 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
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.
@ -657,12 +663,8 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
//Restore preferences
addPreferencesFromResource(R.xml.preferences);
this.ctx = getBaseContext();
if(prefs == null) {
prefs = PreferenceManager.getDefaultSharedPreferences(this.ctx);
editor = prefs.edit();
}
ctx = getBaseContext();
getPreferences(ctx);
//Initialize database
try {
@ -674,18 +676,6 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
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);
userPasswordPref = findPreference(USERPASSWORDPREF);
currentVersionPref = findPreference(CURRENTVERSIONPREF);
@ -865,27 +855,42 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
//Reset user password on userid change
setUserPassword("");
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)) {
try {
userPassword = Crypto.encryptPassword((String) newValue);
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) {
error(TAG, ex.getMessage(), ex, true);
}
} else if (SERVERPREF.equals(key)) {
server = (String) newValue;
server = (String) newValue;
//If preferences have changed, logout
logoutClean(key);
} else if(SYNCENABLEPREF.equals(key)) {
//boolean masterSyncEnabled = ContentResolver.getMasterSyncAutomatically();
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
/*if(syncEnabled && !masterSyncEnabled) {
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);
} else if(SYNCTIMEPREF.equals(key)) {
@ -896,10 +901,10 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
int prefSyncTimeIndex = prefSyncTimeValues.indexOf(syncTime);
String prefSyncTimeEntry = prefSyncTimeEntries.get(prefSyncTimeIndex);
SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, getApplicationContext());
SyncUtils.removePeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, ctx);
if (!syncTime.equals("0")) {
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.parseLong(syncTime), getApplicationContext());
if (!syncTime.equals("0") && syncEnabled) {
SyncUtils.addPeriodicSync(Constants.AUTHORITY, Bundle.EMPTY, Long.parseLong(syncTime), ctx);
}
if(lastSyncTime == 0) {
@ -929,18 +934,6 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang
notifLightsEnabled = (Boolean) newValue;
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
//((BaseAdapter)getPreferenceScreen().getRootAdapter()).notifyDataSetChanged();

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

View File

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

View File

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

View File

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

View File

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

View File

@ -316,6 +316,10 @@ public class NotificationsSyncAdapterService extends Service {
createRequest();
addParam("userID", prefs.getUserID());
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);
sendRequest(User.class, true);

View File

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

View File

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

View File

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

View File

@ -20,13 +20,13 @@ package es.ugr.swad.swadroid.sync;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import es.ugr.swad.swadroid.Constants;
/**
@ -34,8 +34,11 @@ import es.ugr.swad.swadroid.Constants;
*
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com>
*/
@SuppressLint("NewApi")
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) {
long pollFrequencyMsec = frequency * 60000;
@ -48,12 +51,19 @@ public class SyncUtils {
PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
manager.setInexactRepeating(type, triggerAtTime, pollFrequencyMsec, operation);
Log.i(TAG, "Added periodic alarm with pollFrequency=" + pollFrequencyMsec);
} else {
AccountManager am = AccountManager.get(context);
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) {
ContentResolver.setSyncAutomatically(a, Constants.AUTHORITY, true);
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);
PendingIntent operation = PeriodicSyncReceiver.createPendingIntent(context, authority, extras);
manager.cancel(operation);
Log.i(TAG, "Removed periodic alarm");
} else {
AccountManager am = AccountManager.get(context);
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) {
ContentResolver.setSyncAutomatically(a, Constants.AUTHORITY, false);
ContentResolver.removePeriodicSync(a, authority, extras);
Log.i(TAG, "Removed periodic synchronization for account " + a.toString());
}
}
}