Support keeping system awake while downloading anything

This commit is contained in:
drdev
2016-04-17 20:33:38 +00:00
parent 27e0451026
commit 4b5e6acbc0
9 changed files with 49 additions and 2 deletions

View File

@@ -17,6 +17,7 @@ import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Environment; import android.os.Environment;
import android.view.WindowManager;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
@@ -210,5 +211,15 @@ public class Main extends AndroidApplication {
FileUtil.deleteFile(switchOrientationFile); FileUtil.deleteFile(switchOrientationFile);
} }
} }
@Override
public void preventSystemSleep(boolean preventSleep) {
if (preventSleep) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
} }
} }

View File

@@ -283,4 +283,9 @@ public class GuiDesktop implements IGuiBase {
public String encodeSymbols(String str, boolean formatReminderText) { public String encodeSymbols(String str, boolean formatReminderText) {
return FSkin.encodeSymbols(str, formatReminderText); return FSkin.encodeSymbols(str, formatReminderText);
} }
@Override
public void preventSystemSleep(boolean preventSleep) {
}
} }

View File

@@ -75,6 +75,11 @@ public class Main extends IOSApplication.Delegate {
// TODO implement this // TODO implement this
} }
@Override
public void preventSystemSleep(boolean preventSleep) {
// TODO implement this
}
@Override @Override
public boolean isTablet() { public boolean isTablet() {
return Gdx.graphics.getWidth() > Gdx.graphics.getHeight(); return Gdx.graphics.getWidth() > Gdx.graphics.getHeight();

View File

@@ -93,5 +93,11 @@ public class Main {
FileUtil.deleteFile(switchOrientationFile); FileUtil.deleteFile(switchOrientationFile);
} }
} }
@Override
public void preventSystemSleep(boolean preventSleep) {
int k = 1;
k++;
}
} }
} }

View File

@@ -290,4 +290,9 @@ public class GuiMobile implements IGuiBase {
public String encodeSymbols(String str, boolean formatReminderText) { public String encodeSymbols(String str, boolean formatReminderText) {
return str; //not needed for mobile return str; //not needed for mobile
} }
@Override
public void preventSystemSleep(boolean preventSleep) {
Forge.getDeviceAdapter().preventSystemSleep(preventSleep);
}
} }

View File

@@ -227,6 +227,7 @@ public abstract class GuiDownloadService implements Runnable {
@Override @Override
public void run() { public void run() {
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while downloading
Proxy p = getProxy(); Proxy p = getProxy();
@@ -294,8 +295,9 @@ public abstract class GuiDownloadService implements Runnable {
} }
update(++iCard, fileDest); update(++iCard, fileDest);
} }
GuiBase.getInterface().preventSystemSleep(false);
} }
protected Proxy getProxy() { protected Proxy getProxy() {

View File

@@ -19,6 +19,7 @@ import com.esotericsoftware.minlog.Log;
import com.google.common.io.Files; import com.google.common.io.Files;
import forge.FThreads; import forge.FThreads;
import forge.GuiBase;
import forge.interfaces.IProgressBar; import forge.interfaces.IProgressBar;
import forge.util.FileUtil; import forge.util.FileUtil;
@@ -73,6 +74,8 @@ public class GuiDownloadZipService extends GuiDownloadService {
//if assets.zip downloaded successfully, unzip into destination folder //if assets.zip downloaded successfully, unzip into destination folder
try { try {
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while unzipping
if (deleteFolder != null) { if (deleteFolder != null) {
final File deleteDir = new File(deleteFolder); final File deleteDir = new File(deleteFolder);
if (deleteDir.exists()) { if (deleteDir.exists()) {
@@ -132,9 +135,14 @@ public class GuiDownloadZipService extends GuiDownloadService {
catch (final Exception e) { catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
finally {
GuiBase.getInterface().preventSystemSleep(false);
}
} }
public String download(final String filename) { public String download(final String filename) {
GuiBase.getInterface().preventSystemSleep(true); //prevent system from going into sleep mode while downloading
progressBar.reset(); progressBar.reset();
progressBar.setPercentMode(true); progressBar.setPercentMode(true);
progressBar.setDescription("Downloading " + desc); progressBar.setDescription("Downloading " + desc);
@@ -194,8 +202,11 @@ public class GuiDownloadZipService extends GuiDownloadService {
} }
catch (final Exception ex) { catch (final Exception ex) {
Log.error("Downloading " + desc, "Error downloading " + desc, ex); Log.error("Downloading " + desc, "Error downloading " + desc, ex);
return null;
}
finally {
GuiBase.getInterface().preventSystemSleep(false);
} }
return null;
} }
protected void copyInputStream(final InputStream in, final String outPath) throws IOException{ protected void copyInputStream(final InputStream in, final String outPath) throws IOException{

View File

@@ -7,6 +7,7 @@ public interface IDeviceAdapter {
String getDownloadsDir(); String getDownloadsDir();
boolean openFile(String filename); boolean openFile(String filename);
void setLandscapeMode(boolean landscapeMode); void setLandscapeMode(boolean landscapeMode);
void preventSystemSleep(boolean preventSleep);
void restart(); void restart();
void exit(); void exit();
} }

View File

@@ -53,4 +53,5 @@ public interface IGuiBase {
HostedMatch hostMatch(); HostedMatch hostMatch();
void runBackgroundTask(String message, Runnable task); void runBackgroundTask(String message, Runnable task);
String encodeSymbols(String str, boolean formatReminderText); String encodeSymbols(String str, boolean formatReminderText);
void preventSystemSleep(boolean preventSleep);
} }