Commit e12e5bf8 authored by Ravishani W.M.S's avatar Ravishani W.M.S

Upload New File

parent 6334b47e
package com.example.smartpillow;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.ContentProviderClient;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
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.util.ArrayList;
import java.util.Enumeration;
public class MusicTherapyF extends AppCompatActivity {
ListView listView;
String[] items;
Button webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_music_therapy_f);
listView = findViewById(R.id.listViewSong);
webView = findViewById(R.id.youtubelog);
webView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MusicTherapyF.this, MusicTherapyT.class);
startActivity(intent);
}
});
runTimePermission();
}
public void runTimePermission()
{
Dexter.withContext(this).withPermission(Manifest.permission.READ_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();
if (files != null) {
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;
}
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<String>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(myAdapter);*/
customAdapter customAdapter = new customAdapter();
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String songName = (String) listView.getItemAtPosition(i);
startActivity(new Intent(getApplicationContext(), MusicTherapyS.class)
.putExtra("songs", mySongs)
.putExtra("songname", songName)
.putExtra("pos", i));
}
});
}
class customAdapter extends BaseAdapter
{
@Override
public int getCount() {
return items.length;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View myView = getLayoutInflater().inflate(R.layout.list_item,null);
TextView textSong = myView.findViewById(R.id.txtsongname);
textSong.setSelected(true);
textSong.setText(items[i]);
return myView;
}
}
@Override
public void onBackPressed() {
Intent intent = new Intent(MusicTherapyF.this, MainMenu.class);
startActivity(intent);
finish();
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment