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.
This commit is contained in:
Vishesh Handa
2018-10-15 23:37:47 +02:00
parent c0914ffe5f
commit b3432ccc4f
2 changed files with 117 additions and 4 deletions

View File

@ -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<String, Void, Void> {
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);
}
}

View File

@ -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());
}
}
*/
}