aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com/zeapo/pwdstore/git
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/com/zeapo/pwdstore/git')
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.kt14
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitActivity.kt31
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java27
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt44
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/PullOperation.kt10
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/PushOperation.kt2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt10
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.kt10
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/config/GitConfigSessionFactory.kt2
-rw-r--r--app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java175
10 files changed, 167 insertions, 158 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.kt
index 8489eb1d..53fbf77a 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.kt
@@ -10,7 +10,7 @@ import java.io.File
/**
* Creates a new clone operation
*
- * @param fileDir the git working tree directory
+ * @param fileDir the git working tree directory
* @param callingActivity the calling activity
*/
class CloneOperation(fileDir: File, callingActivity: Activity) : GitOperation(fileDir, callingActivity) {
@@ -44,8 +44,8 @@ class CloneOperation(fileDir: File, callingActivity: Activity) : GitOperation(fi
/**
* sets the authentication for the ssh-key scheme
*
- * @param sshKey the ssh-key file
- * @param username the username
+ * @param sshKey the ssh-key file
+ * @param username the username
* @param passphrase the passphrase
* @return the current object
*/
@@ -62,10 +62,10 @@ class CloneOperation(fileDir: File, callingActivity: Activity) : GitOperation(fi
override fun onError(errorMessage: String) {
MaterialAlertDialogBuilder(callingActivity)
.setTitle(callingActivity.resources.getString(R.string.jgit_error_dialog_title))
- .setMessage("Error occured during the clone operation, "
- + callingActivity.resources.getString(R.string.jgit_error_dialog_text)
- + errorMessage
- + "\nPlease check the FAQ for possible reasons why this error might occur.")
+ .setMessage("Error occured during the clone operation, " +
+ callingActivity.resources.getString(R.string.jgit_error_dialog_text) +
+ errorMessage +
+ "\nPlease check the FAQ for possible reasons why this error might occur.")
.setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ -> }
.show()
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.kt
index 11cc4c1b..403a4e12 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/GitActivity.kt
@@ -73,7 +73,6 @@ open class GitActivity : AppCompatActivity() {
}
override fun onNothingSelected(adapterView: AdapterView<*>) {
-
}
}
@@ -107,7 +106,6 @@ open class GitActivity : AppCompatActivity() {
}
override fun onNothingSelected(adapterView: AdapterView<*>) {
-
}
}
@@ -218,10 +216,10 @@ open class GitActivity : AppCompatActivity() {
if (uri != null) {
when (protocol) {
"ssh://" -> {
- var hostname = (serverUser.text.toString()
- + "@" +
- serverUrl.text.toString().trim { it <= ' ' }
- + ":")
+ var hostname = (serverUser.text.toString() +
+ "@" +
+ serverUrl.text.toString().trim { it <= ' ' } +
+ ":")
if (serverPort.text.toString() == "22") {
hostname += serverPath.text.toString()
@@ -258,7 +256,6 @@ open class GitActivity : AppCompatActivity() {
else -> {
}
}
-
}
}
@@ -420,7 +417,6 @@ open class GitActivity : AppCompatActivity() {
} catch (e: Exception) {
// ignore
}
-
}
}
@@ -477,8 +473,8 @@ open class GitActivity : AppCompatActivity() {
return
// Warn if non-empty folder unless it's a just-initialized store that has just a .git folder
- if (localDir.exists() && localDir.listFiles()!!.isNotEmpty()
- && !(localDir.listFiles()!!.size == 1 && localDir.listFiles()!![0].name == ".git")) {
+ if (localDir.exists() && localDir.listFiles()!!.isNotEmpty() &&
+ !(localDir.listFiles()!!.size == 1 && localDir.listFiles()!![0].name == ".git")) {
MaterialAlertDialogBuilder(this)
.setTitle(R.string.dialog_delete_title)
.setMessage(resources.getString(R.string.dialog_delete_msg) + " " + localDir.toString())
@@ -489,7 +485,7 @@ open class GitActivity : AppCompatActivity() {
FileUtils.deleteDirectory(localDir)
launchGitOperation(REQUEST_CLONE)
} catch (e: IOException) {
- //TODO Handle the exception correctly if we are unable to delete the directory...
+ // TODO Handle the exception correctly if we are unable to delete the directory...
e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show()
}
@@ -509,11 +505,10 @@ open class GitActivity : AppCompatActivity() {
e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show()
}
-
}
} catch (e: Exception) {
- //This is what happens when jgit fails :(
- //TODO Handle the diffent cases of exceptions
+ // This is what happens when jgit fails :(
+ // TODO Handle the diffent cases of exceptions
e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show()
}
@@ -612,11 +607,13 @@ open class GitActivity : AppCompatActivity() {
e.printStackTrace()
MaterialAlertDialogBuilder(this).setMessage(e.message).show()
}
-
}
- override fun onActivityResult(requestCode: Int, resultCode: Int,
- data: Intent?) {
+ override fun onActivityResult(
+ requestCode: Int,
+ resultCode: Int,
+ data: Intent?
+ ) {
// In addition to the pre-operation-launch series of intents for OpenKeychain auth
// that will pass through here and back to launchGitOperation, there is one
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java
index 0f0cf508..3dd1cd94 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.java
@@ -3,10 +3,9 @@ package com.zeapo.pwdstore.git;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
-
import com.zeapo.pwdstore.PasswordStore;
import com.zeapo.pwdstore.R;
-
+import java.lang.ref.WeakReference;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.GitCommand;
import org.eclipse.jgit.api.PullCommand;
@@ -17,9 +16,6 @@ import org.eclipse.jgit.api.StatusCommand;
import org.eclipse.jgit.transport.PushResult;
import org.eclipse.jgit.transport.RemoteRefUpdate;
-import java.lang.ref.WeakReference;
-
-
public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
private WeakReference<Activity> activityWeakReference;
private boolean finishOnEnd;
@@ -27,7 +23,11 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
private ProgressDialog dialog;
private GitOperation operation;
- public GitAsyncTask(Activity activity, boolean finishOnEnd, boolean refreshListOnEnd, GitOperation operation) {
+ public GitAsyncTask(
+ Activity activity,
+ boolean finishOnEnd,
+ boolean refreshListOnEnd,
+ GitOperation operation) {
this.activityWeakReference = new WeakReference<>(activity);
this.finishOnEnd = finishOnEnd;
this.refreshListOnEnd = refreshListOnEnd;
@@ -41,7 +41,8 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
}
protected void onPreExecute() {
- this.dialog.setMessage(getActivity().getResources().getString(R.string.running_dialog_text));
+ this.dialog.setMessage(
+ getActivity().getResources().getString(R.string.running_dialog_text));
this.dialog.setCancelable(false);
this.dialog.show();
}
@@ -58,8 +59,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
nbChanges = status.getChanged().size() + status.getMissing().size();
} else if (command instanceof CommitCommand) {
// the previous status will eventually be used to avoid a commit
- if (nbChanges == null || nbChanges > 0)
- command.call();
+ if (nbChanges == null || nbChanges > 0) command.call();
} else if (command instanceof PullCommand) {
final PullResult result = ((PullCommand) command).call();
final RebaseResult rr = result.getRebaseResult();
@@ -79,12 +79,14 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
case REJECTED_REMOTE_CHANGED:
case NON_EXISTING:
case NOT_ATTEMPTED:
- return activity.getString(R.string.git_push_generic_error) + rru.getStatus().name();
+ return activity.getString(R.string.git_push_generic_error)
+ + rru.getStatus().name();
case REJECTED_OTHER_REASON:
if ("non-fast-forward".equals(rru.getMessage())) {
return activity.getString(R.string.git_push_other_error);
} else {
- return activity.getString(R.string.git_push_generic_error) + rru.getMessage();
+ return activity.getString(R.string.git_push_generic_error)
+ + rru.getMessage();
}
default:
break;
@@ -111,8 +113,7 @@ public class GitAsyncTask extends AsyncTask<GitCommand, Integer, String> {
// ignore
}
- if (result == null)
- result = "Unexpected error";
+ if (result == null) result = "Unexpected error";
if (!result.isEmpty()) {
this.operation.onError(result);
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt
index 4ab90e96..e155bdd3 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/GitOperation.kt
@@ -29,7 +29,7 @@ import java.io.File
/**
* Creates a new git operation
*
- * @param fileDir the git working tree directory
+ * @param fileDir the git working tree directory
* @param callingActivity the calling activity
*/
abstract class GitOperation(fileDir: File, internal val callingActivity: Activity) {
@@ -54,8 +54,8 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit
/**
* Sets the authentication using ssh-key scheme
*
- * @param sshKey the ssh-key file
- * @param username the username
+ * @param sshKey the ssh-key file
+ * @param username the username
* @param passphrase the passphrase
* @return the current object
*/
@@ -87,14 +87,16 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit
* Executes the GitCommand in an async task after creating the authentication
*
* @param connectionMode the server-connection mode
- * @param username the username
- * @param sshKey the ssh-key file to use in ssh-key connection mode
- * @param identity the api identity to use for auth in OpenKeychain connection mode
+ * @param username the username
+ * @param sshKey the ssh-key file to use in ssh-key connection mode
+ * @param identity the api identity to use for auth in OpenKeychain connection mode
*/
- fun executeAfterAuthentication(connectionMode: String,
- username: String,
- sshKey: File?,
- identity: SshApiSessionFactory.ApiIdentity?) {
+ fun executeAfterAuthentication(
+ connectionMode: String,
+ username: String,
+ sshKey: File?,
+ identity: SshApiSessionFactory.ApiIdentity?
+ ) {
executeAfterAuthentication(connectionMode, username, sshKey, identity, false)
}
@@ -102,16 +104,18 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit
* Executes the GitCommand in an async task after creating the authentication
*
* @param connectionMode the server-connection mode
- * @param username the username
- * @param sshKey the ssh-key file to use in ssh-key connection mode
- * @param identity the api identity to use for auth in OpenKeychain connection mode
- * @param showError show the passphrase edit text in red
+ * @param username the username
+ * @param sshKey the ssh-key file to use in ssh-key connection mode
+ * @param identity the api identity to use for auth in OpenKeychain connection mode
+ * @param showError show the passphrase edit text in red
*/
- private fun executeAfterAuthentication(connectionMode: String,
- username: String,
- sshKey: File?,
- identity: SshApiSessionFactory.ApiIdentity?,
- showError: Boolean) {
+ private fun executeAfterAuthentication(
+ connectionMode: String,
+ username: String,
+ sshKey: File?,
+ identity: SshApiSessionFactory.ApiIdentity?,
+ showError: Boolean
+ ) {
if (connectionMode.equals("ssh-key", ignoreCase = true)) {
if (sshKey == null || !sshKey.exists()) {
MaterialAlertDialogBuilder(callingActivity)
@@ -199,7 +203,6 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit
.setPositiveButton("Ok") { _, _ -> callingActivity.finish() }
.show()
}
-
}
} else if (connectionMode.equals("OpenKeychain", ignoreCase = true)) {
setAuthentication(username, identity).execute()
@@ -216,7 +219,6 @@ abstract class GitOperation(fileDir: File, internal val callingActivity: Activit
.setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ ->
// authenticate using the user/pwd and then execute the command
setAuthentication(username, password.text.toString()).execute()
-
}
.setNegativeButton(callingActivity.resources.getString(R.string.dialog_cancel)) { _, _ ->
callingActivity.finish()
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.kt
index 90c46878..1ec8ec92 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/PullOperation.kt
@@ -10,7 +10,7 @@ import java.io.File
/**
* Creates a new git operation
*
- * @param fileDir the git working tree directory
+ * @param fileDir the git working tree directory
* @param callingActivity the calling activity
*/
class PullOperation(fileDir: File, callingActivity: Activity) : GitOperation(fileDir, callingActivity) {
@@ -36,10 +36,10 @@ class PullOperation(fileDir: File, callingActivity: Activity) : GitOperation(fil
override fun onError(errorMessage: String) {
MaterialAlertDialogBuilder(callingActivity)
.setTitle(callingActivity.resources.getString(R.string.jgit_error_dialog_title))
- .setMessage("Error occured during the pull operation, "
- + callingActivity.resources.getString(R.string.jgit_error_dialog_text)
- + errorMessage
- + "\nPlease check the FAQ for possible reasons why this error might occur.")
+ .setMessage("Error occured during the pull operation, " +
+ callingActivity.resources.getString(R.string.jgit_error_dialog_text) +
+ errorMessage +
+ "\nPlease check the FAQ for possible reasons why this error might occur.")
.setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ -> callingActivity.finish() }
.show()
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.kt
index 0015c4d1..55feadc3 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/PushOperation.kt
@@ -10,7 +10,7 @@ import java.io.File
/**
* Creates a new git operation
*
- * @param fileDir the git working tree directory
+ * @param fileDir the git working tree directory
* @param callingActivity the calling activity
*/
class PushOperation(fileDir: File, callingActivity: Activity) : GitOperation(fileDir, callingActivity) {
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt
index 1bab0981..b988d0af 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/ResetToRemoteOperation.kt
@@ -12,7 +12,7 @@ import java.io.File
/**
* Creates a new git operation
*
- * @param fileDir the git working tree directory
+ * @param fileDir the git working tree directory
* @param callingActivity the calling activity
*/
class ResetToRemoteOperation(fileDir: File, callingActivity: Activity) : GitOperation(fileDir, callingActivity) {
@@ -42,10 +42,10 @@ class ResetToRemoteOperation(fileDir: File, callingActivity: Activity) : GitOper
override fun onError(errorMessage: String) {
MaterialAlertDialogBuilder(callingActivity)
.setTitle(callingActivity.resources.getString(R.string.jgit_error_dialog_title))
- .setMessage("Error occured during the sync operation, "
- + "\nPlease check the FAQ for possible reasons why this error might occur."
- + callingActivity.resources.getString(R.string.jgit_error_dialog_text)
- + errorMessage)
+ .setMessage("Error occured during the sync operation, " +
+ "\nPlease check the FAQ for possible reasons why this error might occur." +
+ callingActivity.resources.getString(R.string.jgit_error_dialog_text) +
+ errorMessage)
.setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ -> }
.show()
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.kt b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.kt
index a0fd52b2..a795921d 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.kt
@@ -14,7 +14,7 @@ import java.io.File
/**
* Creates a new git operation
*
- * @param fileDir the git working tree directory
+ * @param fileDir the git working tree directory
* @param callingActivity the calling activity
*/
class SyncOperation(fileDir: File, callingActivity: Activity) : GitOperation(fileDir, callingActivity) {
@@ -50,10 +50,10 @@ class SyncOperation(fileDir: File, callingActivity: Activity) : GitOperation(fil
override fun onError(errorMessage: String) {
MaterialAlertDialogBuilder(callingActivity)
.setTitle(callingActivity.resources.getString(R.string.jgit_error_dialog_title))
- .setMessage("Error occured during the sync operation, "
- + "\nPlease check the FAQ for possible reasons why this error might occur."
- + callingActivity.resources.getString(R.string.jgit_error_dialog_text)
- + errorMessage)
+ .setMessage("Error occured during the sync operation, " +
+ "\nPlease check the FAQ for possible reasons why this error might occur." +
+ callingActivity.resources.getString(R.string.jgit_error_dialog_text) +
+ errorMessage)
.setPositiveButton(callingActivity.resources.getString(R.string.dialog_ok)) { _, _ -> callingActivity.finish() }
.show()
}
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/GitConfigSessionFactory.kt b/app/src/main/java/com/zeapo/pwdstore/git/config/GitConfigSessionFactory.kt
index 6485a73c..2ac11ab4 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/config/GitConfigSessionFactory.kt
+++ b/app/src/main/java/com/zeapo/pwdstore/git/config/GitConfigSessionFactory.kt
@@ -19,4 +19,4 @@ open class GitConfigSessionFactory : JschConfigSessionFactory() {
jsch.removeAllIdentity()
return jsch
}
-} \ No newline at end of file
+}
diff --git a/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java b/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java
index dbb5b7b1..7b2c062f 100644
--- a/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java
+++ b/app/src/main/java/com/zeapo/pwdstore/git/config/SshApiSessionFactory.java
@@ -4,9 +4,7 @@ import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
-
import androidx.annotation.NonNull;
-
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.jcraft.jsch.Identity;
import com.jcraft.jsch.JSch;
@@ -14,7 +12,8 @@ import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
import com.zeapo.pwdstore.R;
-
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
import org.eclipse.jgit.errors.UnsupportedCredentialItem;
import org.eclipse.jgit.transport.CredentialItem;
import org.eclipse.jgit.transport.CredentialsProvider;
@@ -33,15 +32,13 @@ import org.openintents.ssh.authentication.request.SigningRequest;
import org.openintents.ssh.authentication.request.SshPublicKeyRequest;
import org.openintents.ssh.authentication.util.SshAuthenticationApiUtils;
-import java.util.List;
-import java.util.concurrent.CountDownLatch;
-
public class SshApiSessionFactory extends GitConfigSessionFactory {
/**
* Intent request code indicating a completed signature that should be posted to an outstanding
* ApiIdentity
*/
public static final int POST_SIGNATURE = 301;
+
private String username;
private Identity identity;
@@ -52,7 +49,8 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
@NonNull
@Override
- protected JSch getJSch(@NonNull final OpenSshConfig.Host hc, @NonNull FS fs) throws JSchException {
+ protected JSch getJSch(@NonNull final OpenSshConfig.Host hc, @NonNull FS fs)
+ throws JSchException {
JSch jsch = super.getJSch(hc, fs);
jsch.removeAllIdentity();
jsch.addIdentity(identity, null);
@@ -64,36 +62,38 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey");
- CredentialsProvider provider = new CredentialsProvider() {
- @Override
- public boolean isInteractive() {
- return false;
- }
+ CredentialsProvider provider =
+ new CredentialsProvider() {
+ @Override
+ public boolean isInteractive() {
+ return false;
+ }
- @Override
- public boolean supports(CredentialItem... items) {
- return true;
- }
+ @Override
+ public boolean supports(CredentialItem... items) {
+ return true;
+ }
- @Override
- public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
- for (CredentialItem item : items) {
- if (item instanceof CredentialItem.Username) {
- ((CredentialItem.Username) item).setValue(username);
+ @Override
+ public boolean get(URIish uri, CredentialItem... items)
+ throws UnsupportedCredentialItem {
+ for (CredentialItem item : items) {
+ if (item instanceof CredentialItem.Username) {
+ ((CredentialItem.Username) item).setValue(username);
+ }
+ }
+ return true;
}
- }
- return true;
- }
- };
+ };
UserInfo userInfo = new CredentialsProviderUserInfo(session, provider);
session.setUserInfo(userInfo);
}
/**
* Helper to build up an ApiIdentity via the invocation of several pending intents that
- * communicate with OpenKeychain. The user of this class must handle onActivityResult and
- * keep feeding the resulting intents into the IdentityBuilder until it can successfully complete
- * the build.
+ * communicate with OpenKeychain. The user of this class must handle onActivityResult and keep
+ * feeding the resulting intents into the IdentityBuilder until it can successfully complete the
+ * build.
*/
public static class IdentityBuilder {
private SshAuthenticationConnection connection;
@@ -106,12 +106,14 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
* Construct a new IdentityBuilder
*
* @param callingActivity Activity that will be used to launch pending intents and that will
- * receive and handle the results.
+ * receive and handle the results.
*/
public IdentityBuilder(Activity callingActivity) {
this.callingActivity = callingActivity;
- List<String> providers = SshAuthenticationApiUtils.getAuthenticationProviderPackageNames(callingActivity);
+ List<String> providers =
+ SshAuthenticationApiUtils.getAuthenticationProviderPackageNames(
+ callingActivity);
if (providers.isEmpty())
throw new RuntimeException(callingActivity.getString(R.string.no_ssh_api_provider));
@@ -120,42 +122,44 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
connection = new SshAuthenticationConnection(callingActivity, providers.get(0));
}
- /**
- * Free any resources associated with this IdentityBuilder
- */
+ /** Free any resources associated with this IdentityBuilder */
public void close() {
- if (connection != null && connection.isConnected())
- connection.disconnect();
+ if (connection != null && connection.isConnected()) connection.disconnect();
}
/**
* Helper to invoke an OpenKeyshain SSH API method and correctly interpret the result.
*
- * @param request The request intent to launch
+ * @param request The request intent to launch
* @param requestCode The request code to use if a pending intent needs to be sent
* @return The resulting intent if the request completed immediately, or null if we had to
- * launch a pending intent to interact with the user
+ * launch a pending intent to interact with the user
*/
private Intent executeApi(Request request, int requestCode) {
Intent result = api.executeApi(request.toIntent());
switch (result.getIntExtra(SshAuthenticationApi.EXTRA_RESULT_CODE, -1)) {
case SshAuthenticationApi.RESULT_CODE_ERROR:
- SshAuthenticationApiError error = result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR);
+ SshAuthenticationApiError error =
+ result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR);
throw new RuntimeException(error.getMessage());
case SshAuthenticationApi.RESULT_CODE_SUCCESS:
break;
case SshAuthenticationApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
- PendingIntent pendingIntent = result.getParcelableExtra(SshAuthenticationApi.EXTRA_PENDING_INTENT);
+ PendingIntent pendingIntent =
+ result.getParcelableExtra(SshAuthenticationApi.EXTRA_PENDING_INTENT);
try {
- callingActivity.startIntentSenderForResult(pendingIntent.getIntentSender(), requestCode, null, 0, 0, 0);
+ callingActivity.startIntentSenderForResult(
+ pendingIntent.getIntentSender(), requestCode, null, 0, 0, 0);
return null;
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
- throw new RuntimeException(callingActivity.getString(R.string.ssh_api_pending_intent_failed));
+ throw new RuntimeException(
+ callingActivity.getString(R.string.ssh_api_pending_intent_failed));
}
default:
- throw new RuntimeException(callingActivity.getString(R.string.ssh_api_unknown_error));
+ throw new RuntimeException(
+ callingActivity.getString(R.string.ssh_api_unknown_error));
}
return result;
@@ -168,8 +172,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
* @param intent The intent to inspect
*/
public void consume(Intent intent) {
- if (intent == null)
- return;
+ if (intent == null) return;
if (intent.hasExtra(SshAuthenticationApi.EXTRA_KEY_ID)) {
keyId = intent.getStringExtra(SshAuthenticationApi.EXTRA_KEY_ID);
@@ -189,27 +192,31 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
*
* @param requestCode The request code to use if a pending intent needs to be sent
* @return The built identity, or null of user interaction is still required (in which case
- * a pending intent will have already been launched)
+ * a pending intent will have already been launched)
*/
public ApiIdentity tryBuild(int requestCode) {
// First gate, need to initiate a connection to the service and wait for it to connect.
if (api == null) {
- connection.connect(new SshAuthenticationConnection.OnBound() {
- @Override
- public void onBound(ISshAuthenticationService sshAgent) {
- api = new SshAuthenticationApi(callingActivity, sshAgent);
- // We can immediately try the next phase without needing to post back
- // though onActivityResult
- tryBuild(requestCode);
- }
-
- @Override
- public void onError() {
- new MaterialAlertDialogBuilder(callingActivity)
- .setMessage(callingActivity.getString(
- R.string.openkeychain_ssh_api_connect_fail)).show();
- }
- });
+ connection.connect(
+ new SshAuthenticationConnection.OnBound() {
+ @Override
+ public void onBound(ISshAuthenticationService sshAgent) {
+ api = new SshAuthenticationApi(callingActivity, sshAgent);
+ // We can immediately try the next phase without needing to post
+ // back
+ // though onActivityResult
+ tryBuild(requestCode);
+ }
+
+ @Override
+ public void onError() {
+ new MaterialAlertDialogBuilder(callingActivity)
+ .setMessage(
+ callingActivity.getString(
+ R.string.openkeychain_ssh_api_connect_fail))
+ .show();
+ }
+ });
return null;
}
@@ -218,8 +225,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
if (keyId == null) {
consume(executeApi(new KeySelectionRequest(), requestCode));
// If we did not immediately get the result, bail for now and wait to be re-entered
- if (keyId == null)
- return null;
+ if (keyId == null) return null;
}
// Third gate, need to get the public key for the selected key. This one often does not
@@ -227,8 +233,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
if (publicKey == null) {
consume(executeApi(new SshPublicKeyRequest(keyId), requestCode));
// If we did not immediately get the result, bail for now and wait to be re-entered
- if (publicKey == null)
- return null;
+ if (publicKey == null) return null;
}
// Have everything we need for now, build the identify
@@ -236,9 +241,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
}
}
- /**
- * A Jsch identity that delegates key operations via the OpenKeychain SSH API
- */
+ /** A Jsch identity that delegates key operations via the OpenKeychain SSH API */
public static class ApiIdentity implements Identity {
private String keyId, description, alg;
private byte[] publicKey;
@@ -247,7 +250,13 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
private CountDownLatch latch;
private byte[] signature;
- ApiIdentity(String keyId, String description, byte[] publicKey, String alg, Activity callingActivity, SshAuthenticationApi api) {
+ ApiIdentity(
+ String keyId,
+ String description,
+ byte[] publicKey,
+ String alg,
+ Activity callingActivity,
+ SshAuthenticationApi api) {
this.keyId = keyId;
this.description = description;
this.publicKey = publicKey;
@@ -271,33 +280,37 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
* Helper to handle the result of an OpenKeyshain SSH API signing request
*
* @param result The result intent to handle
- * @return The signed challenge, or null if it was not immediately available, in which
- * case the latch has been initialized and the pending intent started
+ * @return The signed challenge, or null if it was not immediately available, in which case
+ * the latch has been initialized and the pending intent started
*/
private byte[] handleSignResult(Intent result) {
switch (result.getIntExtra(SshAuthenticationApi.EXTRA_RESULT_CODE, -1)) {
case SshAuthenticationApi.RESULT_CODE_ERROR:
- SshAuthenticationApiError error = result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR);
+ SshAuthenticationApiError error =
+ result.getParcelableExtra(SshAuthenticationApi.EXTRA_ERROR);
throw new RuntimeException(error.getMessage());
case SshAuthenticationApi.RESULT_CODE_SUCCESS:
return result.getByteArrayExtra(SshAuthenticationApi.EXTRA_SIGNATURE);
case SshAuthenticationApi.RESULT_CODE_USER_INTERACTION_REQUIRED:
- PendingIntent pendingIntent = result.getParcelableExtra(SshAuthenticationApi.EXTRA_PENDING_INTENT);
+ PendingIntent pendingIntent =
+ result.getParcelableExtra(SshAuthenticationApi.EXTRA_PENDING_INTENT);
try {
latch = new CountDownLatch(1);
- callingActivity.startIntentSenderForResult(pendingIntent.getIntentSender(), POST_SIGNATURE, null, 0, 0, 0);
+ callingActivity.startIntentSenderForResult(
+ pendingIntent.getIntentSender(), POST_SIGNATURE, null, 0, 0, 0);
return null;
} catch (Exception e) {
e.printStackTrace();
- throw new RuntimeException(callingActivity.getString(R.string.ssh_api_pending_intent_failed));
+ throw new RuntimeException(
+ callingActivity.getString(R.string.ssh_api_pending_intent_failed));
}
default:
if (result.hasExtra(SshAuthenticationApi.EXTRA_CHALLENGE))
return handleSignResult(api.executeApi(result));
- throw new RuntimeException(callingActivity.getString(R.string.ssh_api_unknown_error));
+ throw new RuntimeException(
+ callingActivity.getString(R.string.ssh_api_unknown_error));
}
-
}
@Override
@@ -318,7 +331,6 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
return signature;
}
-
/**
* Post a signature response back to an in-progress operation using this ApiIdentity.
*
@@ -328,8 +340,7 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
try {
signature = handleSignResult(data);
} finally {
- if (latch != null)
- latch.countDown();
+ if (latch != null) latch.countDown();
}
}
@@ -354,8 +365,6 @@ public class SshApiSessionFactory extends GitConfigSessionFactory {
}
@Override
- public void clear() {
-
- }
+ public void clear() {}
}
}