<commit_before>package ua.p2psafety;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

import ua.p2psafety.Network.NetworkManager;
import ua.p2psafety.data.Prefs;
import ua.p2psafety.sms.MessageResolver;
import ua.p2psafety.sms.MyLocation;
import ua.p2psafety.util.Logs;
import ua.p2psafety.util.Utils;

public class SosManager {
    private static SosManager mInstance;
    private Context mContext;
    private Logs logs;

    private Event mEvent;
    private boolean mSosStarted = false;

    private SosManager(Context context) {
        mContext = context;
        mSosStarted = Prefs.getSosStarted(mContext);
        mEvent = Prefs.getEvent(mContext);

        logs = new Logs(context);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();

        if (logs != null)
            logs.close();
    }

    public static SosManager getInstance(Context context) {
        if (mInstance == null)
            mInstance = new SosManager(context);
        mInstance.mContext = context;
        return mInstance;
    }

    public void startSos() {
        Utils.startVibration(mContext);

        // send SMS and email messages
        MessageResolver resolver = new MessageResolver(mContext);
        resolver.sendMessages();

        // start media recording
        switch (Prefs.getMediaRecordType(mContext)) {
            case 1:
                // record audio
                mContext.startService(new Intent(mContext, AudioRecordService.class));
                break;
            case 2:
                // record video
                mContext.startService(new Intent(mContext, VideoRecordService.class));
                break;
        }

        // show hint in notifications panel
        Notifications.notifSosStarted(mContext);

        // report event to the server
        if (Utils.isServerAuthenticated(mContext))
            serverStartSos();
        else
            Utils.setLoading(mContext, false);

        // make phone call
        mContext.startService(new Intent(mContext, PhoneCallService.class));

        setSosStarted(true);
    }

    public void stopSos() {
        // stop media recording
        mContext.stopService(new Intent(mContext, AudioRecordService.class));
        mContext.stopService(new Intent(mContext, VideoRecordService.class));

        Notifications.removeNotification(mContext, Notifications.NOTIF_SOS_STARTED_CODE);
        Notifications.notifSosCanceled(mContext);

        // TODO: send "i'm safe now" SMS and email messages (ask if needed)

        // report event to the server
        if (Utils.isFbAuthenticated(mContext))
            serverStopSos();
        else
            Utils.setLoading(mContext, false);

        mContext.stopService(new Intent(mContext, PhoneCallService.class));
        mContext.stopService(new Intent(mContext, LocationService.class));

        setSosStarted(false);
    }

    public boolean isSosStarted() {
        return mSosStarted;
    }

    public void setEvent(Event event) {
        mEvent = event;
        Prefs.putEvent(mContext, mEvent);
        if (mEvent == null)
            return;

        if (mEvent.getStatus() == Event.STATUS_ACTIVE)
            mSosStarted = true;
        else
            mSosStarted = false;
    }

    public Event getEvent() {
        return mEvent;
    }

    private void serverStartSos() {
        // if you have no event try to create one on server
        // (you must have an event at this point though)
        if (mEvent == null) {
            NetworkManager.createEvent(mContext,
                    new NetworkManager.DeliverResultRunnable<Event>() {
                        @Override
                        public void deliver(Event event) {
                            if (event != null) {
                                setEvent(event);
                                serverActivateSos(); // make this event active
                            } else {
                                Utils.setLoading(mContext, false);
                            }
                        }
                    });
        } else {
            serverActivateSos();
        }
    }

    private void serverActivateSos() {
        mEvent.setStatus(Event.STATUS_ACTIVE);
        MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
            @Override
            public void gotLocation(Location location) {
                Map data = new HashMap();
                if (location != null)
                    data.put("loc", location);
                data.put("text", Prefs.getMessage(mContext));

                NetworkManager.updateEvent(mContext, data, new NetworkManager.DeliverResultRunnable<Boolean>() {
                    @Override
                    public void deliver(Boolean aBoolean) {
                        // start sending location updates
                        mContext.startService(new Intent(mContext, LocationService.class));
                        Utils.setLoading(mContext, false);
                    }
                });
            }
        };
        MyLocation myLocation = new MyLocation(logs);
        myLocation.getLocation(mContext, locationResult);
    }

    private void serverStopSos() {
        if (mEvent != null)
            mEvent.setStatus(Event.STATUS_FINISHED);

        NetworkManager.createEvent(mContext,
                new NetworkManager.DeliverResultRunnable<Event>() {
                    @Override
                    public void deliver(Event event) {
                        setEvent(event);
                        Utils.setLoading((SosActivity) mContext, false);
                    }
                });
    }

    private void setSosStarted(boolean started) {<commit_msg>ticket:67 small fix<commit_after>package ua.p2psafety;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;

import ua.p2psafety.Network.NetworkManager;
import ua.p2psafety.data.Prefs;
import ua.p2psafety.sms.MessageResolver;
import ua.p2psafety.sms.MyLocation;
import ua.p2psafety.util.Logs;
import ua.p2psafety.util.Utils;

public class SosManager {
    private static SosManager mInstance;
    private Context mContext;
    private Logs logs;

    private Event mEvent;
    private boolean mSosStarted = false;

    private SosManager(Context context) {
        mContext = context;
        mSosStarted = Prefs.getSosStarted(mContext);
        mEvent = Prefs.getEvent(mContext);

        logs = new Logs(context);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();

        if (logs != null)
            logs.close();
    }

    public static SosManager getInstance(Context context) {
        if (mInstance == null)
            mInstance = new SosManager(context);
        mInstance.mContext = context;
        return mInstance;
    }

    public void startSos() {
        Utils.startVibration(mContext);

        // send SMS and email messages
        MessageResolver resolver = new MessageResolver(mContext);
        resolver.sendMessages();

        // start media recording
        switch (Prefs.getMediaRecordType(mContext)) {
            case 1:
                // record audio
                mContext.startService(new Intent(mContext, AudioRecordService.class));
                break;
            case 2:
                // record video
                mContext.startService(new Intent(mContext, VideoRecordService.class));
                break;
        }

        // show hint in notifications panel
        Notifications.notifSosStarted(mContext);

        // report event to the server
        if (Utils.isServerAuthenticated(mContext))
            serverStartSos();
        else
            Utils.setLoading(mContext, false);

        // make phone call
        mContext.startService(new Intent(mContext, PhoneCallService.class));

        setSosStarted(true);
    }

    public void stopSos() {
        // stop media recording
        mContext.stopService(new Intent(mContext, AudioRecordService.class));
        mContext.stopService(new Intent(mContext, VideoRecordService.class));

        Notifications.removeNotification(mContext, Notifications.NOTIF_SOS_STARTED_CODE);
        Notifications.notifSosCanceled(mContext);

        // TODO: send "i'm safe now" SMS and email messages (ask if needed)

        // report event to the server
        if (Utils.isFbAuthenticated(mContext))
            serverStopSos();
        else
            Utils.setLoading(mContext, false);

        mContext.stopService(new Intent(mContext, PhoneCallService.class));
        mContext.stopService(new Intent(mContext, LocationService.class));

        setSosStarted(false);
    }

    public boolean isSosStarted() {
        return mSosStarted;
    }

    public void setEvent(Event event) {
        mEvent = event;
        Prefs.putEvent(mContext, mEvent);
        if (mEvent == null)
            return;

        if (mEvent.getStatus() == Event.STATUS_ACTIVE)
            mSosStarted = true;
        else
            mSosStarted = false;
    }

    public Event getEvent() {
        return mEvent;
    }

    private void serverStartSos() {
        // if you have no event try to create one on server
        // (you must have an event at this point though)
        if (mEvent == null) {
            NetworkManager.createEvent(mContext,
                    new NetworkManager.DeliverResultRunnable<Event>() {
                        @Override
                        public void deliver(Event event) {
                            if (event != null) {
                                setEvent(event);
                                serverActivateSos(); // make this event active
                            } else {
                                Utils.setLoading(mContext, false);
                            }
                        }
                    });
        } else {
            serverActivateSos();
        }
    }

    private void serverActivateSos() {
        mEvent.setStatus(Event.STATUS_ACTIVE);
        MyLocation.LocationResult locationResult = new MyLocation.LocationResult() {
            @Override
            public void gotLocation(Location location) {
                Map data = new HashMap();
                if (location != null)
                    data.put("loc", location);
                data.put("text", Prefs.getMessage(mContext));

                NetworkManager.updateEvent(mContext, data, new NetworkManager.DeliverResultRunnable<Boolean>() {
                    @Override
                    public void deliver(Boolean aBoolean) {
                        // start sending location updates
                        mContext.startService(new Intent(mContext, LocationService.class));
                        Utils.setLoading(mContext, false);
                    }
                });
            }
        };
        MyLocation myLocation = new MyLocation(logs);
        myLocation.getLocation(mContext, locationResult);
    }

    private void serverStopSos() {
        if (mEvent != null)
            mEvent.setStatus(Event.STATUS_FINISHED);

        NetworkManager.createEvent(mContext,
                new NetworkManager.DeliverResultRunnable<Event>() {
                    @Override
                    public void deliver(Event event) {
                        setEvent(event);
                        Utils.setLoading(mContext, false);
                    }
                });
    }

    private void setSosStarted(boolean started) {
{'fraction_non_alphanumeric': 0.08458007480448827, 'fraction_numerical': 0.0017001020061203672, 'mean_word_length': 1.81055900621118, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5010326', 'n_tokens_mistral': 3135, 'n_tokens_neox': 2964, 'n_words': 679}