From 6deb9fd51740c05caedda41cb89a529f5c1bfd61 Mon Sep 17 00:00:00 2001 From: Lyndon Date: Wed, 25 Oct 2017 17:40:11 +0200 Subject: [PATCH] - --- .../bewoplaner-chat-android/app/build.gradle | 8 +-- .../ownchat/page/chat/ChatPresenter.java | 53 ++++++++++++---- .../ownchat/page/login/LoginPresenter.java | 2 + .../ownchat/page/profile/ProfileFragment.java | 6 +- .../page/profile/ProfilePresenter.java | 60 ++++++++++++++++++- .../main/res/layout/item_group_chat_left.xml | 23 ++++--- .../main/res/layout/item_group_chat_right.xml | 19 +++--- .../main/res/layout/item_single_chat_left.xml | 15 +++-- .../res/layout/item_single_chat_right.xml | 15 ++--- 9 files changed, 151 insertions(+), 50 deletions(-) diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/build.gradle b/BeWoPlanerChat/bewoplaner-chat-android/app/build.gradle index ef018dc..824bcf2 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/build.gradle +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/build.gradle @@ -28,10 +28,10 @@ apply plugin: 'realm-android' android { compileSdkVersion 25 - buildToolsVersion "25.0.2" + buildToolsVersion "25.0.3" defaultConfig { applicationId "de.beyondsoft.ownchat" - minSdkVersion 16 + minSdkVersion 19 targetSdkVersion 25 versionCode 2 versionName "1.1" @@ -86,8 +86,8 @@ dependencies { // emojis // circle progress // event bus - compile 'com.android.support:appcompat-v7:25.3.0' - compile 'com.android.support:design:25.3.0' + compile 'com.android.support:appcompat-v7:25.3.1' + compile 'com.android.support:design:25.3.1' compile 'com.jakewharton:butterknife:8.4.0' compile 'com.google.dagger:dagger:2.7' compile 'io.reactivex:rxjava:1.1.6' diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/ChatPresenter.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/ChatPresenter.java index 44c8c2f..0c46939 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/ChatPresenter.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/ChatPresenter.java @@ -26,7 +26,6 @@ import de.beyondsoft.ownchat.utils.rx.RxUtils; import org.greenrobot.eventbus.EventBus; -import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -38,7 +37,6 @@ import android.util.Log; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -462,8 +460,6 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha // image/gif, image/jpeg, image/png if((mimeType.equals(Constants.MIME_TYPE_JPEG) || mimeType.equals(Constants.MIME_TYPE_PNG)) && (file.length() > (1000L * 1024L))) { - Log.i("IMAGE DOWN SCALING", "Trying to scale down the image"); - try { BitmapFactory.Options options = new BitmapFactory.Options(); @@ -503,10 +499,6 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha File scaledImage = new File(tmpFileLocation); boolean scaledImageExists = scaledImage.exists(); - if(scaledImageExists) { - Log.i("FILE SIZES", "" + ((int) (file.length() / 1024d)) + "Kb vs. " + (scaledImage.length() / 1024) + "Kb"); - } - // create RequestBody instance from file RequestBody requestFile = RequestBody.create(getView().getMediaType(fileUri), scaledImageExists ? scaledImage : file); @@ -553,8 +545,8 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha public void onNext(MessageResponse baseResponse) { if (scaledImageExists) { boolean delete = scaledImage.delete(); - Log.i("DELETED SCALED IMAGE", "Success: " + delete); } + deleteLocalMessage(message); baseResponse.response.message.status = Message.Status.DELIVERED; baseResponse.response.message.isRead = true; @@ -643,15 +635,50 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha return; } - // TODO: resize file - // image/gif, image/jpeg, image/png + String tmpFileLocation = mContext.getExternalFilesDir(null) + "/tmp_" + file.getName(); - if(mimeType.equals("image/jpeg") || mimeType.equals("image/png")) { + if((mimeType.equals(Constants.MIME_TYPE_JPEG) || mimeType.equals(Constants.MIME_TYPE_PNG)) && (file.length() > (1000L * 1024L))) { + try { + BitmapFactory.Options options = new BitmapFactory.Options(); + Bitmap bitmapToScale = BitmapFactory.decodeFile(file.getPath(), options); + + int originalWidth = options.outWidth; + int originalHeight = options.outHeight; + + int newHeight, newWidth; + double aspectRatio = ((double) originalWidth) / ((double) originalHeight); + + if(originalHeight > originalWidth) { + aspectRatio = ((double) originalHeight) / ((double) originalWidth); + newWidth = 1080; + newHeight = (int) (newWidth * aspectRatio); + } else { + newHeight = 1080; + newWidth = (int) (newHeight * aspectRatio); + } + + Bitmap bitmap = Bitmap.createScaledBitmap(bitmapToScale, newWidth, newHeight, false); + File tmpFile = new File(tmpFileLocation); + tmpFile.createNewFile(); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream); + FileOutputStream fileOutputStream = new FileOutputStream(tmpFile); + fileOutputStream.write(outputStream.toByteArray()); + bitmap.recycle(); + fileOutputStream.flush(); + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } } + File scaledImage = new File(tmpFileLocation); + boolean scaledImageExists = scaledImage.exists(); + // create RequestBody instance from file - RequestBody requestFile = RequestBody.create(getView().getMediaType(fileUri), file); + RequestBody requestFile = RequestBody.create(getView().getMediaType(fileUri), scaledImageExists ? scaledImage : file); // MultipartBody.Part is used to send also the actual file name MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/login/LoginPresenter.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/login/LoginPresenter.java index a2235d7..fc21e00 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/login/LoginPresenter.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/login/LoginPresenter.java @@ -527,6 +527,8 @@ public class LoginPresenter extends MVPAbstractPresenter implements L if(result == 0) { deleteData(true); shouldClearAllPrefs = true; + + // TODO: Der Chat-Code wurde deaktiviert } if(result == 1) { diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfileFragment.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfileFragment.java index d4d3fb0..cf26571 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfileFragment.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfileFragment.java @@ -134,6 +134,7 @@ public class ProfileFragment extends RuntimePermissionBaseFragment implements Pr final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // Check for the freshest data. try { + //noinspection WrongConstant getActivity().getContentResolver().takePersistableUriPermission(uri, takeFlags); } catch (Exception e) { e.printStackTrace(); @@ -210,8 +211,7 @@ public class ProfileFragment extends RuntimePermissionBaseFragment implements Pr @OnTextChanged(value = R.id.profile_phone_number, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED) void phoneNumberChanged() { - mUpdateProfileButton.setEnabled(!TextUtils.isEmpty(mPhoneNumberEditText.getText().toString()) - && mPhoneNumberEditText.getText().toString().trim().length() > 0); + mUpdateProfileButton.setEnabled(!TextUtils.isEmpty(mPhoneNumberEditText.getText().toString()) && mPhoneNumberEditText.getText().toString().trim().length() > 0); } @Override @@ -227,12 +227,14 @@ public class ProfileFragment extends RuntimePermissionBaseFragment implements Pr private void selectFile() { Intent intent; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); } else { intent = new Intent(Intent.ACTION_GET_CONTENT); } + intent.setType("image/*"); intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfilePresenter.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfilePresenter.java index d9aa0e2..c70f3b2 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfilePresenter.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/profile/ProfilePresenter.java @@ -8,13 +8,21 @@ import de.beyondsoft.ownchat.data.prefs.StringPreference; import de.beyondsoft.ownchat.di.scopes.ActivityScope; import de.beyondsoft.ownchat.model.User; import de.beyondsoft.ownchat.page.mvp.MVPAbstractPresenter; +import de.beyondsoft.ownchat.utils.Constants; +import de.beyondsoft.ownchat.utils.FileUtils; import de.beyondsoft.ownchat.utils.SystemUtils; import de.beyondsoft.ownchat.utils.rx.RxUtils; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.Uri; import android.support.annotation.NonNull; +import java.io.ByteArrayOutputStream; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import javax.inject.Inject; import javax.inject.Named; @@ -41,6 +49,9 @@ class ProfilePresenter extends MVPAbstractPresenter implements Prof @Named(AppPrefsConstants.LOGIN_TOKEN) StringPreference mToken; + @Inject + Context mContext; + @Inject ProfilePresenter() { } @@ -97,12 +108,59 @@ class ProfilePresenter extends MVPAbstractPresenter implements Prof } File file = getView().getFile(uri); - RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file); + + String tmpFileLocation = mContext.getExternalFilesDir(null) + "/tmp_" + file.getName(); + + String mimeType = FileUtils.getMimeType(mContext, uri); + + if((mimeType.equals(Constants.MIME_TYPE_JPEG) || mimeType.equals(Constants.MIME_TYPE_PNG)) && (file.length() > (1000L * 1024L))) { + try { + BitmapFactory.Options options = new BitmapFactory.Options(); + + Bitmap bitmapToScale = BitmapFactory.decodeFile(file.getPath(), options); + + int originalWidth = options.outWidth; + int originalHeight = options.outHeight; + + int newHeight, newWidth; + double aspectRatio = ((double) originalWidth) / ((double) originalHeight); + + if(originalHeight > originalWidth) { + aspectRatio = ((double) originalHeight) / ((double) originalWidth); + newWidth = 1080; + newHeight = (int) (newWidth * aspectRatio); + } else { + newHeight = 1080; + newWidth = (int) (newHeight * aspectRatio); + } + + Bitmap bitmap = Bitmap.createScaledBitmap(bitmapToScale, newWidth, newHeight, false); + File tmpFile = new File(tmpFileLocation); + tmpFile.createNewFile(); + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream); + FileOutputStream fileOutputStream = new FileOutputStream(tmpFile); + fileOutputStream.write(outputStream.toByteArray()); + bitmap.recycle(); + fileOutputStream.flush(); + fileOutputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + File scaledImage = new File(tmpFileLocation); + boolean scaledImageExists = scaledImage.exists(); + + RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), scaledImageExists ? scaledImage : file); MultipartBody.Part part = MultipartBody.Part.createFormData("picture", file.getName(), requestBody); + final Subscription subscription = mProfileService.uploadProfileImage(part) .compose(RxUtils.provideDefaultTransformer()) .map(uploadImageResponse -> uploadImageResponse.response.picture) .subscribe(this::uploadImageSuccess, this::profileError); + addSubscription(subscription); } diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_group_chat_left.xml b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_group_chat_left.xml index ff76bf0..5e7ef35 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_group_chat_left.xml +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_group_chat_left.xml @@ -41,31 +41,35 @@ android:textColor="@color/colorManatee" android:textSize="10sp" tools:ignore="SmallSp" - tools:text="Sasha Grey | 15:23" /> + tools:text="Sasha Grey | 15:23" + tools:visibility="visible"/> + android:scaleType="fitCenter" + android:visibility="gone" + tools:src="@drawable/document" + tools:visibility="visible" /> + android:visibility="gone" + tools:visibility="visible" + tools:text="MyFile.txt"/> + android:visibility="gone" + tools:visibility="visible"/> + android:visibility="gone" + tools:visibility="visible" + tools:text="MyFile.txt"/> + tools:visibility="visible"/> diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_left.xml b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_left.xml index e49df78..01cad82 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_left.xml +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_left.xml @@ -33,17 +33,20 @@ android:layout_marginTop="4dp" android:adjustViewBounds="true" android:contentDescription="@null" - android:scaleType="fitStart" - android:visibility="gone" /> + android:scaleType="fitCenter" + android:visibility="gone" + tools:src="@drawable/document" + tools:visibility="visible" /> + android:layout_below="@+id/chat_item_file" + android:textColor="@color/colorBlack" + tools:visibility="visible" + tools:text="MyFile.txt"/> diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_right.xml b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_right.xml index aed7ccd..88bb35c 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_right.xml +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/item_single_chat_right.xml @@ -63,7 +63,7 @@ android:layout_toEndOf="@+id/chat_item_time" android:contentDescription="@null" android:visibility="gone" - tools:visibility="gone" + tools:visibility="visible" /> + tools:src="@drawable/document" + tools:visibility="visible" />