Search in sources :

Example 41 with Pair

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);
        }
    });
}
Also used : Status(com.keylesspalace.tusky.entity.Status) StatusViewData(com.keylesspalace.tusky.viewdata.StatusViewData) Pair(android.support.v4.util.Pair)

Example 42 with Pair

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);
}
Also used : SignedSignatureAttributes(co.krypt.krypton.pgp.packet.SignedSignatureAttributes) EOFException(java.io.EOFException) PacketHeader(co.krypt.krypton.pgp.packet.PacketHeader) List(java.util.List) LinkedList(java.util.LinkedList) UserIDPacket(co.krypt.krypton.pgp.packet.UserIDPacket) LinkedList(java.util.LinkedList) Pair(android.support.v4.util.Pair)

Example 43 with Pair

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);
}
Also used : Scanner(java.util.Scanner) ByteBuffer(java.nio.ByteBuffer) LinkedList(java.util.LinkedList) Pair(android.support.v4.util.Pair)

Example 44 with Pair

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")));
}
Also used : AsciiArmor(co.krypt.krypton.pgp.asciiarmor.AsciiArmor) CommitInfo(co.krypt.krypton.git.CommitInfo) Pair(android.support.v4.util.Pair) Test(org.junit.Test)

Example 45 with Pair

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;
}
Also used : Scanner(java.util.Scanner) JSONObject(org.json.JSONObject) File(java.io.File) FileMeta(com.foobnix.dao2.FileMeta) JSONException(org.json.JSONException) Pair(android.support.v4.util.Pair)

Aggregations

Pair (android.support.v4.util.Pair)79 ArrayList (java.util.ArrayList)39 View (android.view.View)28 Pair (org.apache.commons.math3.util.Pair)20 ActivityOptionsCompat (android.support.v4.app.ActivityOptionsCompat)16 TextView (android.widget.TextView)15 Intent (android.content.Intent)14 List (java.util.List)13 ImageView (android.widget.ImageView)10 ByteProcessor (ij.process.ByteProcessor)9 RecyclerView (android.support.v7.widget.RecyclerView)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 AlertDialog (android.support.v7.app.AlertDialog)7 Pair (mpicbg.trakem2.util.Pair)7 NonNull (android.support.annotation.NonNull)6 Patch (ini.trakem2.display.Patch)6 OsmandSettings (net.osmand.plus.OsmandSettings)6 DialogInterface (android.content.DialogInterface)5 IOException (java.io.IOException)5