package org.thoughtcrime.securesms.preferences; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.preference.Preference; import android.provider.ContactsContract; import android.support.v4.preference.PreferenceFragment; import android.util.Log; import org.thoughtcrime.securesms.ApplicationPreferencesActivity; import org.thoughtcrime.securesms.LogSubmitActivity; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.contacts.ContactAccessor; import org.thoughtcrime.securesms.contacts.ContactIdentityManager; import org.thoughtcrime.securesms.util.TextSecurePreferences; public class AdvancedPreferenceFragment extends PreferenceFragment { private static final String TAG = AdvancedPreferenceFragment.class.getSimpleName(); private static final String SUBMIT_DEBUG_LOG_PREF = "pref_submit_debug_logs"; private static final int PICK_IDENTITY_CONTACT = 1; @Override public void onCreate(Bundle paramBundle) { super.onCreate(paramBundle); addPreferencesFromResource(R.xml.preferences_advanced); initializeIdentitySelection(); this.findPreference(SUBMIT_DEBUG_LOG_PREF) .setOnPreferenceClickListener(new SubmitDebugLogListener()); } @Override public void onResume() { super.onResume(); ((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.preferences__advanced); } @Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); Log.w(TAG, "Got result: " + resultCode + " for req: " + reqCode); if (resultCode == Activity.RESULT_OK && reqCode == PICK_IDENTITY_CONTACT) { handleIdentitySelection(data); } } private void initializeIdentitySelection() { ContactIdentityManager identity = ContactIdentityManager.getInstance(getActivity()); Preference preference = this.findPreference(TextSecurePreferences.IDENTITY_PREF); if (identity.isSelfIdentityAutoDetected()) { this.getPreferenceScreen().removePreference(preference); } else { Uri contactUri = identity.getSelfIdentityUri(); if (contactUri != null) { String contactName = ContactAccessor.getInstance().getNameFromContact(getActivity(), contactUri); preference.setSummary(String.format(getString(R.string.ApplicationPreferencesActivity_currently_s), contactName)); } preference.setOnPreferenceClickListener(new IdentityPreferenceClickListener()); } } private class IdentityPreferenceClickListener implements Preference.OnPreferenceClickListener { @Override public boolean onPreferenceClick(Preference preference) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType(ContactsContract.Contacts.CONTENT_TYPE); startActivityForResult(intent, PICK_IDENTITY_CONTACT); return true; } } private void handleIdentitySelection(Intent data) { Uri contactUri = data.getData(); if (contactUri != null) { TextSecurePreferences.setIdentityContactUri(getActivity(), contactUri.toString()); initializeIdentitySelection(); } } private class SubmitDebugLogListener implements Preference.OnPreferenceClickListener { @Override public boolean onPreferenceClick(Preference preference) { final Intent intent = new Intent(getActivity(), LogSubmitActivity.class); startActivity(intent); return true; } } }