Fix register activity

This commit is contained in:
Dominik Schürmann 2014-02-17 19:06:29 +01:00
parent c4213e8d7a
commit f3bbd34482
3 changed files with 92 additions and 136 deletions

View file

@ -192,7 +192,6 @@ public class OpenPgpProviderActivity extends Activity {
} }
} }
public void sign(Bundle params) { public void sign(Bundle params) {
params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true); params.putBoolean(OpenPgpConstants.PARAMS_REQUEST_ASCII_ARMOR, true);

View file

@ -168,30 +168,30 @@ public abstract class RemoteService extends Service {
/** /**
* Locks current thread and pauses execution of runnables and starts activity for user input * Locks current thread and pauses execution of runnables and starts activity for user input
*/ */
protected void pauseAndStartUserInteraction(String action, BaseCallback callback, Bundle extras) { // protected void pauseAndStartUserInteraction(String action, BaseCallback callback, Bundle extras) {
synchronized (userInputLock) { // synchronized (userInputLock) {
mThreadPool.pause(); // mThreadPool.pause();
//
Log.d(Constants.TAG, "starting activity..."); // Log.d(Constants.TAG, "starting activity...");
Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class); // Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(action); // intent.setAction(action);
//
Messenger messenger = new Messenger(new Handler(getMainLooper(), callback)); // Messenger messenger = new Messenger(new Handler(getMainLooper(), callback));
//
extras.putParcelable(RemoteServiceActivity.EXTRA_MESSENGER, messenger); // extras.putParcelable(RemoteServiceActivity.EXTRA_MESSENGER, messenger);
intent.putExtras(extras); // intent.putExtras(extras);
//
startActivity(intent); // startActivity(intent);
//
// lock current thread for user input // // lock current thread for user input
try { // try {
userInputLock.wait(); // userInputLock.wait();
} catch (InterruptedException e) { // } catch (InterruptedException e) {
Log.e(Constants.TAG, "CryptoService", e); // Log.e(Constants.TAG, "CryptoService", e);
} // }
} // }
} // }
/** /**
* Retrieves AppSettings from database for the application calling this remote service * Retrieves AppSettings from database for the application calling this remote service
@ -216,59 +216,59 @@ public abstract class RemoteService extends Service {
return null; return null;
} }
class RegisterActivityCallback extends BaseCallback { // class RegisterActivityCallback extends BaseCallback {
public static final String PACKAGE_NAME = "package_name"; // public static final String PACKAGE_NAME = "package_name";
//
private boolean allowed = false; // private boolean allowed = false;
private String packageName; // private String packageName;
//
public boolean isAllowed() { // public boolean isAllowed() {
return allowed; // return allowed;
} // }
//
public String getPackageName() { // public String getPackageName() {
return packageName; // return packageName;
} // }
//
@Override // @Override
public boolean handleMessage(Message msg) { // public boolean handleMessage(Message msg) {
if (msg.arg1 == OKAY) { // if (msg.arg1 == OKAY) {
allowed = true; // allowed = true;
packageName = msg.getData().getString(PACKAGE_NAME); // packageName = msg.getData().getString(PACKAGE_NAME);
//
// resume threads // // resume threads
try { // try {
if (isPackageAllowed(packageName)) { // if (isPackageAllowed(packageName)) {
synchronized (userInputLock) { // synchronized (userInputLock) {
userInputLock.notifyAll(); // userInputLock.notifyAll();
} // }
mThreadPool.resume(); // mThreadPool.resume();
} else { // } else {
// Should not happen! // // Should not happen!
Log.e(Constants.TAG, "Should not happen! Emergency shutdown!"); // Log.e(Constants.TAG, "Should not happen! Emergency shutdown!");
mThreadPool.shutdownNow(); // mThreadPool.shutdownNow();
} // }
} catch (WrongPackageSignatureException e) { // } catch (WrongPackageSignatureException e) {
Log.e(Constants.TAG, e.getMessage()); // Log.e(Constants.TAG, e.getMessage());
//
Bundle extras = new Bundle(); // Bundle extras = new Bundle();
extras.putString(RemoteServiceActivity.EXTRA_ERROR_MESSAGE, // extras.putString(RemoteServiceActivity.EXTRA_ERROR_MESSAGE,
getString(R.string.api_error_wrong_signature)); // getString(R.string.api_error_wrong_signature));
pauseAndStartUserInteraction(RemoteServiceActivity.ACTION_ERROR_MESSAGE, null, // pauseAndStartUserInteraction(RemoteServiceActivity.ACTION_ERROR_MESSAGE, null,
extras); // extras);
} // }
} else { // } else {
allowed = false; // allowed = false;
//
synchronized (userInputLock) { // synchronized (userInputLock) {
userInputLock.notifyAll(); // userInputLock.notifyAll();
} // }
mThreadPool.resume(); // mThreadPool.resume();
} // }
return true; // return true;
} // }
//
} // }
/** /**
* Checks if process that binds to this service (i.e. the package name corresponding to the * Checks if process that binds to this service (i.e. the package name corresponding to the

View file

@ -22,7 +22,6 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.Message; import android.os.Message;
import android.os.Messenger; import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBarActivity;
import android.view.View; import android.view.View;
@ -64,17 +63,11 @@ public class RemoteServiceActivity extends ActionBarActivity {
// error message // error message
public static final String EXTRA_ERROR_MESSAGE = "error_message"; public static final String EXTRA_ERROR_MESSAGE = "error_message";
private Messenger mMessenger;
// register view // register view
private AppSettingsFragment mSettingsFragment; private AppSettingsFragment mSettingsFragment;
// select pub keys view // select pub keys view
private SelectPublicKeyFragment mSelectFragment; private SelectPublicKeyFragment mSelectFragment;
// has the user clicked one of the buttons
// or do we need to handle the callback in onStop()
private boolean finishHandled;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -82,32 +75,12 @@ public class RemoteServiceActivity extends ActionBarActivity {
handleActions(getIntent(), savedInstanceState); handleActions(getIntent(), savedInstanceState);
} }
@Override
protected void onStop() {
super.onStop();
if (!finishHandled && mMessenger != null) {
Message msg = Message.obtain();
msg.arg1 = RemoteService.RegisterActivityCallback.CANCEL;
try {
mMessenger.send(msg);
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoServiceActivity", e);
}
}
}
protected void handleActions(Intent intent, Bundle savedInstanceState) { protected void handleActions(Intent intent, Bundle savedInstanceState) {
finishHandled = false;
String action = intent.getAction(); String action = intent.getAction();
final Bundle extras = intent.getExtras(); final Bundle extras = intent.getExtras();
mMessenger = extras.getParcelable(EXTRA_MESSENGER);
/**
* com.android.crypto actions
*/
if (ACTION_REGISTER.equals(action)) { if (ACTION_REGISTER.equals(action)) {
final String packageName = extras.getString(EXTRA_PACKAGE_NAME); final String packageName = extras.getString(EXTRA_PACKAGE_NAME);
final byte[] packageSignature = extras.getByteArray(EXTRA_PACKAGE_SIGNATURE); final byte[] packageSignature = extras.getByteArray(EXTRA_PACKAGE_SIGNATURE);
@ -127,37 +100,21 @@ public class RemoteServiceActivity extends ActionBarActivity {
ProviderHelper.insertApiApp(RemoteServiceActivity.this, ProviderHelper.insertApiApp(RemoteServiceActivity.this,
mSettingsFragment.getAppSettings()); mSettingsFragment.getAppSettings());
Message msg = Message.obtain(); // give params through for new service call
msg.arg1 = RemoteService.RegisterActivityCallback.OKAY; Bundle oldParams = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS);
Bundle data = new Bundle();
data.putString(RemoteService.RegisterActivityCallback.PACKAGE_NAME,
packageName);
msg.setData(data);
try {
mMessenger.send(msg);
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoServiceActivity", e);
}
finishHandled = true; Intent finishIntent = new Intent();
finish(); finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, oldParams);
RemoteServiceActivity.this.setResult(RESULT_OK, finishIntent);
RemoteServiceActivity.this.finish();
} }
} }
}, R.string.api_register_disallow, new View.OnClickListener() { }, R.string.api_register_disallow, new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// Disallow // Disallow
RemoteServiceActivity.this.setResult(RESULT_CANCELED);
Message msg = Message.obtain(); RemoteServiceActivity.this.finish();
msg.arg1 = RemoteService.RegisterActivityCallback.CANCEL;
try {
mMessenger.send(msg);
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoServiceActivity", e);
}
finishHandled = true;
finish();
} }
} }
); );
@ -216,15 +173,15 @@ public class RemoteServiceActivity extends ActionBarActivity {
Intent finishIntent = new Intent(); Intent finishIntent = new Intent();
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params); finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params);
setResult(RESULT_OK, finishIntent); RemoteServiceActivity.this.setResult(RESULT_OK, finishIntent);
finish(); RemoteServiceActivity.this.finish();
} }
}, R.string.btn_do_not_save, new View.OnClickListener() { }, R.string.btn_do_not_save, new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// cancel // cancel
setResult(RESULT_CANCELED); RemoteServiceActivity.this.setResult(RESULT_CANCELED);
finish(); RemoteServiceActivity.this.finish();
} }
} }
); );
@ -265,8 +222,8 @@ public class RemoteServiceActivity extends ActionBarActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
setResult(RESULT_OK); RemoteServiceActivity.this.setResult(RESULT_OK);
finish(); RemoteServiceActivity.this.finish();
} }
}); });
@ -276,7 +233,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
HtmlTextView textView = (HtmlTextView) findViewById(R.id.api_app_error_message_text); HtmlTextView textView = (HtmlTextView) findViewById(R.id.api_app_error_message_text);
textView.setHtmlFromString(text); textView.setHtmlFromString(text);
} else { } else {
Log.e(Constants.TAG, "Wrong action!"); Log.e(Constants.TAG, "Action does not exist!");
setResult(RESULT_CANCELED); setResult(RESULT_CANCELED);
finish(); finish();
} }