// Copyright 2019 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.platform;

import com.google.common.annotations.VisibleForTesting;
import com.google.devtools.build.lib.runtime.BlazeModule;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.util.AbruptExitException;

/** Prevents the computer from going to sleep while a Bazel command is running. */
public final class SleepPreventionModule extends BlazeModule {

  /** Methods for dealing with sleep prevention on local hardware. */
  @VisibleForTesting
  public static final class SleepPrevention extends JniLoader {
    /**
     * Push a request to disable automatic sleep for hardware. Useful for making sure computers
     * don't go to sleep during long builds. Must be matched with a {@link #popDisableSleep} call.
     *
     * @return 0 on success, -1 if sleep is not supported.
     */
    public static native int pushDisableSleep();

    /**
     * Pop a request to disable automatic sleep for hardware. Useful for making sure computers don't
     * go to sleep during long builds. Must be matched with a previous {@link #pushDisableSleep}
     * call.
     *
     * @return 0 on success, -1 if sleep is not supported.
     */
    public static native int popDisableSleep();
  }

  private final Boolean jniEnabled;

  public SleepPreventionModule() {
    this.jniEnabled = !"0".equals(System.getProperty("io.bazel.EnableJni"));
  }

  @Override
  public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
    if (jniEnabled) {
      SleepPrevention.pushDisableSleep();
    }
  }

  @Override
  public void afterCommand() throws AbruptExitException {
    if (jniEnabled) {
      SleepPrevention.popDisableSleep();
    }
  }
}

@@ -1880,162 +1880,8 @@
  %7D%0A%0A
-  private final Boolean jniEnabled;%0A%0A  public SleepPreventionModule() %7B%0A    this.jniEnabled = !%220%22.equals(System.getProperty(%22io.bazel.EnableJni%22));%0A  %7D%0A%0A
   @O
@@ -1880,32 +1880,32 @@
  %7D%0A%0A  @Override%0A
+
   public void be
@@ -1969,32 +1969,42 @@
 ption %7B%0A    if (
+JniLoader.
 jniEnabled) %7B%0A  
@@ -1989,32 +1989,34 @@
 oader.jniEnabled
+()
 ) %7B%0A      SleepP
@@ -2134,16 +2134,26 @@
     if (
+JniLoader.
 jniEnabl
@@ -2154,16 +2154,18 @@
 iEnabled
+()
 ) %7B%0A    

Move over to using JniLoader.jniEnabled to simplify code.