// Path: app/src/main/java/it/skarafaz/mercury/MercuryApplication.java
// public class MercuryApplication extends Application {
//     private static final String S_HAS_PERMANENT_MENU_KEY = "sHasPermanentMenuKey";
//     private static final Logger logger = LoggerFactory.getLogger(MercuryApplication.class);
//     private static Context context;
// 
//     public static Context getContext() {
//         return context;
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
// 
//         context = this;
// 
//         EventBus.builder().addIndex(new EventBusIndex()).build();
// 
//         // hack for devices with hw options button
//         try {
//             ViewConfiguration config = ViewConfiguration.get(this);
//             Field menuKeyField = ViewConfiguration.class.getDeclaredField(S_HAS_PERMANENT_MENU_KEY);
//             if (menuKeyField != null) {
//                 menuKeyField.setAccessible(true);
//                 menuKeyField.setBoolean(config, false);
//             }
//         } catch (Exception e) {
//             logger.error(e.getMessage().replace("\n", " "));
//         }
//     }
// 
//     public static void showProgressDialog(FragmentManager manager, String content) {
//         FragmentTransaction transaction = manager.beginTransaction();
//         transaction.add(ProgressDialogFragment.newInstance(content), ProgressDialogFragment.TAG);
//         transaction.commitAllowingStateLoss();
//     }
// 
//     public static void dismissProgressDialog(FragmentManager manager) {
//         FragmentTransaction transaction = manager.beginTransaction();
//         Fragment fragment = manager.findFragmentByTag(ProgressDialogFragment.TAG);
//         if (fragment != null) {
//             transaction.remove(fragment);
//         }
//         transaction.commitAllowingStateLoss();
//     }
// 
//     public static boolean hasPermission(String permission) {
//         return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
//     }
// 
//     public static boolean requestPermission(Activity activity, int requestCode, String permission) {
//         boolean requested = false;
//         if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
//             ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
//             requested = true;
//         }
//         return requested;
//     }
// 
//     public static boolean isExternalStorageReadable() {
//         String state = Environment.getExternalStorageState();
//         return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
//     }
// 
//     public static boolean isExternalStorageWritable() {
//         String state = Environment.getExternalStorageState();
//         return Environment.MEDIA_MOUNTED.equals(state);
//     }
// }
// 
// Path: app/src/main/java/it/skarafaz/mercury/model/event/SshCommandMessage.java
// public class SshCommandMessage {
//     private String message;
//     private SshCommandDrop<Boolean> drop;
// 
//     public SshCommandMessage(String message, SshCommandDrop<Boolean> drop) {
//         this.message = message;
//         this.drop = drop;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public SshCommandDrop<Boolean> getDrop() {
//         return drop;
//     }
// }
// 
// Path: app/src/main/java/it/skarafaz/mercury/model/event/SshCommandPassword.java
// public class SshCommandPassword {
//     private String message;
//     private SshCommandDrop<String> drop;
// 
//     public SshCommandPassword(String message, SshCommandDrop<String> drop) {
//         this.message = message;
//         this.drop = drop;
//     }
// 
//     public SshCommandDrop<String> getDrop() {
//         return drop;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// }
// 
// Path: app/src/main/java/it/skarafaz/mercury/model/event/SshCommandYesNo.java
// public class SshCommandYesNo {
//     private String message;
//     private SshCommandDrop<Boolean> drop;
// 
//     public SshCommandYesNo(String message, SshCommandDrop<Boolean> drop) {
//         this.message = message;
//         this.drop = drop;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public SshCommandDrop<Boolean> getDrop() {
//         return drop;
//     }
// }


import com.jcraft.jsch.UserInfo;
import it.skarafaz.mercury.MercuryApplication;
import it.skarafaz.mercury.R;
import it.skarafaz.mercury.model.event.SshCommandMessage;
import it.skarafaz.mercury.model.event.SshCommandPassword;
import it.skarafaz.mercury.model.event.SshCommandYesNo;
import org.greenrobot.eventbus.EventBus;
/*
 * Mercury-SSH
 * Copyright (C) 2017 Skarafaz
 *
 * This file is part of Mercury-SSH.
 *
 * Mercury-SSH is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Mercury-SSH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Mercury-SSH.  If not, see <http://www.gnu.org/licenses/>.
 */

package it.skarafaz.mercury.ssh;


public class SshCommandUserInfo implements UserInfo {
    private String password;

    @Override
    public String getPassphrase() {
        return null;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean promptPassword(String message) {
        SshCommandDrop<String> drop = new SshCommandDrop<>();
        message = String.format(MercuryApplication.getContext().getString(R.string.type_login_password), message.toLowerCase());
        EventBus.getDefault().postSticky(new SshCommandPassword(message, drop));
        password = drop.take();
        return password != null;
    }

    @Override
    public boolean promptPassphrase(String message) {
        return false;
    }

    @Override
    public boolean promptYesNo(String message) {
        SshCommandDrop<Boolean> drop = new SshCommandDrop<>();
        EventBus.getDefault().postSticky(new SshCommandYesNo(message, drop));
        return drop.take();
    }

// Path: app/src/main/java/it/skarafaz/mercury/MercuryApplication.java
// public class MercuryApplication extends Application {
//     private static final String S_HAS_PERMANENT_MENU_KEY = "sHasPermanentMenuKey";
//     private static final Logger logger = LoggerFactory.getLogger(MercuryApplication.class);
//     private static Context context;
// 
//     public static Context getContext() {
//         return context;
//     }
// 
//     @Override
//     public void onCreate() {
//         super.onCreate();
// 
//         context = this;
// 
//         EventBus.builder().addIndex(new EventBusIndex()).build();
// 
//         // hack for devices with hw options button
//         try {
//             ViewConfiguration config = ViewConfiguration.get(this);
//             Field menuKeyField = ViewConfiguration.class.getDeclaredField(S_HAS_PERMANENT_MENU_KEY);
//             if (menuKeyField != null) {
//                 menuKeyField.setAccessible(true);
//                 menuKeyField.setBoolean(config, false);
//             }
//         } catch (Exception e) {
//             logger.error(e.getMessage().replace("\n", " "));
//         }
//     }
// 
//     public static void showProgressDialog(FragmentManager manager, String content) {
//         FragmentTransaction transaction = manager.beginTransaction();
//         transaction.add(ProgressDialogFragment.newInstance(content), ProgressDialogFragment.TAG);
//         transaction.commitAllowingStateLoss();
//     }
// 
//     public static void dismissProgressDialog(FragmentManager manager) {
//         FragmentTransaction transaction = manager.beginTransaction();
//         Fragment fragment = manager.findFragmentByTag(ProgressDialogFragment.TAG);
//         if (fragment != null) {
//             transaction.remove(fragment);
//         }
//         transaction.commitAllowingStateLoss();
//     }
// 
//     public static boolean hasPermission(String permission) {
//         return ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
//     }
// 
//     public static boolean requestPermission(Activity activity, int requestCode, String permission) {
//         boolean requested = false;
//         if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) {
//             ActivityCompat.requestPermissions(activity, new String[]{permission}, requestCode);
//             requested = true;
//         }
//         return requested;
//     }
// 
//     public static boolean isExternalStorageReadable() {
//         String state = Environment.getExternalStorageState();
//         return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
//     }
// 
//     public static boolean isExternalStorageWritable() {
//         String state = Environment.getExternalStorageState();
//         return Environment.MEDIA_MOUNTED.equals(state);
//     }
// }
// 
// Path: app/src/main/java/it/skarafaz/mercury/model/event/SshCommandMessage.java
// public class SshCommandMessage {
//     private String message;
//     private SshCommandDrop<Boolean> drop;
// 
//     public SshCommandMessage(String message, SshCommandDrop<Boolean> drop) {
//         this.message = message;
//         this.drop = drop;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public SshCommandDrop<Boolean> getDrop() {
//         return drop;
//     }
// }
// 
// Path: app/src/main/java/it/skarafaz/mercury/model/event/SshCommandPassword.java
// public class SshCommandPassword {
//     private String message;
//     private SshCommandDrop<String> drop;
// 
//     public SshCommandPassword(String message, SshCommandDrop<String> drop) {
//         this.message = message;
//         this.drop = drop;
//     }
// 
//     public SshCommandDrop<String> getDrop() {
//         return drop;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// }
// 
// Path: app/src/main/java/it/skarafaz/mercury/model/event/SshCommandYesNo.java
// public class SshCommandYesNo {
//     private String message;
//     private SshCommandDrop<Boolean> drop;
// 
//     public SshCommandYesNo(String message, SshCommandDrop<Boolean> drop) {
//         this.message = message;
//         this.drop = drop;
//     }
// 
//     public String getMessage() {
//         return message;
//     }
// 
//     public SshCommandDrop<Boolean> getDrop() {
//         return drop;
//     }
// }


// Path: app/src/main/java/it/skarafaz/mercury/ssh/SshCommandUserInfo.java
import com.jcraft.jsch.UserInfo;
import it.skarafaz.mercury.MercuryApplication;
import it.skarafaz.mercury.R;
import it.skarafaz.mercury.model.event.SshCommandMessage;
import it.skarafaz.mercury.model.event.SshCommandPassword;
import it.skarafaz.mercury.model.event.SshCommandYesNo;
import org.greenrobot.eventbus.EventBus;

/*
 * Mercury-SSH
 * Copyright (C) 2017 Skarafaz
 *
 * This file is part of Mercury-SSH.
 *
 * Mercury-SSH is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Mercury-SSH is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Mercury-SSH.  If not, see <http://www.gnu.org/licenses/>.
 */

package it.skarafaz.mercury.ssh;


public class SshCommandUserInfo implements UserInfo {
    private String password;

    @Override
    public String getPassphrase() {
        return null;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public boolean promptPassword(String message) {
        SshCommandDrop<String> drop = new SshCommandDrop<>();
        message = String.format(MercuryApplication.getContext().getString(R.string.type_login_password), message.toLowerCase());
        EventBus.getDefault().postSticky(new SshCommandPassword(message, drop));
        password = drop.take();
        return password != null;
    }

    @Override
    public boolean promptPassphrase(String message) {
        return false;
    }

    @Override
    public boolean promptYesNo(String message) {
        SshCommandDrop<Boolean> drop = new SshCommandDrop<>();
        EventBus.getDefault().postSticky(new SshCommandYesNo(message, drop));
        return drop.take();
    }

    @Override