Search in sources :

Example 1 with ProfileMediaConstraints

use of org.thoughtcrime.securesms.profiles.ProfileMediaConstraints in project Signal-Android by WhisperSystems.

the class EditSelfProfileRepository method getCurrentAvatar.

@Override
public void getCurrentAvatar(@NonNull Consumer<byte[]> avatarConsumer) {
    RecipientId selfId = Recipient.self().getId();
    if (AvatarHelper.hasAvatar(context, selfId)) {
        SimpleTask.run(() -> {
            try {
                return StreamUtil.readFully(AvatarHelper.getAvatar(context, selfId));
            } catch (IOException e) {
                Log.w(TAG, e);
                return null;
            }
        }, avatarConsumer::accept);
    } else if (!excludeSystem) {
        SystemProfileUtil.getSystemProfileAvatar(context, new ProfileMediaConstraints()).addListener(new ListenableFuture.Listener<byte[]>() {

            @Override
            public void onSuccess(byte[] result) {
                avatarConsumer.accept(result);
            }

            @Override
            public void onFailure(ExecutionException e) {
                Log.w(TAG, e);
                avatarConsumer.accept(null);
            }
        });
    }
}
Also used : RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) ProfileMediaConstraints(org.thoughtcrime.securesms.profiles.ProfileMediaConstraints) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ProfileMediaConstraints

use of org.thoughtcrime.securesms.profiles.ProfileMediaConstraints in project Signal-Android by signalapp.

the class CreateProfileActivity method initializeProfileAvatar.

private void initializeProfileAvatar(boolean excludeSystem) {
    Address ourAddress = Address.fromSerialized(TextSecurePreferences.getLocalNumber(this));
    if (AvatarHelper.getAvatarFile(this, ourAddress).exists() && AvatarHelper.getAvatarFile(this, ourAddress).length() > 0) {
        new AsyncTask<Void, Void, byte[]>() {

            @Override
            protected byte[] doInBackground(Void... params) {
                try {
                    return Util.readFully(AvatarHelper.getInputStreamFor(CreateProfileActivity.this, ourAddress));
                } catch (IOException e) {
                    Log.w(TAG, e);
                    return null;
                }
            }

            @Override
            protected void onPostExecute(byte[] result) {
                if (result != null) {
                    avatarBytes = result;
                    GlideApp.with(CreateProfileActivity.this).load(result).circleCrop().into(avatar);
                }
            }
        }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    } else if (!excludeSystem) {
        SystemProfileUtil.getSystemProfileAvatar(this, new ProfileMediaConstraints()).addListener(new ListenableFuture.Listener<byte[]>() {

            @Override
            public void onSuccess(byte[] result) {
                if (result != null) {
                    avatarBytes = result;
                    GlideApp.with(CreateProfileActivity.this).load(result).circleCrop().into(avatar);
                }
            }

            @Override
            public void onFailure(ExecutionException e) {
                Log.w(TAG, e);
            }
        });
    }
}
Also used : Address(org.thoughtcrime.securesms.database.Address) ProfileMediaConstraints(org.thoughtcrime.securesms.profiles.ProfileMediaConstraints) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 3 with ProfileMediaConstraints

use of org.thoughtcrime.securesms.profiles.ProfileMediaConstraints in project Signal-Android by signalapp.

the class CreateProfileActivity method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode) {
        case REQUEST_CODE_AVATAR:
            if (resultCode == Activity.RESULT_OK) {
                Uri outputFile = Uri.fromFile(new File(getCacheDir(), "cropped"));
                Uri inputFile = (data != null ? data.getData() : null);
                if (inputFile == null && captureFile != null) {
                    inputFile = Uri.fromFile(captureFile);
                }
                if (data != null && data.getBooleanExtra("delete", false)) {
                    avatarBytes = null;
                    avatar.setImageDrawable(new ResourceContactPhoto(R.drawable.ic_camera_alt_white_24dp).asDrawable(this, getResources().getColor(R.color.grey_400)));
                } else {
                    new Crop(inputFile).output(outputFile).asSquare().start(this);
                }
            }
            break;
        case Crop.REQUEST_CROP:
            if (resultCode == Activity.RESULT_OK) {
                new AsyncTask<Void, Void, byte[]>() {

                    @Override
                    protected byte[] doInBackground(Void... params) {
                        try {
                            return BitmapUtil.createScaledBytes(CreateProfileActivity.this, Crop.getOutput(data), new ProfileMediaConstraints());
                        } catch (BitmapDecodingException e) {
                            Log.w(TAG, e);
                            return null;
                        }
                    }

                    @Override
                    protected void onPostExecute(byte[] result) {
                        if (result != null) {
                            avatarBytes = result;
                            GlideApp.with(CreateProfileActivity.this).load(avatarBytes).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).circleCrop().into(avatar);
                        } else {
                            Toast.makeText(CreateProfileActivity.this, R.string.CreateProfileActivity_error_setting_profile_photo, Toast.LENGTH_LONG).show();
                        }
                    }
                }.execute();
            }
            break;
    }
}
Also used : Crop(com.soundcloud.android.crop.Crop) ResourceContactPhoto(org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto) ProfileMediaConstraints(org.thoughtcrime.securesms.profiles.ProfileMediaConstraints) Uri(android.net.Uri) File(java.io.File) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException)

Aggregations

ProfileMediaConstraints (org.thoughtcrime.securesms.profiles.ProfileMediaConstraints)3 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Uri (android.net.Uri)1 Crop (com.soundcloud.android.crop.Crop)1 File (java.io.File)1 ResourceContactPhoto (org.thoughtcrime.securesms.contacts.avatars.ResourceContactPhoto)1 Address (org.thoughtcrime.securesms.database.Address)1 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)1 BitmapDecodingException (org.thoughtcrime.securesms.util.BitmapDecodingException)1