2015-11-19 18:21:19 +00:00
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.os.Vibrator;
|
|
|
|
import android.support.annotation.NonNull;
|
2015-11-22 17:28:36 +00:00
|
|
|
import android.text.TextUtils;
|
2015-11-19 18:21:19 +00:00
|
|
|
import android.transition.TransitionInflater;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.Button;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
2016-08-25 01:51:45 +00:00
|
|
|
import org.thoughtcrime.securesms.qr.ScanListener;
|
2015-11-19 18:21:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicLanguage;
|
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
2015-11-23 23:07:41 +00:00
|
|
|
import org.thoughtcrime.securesms.util.task.ProgressDialogAsyncTask;
|
2015-11-19 18:21:19 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.libsignal.IdentityKeyPair;
|
|
|
|
import org.whispersystems.libsignal.InvalidKeyException;
|
|
|
|
import org.whispersystems.libsignal.ecc.Curve;
|
|
|
|
import org.whispersystems.libsignal.ecc.ECPublicKey;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NotFoundException;
|
|
|
|
import org.whispersystems.signalservice.internal.push.DeviceLimitExceededException;
|
2015-11-19 18:21:19 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
public class DeviceActivity extends PassphraseRequiredActionBarActivity
|
2016-08-25 01:51:45 +00:00
|
|
|
implements Button.OnClickListener, ScanListener, DeviceLinkFragment.LinkClickedListener
|
2015-11-19 18:21:19 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
private static final String TAG = DeviceActivity.class.getSimpleName();
|
|
|
|
|
|
|
|
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
|
|
|
private final DynamicLanguage dynamicLanguage = new DynamicLanguage();
|
|
|
|
|
|
|
|
private DeviceAddFragment deviceAddFragment;
|
|
|
|
private DeviceListFragment deviceListFragment;
|
|
|
|
private DeviceLinkFragment deviceLinkFragment;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPreCreate() {
|
|
|
|
dynamicTheme.onCreate(this);
|
|
|
|
dynamicLanguage.onCreate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle bundle, @NonNull MasterSecret masterSecret) {
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
2016-02-11 16:27:30 +00:00
|
|
|
getSupportActionBar().setTitle(R.string.AndroidManifest__linked_devices);
|
2015-11-19 18:21:19 +00:00
|
|
|
this.deviceAddFragment = new DeviceAddFragment();
|
|
|
|
this.deviceListFragment = new DeviceListFragment();
|
|
|
|
this.deviceLinkFragment = new DeviceLinkFragment();
|
|
|
|
|
|
|
|
this.deviceListFragment.setAddDeviceButtonListener(this);
|
|
|
|
this.deviceAddFragment.setScanListener(this);
|
|
|
|
|
2015-12-03 22:39:13 +00:00
|
|
|
if (getIntent().getBooleanExtra("add", false)) {
|
|
|
|
initFragment(android.R.id.content, deviceAddFragment, masterSecret, dynamicLanguage.getCurrentLocale());
|
|
|
|
} else {
|
|
|
|
initFragment(android.R.id.content, deviceListFragment, masterSecret, dynamicLanguage.getCurrentLocale());
|
|
|
|
}
|
2015-11-19 18:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
dynamicLanguage.onResume(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
case android.R.id.home: finish(); return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.replace(android.R.id.content, deviceAddFragment)
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-08-25 01:51:45 +00:00
|
|
|
public void onQrDataFound(final String data) {
|
2015-11-19 18:21:19 +00:00
|
|
|
Util.runOnMain(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
((Vibrator)getSystemService(Context.VIBRATOR_SERVICE)).vibrate(50);
|
2016-08-25 01:51:45 +00:00
|
|
|
Uri uri = Uri.parse(data);
|
2015-11-19 18:21:19 +00:00
|
|
|
deviceLinkFragment.setLinkClickedListener(uri, DeviceActivity.this);
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
deviceAddFragment.setSharedElementReturnTransition(TransitionInflater.from(DeviceActivity.this).inflateTransition(R.transition.fragment_shared));
|
|
|
|
deviceAddFragment.setExitTransition(TransitionInflater.from(DeviceActivity.this).inflateTransition(android.R.transition.fade));
|
|
|
|
|
|
|
|
deviceLinkFragment.setSharedElementEnterTransition(TransitionInflater.from(DeviceActivity.this).inflateTransition(R.transition.fragment_shared));
|
|
|
|
deviceLinkFragment.setEnterTransition(TransitionInflater.from(DeviceActivity.this).inflateTransition(android.R.transition.fade));
|
|
|
|
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.addToBackStack(null)
|
|
|
|
.addSharedElement(deviceAddFragment.getDevicesImage(), "devices")
|
|
|
|
.replace(android.R.id.content, deviceLinkFragment)
|
|
|
|
.commit();
|
|
|
|
|
|
|
|
} else {
|
|
|
|
getSupportFragmentManager().beginTransaction()
|
|
|
|
.setCustomAnimations(R.anim.slide_from_bottom, R.anim.slide_to_bottom,
|
|
|
|
R.anim.slide_from_bottom, R.anim.slide_to_bottom)
|
|
|
|
.replace(android.R.id.content, deviceLinkFragment)
|
|
|
|
.addToBackStack(null)
|
|
|
|
.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onLink(final Uri uri) {
|
|
|
|
new ProgressDialogAsyncTask<Void, Void, Integer>(this,
|
|
|
|
R.string.DeviceProvisioningActivity_content_progress_title,
|
|
|
|
R.string.DeviceProvisioningActivity_content_progress_content)
|
|
|
|
{
|
|
|
|
private static final int SUCCESS = 0;
|
|
|
|
private static final int NO_DEVICE = 1;
|
|
|
|
private static final int NETWORK_ERROR = 2;
|
|
|
|
private static final int KEY_ERROR = 3;
|
|
|
|
private static final int LIMIT_EXCEEDED = 4;
|
2015-11-22 17:28:36 +00:00
|
|
|
private static final int BAD_CODE = 5;
|
2015-11-19 18:21:19 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(Void... params) {
|
|
|
|
try {
|
2016-03-23 17:34:41 +00:00
|
|
|
Context context = DeviceActivity.this;
|
2016-12-20 17:55:52 +00:00
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
2016-03-23 17:34:41 +00:00
|
|
|
String verificationCode = accountManager.getNewDeviceVerificationCode();
|
|
|
|
String ephemeralId = uri.getQueryParameter("uuid");
|
|
|
|
String publicKeyEncoded = uri.getQueryParameter("pub_key");
|
2015-11-22 17:28:36 +00:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(ephemeralId) || TextUtils.isEmpty(publicKeyEncoded)) {
|
|
|
|
Log.w(TAG, "UUID or Key is empty!");
|
|
|
|
return BAD_CODE;
|
|
|
|
}
|
|
|
|
|
2015-11-19 18:21:19 +00:00
|
|
|
ECPublicKey publicKey = Curve.decodePoint(Base64.decode(publicKeyEncoded), 0);
|
|
|
|
IdentityKeyPair identityKeyPair = IdentityKeyUtil.getIdentityKeyPair(context);
|
|
|
|
|
|
|
|
accountManager.addDevice(ephemeralId, publicKey, identityKeyPair, verificationCode);
|
|
|
|
TextSecurePreferences.setMultiDevice(context, true);
|
|
|
|
return SUCCESS;
|
|
|
|
} catch (NotFoundException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return NO_DEVICE;
|
|
|
|
} catch (DeviceLimitExceededException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return LIMIT_EXCEEDED;
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return NETWORK_ERROR;
|
|
|
|
} catch (InvalidKeyException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return KEY_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onPostExecute(Integer result) {
|
|
|
|
super.onPostExecute(result);
|
|
|
|
|
|
|
|
Context context = DeviceActivity.this;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case SUCCESS:
|
|
|
|
Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_success, Toast.LENGTH_SHORT).show();
|
|
|
|
finish();
|
|
|
|
return;
|
|
|
|
case NO_DEVICE:
|
|
|
|
Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_no_device, Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case NETWORK_ERROR:
|
|
|
|
Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_network_error, Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case KEY_ERROR:
|
|
|
|
Toast.makeText(context, R.string.DeviceProvisioningActivity_content_progress_key_error, Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
|
|
|
case LIMIT_EXCEEDED:
|
|
|
|
Toast.makeText(context, R.string.DeviceProvisioningActivity_sorry_you_have_too_many_devices_linked_already, Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
2015-11-22 17:28:36 +00:00
|
|
|
case BAD_CODE:
|
|
|
|
Toast.makeText(context, R.string.DeviceActivity_sorry_this_is_not_a_valid_device_link_qr_code, Toast.LENGTH_LONG).show();
|
|
|
|
break;
|
2015-11-19 18:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getSupportFragmentManager().popBackStackImmediate();
|
|
|
|
}
|
|
|
|
}.execute();
|
|
|
|
}
|
|
|
|
}
|