androhashcheck.MainFrame.java Source code

Java tutorial

Introduction

Here is the source code for androhashcheck.MainFrame.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package androhashcheck;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.ListModel;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;

/**
 *
 * @author Azzaro
 */
public class MainFrame extends JFrame {

    private List<TaskObject> taskList = new ArrayList<>();
    private final DefaultListModel listModel = new DefaultListModel();
    private final SimpleDateFormat dateFormat;
    private final JFrame logInFrame;
    private boolean shouldCheckTasks;

    /**
     * Creates new form MainFrame
     * @param logInFrame
     */
    public MainFrame(JFrame logInFrame) {
        initComponents();
        this.logInFrame = logInFrame;
        jList5.setModel(listModel);
        dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        displayApkFolder();
        //mouse listner - when clicked on uploaded package to show details
        jList3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent evt) {
                JList list = (JList) evt.getSource();
                if (evt.getClickCount() == 2) {
                    int index = list.locationToIndex(evt.getPoint());
                    final TaskObject taskObject = taskList.get(index);
                    java.awt.EventQueue.invokeLater(new Runnable() {
                        @Override
                        public void run() {
                            new TaskFrame(taskObject).setVisible(true);
                        }
                    });
                }
            }
        });
        shouldCheckTasks = true;
        startThreadForTaskUpdatesChecking();
    }

    /**
     * Updating status bar (jList5 model) with now Date and given String.
     * @param newActivity String
     */
    private void updateStatus(String newActivity) {
        String fullStringActivity = dateFormat.format(new Date()) + "    " + newActivity;
        listModel.addElement(fullStringActivity);
    }

    /**
     * Starting thread that will check for data (information) for tasks that are unproccessed.
     */
    private void startThreadForTaskUpdatesChecking() {
        new Thread(new Runnable() {

            @Override
            public void run() {
                while (true) {
                    try {
                        if (!shouldCheckTasks) {
                            continue;
                        }
                        shouldCheckTasks = false;
                        getUserTasks();
                        Thread.sleep(5000);
                        String tasks[] = new String[taskList.size()];
                        for (int i = 0; i < taskList.size(); i++) {
                            TaskObject currentTask = taskList.get(i);
                            tasks[i] = currentTask.getFileName();

                            if (currentTask != null && currentTask.getStatus() != null) {
                                tasks[i] = tasks[i] + "    " + currentTask.getStatus();
                                if (currentTask.getStatus().equalsIgnoreCase("unprocessed")) {
                                    shouldCheckTasks = true;
                                }
                            }
                        }
                        if (!taskList.isEmpty()) {
                            jList3.setListData(tasks);
                        }

                    } catch (InterruptedException ex) {
                        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }).start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        textField1 = new java.awt.TextField();
        jButton5 = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jList3 = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        pullPackagesButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        loadPackButton = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jScrollPane5 = new javax.swing.JScrollPane();
        jList5 = new javax.swing.JList();
        jButton6 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setBackground(new java.awt.Color(0, 153, 204));
        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel2.setText("Android Hash-Checker");
        jLabel2.setOpaque(true);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        jButton2.setText("Submit From File");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Submit Selected");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        textField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textField1ActionPerformed(evt);
            }
        });

        jButton5.setText("From File");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(jList3);

        jLabel3.setBackground(new java.awt.Color(0, 152, 204));
        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("Uploaded files");
        jLabel3.setOpaque(true);

        jLabel4.setBackground(new java.awt.Color(0, 152, 204));
        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel4.setText("Submit selected or choose from file");
        jLabel4.setOpaque(true);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(jPanel1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addGroup(jPanel1Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        398, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jButton5))
                                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 140,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 140,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 75, Short.MAX_VALUE)))
                        .addContainerGap()));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addGap(22, 22, 22)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(textField1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton5))
                        .addGap(20, 20, 20).addComponent(jButton2).addGap(42, 42, 42)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 193, Short.MAX_VALUE)
                        .addGap(14, 14, 14)));

        jPanel2.setBackground(new java.awt.Color(204, 204, 204));

        jScrollPane1.setViewportView(jList1);

        pullPackagesButton.setBackground(new java.awt.Color(0, 152, 204));
        pullPackagesButton.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        pullPackagesButton.setText(">");
        pullPackagesButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pullPackagesButtonActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(jList2);

        loadPackButton.setBackground(new java.awt.Color(0, 152, 204));
        loadPackButton.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        loadPackButton.setText("Load Packages");
        loadPackButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadPackButtonActionPerformed(evt);
            }
        });

        jButton3.setBackground(new java.awt.Color(0, 152, 204));
        jButton3.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jButton3.setText("Select All");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setBackground(new java.awt.Color(0, 152, 204));
        jButton4.setFont(new java.awt.Font("Lucida Grande", 1, 13)); // NOI18N
        jButton4.setText("Select All");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(0, 152, 204));
        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Get packages from emulator/device using adb");
        jLabel1.setOpaque(true);

        jLabel6.setBackground(new java.awt.Color(0, 152, 204));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setText("Packages on device");
        jLabel6.setOpaque(true);

        jLabel7.setBackground(new java.awt.Color(0, 152, 204));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel7.setText("Pulled packages on PC");
        jLabel7.setOpaque(true);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap().addGroup(jPanel2Layout
                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(jPanel2Layout.createSequentialGroup().addGroup(
                                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(jPanel2Layout.createSequentialGroup().addGroup(jPanel2Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                .addComponent(loadPackButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        237, Short.MAX_VALUE)
                                                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        0, Short.MAX_VALUE))
                                                .addGap(8, 8, 8).addComponent(pullPackagesButton).addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                                        .addGroup(jPanel2Layout.createSequentialGroup()
                                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addGap(89, 89, 89)))
                                .addGroup(jPanel2Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0,
                                                Short.MAX_VALUE)
                                        .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel7, javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE))))
                        .addGap(26, 26, 26)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6).addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jScrollPane2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jButton4))
                                .addComponent(pullPackagesButton, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel2Layout.createSequentialGroup().addComponent(jScrollPane1)
                                        .addGap(18, 18, 18).addComponent(loadPackButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton3)))));

        jLabel5.setBackground(new java.awt.Color(0, 152, 204));
        jLabel5.setFont(new java.awt.Font("Lucida Grande", 0, 16)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel5.setText("Status");
        jLabel5.setOpaque(true);

        jScrollPane5.setViewportView(jList5);

        jButton6.setBackground(new java.awt.Color(51, 102, 255));
        jButton6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/androhashcheck/res/logout_icon.png"))); // NOI18N
        jButton6.setToolTipText("");
        jButton6.setActionCommand("Log Out");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(10, 10, 10).addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jScrollPane5)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 45,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 43,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 43,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 23,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                        .addContainerGap()));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private String[] fileToName(File[] files) {
        String[] strings = new String[files.length];
        for (int i = 0; i < files.length; i++) {
            strings[i] = files[i].getName();
        }
        return strings;
    }

    /**
     * For given String executing adb command.
     *
     * @param command String as adb command.
     * @return Displaying result as String.
     */
    @SuppressWarnings("CallToPrintStackTrace")
    private String executeCommand(String command) {
        StringBuilder output = new StringBuilder();

        Process p;
        try {
            p = Runtime.getRuntime().exec(command);
            //p.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null) {

                output.append(line).append("\n");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return output.toString();
    }

    /**
     * Opens file expolorer to choose file to be uploaded.
     *
     * @param evt
     */
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(new File(System.getProperty("user.home")));
        int result = fileChooser.showOpenDialog(this);
        if (result == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fileChooser.getSelectedFile();
            System.out.println("Selected file: " + selectedFile.getAbsolutePath());
            this.textField1.setText(selectedFile.getAbsolutePath());
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void textField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textField1ActionPerformed
    }//GEN-LAST:event_textField1ActionPerformed

    /**
     * Pressing on button will upload to server every selected apk on pc (from
     * list).s
     *
     * @param evt
     */
    @SuppressWarnings("ThrowableResultIgnored")
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new Thread(new Runnable() {

            @Override
            public void run() {
                for (String fileName : (List<String>) jList2.getSelectedValuesList()) {
                    updateStatus(fileName + "is being uploaded to server");
                    upoloadFile("apk/" + fileName);
                }
            }
        }).start();

    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * Upolads file on server for given path as a String.
     *
     * @param fileName
     */
    public void upoloadFile(String fileName) {
        try {
            String url = ConfigClass.serverURL + "/api/upload_task";
            URL obj = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");

            FileBody fileBody = new FileBody(new File(fileName));
            MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.STRICT);
            multipartEntity.addPart("file", fileBody);

            connection.setRequestProperty("Content-Type", multipartEntity.getContentType().getValue());
            connection.setRequestProperty("token", ConfigClass.token);
            try (OutputStream out = connection.getOutputStream()) {
                multipartEntity.writeTo(out);
            }
            int status = connection.getResponseCode();
            System.out.println(status);
            System.out.println(connection.getResponseMessage());

            StringBuilder response;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                String inputLine;
                response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
            }
            updateStatus(fileName + " done uploading, server response: " + response);
            if (status == 200) {
                shouldCheckTasks = true;
                String taskId = response.toString().replace("}", "").split(":")[1].trim();
                TaskObject newTask = new TaskObject();
                newTask.setFileName(fileName);
                newTask.setTaskId(taskId);
                taskList.add(newTask);
            }
        } catch (Exception ex) {
            updateStatus("error with uploading " + fileName);
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void getUserTasks() {
        updateStatus("getting user tasks");
        new Thread(new Runnable() {

            @Override
            public void run() {
                String url = ConfigClass.serverURL + "/api/tasks";
                try {
                    URL obj = new URL(url);
                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                    con.setRequestMethod("GET");
                    con.setRequestProperty("token", ConfigClass.token);
                    int responseCode = con.getResponseCode();
                    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String inputLine;
                    StringBuilder response = new StringBuilder();

                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
                    //print result
                    System.out.println(response.toString());
                    Gson gson = new Gson();
                    Map<String, Task[]> map = new Gson().fromJson(response.toString(),
                            new TypeToken<HashMap<String, Task[]>>() {
                            }.getType());
                    Task[] tasks = map.get("tasks");
                    taskList = new ArrayList<>();
                    for (Task task : tasks) {
                        TaskObject taskObject = new TaskObject();
                        taskObject.setFileName(task.getApk_name());
                        taskObject.setTask(task);
                        taskList.add(taskObject);
                    }
                    updateStatus("updated user tasks");
                } catch (IOException | JsonSyntaxException ignorable) {
                    listModel.addElement("unable to get user tasks");
                }
            }
        }).start();
    }

    /**
     * Submit from file button. File that is selected from file explorer will be
     * upoladed to server.s
     *
     * @param evt
     */
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        new Thread(new Runnable() {
            @Override
            public void run() {
                upoloadFile(textField1.getText());
            }
        }).start();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * Select every apk on pc to be ready to be uploaded.
     *
     * @param evt
     */
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        int end = jList2.getModel().getSize() - 1;
        if (end >= 0) {
            jList2.setSelectionInterval(0, end);
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * Select all packgaes on device/emulator to be pull to pc.
     *
     * @param evt
     */
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        int end = jList1.getModel().getSize() - 1;
        if (end >= 0) {
            jList1.setSelectionInterval(0, end);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * Pressing on button will display all packages on device/emulator.
     *
     * @param evt
     */
    private void loadPackButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadPackButtonActionPerformed
        new Thread(new Runnable() {

            @Override
            public void run() {
                updateStatus("started to list packages form phone/emulator");
                String lines = executeCommand(ConfigClass.pathToADB + "adb shell pm list packages");
                String[] linesArray = lines.split("\n");
                String[] filteredLines = new String[linesArray.length];
                int newIndex = 0;
                for (String line : linesArray) {
                    if (line.contains("package")) {
                        filteredLines[newIndex] = line.replaceFirst("package:", "");
                        newIndex++;
                    }
                }
                updateStatus("listing packages finished");
                jList1.setListData(filteredLines);
            }
        }).start();
    }//GEN-LAST:event_loadPackButtonActionPerformed

    /**
     * Pressing on button will pull every selected package from emulator/device
     * to pc.
     *
     * @param evt
     */
    private void pullPackagesButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pullPackagesButtonActionPerformed

        new Thread(new Runnable() {

            @Override
            public void run() {
                updateStatus("started pulling selected packages to PC");

                List<String> selectedPackages = jList1.getSelectedValuesList();
                String cmd1 = ConfigClass.pathToADB + "adb shell pm path "; //+ packageName
                String cmd2 = ConfigClass.pathToADB + "adb pull "; //+path
                String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();

                for (String packageName : selectedPackages) {

                    String pathPackage = executeCommand(cmd1 + packageName);
                    System.out.println("package path on phone: " + pathPackage);

                    String pathToFile = " " + currentPath + "/apk/" + packageName + ""; //deleted " --- "
                    String resultString = executeCommand(cmd2 + pathPackage.split(":")[1] + pathToFile);

                    System.out.println("result string: " + resultString);
                    System.out.println("path: \"" + currentPath + "/apk");
                }

                updateStatus("pulling packages finised");

            }
        }).start();
    }//GEN-LAST:event_pullPackagesButtonActionPerformed

    /**
     * OnClick button method.
     * Clicking on button will log out user.
     * @param evt 
     */
    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        this.logInFrame.setVisible(true);
        ConfigClass.token = null;
        ConfigClass.email = null;
        ConfigClass.password = null;
        this.dispose();
    }//GEN-LAST:event_jButton6ActionPerformed

    /**
     * Displays packages to User that are in .../apk folder.
     */
    private void displayApkFolder() {
        String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();
        File folder = new File(currentPath + "/apk");
        File[] listOfFiles = folder.listFiles();
        jList2.setListData(fileToName(listOfFiles));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JList jList3;
    private javax.swing.JList jList5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JButton loadPackButton;
    private javax.swing.JButton pullPackagesButton;
    private java.awt.TextField textField1;
    // End of variables declaration//GEN-END:variables
}