/**
   * Function that sets up the list view that contains the ingredients. This allows the user to
   * immediately receive feedback on the ingredients they have entered, as well as allowing them to
   * long press to delete ingredients.
   */
  public void setUpListView() {
    // Set up the list view
    ListView listView = (ListView) findViewById(R.id.lViewIngredients);
    GlobalApplication app = (GlobalApplication) getApplication();
    ArrayList<String> content = app.getCurrentRecipe().getIngredients().getStringArrayList();
    ArrayAdapter<String> adapter =
        new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, content);
    listView.setAdapter(adapter);

    // Set what happens when a user long clicks on an item (prompts for
    // deletion
    listView.setOnItemLongClickListener(
        new OnItemLongClickListener() {

          @Override
          public boolean onItemLongClick(
              AdapterView<?> arg0, View v, final int position, long arg3) {
            // Builds the alert dialog box
            AlertDialog.Builder prompt = new AlertDialog.Builder(v.getContext());
            prompt.setTitle("Delete Ingredient");
            prompt.setMessage(
                "Are you sure you want to delete this ingredient? It "
                    + "will be gone... forever.");

            prompt.setNegativeButton(
                "No",
                new DialogInterface.OnClickListener() {

                  // User has changed their mind
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    return;
                  }
                });
            prompt.setPositiveButton(
                "Yes",
                new DialogInterface.OnClickListener() {

                  // User does want to delete the ingredient they are
                  // long pressing
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                    // Get the global app state
                    GlobalApplication app = (GlobalApplication) getApplication();
                    Pantry pantry = app.getCurrentRecipe().getIngredients();
                    pantry.removeIngredient(position);
                    refresh();
                  }
                });
            prompt.show();
            return false;
          }
        });
  }
 /**
  * 프로필 이미지에 대해 view를 update한다.
  *
  * @param profileImageURL 화면에 반영할 프로필 이미지
  */
 public void setProfileURL(final String profileImageURL) {
   this.profileImageURL = profileImageURL;
   if (profile != null && profileImageURL != null) {
     Application app = GlobalApplication.getGlobalApplicationContext();
     if (app == null)
       throw new UnsupportedOperationException(
           "needs com.kakao.GlobalApplication in order to use ImageLoader");
     profile.setImageUrl(profileImageURL, ((GlobalApplication) app).getImageLoader());
   }
 }
  /**
   * Function that is called when the user clicks on the delete all button. Deletes all of the
   * ingredients, and updates the pantry accordingly.
   */
  public void onDeleteAll() {
    GlobalApplication app = (GlobalApplication) getApplication();
    Pantry pantry = app.getCurrentRecipe().getIngredients();

    // If the ingredients is empty, we don't need to delete anything
    if (pantry.isEmpty()) {
      showMessage("Nothing to delete");
      return;
    }

    // Builds the alert dialog box
    AlertDialog.Builder prompt = new AlertDialog.Builder(this);
    prompt.setTitle("Delete All");
    prompt.setMessage(
        "Are you sure you want to delete all ingredients? They " + "will be gone... forever.");

    prompt.setNegativeButton(
        "No",
        new DialogInterface.OnClickListener() {

          // User has changed their mind
          @Override
          public void onClick(DialogInterface dialog, int which) {
            return;
          }
        });
    prompt.setPositiveButton(
        "Yes",
        new DialogInterface.OnClickListener() {

          // User does want to delete all ingredients
          @Override
          public void onClick(DialogInterface dialog, int which) {
            GlobalApplication app = (GlobalApplication) getApplication();
            Pantry pantry = app.getCurrentRecipe().getIngredients();
            pantry.emptyPantry();
            refresh();
          }
        });
    prompt.show();
    return;
  }
  /**
   * Function that is called when the user clicks the add ingredient button. Adds the ingredient to
   * the recipe. Also refreshes the view to show the new ingredient.
   */
  public void onAdd() {
    EditText quantity = (EditText) findViewById(R.id.addIngredientQuantity);
    EditText name = (EditText) findViewById(R.id.addIngredientName);
    Spinner units = (Spinner) findViewById(R.id.addIngredientMeasurement);

    // Get the units the user inputed
    int position = units.getSelectedItemPosition();
    String unitsString = Constants.getUnitFromPosition(position);
    String nameString = name.getText().toString();
    String quantityString = quantity.getText().toString();

    // Do the integer conversion like this just in case no number is entered
    Integer amount;
    try {
      amount = new Integer(quantityString);
    } catch (NumberFormatException e) {
      showMessage("Invalid number");
      return;
    }

    Ingredient ingredient = new Ingredient(nameString, amount, unitsString);

    // Check for ingredient validity
    if (ingredient.isValidInfo() != Constants.GOOD) {
      showMessage("Invalid info entered");
      return;
    }

    GlobalApplication app = (GlobalApplication) getApplication();
    Pantry pantry = app.getCurrentRecipe().getIngredients();
    pantry.addIngredient(ingredient);

    // Clear the edit text boxes for future ingredients
    name.setText("");
    quantity.setText("");

    refresh();

    return;
  }
  /*
   * (non-Javadoc)
   * @see android.app.IntentService#onHandleIntent(android.content.Intent)
   */
  @Override
  protected void onHandleIntent(Intent intent) {
    Context context = GlobalApplication.getGlobalAppContext();
    if (intent.getAction().equals(FileTransferService.ACTION_SEND_FILE)) {
      String host = intent.getExtras().getString(FileTransferService.EXTRAS_GROUP_OWNER_ADDRESS);
      String InetAddress = intent.getExtras().getString(FileTransferService.inetaddress);
      CommonMethods.e("LocalIp Received while first connect", "host address" + host);

      Socket socket = new Socket();
      int port = intent.getExtras().getInt(FileTransferService.EXTRAS_GROUP_OWNER_PORT);

      try {

        Log.d(WiFiDirectActivity.TAG, "Opening client socket for First tiime- ");
        socket.bind(null);
        socket.connect((new InetSocketAddress(host, port)), FileTransferService.SOCKET_TIMEOUT);
        Log.d(WiFiDirectActivity.TAG, "Client socket - " + socket.isConnected());
        OutputStream stream = socket.getOutputStream();
        ContentResolver cr = context.getContentResolver();
        InputStream is = null;

        /*
         * Object that is used to send file name with extension and recieved on other side.
         */
        ObjectOutputStream oos = new ObjectOutputStream(stream);
        WiFiTransferModal transObj = new WiFiTransferModal(InetAddress);

        oos.writeObject(transObj);
        System.out.println("Sending request to Socket Server");

        oos.close(); // close the ObjectOutputStream after sending data.
      } catch (IOException e) {
        Log.e(WiFiDirectActivity.TAG, e.getMessage());
        e.printStackTrace();
      } finally {
        if (socket != null) {
          if (socket.isConnected()) {
            try {
              CommonMethods.e("WiFiClientIP Service", "First Connection service socket closed");
              socket.close();
            } catch (Exception e) {
              // Give up
              e.printStackTrace();
            }
          }
        }
      }
    }
  }