diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java index 628970b27..bb3df2541 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyFragment.java @@ -18,24 +18,30 @@ package org.sufficientlysecure.keychain.ui; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; import android.database.Cursor; +import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; +import android.provider.ContactsContract; 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.View; import android.view.ViewGroup; -import android.widget.AdapterView; -import android.widget.ListView; +import android.widget.*; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; import org.sufficientlysecure.keychain.compatibility.DialogFragmentWorkaround; +import org.sufficientlysecure.keychain.pgp.KeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.ui.adapter.UserIdsAdapter; import org.sufficientlysecure.keychain.ui.dialog.UserIdInfoDialogFragment; +import org.sufficientlysecure.keychain.util.ContactHelper; import org.sufficientlysecure.keychain.util.Log; public class ViewKeyFragment extends LoaderFragment implements @@ -44,8 +50,14 @@ public class ViewKeyFragment extends LoaderFragment implements public static final String ARG_DATA_URI = "uri"; private ListView mUserIds; + //private ListView mLinkedSystemContact; boolean mIsSecret = false; + private String mName; + + LinearLayout mSystemContactLayout; + ImageView mSystemContactPicture; + TextView mSystemContactName; private static final int LOADER_ID_UNIFIED = 0; private static final int LOADER_ID_USER_IDS = 1; @@ -81,6 +93,10 @@ public class ViewKeyFragment extends LoaderFragment implements } }); + mSystemContactLayout = (LinearLayout) view.findViewById(R.id.system_contact_layout); + mSystemContactName = (TextView) view.findViewById(R.id.system_contact_name); + mSystemContactPicture = (ImageView) view.findViewById(R.id.system_contact_picture); + return root; } @@ -100,6 +116,49 @@ public class ViewKeyFragment extends LoaderFragment implements } } + /** + * Checks if a system contact exists for given masterKeyId, and if it does, sets name, picture + * and onClickListener for the linked system contact's layout + * + * @param name + * @param masterKeyId + */ + private void loadLinkedSystemContact(String name, final long masterKeyId) { + final Context context = mSystemContactName.getContext(); + final ContentResolver resolver = context.getContentResolver(); + + final long contactId = ContactHelper.findContactId(resolver, masterKeyId); + + if (contactId != -1) {//contact exists for given master key + mSystemContactName.setText(name); + + Bitmap picture = ContactHelper.loadPhotoByMasterKeyId(resolver, masterKeyId, true); + if (picture != null) mSystemContactPicture.setImageBitmap(picture); + + mSystemContactLayout.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + launchContactActivity(contactId, context); + } + }); + } + } + + /** + * launches the default android Contacts app to view a contact with the passed + * contactId (CONTACT_ID column from ContactsContract.RawContact table which is _ID column in + * ContactsContract.Contact table) + * + * @param contactId _ID for row in ContactsContract.Contacts table + * @param context + */ + private void launchContactActivity(final long contactId, Context context) { + Intent intent = new Intent(Intent.ACTION_VIEW); + Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId)); + intent.setData(uri); + context.startActivity(intent); + } + @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); @@ -148,6 +207,7 @@ public class ViewKeyFragment extends LoaderFragment implements getLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this); } + @Override public Loader onCreateLoader(int id, Bundle args) { setContentShown(false); @@ -164,6 +224,7 @@ public class ViewKeyFragment extends LoaderFragment implements } } + @Override public void onLoadFinished(Loader loader, Cursor data) { /* TODO better error handling? May cause problems when a key is deleted, * because the notification triggers faster than the activity closes. @@ -179,7 +240,12 @@ public class ViewKeyFragment extends LoaderFragment implements if (data.moveToFirst()) { mIsSecret = data.getInt(INDEX_HAS_ANY_SECRET) != 0; - + if (mName == null) {//to ensure we load the linked system contact only once + String[] mainUserId = KeyRing.splitUserId(data.getString(INDEX_USER_ID)); + mName = mainUserId[0]; + long masterKeyId = data.getLong(INDEX_MASTER_KEY_ID); + loadLinkedSystemContact(mName, masterKeyId); + } // load user ids after we know if it's a secret key mUserIdsAdapter = new UserIdsAdapter(getActivity(), null, 0, !mIsSecret, null); mUserIds.setAdapter(mUserIdsAdapter); @@ -202,6 +268,7 @@ public class ViewKeyFragment extends LoaderFragment implements * This is called when the last Cursor provided to onLoadFinished() above is about to be closed. * We need to make sure we are no longer using it. */ + @Override public void onLoaderReset(Loader loader) { switch (loader.getId()) { case LOADER_ID_USER_IDS: { diff --git a/OpenKeychain/src/main/res/layout/view_key_fragment.xml b/OpenKeychain/src/main/res/layout/view_key_fragment.xml index bba412f99..59939d8fa 100644 --- a/OpenKeychain/src/main/res/layout/view_key_fragment.xml +++ b/OpenKeychain/src/main/res/layout/view_key_fragment.xml @@ -1,7 +1,7 @@ + xmlns:card_view="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent"> + android:text="@string/section_user_ids"/> + android:layout_marginBottom="4dp"/> + + + + + + + + + + + + + + diff --git a/OpenKeychain/src/main/res/values/strings.xml b/OpenKeychain/src/main/res/values/strings.xml index 05daf8951..8ac9e8c5c 100644 --- a/OpenKeychain/src/main/res/values/strings.xml +++ b/OpenKeychain/src/main/res/values/strings.xml @@ -47,6 +47,7 @@ "Identities" + "Linked System Contact" "Should you trust this key?" Proof verification "Proofs from the cloud" @@ -644,6 +645,8 @@ "Confirmed Key" "Unconfirmed: Scan QR Code to confirm key!" + "<none>" + "Keys" "Encrypt/Decrypt"