aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/GitClone.java
blob: 6f57a9f707faacae31e5c5a38b0f4cced68af69d (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.zeapo.pwdstore;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;

import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.errors.InvalidRemoteException;
import org.eclipse.jgit.transport.JschConfigSessionFactory;
import org.eclipse.jgit.transport.OpenSshConfig;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.util.FS;

import java.io.File;
import java.io.IOException;


public class GitClone extends Activity {

    private Activity activity;
    private Context context;

    private String protocol;
    private String connectionMode;

    private File localDir;
    private String hostname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_git_clone);

        context = getApplicationContext();
        activity = this;

        // init the spinner for protocols
        Spinner protcol_spinner = (Spinner) findViewById(R.id.clone_protocol);
        ArrayAdapter<CharSequence> protocol_adapter = ArrayAdapter.createFromResource(this,
                R.array.clone_protocols, android.R.layout.simple_spinner_item);
        protocol_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        protcol_spinner.setAdapter(protocol_adapter);
        protcol_spinner.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        protocol = ((Spinner)findViewById(R.id.clone_protocol)).getSelectedItem().toString();
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                }
        );

        // init the spinner for connection modes
        Spinner connection_mode_spinner = (Spinner) findViewById(R.id.connection_mode);
        ArrayAdapter<CharSequence> connection_mode_adapter = ArrayAdapter.createFromResource(this,
                R.array.connection_modes, android.R.layout.simple_spinner_item);
        connection_mode_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        connection_mode_spinner.setAdapter(connection_mode_adapter);
        connection_mode_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                String selection = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString();

                if (selection.equalsIgnoreCase("ssh-key")) {
                    new AlertDialog.Builder(activity)
                            .setMessage("Authentication method not implemented yet")
                            .setPositiveButton("OK",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    }
                            ).show();
                    ((Button) findViewById(R.id.clone_button)).setEnabled(false);
                } else {
                    ((Button) findViewById(R.id.clone_button)).setEnabled(true);
                }
                connectionMode = selection;
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.git_clone, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /* The clone process has to be on a different thread than the main one */
    private class CloneTask extends AsyncTask<CloneCommand, Integer, Long> {
        private ProgressDialog dialog;

        public CloneTask(Activity activity) {
            context = activity;
            dialog = new ProgressDialog(context);
        }

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

        protected void onPostExecute(Long result) {
            if (result < 0) {
                new AlertDialog.Builder(activity).
                        setTitle("Invalid remote repository path").
                        setMessage("Please check that the repository path is correct.\nDid you forget to specify the path after the hostname?").
                        setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                            }
                        }).show();
            }
            this.dialog.dismiss();
        }


        protected Long doInBackground(CloneCommand... cmd) {
            int count = cmd.length;
            long totalSize = 0;
            for (int i = 0; i < count; i++) {
                try {
                    cmd[i].call();
                } catch (InvalidRemoteException e) {
                    return new Long(-1);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                totalSize++;
            }
            return totalSize;
        }
    }

    protected class GitConfigSessionFactory extends JschConfigSessionFactory {

        public void configure(OpenSshConfig.Host hc, Session session) {
            session.setConfig("StrictHostKeyChecking", "no");
        }

        @Override
        protected JSch
        getJSch(final OpenSshConfig.Host hc, FS fs) throws JSchException {
            JSch jsch = super.getJSch(hc, fs);
            jsch.removeAllIdentity();
            return jsch;
        }
    }


    public void cloneRepository(View view) {
        localDir = new File(getApplicationContext().getCacheDir().getAbsoluteFile() + "/store");

        hostname = ((TextView) findViewById(R.id.clone_uri)).getText().toString();
        // don't ask the user, take off the protocol that he puts in
        hostname = hostname.replaceFirst("^.+://", "");
        ((TextView) findViewById(R.id.clone_uri)).setText(hostname);

        // now cheat a little and prepend the real protocol
        // jGit does not accept a ssh:// but requires https://
        if (!protocol.equals("ssh://"))  hostname = protocol + hostname;

        if (localDir.exists()) {
            new AlertDialog.Builder(this).
                    setTitle(R.string.dialog_delete_title).
                    setMessage(R.string.dialog_delete_msg).
                    setCancelable(false).
                    setPositiveButton(R.string.dialog_delete,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    try {
                                        FileUtils.deleteDirectory(localDir);
                                        authenticateThenClone(localDir);
                                    } catch (IOException e) {
                                        //TODO Handle the exception correctly if we are unable to delete the directory...
                                        e.printStackTrace();
                                    } catch (Exception e) {
                                        //This is what happens when jgit fails :(
                                        //TODO Handle the diffent cases of exceptions
                                    }

                                    dialog.cancel();
                                }
                            }
                    ).
                    setNegativeButton(R.string.dialog_do_not_delete,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            }
                    ).
                    show();
        } else {
            try {
                authenticateThenClone(localDir);
            } catch (Exception e) {
                //This is what happens when jgit fails :(
                //TODO Handle the diffent cases of exceptions
                e.printStackTrace();
            }
        }
    }


    private void authenticateThenClone(final File localDir) {
        String connectionMode = ((Spinner) findViewById(R.id.connection_mode)).getSelectedItem().toString();

        if (connectionMode.equalsIgnoreCase("ssh-key")) {

        } else {
            // Set an EditText view to get user input
            final LinearLayout layout = new LinearLayout(activity);
            layout.setOrientation(LinearLayout.VERTICAL);

            final EditText username = new EditText(activity);
            username.setHint("Username");
            username.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);

            final EditText password = new EditText(activity);
            password.setHint("Password");
            password.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
            password.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);

            layout.addView(username);
            layout.addView(password);

            new AlertDialog.Builder(activity)
                    .setTitle("Authenticate")
                    .setMessage("Please provide your usename and password for this repository")
                    .setView(layout)
                    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {

                            SshSessionFactory.setInstance(new GitConfigSessionFactory());

                            CloneCommand cmd = Git.cloneRepository().
                                    setCredentialsProvider(new UsernamePasswordCredentialsProvider("git", "nicomint")).
                                    setCloneAllBranches(true).
                                    setDirectory(localDir).
                                    setURI(hostname);

                            new CloneTask(activity).execute(cmd);
                        }
                    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    // Do nothing.
                }
            }).show();
        }
    }



}