Skip to content

hugorosario/transmission-android

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ABOUT

  Transmission is a fast, easy, and free BitTorrent client.
  Visit https://transmissionbt.com/ for more information.
  
  This repository is an Android port of the transmission daemon version 2.94.
  Modifications have been made to the source code of transmission and dependency 
  libraries in order to make this compile without errors.
  
  It provides a nice interactive build script and supports cross compiling to ARM and X86 android.

BUILDING
  
  Download and extract the latest Android NDK from https://developer.android.com/ndk/downloads/
  I already had revision 17c and it worked fine.
  
  Clone this repository.
  
  $ git clone https://github.com/hugorosario/transmission-android.git
  
  Edit the "build.sh" script inside the "builder" folder and set your NDK_ROOT directory to where you extracted it.
  For example, export NDK_ROOT=/home/dev/android-ndk
  
  Run the interactive build script.
  $ ./build.sh
  
  Select architecture (ARM or X86) to build the toolchain. 
  Select "Build all (dependencies + transmission)".
  
  If everything went good, you should have a transmission-daemon binary in your toolchain sysroot/usr/bin folder.

RUNNING
  
  Since this is a binary executable and not an APK, it needs to be launched from JAVA code.
  This means you need to build and APK to start/stop the daemon.
  
  In order to execute the daemon, you have to copy it to the app private space and make it executable.
  This is required because Android does not allow launching executables located on the SD card. 
  Here is some code that extracts the binary from the RAW folder into getFilesDir() location 
  and makes it executable:
  
  static boolean initialize(Context context) {
      try {
          initialized = false;
          String appFileDirectory = context.getFilesDir().getParent();
          binary = new File(appFileDirectory + "/transmissiond");
          extractResource(context, R.raw.transmission_x86, binary);
          if (binary.exists())
              initialized = binary.setExecutable(true);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return initialized;
  }

  static void extractResource(Context context, int id, File dest){
      try {
          InputStream ins = context.getResources().openRawResource(id);
          final byte[] buffer = new byte[ins.available()];
          ins.read(buffer);
          ins.close();
          if (dest.exists())
              dest.delete();
          FileOutputStream fos = new FileOutputStream(dest);
          fos.write(buffer);
          fos.close();
      }catch (Exception e){
          e.printStackTrace();
      }
  }
  
  Here is some code that might help launching the daemon (many parameters are missing from this sample code):
  
  public static void startDaemon(final TransmissionListener listener){
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                if (isInitialized()){
                    process = new ProcessBuilder().command(binary.getAbsolutePath(),"-f").redirectErrorStream(true).start();
                    listener.onStatusUpdated(false, "transmission-daemon started!");
                    Thread readThread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                                while (!Thread.currentThread().isInterrupted()) {
                                    if (bufferedReader.ready()) {
                                        String line = bufferedReader.readLine();
                                        listener.onLog(line);
                                    }
                                }
                                bufferedReader.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    readThread.start();
                    Thread errorThread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
                                while (!Thread.currentThread().isInterrupted()) {
                                    if (bufferedReader.ready()) {
                                        String line = bufferedReader.readLine();
                                        listener.onError(line);
                                    }
                                }
                                bufferedReader.close();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });
                    errorThread.start();
                    process.waitFor();
                    readThread.interrupt();
                    errorThread.interrupt();
                    process = null;
                    listener.onStatusUpdated(false, "transmission-daemon stopped!");
                }
            } catch (Exception e) {
                e.printStackTrace();
                listener.onStatusUpdated(false, "FATAL ERROR : " + e.getMessage());
            }
        }
    }).start();
}

  
  
TODO
  I will soon add a sample APK that starts/stops the daemon and has a nice helper class for transmission settings.