/** * @param text * @return */ private boolean startConsoleActivity() { Uri uri = TransportFactory.getUri( (String) transportSpinner.getSelectedItem(), quickconnect.getText().toString()); if (uri == null) { quickconnect.setError( getString( R.string.list_format_error, TransportFactory.getFormatHint( (String) transportSpinner.getSelectedItem(), HostListActivity.this))); return false; } HostBean host = TransportFactory.findHost(hostdb, uri); if (host == null) { host = TransportFactory.getTransport(uri.getScheme()).createHost(uri); host.setColor(HostDatabase.COLOR_GRAY); host.setPubkeyId(HostDatabase.PUBKEYID_ANY); hostdb.saveHost(host); } Intent intent = new Intent(HostListActivity.this, ConsoleActivity.class); intent.setData(uri); startActivity(intent); return true; }
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // create menu to handle hosts // create menu to handle deleting and sharing lists AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; final HostBean host = (HostBean) this.getListView().getItemAtPosition(info.position); menu.setHeaderTitle(host.getNickname()); // edit, disconnect, delete MenuItem connect = menu.add(R.string.list_host_disconnect); final TerminalBridge bridge = bound.getConnectedBridge(host); connect.setEnabled((bridge != null)); connect.setOnMenuItemClickListener( new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { bridge.dispatchDisconnect(true); updateHandler.sendEmptyMessage(-1); return true; } }); MenuItem edit = menu.add(R.string.list_host_edit); edit.setOnMenuItemClickListener( new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { Intent intent = new Intent(HostListActivity.this, HostEditorActivity.class); intent.putExtra(Intent.EXTRA_TITLE, host.getId()); HostListActivity.this.startActivityForResult(intent, REQUEST_EDIT); return true; } }); MenuItem portForwards = menu.add(R.string.list_host_portforwards); portForwards.setOnMenuItemClickListener( new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { Intent intent = new Intent(HostListActivity.this, PortForwardListActivity.class); intent.putExtra(Intent.EXTRA_TITLE, host.getId()); HostListActivity.this.startActivityForResult(intent, REQUEST_EDIT); return true; } }); if (!TransportFactory.canForwardPorts(host.getProtocol())) portForwards.setEnabled(false); MenuItem delete = menu.add(R.string.list_host_delete); delete.setOnMenuItemClickListener( new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { // prompt user to make sure they really want this new AlertDialog.Builder(HostListActivity.this) .setMessage(getString(R.string.delete_message, host.getNickname())) .setPositiveButton( R.string.delete_pos, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // make sure we disconnect if (bridge != null) bridge.dispatchDisconnect(true); hostdb.deleteHost(host); updateHandler.sendEmptyMessage(-1); } }) .setNegativeButton(R.string.delete_neg, null) .create() .show(); return true; } }); }
@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.act_hostlist); this.setTitle( String.format( "%s: %s", getResources().getText(R.string.app_name), getResources().getText(R.string.title_hosts_list))); // check for eula agreement this.prefs = PreferenceManager.getDefaultSharedPreferences(this); boolean agreed = prefs.getBoolean(PreferenceConstants.EULA, false); if (!agreed) { this.startActivityForResult(new Intent(this, WizardActivity.class), REQUEST_EULA); } this.makingShortcut = Intent.ACTION_CREATE_SHORTCUT.equals(getIntent().getAction()) || Intent.ACTION_PICK.equals(getIntent().getAction()); // connect with hosts database and populate list this.hostdb = new HostDatabase(this); ListView list = this.getListView(); this.sortedByColor = prefs.getBoolean(PreferenceConstants.SORT_BY_COLOR, false); // this.list.setSelector(R.drawable.highlight_disabled_pressed); list.setOnItemClickListener( new OnItemClickListener() { public synchronized void onItemClick( AdapterView<?> parent, View view, int position, long id) { // launch off to console details HostBean host = (HostBean) parent.getAdapter().getItem(position); Uri uri = host.getUri(); Intent contents = new Intent(Intent.ACTION_VIEW, uri); contents.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); if (makingShortcut) { // create shortcut if requested ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(HostListActivity.this, R.drawable.icon); Intent intent = new Intent(); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, contents); intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, host.getNickname()); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); setResult(RESULT_OK, intent); finish(); } else { // otherwise just launch activity to show this host HostListActivity.this.startActivity(contents); } } }); this.registerForContextMenu(list); quickconnect = (TextView) this.findViewById(R.id.front_quickconnect); quickconnect.setVisibility(makingShortcut ? View.GONE : View.VISIBLE); quickconnect.setOnKeyListener( new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_UP) return false; if (keyCode != KeyEvent.KEYCODE_ENTER) return false; return startConsoleActivity(); } }); transportSpinner = (Spinner) findViewById(R.id.transport_selection); transportSpinner.setVisibility(makingShortcut ? View.GONE : View.VISIBLE); ArrayAdapter<String> transportSelection = new ArrayAdapter<String>( this, android.R.layout.simple_spinner_item, TransportFactory.getTransportNames()); transportSelection.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); transportSpinner.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener() { public void onItemSelected(AdapterView<?> arg0, View view, int position, long id) { String formatHint = TransportFactory.getFormatHint( (String) transportSpinner.getSelectedItem(), HostListActivity.this); quickconnect.setHint(formatHint); quickconnect.setError(null); quickconnect.requestFocus(); } public void onNothingSelected(AdapterView<?> arg0) {} }); transportSpinner.setAdapter(transportSelection); this.inflater = LayoutInflater.from(this); }