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) {
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
*/
protected void pauseAndStartUserInteraction(String action, BaseCallback callback, Bundle extras) {
synchronized (userInputLock) {
mThreadPool.pause();
Log.d(Constants.TAG, "starting activity...");
Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(action);
Messenger messenger = new Messenger(new Handler(getMainLooper(), callback));
extras.putParcelable(RemoteServiceActivity.EXTRA_MESSENGER, messenger);
intent.putExtras(extras);
startActivity(intent);
// lock current thread for user input
try {
userInputLock.wait();
} catch (InterruptedException e) {
Log.e(Constants.TAG, "CryptoService", e);
}
}
}
// protected void pauseAndStartUserInteraction(String action, BaseCallback callback, Bundle extras) {
// synchronized (userInputLock) {
// mThreadPool.pause();
//
// Log.d(Constants.TAG, "starting activity...");
// Intent intent = new Intent(getBaseContext(), RemoteServiceActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setAction(action);
//
// Messenger messenger = new Messenger(new Handler(getMainLooper(), callback));
//
// extras.putParcelable(RemoteServiceActivity.EXTRA_MESSENGER, messenger);
// intent.putExtras(extras);
//
// startActivity(intent);
//
// // lock current thread for user input
// try {
// userInputLock.wait();
// } catch (InterruptedException e) {
// Log.e(Constants.TAG, "CryptoService", e);
// }
// }
// }
/**
* Retrieves AppSettings from database for the application calling this remote service
@ -216,59 +216,59 @@ public abstract class RemoteService extends Service {
return null;
}
class RegisterActivityCallback extends BaseCallback {
public static final String PACKAGE_NAME = "package_name";
private boolean allowed = false;
private String packageName;
public boolean isAllowed() {
return allowed;
}
public String getPackageName() {
return packageName;
}
@Override
public boolean handleMessage(Message msg) {
if (msg.arg1 == OKAY) {
allowed = true;
packageName = msg.getData().getString(PACKAGE_NAME);
// resume threads
try {
if (isPackageAllowed(packageName)) {
synchronized (userInputLock) {
userInputLock.notifyAll();
}
mThreadPool.resume();
} else {
// Should not happen!
Log.e(Constants.TAG, "Should not happen! Emergency shutdown!");
mThreadPool.shutdownNow();
}
} catch (WrongPackageSignatureException e) {
Log.e(Constants.TAG, e.getMessage());
Bundle extras = new Bundle();
extras.putString(RemoteServiceActivity.EXTRA_ERROR_MESSAGE,
getString(R.string.api_error_wrong_signature));
pauseAndStartUserInteraction(RemoteServiceActivity.ACTION_ERROR_MESSAGE, null,
extras);
}
} else {
allowed = false;
synchronized (userInputLock) {
userInputLock.notifyAll();
}
mThreadPool.resume();
}
return true;
}
}
// class RegisterActivityCallback extends BaseCallback {
// public static final String PACKAGE_NAME = "package_name";
//
// private boolean allowed = false;
// private String packageName;
//
// public boolean isAllowed() {
// return allowed;
// }
//
// public String getPackageName() {
// return packageName;
// }
//
// @Override
// public boolean handleMessage(Message msg) {
// if (msg.arg1 == OKAY) {
// allowed = true;
// packageName = msg.getData().getString(PACKAGE_NAME);
//
// // resume threads
// try {
// if (isPackageAllowed(packageName)) {
// synchronized (userInputLock) {
// userInputLock.notifyAll();
// }
// mThreadPool.resume();
// } else {
// // Should not happen!
// Log.e(Constants.TAG, "Should not happen! Emergency shutdown!");
// mThreadPool.shutdownNow();
// }
// } catch (WrongPackageSignatureException e) {
// Log.e(Constants.TAG, e.getMessage());
//
// Bundle extras = new Bundle();
// extras.putString(RemoteServiceActivity.EXTRA_ERROR_MESSAGE,
// getString(R.string.api_error_wrong_signature));
// pauseAndStartUserInteraction(RemoteServiceActivity.ACTION_ERROR_MESSAGE, null,
// extras);
// }
// } else {
// allowed = false;
//
// synchronized (userInputLock) {
// userInputLock.notifyAll();
// }
// mThreadPool.resume();
// }
// return true;
// }
//
// }
/**
* 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.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
@ -64,17 +63,11 @@ public class RemoteServiceActivity extends ActionBarActivity {
// error message
public static final String EXTRA_ERROR_MESSAGE = "error_message";
private Messenger mMessenger;
// register view
private AppSettingsFragment mSettingsFragment;
// select pub keys view
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -82,32 +75,12 @@ public class RemoteServiceActivity extends ActionBarActivity {
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) {
finishHandled = false;
String action = intent.getAction();
final Bundle extras = intent.getExtras();
mMessenger = extras.getParcelable(EXTRA_MESSENGER);
/**
* com.android.crypto actions
*/
if (ACTION_REGISTER.equals(action)) {
final String packageName = extras.getString(EXTRA_PACKAGE_NAME);
final byte[] packageSignature = extras.getByteArray(EXTRA_PACKAGE_SIGNATURE);
@ -127,37 +100,21 @@ public class RemoteServiceActivity extends ActionBarActivity {
ProviderHelper.insertApiApp(RemoteServiceActivity.this,
mSettingsFragment.getAppSettings());
Message msg = Message.obtain();
msg.arg1 = RemoteService.RegisterActivityCallback.OKAY;
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);
}
// give params through for new service call
Bundle oldParams = extras.getBundle(OpenPgpConstants.PI_RESULT_PARAMS);
finishHandled = true;
finish();
Intent finishIntent = new Intent();
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, oldParams);
RemoteServiceActivity.this.setResult(RESULT_OK, finishIntent);
RemoteServiceActivity.this.finish();
}
}
}, R.string.api_register_disallow, new View.OnClickListener() {
@Override
public void onClick(View v) {
// Disallow
Message msg = Message.obtain();
msg.arg1 = RemoteService.RegisterActivityCallback.CANCEL;
try {
mMessenger.send(msg);
} catch (RemoteException e) {
Log.e(Constants.TAG, "CryptoServiceActivity", e);
}
finishHandled = true;
finish();
RemoteServiceActivity.this.setResult(RESULT_CANCELED);
RemoteServiceActivity.this.finish();
}
}
);
@ -216,15 +173,15 @@ public class RemoteServiceActivity extends ActionBarActivity {
Intent finishIntent = new Intent();
finishIntent.putExtra(OpenPgpConstants.PI_RESULT_PARAMS, params);
setResult(RESULT_OK, finishIntent);
finish();
RemoteServiceActivity.this.setResult(RESULT_OK, finishIntent);
RemoteServiceActivity.this.finish();
}
}, R.string.btn_do_not_save, new View.OnClickListener() {
@Override
public void onClick(View v) {
// cancel
setResult(RESULT_CANCELED);
finish();
RemoteServiceActivity.this.setResult(RESULT_CANCELED);
RemoteServiceActivity.this.finish();
}
}
);
@ -265,8 +222,8 @@ public class RemoteServiceActivity extends ActionBarActivity {
@Override
public void onClick(View v) {
setResult(RESULT_OK);
finish();
RemoteServiceActivity.this.setResult(RESULT_OK);
RemoteServiceActivity.this.finish();
}
});
@ -276,7 +233,7 @@ public class RemoteServiceActivity extends ActionBarActivity {
HtmlTextView textView = (HtmlTextView) findViewById(R.id.api_app_error_message_text);
textView.setHtmlFromString(text);
} else {
Log.e(Constants.TAG, "Wrong action!");
Log.e(Constants.TAG, "Action does not exist!");
setResult(RESULT_CANCELED);
finish();
}