Completed automatic synchronization of notifications

This commit is contained in:
Juan Miguel Boyero Corral 2012-04-18 21:41:11 +02:00
parent 361cd6cf8c
commit fbe3233588
9 changed files with 104 additions and 36 deletions

View File

@ -7,6 +7,9 @@
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
@ -126,6 +129,12 @@
android:configChanges="keyboard|keyboardHidden|orientation"
android:label="@string/noticesModuleLabel"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>
<activity
android:name=".sync.AccountAuthenticator"
android:configChanges="keyboard|keyboardHidden|orientation"
android:theme="@android:style/Theme.Translucent.NoTitleBar" >
</activity>
<activity
android:name=".modules.downloads.DirectoryTreeDownload"
@ -140,8 +149,8 @@
<provider
android:name=".sync.DummyProvider"
android:authorities="es.ugr.swad.content"
android:label="DummyProvider"
android:authorities="es.ugr.swad.swadroid.content"
android:label="SWADroid"
android:syncable="true" />
<service

View File

@ -162,5 +162,5 @@
<string name="scan_not_valid_code">Código QR no válido</string>
<string name="scan_data_not_found">No existen datos de este alumno</string>
<string name="scan_no_codes">No se ha detectado ningún código QR válido</string>
<string name="usersPresent">Estudiantes presentes</string>n
<string name="usersPresent">Estudiantes presentes</string>
</resources>

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="General Settings" />
<!-- <PreferenceCategory
android:title="@string/generalSettings" />
<PreferenceScreen
android:key="account_settings"
android:title="Account Settings"
android:summary="Sync frequency, notifications, etc.">
android:title="@string/accountSettings"
android:summary="@string/accountSettingsSummary">
<intent
android:action="es.ugr.swad.swadroid.Preferences.ACCOUNT_SETUP"
android:targetPackage="es.ugr.swad.swadroid"
android:targetClass="es.ugr.swad.swadroid.Preferences" />
</PreferenceScreen>
</PreferenceScreen> -->
</PreferenceScreen>

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="es.ugr.swad.swadroid"
android:icon="@drawable/ic_launcher_swadroid"
android:smallIcon="@drawable/ic_launcher_swadroid"
android:label="@string/app_name"

View File

@ -61,6 +61,7 @@ import es.ugr.swad.swadroid.modules.downloads.DirectoryTreeDownload
import es.ugr.swad.swadroid.modules.notifications.Notifications;
import es.ugr.swad.swadroid.modules.tests.Tests;
import es.ugr.swad.swadroid.ssl.SecureConnection;
import es.ugr.swad.swadroid.sync.AccountAuthenticator;
/**
* Main class of the application.
@ -288,7 +289,7 @@ public class SWADMain extends MenuExpandableListActivity {
//Initialize HTTPS connections
SecureConnection.initSecureConnection();
//Check if this is the first run after an install or upgrade
lastVersion = prefs.getLastVersion();
currentVersion = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
@ -298,6 +299,11 @@ public class SWADMain extends MenuExpandableListActivity {
showConfigurationDialog();
dbHelper.initializeDB();
//prefs.upgradeCredentials();
//Configure automatic synchronization
Intent activity = new Intent(getBaseContext(), AccountAuthenticator.class);
startActivity(activity);
prefs.setLastVersion(currentVersion);
firstRun = true;
Global.setSelectedCourseCode(-1);
@ -307,6 +313,11 @@ public class SWADMain extends MenuExpandableListActivity {
//showUpgradeDialog();
dbHelper.upgradeDB(this);
//prefs.upgradeCredentials();
//Configure automatic synchronization
Intent activity = new Intent(getBaseContext(), AccountAuthenticator.class);
startActivity(activity);
prefs.setLastVersion(currentVersion);
}
listCourses = dbHelper.getAllRows(Global.DB_TABLE_COURSES,"","name");

View File

@ -488,7 +488,8 @@ public abstract class Module extends MenuActivity {
boolean showDialog, isLoginModule;
/**
* Shows progress dialog and connects to SWAD in background
* Shows progress dialog and connects to SWAD in bac
Log.i(TAG, "performSync: " + account.toString());kground
* @param showDialog Flag for show a progress dialog or not
* @param progressDescription Description to be showed in dialog
* @param progressTitle Title to be showed in dialog

View File

@ -20,20 +20,15 @@ package es.ugr.swad.swadroid.modules.notifications;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Vector;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.accounts.Account;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
@ -48,7 +43,6 @@ import com.android.dataframework.DataFramework;
import es.ugr.swad.swadroid.Global;
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.modules.Module;
/**
@ -60,7 +54,7 @@ public class Notifications extends Module {
/**
* Max size to store notifications
*/
private static final int SIZE_LIMIT = 25;
//private static final int SIZE_LIMIT = 25;
/**
* Notifications adapter for showing the data
*/
@ -80,15 +74,23 @@ public class Notifications extends Module {
/**
* Notifications counter
*/
private int notifCount;
//private int notifCount;
/**
* Unique identifier for notification alerts
*/
private int NOTIF_ALERT_ID = 1982;
//private int NOTIF_ALERT_ID = 1982;
/**
* Notifications tag name for Logcat
*/
public static final String TAG = Global.APP_TAG + " Notifications";
/**
* Account type
*/
private static String accountType = "es.ugr.swad.swadroid";
/**
* Synchronization authority
*/
private static String authority = "es.ugr.swad.swadroid.content";
/**
* Refreshes data on screen
@ -125,14 +127,13 @@ public class Notifications extends Module {
TextView sender = (TextView) v.findViewById(R.id.eventSender);
TextView course = (TextView) v.findViewById(R.id.eventLocation);
TextView summary = (TextView) v.findViewById(R.id.eventSummary);
TextView content = (TextView) v.findViewById(R.id.eventText);
TextView content = (TextView) v.findViewById(R.id.eventText);
Intent activity = new Intent(getApplicationContext(), NotificationItem.class);
activity.putExtra("sender", sender.getText().toString());
activity.putExtra("course", course.getText().toString());
activity.putExtra("summary", summary.getText().toString());
activity.putExtra("content", content.getText().toString());
startActivity(activity);
}
};
@ -198,6 +199,8 @@ public class Notifications extends Module {
@Override
protected void onResume() {
super.onResume();
refreshScreen();
}
@ -208,9 +211,13 @@ public class Notifications extends Module {
protected void requestService() throws NoSuchAlgorithmException,
IOException, XmlPullParserException, SoapFault,
IllegalAccessException, InstantiationException {
//Call synchronization service
Account account = new Account(getString(R.string.app_name), accountType);
ContentResolver.requestSync(account, authority, null);
//Calculates next timestamp to be requested
Long timestamp = new Long(dbHelper.getFieldOfLastNotification("eventTime"));
/*Long timestamp = new Long(dbHelper.getFieldOfLastNotification("eventTime"));
timestamp++;
//Creates webservice request, adds required params and sends request to webservice
@ -243,7 +250,7 @@ public class Notifications extends Module {
/*if(isDebuggable)
Log.d(TAG, n.toString());*/
}
/*}
//Request finalized without errors
Log.i(TAG, "Retrieved " + notifCount + " notifications");
@ -252,10 +259,10 @@ public class Notifications extends Module {
dbHelper.clearOldNotifications(SIZE_LIMIT);
dbHelper.endTransaction();
}
}*/
}
protected void alertNotif() {
/*protected void alertNotif() {
if(notifCount > 0) {
//Obtain a reference to the notification service
String ns = Context.NOTIFICATION_SERVICE;
@ -293,7 +300,7 @@ public class Notifications extends Module {
//Send alert
notManager.notify(NOTIF_ALERT_ID, notif);
}
}
}*/
/* (non-Javadoc)
* @see es.ugr.swad.swadroid.modules.Module#connect()
@ -314,7 +321,7 @@ public class Notifications extends Module {
refreshScreen();
//Toast.makeText(this, R.string.notificationsDownloadedMsg, Toast.LENGTH_SHORT).show();
alertNotif();
//alertNotif();
ProgressBar pb = (ProgressBar)this.findViewById(R.id.progress_refresh);
ImageButton updateButton = (ImageButton)this.findViewById(R.id.refresh);

View File

@ -19,12 +19,35 @@
package es.ugr.swad.swadroid.modules.notifications;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Vector;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.KeepAliveHttpsTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import com.android.dataframework.DataFramework;
import es.ugr.swad.swadroid.Base64;
import es.ugr.swad.swadroid.Global;
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 android.accounts.Account;
import android.accounts.OperationCanceledException;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncResult;

View File

@ -1,30 +1,46 @@
package es.ugr.swad.swadroid.sync;
import es.ugr.swad.swadroid.Preferences;
import es.ugr.swad.swadroid.R;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorActivity;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.os.Bundle;
public class AccountAuthenticator extends AccountAuthenticatorActivity {
/**
* Account type
*/
private static String accountType = "es.ugr.swad.swadroid";
/**
* Synchronization authority
*/
private static String authority = "es.ugr.swad.swadroid.content";
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
Preferences prefs = new Preferences();
prefs.getPreferences(getBaseContext());
Account account = new Account(prefs.getUserID(), "es.ugr.swad.swadroid");
Account account = new Account(getString(R.string.app_name), accountType);
AccountManager am = AccountManager.get(this);
boolean accountCreated = am.addAccountExplicitly(account, prefs.getUserPassword(), null);
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, prefs.getUserID());
result.putString(AccountManager.KEY_ACCOUNT_TYPE, "es.ugr.swad.swadroid");
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
response.onResult(result);
//Configure automatic synchronization
ContentResolver.setIsSyncable(account, authority, 1);
ContentResolver.setMasterSyncAutomatically(true);
ContentResolver.setSyncAutomatically(account, authority, true);
}
finish();
}