Advertisement
Guest User

Untitled

a guest
Jul 25th, 2012
692
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. //    jDownloader - Downloadmanager
  2. //    Copyright (C) 2009  JD-Team support@jdownloader.org
  3. //
  4. //    This program is free software: you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation, either version 3 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. package org.jdownloader.extensions.antistandby;
  18.  
  19. import jd.controlling.downloadcontroller.DownloadWatchDog;
  20.  
  21. import org.appwork.utils.logging.Log;
  22. import org.appwork.utils.logging2.LogSource;
  23. import org.jdownloader.logging.LogController;
  24.  
  25. import com.sun.jna.Native;
  26.  
  27. public class WindowsAntiStandby extends Thread implements Runnable {
  28.  
  29.     private boolean                    run      = false;
  30.     private static final int           sleep    = 5000;
  31.     private final AntiStandbyExtension jdAntiStandby;
  32.  
  33.     private Kernel32                   kernel32 = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
  34.  
  35.     public WindowsAntiStandby(AntiStandbyExtension jdAntiStandby) {
  36.         super();
  37.         this.jdAntiStandby = jdAntiStandby;
  38.     }
  39.  
  40.     @Override
  41.     public void run() {
  42.         LogSource logger = LogController.CL(AntiStandbyExtension.class);
  43.         try {
  44.             while (!isInterrupted()) {
  45.  
  46.                 switch (jdAntiStandby.getMode()) {
  47.                 case DOWNLOADING:
  48.                     if (DownloadWatchDog.getInstance().getStateMachine().hasPassed(DownloadWatchDog.RUNNING_STATE, DownloadWatchDog.STOPPING_STATE)) {
  49.                         if (!run) {
  50.                             run = true;
  51.                             logger.fine("JDAntiStandby: Start");
  52.                         }
  53.                         kernel32.SetThreadExecutionState(Kernel32.ES_CONTINUOUS | Kernel32.ES_SYSTEM_REQUIRED | Kernel32.ES_DISPLAY_REQUIRED);
  54.  
  55.                     } else {
  56.                         if (run) {
  57.                             run = false;
  58.                             logger.fine("JDAntiStandby: Stop");
  59.                             kernel32.SetThreadExecutionState(Kernel32.ES_CONTINUOUS);
  60.                         }
  61.                     }
  62.                     break;
  63.                 case RUNNING:
  64.                     if (!run) {
  65.                         run = true;
  66.                         logger.fine("JDAntiStandby: Start");
  67.                     }
  68.                     kernel32.SetThreadExecutionState(Kernel32.ES_CONTINUOUS | Kernel32.ES_SYSTEM_REQUIRED | Kernel32.ES_DISPLAY_REQUIRED);
  69.                     break;
  70.                 default:
  71.                     logger.finest("JDAntiStandby: Config error (unknown mode: " + jdAntiStandby.getMode() + ")");
  72.                 }
  73.  
  74.                 Thread.sleep(sleep);
  75.  
  76.             }
  77.  
  78.         } catch (InterruptedException e) {
  79.             Log.exception(e);
  80.         } finally {
  81.             logger.close();
  82.             kernel32.SetThreadExecutionState(Kernel32.ES_CONTINUOUS);
  83.             logger.fine("JDAntiStandby: Terminated");
  84.  
  85.         }
  86.     }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement