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;
}
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;
}
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();
}
}
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)));
}
});
}
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();
}
}
Aggregations