2014-11-04 14:19:48 +00:00
|
|
|
package org.thoughtcrime.securesms.preferences;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
2015-03-14 10:36:35 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.DialogInterface;
|
2014-11-04 14:19:48 +00:00
|
|
|
import android.content.Intent;
|
2015-06-11 21:42:32 +00:00
|
|
|
import android.content.pm.PackageManager;
|
2014-11-04 14:19:48 +00:00
|
|
|
import android.net.Uri;
|
2017-10-23 20:03:32 +00:00
|
|
|
import android.os.AsyncTask;
|
2014-11-04 14:19:48 +00:00
|
|
|
import android.os.Bundle;
|
|
|
|
import android.provider.ContactsContract;
|
2015-06-11 21:42:32 +00:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-05-20 21:36:30 +00:00
|
|
|
import android.support.v7.app.AlertDialog;
|
2017-09-21 01:10:44 +00:00
|
|
|
import android.support.v7.preference.CheckBoxPreference;
|
|
|
|
import android.support.v7.preference.Preference;
|
2014-11-04 14:19:48 +00:00
|
|
|
import android.util.Log;
|
2015-03-14 10:36:35 +00:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import com.google.android.gms.gcm.GoogleCloudMessaging;
|
2014-11-04 14:19:48 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationPreferencesActivity;
|
|
|
|
import org.thoughtcrime.securesms.LogSubmitActivity;
|
|
|
|
import org.thoughtcrime.securesms.R;
|
2015-03-14 10:36:35 +00:00
|
|
|
import org.thoughtcrime.securesms.RegistrationActivity;
|
2014-11-04 14:19:48 +00:00
|
|
|
import org.thoughtcrime.securesms.contacts.ContactAccessor;
|
|
|
|
import org.thoughtcrime.securesms.contacts.ContactIdentityManager;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
2014-11-04 14:19:48 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2017-03-14 20:24:24 +00:00
|
|
|
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
|
2015-03-14 10:36:35 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2014-11-04 14:19:48 +00:00
|
|
|
|
2017-09-21 01:10:44 +00:00
|
|
|
public class AdvancedPreferenceFragment extends CorrectedPreferenceFragment {
|
2014-11-04 14:19:48 +00:00
|
|
|
private static final String TAG = AdvancedPreferenceFragment.class.getSimpleName();
|
|
|
|
|
2015-03-14 10:36:35 +00:00
|
|
|
private static final String PUSH_MESSAGING_PREF = "pref_toggle_push_messaging";
|
2014-11-04 14:19:48 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
initializeIdentitySelection();
|
|
|
|
|
2015-06-11 21:42:32 +00:00
|
|
|
Preference submitDebugLog = this.findPreference(SUBMIT_DEBUG_LOG_PREF);
|
|
|
|
submitDebugLog.setOnPreferenceClickListener(new SubmitDebugLogListener());
|
|
|
|
submitDebugLog.setSummary(getVersion(getActivity()));
|
2014-11-04 14:19:48 +00:00
|
|
|
}
|
|
|
|
|
2017-09-21 01:10:44 +00:00
|
|
|
@Override
|
|
|
|
public void onCreatePreferences(@Nullable Bundle savedInstanceState, String rootKey) {
|
|
|
|
addPreferencesFromResource(R.xml.preferences_advanced);
|
|
|
|
}
|
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
((ApplicationPreferencesActivity) getActivity()).getSupportActionBar().setTitle(R.string.preferences__advanced);
|
2015-11-24 22:14:07 +00:00
|
|
|
|
|
|
|
initializePushMessagingToggle();
|
2014-11-04 14:19:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 10:36:35 +00:00
|
|
|
private void initializePushMessagingToggle() {
|
|
|
|
CheckBoxPreference preference = (CheckBoxPreference)this.findPreference(PUSH_MESSAGING_PREF);
|
2015-11-24 22:14:07 +00:00
|
|
|
|
|
|
|
if (TextSecurePreferences.isPushRegistered(getActivity())) {
|
|
|
|
preference.setChecked(true);
|
|
|
|
preference.setSummary(TextSecurePreferences.getLocalNumber(getActivity()));
|
|
|
|
} else {
|
|
|
|
preference.setChecked(false);
|
|
|
|
preference.setSummary(R.string.preferences__free_private_messages_and_calls);
|
|
|
|
}
|
|
|
|
|
2015-03-14 10:36:35 +00:00
|
|
|
preference.setOnPreferenceChangeListener(new PushMessagingClickListener());
|
|
|
|
}
|
|
|
|
|
2014-11-04 14:19:48 +00:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 21:42:32 +00:00
|
|
|
|
|
|
|
private @NonNull String getVersion(@Nullable Context context) {
|
|
|
|
try {
|
|
|
|
if (context == null) return "";
|
|
|
|
|
|
|
|
String app = context.getString(R.string.app_name);
|
|
|
|
String version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
|
|
|
|
|
|
|
|
return String.format("%s %s", app, version);
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return context.getString(R.string.app_name);
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 14:19:48 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 10:36:35 +00:00
|
|
|
|
|
|
|
private class PushMessagingClickListener implements Preference.OnPreferenceChangeListener {
|
|
|
|
private static final int SUCCESS = 0;
|
|
|
|
private static final int NETWORK_ERROR = 1;
|
|
|
|
|
|
|
|
private class DisablePushMessagesTask extends ProgressDialogAsyncTask<Void, Void, Integer> {
|
|
|
|
private final CheckBoxPreference checkBoxPreference;
|
|
|
|
|
|
|
|
public DisablePushMessagesTask(final CheckBoxPreference checkBoxPreference) {
|
2015-10-27 00:08:19 +00:00
|
|
|
super(getActivity(), R.string.ApplicationPreferencesActivity_unregistering, R.string.ApplicationPreferencesActivity_unregistering_from_signal_messages_and_calls);
|
2015-03-14 10:36:35 +00:00
|
|
|
this.checkBoxPreference = checkBoxPreference;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
switch (result) {
|
|
|
|
case NETWORK_ERROR:
|
|
|
|
Toast.makeText(getActivity(),
|
|
|
|
R.string.ApplicationPreferencesActivity_error_connecting_to_server,
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case SUCCESS:
|
|
|
|
TextSecurePreferences.setPushRegistered(getActivity(), false);
|
2015-11-24 22:14:07 +00:00
|
|
|
initializePushMessagingToggle();
|
2015-03-14 10:36:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... params) {
|
|
|
|
try {
|
2017-03-14 20:24:24 +00:00
|
|
|
Context context = getActivity();
|
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
2015-03-14 10:36:35 +00:00
|
|
|
|
2015-11-03 22:45:49 +00:00
|
|
|
try {
|
|
|
|
accountManager.setGcmId(Optional.<String>absent());
|
|
|
|
} catch (AuthorizationFailedException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
|
2017-03-01 00:59:15 +00:00
|
|
|
if (!TextSecurePreferences.isGcmDisabled(context)) {
|
|
|
|
GoogleCloudMessaging.getInstance(context).unregister();
|
|
|
|
}
|
2015-03-14 10:36:35 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, ioe);
|
|
|
|
return NETWORK_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onPreferenceChange(final Preference preference, Object newValue) {
|
|
|
|
if (((CheckBoxPreference)preference).isChecked()) {
|
2015-05-20 21:36:30 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
|
2015-03-24 12:44:22 +00:00
|
|
|
builder.setIconAttribute(R.attr.dialog_info_icon);
|
2015-10-27 00:08:19 +00:00
|
|
|
builder.setTitle(R.string.ApplicationPreferencesActivity_disable_signal_messages_and_calls);
|
|
|
|
builder.setMessage(R.string.ApplicationPreferencesActivity_disable_signal_messages_and_calls_by_unregistering);
|
2015-03-14 10:36:35 +00:00
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
2017-10-23 20:03:32 +00:00
|
|
|
new DisablePushMessagesTask((CheckBoxPreference)preference).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
2015-03-14 10:36:35 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
builder.show();
|
|
|
|
} else {
|
|
|
|
Intent nextIntent = new Intent(getActivity(), ApplicationPreferencesActivity.class);
|
|
|
|
|
|
|
|
Intent intent = new Intent(getActivity(), RegistrationActivity.class);
|
|
|
|
intent.putExtra("cancel_button", true);
|
|
|
|
intent.putExtra("next_intent", nextIntent);
|
|
|
|
startActivity(intent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-11-04 14:19:48 +00:00
|
|
|
}
|