// Copyright 2013 Square, Inc.
package com.squareup.spoon;

import android.os.Build;
import java.io.File;
import java.io.IOException;

abstract class Chmod {
  private static final Chmod INSTANCE;
  static {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
      INSTANCE = new Java6Chmod();
    } else {
      INSTANCE = new Java5Chmod();
    }
  }

  static void chmodPlusR(File file) {
    INSTANCE.plusR(file);
  }

  static void chmodPlusRWX(File file) {
    INSTANCE.plusRWX(file);
  }

  protected abstract void plusR(File file);
  protected abstract void plusRWX(File file);

  private static class Java5Chmod extends Chmod {
    @Override protected void plusR(File file) {
      try {
        Runtime.getRuntime().exec("chmod +r \"" + file.getAbsolutePath() + "\"");
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }

    @Override protected void plusRWX(File file) {
      try {
        Runtime.getRuntime().exec("chmod +rwx \"" + file.getAbsolutePath() + "\"");
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  }

  private static class Java6Chmod extends Chmod {
    @Override protected void plusR(File file) {
      file.setReadable(true, false);
    }

    @Override protected void plusRWX(File file) {
      file.setReadable(true, false);
      file.setWritable(true, false);
      file.setExecutable(true, false);
    }
  }
}

@@ -744,23 +744,37 @@
 xec(
-%22chmod +r %5C%22%22 +
+new String%5B%5D %7B%22chmod%22, %22644%22,
  fil
@@ -784,39 +784,33 @@
 etAbsolutePath()
- + %22%5C%22%22
+%7D
 );%0A      %7D catch
@@ -982,25 +982,37 @@
 xec(
-%22chmod +rwx %5C%22%22 +
+new String%5B%5D %7B%22chmod%22, %22777%22,
  fil
@@ -1034,15 +1034,9 @@
 th()
- + %22%5C%22%22
+%7D
 );%0A 

Support chmod properly on Java 5.