This commit is contained in:
Nikhil Peter Raj 2014-03-20 23:37:29 +05:30
parent 77365202e0
commit 0a8b45ee88

View File

@ -19,22 +19,24 @@ package org.sufficientlysecure.keychain.ui;
import android.app.Activity; import android.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import com.beardedhen.androidbootstrap.BootstrapButton; import com.beardedhen.androidbootstrap.BootstrapButton;
import org.spongycastle.openpgp.PGPSecretKey;
import org.spongycastle.openpgp.PGPSecretKeyRing;
import org.sufficientlysecure.keychain.Id;
import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.R;
import org.sufficientlysecure.keychain.pgp.PgpKeyHelper; import org.sufficientlysecure.keychain.pgp.PgpKeyHelper;
import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.provider.KeychainContract;
public class SelectSecretKeyLayoutFragment extends Fragment { public class SelectSecretKeyLayoutFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private TextView mKeyUserId; private TextView mKeyUserId;
private TextView mKeyUserIdRest; private TextView mKeyUserIdRest;
@ -43,12 +45,25 @@ public class SelectSecretKeyLayoutFragment extends Fragment {
private BootstrapButton mSelectKeyButton; private BootstrapButton mSelectKeyButton;
private Boolean mFilterCertify; private Boolean mFilterCertify;
private Uri mReceivedUri = null;
private SelectSecretKeyCallback mCallback; private SelectSecretKeyCallback mCallback;
private static final int REQUEST_CODE_SELECT_KEY = 8882; private static final int REQUEST_CODE_SELECT_KEY = 8882;
//Loader ID needs to be different from the usual 0
private static final int LOADER_ID = 2;
//The Projection we will retrieve, Master Key ID is for convenience sake,
//to avoid having to pass the Key Around
final String[] PROJECTION = new String[]{KeychainContract.UserIds.USER_ID
, KeychainContract.KeyRings.MASTER_KEY_ID};
final int INDEX_USER_ID = 0;
final int INDEX_MASTER_KEY_ID = 1;
public interface SelectSecretKeyCallback { public interface SelectSecretKeyCallback {
void onKeySelected(long secretKeyId); void onKeySelected(long secretKeyId);
} }
public void setCallback(SelectSecretKeyCallback callback) { public void setCallback(SelectSecretKeyCallback callback) {
@ -59,64 +74,26 @@ public class SelectSecretKeyLayoutFragment extends Fragment {
mFilterCertify = filterCertify; mFilterCertify = filterCertify;
} }
public void selectKey(long secretKeyId) { public void setNoKeySelected() {
if (secretKeyId == Id.key.none) {
mNoKeySelected.setVisibility(View.VISIBLE); mNoKeySelected.setVisibility(View.VISIBLE);
mKeyUserId.setVisibility(View.GONE); mKeyUserId.setVisibility(View.GONE);
mKeyUserIdRest.setVisibility(View.GONE); mKeyUserIdRest.setVisibility(View.GONE);
mKeyMasterKeyIdHex.setVisibility(View.GONE); mKeyMasterKeyIdHex.setVisibility(View.GONE);
} else {
PGPSecretKeyRing keyRing = ProviderHelper.getPGPSecretKeyRingByMasterKeyId(
getActivity(), secretKeyId);
if (keyRing != null) {
PGPSecretKey key = PgpKeyHelper.getMasterKey(keyRing);
String masterkeyIdHex = PgpKeyHelper.convertKeyIdToHexShort(secretKeyId);
if (key != null) {
String userId = PgpKeyHelper.getMainUserIdSafe(getActivity(), key);
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
String userName, userEmail;
if (userIdSplit[0] != null) {
userName = userIdSplit[0];
} else {
userName = getActivity().getResources().getString(R.string.user_id_no_name);
} }
if (userIdSplit[1] != null) { public void setSelectedKeyData(String userName, String email, String masterKeyHex) {
userEmail = userIdSplit[1];
} else { mNoKeySelected.setVisibility(View.GONE);
userEmail = getActivity().getResources().getString(R.string.error_user_id_no_email);
}
mKeyMasterKeyIdHex.setText(masterkeyIdHex);
mKeyUserId.setText(userName); mKeyUserId.setText(userName);
mKeyUserIdRest.setText(userEmail); mKeyUserIdRest.setText(email);
mKeyMasterKeyIdHex.setVisibility(View.VISIBLE); mKeyMasterKeyIdHex.setText(masterKeyHex);
mKeyUserId.setVisibility(View.VISIBLE); mKeyUserId.setVisibility(View.VISIBLE);
mKeyUserIdRest.setVisibility(View.VISIBLE); mKeyUserIdRest.setVisibility(View.VISIBLE);
mNoKeySelected.setVisibility(View.GONE);
} else {
mKeyMasterKeyIdHex.setVisibility(View.GONE);
mKeyUserId.setVisibility(View.GONE);
mKeyUserIdRest.setVisibility(View.GONE);
mNoKeySelected.setVisibility(View.VISIBLE);
}
} else {
mKeyMasterKeyIdHex.setText(
getActivity().getResources()
.getString(R.string.no_keys_added_or_updated)
+ " for master id: " + secretKeyId);
mKeyMasterKeyIdHex.setVisibility(View.VISIBLE); mKeyMasterKeyIdHex.setVisibility(View.VISIBLE);
mKeyUserId.setVisibility(View.GONE);
mKeyUserIdRest.setVisibility(View.GONE);
mNoKeySelected.setVisibility(View.GONE);
}
} }
}
public void setError(String error) { public void setError(String error) {
mNoKeySelected.requestFocus(); mNoKeySelected.requestFocus();
@ -147,29 +124,80 @@ public class SelectSecretKeyLayoutFragment extends Fragment {
return view; return view;
} }
//For AppSettingsFragment
public void selectKey(long masterKeyId){
Uri buildUri = KeychainContract.KeyRings.buildSecretKeyRingsByMasterKeyIdUri(String.valueOf(masterKeyId));
mReceivedUri=buildUri;
getActivity().getSupportLoaderManager().restartLoader(LOADER_ID, null, this);
}
private void startSelectKeyActivity() { private void startSelectKeyActivity() {
Intent intent = new Intent(getActivity(), SelectSecretKeyActivity.class); Intent intent = new Intent(getActivity(), SelectSecretKeyActivity.class);
intent.putExtra(SelectSecretKeyActivity.EXTRA_FILTER_CERTIFY, mFilterCertify); intent.putExtra(SelectSecretKeyActivity.EXTRA_FILTER_CERTIFY, mFilterCertify);
startActivityForResult(intent, REQUEST_CODE_SELECT_KEY); startActivityForResult(intent, REQUEST_CODE_SELECT_KEY);
} }
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//We don't care about the Loader id
return new CursorLoader(getActivity(), mReceivedUri, PROJECTION, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
if (data.moveToFirst()) {
String userName, email, masterKeyHex;
String userID = data.getString(INDEX_USER_ID);
long masterKeyID = data.getLong(INDEX_MASTER_KEY_ID);
String splitUserID[] = PgpKeyHelper.splitUserId(userID);
if (splitUserID[0] != null) {
userName = splitUserID[0];
} else {
userName = getActivity().getResources().getString(R.string.user_id_no_name);
}
if (splitUserID[1] != null) {
email = splitUserID[1];
} else {
email = getActivity().getResources().getString(R.string.error_user_id_no_email);
}
//TODO Can the cursor return invalid values for the Master Key ?
masterKeyHex = PgpKeyHelper.convertKeyIdToHexShort(masterKeyID);
//Set the data
setSelectedKeyData(userName, email, masterKeyHex);
//Give value to the callback
mCallback.onKeySelected(masterKeyID);
} else {
//Set The empty View
setNoKeySelected();
}
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
return;
}
// Select Secret Key Activity delivers the intent which was sent by it using interface to Select // Select Secret Key Activity delivers the intent which was sent by it using interface to Select
// Secret Key Fragment.Intent contains Master Key Id, User Email, User Name, Master Key Id Hex. // Secret Key Fragment.Intent contains the passed Uri
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode & 0xFFFF) { switch (requestCode & 0xFFFF) {
case REQUEST_CODE_SELECT_KEY: { case REQUEST_CODE_SELECT_KEY: {
long secretKeyId;
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras(); mReceivedUri = data.getData();
secretKeyId = bundle.getLong(SelectSecretKeyActivity.RESULT_EXTRA_MASTER_KEY_ID);
selectKey(secretKeyId); //Must be restartLoader() or the data will not be updated on selecting a new key
getActivity().getSupportLoaderManager().restartLoader(LOADER_ID, null, this);
// remove displayed errors
mKeyUserId.setError(null); mKeyUserId.setError(null);
// give value back to callback
mCallback.onKeySelected(secretKeyId);
} }
break; break;
} }