diff --git a/src/com/offsec/nethunter/AppNavHomeActivity.java b/src/com/offsec/nethunter/AppNavHomeActivity.java index f48112a..0080772 100644 --- a/src/com/offsec/nethunter/AppNavHomeActivity.java +++ b/src/com/offsec/nethunter/AppNavHomeActivity.java @@ -17,6 +17,8 @@ import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.TextView; @@ -62,10 +64,8 @@ public class AppNavHomeActivity extends AppCompatActivity implements KaliGPSUpda private NavigationView navigationView; private CharSequence mTitle = "NetHunter"; private Stack titles = new Stack<>(); - private static SharedPreferences prefs; + private SharedPreferences prefs; public static MenuItem lastSelectedMenuItem; - public Context context; - public Activity activity; private boolean locationUpdatesRequested = false; private KaliGPSUpdates.Receiver locationUpdateReceiver; private NhPaths nhPaths; @@ -78,14 +78,12 @@ public class AppNavHomeActivity extends AppCompatActivity implements KaliGPSUpda protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - this.context = getApplicationContext(); - this.activity = this; // Initiate the NhPaths singleton class, and it will then keep living until the app dies. // Also with its sharepreference listener registered, the CHROOT_PATH variable can be updated immediately on sharepreference changes. - nhPaths = NhPaths.getInstance(context); + nhPaths = NhPaths.getInstance(getApplicationContext()); // Initiate the PermissionCheck class. - permissionCheck = new PermissionCheck(activity, context); + permissionCheck = new PermissionCheck(this, getApplicationContext()); // Register the nethunter receiver with intent actions. nethunterReceiver = new NethunterReceiver(); IntentFilter AppNavHomeIntentFilter = new IntentFilter(); @@ -93,13 +91,13 @@ protected void onCreate(Bundle savedInstanceState) { AppNavHomeIntentFilter.addAction(NethunterReceiver.BACKPRESSED); AppNavHomeIntentFilter.addAction(NethunterReceiver.CHECKCHROOT); AppNavHomeIntentFilter.addAction("ChrootManager"); - activity.registerReceiver(nethunterReceiver, new IntentFilter(AppNavHomeIntentFilter)); + this.registerReceiver(nethunterReceiver, new IntentFilter(AppNavHomeIntentFilter)); // initiate prefs. prefs = getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE); // Start copying the app files to the corresponding path. - ProgressDialog progressDialog = new ProgressDialog(activity); - CopyBootFilesAsyncTask copyBootFilesAsyncTask = new CopyBootFilesAsyncTask(context, activity, progressDialog); + ProgressDialog progressDialog = new ProgressDialog(this); + CopyBootFilesAsyncTask copyBootFilesAsyncTask = new CopyBootFilesAsyncTask(getApplicationContext(), this, progressDialog); copyBootFilesAsyncTask.setListener(new CopyBootFilesAsyncTask.CopyBootFilesAsyncTaskListener() { @Override public void onAsyncTaskPrepare() { @@ -126,7 +124,7 @@ public void onAsyncTaskFinished(Object result) { } // Thirdly, check if nethunter terminal app has been installed. - if (context.getPackageManager().getLaunchIntentForPackage("com.offsec.nhterm") == null) { + if (getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.offsec.nhterm") == null) { showWarningDialog("Nethunter app cannot be run properly", "Nethunter terminal is not installed yet.", true); } @@ -141,7 +139,7 @@ public void onAsyncTaskFinished(Object result) { @Override - public boolean onOptionsItemSelected(MenuItem item) { + public boolean onOptionsItemSelected(@NonNull MenuItem item) { if (mDrawerToggle.onOptionsItemSelected(item)) { return true; } @@ -262,7 +260,7 @@ public void onStopRequested() { @Override protected void onStart() { super.onStart(); - if (navigationView != null) startService(new Intent(context, CompatCheckService.class)); + if (navigationView != null) startService(new Intent(getApplicationContext(), CompatCheckService.class)); } @Override @@ -291,8 +289,9 @@ private void setRootView(){ mDrawerLayout = findViewById(R.id.drawer_layout); navigationView = findViewById(R.id.navigation_view); + final ViewGroup nullParent = null; LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); - LinearLayout navigationHeadView = (LinearLayout) inflater.inflate(R.layout.sidenav_header, null); + LinearLayout navigationHeadView = (LinearLayout) inflater.inflate(R.layout.sidenav_header, nullParent); navigationView.addHeaderView(navigationHeadView); FloatingActionButton readmeButton = navigationHeadView.findViewById(R.id.info_fab); @@ -318,7 +317,7 @@ private void setRootView(){ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // detail for android 5 devices - getWindow().setStatusBarColor(ContextCompat.getColor(activity, R.color.darkTitle)); + getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.darkTitle)); } getSupportFragmentManager() @@ -342,10 +341,10 @@ private void setRootView(){ lastSelectedMenuItem = navigationView.getMenu().getItem(0); lastSelectedMenuItem.setChecked(true); } - mDrawerToggle = new ActionBarDrawerToggle(activity, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed); + mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_opened, R.string.drawer_closed); mDrawerLayout.setDrawerListener(mDrawerToggle); mDrawerToggle.syncState(); - startService(new Intent(context, CompatCheckService.class)); + startService(new Intent(getApplicationContext(), CompatCheckService.class)); } private void checkUpdate() { @@ -369,7 +368,9 @@ private void showLicense() { .setNegativeButton("Close", (dialog, which) -> dialog.cancel()); //nhwarning AlertDialog ad = adb.create(); ad.setCancelable(false); - ad.getWindow().getAttributes().windowAnimations = R.style.DialogStyle; + if (ad.getWindow() != null) { + ad.getWindow().getAttributes().windowAnimations = R.style.DialogStyle; + } ad.show(); } @@ -438,7 +439,7 @@ private void setupDrawerContent(NavigationView navigationView) { changeFragment(fragmentManager, MITMfFragment.newInstance(itemId)); break; case R.id.vnc_item: - if (context.getPackageManager().getLaunchIntentForPackage("com.offsec.nhvnc") == null) { + if (getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.offsec.nhvnc") == null) { showWarningDialog("", "Nethunter VNC is not installed yet.", false); } else { if (!permissionCheck.isAllPermitted(PermissionCheck.NH_VNC_PERMISSIONS)) { @@ -521,7 +522,7 @@ private boolean isAllRequiredPermissionsGranted(){ } public void showWarningDialog(String title, String message, boolean NeedToExit) { - android.app.AlertDialog.Builder warningAD = new android.app.AlertDialog.Builder(activity); + android.app.AlertDialog.Builder warningAD = new android.app.AlertDialog.Builder(this); warningAD.setCancelable(false); warningAD.setTitle(title); warningAD.setMessage(message); @@ -569,7 +570,11 @@ public void onReceive(Context context, Intent intent) { } } } catch (Exception e) { - Log.e(AppNavHomeActivity.TAG, e.getMessage()); + if (e.getMessage() != null) { + Log.e(AppNavHomeActivity.TAG, e.getMessage()); + } else { + Log.e(AppNavHomeActivity.TAG, "e.getMessage is Null."); + } } break; } diff --git a/src/com/offsec/nethunter/ChrootManagerFragment.java b/src/com/offsec/nethunter/ChrootManagerFragment.java index b7ae9e3..6784376 100644 --- a/src/com/offsec/nethunter/ChrootManagerFragment.java +++ b/src/com/offsec/nethunter/ChrootManagerFragment.java @@ -107,7 +107,7 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat resultViewerLoggerTextView.setMovementMethod(new ScrollingMovementMethod()); kaliFolderTextView.setClickable(true); kaliFolderTextView.setText(sharedPreferences.getString(SharePrefTag.CHROOT_ARCH_SHAREPREF_TAG, NhPaths.ARCH_FOLDER)); - LinearLayoutCompat kaliViewFolderlinearLayout = view.findViewById(R.id.f_chrootmanager_viewholder); + final LinearLayoutCompat kaliViewFolderlinearLayout = view.findViewById(R.id.f_chrootmanager_viewholder); kaliViewFolderlinearLayout.setOnClickListener(view1 -> new AlertDialog.Builder(activity) .setMessage(baseChrootPathTextView.getText().toString() + kaliFolderTextView.getText().toString()) @@ -144,10 +144,7 @@ public void onDestroyView() { addMetaPkgButton = null; removeChrootButton = null; backupChrootButton = null; - sharedPreferences = null; chrootManagerAsynctask = null; - context = null; - activity = null; } private void setEditButton(){ diff --git a/src/com/offsec/nethunter/CustomCommandsFragment.java b/src/com/offsec/nethunter/CustomCommandsFragment.java index adfedbb..ecb0c45 100644 --- a/src/com/offsec/nethunter/CustomCommandsFragment.java +++ b/src/com/offsec/nethunter/CustomCommandsFragment.java @@ -1,70 +1,32 @@ package com.offsec.nethunter; -import android.app.Activity; import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.content.pm.PackageManager; import android.os.Bundle; -import android.os.Vibrator; -import android.util.Log; import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; -import android.view.inputmethod.InputMethodManager; -import android.widget.BaseAdapter; -import android.widget.Button; -import android.widget.CheckBox; -import android.widget.EditText; -import android.widget.ListView; -import android.widget.SearchView; -import android.widget.Spinner; -import android.widget.TextView; -import android.widget.Toast; -import android.widget.SearchView; -import com.offsec.nethunter.utils.BootKali; -import com.offsec.nethunter.utils.NhPaths; -import com.offsec.nethunter.utils.ShellExecuter; - -import java.util.List; +import com.offsec.nethunter.RecyclerViewAdapter.CustomCommandsRecycleViewAdapter; +import com.offsec.nethunter.SQL.CustomCommandsSQL; +import com.offsec.nethunter.viewmodels.CustomCommandsViewModel; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import android.widget.SearchView; import androidx.fragment.app.Fragment; - -//import androidx.appcompat.widget.SearchView; +import androidx.lifecycle.ViewModelProviders; public class CustomCommandsFragment extends Fragment { - - private static final String ARG_SECTION_NUMBER = "section_number"; private static final String TAG = "CustomCommandsFragment"; - private CustomCommandsSQL database; - private ListView commandListView; - private CmdLoader commandAdapter; - private List commandList; - private String bootScriptPath; - private String shebang; - private String custom_commands_runlevel; - private final ShellExecuter exe = new ShellExecuter(); + private CustomCommandsRecycleViewAdapter customCommandsRecycleViewAdapterTitles; private Context context; - private Activity activity; + private static int targetPositionId; + public CustomCommandsFragment() { } - /** - * Returns a new instance of this fragment for the given section - * number. - */ public static CustomCommandsFragment newInstance(int sectionNumber) { - CustomCommandsFragment fragment = new CustomCommandsFragment(); Bundle args = new Bundle(); args.putInt(ARG_SECTION_NUMBER, sectionNumber); @@ -75,458 +37,33 @@ public static CustomCommandsFragment newInstance(int sectionNumber) { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - context = getContext(); - activity = getActivity(); - } - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//this runs BEFORE the ui is available - SharedPreferences sharedpreferences = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Context.MODE_PRIVATE); - database = new CustomCommandsSQL(context); - if (!sharedpreferences.contains("initial_commands")) { - SharedPreferences.Editor editor = sharedpreferences.edit(); - editor.putString("initial_commands", "added"); - editor.apply(); - setUpInitialCommands(); - } - - bootScriptPath = NhPaths.APP_INITD_PATH; - shebang = "#!/system/bin/sh\n\n# Run at boot CustomCommand: "; - custom_commands_runlevel = "90"; - - View rootView = inflater.inflate(R.layout.custom_commands, container, false); - final Button addCommand = rootView.findViewById(R.id.addCommand); + this.context = getContext(); setHasOptionsMenu(true); - final SearchView searchStr = rootView.findViewById(R.id.searchCommand); - main(rootView); - // set up listeners - addCommand.setOnClickListener(v -> showCommandDialog("ADD", null, 0)); - - searchStr.setOnQueryTextListener(new SearchView.OnQueryTextListener() { - - @Override - public boolean onQueryTextSubmit(String query) { - commandList.clear(); - commandList.addAll(database.getAllCommandsFiltered(query)); - commandAdapter.notifyDataSetChanged(); - return false; - } - - @Override - public boolean onQueryTextChange(String newText) { - return false; - } - - }); - return rootView; - - } - - private void addToBoot(CustomCommand command) { - String _label = command.getCommand_label(); - String _cmd = command.getCommand(); - //String _mode = command.getExec_Mode(); - String _sendTo = command.getSend_To_Shell(); - - String composedCommand; - if (_sendTo.equals("KALI")) { - composedCommand = "su -c '"+NhPaths.APP_SCRIPTS_PATH+"/bootkali custom_cmd " + _cmd + "'"; - } else { - // SEND TO ANDROID - // no sure, if we add su -c , we cant exec comands as a normal android user - composedCommand = _cmd; - } - String bootServiceFile = bootScriptPath + "/" + custom_commands_runlevel + "_" + command.getId() + "_custom_command"; - String fileContents = shebang + _label + "\n" + composedCommand; - Log.d("bootScript", fileContents); - exe.RunAsRoot(new String[]{ - "cat > " + bootServiceFile + " < { - ((Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE)).vibrate(50); - - CustomCommand currenCommand = (CustomCommand) commandListView.getItemAtPosition(position); - showCommandDialog("EDIT", currenCommand, position); - - return false; - }); - - } - - private static void hideSoftKeyboard(final View caller) { - caller.postDelayed(() -> { - InputMethodManager imm = (InputMethodManager) caller.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); - imm.hideSoftInputFromWindow(caller.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); - }, 100); - } - - private void showCommandDialog(String action, CustomCommand commandInfo, int position) { - // common setup - LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - final View promptsView = inflater.inflate(R.layout.custon_commands_dialog, null); - - AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(activity); - alertDialogBuilder.setView(promptsView); - alertDialogBuilder.setCancelable(false); - alertDialogBuilder.setNegativeButton("Cancel", - (dialog, id) -> { - dialog.cancel(); - hideSoftKeyboard(getView()); - }); - - final Spinner command_exec_mode = promptsView.findViewById(R.id.spinnerExecMode); - final CheckBox run_at_boot = promptsView.findViewById(R.id.custom_comands_runAtBoot); - - run_at_boot.setOnCheckedChangeListener((buttonView, isChecked) -> { - if (isChecked) { - command_exec_mode.setSelection(0); - command_exec_mode.setEnabled(false); - } else { - command_exec_mode.setEnabled(true); - } - }); - switch (action) { - case "ADD": - saveNewCommand(alertDialogBuilder, promptsView); - break; - case "EDIT": - editCommand(alertDialogBuilder, promptsView, commandInfo, position); - break; - } - } + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); - private void saveNewCommand(AlertDialog.Builder alertDialogBuilder, View promptsView) { - final EditText userInputBtnLabel = promptsView.findViewById(R.id.editText_launcher_btn_label); - final EditText userInputCommand = promptsView.findViewById(R.id.editText_launcher_command); - final Spinner command_exec_mode = promptsView.findViewById(R.id.spinnerExecMode); - final Spinner command_run_in_shell = promptsView.findViewById(R.id.spinnerRun_in_shell); - final CheckBox run_at_boot = promptsView.findViewById(R.id.custom_comands_runAtBoot); - alertDialogBuilder - .setPositiveButton("OK", - (dialog, id) -> { - if (userInputBtnLabel.getText().length() > 0 && - userInputCommand.getText().length() > 0) { - Integer _run_at_boot = 0; - if (run_at_boot.isChecked()) { - _run_at_boot = 1; + CustomCommandsViewModel customCommandsViewModel = ViewModelProviders.of(this).get(CustomCommandsViewModel.class); + customCommandsViewModel.init(context); + //customCommandsViewModel.getLiveDataCustomCommandsModelList().observe(this, customCommandsModelList -> customCommandsRecycleViewAdapterTitles.notifyDataSetChanged()); - } - CustomCommand _insertedCommand = database.addCommand(userInputBtnLabel.getText().toString(), - userInputCommand.getText().toString(), - command_exec_mode.getSelectedItem().toString(), - command_run_in_shell.getSelectedItem().toString(), _run_at_boot); - NhPaths.showMessage(context,"Command created."); + /* + recyclerViewServiceTitle = view.findViewById(R.id.f_kaliservices_recycleviewServiceTitle); + customCommandsRecycleViewAdapterTitles = new KaliServiceRecycleViewAdapterTitles(context, customCommandsViewModel.getLiveDataCustomCommandsModelList().getValue()); + LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); + recyclerViewServiceTitle.setLayoutManager(linearLayoutManager); + recyclerViewServiceTitle.setAdapter(customCommandsRecycleViewAdapterTitles); - if (_run_at_boot == 1) { - addToBoot(_insertedCommand); - } - // add to top of the list - commandList.add(0, _insertedCommand); - commandAdapter.notifyDataSetChanged(); - } else { - NhPaths.showMessage(context, getString(R.string.toast_input_error_launcher)); - } - hideSoftKeyboard(getView()); - }); - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.show(); - } - - private void editCommand(AlertDialog.Builder alertDialogBuilder, View promptsView, CustomCommand commandInfo, final int position) { - - final EditText userInputCommandLabel = promptsView.findViewById(R.id.editText_launcher_btn_label); - final EditText userInputCommand = promptsView.findViewById(R.id.editText_launcher_command); - final Spinner command_exec_mode = promptsView.findViewById(R.id.spinnerExecMode); - final Spinner command_run_in_shell = promptsView.findViewById(R.id.spinnerRun_in_shell); - final CheckBox run_at_boot = promptsView.findViewById(R.id.custom_comands_runAtBoot); - // command Info - final long _id = commandInfo.getId(); - String _label = commandInfo.getCommand_label(); - String _cmd = commandInfo.getCommand(); - String _mode = commandInfo.getExec_Mode(); - String _sendTo = commandInfo.getSend_To_Shell(); - Integer _runAtBoot = commandInfo.getRun_At_Boot(); - userInputCommandLabel.setText(_label); - userInputCommand.setText(_cmd); - - if (_runAtBoot == 1) { - run_at_boot.setChecked(true); - command_exec_mode.setSelection(0); // allways background - command_exec_mode.setEnabled(false); // force option 1 - } - - if (_sendTo.equals("KALI")) { - command_run_in_shell.setSelection(0); - } else { - // android - command_run_in_shell.setSelection(1); - } - if (_mode.equals("BACKGROUND")) { - command_exec_mode.setSelection(0); - } else { - // interactive - command_exec_mode.setSelection(1); - } - alertDialogBuilder - .setPositiveButton("Update", - (dialog, id) -> { - - if (userInputCommandLabel.getText().length() > 0 && - userInputCommand.getText().length() > 0) { - Integer _run_at_boot = 0; - if (run_at_boot.isChecked()) { - _run_at_boot = 1; - } - CustomCommand _updatedCommand = new CustomCommand(_id, - userInputCommandLabel.getText().toString(), - userInputCommand.getText().toString(), - command_exec_mode.getSelectedItem().toString(), - command_run_in_shell.getSelectedItem().toString(), _run_at_boot); - - database.updateCommand(_updatedCommand); - if (_run_at_boot == 1) { - addToBoot(_updatedCommand); - } else { - removeFromBoot(_updatedCommand.getId()); - } - NhPaths.showMessage(context, "Command Updated"); - commandList.set(position, _updatedCommand); - commandAdapter.notifyDataSetChanged(); - - } else { - NhPaths.showMessage(context, getString(R.string.toast_input_error_launcher)); - } - hideSoftKeyboard(getView()); - }) - .setNeutralButton("Delete", - (dialog, id) -> { - database.deleteCommand(_id); - removeFromBoot(_id); - commandList.remove(position); - commandAdapter.notifyDataSetChanged(); - hideSoftKeyboard(getView()); - NhPaths.showMessage(context, "Command Deleted"); - }); - AlertDialog alertDialog = alertDialogBuilder.create(); - alertDialog.show(); - } - - private void setUpInitialCommands() { - database.addCommand("Update Kali metapackages", NhPaths.makeTermTitle("Updating Kali") + "apt-get update && apt-get upgrade", "INTERACTIVE", "KALI", 0); - database.addCommand("Wlan1 Monitor Mode", NhPaths.makeTermTitle("Wlan1 Monitor UP") + "sudo ifconfig wlan1 down && sudo iwconfig wlan1 mode monitor && sudo ifconfig wlan1 up && echo \"wlan1 Monitor mode enabled\" && sleep 3 && exit", "INTERACTIVE", "KALI", 0); - database.addCommand("Launch Wifite", NhPaths.makeTermTitle("Wifite") + "wifite", "INTERACTIVE", "KALI", 0); - database.addCommand("Dump Mifare", NhPaths.makeTermTitle("DumpMifare") + "dumpmifare.sh", "INTERACTIVE", "KALI", 0); - database.addCommand("Backup Kali Chroot", NhPaths.makeTermTitle("Backup_Kali_Chroot") + "su --mount-master -c 'chroot_backup /data/local/nhsystem/kali-armhf /sdcard/kalifs-backup.tar.gz'", - "INTERACTIVE", "ANDROID", 0); - } -} - -class CmdLoader extends BaseAdapter { - - private final List _commandList; - private final Context _mContext; - private final ShellExecuter exe = new ShellExecuter(); - - public CmdLoader(Context context, List commandList) { - - _mContext = context; - _commandList = commandList; + */ } - - static class ViewHolderItem { - // The switch - //Switch sw; - // the msg holder - - TextView execmode; - TextView sendtocmd; - TextView runatboot; - // the service title - TextView cwTitle; - // run at boot checkbox - Button cwButton; - } - - public int getCount() { - // return the number of services - return _commandList.size(); - } - - // getView method is called for each item of ListView - public View getView(final int position, View convertView, ViewGroup parent) { - // inflate the layout for each item of listView (our services) - - ViewHolderItem vH; - - if (convertView == null) { - LayoutInflater inflater = (LayoutInflater) _mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - convertView = inflater.inflate(R.layout.custom_commands_item, parent, false); - - // set up the ViewHolder - vH = new ViewHolderItem(); - // get the reference of switch and the text view - vH.cwTitle = convertView.findViewById(R.id.command_tag); - // vH.cwSwich = (Switch) convertView.findViewById(R.id.switch1); - vH.execmode = convertView.findViewById(R.id.execmode); - vH.sendtocmd = convertView.findViewById(R.id.sendtocmd); - vH.runatboot = convertView.findViewById(R.id.custom_comands_runAtBoot_text); - vH.cwButton = convertView.findViewById(R.id.runCommand); - convertView.setTag(vH); - //System.out.println ("created row"); - } else { - // recycle the items in the list if already exists - vH = (ViewHolderItem) convertView.getTag(); - } - - // remove listeners - final CustomCommand commandInfo = getItem(position); - String _label = commandInfo.getCommand_label(); - // String _cmd = commandInfo.getCommand(); - String _mode = commandInfo.getExec_Mode(); - String _sendTo = commandInfo.getSend_To_Shell(); - Integer _runAtBoot = commandInfo.getRun_At_Boot(); - String _runAtBoot_txt = "NO"; - if (_runAtBoot == 1) { - _runAtBoot_txt = "YES"; - vH.runatboot.setTextColor(_mContext.getResources().getColor(R.color.darkorange)); - } else { - vH.runatboot.setTextColor(_mContext.getResources().getColor(R.color.blue)); - } - vH.cwButton.setOnClickListener(null); - // set service name - vH.cwTitle.setText(_label); - vH.execmode.setText(_mode); - vH.sendtocmd.setText(_sendTo); - vH.runatboot.setText(_runAtBoot_txt); - vH.cwButton.setOnClickListener(v -> doCustomCommand(commandInfo)); - return convertView; - - } - - public CustomCommand getItem(int position) { - return _commandList.get(position); - } - - public long getItemId(int position) { - return position; - } - - private boolean checkTerminalExternalPermission(String permission) { - int res = _mContext.checkCallingOrSelfPermission(permission); - return (res == PackageManager.PERMISSION_GRANTED); - } - - private void doCustomCommand(CustomCommand commandInfo) { - - String _label = commandInfo.getCommand_label(); - String _cmd = commandInfo.getCommand(); - String _mode = commandInfo.getExec_Mode(); - String _sendTo = commandInfo.getSend_To_Shell(); - String composedCommand; - - if (_mode.equals("BACKGROUND")) { - if (_sendTo.equals("KALI")) { - new BootKali(_cmd).run_bg(); - NhPaths.showMessage(_mContext, "Kali cmd done."); - } else { - // dont run all the bg commands as root - exe.Executer(_cmd); - NhPaths.showMessage(_mContext, "Android cmd done."); - } - } else try { - // INTERACTIVE - if (_sendTo.equals("KALI")) { - Intent intent = - new Intent("com.offsec.nhterm.RUN_SCRIPT_NH"); - intent.addCategory(Intent.CATEGORY_DEFAULT); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra("com.offsec.nhterm.iInitialCommand", _cmd); - _mContext.startActivity(intent); - - } else { - Intent intent = - new Intent("com.offsec.nhterm.RUN_SCRIPT"); - intent.addCategory(Intent.CATEGORY_DEFAULT); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - intent.putExtra("com.offsec.nhterm.iInitialCommand", _cmd); - _mContext.startActivity(intent); - - - } - - } catch (Exception e) { - if (!checkTerminalExternalPermission("com.offsec.nhterm.permission.RUN_SCRIPT_NH") || - !checkTerminalExternalPermission("com.offsec.nhterm.permission.RUN_SCRIPT")) { - NhPaths.showMessage(_mContext, _mContext.getString(R.string.toast_error_permissions)); - } else { - NhPaths.showMessage(_mContext, _mContext.getString(R.string.toast_install_terminal)); - } - } - } - } diff --git a/src/com/offsec/nethunter/DuckHunterConvertFragment.java b/src/com/offsec/nethunter/DuckHunterConvertFragment.java index 62c0b2d..090f203 100644 --- a/src/com/offsec/nethunter/DuckHunterConvertFragment.java +++ b/src/com/offsec/nethunter/DuckHunterConvertFragment.java @@ -136,8 +136,6 @@ public void onPause() { public void onDestroyView() { super.onDestroyView(); editsource = null; - context = null; - activity = null; } @Override diff --git a/src/com/offsec/nethunter/DuckHunterFragment.java b/src/com/offsec/nethunter/DuckHunterFragment.java index 1395b43..37e10a5 100644 --- a/src/com/offsec/nethunter/DuckHunterFragment.java +++ b/src/com/offsec/nethunter/DuckHunterFragment.java @@ -178,8 +178,6 @@ public void onDestroyView() { super.onDestroyView(); menu = null; mViewPager = null; - context = null; - activity = null; } private static void setLang() { diff --git a/src/com/offsec/nethunter/DuckHunterPreviewFragment.java b/src/com/offsec/nethunter/DuckHunterPreviewFragment.java index 5d53736..7f45561 100644 --- a/src/com/offsec/nethunter/DuckHunterPreviewFragment.java +++ b/src/com/offsec/nethunter/DuckHunterPreviewFragment.java @@ -65,7 +65,6 @@ public void onPause() { public void onDestroyView() { super.onDestroyView(); previewSource = null; - activity = null; } public class PreviewDuckyBroadcastReceiver extends BroadcastReceiver { diff --git a/src/com/offsec/nethunter/KaliServicesFragment.java b/src/com/offsec/nethunter/KaliServicesFragment.java index de94fce..35652f8 100644 --- a/src/com/offsec/nethunter/KaliServicesFragment.java +++ b/src/com/offsec/nethunter/KaliServicesFragment.java @@ -4,26 +4,22 @@ import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; -import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; -import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; -import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import com.google.android.material.floatingactionbutton.FloatingActionButton; -import com.offsec.nethunter.AsyncTask.KaliServicesAsyncTask; -import com.offsec.nethunter.RecyclerViewAdapter.KaliServiceRecycleViewAdapterTitles; +import com.offsec.nethunter.RecyclerViewAdapter.KaliServicesRecycleViewAdapter; import com.offsec.nethunter.RecyclerViewAdapter.KaliServicesRecycleViewAdapterDeleteItems; import com.offsec.nethunter.RecyclerViewData.KaliServicesData; import com.offsec.nethunter.SQL.KaliServicesSQL; @@ -42,7 +38,6 @@ import androidx.lifecycle.ViewModelProviders; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; -import androidx.transition.Visibility; public class KaliServicesFragment extends Fragment { private static final String TAG = "KaliServicesFragment"; @@ -53,7 +48,7 @@ public class KaliServicesFragment extends Fragment { private Button addButton; private Button deleteButton; private Button moveButton; - private KaliServiceRecycleViewAdapterTitles kaliServiceRecycleViewAdapterTitles; + private KaliServicesRecycleViewAdapter kaliServicesRecycleViewAdapter; private static int targetPositionId; public static KaliServicesFragment newInstance(int sectionNumber) { @@ -84,13 +79,13 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat super.onViewCreated(view, savedInstanceState); KaliServicesViewModel kaliServicesViewModel = ViewModelProviders.of(this).get(KaliServicesViewModel.class); kaliServicesViewModel.init(context); - kaliServicesViewModel.getLiveDataKaliServicesModelList().observe(this, kaliServicesModelList -> kaliServiceRecycleViewAdapterTitles.notifyDataSetChanged()); + kaliServicesViewModel.getLiveDataKaliServicesModelList().observe(this, kaliServicesModelList -> kaliServicesRecycleViewAdapter.notifyDataSetChanged()); + kaliServicesRecycleViewAdapter = new KaliServicesRecycleViewAdapter(context, kaliServicesViewModel.getLiveDataKaliServicesModelList().getValue()); RecyclerView recyclerViewServiceTitle = view.findViewById(R.id.f_kaliservices_recycleviewServiceTitle); - kaliServiceRecycleViewAdapterTitles = new KaliServiceRecycleViewAdapterTitles(context, kaliServicesViewModel.getLiveDataKaliServicesModelList().getValue()); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); recyclerViewServiceTitle.setLayoutManager(linearLayoutManager); - recyclerViewServiceTitle.setAdapter(kaliServiceRecycleViewAdapterTitles); + recyclerViewServiceTitle.setAdapter(kaliServicesRecycleViewAdapter); refreshButton = view.findViewById(R.id.f_kaliservices_refreshButton); addButton = view.findViewById(R.id.f_kaliservices_addItemButton); @@ -122,7 +117,7 @@ public boolean onQueryTextSubmit(String query) { @Override public boolean onQueryTextChange(String newText) { - kaliServiceRecycleViewAdapterTitles.getFilter().filter(newText); + kaliServicesRecycleViewAdapter.getFilter().filter(newText); return false; } }); @@ -200,13 +195,11 @@ public void onStart() { @Override public void onDestroyView() { super.onDestroyView(); - activity = null; - context = null; refreshButton = null; addButton = null; deleteButton = null; moveButton = null; - kaliServiceRecycleViewAdapterTitles = null; + kaliServicesRecycleViewAdapter = null; } private void onRefreshItemSetup(){ diff --git a/src/com/offsec/nethunter/MacchangerFragment.java b/src/com/offsec/nethunter/MacchangerFragment.java index 2cafadb..247dd3f 100644 --- a/src/com/offsec/nethunter/MacchangerFragment.java +++ b/src/com/offsec/nethunter/MacchangerFragment.java @@ -145,8 +145,6 @@ public void onDestroyView() { currentMacTextView = null; currentHostNameTextView = null; reloadImageButton = null; - context = null; - activity = null; } private void genRandomMACAddress() { diff --git a/src/com/offsec/nethunter/NetHunterFragment.java b/src/com/offsec/nethunter/NetHunterFragment.java index cb2dc3c..be391a1 100644 --- a/src/com/offsec/nethunter/NetHunterFragment.java +++ b/src/com/offsec/nethunter/NetHunterFragment.java @@ -83,10 +83,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat nethunterViewModel.getLiveDataNethunterModelList().observe(this, nethunterModelList -> { nethunterRecyclerViewAdapter.notifyDataSetChanged(); }); + + nethunterRecyclerViewAdapter = new NethunterRecyclerViewAdapter(context, nethunterViewModel.getLiveDataNethunterModelList().getValue()); RecyclerView itemRecyclerView = view.findViewById(R.id.f_nethunter_recyclerview); LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false); itemRecyclerView.setLayoutManager(linearLayoutManager); - nethunterRecyclerViewAdapter = new NethunterRecyclerViewAdapter(context, nethunterViewModel.getLiveDataNethunterModelList().getValue()); itemRecyclerView.setAdapter(nethunterRecyclerViewAdapter); refreshButton = view.findViewById(R.id.f_nethunter_refreshButton); @@ -195,8 +196,6 @@ public void onStart() { @Override public void onDestroyView() { super.onDestroyView(); - context = null; - activity = null; refreshButton = null; addButton = null; deleteButton = null;