Video downloader in settings
This commit is contained in:
@@ -1,16 +1,24 @@
|
||||
package ru.vtosters.lite.downloaders;
|
||||
|
||||
import static ru.vtosters.lite.net.Request.makeRequest;
|
||||
import static ru.vtosters.lite.utils.Globals.getString;
|
||||
import static ru.vtosters.lite.utils.Globals.getUserToken;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.DownloadManager;
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.vk.core.util.ToastUtils;
|
||||
import com.vk.dto.common.VideoFile;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -71,4 +79,30 @@ public class VideoDownloader{
|
||||
}));
|
||||
builder.show();
|
||||
}
|
||||
|
||||
public static void parseVideoLink(String url, Context ctx){
|
||||
String videoId = url.split("video")[1];
|
||||
String ownerId = videoId.split("_")[0];
|
||||
|
||||
final ProgressDialog progressDialog = new ProgressDialog(ctx);
|
||||
progressDialog.setMessage("Обработка видео");
|
||||
progressDialog.show();
|
||||
|
||||
makeRequest("https://" + "api.vk.com" + "/method/video.get?owner_id=" + ownerId + "&videos=" + videoId + "&v=5.99&access_token=" + getUserToken(),
|
||||
response -> {
|
||||
progressDialog.cancel();
|
||||
|
||||
try {
|
||||
JSONObject mainJson = new JSONObject(response);
|
||||
JSONObject responseJson = mainJson.getJSONObject("response");
|
||||
JSONArray itemsJson = responseJson.getJSONArray("items");
|
||||
|
||||
VideoFile videoFile = new VideoFile(itemsJson.getJSONObject(0));
|
||||
downloadVideo(videoFile, ctx);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(ctx, "Видео нельзя скачать. Ошибка: " + e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,14 +2,50 @@ package ru.vtosters.lite.ui.fragments;
|
||||
|
||||
import static ru.vtosters.lite.utils.Globals.getIdentifier;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.widget.EditText;
|
||||
import android.widget.LinearLayout;
|
||||
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.preference.Preference;
|
||||
|
||||
import com.vtosters.lite.general.fragments.MaterialPreferenceToolbarFragment;
|
||||
|
||||
import ru.vtosters.lite.downloaders.VideoDownloader;
|
||||
|
||||
public class MediaFragment extends MaterialPreferenceToolbarFragment{
|
||||
public static void download(Context ctx){
|
||||
AlertDialog.Builder alertDialog = new AlertDialog.Builder(ctx);
|
||||
alertDialog.setTitle("Введите ссылку на видео");
|
||||
|
||||
final EditText input = new EditText(ctx);
|
||||
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
|
||||
LinearLayout.LayoutParams.MATCH_PARENT,
|
||||
LinearLayout.LayoutParams.MATCH_PARENT);
|
||||
input.setLayoutParams(lp);
|
||||
alertDialog.setView(input);
|
||||
|
||||
alertDialog.setPositiveButton("Скачать", (dialog, which) -> VideoDownloader.parseVideoLink(input.getText().toString(), ctx));
|
||||
alertDialog.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle bundle){
|
||||
super.onCreate(bundle);
|
||||
addPreferencesFromResource(getIdentifier("preferences_media", "xml"));
|
||||
prefs();
|
||||
}
|
||||
|
||||
private void prefs(){
|
||||
findPreference("download_video").setOnPreferenceClickListener(new MediaFragment.download());
|
||||
}
|
||||
|
||||
public class download implements Preference.OnPreferenceClickListener{
|
||||
@Override // android.support.v7.preference.Preference.c
|
||||
public boolean onPreferenceClick(Preference preference){
|
||||
download(getActivity());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -211,10 +211,6 @@ public class Preferences{
|
||||
return getBoolValue("postsredesign", true);
|
||||
}
|
||||
|
||||
public static boolean quic(){
|
||||
return getBoolValue("quic", true);
|
||||
}
|
||||
|
||||
public static boolean returnnorifs(){
|
||||
return getBoolValue("returnnorifs", false);
|
||||
}
|
||||
|
||||
@@ -49,6 +49,9 @@
|
||||
android:title="Новый раздел музыки (beta)" />
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory android:title="Видео">
|
||||
<Preference
|
||||
android:key="download_video"
|
||||
android:title="Скачать видео" />
|
||||
<com.vtosters.lite.ui.MaterialSwitchPreference
|
||||
android:defaultValue="false"
|
||||
android:key="isEnableExternalOpening"
|
||||
@@ -59,9 +62,6 @@
|
||||
android:key="__dbg_disable_video_feed"
|
||||
android:summary="@string/interfacevideorecsum"
|
||||
android:title="@string/interfacevideorec" />
|
||||
<Preference
|
||||
android:key="download_video"
|
||||
android:title="Скачать видео" />
|
||||
</PreferenceCategory>
|
||||
<PreferenceCategory android:title="@string/vkx_title">
|
||||
<Preference
|
||||
|
||||
Reference in New Issue
Block a user