package org.travinskiy.alex.universalcontroller;

import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

public class ConnectionService extends IntentService {

    private static final int RUN_INTERVAL = 60 * 1000;

    public static Intent newIntent(Context context) {
        return new Intent(context, ConnectionService.class);
    }

    public ConnectionService() {
        super(null);
    }

    public static boolean isServiceOn(Context context) {
        Intent intent = newIntent(context);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_NO_CREATE);
        return pendingIntent != null;
    }

    public static void setServiceAlarm(Context context, boolean turnOn) {
        Intent intent = newIntent(context);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);

        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        if (turnOn) {
            alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(),
                    RUN_INTERVAL, pendingIntent);
        } else {
            alarmManager.cancel(pendingIntent);
            pendingIntent.cancel();
        }
    }

    @Override
    protected void onHandleIntent(Intent intent) {
    }
}

@@ -226,22 +226,177 @@
 oid.
-os.SystemClock
+net.wifi.WifiInfo;%0Aimport android.net.wifi.WifiManager;%0Aimport android.os.Build;%0Aimport android.os.SystemClock;%0Aimport android.provider.Settings;%0Aimport android.util.Log
 ;%0A%0Ap
@@ -475,19 +475,20 @@
 c final 
-int
+long
  RUN_INT
@@ -505,16 +505,81 @@
  * 1000;
+%0A    private static final long DELAY_TO_WIFI_CONNECT = 15 * 1000;
 %0A%0A    pu
@@ -1688,12 +1688,1376 @@
 ) %7B%0A    
-%7D
+    boolean stopInAirplaneMode = AppPreferences.getPrefStopInAirplaneMode(getApplicationContext());%0A        if (stopInAirplaneMode && isAirplaneModeOn(getApplicationContext())) %7B%0A            Log.i(%22AIRPLANE_MODE_ON%22, %22TRUE%22);%0A            return;%0A        %7D%0A        WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);%0A        if (wifiManager.isWifiEnabled()) %7B%0A            disableNotConnectedWifi(wifiManager);%0A        %7D else %7B%0A            wifiManager.setWifiEnabled(true);%0A            SystemClock.sleep(DELAY_TO_WIFI_CONNECT);%0A            disableNotConnectedWifi(wifiManager);%0A        %7D%0A    %7D%0A%0A    private void disableNotConnectedWifi(WifiManager wifiManager) %7B%0A        WifiInfo wifiInfo = wifiManager.getConnectionInfo();%0A        if (wifiInfo.getSSID() == null) %7B%0A            wifiManager.setWifiEnabled(false);%0A        %7D else %7B%0A            Log.i(%22WIFI_SSID%22, wifiInfo.getSSID());%0A        %7D%0A    %7D%0A%0A    private static boolean isAirplaneModeOn(Context context) %7B%0A        if (Build.VERSION.SDK_INT %3C Build.VERSION_CODES.JELLY_BEAN_MR1) %7B%0A            return Settings.System.getInt(context.getContentResolver(),%0A                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;%0A        %7D else %7B%0A            return Settings.Global.getInt(context.getContentResolver(),%0A                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;%0A        %7D%0A    %7D%0A
 %0A%7D%0A

Implement onHandleIntent to turn on and off Wifi