use of com.biglybt.pif.utils.PowerManagementListener in project BiglyBT by BiglySoftware.
the class CoreImpl method checkSleepActions.
protected void checkSleepActions() {
boolean ps_downloading = COConfigurationManager.getBooleanParameter("Prevent Sleep Downloading");
boolean ps_fp_seed = COConfigurationManager.getBooleanParameter("Prevent Sleep FP Seeding");
String tag_name = COConfigurationManager.getStringParameter("Prevent Sleep Tag");
Tag ps_tag = null;
tag_name = tag_name.trim();
if (!tag_name.isEmpty()) {
ps_tag = TagManagerFactory.getTagManager().getTagType(TagType.TT_DOWNLOAD_MANUAL).getTag(tag_name, true);
}
String declining_subsystems = "";
for (PowerManagementListener l : power_listeners) {
try {
if (!l.requestPowerStateChange(PowerManagementListener.ST_SLEEP, null)) {
declining_subsystems += (declining_subsystems.length() == 0 ? "" : ",") + l.getPowerName();
}
} catch (Throwable e) {
Debug.out(e);
}
}
PlatformManager platform = PlatformManagerFactory.getPlatformManager();
if (declining_subsystems.length() == 0 && !(ps_downloading || ps_fp_seed || ps_tag != null)) {
if (platform.getPreventComputerSleep()) {
setPreventComputerSleep(platform, false, "configuration change");
}
return;
}
boolean prevent_sleep = false;
String prevent_reason = null;
if (declining_subsystems.length() > 0) {
prevent_sleep = true;
prevent_reason = "subsystems declined sleep: " + declining_subsystems;
} else if (ps_tag != null && ps_tag.getTaggedCount() > 0) {
prevent_sleep = true;
prevent_reason = "tag '" + tag_name + "' has entries";
} else {
List<DownloadManager> managers = getGlobalManager().getDownloadManagers();
for (DownloadManager manager : managers) {
int state = manager.getState();
if (state == DownloadManager.STATE_FINISHING || manager.getDownloadState().getFlag(DownloadManagerState.FLAG_METADATA_DOWNLOAD)) {
if (ps_downloading) {
prevent_sleep = true;
prevent_reason = "active downloads";
break;
}
} else {
if (state == DownloadManager.STATE_DOWNLOADING) {
PEPeerManager pm = manager.getPeerManager();
if (pm != null) {
if (pm.hasDownloadablePiece()) {
if (ps_downloading) {
prevent_sleep = true;
prevent_reason = "active downloads";
break;
}
} else {
// its effectively seeding, change so logic about recheck obeyed below
state = DownloadManager.STATE_SEEDING;
}
}
}
if (state == DownloadManager.STATE_SEEDING && ps_fp_seed) {
DiskManager disk_manager = manager.getDiskManager();
if (disk_manager != null && disk_manager.getCompleteRecheckStatus() != -1) {
if (ps_downloading) {
prevent_sleep = true;
prevent_reason = "active downloads";
break;
}
} else {
try {
DefaultRankCalculator calc = StartStopRulesDefaultPlugin.getRankCalculator(PluginCoreUtils.wrap(manager));
if (calc.getCachedIsFP()) {
prevent_sleep = true;
prevent_reason = "first-priority seeding";
break;
}
} catch (Throwable e) {
}
}
}
}
}
}
if (prevent_sleep != platform.getPreventComputerSleep()) {
if (prevent_sleep) {
prevent_sleep_remove_trigger = false;
} else {
if (!prevent_sleep_remove_trigger) {
prevent_sleep_remove_trigger = true;
return;
}
}
if (prevent_reason == null) {
if (ps_downloading && ps_fp_seed) {
prevent_reason = "no active downloads or first-priority seeding";
} else if (ps_downloading) {
prevent_reason = "no active downloads";
} else {
prevent_reason = "no active first-priority seeding";
}
}
setPreventComputerSleep(platform, prevent_sleep, prevent_reason);
}
}
Aggregations