private void doDim(boolean fade) {
    View tintView = findViewById(R.id.window_tint);
    if (tintView == null) return;

    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();

    winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
    winParams.flags |= (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

    // dim the wallpaper somewhat (how much is determined below)
    winParams.flags |= (WindowManager.LayoutParams.FLAG_DIM_BEHIND);

    if (mDimmed) {
      winParams.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;
      winParams.dimAmount = DIM_BEHIND_AMOUNT_DIMMED;
      winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_OFF;

      // show the window tint
      tintView.startAnimation(
          AnimationUtils.loadAnimation(this, fade ? R.anim.dim : R.anim.dim_instant));
    } else {
      winParams.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
      winParams.dimAmount = DIM_BEHIND_AMOUNT_NORMAL;
      winParams.buttonBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE;

      // hide the window tint
      tintView.startAnimation(
          AnimationUtils.loadAnimation(this, fade ? R.anim.undim : R.anim.undim_instant));
    }

    win.setAttributes(winParams);
  }
 private void setClockStyle() {
   Utils.setClockStyle(
       this, mDigitalClock, mAnalogClock, ScreensaverSettingsActivity.KEY_CLOCK_STYLE);
   mSaverView = findViewById(R.id.main_clock);
   int brightness =
       PreferenceManager.getDefaultSharedPreferences(this)
           .getInt(
               ScreensaverSettingsActivity.KEY_BRIGHTNESS,
               ScreensaverSettingsActivity.BRIGHTNESS_DEFAULT);
   Utils.dimView(brightness, mSaverView);
   boolean dim = brightness < ScreensaverSettingsActivity.BRIGHTNESS_NIGHT;
   if (dim) {
     WindowManager.LayoutParams lp = getWindow().getAttributes();
     lp.buttonBrightness = 0;
     lp.screenBrightness = 0.01f;
     getWindow().setAttributes(lp);
   }
 }
  @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) {
    }
  }