package de.onemoresecond.traffic.activities;

import android.app.FragmentManager;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import de.onemoresecond.traffic.R;
import de.onemoresecond.traffic.fragments.SettingsFragment;
import de.onemoresecond.traffic.fragments.TrafficFragment;
import de.onemoresecond.traffic.util.TrafficServiceControl;


public class MainActivity extends AppCompatActivity {

    private Menu menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new TrafficFragment())
                    .commit();
        }

        getFragmentManager().addOnBackStackChangedListener(
            new FragmentManager.OnBackStackChangedListener() {

            @Override
            public void onBackStackChanged() {
                if (getFragmentManager().getBackStackEntryCount() > 0) {
                    if (getSupportActionBar() != null)
                        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                } else {
                    if (getSupportActionBar() != null)
                        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
                }
            }
        });

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        if (preferences.getBoolean("pref_use_service", false)) {
            TrafficServiceControl.start(this);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        this.menu = menu;
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            showOverflowMenu(false);
            getFragmentManager().beginTransaction()
                    .replace(R.id.container, new SettingsFragment())
                    .addToBackStack("settings")
                    .commit();
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        showOverflowMenu(true);
    }

    @Override
    public boolean onSupportNavigateUp() {
        getFragmentManager().popBackStack();
        showOverflowMenu(true);
        return true;
    }

    public void showOverflowMenu(boolean showMenu){
        if(menu == null)
            return;
        menu.setGroupVisible(R.id.settings_group, showMenu);
    }
}

@@ -58,31 +58,99 @@
 oid.app.
-FragmentManager
+ActivityManager;%0Aimport android.app.FragmentManager;%0Aimport android.content.Context
 ;%0Aimport
@@ -550,32 +550,89 @@
 rafficFragment;%0A
+import de.onemoresecond.traffic.services.TrafficService;%0A
 import de.onemor
@@ -2104,16 +2104,38 @@
 , false)
+ && !checkForService()
 ) %7B%0A    
@@ -3206,16 +3206,16 @@
 return;%0A
-
         
@@ -3272,11 +3272,418 @@
 );%0A    %7D
+%0A%0A    public boolean checkForService() %7B%0A        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);%0A        for (ActivityManager.RunningServiceInfo info: manager.getRunningServices(Integer.MAX_VALUE)) %7B%0A            if (TrafficService.class.getName().equals(info.service.getClassName())) %7B%0A                return true;%0A            %7D%0A        %7D%0A%0A        return false;%0A    %7D
 %0A%7D%0A

Check if service is running already before starting it