Code example for ListFragment

Methods: getActivity, startActivityForResult

0
     }  
     
    /** execute the selected command */ 
    private final void executeCommand(final Command cmd)
    { 
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
         
		if (!cmd.getCommandValues().equals(""))
		{ 
			// handle whatever values are defined for the command 
			// values can be either a list of "|" separated items to pick from 
			// or a sub-command to be executed first 
			// sub-commands are of the form "??something??:somewhere" 
			// allowed sub-commands are  
			String strPickFile = "??pickfile??";
			String strPickDir = "??pickdir??";
			// using OpenIntent's FileManager: http://www.openintents.org/en/node/159 
			 
			 
			if ((cmd.getCommandValues().startsWith(strPickFile)) || (cmd.getCommandValues().startsWith(strPickDir)))
			{ 
				// check for additional params in the sub-command 
				CharSequence[] tokens = cmd.getCommandValues().split("\\:");
				String strSuggestion = "";
				if (tokens.length > 1)
				{ 
					strSuggestion = (String)tokens[1];
				} 
				Intent myIntent = null;
				if (cmd.getCommandValues().startsWith(strPickFile))
				{ 
					myIntent = new Intent("org.openintents.action.PICK_FILE");
					myIntent.putExtra("org.openintents.extra.TITLE", "Pick a file");
					myIntent.putExtra("org.openintents.extra.BUTTON_TEXT", "Pick");
					 
				} 
				if (cmd.getCommandValues().startsWith(strPickDir))
				{ 
					myIntent = new Intent("org.openintents.action.PICK_DIRECTORY");
					myIntent.putExtra("org.openintents.extra.TITLE", "Pick a directory");
					myIntent.putExtra("org.openintents.extra.BUTTON_TEXT", "Pick");
				} 
				 
				if (myIntent == null)
				{ 
					Toast.makeText(getActivity(), "sub-command could not be resolved, check the syntax of your command", 
							Toast.LENGTH_SHORT).show();
					return; 
				} 
				if (!strSuggestion.equals(""))
				{ 
					myIntent.setData(Uri.parse("file://" + strSuggestion));
				} 
				 
				try 
				{ 
					startActivityForResult(myIntent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);
				} 
				catch (ActivityNotFoundException e)
				{ 
					// No compatible file manager was found. 
					Toast.makeText(getActivity(), "You must install OpenIntent's FileManager to use this feature", 
							Toast.LENGTH_SHORT).show();
				} 
				 
			} 
			else 
			{ 
				final CharSequence[] items = cmd.getCommandValues().split("\\|");
	 
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setTitle("Pick a Value");
				builder.setCancelable(false);
	 
				builder.setItems(items, new DialogInterface.OnClickListener() {
				    public void onClick(DialogInterface dialog, int item) {
				    	String strSelection = (String) items[item];
				    	CharSequence[] tokens = strSelection.split("\\:");
				    	strSelection = (String) tokens[0];
				    	 
				    	m_myCommand = cmd;
//				        cmd.execute(strSelection); 
				        new ExecuteCommandTask().execute(strSelection);
				        Toast.makeText(getActivity(), "Executing " + m_myCommand.getCommand(), Toast.LENGTH_LONG).show();
		    			//refreshList(); 
		    			//refreshCommandsCache(); 
				    } 
				}); 
				AlertDialog alert = builder.show();
			} 
		} 
		else 
		{ 
//			ArrayList<String> myRes = m_myCommand.execute(); 
			new ExecuteCommandTask().execute("");
			Toast.makeText(getActivity(), "Executing " + m_myCommand.getCommand(), Toast.LENGTH_LONG).show();
//			showDialog(myRes); 
			//refreshList(); 
			//refreshCommandsCache();