Put model into own class for easier use in service

This commit is contained in:
Dominik Schürmann 2013-09-06 12:34:06 +02:00
parent 1beb85acf5
commit fabb0389fc
5 changed files with 149 additions and 65 deletions

View File

@ -36,6 +36,7 @@ import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings;
import org.sufficientlysecure.keychain.provider.KeychainContract.Keys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables;
import org.sufficientlysecure.keychain.remote_api.AppSettings;
import org.sufficientlysecure.keychain.util.IterableIterator;
import org.sufficientlysecure.keychain.util.Log;
@ -739,12 +740,50 @@ public class ProviderHelper {
return packageNames;
}
public static void addCryptoConsumer(Context context, String packageName, long keyId,
boolean asciiArmor) {
private static void contentValueForApiApps() {
}
public static void insertApiApp(Context context, AppSettings appSettings) {
ContentValues values = new ContentValues();
values.put(ApiApps.PACKAGE_NAME, packageName);
values.put(ApiApps.KEY_ID, keyId);
values.put(ApiApps.ASCII_ARMOR, asciiArmor);
values.put(ApiApps.PACKAGE_NAME, appSettings.getPackageName());
values.put(ApiApps.KEY_ID, appSettings.getKeyId());
values.put(ApiApps.ASCII_ARMOR, appSettings.isAsciiArmor());
// TODO: other parameters
context.getContentResolver().insert(ApiApps.CONTENT_URI, values);
}
public static void updateApiApp(Context context, AppSettings appSettings, Uri uri) {
final ContentValues cv = new ContentValues();
cv.put(KeychainContract.ApiApps.KEY_ID, appSettings.getKeyId());
cv.put(KeychainContract.ApiApps.ASCII_ARMOR, appSettings.isAsciiArmor());
// TODO: other parameters
if (context.getContentResolver().update(uri, cv, null, null) <= 0) {
throw new RuntimeException();
}
}
public static AppSettings getApiAppSettings(Context context, Uri uri) {
AppSettings settings = new AppSettings();
Cursor cur = context.getContentResolver().query(uri, null, null, null, null);
if (cur.moveToFirst()) {
settings.setPackageName(cur.getString(cur
.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME)));
settings.setKeyId(cur.getLong(cur.getColumnIndex(KeychainContract.ApiApps.KEY_ID)));
settings.setAsciiArmor(cur.getInt(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.ASCII_ARMOR)) == 1);
settings.setPackageName(cur.getString(cur
.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME)));
settings.setPackageName(cur.getString(cur
.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME)));
}
return settings;
}
}

View File

@ -0,0 +1,70 @@
package org.sufficientlysecure.keychain.remote_api;
import org.sufficientlysecure.keychain.Id;
public class AppSettings {
private String packageName;
private long keyId = Id.key.none;
private boolean asciiArmor;
private int encryptionAlgorithm = 7; // AES-128
private int hashAlgorithm = 10; // SHA-512
private int compression = 2; // zlib
public AppSettings() {
}
public AppSettings(String packageName) {
super();
this.packageName = packageName;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
public long getKeyId() {
return keyId;
}
public void setKeyId(long scretKeyId) {
this.keyId = scretKeyId;
}
public boolean isAsciiArmor() {
return asciiArmor;
}
public void setAsciiArmor(boolean asciiArmor) {
this.asciiArmor = asciiArmor;
}
public int getEncryptionAlgorithm() {
return encryptionAlgorithm;
}
public void setEncryptionAlgorithm(int encryptionAlgorithm) {
this.encryptionAlgorithm = encryptionAlgorithm;
}
public int getHashAlgorithm() {
return hashAlgorithm;
}
public void setHashAlgorithm(int hashAlgorithm) {
this.hashAlgorithm = hashAlgorithm;
}
public int getCompression() {
return compression;
}
public void setCompression(int compression) {
this.compression = compression;
}
}

View File

@ -2,12 +2,10 @@ package org.sufficientlysecure.keychain.remote_api;
import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract;
import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.Log;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
@ -20,9 +18,7 @@ import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
public class AppSettingsActivity extends SherlockFragmentActivity {
// model
private Uri mAppUri;
private String mPackageName;
private AppSettingsFragment settingsFragment;
@ -91,26 +87,8 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
}
private void loadData(Uri appUri) {
Cursor cur = getContentResolver().query(appUri, null, null, null, null);
if (cur.moveToFirst()) {
mPackageName = cur.getString(cur.getColumnIndex(KeychainContract.ApiApps.PACKAGE_NAME));
settingsFragment.setPackage(mPackageName);
try {
long secretKeyId = (cur.getLong(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.KEY_ID)));
Log.d(Constants.TAG, "mSecretKeyId: " + secretKeyId);
settingsFragment.setSecretKey(secretKeyId);
boolean asciiArmor = (cur.getInt(cur
.getColumnIndexOrThrow(KeychainContract.ApiApps.ASCII_ARMOR)) == 1);
settingsFragment.setAsciiArmor(asciiArmor);
} catch (IllegalArgumentException e) {
Log.e(Constants.TAG, "AppSettingsActivity", e);
}
}
AppSettings settings = ProviderHelper.getApiAppSettings(this, appUri);
settingsFragment.setAppSettings(settings);
}
private void revokeAccess() {
@ -121,15 +99,7 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
}
private void save() {
final ContentValues cv = new ContentValues();
cv.put(KeychainContract.ApiApps.KEY_ID, settingsFragment.getSecretKeyId());
cv.put(KeychainContract.ApiApps.ASCII_ARMOR, settingsFragment.isAsciiArmor());
// TODO: other parameters
if (getContentResolver().update(mAppUri, cv, null, null) <= 0) {
throw new RuntimeException();
}
ProviderHelper.updateApiApp(this, settingsFragment.getAppSettings(), mAppUri);
finish();
}

View File

@ -26,6 +26,8 @@ import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
@ -33,7 +35,7 @@ import android.widget.TextView;
public class AppSettingsFragment extends Fragment {
// model
private long mSecretKeyId = Id.key.none;
private AppSettings appSettings;
// view
private LinearLayout mAdvancedSettingsContainer;
@ -45,21 +47,15 @@ public class AppSettingsFragment extends Fragment {
private Button mSelectKeyButton;
private CheckBox mAsciiArmorCheckBox;
public void setSecretKey(long keyId) {
mSecretKeyId = keyId;
updateSelectedKeyView(keyId);
public AppSettings getAppSettings() {
return appSettings;
}
public long getSecretKeyId() {
return mSecretKeyId;
}
public void setAsciiArmor(boolean value) {
mAsciiArmorCheckBox.setChecked(value);
}
public boolean isAsciiArmor() {
return mAsciiArmorCheckBox.isChecked();
public void setAppSettings(AppSettings appSettings) {
this.appSettings = appSettings;
setPackage(appSettings.getPackageName());
updateSelectedKeyView(appSettings.getKeyId());
mAsciiArmorCheckBox.setChecked(appSettings.isAsciiArmor());
}
/**
@ -92,6 +88,14 @@ public class AppSettingsFragment extends Fragment {
}
});
mAsciiArmorCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
appSettings.setAsciiArmor(isChecked);
}
});
final Animation visibleAnimation = new AlphaAnimation(0.0f, 1.0f);
visibleAnimation.setDuration(250);
final Animation invisibleAnimation = new AlphaAnimation(1.0f, 0.0f);
@ -123,7 +127,6 @@ public class AppSettingsFragment extends Fragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void selectSecretKey() {
@ -131,7 +134,7 @@ public class AppSettingsFragment extends Fragment {
startActivityForResult(intent, Id.request.secret_keys);
}
public void setPackage(String packageName) {
private void setPackage(String packageName) {
PackageManager pm = getActivity().getApplicationContext().getPackageManager();
// get application name and icon from package manager
@ -181,15 +184,16 @@ public class AppSettingsFragment extends Fragment {
switch (requestCode) {
case Id.request.secret_keys: {
long secretKeyId;
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
mSecretKeyId = bundle.getLong(SelectSecretKeyActivity.RESULT_EXTRA_MASTER_KEY_ID);
Log.d(Constants.TAG, "jo " + mSecretKeyId);
secretKeyId = bundle.getLong(SelectSecretKeyActivity.RESULT_EXTRA_MASTER_KEY_ID);
} else {
mSecretKeyId = Id.key.none;
secretKeyId = Id.key.none;
}
updateSelectedKeyView(mSecretKeyId);
appSettings.setKeyId(secretKeyId);
updateSelectedKeyView(secretKeyId);
break;
}

View File

@ -151,15 +151,14 @@ public class CryptoServiceActivity extends SherlockFragmentActivity {
public void onClick(View v) {
// Allow
if (settingsFragment.getSecretKeyId() == Id.key.none) {
// user needs to select a key!
if (settingsFragment.getAppSettings().getKeyId() == Id.key.none) {
Toast.makeText(CryptoServiceActivity.this,
R.string.api_register_error_select_key, Toast.LENGTH_LONG)
.show();
} else {
ProviderHelper.addCryptoConsumer(CryptoServiceActivity.this,
packageName, settingsFragment.getSecretKeyId(),
settingsFragment.isAsciiArmor());
// Intent data = new Intent();
ProviderHelper.insertApiApp(CryptoServiceActivity.this,
settingsFragment.getAppSettings());
try {
mServiceCallback.onRegistered(true, packageName);
@ -199,7 +198,9 @@ public class CryptoServiceActivity extends SherlockFragmentActivity {
settingsFragment = (AppSettingsFragment) getSupportFragmentManager().findFragmentById(
R.id.api_app_settings_fragment);
settingsFragment.setPackage(packageName);
AppSettings settings = new AppSettings(packageName);
settingsFragment.setAppSettings(settings);
// TODO: handle if app is already registered
// LinearLayout layoutRegister = (LinearLayout)