Langage : Java
Posté le
Télécharger | Reposter
package com.armdev.DemoPlayer.adapter; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.PendingIntent; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.IntentSender; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.armdev.DemoPlayer.VideoModel; import com.armdev.DemoPlayer.activity.VideoPlayer; import com.armdev.advid.R; import com.bumptech.glide.Glide; import com.google.android.material.bottomsheet.BottomSheetDialog; import com.google.android.material.snackbar.Snackbar; import java.io.File; import java.util.ArrayList; public class VideosAdapter extends RecyclerView.Adapter<VideosAdapter.MyHolder> { public static ArrayList<VideoModel> videoFolder = new ArrayList<>(); private final Context context; public VideosAdapter(ArrayList<VideoModel> videoFolder, Context context) { VideosAdapter.videoFolder = videoFolder; this.context = context; } @NonNull @Override public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(context).inflate(R.layout.files_view, parent, false); return new MyHolder(view); } @Override public void onBindViewHolder(@NonNull VideosAdapter.MyHolder holder, int position) { Glide.with(context).load(videoFolder.get(position).getPath()).into(holder.thumbnail); holder.title.setText(videoFolder.get(position).getTitle()); holder.duration.setText(videoFolder.get(position).getDuration()); holder.size.setText(videoFolder.get(position).getSize()); holder.quality.setText(videoFolder.get(position).getResolution()); holder.menu.setOnClickListener(v -> { BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(context,R.style.BottomSheetDialogTheme); View bottomSheetView = LayoutInflater.from(context).inflate(R.layout.file_menu, null); bottomSheetDialog.setContentView(bottomSheetView); bottomSheetDialog.show(); bottomSheetView.findViewById(R.id.menu_down).setOnClickListener(v1 -> bottomSheetDialog.dismiss()); bottomSheetView.findViewById(R.id.menu_share).setOnClickListener(v2 -> { bottomSheetDialog.dismiss(); shareFile(position); }); bottomSheetView.findViewById(R.id.menu_rename).setOnClickListener(v3 -> { renameFile(position, v); bottomSheetDialog.dismiss(); }); bottomSheetView.findViewById(R.id.menu_properties).setOnClickListener(v4 -> { showProperties(position); bottomSheetDialog.dismiss(); }); bottomSheetView.findViewById(R.id.menu_delete).setOnClickListener(v5 -> { deleteFile(position, v); bottomSheetDialog.dismiss(); }); }); holder.itemView.setOnClickListener(v -> { Intent intent = new Intent(context, VideoPlayer.class); intent.putExtra("p", position); context.startActivity(intent); }); } private void shareFile(int p){ Uri uri = Uri.parse(videoFolder.get(p).getPath()); Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("video/*"); intent.putExtra(Intent.EXTRA_STREAM, uri); context.startActivity(Intent.createChooser(intent,"share")); } //@RequiresApi(api = Build.VERSION_CODES.R) private void deleteFile(int p, View view){ AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("Supprimer ?").setMessage(videoFolder.get(p).getTitle()).setNegativeButton("Annuler", (dialog, which) -> { }).setPositiveButton("Oui", (dialog, which) -> { ArrayList<Uri> fichiers = new ArrayList<>(); Uri contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Long.parseLong(videoFolder.get(p).getId())); File file = new File(videoFolder.get(p).getPath()); fichiers.add(contentUri); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { PendingIntent demande = MediaStore.createDeleteRequest(context.getContentResolver(), fichiers); try { ((Activity) context).startIntentSenderForResult(demande.getIntentSender(),1,new Intent(), 0,0,0); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } }else{ boolean deleted = file.delete(); if (deleted){ context.getApplicationContext().getContentResolver().delete(contentUri, null, null); videoFolder.remove(p); notifyItemRemoved(p); notifyItemRangeChanged(p,videoFolder.size()); Snackbar.make(view, "Fichier supprimé avec succès", Snackbar.LENGTH_SHORT).show(); }else{ Snackbar.make(view, "Erreur, le fichier n'a pas été supprimé", Snackbar.LENGTH_SHORT).show(); } } }).show(); } private void renameFile(int position, View view){ final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.rename_layout); final EditText editText = dialog.findViewById(R.id.rename_edit_text); Button cancel = dialog.findViewById(R.id.cancel_rename_button); Button rename_btn = dialog.findViewById(R.id.rename_button); final File renameFile = new File(videoFolder.get(position).getPath()); String nameText = renameFile.getName(); nameText = nameText.substring(0, nameText.lastIndexOf(".")); editText.setText(nameText); editText.clearFocus(); dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); cancel.setOnClickListener(v -> dialog.dismiss()); rename_btn.setOnClickListener(v -> { String onlyPath = renameFile.getParentFile().getAbsolutePath(); String ext = renameFile.getAbsolutePath(); ext = ext.substring(ext.lastIndexOf(".")); String newPath = onlyPath + "/" + editText.getText() + ext; File newFile = new File(newPath); ArrayList<Uri> fichiers = new ArrayList<>(); Uri contentUri = ContentUris.withAppendedId(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, Long.parseLong(videoFolder.get(position).getId())); fichiers.add(contentUri); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { PendingIntent demande = MediaStore.createWriteRequest(context.getContentResolver(), fichiers); try { ((Activity) context).startIntentSenderForResult(demande.getIntentSender(),2,new Intent(), 0,0,0); } catch (IntentSender.SendIntentException e) { e.printStackTrace(); } }else{ boolean rename = renameFile.renameTo(newFile); if(rename){ context.getApplicationContext().getContentResolver().delete(MediaStore.Files.getContentUri("external"), MediaStore.MediaColumns.DATA + "=?", new String[]{renameFile.getAbsolutePath()}); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(newFile)); context.getApplicationContext().sendBroadcast(intent); Snackbar.make(view, "Le fichier a bien été renommé", Snackbar.LENGTH_SHORT).show(); }else { Snackbar.make(view, "Erreur, le fichier n'a pas été renommé", Snackbar.LENGTH_SHORT).show(); } } dialog.dismiss(); }); dialog.show(); } private void showProperties(int p){ Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.file_properties); String name = videoFolder.get(p).getTitle(); String path = videoFolder.get(p).getPath(); String size = videoFolder.get(p).getSize(); String duration = videoFolder.get(p).getDuration(); String resolution = videoFolder.get(p).getResolution(); TextView tit = dialog.findViewById(R.id.pro_title); TextView st = dialog.findViewById(R.id.pro_storage); TextView siz = dialog.findViewById(R.id.pro_size); TextView dur = dialog.findViewById(R.id.pro_duration); TextView res = dialog.findViewById(R.id.pro_resolution); tit.setText(name); st.setText(path); siz.setText(size); dur.setText(duration); res.setText(resolution+"p"); dialog.show(); } @Override public int getItemCount() { return videoFolder.size(); } public void updateSearchList(ArrayList<VideoModel> searchList) { videoFolder = new ArrayList<>(); videoFolder.addAll(searchList); notifyDataSetChanged(); } public class MyHolder extends RecyclerView.ViewHolder{ ImageView thumbnail, menu; TextView title, size, duration, quality; public MyHolder(@NonNull View itemView) { super(itemView); thumbnail = itemView.findViewById(R.id.thumbnail); title = itemView.findViewById(R.id.video_title); size = itemView.findViewById(R.id.video_size); duration = itemView.findViewById(R.id.video_duration); quality = itemView.findViewById(R.id.video_quality); menu = itemView.findViewById(R.id.video_menu); } } }
x
Éditer le texte

Merci d'entrer le mot de passe que vous avez indiqué à la création du texte.

x
Télécharger le texte

Merci de choisir le format du fichier à télécharger.