Skip to content

Commit

Permalink
when sync fails, app doesn't crash, it informs the user with notifica…
Browse files Browse the repository at this point in the history
…tions
  • Loading branch information
konradrenner committed Jul 1, 2015
1 parent 8ca21e2 commit ada14ec
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "org.kore.kolabnotes.android"
minSdkVersion 19
targetSdkVersion 22
versionCode 6
versionName "0.1.0-alpha6"
versionCode 7
versionName "0.1.0-beta1"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.AbstractThreadedSyncAdapter;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SyncResult;
import android.os.Bundle;
import android.support.v7.app.NotificationCompat;
import android.util.Log;

import org.kore.kolab.notes.AccountInformation;
import org.kore.kolab.notes.RemoteNotesRepository;
import org.kore.kolab.notes.imap.ImapNotesRepository;
import org.kore.kolab.notes.v3.KolabConfigurationParserV3;
import org.kore.kolab.notes.v3.KolabNotesParserV3;
import org.kore.kolabnotes.android.R;
import org.kore.kolabnotes.android.Utils;
import org.kore.kolabnotes.android.content.RepositoryManager;
import org.kore.kolabnotes.android.security.AuthenticatorActivity;
Expand Down Expand Up @@ -64,8 +68,9 @@ public void syncNow(Account account, Bundle extras, SyncResult syncResult){
}

public void syncNow(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult){
Context context = getContext();

AccountManager accountManager = AccountManager.get(getContext());
AccountManager accountManager = AccountManager.get(context);

String email = accountManager.getUserData(account, AuthenticatorActivity.KEY_EMAIL);
String rootFolder = accountManager.getUserData(account,AuthenticatorActivity.KEY_ROOT_FOLDER);
Expand All @@ -90,14 +95,35 @@ public void syncNow(Account account, Bundle extras, String authority, ContentPro

AccountInformation info = builder.build();
ImapNotesRepository imapRepository = new ImapNotesRepository(new KolabNotesParserV3(), info, rootFolder, new KolabConfigurationParserV3());
imapRepository.refresh(new RefreshListener());
try {
imapRepository.refresh(new RefreshListener());
}catch(Exception e){
final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_kjots).setContentTitle(context.getResources().getString(R.string.sync_failed)).setContentText("Refresh failed:" + e).build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,notification);
}

RepositoryManager manager = new RepositoryManager(getContext(),imapRepository);
manager.sync(email, rootFolder);
try{
manager.sync(email, rootFolder);
}catch(Exception e){
final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_kjots).setContentTitle(context.getResources().getString(R.string.sync_failed)).setContentText("Sync failed:"+e).build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(2,notification);
}

Utils.updateWidgetsForChange(getContext());

imapRepository.merge();
try{
imapRepository.merge();
}catch(Exception e){
final Notification notification = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_kjots).setContentTitle(context.getResources().getString(R.string.sync_failed)).setContentText("Merge failed:"+e).build();

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(3,notification);
}
}

static class RefreshListener implements RemoteNotesRepository.Listener{
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,5 @@
<string name="select_or_create_hint">select or create note icon</string>
<string name="note_deleted">Notiz gelöscht</string>
<string name="note_saved">Notiz gespeichert</string>
<string name="sync_failed">Synchronisation fehlgeschlagen</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@
<string name="select_or_create_hint">select or create note icon</string>
<string name="note_deleted">Note deleted</string>
<string name="note_saved">Note saved</string>
<string name="sync_failed">Synchronisation failed</string>
</resources>

0 comments on commit ada14ec

Please sign in to comment.