2013-06-17 15:52:09 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2013 Dominik Schürmann <dominik@dominikschuermann.de>
|
|
|
|
|
*
|
2013-09-06 16:17:01 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
2013-06-17 15:52:09 +02:00
|
|
|
*
|
2013-09-06 16:17:01 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2013-06-17 15:52:09 +02:00
|
|
|
*
|
2013-09-06 16:17:01 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2013-06-17 15:52:09 +02:00
|
|
|
*/
|
|
|
|
|
|
2013-07-01 23:23:53 +02:00
|
|
|
package org.sufficientlysecure.keychain.remote_api;
|
2013-05-28 15:10:36 +02:00
|
|
|
|
|
|
|
|
import org.sufficientlysecure.keychain.Constants;
|
2013-09-06 10:03:25 +02:00
|
|
|
import org.sufficientlysecure.keychain.Id;
|
2013-05-28 15:10:36 +02:00
|
|
|
import org.sufficientlysecure.keychain.R;
|
2013-09-08 15:30:05 +02:00
|
|
|
import org.sufficientlysecure.keychain.helper.ActionBarHelper;
|
2013-05-28 15:10:36 +02:00
|
|
|
import org.sufficientlysecure.keychain.helper.PgpMain;
|
2013-06-17 16:59:27 +02:00
|
|
|
import org.sufficientlysecure.keychain.provider.ProviderHelper;
|
2013-09-08 17:04:33 +02:00
|
|
|
import org.sufficientlysecure.keychain.ui.SelectPublicKeyFragment;
|
2013-05-28 15:10:36 +02:00
|
|
|
import org.sufficientlysecure.keychain.ui.dialog.PassphraseDialogFragment;
|
|
|
|
|
import org.sufficientlysecure.keychain.util.Log;
|
|
|
|
|
|
2013-06-17 15:52:09 +02:00
|
|
|
import android.content.ComponentName;
|
|
|
|
|
import android.content.Context;
|
2013-05-28 15:10:36 +02:00
|
|
|
import android.content.Intent;
|
2013-06-17 15:52:09 +02:00
|
|
|
import android.content.ServiceConnection;
|
2013-05-28 15:10:36 +02:00
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Handler;
|
2013-06-17 15:52:09 +02:00
|
|
|
import android.os.IBinder;
|
2013-05-28 15:10:36 +02:00
|
|
|
import android.os.Message;
|
|
|
|
|
import android.os.Messenger;
|
2013-07-25 16:25:20 +02:00
|
|
|
import android.os.RemoteException;
|
2013-05-28 15:10:36 +02:00
|
|
|
import android.view.View;
|
2013-09-06 10:03:25 +02:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import com.actionbarsherlock.app.SherlockFragmentActivity;
|
2013-05-28 15:10:36 +02:00
|
|
|
|
2013-09-06 11:55:08 +02:00
|
|
|
public class CryptoServiceActivity extends SherlockFragmentActivity {
|
2013-05-28 15:10:36 +02:00
|
|
|
|
2013-09-08 15:30:05 +02:00
|
|
|
public static final String ACTION_REGISTER = Constants.INTENT_PREFIX + "API_ACTIVITY_REGISTER";
|
|
|
|
|
public static final String ACTION_CACHE_PASSPHRASE = Constants.INTENT_PREFIX
|
|
|
|
|
+ "API_ACTIVITY_CACHE_PASSPHRASE";
|
|
|
|
|
public static final String ACTION_SELECT_PUB_KEYS = Constants.INTENT_PREFIX
|
|
|
|
|
+ "API_ACTIVITY_SELECT_PUB_KEYS";
|
2013-05-28 15:10:36 +02:00
|
|
|
|
2013-06-17 15:52:09 +02:00
|
|
|
public static final String EXTRA_SECRET_KEY_ID = "secretKeyId";
|
|
|
|
|
public static final String EXTRA_PACKAGE_NAME = "packageName";
|
2013-09-08 17:04:33 +02:00
|
|
|
public static final String EXTRA_SELECTED_MASTER_KEY_IDS = "masterKeyIds";
|
2013-06-17 15:52:09 +02:00
|
|
|
|
2013-07-25 16:25:20 +02:00
|
|
|
private IServiceActivityCallback mServiceCallback;
|
2013-06-17 15:52:09 +02:00
|
|
|
private boolean mServiceBound;
|
|
|
|
|
|
2013-09-08 15:30:05 +02:00
|
|
|
// register view
|
2013-09-08 17:04:33 +02:00
|
|
|
AppSettingsFragment mSettingsFragment;
|
|
|
|
|
// select pub key view
|
|
|
|
|
SelectPublicKeyFragment mSelectFragment;
|
2013-09-06 10:03:25 +02:00
|
|
|
|
2013-06-17 15:52:09 +02:00
|
|
|
private ServiceConnection mServiceActivityConnection = new ServiceConnection() {
|
|
|
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
2013-07-25 16:25:20 +02:00
|
|
|
mServiceCallback = IServiceActivityCallback.Stub.asInterface(service);
|
2013-06-17 15:52:09 +02:00
|
|
|
Log.d(Constants.TAG, "connected to ICryptoServiceActivity");
|
|
|
|
|
mServiceBound = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onServiceDisconnected(ComponentName name) {
|
2013-07-25 16:25:20 +02:00
|
|
|
mServiceCallback = null;
|
2013-06-17 15:52:09 +02:00
|
|
|
Log.d(Constants.TAG, "disconnected from ICryptoServiceActivity");
|
|
|
|
|
mServiceBound = false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If not already bound, bind!
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public boolean bindToService() {
|
2013-07-25 16:25:20 +02:00
|
|
|
if (mServiceCallback == null && !mServiceBound) { // if not already connected
|
2013-06-17 15:52:09 +02:00
|
|
|
try {
|
|
|
|
|
Log.d(Constants.TAG, "not bound yet");
|
|
|
|
|
|
|
|
|
|
Intent serviceIntent = new Intent();
|
2013-09-06 11:55:08 +02:00
|
|
|
serviceIntent
|
|
|
|
|
.setAction("org.sufficientlysecure.keychain.crypto_provider.IServiceActivityCallback");
|
2013-06-17 15:52:09 +02:00
|
|
|
bindService(serviceIntent, mServiceActivityConnection, Context.BIND_AUTO_CREATE);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.d(Constants.TAG, "Exception", e);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else { // already connected
|
|
|
|
|
Log.d(Constants.TAG, "already bound... ");
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void unbindFromService() {
|
|
|
|
|
unbindService(mServiceActivityConnection);
|
|
|
|
|
}
|
2013-05-28 15:10:36 +02:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
|
2013-06-17 15:52:09 +02:00
|
|
|
Log.d(Constants.TAG, "onCreate…");
|
|
|
|
|
|
|
|
|
|
// bind to our own crypto service
|
|
|
|
|
bindToService();
|
|
|
|
|
|
2013-09-08 17:04:33 +02:00
|
|
|
handleActions(getIntent(), savedInstanceState);
|
2013-05-28 15:10:36 +02:00
|
|
|
}
|
|
|
|
|
|
2013-06-17 15:52:09 +02:00
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
2013-05-28 15:10:36 +02:00
|
|
|
|
2013-06-17 15:52:09 +02:00
|
|
|
// unbind from our crypto service
|
|
|
|
|
if (mServiceActivityConnection != null) {
|
|
|
|
|
unbindFromService();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-28 15:10:36 +02:00
|
|
|
|
2013-09-08 17:04:33 +02:00
|
|
|
protected void handleActions(Intent intent, Bundle savedInstanceState) {
|
2013-05-28 15:10:36 +02:00
|
|
|
String action = intent.getAction();
|
|
|
|
|
Bundle extras = intent.getExtras();
|
|
|
|
|
|
|
|
|
|
if (extras == null) {
|
|
|
|
|
extras = new Bundle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* com.android.crypto actions
|
|
|
|
|
*/
|
2013-06-17 15:52:09 +02:00
|
|
|
if (ACTION_REGISTER.equals(action)) {
|
|
|
|
|
final String packageName = extras.getString(EXTRA_PACKAGE_NAME);
|
|
|
|
|
|
2013-09-06 09:44:56 +02:00
|
|
|
// Inflate a "Done"/"Cancel" custom action bar view
|
2013-09-08 15:30:05 +02:00
|
|
|
ActionBarHelper.setDoneCancelView(getSupportActionBar(), R.string.api_register_allow,
|
2013-09-06 09:44:56 +02:00
|
|
|
new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
// Allow
|
|
|
|
|
|
2013-09-06 12:34:06 +02:00
|
|
|
// user needs to select a key!
|
2013-09-08 17:04:33 +02:00
|
|
|
if (mSettingsFragment.getAppSettings().getKeyId() == Id.key.none) {
|
2013-09-06 11:55:08 +02:00
|
|
|
Toast.makeText(CryptoServiceActivity.this,
|
2013-09-06 10:03:25 +02:00
|
|
|
R.string.api_register_error_select_key, Toast.LENGTH_LONG)
|
|
|
|
|
.show();
|
|
|
|
|
} else {
|
2013-09-06 12:34:06 +02:00
|
|
|
ProviderHelper.insertApiApp(CryptoServiceActivity.this,
|
2013-09-08 17:04:33 +02:00
|
|
|
mSettingsFragment.getAppSettings());
|
2013-09-06 10:03:25 +02:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
mServiceCallback.onRegistered(true, packageName);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(Constants.TAG, "ServiceActivity");
|
|
|
|
|
}
|
|
|
|
|
finish();
|
2013-09-06 09:44:56 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-09-08 15:30:05 +02:00
|
|
|
}, R.string.api_register_disallow, new View.OnClickListener() {
|
2013-09-06 09:44:56 +02:00
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
// Disallow
|
2013-09-08 16:08:36 +02:00
|
|
|
|
2013-09-06 09:44:56 +02:00
|
|
|
try {
|
|
|
|
|
mServiceCallback.onRegistered(false, packageName);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(Constants.TAG, "ServiceActivity");
|
|
|
|
|
}
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2013-09-05 00:27:17 +02:00
|
|
|
setContentView(R.layout.api_app_register_activity);
|
|
|
|
|
|
2013-09-08 17:04:33 +02:00
|
|
|
mSettingsFragment = (AppSettingsFragment) getSupportFragmentManager().findFragmentById(
|
2013-09-06 10:03:25 +02:00
|
|
|
R.id.api_app_settings_fragment);
|
|
|
|
|
|
2013-09-06 12:34:06 +02:00
|
|
|
AppSettings settings = new AppSettings(packageName);
|
2013-09-08 17:04:33 +02:00
|
|
|
mSettingsFragment.setAppSettings(settings);
|
2013-06-17 15:52:09 +02:00
|
|
|
} else if (ACTION_CACHE_PASSPHRASE.equals(action)) {
|
2013-05-28 15:10:36 +02:00
|
|
|
long secretKeyId = extras.getLong(EXTRA_SECRET_KEY_ID);
|
|
|
|
|
|
2013-09-08 15:30:05 +02:00
|
|
|
showPassphraseDialog(secretKeyId);
|
|
|
|
|
} else if (ACTION_SELECT_PUB_KEYS.equals(action)) {
|
2013-09-08 17:04:33 +02:00
|
|
|
long[] selectedMasterKeyIds = intent.getLongArrayExtra(EXTRA_SELECTED_MASTER_KEY_IDS);
|
2013-09-08 15:30:05 +02:00
|
|
|
|
2013-09-08 16:08:36 +02:00
|
|
|
// Inflate a "Done"/"Cancel" custom action bar view
|
|
|
|
|
ActionBarHelper.setDoneCancelView(getSupportActionBar(), R.string.btn_okay,
|
|
|
|
|
new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
// ok
|
|
|
|
|
|
2013-09-08 17:04:33 +02:00
|
|
|
try {
|
|
|
|
|
mServiceCallback.onSelectedPublicKeys(mSelectFragment
|
|
|
|
|
.getSelectedMasterKeyIds());
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(Constants.TAG, "ServiceActivity");
|
|
|
|
|
}
|
|
|
|
|
finish();
|
2013-09-08 16:08:36 +02:00
|
|
|
}
|
|
|
|
|
}, R.string.btn_doNotSave, new View.OnClickListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
// cancel
|
|
|
|
|
|
2013-09-08 17:04:33 +02:00
|
|
|
// TODO: currently does the same as OK...
|
|
|
|
|
try {
|
|
|
|
|
mServiceCallback.onSelectedPublicKeys(mSelectFragment
|
|
|
|
|
.getSelectedMasterKeyIds());
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(Constants.TAG, "ServiceActivity");
|
|
|
|
|
}
|
|
|
|
|
finish();
|
2013-09-08 16:08:36 +02:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2013-09-08 17:04:33 +02:00
|
|
|
setContentView(R.layout.api_app_select_pub_keys_activity);
|
|
|
|
|
|
|
|
|
|
// Check that the activity is using the layout version with
|
|
|
|
|
// the fragment_container FrameLayout
|
|
|
|
|
if (findViewById(R.id.api_select_pub_keys_fragment_container) != null) {
|
|
|
|
|
|
|
|
|
|
// However, if we're being restored from a previous state,
|
|
|
|
|
// then we don't need to do anything and should return or else
|
|
|
|
|
// we could end up with overlapping fragments.
|
|
|
|
|
if (savedInstanceState != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create an instance of the fragment
|
|
|
|
|
mSelectFragment = SelectPublicKeyFragment.newInstance(selectedMasterKeyIds);
|
|
|
|
|
|
|
|
|
|
// Add the fragment to the 'fragment_container' FrameLayout
|
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
|
.add(R.id.api_select_pub_keys_fragment_container, mSelectFragment).commit();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-28 15:10:36 +02:00
|
|
|
} else {
|
|
|
|
|
Log.e(Constants.TAG, "Wrong action!");
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Shows passphrase dialog to cache a new passphrase the user enters for using it later for
|
|
|
|
|
* encryption. Based on mSecretKeyId it asks for a passphrase to open a private key or it asks
|
|
|
|
|
* for a symmetric passphrase
|
|
|
|
|
*/
|
|
|
|
|
private void showPassphraseDialog(long secretKeyId) {
|
|
|
|
|
// Message is received after passphrase is cached
|
|
|
|
|
Handler returnHandler = new Handler() {
|
|
|
|
|
@Override
|
|
|
|
|
public void handleMessage(Message message) {
|
|
|
|
|
if (message.what == PassphraseDialogFragment.MESSAGE_OKAY) {
|
2013-07-25 16:25:20 +02:00
|
|
|
try {
|
|
|
|
|
mServiceCallback.onCachedPassphrase(true);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(Constants.TAG, "ServiceActivity");
|
|
|
|
|
}
|
2013-05-28 15:10:36 +02:00
|
|
|
finish();
|
2013-09-08 18:21:04 +02:00
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
mServiceCallback.onCachedPassphrase(false);
|
|
|
|
|
} catch (RemoteException e) {
|
|
|
|
|
Log.e(Constants.TAG, "ServiceActivity");
|
|
|
|
|
}
|
|
|
|
|
finish();
|
2013-05-28 15:10:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Create a new Messenger for the communication back
|
|
|
|
|
Messenger messenger = new Messenger(returnHandler);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
PassphraseDialogFragment passphraseDialog = PassphraseDialogFragment.newInstance(this,
|
|
|
|
|
messenger, secretKeyId);
|
|
|
|
|
|
|
|
|
|
passphraseDialog.show(getSupportFragmentManager(), "passphraseDialog");
|
|
|
|
|
} catch (PgpMain.PgpGeneralException e) {
|
|
|
|
|
Log.d(Constants.TAG, "No passphrase for this secret key, encrypt directly!");
|
|
|
|
|
// send message to handler to start encryption directly
|
|
|
|
|
returnHandler.sendEmptyMessage(PassphraseDialogFragment.MESSAGE_OKAY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|