Skip to content

Commit

Permalink
Call startService() in onServiceConnected() to fix callback being called
Browse files Browse the repository at this point in the history
before it has been set (such as on the error condition of no key
present).
Set empty result in handleError() to have app shutdown correctly.
Use handleError() to close out app on USB permission denied.
  • Loading branch information
zbrowning committed Apr 23, 2018
1 parent 2df4e62 commit d407f0c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
11 changes: 5 additions & 6 deletions app/src/main/java/to/crp/android/onlykeyu2f/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public void onServiceConnected(final ComponentName name, final IBinder service)
myService = binder.getService();
bound = true;
myService.setCallback(MainActivity.this); // register

final Intent intent = new Intent(MainActivity.this, OKService.class);
intent.putExtra(Intent.EXTRA_INTENT, getIntent());
startService(intent);
}

@Override
Expand Down Expand Up @@ -64,14 +68,9 @@ protected void onStart() {
return;
}

Intent intent = new Intent(this, OKService.class);
final Intent intent = new Intent(this, OKService.class);
bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

intent = new Intent(this, OKService.class);
intent.putExtra(Intent.EXTRA_INTENT, getIntent());

startService(intent);

moveTaskToBack(false);
}

Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/to/crp/android/onlykeyu2f/OKService.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ public void onReceive(Context context, Intent intent) {
handleError(ioe);
}
} else {
toastLong(getString(R.string.msg_perm_denied));
Log.d(TAG, "Permission denied.");
handleError(new Exception(getString(R.string.err_perm_denied)));
}
}
}
Expand Down Expand Up @@ -150,6 +149,11 @@ private void addOnlyKey() throws IOException {
private void handleError(final Exception e) {
toastLong(getString(R.string.msg_error) + ": " + e.getMessage());
Log.e(TAG, e.getMessage(), e);

if (callback != null) {
callback.resultData("");
}

stopSelf();
}

Expand Down
7 changes: 3 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<resources>
<string name="app_name">OnlyKey U2F</string>
<string name="err_perm_denied">Permission denied!</string>
<string name="err_no_onlykey">No OnlyKey present.</string>
<string name="launch_err">"OnlyKey U2F cannot be launched directly."</string>
<string name="msg_attach_onlykey">Attach OnlyKey.</string>
<string name="msg_dev_locked">OnlyKey is locked.</string>
<string name="msg_dev_unlocked">OnlyKey is unlocked.</string>
<string name="msg_error">Error!</string>
<string name="msg_no_onlykey">No OnlyKey present.</string>
<string name="msg_error">OnlyKey Error!</string>
<string name="msg_ok_attached">OnlyKey attached.</string>
<string name="msg_ok_detached">OnlyKey detached.</string>
<string name="msg_perm_denied">OnlyKey: permission denied!</string>
<string name="msg_setup_required">OnlyKey setup required!</string>
<string name="msg_set_time">OnlyKey time set!</string>
<string name="msg_waiting_for_unlock">Waiting for OnlyKey unlock&#8230;</string>
Expand Down

0 comments on commit d407f0c

Please sign in to comment.