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.Keys;
import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds; import org.sufficientlysecure.keychain.provider.KeychainContract.UserIds;
import org.sufficientlysecure.keychain.provider.KeychainDatabase.Tables; 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.IterableIterator;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
@ -739,12 +740,50 @@ public class ProviderHelper {
return packageNames; return packageNames;
} }
public static void addCryptoConsumer(Context context, String packageName, long keyId, private static void contentValueForApiApps() {
boolean asciiArmor) {
}
public static void insertApiApp(Context context, AppSettings appSettings) {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(ApiApps.PACKAGE_NAME, packageName); values.put(ApiApps.PACKAGE_NAME, appSettings.getPackageName());
values.put(ApiApps.KEY_ID, keyId); values.put(ApiApps.KEY_ID, appSettings.getKeyId());
values.put(ApiApps.ASCII_ARMOR, asciiArmor); values.put(ApiApps.ASCII_ARMOR, appSettings.isAsciiArmor());
// TODO: other parameters
context.getContentResolver().insert(ApiApps.CONTENT_URI, values); 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.Constants;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper;
import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Log;
import android.content.ContentValues;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -20,9 +18,7 @@ import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem; import com.actionbarsherlock.view.MenuItem;
public class AppSettingsActivity extends SherlockFragmentActivity { public class AppSettingsActivity extends SherlockFragmentActivity {
// model
private Uri mAppUri; private Uri mAppUri;
private String mPackageName;
private AppSettingsFragment settingsFragment; private AppSettingsFragment settingsFragment;
@ -91,26 +87,8 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
} }
private void loadData(Uri appUri) { private void loadData(Uri appUri) {
Cursor cur = getContentResolver().query(appUri, null, null, null, null); AppSettings settings = ProviderHelper.getApiAppSettings(this, appUri);
if (cur.moveToFirst()) { settingsFragment.setAppSettings(settings);
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);
}
}
} }
private void revokeAccess() { private void revokeAccess() {
@ -121,15 +99,7 @@ public class AppSettingsActivity extends SherlockFragmentActivity {
} }
private void save() { private void save() {
final ContentValues cv = new ContentValues(); ProviderHelper.updateApiApp(this, settingsFragment.getAppSettings(), mAppUri);
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();
}
finish(); finish();
} }

View File

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

View File

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