From ef5f67ffc167796bd8b366bfd1efa2924047dced Mon Sep 17 00:00:00 2001 From: Juan Miguel Boyero Corral Date: Thu, 26 Apr 2012 21:18:24 +0200 Subject: [PATCH] Fixed initialization of server URL --- .../src/es/ugr/swad/swadroid/Preferences.java | 11 +- .../es/ugr/swad/swadroid/modules/Module.java | 2 +- .../modules/notifications/Notifications.java | 2 +- .../NotificationsSyncAdapterService.java | 169 +++++++++++++++++- .../swadroid/sync/AccountAuthenticator.java | 2 +- 5 files changed, 179 insertions(+), 7 deletions(-) diff --git a/SWADroid/src/es/ugr/swad/swadroid/Preferences.java b/SWADroid/src/es/ugr/swad/swadroid/Preferences.java index afebf0d6..f1d77d8a 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/Preferences.java +++ b/SWADroid/src/es/ugr/swad/swadroid/Preferences.java @@ -192,7 +192,12 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang * Gets server URL. * @return Server URL. */ - public String getServer() { + public String getServer() { + if(server.isEmpty()) { + server = Global.getDefaultServer(); + serverPref.setSummary(server); + } + return server; } @@ -459,7 +464,7 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang return true; } }); - serverPref.setSummary(prefs.getString(SERVERPREF, Global.getDefaultServer())); + serverPref.setSummary(server); try { currentVersionPref.setSummary(getPackageManager().getPackageInfo(getPackageName(), 0).versionName); @@ -516,7 +521,7 @@ public class Preferences extends PreferenceActivity implements OnPreferenceChang if(!prefs.getString(USERPASSWORDPREF, "").equals("")) userPasswordPref.setSummary(stars); - serverPref.setSummary(prefs.getString(SERVERPREF, Global.getDefaultServer())); + serverPref.setSummary(server); } /* (non-Javadoc) diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/Module.java b/SWADroid/src/es/ugr/swad/swadroid/modules/Module.java index 6d36ebc1..1adf782c 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/modules/Module.java +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/Module.java @@ -582,7 +582,7 @@ public abstract class Module extends MenuActivity { //Request finalized with errors onError(); - error(errorMsg); + error(errorMsg); /*if(isDebuggable) { e.printStackTrace(); connection.debug = true; diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java b/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java index fea70b12..19632835 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java @@ -214,7 +214,7 @@ public class Notifications extends Module { //Call synchronization service Account account = new Account(getString(R.string.app_name), accountType); - ContentResolver.requestSync(account, authority, null); + ContentResolver.requestSync(account, authority, new Bundle()); //Calculates next timestamp to be requested /*Long timestamp = new Long(dbHelper.getFieldOfLastNotification("eventTime")); diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/NotificationsSyncAdapterService.java b/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/NotificationsSyncAdapterService.java index 5f35ff00..f4c5ccb1 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/NotificationsSyncAdapterService.java +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/NotificationsSyncAdapterService.java @@ -20,12 +20,14 @@ package es.ugr.swad.swadroid.modules.notifications; import java.io.IOException; +import java.security.KeyManagementException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Vector; import org.ksoap2.SoapEnvelope; import org.ksoap2.SoapFault; +import org.ksoap2.serialization.KvmSerializable; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.KeepAliveHttpsTransportSE; @@ -39,6 +41,8 @@ import es.ugr.swad.swadroid.Preferences; import es.ugr.swad.swadroid.R; import es.ugr.swad.swadroid.model.DataBaseHelper; import es.ugr.swad.swadroid.model.SWADNotification; +import es.ugr.swad.swadroid.model.User; +import es.ugr.swad.swadroid.ssl.SecureConnection; import android.accounts.Account; import android.accounts.OperationCanceledException; @@ -62,7 +66,6 @@ import android.util.Log; public class NotificationsSyncAdapterService extends Service { private static final String TAG = "NotificationsSyncAdapterService"; private static SyncAdapterImpl sSyncAdapter = null; - private static ContentResolver mContentResolver = null; public NotificationsSyncAdapterService() { super(); @@ -103,5 +106,169 @@ public class NotificationsSyncAdapterService extends Service { mContentResolver = context.getContentResolver(); Log.i(TAG, "performSync: " + account.toString()); //This is where the magic will happen! + /** + * Use of KeepAliveHttpsTransport deals with the problems with the Android ssl libraries having trouble + * with certificates and certificate authorities somehow messing up connecting/needing reconnects. + */ + URL = prefs.getServer(); + connection = new KeepAliveHttpsTransportSE(URL, 443, "", TIMEOUT); + SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); + System.setProperty("http.keepAlive", "false"); + envelope.setOutputSoapObject(request); + //connection.debug = true; + connection.call(SOAP_ACTION, envelope); + //Log.d(TAG, connection.requestDump.toString()); + //Log.d(TAG, connection.responseDump.toString()); + + if(simple && !(envelope.getResponse() instanceof SoapFault)) { + result = envelope.bodyIn; + } else { + result = envelope.getResponse(); + } + } + + private static class SyncAdapterImpl extends AbstractThreadedSyncAdapter { + private Context mContext; + + public SyncAdapterImpl(Context context) { + super(context, true); + mContext = context; + prefs = new Preferences(); + try { + db = DataFramework.getInstance(); + db.open(mContext, mContext.getPackageName()); + dbHelper = new DataBaseHelper(db); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { + try { + prefs.getPreferences(mContext); + NotificationsSyncAdapterService.performSync(mContext, account, extras, authority, provider, syncResult); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + @Override + public IBinder onBind(Intent intent) { + IBinder ret = null; + ret = getSyncAdapter().getSyncAdapterBinder(); + return ret; + } + + private SyncAdapterImpl getSyncAdapter() { + if (sSyncAdapter == null) + sSyncAdapter = new SyncAdapterImpl(this); + return sSyncAdapter; + } + + private static void performSync(Context context, Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) + throws OperationCanceledException, SoapFault, IOException, IllegalAccessException, InstantiationException, XmlPullParserException, NoSuchAlgorithmException, KeyManagementException { + + //Initialize HTTPS connections + SecureConnection.initSecureConnection(); + + //If last login time > Global.RELOGIN_TIME, force login + if(System.currentTimeMillis()-Global.getLastLoginTime() > Global.RELOGIN_TIME) { + Global.setLogged(false); + } + + if(!Global.isLogged()) { + Log.d(TAG, "Not logged"); + + METHOD_NAME = "loginByUserPasswordKey"; + MessageDigest md = MessageDigest.getInstance("SHA-512"); + md.update(prefs.getUserPassword().getBytes()); + String userPassword = new String(Base64.encodeBytes(md.digest())); + userPassword = userPassword.replace('+','-').replace('/','_').replace('=', ' ').replaceAll("\\s+", "").trim(); + + createRequest(); + addParam("userID", prefs.getUserID()); + addParam("userPassword", userPassword); + addParam("appKey", Global.getAppKey()); + sendRequest(true); + + if (result != null) { + KvmSerializable ks = (KvmSerializable) result; + + //Stores user data returned by webservice response + User loggedUser = new User( + Long.parseLong(ks.getProperty(0).toString()), // id + Integer.parseInt(ks.getProperty(1).toString()), // userTypeCode + ks.getProperty(2).toString(), // wsKey + ks.getProperty(3).toString(), // userID + null, // userNickname + ks.getProperty(4).toString(), // userSurname1 + ks.getProperty(5).toString(), // userSurname2 + ks.getProperty(6).toString(), // userFirstName + ks.getProperty(7).toString(), // userTypeName + null, // photoPath + Integer.parseInt(ks.getProperty(8).toString()) // userRole + ); + + Global.setLoggedUser(loggedUser); + + //Update application last login time + Global.setLastLoginTime(System.currentTimeMillis()); + } + } + + if(Global.isLogged()) { + Log.d(TAG, "Logged"); + + //Calculates next timestamp to be requested + Long timestamp = new Long(dbHelper.getFieldOfLastNotification("eventTime")); + timestamp++; + + //Creates webservice request, adds required params and sends request to webservice + METHOD_NAME = "getNotifications"; + createRequest(); + addParam("wsKey", Global.getLoggedUser().getWsKey()); + addParam("beginTime", timestamp); + sendRequest(false); + + if (result != null) { + dbHelper.beginTransaction(); + + //Stores notifications data returned by webservice response + Vector res = (Vector) result; + SoapObject soap = (SoapObject) res.get(1); + notifCount = soap.getPropertyCount(); + for (int i = 0; i < notifCount; i++) { + SoapObject pii = (SoapObject)soap.getProperty(i); + Long notificationCode = new Long(pii.getProperty("notificationCode").toString()); + String eventType = pii.getProperty("eventType").toString(); + Long eventTime = new Long(pii.getProperty("eventTime").toString()); + String userSurname1 = pii.getProperty("userSurname1").toString(); + String userSurname2 = pii.getProperty("userSurname2").toString(); + String userFirstName = pii.getProperty("userFirstname").toString(); + String location = pii.getProperty("location").toString(); + String summary = pii.getProperty("summary").toString(); + Integer status = new Integer(pii.getProperty("status").toString()); + String content = pii.getProperty("content").toString(); + SWADNotification n = new SWADNotification(notificationCode, eventType, eventTime, userSurname1, userSurname2, userFirstName, location, summary, status, content); + dbHelper.insertNotification(n); + + /*if(isDebuggable) + Log.d(TAG, n.toString());*/ + } + + //Request finalized without errors + Log.i(TAG, "Retrieved " + notifCount + " notifications"); + + //Clear old notifications to control database size + dbHelper.clearOldNotifications(SIZE_LIMIT); + + dbHelper.endTransaction(); + } + + alertNotif(context); + } + } } } diff --git a/SWADroid/src/es/ugr/swad/swadroid/sync/AccountAuthenticator.java b/SWADroid/src/es/ugr/swad/swadroid/sync/AccountAuthenticator.java index 0050fde8..ef7766a6 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/sync/AccountAuthenticator.java +++ b/SWADroid/src/es/ugr/swad/swadroid/sync/AccountAuthenticator.java @@ -27,7 +27,7 @@ public class AccountAuthenticator extends AccountAuthenticatorActivity { Account account = new Account(getString(R.string.app_name), accountType); AccountManager am = AccountManager.get(this); 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