Skip to content

Instantly share code, notes, and snippets.

@Raf0707
Created September 7, 2023 16:25
Show Gist options
  • Save Raf0707/04b5e34da71b682ae91a1a93213e7425 to your computer and use it in GitHub Desktop.
Save Raf0707/04b5e34da71b682ae91a1a93213e7425 to your computer and use it in GitHub Desktop.
MyPlayer MainActivity
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.karumi.dexter.Dexter;
import com.karumi.dexter.PermissionToken;
import com.karumi.dexter.listener.PermissionDeniedResponse;
import com.karumi.dexter.listener.PermissionGrantedResponse;
import com.karumi.dexter.listener.PermissionRequest;
import com.karumi.dexter.listener.single.PermissionListener;
import java.io.File;
import java.lang.reflect.Array;
import java.util.ArrayList;
import raf.tabiin.soulplayer.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding b;
ListView listView;
String[] items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
b = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(b.getRoot());
listView = b.listViewSong;
runtimePermission();
}
public void runtimePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Dexter.withContext(this).withPermission(Manifest.permission.MANAGE_EXTERNAL_STORAGE)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
displaySongs();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
}
public ArrayList<File> findSong(File file) {
ArrayList<File> arrayList = new ArrayList<>();
File[] files = file.listFiles();
for (File singleFile : files) {
if (singleFile.isDirectory() && !singleFile.isHidden()) {
arrayList.addAll(findSong(singleFile));
} else if (singleFile.getName().endsWith(".mp3") || singleFile.getName().endsWith(".wav")) {
arrayList.add(singleFile);
}
}
return arrayList;
}
public void displaySongs() {
final ArrayList<File> mySongs = findSong(Environment.getExternalStorageDirectory());
items = new String[mySongs.size()];
for (int i = 0; i < mySongs.size(); ++i) {
items[i] = mySongs.get(i).getName().toString()
.replace(".mp3", "")
.replace(".wav", "");
}
/*ArrayAdapter<String> myAdapter =
new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);*/
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
}
class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int position) {
return items[position];
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View myView = getLayoutInflater().inflate(R.layout.list_item, b.getRoot());
TextView textSong = myView.findViewById(R.id.txtSongName);
textSong.setSelected(true);
textSong.setText(items[position]);
return myView;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment