Skip to content

Commit

Permalink
Add XDCC toggle in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
astarivi committed Jul 26, 2023
1 parent 9c9da53 commit bc0b971
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
binding.advancedSearch.setOnCheckedChangeListener(this::triggerSave);
binding.strictModeValue.setOnCheckedChangeListener(this::triggerSave);
binding.autoMoveValue.setOnCheckedChangeListener(this::triggerSave);
binding.xdccValue.setOnCheckedChangeListener(this::triggerSave);

binding.clearCacheTrigger.setOnClickListener(view -> {
Utils.clearCache();
Expand Down Expand Up @@ -196,6 +197,11 @@ private void saveSettings(){
binding.analyticsValue.isChecked()
);

config.setBooleanProperty(
"use_xdcc",
binding.xdccValue.isChecked()
);

config.setBooleanProperty(
"show_ipv6",
binding.ipv6SorcesValue.isChecked()
Expand Down Expand Up @@ -251,6 +257,10 @@ private void loadSettings() {
);

// Switches
binding.xdccValue.setChecked(
config.getBooleanProperty("use_xdcc", false)
);

binding.analyticsValue.setChecked(
config.getBooleanProperty("analytics", true)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ public void startDownload(Context context, Result result) {
10
);

boolean useXdcc = appProperties.getBooleanProperty("use_xdcc", false);

Logger.info("User has set 'use XDCC' to {}", useXdcc);

xdccDownloader.setXDCC(
useXdcc
);

xdccDownloader.setXDCCDownloadListener(new XDCCDownloadListener() {
@Override
public void onDownloadReadyToPlay(int i, File file) {
Expand Down
39 changes: 38 additions & 1 deletion app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
>
<com.google.android.material.materialswitch.MaterialSwitch
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
Expand All @@ -371,6 +371,43 @@
android:fontFamily="@font/lato_regular"
/>
</LinearLayout>

<View
style="@style/Divider"
android:layout_marginEnd="25dp"
android:layout_marginStart="25dp"
android:layout_marginTop="15dp"
android:layout_marginBottom="10dp"
/>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<com.google.android.material.materialswitch.MaterialSwitch
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:textSize="17sp"
android:textColor="?attr/colorOnSurfaceVariant"
android:text="@string/xdcc_title"
android:layout_width="match_parent"
android:layout_height="42dp"
android:id="@+id/xdccValue"
android:fontFamily="@font/lato_regular"
/>
<TextView
android:layout_marginStart="10dp"
android:layout_marginEnd="90dp"
android:textSize="13sp"
android:textColor="?attr/colorOnSurfaceVariant"
android:text="@string/xdcc_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/lato_regular"
/>
</LinearLayout>
</LinearLayout>
</com.google.android.material.card.MaterialCardView>

Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_home_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="24dp"
>
>

<TextView
android:id="@+id/home_items_title"
Expand All @@ -23,6 +23,8 @@
android:id="@+id/home_items_recycler"
android:layout_width="match_parent"
android:layout_height="196dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:clipToPadding="false"
android:clipChildren="false"
/>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@
<string name="dev_hide_title">Hide Developer Menu</string>
<string name="dev_hide_description">Hides the developer section again</string>
<string name="dev_section_toast">Welcome to developer mode, fellow weeb</string>
<string name="xdcc_title">Use XDCC</string>
<string name="xdcc_description">When enabled, XDCC will be used instead of DCC; this may result in faster downloads. May cause complete download failure</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.astarivi.kaizolib.xdcc.base.XDCCDownloadListener;
import com.astarivi.kaizolib.xdcc.base.XDCCFailure;
import com.astarivi.kaizolib.xdcc.model.DCC;

import org.tinylog.Logger;

import java.io.*;
Expand All @@ -21,6 +22,7 @@ public class XDCCDownloader {
private int speedKBps = 0;
private long lastTimeReminderRan = 0;
private XDCCDownloadListener downloadEventListener;
private boolean isXDCC = false;

// Uses default timeout of 20 seconds
public XDCCDownloader(DCC dcc, File downloadFile) {
Expand All @@ -41,6 +43,10 @@ public void setXDCCDownloadListener(XDCCDownloadListener listener){
downloadEventListener = listener;
}

public void setXDCC(boolean value) {
isXDCC = value;
}

@SuppressWarnings("unused")
public void stop() {
this.stop = true;
Expand Down Expand Up @@ -102,7 +108,7 @@ public void start() {
Logger.debug("About to start download loop. Downloading from: " + dcc.getIp());

// Signal start
output.write(0);
write(output, 0);

while (downloadedLength < fileLength && !stop) {
// Has the download not even started?
Expand Down Expand Up @@ -135,7 +141,7 @@ public void start() {

fileOutput.write(buffer, 0, read);
// For old DCC compatibility
output.write((int) downloadedLength);
write(output, (int) downloadedLength);

//Update progress every 400 repetitions to avoid calculating the progress so often.
if (repetitions >= 400) {
Expand Down Expand Up @@ -210,7 +216,7 @@ private void contactServer(OutputStream output) throws IOException {
// First time we run
if (lastTimeReminderRan == 0) {
lastTimeReminderRan = timeNow;
output.write(0);
write(output, 0);
Logger.debug("Telling server that we're waiting.");
return;
}
Expand All @@ -220,7 +226,13 @@ private void contactServer(OutputStream output) throws IOException {
}

lastTimeReminderRan = timeNow;
output.write(0);
write(output, 0);
Logger.debug("Telling server that we're waiting.");
}

private void write(OutputStream stream, int value) throws IOException {
if (isXDCC) return;

stream.write(value);
}
}

0 comments on commit bc0b971

Please sign in to comment.