I have limited the storage of firebase, to only allow authenticated users to read and write from Firebase console. All my project requests are working except the download page of PDF files. I'm suspecting i didn't add the authentication listener in the correct way.
This is my code for my Adapter and my Activity:
Download Activity:
public class pdfDownloader extends AppCompatActivity {
FirebaseFirestore db;
FirebaseAuth mAuth;
FirebaseAuth.AuthStateListener mAuthListner;
FirebaseUser mUser;
RecyclerView mRecyclerView;
ArrayList<DownModel> downModelArrayList = new ArrayList<>();
MyPdfAdapter myAdapter;
public static final String TAG="LOGIN";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_downloader);
mAuth = FirebaseAuth.getInstance();
mUser = FirebaseAuth.getInstance().getCurrentUser();
mAuthListner = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(mUser != null) {
dataFromFirebase();
} else {
Log.d(TAG,"AuthStateChanged:Logout");
}
}
};
setUpRV();
setUpFB();
dataFromFirebase();
}
protected void onStart() { // allowing users to see the links
super.onStart();
if(mUser == null) {
dataFromFirebase();
} else {
Log.d(TAG,"AuthStateChanged:Logout");
}
}
private void dataFromFirebase() {
if(downModelArrayList.size() > 0)
downModelArrayList.clear();
db.collection("files")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
for(DocumentSnapshot documentSnapshot: Objects.requireNonNull(task.getResult())) {
DownModel downModel= new DownModel(documentSnapshot.getString("name"),
documentSnapshot.getString("link"));
downModelArrayList.add(downModel);
}
myAdapter= new MyPdfAdapter(pdfDownloader.this,downModelArrayList);
mRecyclerView.setAdapter(myAdapter);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(pdfDownloader.this, "Error ;-.-;", Toast.LENGTH_SHORT).show();
}
});
}
private void setUpFB(){
db = FirebaseFirestore.getInstance();
}
private void setUpRV(){
mRecyclerView= findViewById(R.id.recycle);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
}
}
Download Adapter:
public class MyPdfAdapter extends RecyclerView.Adapter<MyViewHolder> {
pdfDownloader pdfDownloader;
ArrayList<DownModel> downModels;
FirebaseFirestore db;
public MyPdfAdapter(pdfDownloader pdfDownloader, ArrayList<DownModel> downModels) {
this.pdfDownloader = pdfDownloader;
this.downModels = downModels;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater layoutInflater = LayoutInflater.from(pdfDownloader.getBaseContext());
View view = layoutInflater.inflate(R.layout.elements, null, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
myViewHolder.mName.setText(downModels.get(i).getName());
myViewHolder.mLink.setText(downModels.get(i).getLink());
myViewHolder.deletePdfBtn.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
deletepdf();
}
});
myViewHolder.mDownload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
downloadFile(myViewHolder.mName.getContext(),downModels.get(i).getName(), ".pdf", DIRECTORY_DOWNLOADS,downModels.get(i).getLink());
}
});
}
private void deletepdf() {
db.collection( "files" ).document()
.delete()
.addOnSuccessListener( new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
} );
}
public void downloadFile(Context context, String fileName, String fileExtension, String destinationDirectory, String url) {
DownloadManager downloadmanager = (DownloadManager) context.
getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
downloadmanager.enqueue(request);
}
@Override
public int getItemCount() {
return downModels.size();
}
}