导航菜单

页面标题

页面副标题

St.John's v1.0.9 - FilePickerActivity.java 源代码

正在查看: St.John's v1.0.9 应用的 FilePickerActivity.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.mcb.stjohnsemschool.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.core.view.MenuItemCompat;
import com.mcb.stjohnsemschool.adapter.FilesInStorageAdapter;
import com.mcb.stjohnsemschool.interfaces.SetSelectedFileCountInterface;
import com.mcb.stjohnsemschool.model.FileInStorageModel;
import com.mcb.stjohnsemschool.utils.FileUtils;
import com.mcb.stjohnsemschool.utils.TransparentProgressDialog;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;

public class FilePickerActivity extends AppCompatActivity implements SetSelectedFileCountInterface, SearchView.OnQueryTextListener {
    private Activity activity;
    private TextView mBrowse;
    private ListView mFiles;
    private TextView mNoData;
    private TextView mNoFilesSelected;
    private TransparentProgressDialog mProgressbar;
    private MenuItem saveMenuItem;
    private int selectedFilterPos;
    SetSelectedFileCountInterface setSelectedFileCountInterface;
    private ArrayList<FileInStorageModel> files = new ArrayList<>();
    private ArrayList<FileInStorageModel> filesDup = new ArrayList<>();
    private ArrayList<String> filePaths = new ArrayList<>();
    private boolean allowMultiple = false;
    private boolean isFromBrowse = false;

    public boolean onQueryTextSubmit(String str) {
        return false;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_file_picker);
        this.setSelectedFileCountInterface = this;
        this.activity = this;
        getSupportActionBar().setTitle("Select Files");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.mBrowse = (TextView) findViewById(R.id.txv_browse);
        this.mFiles = (ListView) findViewById(R.id.lst_files);
        this.mNoData = (TextView) findViewById(R.id.txv_no_data);
        this.mNoFilesSelected = (TextView) findViewById(R.id.txv_no_of_files_selected);
        this.mProgressbar = new TransparentProgressDialog(this, R.drawable.spinner_loading_imag);
        Bundle extras = getIntent().getExtras();
        this.filePaths = extras.getStringArrayList("AlreadySelectedFilePaths");
        this.allowMultiple = extras.getBoolean("ALLOW_MULTIPLE", false);
        setSaveButtonVisibility();
        this.mNoFilesSelected.setText(this.filePaths.size() + " Files Selected");
        this.mBrowse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent("android.intent.action.OPEN_DOCUMENT");
                intent.addCategory("android.intent.category.OPENABLE");
                intent.setType("*/*");
                intent.putExtra("android.intent.extra.ALLOW_MULTIPLE", FilePickerActivity.this.allowMultiple);
                FilePickerActivity.this.startActivityForResult(intent, 200);
            }
        });
        new GetFiles().execute(new String[0]);
    }

    public class GetFiles extends AsyncTask<String, String, String> {
        public GetFiles() {
        }

        @Override
        protected void onPreExecute() {
            FilePickerActivity.this.mProgressbar.show();
        }

        @Override
        public String doInBackground(String... strArr) {
            try {
                ArrayList allFiles = FilePickerActivity.this.getAllFiles();
                ArrayList allImages = FilePickerActivity.this.getAllImages();
                FilePickerActivity.this.files.clear();
                FilePickerActivity.this.files.addAll(allFiles);
                FilePickerActivity.this.files.addAll(allImages);
                Collections.sort(FilePickerActivity.this.files);
                FilePickerActivity.this.filesDup.clear();
                FilePickerActivity.this.filesDup.addAll(FilePickerActivity.this.files);
                return null;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        public void onPostExecute(String str) {
            if (FilePickerActivity.this.mProgressbar != null && FilePickerActivity.this.mProgressbar.isShowing()) {
                FilePickerActivity.this.mProgressbar.dismiss();
            }
            FilePickerActivity.this.addDataToView();
        }
    }

    public ArrayList<FileInStorageModel> getAllFiles() {
        ArrayList<FileInStorageModel> arrayList = new ArrayList<>();
        try {
            Cursor query = getContentResolver().query(MediaStore.Files.getContentUri("external"), new String[]{"_data", "date_added", "_size"}, "media_type!=1 AND media_type!=3", null, "date_added DESC");
            if (query != null) {
                while (query.moveToNext()) {
                    String string = query.getString(query.getColumnIndexOrThrow("_data"));
                    long j = query.getLong(query.getColumnIndexOrThrow("_size"));
                    if (string != null && j > 0) {
                        File file = new File(string);
                        String supportedPath = getSupportedPath(string);
                        if (supportedPath != null && supportedPath.length() > 0 && !file.isDirectory() && file.exists()) {
                            long j2 = query.getLong(query.getColumnIndexOrThrow("date_added"));
                            String substring = supportedPath.substring(supportedPath.lastIndexOf("/") + 1);
                            String substring2 = substring.substring(substring.lastIndexOf(".") + 1);
                            FileInStorageModel fileInStorageModel = new FileInStorageModel();
                            fileInStorageModel.setFilePath(supportedPath);
                            fileInStorageModel.setFileName(substring);
                            fileInStorageModel.setExtension(substring2);
                            fileInStorageModel.setDateTime(j2);
                            fileInStorageModel.setSize(j);
                            arrayList.add(fileInStorageModel);
                        }
                    }
                }
                query.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    private String getSupportedPath(String str) {
        String[] strArr = {".pdf", ".doc", ".docx", ".xls", ".xlsx", ".avi", ".mp4", ".m4p", ".m4v", ".wmv", ".mov", ".webm", ".mpg", ".mp2", ".mpeg", ".mpe", ".mpv", ".ogg", ".flv", ".mkv", ".epub", ".ppt", ".pptx"};
        for (int i = 0; i < 23; i++) {
            if (str.toLowerCase().endsWith(strArr[i])) {
                return str;
            }
        }
        return null;
    }

    public ArrayList<FileInStorageModel> getAllImages() {
        ArrayList<FileInStorageModel> arrayList = new ArrayList<>();
        try {
            Cursor query = getContentResolver().query(MediaStore.Files.getContentUri("external"), null, "media_type=1", null, "_id DESC");
            if (query != null) {
                while (query.moveToNext()) {
                    String string = query.getString(query.getColumnIndexOrThrow("_data"));
                    long j = query.getLong(query.getColumnIndexOrThrow("_size"));
                    if (string != null && j > 0) {
                        File file = new File(string);
                        if (!string.toLowerCase().endsWith("gif") && !file.isDirectory() && file.exists()) {
                            long j2 = query.getLong(query.getColumnIndexOrThrow("date_added"));
                            String substring = string.substring(string.lastIndexOf("/") + 1);
                            String substring2 = substring.substring(substring.lastIndexOf(".") + 1);
                            FileInStorageModel fileInStorageModel = new FileInStorageModel();
                            fileInStorageModel.setFilePath(string);
                            fileInStorageModel.setFileName(substring);
                            fileInStorageModel.setExtension(substring2);
                            fileInStorageModel.setDateTime(j2);
                            fileInStorageModel.setSize(j);
                            arrayList.add(fileInStorageModel);
                        }
                    }
                }
                query.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return arrayList;
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_search_save_sort, menu);
        this.saveMenuItem = menu.findItem(R.id.save_activity);
        setSaveButtonVisibility();
        MenuItemCompat.getActionView(menu.findItem(R.id.menu_search)).setOnQueryTextListener(this);
        return true;
    }

    private void setSaveButtonVisibility() {
        if (this.saveMenuItem != null) {
            if (this.filePaths.size() > 0) {
                this.saveMenuItem.setVisible(true);
            } else {
                this.saveMenuItem.setVisible(false);
            }
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        int itemId = menuItem.getItemId();
        if (itemId == 16908332) {
            onBackPressed();
        } else if (itemId == 2131361888) {
            onCreateDialogSingleChoice().show();
        } else if (itemId == 2131363540) {
            this.isFromBrowse = false;
            uploadSelectedFiles();
        }
        return super.onOptionsItemSelected(menuItem);
    }

    public Dialog onCreateDialogSingleChoice() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
        builder.setTitle("Sort By").setSingleChoiceItems(new CharSequence[]{"File Name", "Date Time"}, this.selectedFilterPos, (DialogInterface.OnClickListener) null).setPositiveButton("APPLY FILTER", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                FilePickerActivity.this.selectedFilterPos = ((AlertDialog) dialogInterface).getListView().getCheckedItemPosition();
                FilePickerActivity.this.sortFiles();
                dialogInterface.dismiss();
            }
        });
        return builder.create();
    }

    public void sortFiles() {
        int i = this.selectedFilterPos;
        if (i == 0) {
            Collections.sort(this.files);
        } else if (i == 1) {
            Collections.sort(this.files, new Comparator<FileInStorageModel>() {
                @Override
                public int compare(FileInStorageModel fileInStorageModel, FileInStorageModel fileInStorageModel2) {
                    if (fileInStorageModel.getDateTime() > fileInStorageModel2.getDateTime()) {
                        return -1;
                    }
                    return fileInStorageModel.getDateTime() < fileInStorageModel2.getDateTime() ? 1 : 0;
                }
            });
        }
        addDataToView();
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        String str;
        super.onActivityResult(i, i2, intent);
        if (i != 200 || intent == null) {
            return;
        }
        Uri data = intent.getData();
        String str2 = null;
        if (data != null) {
            this.filePaths.clear();
            try {
                str2 = FileUtils.saveFileIntoExternalStorageByUri(getApplicationContext(), data);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (str2 != null && str2.length() > 0) {
                this.filePaths.add(str2);
            }
        } else {
            ClipData clipData = intent.getClipData();
            if (clipData != null) {
                this.filePaths.clear();
                int itemCount = clipData.getItemCount();
                for (int i3 = 0; i3 < itemCount; i3++) {
                    try {
                        str = FileUtils.saveFileIntoExternalStorageByUri(getApplicationContext(), clipData.getItemAt(i3).getUri());
                    } catch (Exception e2) {
                        e2.printStackTrace();
                        str = null;
                    }
                    if (str != null && str.length() > 0) {
                        this.filePaths.add(str);
                    }
                }
            }
        }
        if (this.filePaths.size() > 0) {
            this.isFromBrowse = true;
            uploadSelectedFiles();
        }
    }

    private void uploadSelectedFiles() {
        String str;
        Iterator<String> it = this.filePaths.iterator();
        long j = 0;
        while (true) {
            if (!it.hasNext()) {
                str = "";
                break;
            }
            int parseInt = Integer.parseInt(String.valueOf(new File(it.next()).length() / 1024));
            if (parseInt > 10240) {
                str = this.allowMultiple ? "Each file size should not exceed 10 MB and Over all files size should not exceed 50 MB" : "File size should not exceed 10 MB";
            } else {
                j += parseInt;
            }
        }
        String str2 = (str.length() != 0 || j <= 51200) ? str : "Each file size should not exceed 10 MB and Over all files size should not exceed 50 MB";
        if (str2.length() > 0) {
            Iterator<String> it2 = this.filePaths.iterator();
            while (it2.hasNext()) {
                String next = it2.next();
                if (this.isFromBrowse && next != null && next.length() > 0) {
                    FileUtils.deleteFile(getApplicationContext(), next);
                }
            }
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Error");
            builder.setMessage(str2).setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.dismiss();
                }
            });
            builder.create().show();
            return;
        }
        Intent intent = new Intent();
        intent.putStringArrayListExtra("FilePaths", this.filePaths);
        intent.putExtra("IsFromBrowse", this.isFromBrowse);
        setResult(100, intent);
        finish();
    }

    private long getOverAllFileSize() {
        long j = 0;
        while (this.filePaths.iterator().hasNext()) {
            j += Integer.parseInt(String.valueOf(new File(r0.next()).length() / 1024));
        }
        return j;
    }

    public void setSelectedFileCountInterface(String str) {
        if (this.allowMultiple) {
            if (this.filePaths.contains(str)) {
                this.filePaths.remove(str);
            } else if (this.filePaths.size() < 5) {
                this.filePaths.add(str);
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Error");
                builder.setMessage("You can't select more than 5 files").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
                builder.create().show();
            }
        } else {
            this.filePaths.clear();
            this.filePaths.add(str);
        }
        setSaveButtonVisibility();
        this.mNoFilesSelected.setText(this.filePaths.size() + " Files Selected");
    }

    public boolean isFileSelected(String str) {
        return this.filePaths.contains(str);
    }

    public boolean onQueryTextChange(String str) {
        if (str.length() > 0) {
            ArrayList<FileInStorageModel> arrayList = new ArrayList<>();
            for (int i = 0; i < this.filesDup.size(); i++) {
                FileInStorageModel fileInStorageModel = this.filesDup.get(i);
                if (fileInStorageModel.getFileName().toLowerCase().contains(str.toLowerCase())) {
                    arrayList.add(fileInStorageModel);
                }
            }
            this.files = arrayList;
        } else {
            this.files.clear();
            this.files.addAll(this.filesDup);
        }
        addDataToView();
        return false;
    }

    public void addDataToView() {
        ListAdapter filesInStorageAdapter = new FilesInStorageAdapter(getApplicationContext(), this.files, this.setSelectedFileCountInterface);
        this.mFiles.setAdapter(filesInStorageAdapter);
        filesInStorageAdapter.notifyDataSetChanged();
        if (this.files.size() > 0) {
            this.mFiles.setVisibility(0);
            this.mNoData.setVisibility(8);
        } else {
            this.mFiles.setVisibility(8);
            this.mNoData.setVisibility(0);
        }
    }

    protected void onStop() {
        TransparentProgressDialog transparentProgressDialog = this.mProgressbar;
        if (transparentProgressDialog != null && transparentProgressDialog.isShowing()) {
            this.mProgressbar.dismiss();
        }
        super.onStop();
    }
}