package org.sana.android.app;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import org.sana.android.service.impl.ApplicationService;

import java.util.Calendar;

/**
 * Created by winkler.em@gmail.com, on 06/05/2017.
 */
public class Main extends Application {
    public static final String TAG = Main.class.getSimpleName();
    private static final int UPDATE_CHECK = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        onCreateUpdateAlarm(this);
    }

    private void onCreateUpdateAlarm(Context context){
        Intent check = new Intent(this, ApplicationService.class);
        check.setAction(ApplicationService.UPDATE_CHECK);
        startService(check);
        Calendar calendar = Calendar.getInstance();
        Intent intent = new Intent(this, UpdateReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(this, UPDATE_CHECK,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis() + 10000,
                AlarmManager.INTERVAL_HOUR*4, pi);
    }
}

@@ -186,16 +186,123 @@
 Intent;%0A
+import android.content.pm.PackageManager;%0Aimport android.net.Uri;%0A%0Aimport org.sana.android.content.Intents;
 %0Aimport 
@@ -350,16 +350,70 @@
 Service;
+%0Aimport org.sana.android.service.impl.DispatchService;
 %0A%0Aimport
@@ -531,16 +531,40 @@
 ication 
+implements ErrorHandler 
 %7B%0A    pu
@@ -735,24 +735,200 @@
 onCreate();%0A
+        String device = ConnectionManager.getDeviceName(this);%0A        Thread.currentThread().setUncaughtExceptionHandler(%0A                new ExceptionHandler(this, device));%0A
         onCr
@@ -1644,11 +1644,1691 @@
 );%0A    %7D
+%0A%0A    public void onHandleReport(Uri reportUri, String message) %7B%0A        try %7B%0A            Preferences.setLoggedIn(this, false);%0A            Intent dispatcher = DispatchService.shutdown(this);%0A            stopService(dispatcher);%0A            //TODO Add dirty start flag%0A            //SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);%0A            //SharedPreferences.Editor editor = sp.edit();%0A            PackageManager pm = getApplicationContext().getPackageManager();%0A            String packageName = getApplicationContext().getPackageName();%0A            Intent intent = pm.getLaunchIntentForPackage(packageName);%0A            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);%0A            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);%0A            intent.putExtra(Intents.EXTRA_REPORT, reportUri);%0A            intent.putExtra(Intents.EXTRA_MESSAGE, message);%0A            startActivity(intent);%0A            Intent error = ErrorManager.getReport(this, reportUri, message);%0A%0A            AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);%0A            PendingIntent next = PendingIntent.getService(getBaseContext(), 1, error, PendingIntent.FLAG_ONE_SHOT);%0A            PendingIntent crash = PendingIntent.getService(getBaseContext(), 2, error, PendingIntent.FLAG_ONE_SHOT);%0A            am.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, next);%0A            am.set(AlarmManager.RTC, System.currentTimeMillis() + 4000, crash);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A        %7D%0A        System.exit(0);%0A    %7D%0A%0A    public void onHandleReport(Uri reportUri) %7B%0A        onHandleReport(reportUri, %22null%22);%0A    %7D
 %0A%7D%0A

Integrate exception handling