0

I created an application that is used to download the files of a module that I have done! the application works well !. I'm trying to display the activity files that download from the FTP server in text view. But I am not able to display more than 3 messages. i create chronometer to, and when the apps download file, the chronometer STOP at 10 sec... and restart after downloaded all file at 2m30sec.... WHY ??? All message in start shift in IF condition is good... but all message in else doesnt work! I NEED HELP PLEASE ... I post a part of my code and what i need to do. Thx to all for reply!

Jira: We have created an application for drillers. This application connects to a module to download files. Once the day is complete, the driller performs a Stop Shift. We want to display a message to the driller when he starts and stop his shift. Action of StartShift 1017 - 1032 Action of StopShift 1035 - 1063

I created a textview to show at the user, the activity of the application. The name of the textView: textViewDuration.

1 - We need to display the download of files in this textview. At the line number 794 and 846 of the DashBoardActivity.java Like: "Downloading: nameOfFile"

2 - We need to display the upload files name in this textview. At line number 195, 265 and 329 of FtpMultiUploadTask.java Like: "Uploading: nameOfFile"

I do not know why, I am able to display only 3 messages. At line 1026 of DashBoardActivity ... textViewDuration.setText("Sending 'T' file"); and at line 1134 of DashBoardActivity ... textViewDuration.setText("Downloading all files ... Please Wait"); and reset to empty at line 1208 of DashBoardActivity ... textViewDuration.setText("");

Once the application shows this 3 messages, it does not display the others. All setText i do on activity download, FtpMultiUpload or on stop shift does not work. Line 1035, 1047, 1049, 1052, 1054, 1057 of DashBoardActivity. I need help to change text of TextView when the download and upload of file as going on.

private void startShift() {


    if (isToggleButtonChecked == (false)) {

        writeLog(DashBoardActivity.this, "Version Unidrill: 0.3.13");
        writeLog(DashBoardActivity.this, "Conn. Unibridge: " + UserPreferenceHelper.PREF_HOTSPOT);
        writeLog(DashBoardActivity.this, "SSID: " + UserPreferenceHelper.getInstance(DashBoardActivity.this).getString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SSID_NAME));
        writeLog(DashBoardActivity.this, "START SHIFT");
        //Toast.makeText(this , "Start Shift",Toast.LENGTH_LONG);
        mChronometer.setBase(SystemClock.elapsedRealtime());
        mChronometer.start();
        textViewDuration.setText("Sending 'T' file");
        indicateur.setVisibility(toggleButtonStart.VISIBLE);
        isToggleButtonChecked = true;
        toggleButtonStart.setText("STOP SHIFT");
        UserPreferenceHelper.getInstance(this).saveString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SHIFT_START, "true");

        uniDrillApplication.runTaskModeDEMO();

    } else {

/*
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                textViewDuration.setText("Downloading all files ... Please Wait");
            }
        });
*/
        ((TextView) findViewById(R.id.textViewDuration)).setText("STOP SHIFT");
        //textViewDuration.setText("Downloading all files ... Please Wait");
        UserPreferenceHelper.getInstance(this).saveString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SHIFT_START, "false");
        UserPreferenceHelper.getInstance(this).saveString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_IS_STOP_SHIFT, "0");
        uniDrillApplication.is_downloaded = "0";

        downloadFile();
        this.textViewDuration.setText("");
        SystemClock.sleep(5000);
        uniDrillApplication.stopTask();
        writeLog(DashBoardActivity.this, "STOP SHIFT");
        writeLog(DashBoardActivity.this, "CREATE PDF REPORT");
        textViewDuration.setText("Create Report PDF");
        createPDF();
        textViewDuration.setText("");
        SystemClock.sleep(5000);
        writeLog(DashBoardActivity.this, "PREPARING TO SEND MAIL");
        textViewDuration.setText("Waiting wifi with internet");
        sendMail();
        textViewDuration.setText("Send Mail");
        SystemClock.sleep(10000);
        showProgress();
        textViewDuration.setText("Sending file to FTP UNIDRAULIK");
        sendFileToServer();
        textViewDuration.setText("");
        indicateur.setVisibility(toggleButtonStart.INVISIBLE);
        isToggleButtonChecked = false;
        toggleButtonStart.setText("START SHIFT");
        mChronometer.stop();
        mChronometer.setBase(SystemClock.elapsedRealtime());



    }

}



 private Boolean download(Context ctx, String server, int portNumber, String user, String password){
    //this.textViewDuration.setText("Rendu dans le download");
    ((TextView) findViewById(R.id.textViewDuration)).setText("Debut du download!!!");
    showProgress();
    //toggleButtonStart.setText("Preparing to download file");
    boolean success =false;
    //dismissProgressDialog();
    final Pattern PATTERN = Pattern.compile("(.*?)(?:\\((\\d+)\\))?(\\.[^.]*)?");
    final Context _ctx = ctx;
    String _server = server;
    int _portNumber = portNumber;
    String _user = user;
    String _password = password;
    final String UNIBRIDGE_FILE_PATH = "/mnt/myDrive";
    String sdcardDestinationPath;
    FTPClient ftpClient = new FTPClient();
    List<String> _listFile = new ArrayList<>();
    sdcardDestinationPath = Environment.getExternalStorageDirectory()+"/"+ this.getApplicationContext().getPackageName();
    int TENSECONDS  = 10*1000;
    //showToast("DOWNLOADING ALL FILES! PLEASE WAIT ...");
    //Toast.makeText(this, "Downloading ALL files! Please Wait!", Toast.LENGTH_LONG).show();
    //if (toastTest != null){
    //    toastTest.cancel();
    //}
    //toastTest = Toast.makeText(this, "Preparing to download ALL files! Please Wait...", Toast.LENGTH_SHORT);
    //toastTest.show();
    try {
        ftpClient.setControlEncoding("UTF-8");
        ftpClient.setConnectTimeout(8000);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        //showCustomPopup(GenericDialog.GenericDialogType.NONE, "Connexion to FTP UNIBRIDGE", null, null, null);
        ftpClient.connect(_server, _portNumber);
        writeLog(this,"Connexion to ftp unibridge ...");

        boolean isConnected = ftpClient.login(_user, _password);
        //prg.setProgress(15);
        if (isConnected) {

            writeLog(this,"Connexion to ftp unibridge = OK");
            //showCustomPopup(GenericDialog.GenericDialogType.WARNING, "Connexion to FTP UNIBRIDGE = OK", null, null, null);
 /*
                //ALERTE AVEC TIMER A 2 SECONDES
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Connected to Unibridge FTP");
            builder.setMessage("OK");
            builder.setCancelable(true);
            final AlertDialog dlg = builder.create();
            dlg.show();
            final Timer t = new Timer();
            t.schedule(new TimerTask() {
                public void run() {
                    dlg.dismiss(); // when the task active then close the dialog
                    t.cancel(); // also just top the timer thread, otherwise, you may receive a crash report
                }
            }, 2000);
*/
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            ftpClient.printWorkingDirectory();
            ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
            //showServerReply(_ctx);
            FTPFile[] files = ftpClient.listFiles();
            //prg.setProgress(20);
            for (FTPFile file : files) {

                final String fileName = file.getName();

                //textViewDuration.setText("Checking file: " + fileName);


                //Context baseCtx = getBaseContext();
                //Context appCtx = getApplicationContext();
/*
                DashBoardActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), fileName, Toast.LENGTH_LONG).show();
                    }
                });
                */
                //Toast.makeText(getBaseContext(), fileName, Toast.LENGTH_LONG).show();
                /*
                DashBoardActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), fileName, Toast.LENGTH_LONG).show();
                    }
                });
                **/
                /*
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(DashBoardActivity.this, fileName, Toast.LENGTH_LONG).show();
                    }
                });
                */
                //Toast.makeText(this, file.getName(), Toast.LENGTH_LONG).show();
                ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
                boolean verifFile = false;
                String name = file.getName();
                if (file.isDirectory()) {
                    //TODO :  Vérifier pourquoi le fichier 700101 existe. Idéalement faire conditions contraire (if not en java)
                    if(!name.matches("\\d+(?:\\.\\d+)?") || (name.matches("700101"))) {
                        //return false;
                    }else{
                        writeLog(this, "current working directory : "+ftpClient.printWorkingDirectory());
                        writeLog(this, "change working directory to : " + name);
                        ftpClient.changeWorkingDirectory(name);
                        writeLog(this, "new working directory : "+ftpClient.printWorkingDirectory());

                        FTPFile[] unibridgeFiles = ftpClient.listFiles();

                        int i = 0;
                        for (FTPFile unibridgefile : unibridgeFiles) {
                            FTPFile[] fileDirectory = ftpClient.listFiles();
                            if(fileDirectory.length == 2){
                                writeLog(this, "Delete directory 1: "+ name);
                                writeLog(this, "___________________");
                                ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
                                boolean checkDelete = ftpClient.removeDirectory(name);
                            }
                            if (! unibridgefile.isDirectory()) {
                                //download
                                OutputStream outputStream = null;
                                try {
                                    writeLog(this, "LocalFilePath : " + sdcardDestinationPath);
                                    writeLog(this, "File : " + unibridgefile.getName());

                                    if(!".".equals(unibridgefile.getName()) && !"..".equals(unibridgefile.getName())){

                                        if (unibridgefile.getName().contains(".csv")){
                                            //String synchroFileName = "T"+ DateTools.getHours(this);
                                            String synchroFileName = DateTools.getDateToString(new Date(),FORMAT_DATE_SYNCHRO);
                                            System.out.println(synchroFileName);
                                            String hh = "-" + synchroFileName.substring(6,8);
                                            String date = synchroFileName.substring(0,6);
                                            String checkShift = UserPreferenceHelper.getInstance(this).getString(UserPreferenceHelper.getInstance(this).USER_ID + UserPreferenceHelper.PREF_SHIFT_START);

                                            if(checkShift.equals("true")){
                                                if (unibridgefile.getName().contains("D") && unibridgefile.getName().contains(date) && unibridgefile.getName().contains(hh)){

                                                }else if(unibridgefile.getName().contains("E") && unibridgefile.getName().contains(date)){

                                                }else if(unibridgefile.getName().contains("R") && unibridgefile.getName().contains(date)){

                                                }else{
                                                    //Toast.makeText(getApplicationContext(), "Downloading: 00000000" , Toast.LENGTH_LONG).show();
                                                    //SystemClock.sleep(2000);
                                                    File _file = new File(sdcardDestinationPath, unibridgefile.getName());
                                                    FileOutputStream destFileStream = new FileOutputStream(_file);
                                                    //if (toastTest != null){
                                                    //    toastTest.cancel();
                                                    //}
                                                    //toastTest = Toast.makeText(getBaseContext(), "Downloading: " + unibridgefile.getName(), Toast.LENGTH_SHORT);
                                                    //toastTest.show();

                                                    outputStream = new BufferedOutputStream(destFileStream);
                                                    /*
HERE WE NEED MESSAGE TO SHOW EACH FILE IS DOWNLOADING
 */
                                                    ftpClient.retrieveFile(unibridgefile.getName(), outputStream);
                                                    final String nameFile = unibridgefile.getName();




                                                    //Toast.makeText(getApplicationContext(),"Your message",Toast.LENGTH_LONG).show();


                                                    //toast.setText("Downloading: " + nameFile);
                                                    //toast.setDuration(Toast.LENGTH_LONG);
                                                    //toast.show();
/*
                                                    Runnable changeText = 
new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(DashBoardActivity.this, "Downloading: " + nameFile, Toast.LENGTH_SHORT).show();
                                                        }
                                                    };

runOnUiThread(changeText);
                                                    */
                                                    /*
                                                    final Handler handler= new Handler();
                                                    final Runnable r = new Runnable() {
                                                        public void run() {
                                                            Toast.makeText(DashBoardActivity.this, "Downloading: " + nameFile, Toast.LENGTH_SHORT).show();

 //handler.postDelayed(this, 1000);
                                                        }
                                                    };
                                                    handler.postDelayed(r, 5);
*/
                                                    /*
                                                    Handler handler = new Handler(Looper.getMainLooper());
                                                    handler.post(new Runnable() {
                                                        @Override
                                                        public void run() {
                                                            Toast.makeText(getApplicationContext(),"Downloading: " + nameFile,Toast.LENGTH_SHORT).show();
                                                        }
                                                    });
*/
                                                    boolean deletefile = ftpClient.deleteFile(unibridgefile.getName());


                                                }
                                            }else{
                                                File _file = new File(sdcardDestinationPath, unibridgefile.getName());
                                                FileOutputStream destFileStream = new FileOutputStream(_file);
                                                outputStream = new BufferedOutputStream(destFileStream);

                                                /*
HERE WE NEED MESSAGE TO SHOW EACH FILE IS DOWNLOADING
 */
                                                ftpClient.retrieveFile(unibridgefile.getName(), outputStream);





                                                //Handler handler = new Handler(Looper.getMainLooper());

                                                //handler.post(new Runnable() {

                                                //   @Override
                                                //    public void run() {
                                                //        Toast.makeText(DashBoardActivity.this.getApplicationContext(),"Downloading: tesssst",Toast.LENGTH_SHORT).show();
                                                //    }
                                                //});
                                                boolean deletefile = ftpClient.deleteFile(unibridgefile.getName());
                                                textViewDuration.setText("");
                                                /*
                                            _listFile.add(unibridgefile.getName());

                                            writeLog(_ctx,"Download file :" +  unibridgefile.getName() + " in progress.");

                                            String fileName = "";
                                            File _file = new File(sdcardDestinationPath, unibridgefile.getName());


                                            Matcher m = PATTERN.matcher(unibridgefile.getName());
                                            if (m.matches()) {
                                                String prefix = m.group(1);
                                                String last = m.group(2);
                                                String suffix = m.group(3);
                                                if (suffix == null) suffix = "";

                                                int count = 0;

                                                while(_file.exists()) {
                                                    count++;
                                                    fileName = prefix + "(" + count + ")" + suffix;
                                                    _file = new File(sdcardDestinationPath, fileName);
                                                    verifFile = true;
                                                }
                                            }

                                            FileOutputStream destFileStream = new FileOutputStream(_file);
                                            outputStream = new BufferedOutputStream(destFileStream);

                                            success = ftpClient.retrieveFile(unibridgefile.getName(), outputStream);
                                            if (verifFile){
                                                String line = "";
                                                BufferedReader br = new BufferedReader(new FileReader(sdcardDestinationPath + "/" + fileName));
                                                br.readLine();
                                                FileWriter fw = new FileWriter(sdcardDestinationPath + "/" + unibridgefile.getName(), true);
                                                while((line = br.readLine()) != null){
                                                    fw.append(line + "\n");
                                                }
                                                fw.flush();
                                                fw.close();
                                                br.close();
                                                _file.delete();


                                            }

                                            if(success){
                                                //writeLog(_ctx, unibridgefile.getName()+" downloaded to "+ sdcardDestinationPath);
                                                String deleteFilePath = ftpClient.printWorkingDirectory() + "/" + unibridgefile.getName();
                                                //boolean test = ftpClient.deleteFile(deleteFilePath);
                                                //System.out.println(test);

                                                boolean deletefile = ftpClient.deleteFile(unibridgefile.getName());
                                                FTPFile[] fileDirectory2 = ftpClient.listFiles();
                                                if(fileDirectory2.length == 2){
                                                    ftpClient.changeWorkingDirectory(UNIBRIDGE_FILE_PATH);
                                                    boolean checkDelete = ftpClient.removeDirectory(name);
                                                    writeLog(_ctx, "Delete directory: "+ name);
                                                    writeLog(_ctx, "____________________ ");
                                                }
                                            }else{
                                                writeLog(_ctx, unibridgefile.getName()+" not downloaded to "+ sdcardDestinationPath);
                                            }
                                            */
                                            }
                                        }
                                    }

                                } finally {
                                    if (outputStream != null) {
                                        outputStream.close();

                                    }
                                }

                            }
                        }
                        writeLog(this, "current working directory : "+ftpClient.printWorkingDirectory());
                        writeLog(this, "change working directory to parent ");
                        ftpClient.changeToParentDirectory();
                        writeLog(this, "new working directory : "+ftpClient.printWorkingDirectory());
                       /* if(success){
                            int test = ftpClient.dele(ftpClient.printWorkingDirectory());
                            System.out.println(test);
                            boolean delete = ftpClient.deleteFile(ftpClient.printWorkingDirectory());
                            System.out.println(delete);
                        }*/
                    }
                }
            }

            if (ftpClient.isConnected()){
                ftpClient.disconnect();
            }
            dismissProgressDialog();
            return success;
        } else {
            dismissProgressDialog();
            writeLog(this, "Connexion to ftp unibridge = KO");
            return false;
        }
    } catch (Exception e) {
        Log.d("FTP", e.toString());
        writeLog(this, "Exception ="+ e.toString());
        // return false;
        success =false;
    }finally {

        //if the IO is timed out or force disconnected, exceptions may be thrown when trying to logout/disconnect
        try
        {

            ftpClient.setSoTimeout(TENSECONDS);
            ftpClient.logout();

        }
        catch(Exception innerException)
        {

            success = false;

        }
        finally
        {

            try {
                ftpClient.disconnect();
            }catch (IOException ex){
                ex.printStackTrace();
            }

            success = false;
        }

    }
    textViewDuration.setText("");
    return success;

}

this is when i open app

When i press Start Shift... the textView change ... but the chrono stay to 0!

The chrono start at 0:05, and the text change... but i wan't show to user the name of file ... not a please wait :)

When all file is downloded, the text view dissapear ... and i press stop shift, but the text view never show again and the chrono stop

After all task are done, the chrono is reset to 0, and ready to start other shift!

Pic #1: this is when i open app Pic #2: When i press Start Shift... the textView change ... but the chrono stay to 0! Pic #3: The chrono start at 0:05, and the text change... but i wan't show to user the name of file ... not a please wait :) Pic #4: When all file is downloded, the text view dissapear ... and i press stop shift, but the text view never show again and the chrono stop Pic #5: After all task are done, the chrono is reset to 0, and ready to start other shift!

0 Answers0