-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1712 from ManfredKarrer/PreventAppNap
Play silent sound to avoid standby mode
- Loading branch information
Showing
9 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* This file is part of Bisq. | ||
* | ||
* Bisq is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or (at | ||
* your option) any later version. | ||
* | ||
* Bisq is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public | ||
* License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Bisq. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package bisq.core.app; | ||
|
||
import bisq.core.user.Preferences; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
|
||
|
||
import javax.sound.sampled.AudioFormat; | ||
import javax.sound.sampled.AudioInputStream; | ||
import javax.sound.sampled.AudioSystem; | ||
import javax.sound.sampled.DataLine; | ||
import javax.sound.sampled.SourceDataLine; | ||
|
||
@Slf4j | ||
public class AvoidStandbyMode { | ||
private final Preferences preferences; | ||
private volatile boolean isStopped; | ||
|
||
@Inject | ||
public AvoidStandbyMode(Preferences preferences) { | ||
this.preferences = preferences; | ||
|
||
preferences.getUseStandbyModeProperty().addListener((observable, oldValue, newValue) -> { | ||
if (newValue) { | ||
isStopped = true; | ||
log.info("AvoidStandbyMode stopped"); | ||
} else { | ||
start(); | ||
} | ||
}); | ||
} | ||
|
||
public void init() { | ||
isStopped = preferences.isUseStandbyMode(); | ||
if (!isStopped) { | ||
start(); | ||
} | ||
} | ||
|
||
private void start() { | ||
isStopped = false; | ||
log.info("AvoidStandbyMode started"); | ||
Thread thread = new Thread(this::play); | ||
thread.setName("AvoidStandbyMode-thread"); | ||
thread.start(); | ||
} | ||
|
||
|
||
private void play() { | ||
if (!isStopped) { | ||
OutputStream outputStream = null; | ||
InputStream inputStream = null; | ||
try { | ||
inputStream = getClass().getClassLoader().getResourceAsStream("silent.aiff"); | ||
File soundFile = File.createTempFile("Bisq-", "-PreventAppNap-soundFile"); | ||
soundFile.deleteOnExit(); | ||
outputStream = new FileOutputStream(soundFile); | ||
IOUtils.copy(inputStream, outputStream); | ||
outputStream.close(); | ||
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundFile); | ||
byte tempBuffer[] = new byte[10000]; | ||
AudioFormat audioFormat = audioInputStream.getFormat(); | ||
DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat); | ||
SourceDataLine sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); | ||
sourceDataLine.open(audioFormat); | ||
sourceDataLine.start(); | ||
int cnt; | ||
while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1 && !isStopped) { | ||
if (cnt > 0) { | ||
sourceDataLine.write(tempBuffer, 0, cnt); | ||
} | ||
} | ||
sourceDataLine.drain(); | ||
sourceDataLine.close(); | ||
play(); | ||
} catch (Exception e) { | ||
log.error(e.toString()); | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (inputStream != null) | ||
inputStream.close(); | ||
if (outputStream != null) | ||
outputStream.close(); | ||
} catch (IOException ignore) { | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters