@Override
  public void onDestroy() {
    super.onDestroy();
    destroyed = true;

    try {
      android.provider.Settings.System.putInt(
          getContentResolver(), SAMSUNG_BACK_LIGHT_SETTING, samsungBackLightValue);
    } catch (Exception e) {
    }

    if (lightSensor != null) {
      unregisterReceiver(screenOffReceiver);
      sensors.unregisterListener(this, lightSensor);
    }
    if (view != null) {
      windowManager.removeView(view);
    }
    Ntf.show(this, false);
    Log.d(LOG, "Service stopped");
    running = false;
    MainActivity guiCopy = gui;
    if (guiCopy != null) guiCopy.updateCheckbox();
  }
  @Override
  public void onCreate() {
    super.onCreate();

    running = true;
    MainActivity guiCopy = gui;
    if (guiCopy != null) {
      guiCopy.updateCheckbox();
    }

    Log.d(LOG, "Service started");
    Cfg.Init(this);

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    view = new ImageView(this);
    DisplayMetrics metrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(metrics);
    bmp = Bitmap.createBitmap(Grids.GridSideSize, Grids.GridSideSize, Bitmap.Config.ARGB_4444);

    updatePattern();
    BitmapDrawable draw = new BitmapDrawable(bmp);
    draw.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    draw.setFilterBitmap(false);
    draw.setAntiAlias(false);
    view.setBackground(draw);

    WindowManager.LayoutParams params =
        new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams
                .TYPE_SYSTEM_OVERLAY, // WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                                      // //WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                                      // //WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
            PixelFormat.TRANSPARENT);

    params.buttonBrightness = 0.0f;
    params.dimAmount = 0.0f;

    windowManager.addView(view, params);

    final Handler handler = new Handler();
    handler.postDelayed(
        new Runnable() {
          @Override
          public void run() {
            updatePattern();
            view.invalidate();
            if (!destroyed) {
              handler.postDelayed(this, Grids.ShiftTimeouts[Cfg.ShiftTimeoutIdx]);
            }
          }
        },
        Grids.ShiftTimeouts[Cfg.ShiftTimeoutIdx]);

    if (Cfg.UseLightSensor) {
      sensors = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
      lightSensor = sensors.getDefaultSensor(Sensor.TYPE_LIGHT);
      if (lightSensor != null) {
        StartSensor.get().registerListener(sensors, this, lightSensor, 1200000, 1000000);
      }

      IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
      filter.addAction(Intent.ACTION_SCREEN_OFF);
      screenOffReceiver = new ScreenOffReceiver();
      registerReceiver(screenOffReceiver, filter);
    }

    try {
      samsungBackLightValue =
          android.provider.Settings.System.getInt(getContentResolver(), SAMSUNG_BACK_LIGHT_SETTING);
      android.provider.Settings.System.putInt(getContentResolver(), SAMSUNG_BACK_LIGHT_SETTING, 0);
    } catch (Exception e) {
    }
  }