aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/GitAsyncTask.java
blob: 085798cecd43a486063a4e8465d5c5a3b5932e6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.zeapo.pwdstore;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.api.errors.TransportException;


public class GitAsyncTask extends AsyncTask<GitCommand, Integer, Integer> {
    private Activity activity;
    private boolean finishOnEnd;
    private ProgressDialog dialog;

    public GitAsyncTask(Activity activity, boolean finishOnEnd) {
        this.activity = activity;
        this.finishOnEnd = finishOnEnd;

        dialog = new ProgressDialog(this.activity);
    }

    protected void onPreExecute() {
        this.dialog.setMessage("Running command...");
        this.dialog.setCancelable(false);
        this.dialog.show();
    }

    @Override
    protected Integer doInBackground(GitCommand... cmd) {
        int count = cmd.length;
        Integer totalSize = 0;
        for (int i = 0; i < count; i++) {
            try {
                cmd[i].call();
            } catch (JGitInternalException e) {
                e.printStackTrace();
                return -99;
            } catch (InvalidRemoteException e) {
                e.printStackTrace();
                return -1;
            } catch (TransportException e) {
                e.printStackTrace();
                return -2;
            } catch (Exception e) {
                e.printStackTrace();
            }
            totalSize++;
        }
        return totalSize;
    }

    protected void onPostExecute(Integer result) {
        Log.i("GIT_ASYNC", result + "");
        this.dialog.dismiss();
        if (finishOnEnd) {
            this.activity.finish();
        }
    }
}