From b3432ccc4f7252effb32fee9ff5c8dce023fed7c Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 15 Oct 2018 23:37:47 +0200 Subject: [PATCH] Add a GitClone async task Also add a lot of misc code which was there from a previous spike. We need an AsyncTask as one cannot run network operations in the main thread. --- .../com/example/journal/GitCloneTask.java | 39 +++++++++ .../com/example/journal/MainActivity.java | 82 ++++++++++++++++++- 2 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 android/app/src/main/java/com/example/journal/GitCloneTask.java diff --git a/android/app/src/main/java/com/example/journal/GitCloneTask.java b/android/app/src/main/java/com/example/journal/GitCloneTask.java new file mode 100644 index 00000000..7ae4a716 --- /dev/null +++ b/android/app/src/main/java/com/example/journal/GitCloneTask.java @@ -0,0 +1,39 @@ +package com.example.journal; + +import android.os.AsyncTask; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; + +import java.io.File; + +import io.flutter.plugin.common.MethodChannel.Result; + +public class GitCloneTask extends AsyncTask { + private Result result; + + public GitCloneTask(Result _result) { + result = _result; + } + + protected Void doInBackground(String... params) { + String url = params[0]; + String filesDir = params[1]; + File directory = new File(filesDir + "/git"); + + try { + Git git = Git.cloneRepository() + .setURI(url) + .setDirectory(directory) + .call(); + } + catch (GitAPIException e) { + System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); + } + return null; + } + + protected void onPostExecute(Void taskResult) { + result.success(null); + } +} diff --git a/android/app/src/main/java/com/example/journal/MainActivity.java b/android/app/src/main/java/com/example/journal/MainActivity.java index e2d3a790..047f4ea2 100644 --- a/android/app/src/main/java/com/example/journal/MainActivity.java +++ b/android/app/src/main/java/com/example/journal/MainActivity.java @@ -3,6 +3,7 @@ package com.example.journal; import java.io.File; import android.os.Bundle; +import android.util.Log; import io.flutter.app.FlutterActivity; import io.flutter.plugins.GeneratedPluginRegistrant; @@ -13,6 +14,8 @@ import io.flutter.plugin.common.MethodChannel; import io.flutter.plugin.common.MethodChannel.MethodCallHandler; import io.flutter.plugin.common.MethodChannel.Result; +import io.flutter.util.PathUtils; + import org.eclipse.jgit.api.CloneCommand; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; @@ -38,8 +41,18 @@ public class MainActivity extends FlutterActivity { return; } - gitClone(cloneUrl, filePath); - result.success(null); + String filesDir = PathUtils.getFilesDir(getApplicationContext()); + Log.d("vhanda", filesDir); + String cloneLocation = filesDir + "/git"; + + new GitCloneTask(result).execute(cloneUrl, cloneLocation); + /* + if (gitClone(cloneUrl, filePath)) { + result.success(null); + } else { + result.error("UNAVAILABLE", "Battery level not available.", null); + } + */ return; } @@ -55,20 +68,81 @@ public class MainActivity extends FlutterActivity { }); } - private void gitClone(String url, String filePath) { + private boolean gitClone(String url, String filePath) { // TODO: Progress // TODO: Credentials // TODO: Handle errors! - File directory = new File(filePath); + + File directory = new File("/git"); try { Git git = Git.cloneRepository() .setURI(url) .setDirectory(directory) .call(); + return true; + } + catch (GitAPIException e) { + System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); + return false; + } + } + + /* + private void gitAdd(String gitRootUrl, String gitFileUrl) { + File directory = new File(gitRootUrl); + + try { + Git git = Git.open(directory); + + git.add() + .addFilepattern(gitFileUrl) + .call(); } catch (GitAPIException e) { System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); } + catch (java.io.IOException e) { + System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage()); + } } + + private void gitRemove(String gitRootUrl, String gitFileUrl) { + File directory = new File(gitRootUrl); + + try { + Git git = Git.open(directory); + + git.rm() + .addFilepattern(gitFileUrl) + .call(); + } + catch (GitAPIException e) { + System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); + } + catch (java.io.IOException e) { + System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage()); + } + } + + private void gitCommit(String gitRootUrl, String message) { + File directory = new File(gitRootUrl); + + try { + Git git = Git.open(directory); + + git.commit() + .setAuthor("JournalApp", "none@example.com") + .setMessage(message) + .call(); + } + catch (GitAPIException e) { + System.err.println("Error Cloning repository " + url + " : "+ e.getMessage()); + } + catch (java.io.IOException e) { + System.err.println("Error Opening GitRepo " + gitRootUrl + " : "+ e.getMessage()); + } + } + */ + }