Skip to content

Commit

Permalink
update(Twitter - Native Downloader): Now saves photos on `/sdcard/Pic…
Browse files Browse the repository at this point in the history
…tures/Twitter`, filenames now follow its index.
  • Loading branch information
crimera committed Jul 18, 2024
1 parent b44f715 commit e5b7919
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
21 changes: 18 additions & 3 deletions app/src/main/java/app/revanced/integrations/twitter/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
import android.app.DownloadManager;
import android.net.Uri;
import android.os.Build;
import app.revanced.integrations.twitter.Pref;
import android.os.Environment;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;

Expand Down Expand Up @@ -234,6 +232,17 @@ public static String[] addPref(String[] prefs, String pref) {
return bigger;
}

public static String getExtension(String filename) {
String extension = "";

int i = filename.lastIndexOf('.');
if (i > 0) {
extension = filename.substring(i+1);
}

return extension;
}

public static void downloadFile(String url, String filename) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setDescription("Downloading " + filename);
Expand All @@ -243,7 +252,13 @@ public static void downloadFile(String url, String filename) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Pref.getPublicFolder(), Pref.getVideoFolder(filename));

if (getExtension(filename).equals("jpg")) {
request.setDestinationInExternalPublicDir("Pictures", "Twitter/"+filename);
} else {
request.setDestinationInExternalPublicDir(Pref.getPublicFolder(), Pref.getVideoFolder(filename));
}

DownloadManager manager = (DownloadManager) ctx.getSystemService(Context.DOWNLOAD_SERVICE);
long downloadId = manager.enqueue(request);
ctx.registerReceiver(new BroadcastReceiver() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.widget.LinearLayout;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -46,33 +46,33 @@ public static String downloadString(){


private static String getExtension(String typ){
if(typ.equals("video/mp4")){
return "mp4";
switch (typ) {
case "video/mp4":
return "mp4";
case "video/webm":
return "webm";
case "application/x-mpegURL":
return "m3u8";
default:
return "jpg";
}
if(typ.equals("video/webm")){
return "webm";
}
if(typ.equals("application/x-mpegURL")){
return "m3u8";
}
return "jpg";
}

private static String getFilename(String postId,String username){
return username + "_" + String.valueOf(postId);
return username + "_" + postId;
}


private static ArrayList<HashMap> getMedia(Object yghObj){
ArrayList<HashMap> mediaData = new ArrayList<HashMap>();
ArrayList<HashMap> mediaData = new ArrayList<>();

Class<?> yghClazz = yghObj.getClass();
Class<?> superClass = yghClazz.getSuperclass();

if (superClass.isInstance(yghObj)) {
try {
Object superClassInstance = superClass.cast(yghObj);
List<?> list = null;
List<?> list;

if(isMapNotPresent("F_MEDIALIST")){
for (Field field : superClass.getDeclaredFields()) {
Expand Down Expand Up @@ -181,7 +181,7 @@ public static void downloader(Context ctx,Object t57){
HashMap<String,Object> cachedData = new HashMap<>();
String fileName = "";
String username = "";
ArrayList<HashMap> mediaData = new ArrayList<>();
ArrayList<HashMap> mediaData;

Class<?> clazz = t57.getClass();
clazz.cast(t57);
Expand Down Expand Up @@ -227,7 +227,7 @@ public static void downloader(Context ctx,Object t57){
fileName = getFilename(postId,username);

Method yghMethod = clazz.getDeclaredMethod(getMapping("M_MEDIAOBJ"));
Object yghObj = (Object) yghMethod.invoke(t57);
Object yghObj = yghMethod.invoke(t57);


mediaData = getMedia(yghObj);
Expand Down Expand Up @@ -266,26 +266,30 @@ private static void alertbox(Context ctx,String filename,ArrayList<HashMap> medi
String[] choices = new String[n];
for(int i=0;i<n;i++){
HashMap<String, String> hashMap = mediaData.get(i);
String typ = (String)hashMap.get("type");
choices[i] = "• "+typ+" "+String.valueOf(i+1);
String typ = hashMap.get("type");
choices[i] = "• "+typ+" "+ (i + 1);
}

builder.setItems(choices, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
ArrayList<HashMap> mData = new ArrayList<HashMap>();
HashMap<String,String> media = mediaData.get(which);
mData.add(media);
downloadMedia(filename,mData);
dialogInterface.dismiss();
}
builder.setItems(choices, (dialogInterface, which) -> {
ArrayList<HashMap> mData = new ArrayList<>();
HashMap<String,String> media = mediaData.get(which);
mData.add(media);
downloadMedia(filename+"_"+(which+1),mData);
dialogInterface.dismiss();
});
builder.setNegativeButton(strRes("piko_pref_native_downloader_download_all"),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int index) {
downloadMedia(filename,mediaData);
dialogInterface.dismiss();
(dialogInterface, index) -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
int i = 0;
for (HashMap ignored : mediaData) {
ArrayList<HashMap> mData = new ArrayList<>();
HashMap media = mediaData.get(i);
mData.add(media);
downloadMedia(filename+"_"+(++i),mData);
}

}
dialogInterface.dismiss();
});

builder.show();
Expand All @@ -299,10 +303,10 @@ private static void downloadMedia(String filename,ArrayList<HashMap> mediaData){
int n = mediaData.size();
for(int i=0;i<n;i++){
HashMap<String, String> media = mediaData.get(i);
String mediaUrl = (String)media.get("url");
String ext = (String)media.get("ext");
String mediaUrl = media.get("url");
String ext = media.get("ext");

String updFileName = filename+"_"+String.valueOf(i+1)+"."+ext;
String updFileName = filename+"."+ext;

Utils.downloadFile(mediaUrl,updFileName);
}
Expand Down

0 comments on commit e5b7919

Please sign in to comment.