added asyns downloading

This commit is contained in:
Vologhat
2022-07-10 16:37:31 +03:00
parent b7e1326489
commit 871695731c
4 changed files with 50 additions and 19 deletions
@@ -1,6 +1,8 @@
package ru.vtosters.lite.downloaders;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;
@@ -20,7 +22,7 @@ public class AudioDownloader {
return;
}
Log.v("VTosters Lite", "Downloading audio " + track.D);
downloadAudioAsync(track, Environment.DIRECTORY_MUSIC + File.pathSeparator + "test", new Callback() {
downloadAudioAsync(track, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC) + "/test", new Callback() {
@Override
public void onProgress(int progress) {
ToastUtils.a("Загружено " + progress + "%");
@@ -44,7 +46,8 @@ public class AudioDownloader {
}
private static void downloadAudioAsync(MusicTrack track, String path, Callback callback) {
var destTs = path + ".ts";
new M3UDownloader(track.D, new File(destTs), callback).execute();
File outDir = new File(path);
if (!outDir.exists()) outDir.mkdirs();
new M3UDownloader(track.D, outDir, callback).execute();
}
}
@@ -1,5 +1,6 @@
package ru.vtosters.lite.music;
import android.os.StrictMode;
import android.util.Log;
import java.io.File;
@@ -12,9 +13,16 @@ import java.util.List;
import javax.crypto.NoSuchPaddingException;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import ru.vtosters.lite.utils.StreamUtils;
public class M3UDownloader{
public class M3UDownloader {
private OkHttpClient mClient = new OkHttpClient();
private VKM3UParser mParser;
private final String mURL;
private final File mOutDir;
@@ -27,26 +35,44 @@ public class M3UDownloader{
}
public void execute() {
VKM3UParser parser = new VKM3UParser(mURL);
List<TransportStream> tses = parser.getTransportStreams();
mCallback.onProgress(5);
for(TransportStream ts : tses) {
String tsURL = parser.getBaseUrl() + ts.getName();
Log.d("M3UDownloader", "Downloading " + tsURL);
try {
var request = new Request.a()
.b(mURL)
.a();
mClient.a(request).a(new okhttp3.Callback() {
@Override
public void a(Call call, IOException e) {
}
@Override
public void a(Call call, Response response) throws IOException {
parse(response.a().g());
}
});
}
private void parse(String payload) {
try {
VKM3UParser parser = new VKM3UParser(payload);
List<TransportStream> tses = parser.getTransportStreams();
mCallback.onProgress(5);
for (TransportStream ts : tses) {
String tsURL = parser.getBaseUrl() + ts.getName();
Log.d("M3UDownloader", "Downloading " + tsURL);
InputStream is = StreamUtils.openStream(tsURL);
String key = StreamUtils.readAllLines(StreamUtils.openStream(ts.getKeyURL()));
byte[] content = TransportStream.METHOD_AES128.equals(ts.getMethod())
? StreamUtils.decodeStream(is, ts.getKeyURL())
? StreamUtils.decodeStream(is, key)
: StreamUtils.readAllBytes(is);
File tsDump = new File(mOutDir, ts.getName());
StreamUtils.writeToFile(tsDump, content);
mCallback.onSizeReceived((long) content.length * tses.size(), parser.getHeapSize());
mCallback.onProgress(10 + Math.round(80.0f * tses.indexOf(ts) / tses.size()));
} catch (IOException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException e) {
e.printStackTrace();
mCallback.onFailure();
return;
}
} catch (IOException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidAlgorithmParameterException | InvalidKeyException e) {
e.printStackTrace();
mCallback.onFailure();
return;
}
mCallback.onSuccess();
}
@@ -1,5 +1,7 @@
package ru.vtosters.lite.music;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
@@ -28,10 +28,9 @@ public class StreamUtils {
return readAllBytes(cip);
}
public static Cipher getCipher(String keyURL) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
String key = new String(readAllBytes(openStream(keyURL)));
public static Cipher getCipher(String key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Key keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
Key keySpec = new SecretKeySpec(key.getBytes(), "AES");
// bypassing ecb
byte[] iv = new byte[16];
Arrays.fill(iv, (byte) 0x0);
@@ -59,6 +58,7 @@ public class StreamUtils {
}
public static void writeToFile(File file, byte[] content) throws IOException {
if (!file.exists()) file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
}