2012-07-23 13:42:29 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-07-23 13:42:29 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-07-23 13:42:29 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
2013-06-21 11:56:59 -07:00
|
|
|
import android.os.Bundle;
|
2011-12-20 10:20:44 -08:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
2014-04-21 08:40:19 -07:00
|
|
|
import org.whispersystems.libaxolotl.IdentityKey;
|
2013-07-09 19:48:33 -07:00
|
|
|
import org.whispersystems.textsecure.util.Base64;
|
2013-02-10 17:30:51 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Dialogs;
|
2013-06-21 11:56:59 -07:00
|
|
|
import org.thoughtcrime.securesms.util.DynamicTheme;
|
2013-02-10 17:30:51 -08:00
|
|
|
|
2012-07-23 13:42:29 -07:00
|
|
|
import com.actionbarsherlock.view.Menu;
|
|
|
|
import com.actionbarsherlock.view.MenuInflater;
|
|
|
|
import com.actionbarsherlock.view.MenuItem;
|
2013-09-11 15:32:18 -07:00
|
|
|
import org.whispersystems.textsecure.zxing.integration.IntentIntegrator;
|
|
|
|
import org.whispersystems.textsecure.zxing.integration.IntentResult;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Activity for initiating/receiving key QR code scans.
|
2012-07-23 13:42:29 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* @author Moxie Marlinspike
|
|
|
|
*/
|
2013-02-10 17:30:51 -08:00
|
|
|
public abstract class KeyScanningActivity extends PassphraseRequiredSherlockActivity {
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2013-06-21 11:56:59 -07:00
|
|
|
private final DynamicTheme dynamicTheme = new DynamicTheme();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle bundle) {
|
|
|
|
dynamicTheme.onCreate(this);
|
|
|
|
super.onCreate(bundle);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
dynamicTheme.onResume(this);
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
@Override
|
2012-07-23 13:42:29 -07:00
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
super.onPrepareOptionsMenu(menu);
|
|
|
|
|
|
|
|
MenuInflater inflater = this.getSupportMenuInflater();
|
|
|
|
menu.clear();
|
|
|
|
|
|
|
|
inflater.inflate(R.menu.key_scanning, menu);
|
|
|
|
|
|
|
|
menu.findItem(R.id.menu_scan).setTitle(getScanString());
|
|
|
|
menu.findItem(R.id.menu_get_scanned).setTitle(getDisplayString());
|
|
|
|
|
|
|
|
return true;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
@Override
|
2012-07-23 13:42:29 -07:00
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
super.onOptionsItemSelected(item);
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2012-07-23 13:42:29 -07:00
|
|
|
switch (item.getItemId()) {
|
|
|
|
case R.id.menu_scan: initiateScan(); return true;
|
|
|
|
case R.id.menu_get_scanned: initiateDisplay(); return true;
|
|
|
|
case android.R.id.home: finish(); return true;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
|
|
|
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
if ((scanResult != null) && (scanResult.getContents() != null)) {
|
|
|
|
String data = scanResult.getContents();
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
if (data.equals(Base64.encodeBytes(getIdentityKeyToCompare().serialize()))) {
|
2014-03-01 01:32:00 +01:00
|
|
|
Dialogs.showInfoDialog(this, getVerifiedTitle(), getVerifiedMessage());
|
2011-12-20 10:20:44 -08:00
|
|
|
} else {
|
2014-03-01 01:32:00 +01:00
|
|
|
Dialogs.showAlertDialog(this, getNotVerifiedTitle(), getNotVerifiedMessage());
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
} else {
|
2012-09-19 19:56:04 -07:00
|
|
|
Toast.makeText(this, R.string.KeyScanningActivity_no_scanned_key_found_exclamation,
|
|
|
|
Toast.LENGTH_LONG).show();
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
}
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
protected void initiateScan() {
|
|
|
|
IntentIntegrator.initiateScan(this);
|
|
|
|
}
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
protected void initiateDisplay() {
|
|
|
|
IntentIntegrator.shareText(this, Base64.encodeBytes(getIdentityKeyToDisplay().serialize()));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract String getScanString();
|
|
|
|
protected abstract String getDisplayString();
|
|
|
|
|
|
|
|
protected abstract String getNotVerifiedTitle();
|
|
|
|
protected abstract String getNotVerifiedMessage();
|
|
|
|
|
2014-04-21 08:40:19 -07:00
|
|
|
protected abstract IdentityKey getIdentityKeyToCompare();
|
|
|
|
protected abstract IdentityKey getIdentityKeyToDisplay();
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
protected abstract String getVerifiedTitle();
|
|
|
|
protected abstract String getVerifiedMessage();
|
2012-07-23 13:42:29 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|