Skip to content

Commit

Permalink
Fix global change in night mode #2
Browse files Browse the repository at this point in the history
Fix whitespaces in folder name Dar9586/NClientV2#371
Added .torrent download and open Dar9586/NClientV2#374
Bug fix
  • Loading branch information
aha1008 committed Jan 30, 2023
1 parent d116123 commit d51956d
Show file tree
Hide file tree
Showing 18 changed files with 96 additions and 42 deletions.
46 changes: 39 additions & 7 deletions app/src/main/java/com/dar/nclientv2/GalleryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.FileProvider;

import com.dar.nclientv2.adapters.GalleryAdapter;
import com.dar.nclientv2.api.InspectorV3;
Expand All @@ -46,6 +47,7 @@

import net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -253,12 +255,9 @@ private void menuItemsVisible(Menu menu) {
menu.findItem(R.id.related).setVisible(isValidOnline);
menu.findItem(R.id.comments).setVisible(isValidOnline);

menu.findItem(R.id.download_torrent).setVisible(isLogged);
menu.findItem(R.id.share).setVisible(gallery.isValid());
menu.findItem(R.id.load_internet).setVisible(isLocal && gallery.isValid());

if (isValidOnline && isLogged) ;
//instantiateWebView();

}

@Override
Expand All @@ -278,6 +277,7 @@ public boolean onOptionsItemSelected(final MenuItem item) {
requestStorage();
} else if (id == R.id.add_online_gallery) addToFavorite(item);
else if (id == R.id.change_view) updateColumnCount(true);
else if (id == R.id.download_torrent) downloadTorrent();
else if (id == R.id.load_internet) toInternet();
else if (id == R.id.manage_status) updateStatus();
else if (id == R.id.share) Global.shareGallery(this, gallery);
Expand All @@ -286,9 +286,6 @@ else if (id == R.id.comments) {
i.putExtra(getPackageName() + ".GALLERYID", gallery.getId());
startActivity(i);
} else if (id == R.id.related) {
/*Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(getPackageName() + ".RELATED", gallery.getId());
startActivity(intent);*/
recycler.smoothScrollToPosition(recycler.getAdapter().getItemCount());
} else if (id == R.id.favorite_manager) {
if (isLocalFavorite) {
Expand All @@ -306,6 +303,41 @@ else if (id == R.id.comments) {
return super.onOptionsItemSelected(item);
}

private void downloadTorrent() {
if(!Global.hasStoragePermission(this)){
return;
}

String url = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/download", gallery.getId());
String referer = String.format(Locale.US, Utility.getBaseUrl() + "g/%d/", gallery.getId());

new AuthRequest(referer, url, new Callback() {
@Override
public void onFailure(@NonNull Call call,@NonNull IOException e) {
GalleryActivity.this.runOnUiThread(() ->
Toast.makeText(GalleryActivity.this, R.string.failed, Toast.LENGTH_SHORT).show()
);
}

@Override
public void onResponse(@NonNull Call call,@NonNull Response response) throws IOException {
File file=new File(Global.TORRENTFOLDER,gallery.getId()+".torrent");
Utility.writeStreamToFile(response.body().byteStream(), file);
Intent intent=new Intent(Intent.ACTION_VIEW);
Uri torrentUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
torrentUri = FileProvider.getUriForFile(GalleryActivity.this, GalleryActivity.this.getPackageName() + ".provider", file);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}else{
torrentUri=Uri.fromFile(file);
}
intent.setDataAndType(torrentUri, "application/x-bittorrent");
GalleryActivity.this.startActivity(intent);
file.deleteOnExit();
}
}).setMethod("GET",null).start();
}

private void updateStatus() {
List<String> statuses = StatusManager.getNames();
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(this);
Expand Down
10 changes: 6 additions & 4 deletions app/src/main/java/com/dar/nclientv2/adapters/LocalAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,12 @@ public void triggerUpdateProgress(GalleryDownloaderV2 downloader, int reach, int
public void triggerEndDownload(GalleryDownloaderV2 downloader) {
LocalGallery l = downloader.localGallery();
galleryDownloaders.remove(downloader);
dataset.remove(l);
dataset.add(l);
LogUtility.d(l);
sortElements();
if(l!=null) {
dataset.remove(l);
dataset.add(l);
LogUtility.d(l);
sortElements();
}
context.runOnUiThread(() -> notifyItemRangeChanged(0, getItemCount()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static String getPathTitle(@Nullable String title, @NonNull String defaul
String pathTitle = title.replace('/', ' ').replaceAll("[/|\\\\*\"'?:<>]", " ");
while (pathTitle.contains(" "))
pathTitle = pathTitle.replace(" ", " ");
return pathTitle;
return pathTitle.trim();
}

public static String getPathTitle(@Nullable String title) {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/dar/nclientv2/async/VersionChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public void onFailure(@NonNull Call call, @NonNull IOException e) {
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
context.getSharedPreferences("Settings", 0).edit().putBoolean("downloaded", false).apply();
if (Global.UPDATEFOLDER == null) {
Global.initStorage(context);
}
Global.UPDATEFOLDER.mkdirs();
f.createNewFile();
FileOutputStream stream = new FileOutputStream(f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
import com.dar.nclientv2.utility.Utility;

import java.io.File;
import java.io.FileOutputStream;

import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -277,7 +277,7 @@ private boolean savePage(PageContainer page) {
r.close();
return false;
}
long written = writeStreamToFile(r.body().byteStream(), filePath);
long written = Utility.writeStreamToFile(r.body().byteStream(), filePath);
r.close();
if (written != len) {
filePath.delete();
Expand All @@ -290,20 +290,7 @@ private boolean savePage(PageContainer page) {
return false;
}

private long writeStreamToFile(InputStream inputStream, File filePath) throws IOException {
FileOutputStream outputStream = new FileOutputStream(filePath);
int read;
long totalByte = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
totalByte += read;
}
outputStream.flush();
outputStream.close();
inputStream.close();
return totalByte;
}


public void initDownload() {
if (initialized) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
});

photoView.setOnScaleChangeListener((float scaleFactor, float focusX, float focusY)->{
this.zoomChangeListener.onZoomChange(rootView, photoView.getScale());
});
if(this.zoomChangeListener!=null) {
this.zoomChangeListener.onZoomChange(rootView, photoView.getScale());

photoView.setMaximumScale(MAX_SCALE);
retryButton.setOnClickListener(v -> loadImage());
Expand Down
12 changes: 7 additions & 5 deletions app/src/main/java/com/dar/nclientv2/settings/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.UiModeManager;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -67,6 +67,7 @@ public class Global {
private static final String PDFFOLDER_NAME = "PDF";
private static final String UPDATEFOLDER_NAME = "Update";
private static final String ZIPFOLDER_NAME = "ZIP";
private static final String TORRENTFOLDER_NAME = "Torrents";
private static final String BACKUPFOLDER_NAME = "Backup";
private static final DisplayMetrics lastDisplay = new DisplayMetrics();
public static OkHttpClient client = null;
Expand All @@ -76,6 +77,7 @@ public class Global {
public static File SCREENFOLDER;
public static File PDFFOLDER;
public static File UPDATEFOLDER;
public static File TORRENTFOLDER;
public static File ZIPFOLDER;
public static File BACKUPFOLDER;
private static Language onlyLanguage;
Expand Down Expand Up @@ -164,7 +166,6 @@ public static String getDefaultFileParent(Context context) {
}
return f.getAbsolutePath();
}

private static void initFilesTree(Context context) {
List<File> files = getUsableFolders(context);
String path = context.getSharedPreferences("Settings", Context.MODE_PRIVATE).getString(context.getString(R.string.key_save_path), getDefaultFileParent(context));
Expand All @@ -174,12 +175,14 @@ private static void initFilesTree(Context context) {
if (!files.contains(ROOTFOLDER) && !isExternalStorageManager())
ROOTFOLDER = new File(getDefaultFileParent(context));
MAINFOLDER = new File(ROOTFOLDER, MAINFOLDER_NAME);
LogUtility.d(MAINFOLDER);
OLD_GALLERYFOLDER = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), MAINFOLDER_NAME);
DOWNLOADFOLDER = new File(MAINFOLDER, DOWNLOADFOLDER_NAME);
SCREENFOLDER = new File(MAINFOLDER, SCREENFOLDER_NAME);
PDFFOLDER = new File(MAINFOLDER, PDFFOLDER_NAME);
UPDATEFOLDER = new File(MAINFOLDER, UPDATEFOLDER_NAME);
ZIPFOLDER = new File(MAINFOLDER, ZIPFOLDER_NAME);
TORRENTFOLDER = new File(MAINFOLDER, TORRENTFOLDER_NAME);
BACKUPFOLDER = new File(MAINFOLDER, BACKUPFOLDER_NAME);
}

Expand Down Expand Up @@ -551,6 +554,7 @@ public static void initStorage(Context context) {
Global.UPDATEFOLDER.mkdir(),
Global.SCREENFOLDER.mkdir(),
Global.ZIPFOLDER.mkdir(),
Global.TORRENTFOLDER.mkdir(),
Global.BACKUPFOLDER.mkdir(),
};
LogUtility.d(
Expand All @@ -561,6 +565,7 @@ public static void initStorage(Context context) {
"4:" + Global.UPDATEFOLDER + bools[3] + '\n' +
"5:" + Global.SCREENFOLDER + bools[4] + '\n' +
"5:" + Global.ZIPFOLDER + bools[5] + '\n' +
"5:" + Global.TORRENTFOLDER + bools[5] + '\n' +
"6:" + Global.BACKUPFOLDER + bools[6] + '\n'
);

Expand Down Expand Up @@ -714,9 +719,6 @@ public static File findGalleryFolder(Context context, int id) {

private static void updateConfigurationNightMode(AppCompatActivity activity, Configuration c) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
UiModeManager manager = (UiModeManager) activity.getSystemService(Context.UI_MODE_SERVICE);
if (manager != null) manager.setNightMode(UiModeManager.MODE_NIGHT_NO);

c.uiMode &= (~Configuration.UI_MODE_NIGHT_MASK);//clear night mode bits
c.uiMode |= Configuration.UI_MODE_NIGHT_NO; //disable night mode
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/com/dar/nclientv2/utility/Utility.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
Expand Down Expand Up @@ -123,6 +124,21 @@ private static void saveImage(@NonNull Bitmap bitmap, @NonNull File output) {
}
}

public static long writeStreamToFile(InputStream inputStream, File filePath) throws IOException {
FileOutputStream outputStream = new FileOutputStream(filePath);
int read;
long totalByte = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
totalByte += read;
}
outputStream.flush();
outputStream.close();
inputStream.close();
return totalByte;
}

public static void sendImage(Context context, Drawable drawable, String text) {
context = context.getApplicationContext();
try {
Expand Down
7 changes: 6 additions & 1 deletion app/src/main/res/menu/gallery.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
android:orderInCategory="400"
android:title="@string/download_gallery"
app:showAsAction="ifRoom|collapseActionView" />

<item
android:id="@+id/add_online_gallery"
android:icon="@drawable/ic_star"
Expand All @@ -43,7 +44,11 @@
android:orderInCategory="550"
android:title="@string/change_status"
app:showAsAction="never" />

<item
android:id="@+id/download_torrent"
android:orderInCategory="400"
android:title="@string/download_torrent"
app:showAsAction="never" />
<item
android:id="@+id/share"
android:icon="@drawable/ic_share"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/values-ar-rSA/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,5 @@
<string name="title_change_page_buttons">غيِّر الصفحة بالنقر على الصورة</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>

<string name="download_torrent">Download torrent</string>
</resources>
2 changes: 1 addition & 1 deletion app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,5 @@
<string name="title_change_page_buttons">Ändere Seite Klicke auf das Bild</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>

<string name="download_torrent">Download torrent</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,6 @@
<string name="title_change_page_buttons">Cambiar de página haciendo click en la imagen</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>
<string name="download_torrent">Download torrent</string>

</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values-it-rIT/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,6 @@
<string name="title_change_page_buttons">Cambia pagina cliccando sull\'immagine</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>
<string name="download_torrent">Download torrent</string>

</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values-ru-rRU/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@
<string name="title_change_page_buttons">Перелистывание страницы нажатием на изображение</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>

<string name="download_torrent">Download torrent</string>

<string-array name="scroll_type">
<item>Горизонтальный</item>
<item>Вертикальный</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-tr-rTR/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,5 @@
<string name="title_change_page_buttons">Resme tıklayarak sayfa değiştir</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>
<string name="download_torrent">Download torrent</string>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@
<string name="title_change_page_buttons">Change page clicking on the image</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>

<string name="download_torrent">Download torrent</string>

<string-array name="scroll_type">
<item>水平</item>
<item>垂直</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@
<string name="title_change_page_buttons">Change page clicking on the image</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>
<string name="download_torrent">Download torrent</string>


</resources>
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@
<string name="title_change_page_buttons">Change page clicking on the image</string>
<string name="export_finished">Export finished</string>
<string name="import_finished">Import finished</string>

<string name="download_torrent">Download torrent</string>

<string-array name="scroll_type">
<item>Horizontal</item>
<item>Vertical</item>
Expand Down

0 comments on commit d51956d

Please sign in to comment.