use of mpicbg.trakem2.util.Pair in project Tusky by tuskyapp.
the class TimelineFragment method onFavourite.
@Override
public void onFavourite(final boolean favourite, final int position) {
final Status status = statuses.get(position).getAsRight();
timelineCases.favouriteWithCallback(status, favourite, new Callback<Status>() {
@Override
public void onResponse(@NonNull Call<Status> call, @NonNull Response<Status> response) {
if (response.isSuccessful()) {
status.setFavourited(favourite);
if (status.getReblog() != null) {
status.getReblog().setFavourited(favourite);
}
Pair<StatusViewData.Concrete, Integer> actual = findStatusAndPosition(position, status);
if (actual == null)
return;
StatusViewData newViewData = new StatusViewData.Builder(actual.first).setFavourited(favourite).createStatusViewData();
statuses.setPairedItem(actual.second, newViewData);
adapter.changeItem(actual.second, newViewData, false);
}
}
@Override
public void onFailure(@NonNull Call<Status> call, @NonNull Throwable t) {
Log.d(TAG, "Failed to favourite status " + status.getId(), t);
}
});
}
use of mpicbg.trakem2.util.Pair in project krypton-android by kryptco.
the class CertifiedPublicKey method parse.
public static CertifiedPublicKey parse(DataInputStream in) throws InvalidPacketTagException, UnsupportedOldPacketLengthTypeException, UnsupportedNewFormatException, UnsupportedPublicKeyAlgorithmException, UnsupportedPublicKeyVersionException, InvalidEd25519PublicKeyFormatException, IOException, InvalidUTF8Exception, DuplicateSubpacketException, NoSuchAlgorithmException, UnsupportedHashAlgorithmException, InvalidSubpacketLengthException, UnsupportedCriticalSubpacketTypeException, UnsupportedSignatureVersionException {
PublicKeyPacket publicKeyPacket = null;
boolean lastPacketUserIDOrSignature = false;
List<Pair<UserIDPacket, List<SignedSignatureAttributes>>> identities = new LinkedList<>();
while (true) {
try {
PacketHeader header = PacketHeader.parse(in);
Log.d("PGP", "found packet with type " + header.tag.packetType.toString());
switch(header.tag.packetType) {
case SIGNATURE:
SignedSignatureAttributes signaturePacket = SignedSignatureAttributes.parse(header, in);
if (lastPacketUserIDOrSignature && identities.size() > 0) {
identities.get(identities.size() - 1).second.add(signaturePacket);
}
break;
case PUBLIC_KEY:
if (publicKeyPacket != null) {
// only accept first public key packet
in.skip(header.length.bodyLength);
continue;
}
publicKeyPacket = PublicKeyPacket.parse(header, in);
break;
case USER_ID:
identities.add(new Pair<UserIDPacket, List<SignedSignatureAttributes>>(UserIDPacket.parse(header, in), new LinkedList<SignedSignatureAttributes>()));
break;
default:
in.skip(header.length.bodyLength);
break;
}
lastPacketUserIDOrSignature = header.tag.packetType == PacketType.USER_ID || header.tag.packetType == PacketType.SIGNATURE;
} catch (EOFException e) {
break;
}
}
return new CertifiedPublicKey(publicKeyPacket, identities);
}
use of mpicbg.trakem2.util.Pair in project krypton-android by kryptco.
the class AsciiArmor method parse.
public static AsciiArmor parse(String text) throws InvalidAsciiArmorException, UnsupportedHeaderLineException, CryptoException {
List<String> lines = new LinkedList<>();
Scanner scanner = new Scanner(text);
int dataStart = 1;
boolean foundEmptyLine = false;
for (int idx = 0; scanner.hasNextLine(); idx++) {
String line = scanner.nextLine().trim();
if (line.equals("")) {
if (foundEmptyLine) {
break;
}
foundEmptyLine = true;
dataStart = idx + 1;
}
lines.add(line);
}
scanner.close();
final int dataEnd = lines.size() - 2;
if (dataStart > dataEnd) {
throw new InvalidAsciiArmorException();
}
if (lines.size() < 4) {
throw new InvalidAsciiArmorException("not enough lines");
}
String startHeaderLine = lines.get(0).replace(HEADER_LINE_PREFIX, "").replace(HEADER_LINE_SUFFIX, "");
String endHeaderLine = lines.get(lines.size() - 1).replace(LAST_LINE_PREFIX, "").replace(LAST_LINE_SUFFIX, "");
if (!startHeaderLine.equals(endHeaderLine)) {
throw new InvalidAsciiArmorException("start and end header lines do not match");
}
HeaderLine headerLine = HeaderLine.fromString(startHeaderLine);
String b64Data = "";
for (int i = dataStart; i < dataEnd; i++) {
b64Data += lines.get(i).trim();
}
final byte[] data = Base64.decode(b64Data);
final int computedCRC = CRC24.compute(data);
String crcLine = lines.get(lines.size() - 2);
byte[] crcData = Base64.decode(crcLine.replaceFirst("=", ""));
if (crcData.length != 3) {
throw new InvalidAsciiArmorException("crc wrong length");
}
ByteBuffer givenCRCBuf = ByteBuffer.allocate(4).put((byte) 0).put(crcData);
givenCRCBuf.flip();
int givenCRC = givenCRCBuf.getInt();
if (givenCRC != computedCRC) {
throw new InvalidAsciiArmorException("invalid CRC");
}
List<Pair<String, String>> headers = new LinkedList<>();
for (int i = 1; i < dataStart - 1; i++) {
String header = lines.get(i);
String[] split = header.split(": ");
if (split.length != 2) {
throw new InvalidAsciiArmorException("invalid header");
}
headers.add(new Pair<>(split[0], split[1]));
}
return new AsciiArmor(headerLine, headers, data);
}
use of mpicbg.trakem2.util.Pair in project krypton-android by kryptco.
the class GitTest method testCommitHash.
@Test
public void testCommitHash() throws Exception {
byte[] sigMessage = Base64.decode("iF4EABYKAAYFAlkkmD8ACgkQ4eT0x9ceFp1gNQD+LWiJFax8iQqgr0yJ1P7JFGvMwuZc8r05h6U+X+lyKYEBAK939lEX1rvBmcetftVbRlOMX5oQZwBLt/NJh+nQ3ssC");
CommitInfo commit = new CommitInfo("2c4df4a89ac5b0b8b21fd2aad4d9b19cd91e7049", "1cd97d0545a25c578e3f4da5283106606276eadf", null, "Alex Grinman <alex@krypt.co> 1495570495 -0400", "Alex Grinman <alex@krypt.co> 1495570495 -0400", "\ntest1234\n".getBytes("UTF-8"));
AsciiArmor aa = new AsciiArmor(AsciiArmor.HeaderLine.SIGNATURE, Collections.singletonList(new Pair<String, String>("Comment", "Created With Kryptonite")), sigMessage);
Assert.assertTrue(Arrays.equals(commit.commitHash(aa.toString()), Base16.decode("84e09dac58d81b1f3fc4806b1b4cb18af3cca0ea")));
}
use of mpicbg.trakem2.util.Pair in project LibreraReader by foobnix.
the class ExportSettingsManager method importAll.
public boolean importAll(File file) {
if (file == null) {
return false;
}
LOG.d("TEST", "Import all from " + file.getPath());
try {
String json = new Scanner(file).useDelimiter("\\A").next();
LOG.d("[IMPORT]", json);
JSONObject jsonObject = new JSONObject(json);
importFromJSon(jsonObject.optJSONObject(PREFIX_PDF), pdfSP);
importFromJSon(jsonObject.optJSONObject(PREFIX_BOOKS), booksSP);
importFromJSon(jsonObject.optJSONObject(PREFIX_BOOKMARKS_PREFERENCES), viewerSP);
importFromJSon(jsonObject.optJSONObject(PREFIX_BOOK_CSS), bookCSS);
jsonToMeta(jsonObject.optJSONArray(PREFIX_RECENT), new ResultResponse<String>() {
@Override
public boolean onResultRecive(String result) {
AppDB.get().addRecent(result);
return false;
}
});
jsonToMeta(jsonObject.optJSONArray(PREFIX_STARS_Books), new ResultResponse<String>() {
@Override
public boolean onResultRecive(String result) {
AppDB.get().addStarFile(result);
return false;
}
});
jsonToMeta(jsonObject.optJSONArray(PREFIX_STARS_Folders), new ResultResponse<String>() {
@Override
public boolean onResultRecive(String result) {
AppDB.get().addStarFolder(result);
return false;
}
});
jsonTagsToMeta(jsonObject.optJSONArray(PREFIX_TAGS_BOOKS), new ResultResponse<Pair<String, String>>() {
@Override
public boolean onResultRecive(Pair<String, String> result) {
try {
if (new File(result.first).isFile()) {
FileMeta meta = AppDB.get().getOrCreate(result.first);
meta.setTag(result.second);
AppDB.get().update(meta);
}
} catch (Exception e) {
LOG.e(e);
}
return false;
}
});
return true;
} catch (Exception e) {
LOG.e(e);
Toast.makeText(c, e.getMessage(), Toast.LENGTH_LONG).show();
}
return false;
}
Aggregations