// Path: source-code/app/src/main/java/org/buildmlearn/toolkit/ToolkitApplication.java
// public class ToolkitApplication extends Application {
// 
//     private static String dir;
// 
//     private static boolean isExternalStorageAvailable = false;
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for unzipped apks
//      */
//     public static String getUnZipDir() {
//         return dir + Constants.UNZIP;
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         storagePathsValidate();
//     }
// 
//     public void storagePathsValidate() {
//         if (checkExternalStorage()) {
//             isExternalStorageAvailable = true;
//             dir = Environment.getExternalStorageDirectory().getAbsolutePath();
//         } else {
//             dir = getFilesDir().getAbsolutePath();
//         }
// 
//         ArrayList<String> paths = new ArrayList<>();
//         paths.add(getProjectDir());
//         paths.add(getApkDir());
//         paths.add(getSavedDir());
//         paths.add(getUnZipDir());
// 
//         for (String path : paths) {
//             File f = new File(path);
//             if (!f.isDirectory()) {
//                 f.mkdirs();
//             }
//         }
//     }
// 
//     /**
//      * @return folder file
//      * @brief Returns external storage directory.
//      */
//     public File getDir() {
//         return Environment.getExternalStorageDirectory();
//     }
// 
//     /**
//      * @return folder path
//      * @brief Returns directory for BuildmLearn toolkit  manually created files.
//      */
//     public String getProjectDir() {
//         return dir + Constants.BUILD_M_LEARN_PATH;
// 
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for saved projects
//      */
//     public String getSavedDir() {
//         return dir + Constants.SAVED_DIR;
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for saved projects
//      */
//     public String getDraftDir() {
//         return dir + Constants.DRAFT_DIR;
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for storing generated apks
//      */
//     public String getApkDir() {
//         return dir + Constants.APK_DIR;
//     }
// 
//     /**
//      * @return true if external storage is present, else false
//      * @brief Checks if external storage is present for storing data
//      */
//     public boolean checkExternalStorage() {
// 
//         boolean result = false;
//         File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/BuildmLearn123/");
//         if (!f.isDirectory()) {
//             result = f.mkdirs();
//             f.delete();
//         }
//         return result;
//     }
// 
//     public boolean isExternalStorageAvailable() {
//         return isExternalStorageAvailable;
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for Download directory
//      */
//     public String getDownloadDirectory() {
//         return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
//     }
// }
// 
// Path: source-code/app/src/main/java/org/buildmlearn/toolkit/model/KeyStoreDetails.java
// public class KeyStoreDetails {
// 
//     private final String assetsPath;
//     private final String password;
//     private final String alias;
//     private final String aliasPassword;
// 
//     public KeyStoreDetails(String password, String alias, String aliasPassword) {
//         this.assetsPath = "TestKeyStore.jks";
//         this.password = password;
//         this.alias = alias;
//         this.aliasPassword = aliasPassword;
//     }
// 
//     public String getAssetsPath() {
//         return assetsPath;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public String getAlias() {
//         return alias;
//     }
// 
//     public String getAliasPassword() {
//         return aliasPassword;
//     }
// }


import android.Manifest;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings.Secure;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import org.buildmlearn.toolkit.R;
import org.buildmlearn.toolkit.ToolkitApplication;
import org.buildmlearn.toolkit.model.KeyStoreDetails;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.security.UnrecoverableKeyException;
import java.util.Calendar;
import kellinwood.security.zipsigner.AutoKeyException;
import kellinwood.security.zipsigner.ZipSigner;
import kellinwood.security.zipsigner.optional.CustomKeySigner;
import pxb.android.axml.AxmlReader;
import pxb.android.axml.AxmlVisitor;
import pxb.android.axml.AxmlWriter;
import pxb.android.axml.NodeVisitor;
    }

    public void setSignerThreadListener(OnSignComplete listener) {
        this.listener = listener;
    }

    public void run() {

        int permissionCheck = ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck == -1) {
            listener.onFail(new FileNotFoundException());
            return;
        }

        FileUtils.copyAssets(context, assetsApk, toolkit.getApkDir());
        FileUtils.copyAssets(context, keyDetails.getAssetsPath(), toolkit.getApkDir());

        try {
            FileUtils.unZip(toolkit.getApkDir() + assetsApk, ToolkitApplication.getUnZipDir() + TEMP_FOLDER);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String packageName = "org.buildmlearn.app";
            packageName += Secure.getString(context.getContentResolver(),
                    Secure.ANDROID_ID);
            Calendar rightNow = Calendar.getInstance();
            packageName += String.valueOf(rightNow.getTimeInMillis());

// Path: source-code/app/src/main/java/org/buildmlearn/toolkit/ToolkitApplication.java
// public class ToolkitApplication extends Application {
// 
//     private static String dir;
// 
//     private static boolean isExternalStorageAvailable = false;
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for unzipped apks
//      */
//     public static String getUnZipDir() {
//         return dir + Constants.UNZIP;
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
//         storagePathsValidate();
//     }
// 
//     public void storagePathsValidate() {
//         if (checkExternalStorage()) {
//             isExternalStorageAvailable = true;
//             dir = Environment.getExternalStorageDirectory().getAbsolutePath();
//         } else {
//             dir = getFilesDir().getAbsolutePath();
//         }
// 
//         ArrayList<String> paths = new ArrayList<>();
//         paths.add(getProjectDir());
//         paths.add(getApkDir());
//         paths.add(getSavedDir());
//         paths.add(getUnZipDir());
// 
//         for (String path : paths) {
//             File f = new File(path);
//             if (!f.isDirectory()) {
//                 f.mkdirs();
//             }
//         }
//     }
// 
//     /**
//      * @return folder file
//      * @brief Returns external storage directory.
//      */
//     public File getDir() {
//         return Environment.getExternalStorageDirectory();
//     }
// 
//     /**
//      * @return folder path
//      * @brief Returns directory for BuildmLearn toolkit  manually created files.
//      */
//     public String getProjectDir() {
//         return dir + Constants.BUILD_M_LEARN_PATH;
// 
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for saved projects
//      */
//     public String getSavedDir() {
//         return dir + Constants.SAVED_DIR;
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for saved projects
//      */
//     public String getDraftDir() {
//         return dir + Constants.DRAFT_DIR;
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for storing generated apks
//      */
//     public String getApkDir() {
//         return dir + Constants.APK_DIR;
//     }
// 
//     /**
//      * @return true if external storage is present, else false
//      * @brief Checks if external storage is present for storing data
//      */
//     public boolean checkExternalStorage() {
// 
//         boolean result = false;
//         File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/BuildmLearn123/");
//         if (!f.isDirectory()) {
//             result = f.mkdirs();
//             f.delete();
//         }
//         return result;
//     }
// 
//     public boolean isExternalStorageAvailable() {
//         return isExternalStorageAvailable;
//     }
// 
//     /**
//      * @return Folder path
//      * @brief Returns folder path for Download directory
//      */
//     public String getDownloadDirectory() {
//         return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
//     }
// }
// 
// Path: source-code/app/src/main/java/org/buildmlearn/toolkit/model/KeyStoreDetails.java
// public class KeyStoreDetails {
// 
//     private final String assetsPath;
//     private final String password;
//     private final String alias;
//     private final String aliasPassword;
// 
//     public KeyStoreDetails(String password, String alias, String aliasPassword) {
//         this.assetsPath = "TestKeyStore.jks";
//         this.password = password;
//         this.alias = alias;
//         this.aliasPassword = aliasPassword;
//     }
// 
//     public String getAssetsPath() {
//         return assetsPath;
//     }
// 
//     public String getPassword() {
//         return password;
//     }
// 
//     public String getAlias() {
//         return alias;
//     }
// 
//     public String getAliasPassword() {
//         return aliasPassword;
//     }
// }


// Path: source-code/app/src/main/java/org/buildmlearn/toolkit/utilities/SignerThread.java
import android.Manifest;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings.Secure;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;
import org.buildmlearn.toolkit.R;
import org.buildmlearn.toolkit.ToolkitApplication;
import org.buildmlearn.toolkit.model.KeyStoreDetails;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.security.UnrecoverableKeyException;
import java.util.Calendar;
import kellinwood.security.zipsigner.AutoKeyException;
import kellinwood.security.zipsigner.ZipSigner;
import kellinwood.security.zipsigner.optional.CustomKeySigner;
import pxb.android.axml.AxmlReader;
import pxb.android.axml.AxmlVisitor;
import pxb.android.axml.AxmlWriter;
import pxb.android.axml.NodeVisitor;

    }

    public void setSignerThreadListener(OnSignComplete listener) {
        this.listener = listener;
    }

    public void run() {

        int permissionCheck = ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (permissionCheck == -1) {
            listener.onFail(new FileNotFoundException());
            return;
        }

        FileUtils.copyAssets(context, assetsApk, toolkit.getApkDir());
        FileUtils.copyAssets(context, keyDetails.getAssetsPath(), toolkit.getApkDir());

        try {
            FileUtils.unZip(toolkit.getApkDir() + assetsApk, ToolkitApplication.getUnZipDir() + TEMP_FOLDER);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            String packageName = "org.buildmlearn.app";
            packageName += Secure.getString(context.getContentResolver(),
                    Secure.ANDROID_ID);
            Calendar rightNow = Calendar.getInstance();
            packageName += String.valueOf(rightNow.getTimeInMillis());

            modifyManifest(new String[]{ToolkitApplication.getUnZipDir() + TEMP_FOLDER + "/AndroidManifest.xml", packageName});