Search in sources :

Example 1 with Consumer

use of im.actor.runtime.function.Consumer in project actor-platform by actorapp.

the class BaseFragment method execute.

public <T> Promise<T> execute(Promise<T> promise, int title) {
    final ProgressDialog dialog = ProgressDialog.show(getContext(), "", getString(title), true, false);
    promise.then(new Consumer<T>() {

        @Override
        public void apply(T t) {
            dismissDialog(dialog);
        }
    }).failure(new Consumer<Exception>() {

        @Override
        public void apply(Exception e) {
            dismissDialog(dialog);
        }
    });
    return promise;
}
Also used : Consumer(im.actor.runtime.function.Consumer) ProgressDialog(android.app.ProgressDialog)

Example 2 with Consumer

use of im.actor.runtime.function.Consumer in project actor-platform by actorapp.

the class BlockedListFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View res = inflater.inflate(R.layout.fragment_recycler_list, container, false);
    list = (RecyclerView) res.findViewById(R.id.listView);
    list.setLayoutManager(new LinearLayoutManager(getActivity()));
    emptyView = (TextView) res.findViewById(R.id.emptyView);
    emptyView.setText(R.string.blocked_loading);
    res.setBackgroundColor(ActorSDK.sharedActor().style.getBackyardBackgroundColor());
    emptyView.setTextColor(ActorSDK.sharedActor().style.getTextSecondaryColor());
    adapter = new BlockedAdapter(new ArrayList<User>(), new BlockedAdapter.OnBlockedClickListener() {

        @Override
        public void onClick(UserVM u) {
            execute(messenger().unblockUser(u.getId()).then(new Consumer<Void>() {

                @Override
                public void apply(Void aVoid) {
                    checkBlockedList();
                }
            }));
        }
    });
    list.setAdapter(adapter);
    checkBlockedList();
    return res;
}
Also used : UserVM(im.actor.core.viewmodel.UserVM) Consumer(im.actor.runtime.function.Consumer) ArrayList(java.util.ArrayList) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) Void(im.actor.runtime.actors.messages.Void) RecyclerView(android.support.v7.widget.RecyclerView) TextView(android.widget.TextView) View(android.view.View)

Example 3 with Consumer

use of im.actor.runtime.function.Consumer in project actor-platform by actorapp.

the class KeyManagerActor method onMainKeysReady.

private void onMainKeysReady() {
    Log.d(TAG, "Main Keys are ready");
    //
    // Generation required pre keys
    //
    int missingKeysCount = Math.max(0, Configuration.EPHEMERAL_KEYS_COUNT - ownKeys.getPreKeys().length);
    if (missingKeysCount > 0) {
        ownKeys = ownKeys.appendPreKeys(ManagedList.of(PrivateKey.GENERATOR, missingKeysCount).toArray(new PrivateKey[0]));
        encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
    }
    //
    // Uploading own pre keys
    //
    final ManagedList<PrivateKey> pendingEphermalKeys = ManagedList.of(ownKeys.getPreKeys()).filter(PrivateKey.NOT_UPLOADED);
    if (pendingEphermalKeys.size() > 0) {
        ArrayList<ApiEncryptionKey> uploadingKeys = pendingEphermalKeys.map(PrivateKey.TO_API);
        ArrayList<ApiEncryptionKeySignature> uploadingSignatures = pendingEphermalKeys.map(PrivateKey.SIGN(ownKeys.getIdentityKey()));
        api(new RequestUploadPreKey(ownKeys.getKeyGroupId(), uploadingKeys, uploadingSignatures)).then(new Consumer<ResponseVoid>() {

            @Override
            public void apply(ResponseVoid responseVoid) {
                ownKeys = ownKeys.markAsUploaded(pendingEphermalKeys.toArray(new PrivateKey[pendingEphermalKeys.size()]));
                encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
                onAllKeysReady();
            }
        }).failure(new Consumer<Exception>() {

            @Override
            public void apply(Exception e) {
                Log.w(TAG, "Ephemeral keys upload error");
                Log.e(TAG, e);
            // Ignore. This will freeze all encryption operations.
            }
        });
    } else {
        onAllKeysReady();
    }
}
Also used : ResponseVoid(im.actor.core.api.rpc.ResponseVoid) PrivateKey(im.actor.core.modules.encryption.entity.PrivateKey) RequestUploadPreKey(im.actor.core.api.rpc.RequestUploadPreKey) IOException(java.io.IOException) Consumer(im.actor.runtime.function.Consumer) ApiEncryptionKeySignature(im.actor.core.api.ApiEncryptionKeySignature) ApiEncryptionKey(im.actor.core.api.ApiEncryptionKey)

Example 4 with Consumer

use of im.actor.runtime.function.Consumer in project actor-platform by actorapp.

the class EncryptedPeerActor method doEncrypt.

private Promise<EncryptBoxResponse> doEncrypt(final byte[] data) {
    if (!isReady) {
        stash();
        return null;
    }
    //
    // Stage 1: Loading User Key Groups
    // Stage 2: Pick sessions for encryption
    // Stage 3: Encrypt box_key int session
    // Stage 4: Encrypt box
    //
    final byte[] encKey = Crypto.randomBytes(32);
    final byte[] encKeyExtended = keyPrf.calculate(encKey, "ActorPackage", 128);
    Log.d(TAG, "doEncrypt");
    final long start = Runtime.getActorTime();
    return PromisesArray.of(theirKeys.getUserKeysGroups()).filter(new Predicate<UserKeysGroup>() {

        @Override
        public boolean apply(UserKeysGroup keysGroup) {
            return !ignoredKeyGroups.contains(keysGroup.getKeyGroupId());
        }
    }).mapOptional(new Function<UserKeysGroup, Promise<SessionActor>>() {

        @Override
        public Promise<SessionActor> apply(final UserKeysGroup keysGroup) {
            if (activeSessions.containsKey(keysGroup.getKeyGroupId())) {
                return success(activeSessions.get(keysGroup.getKeyGroupId()).getSessions().get(0));
            }
            return context().getEncryption().getSessionManagerInt().pickSession(uid, keysGroup.getKeyGroupId()).failure(new Consumer<Exception>() {

                @Override
                public void apply(Exception e) {
                    ignoredKeyGroups.add(keysGroup.getKeyGroupId());
                }
            }).map(new Function<PeerSession, SessionActor>() {

                @Override
                public SessionActor apply(PeerSession src) {
                    return spawnSession(src);
                }
            });
        }
    }).mapOptional(encrypt(encKeyExtended)).zip().map(new Function<List<EncryptedSessionActor.EncryptedPackageRes>, EncryptBoxResponse>() {

        @Override
        public EncryptBoxResponse apply(List<EncryptedSessionActor.EncryptedPackageRes> src) {
            if (src.size() == 0) {
                throw new RuntimeException("No sessions available");
            }
            Log.d(TAG, "Keys Encrypted in " + (Runtime.getActorTime() - start) + " ms");
            ArrayList<EncryptedBoxKey> encryptedKeys = new ArrayList<>();
            for (EncryptedSessionActor.EncryptedPackageRes r : src) {
                Log.d(TAG, "Keys: " + r.getKeyGroupId());
                encryptedKeys.add(new EncryptedBoxKey(uid, r.getKeyGroupId(), "curve25519", r.getData()));
            }
            byte[] encData;
            try {
                encData = ActorBox.closeBox(ByteStrings.intToBytes(ownKeyGroupId), data, Crypto.randomBytes(32), new ActorBoxKey(encKeyExtended));
            } catch (IntegrityException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
            Log.d(TAG, "All Encrypted in " + (Runtime.getActorTime() - start) + " ms");
            return new EncryptBoxResponse(new EncryptedBox(encryptedKeys.toArray(new EncryptedBoxKey[encryptedKeys.size()]), ByteStrings.merge(ByteStrings.intToBytes(ownKeyGroupId), encData)));
        }
    });
}
Also used : ArrayList(java.util.ArrayList) IntegrityException(im.actor.runtime.crypto.IntegrityException) UserKeysGroup(im.actor.core.modules.encryption.entity.UserKeysGroup) IOException(java.io.IOException) IntegrityException(im.actor.runtime.crypto.IntegrityException) Function(im.actor.runtime.function.Function) Consumer(im.actor.runtime.function.Consumer) PeerSession(im.actor.core.entity.encryption.PeerSession) EncryptedBoxKey(im.actor.core.modules.encryption.entity.EncryptedBoxKey) EncryptedBox(im.actor.core.modules.encryption.entity.EncryptedBox) ArrayList(java.util.ArrayList) List(java.util.List) ActorBoxKey(im.actor.runtime.crypto.box.ActorBoxKey)

Example 5 with Consumer

use of im.actor.runtime.function.Consumer in project actor-platform by actorapp.

the class KeyManagerActor method preStart.

@Override
public void preStart() {
    Log.d(TAG, "Starting KeyManager...");
    //
    // Initialization key storage
    //
    encryptionKeysStorage = Storage.createKeyValue("encryption_keys");
    //
    // Initialization own private keys
    //
    ownKeys = null;
    byte[] ownKeysStorage = encryptionKeysStorage.loadItem(0);
    if (ownKeysStorage != null) {
        try {
            ownKeys = new PrivateKeyStorage(ownKeysStorage);
            // If we need re-save key storage
            if (ownKeys.isWasRegenerated()) {
                encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    if (ownKeys == null) {
        Curve25519KeyPair identityPrivate = Curve25519.keyGen(Crypto.randomBytes(64));
        Curve25519KeyPair key0 = Curve25519.keyGen(Crypto.randomBytes(64));
        ownKeys = new PrivateKeyStorage(0, new PrivateKey(RandomUtils.nextRid(), "curve25519", identityPrivate.getPrivateKey(), identityPrivate.getPublicKey()), new PrivateKey[] { new PrivateKey(RandomUtils.nextRid(), "curve25519", key0.getPrivateKey(), key0.getPublicKey()) }, new PrivateKey[0]);
        encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
    }
    if (ownKeys.getKeyGroupId() == 0) {
        ApiEncryptionKey identityKey = ownKeys.getIdentityKey().toApiKey();
        ArrayList<ApiEncryptionKey> keys = ManagedList.of(ownKeys.getKeys()).map(PrivateKey.TO_API);
        ArrayList<ApiEncryptionKeySignature> signatures = ManagedList.of(ownKeys.getKeys()).map(PrivateKey.SIGN(ownKeys.getIdentityKey()));
        Log.d(TAG, "Creation of new key group");
        api(new RequestCreateNewKeyGroup(identityKey, Configuration.SUPPORTED, keys, signatures)).then(new Consumer<ResponseCreateNewKeyGroup>() {

            @Override
            public void apply(ResponseCreateNewKeyGroup response) {
                ownKeys = ownKeys.setGroupId(response.getKeyGroupId());
                encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
                onMainKeysReady();
            }
        }).failure(new Consumer<Exception>() {

            @Override
            public void apply(Exception e) {
                Log.w(TAG, "Keys upload error");
                Log.e(TAG, e);
            // Just ignore
            }
        });
    } else {
        onMainKeysReady();
    }
}
Also used : RequestCreateNewKeyGroup(im.actor.core.api.rpc.RequestCreateNewKeyGroup) PrivateKeyStorage(im.actor.core.modules.encryption.entity.PrivateKeyStorage) PrivateKey(im.actor.core.modules.encryption.entity.PrivateKey) ResponseCreateNewKeyGroup(im.actor.core.api.rpc.ResponseCreateNewKeyGroup) IOException(java.io.IOException) IOException(java.io.IOException) Consumer(im.actor.runtime.function.Consumer) ApiEncryptionKeySignature(im.actor.core.api.ApiEncryptionKeySignature) Curve25519KeyPair(im.actor.runtime.crypto.Curve25519KeyPair) ApiEncryptionKey(im.actor.core.api.ApiEncryptionKey)

Aggregations

Consumer (im.actor.runtime.function.Consumer)5 IOException (java.io.IOException)3 ApiEncryptionKey (im.actor.core.api.ApiEncryptionKey)2 ApiEncryptionKeySignature (im.actor.core.api.ApiEncryptionKeySignature)2 PrivateKey (im.actor.core.modules.encryption.entity.PrivateKey)2 ArrayList (java.util.ArrayList)2 ProgressDialog (android.app.ProgressDialog)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 RequestCreateNewKeyGroup (im.actor.core.api.rpc.RequestCreateNewKeyGroup)1 RequestUploadPreKey (im.actor.core.api.rpc.RequestUploadPreKey)1 ResponseCreateNewKeyGroup (im.actor.core.api.rpc.ResponseCreateNewKeyGroup)1 ResponseVoid (im.actor.core.api.rpc.ResponseVoid)1 PeerSession (im.actor.core.entity.encryption.PeerSession)1 EncryptedBox (im.actor.core.modules.encryption.entity.EncryptedBox)1 EncryptedBoxKey (im.actor.core.modules.encryption.entity.EncryptedBoxKey)1 PrivateKeyStorage (im.actor.core.modules.encryption.entity.PrivateKeyStorage)1 UserKeysGroup (im.actor.core.modules.encryption.entity.UserKeysGroup)1