diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/BaseChatActivity.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/BaseChatActivity.java index c2a17ce..3055288 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/BaseChatActivity.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/BaseChatActivity.java @@ -281,7 +281,7 @@ abstract class BaseChatActivity extends RuntimePermissionActivity implements Cha private void selectFile(int requestCode) { // TODO: show attachment type dialog first - final String[] zipFileTypes = {".zip", ".rar", ".7zip"}; + final String[] zipFileTypes = {".zip", ".rar", ".7z"}; final String[] pdfFileTypes = {".pdf"}; final String[] docFileTypes = {".txt", ".doc", ".xls", ".docx", ".xlsx", ".odt"}; @@ -337,7 +337,10 @@ abstract class BaseChatActivity extends RuntimePermissionActivity implements Cha return; } - if (requestCode == SELECT_IMAGE_FILE || requestCode == SELECT_DOC_FILE) { + ArrayList var1 = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA); + ArrayList var2 = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS); + + if ((requestCode == SELECT_IMAGE_FILE || requestCode == SELECT_DOC_FILE) && ((var1 != null && var1.size() > 0) || (var2 != null && var2.size() > 0))) { String filePath = data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA) != null ? data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_MEDIA).get(0) : data.getStringArrayListExtra(FilePickerConst.KEY_SELECTED_DOCS).get(0); 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 b6af0c1..4d19aa1 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 @@ -135,21 +135,18 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha Subscription subscription = mChatService.getMessages(groupId, mPage) .compose(RxUtils.provideDefaultTransformer()) .flatMap(new ErrorHandlingResponseConverter<>()) - .flatMap(new Func1>>() { - @Override - public Observable> call(MessagesResponse messageResponse) { - ArrayList messages = messageResponse.response.messages; + .flatMap(messageResponse -> { + ArrayList messages = messageResponse.response.messages; - for (int i = 0, size = messages.size(); i < size; i++) { - messages.get(i).status = Message.Status.DELIVERED; - messages.get(i).isRead = true; - messages.get(i).userId = mUserIdPreference.get(); - } - - mFetchMore = messageResponse.response.hasMorePages; - - return Observable.just(messageResponse.response.messages); + for (int i = 0, size = messages.size(); i < size; i++) { + messages.get(i).status = Message.Status.DELIVERED; + messages.get(i).isRead = true; + messages.get(i).userId = mUserIdPreference.get(); } + + mFetchMore = messageResponse.response.hasMorePages; + + return Observable.just(messageResponse.response.messages); }) .subscribe(this::messagesSuccess, this::messagesError); addSubscription(subscription); @@ -427,17 +424,27 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha return; } - String tmpFileLocation = FileUtils.getTmpFilesDir(mContext) + "/tmp_" + file.getName(); - FileUtils.clearTmpFilesDir(mContext); + if(mUploadMaxSizePreference.get() > 0 && file.length() > mUploadMaxSizePreference.get()) { + getView().showError(R.string.error_file_is_too_large); + return; + } - String[] split = file.getAbsolutePath().split("/"); + if(mimeType != null && (mimeType.equals(Constants.MIME_TYPE_JPEG) || mimeType.equals(Constants.MIME_TYPE_PNG)) && (file.length() > (1000L * 1024L))) { + FileUtils.clearTmpFilesDir(mContext); + + file = FileUtils.scaleImageDown(file, mContext); + } + + final File fileToSend = file; + + String[] split = fileToSend.getAbsolutePath().split("/"); Message message = new Message(findUniqueRandomId(mRealmProvider, mUserIdPreference.get()), mGroupId, System.currentTimeMillis() / 1000, messageText, - file.getPath(), - file.getPath(), + fileToSend.getPath(), + fileToSend.getPath(), split[split.length - 1], fileUri.toString(), mUserIdPreference.get(), @@ -464,60 +471,11 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha message.createdAt *= 1000; getView().addOneMessage(message); - 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(); - - File fileToSend = scaledImageExists ? scaledImage : file; - - Log.d("FILE_SIZE", "" + file.length() + " vs. " + mUploadMaxSizePreference.get()); - - if(mUploadMaxSizePreference.get() > 0L && fileToSend.length() > mUploadMaxSizePreference.get()){ - getView().showError(R.string.error_file_is_too_large); - return; - } - // create RequestBody instance from file RequestBody requestFile = RequestBody.create(getView().getMediaType(mimeType), fileToSend); // MultipartBody.Part is used to send also the actual file name - MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); + MultipartBody.Part body = MultipartBody.Part.createFormData("file", fileToSend.getName(), requestFile); // add another part within the multipart request String groupIdString = String.valueOf(mGroupId); @@ -535,6 +493,7 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha @Override public void onError(Throwable e) { + FileUtils.clearTmpFilesDir(mContext); Log.e("sendFile", "onError", e); @SuppressWarnings("UnnecessaryLocalVariable") Message newMessage = message; newMessage.createdAt /= 1000; @@ -557,8 +516,7 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha @Override public void onNext(MessageResponse baseResponse) { - boolean delete = scaledImage.delete(); - + FileUtils.clearTmpFilesDir(mContext); deleteLocalMessage(message); baseResponse.response.message.status = Message.Status.DELIVERED; baseResponse.response.message.isRead = true; @@ -632,13 +590,13 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha @Override public void resendMessage(Message message, String mimeType) { - if (mSystemUtils.isNetworkUnavailable()) { + if(mSystemUtils.isNetworkUnavailable()) { getView().enableMessageRetry(message.id); getView().showError(R.string.error_no_internet_connection); return; } - if (TextUtils.isEmpty(message.message)) { + if(!TextUtils.isEmpty(message.filePath)) { Uri fileUri = Uri.parse(message.uri); File file = getView().getFile(fileUri); @@ -647,50 +605,21 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha return; } - String tmpFileLocation = mContext.getExternalFilesDir(null) + "/tmp_" + file.getName(); - - 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(); - } + if(mUploadMaxSizePreference.get() > 0 && file.length() > mUploadMaxSizePreference.get()) { + getView().showError(R.string.error_file_is_too_large); + return; } - File scaledImage = new File(tmpFileLocation); - boolean scaledImageExists = scaledImage.exists(); + if(mimeType != null && (mimeType.equals(Constants.MIME_TYPE_JPEG) || mimeType.equals(Constants.MIME_TYPE_PNG)) && (file.length() > (1000L * 1024L))) { + FileUtils.clearTmpFilesDir(mContext); + + file = FileUtils.scaleImageDown(file, mContext); + } + + final File fileToSend = file; // create RequestBody instance from file - RequestBody requestFile = RequestBody.create(getView().getMediaType(fileUri), scaledImageExists ? scaledImage : file); + RequestBody requestFile = RequestBody.create(getView().getMediaType(fileUri), fileToSend); // MultipartBody.Part is used to send also the actual file name MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile); @@ -711,20 +640,27 @@ public class ChatPresenter extends MVPAbstractPresenter implements Cha @Override public void onError(Throwable e) { + FileUtils.clearTmpFilesDir(mContext); + Log.e("resendMessage", "onError", e); + if (!isAttached()) { return; } + getView().enableMessageRetry(message.id); + if (mSystemUtils.isNetworkUnavailable()) { getView().showError(R.string.error_no_internet_connection); return; } + getView().showError(R.string.error_retry_failed); } @Override public void onNext(MessageResponse baseResponse) { + FileUtils.clearTmpFilesDir(mContext); deleteLocalMessage(message); baseResponse.response.message.status = Message.Status.DELIVERED; baseResponse.response.message.isRead = true; diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/GroupChatAdapter.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/GroupChatAdapter.java index f4fed3b..5d3801c 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/GroupChatAdapter.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/chat/GroupChatAdapter.java @@ -49,6 +49,7 @@ class GroupChatAdapter extends BaseChatAdapter { @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { int viewType = getItemViewType(position); + switch (viewType) { case LOADING_TYPE: break; diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/impressum/ImpressumActivity.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/impressum/ImpressumActivity.java index 8ea682d..0355703 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/impressum/ImpressumActivity.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/impressum/ImpressumActivity.java @@ -30,7 +30,7 @@ public class ImpressumActivity extends AppCompatActivity { setContentView(R.layout.activity_web_view); ButterKnife.bind(this); mWebView.setWebViewClient(new Callback()); - mWebView.loadUrl("file:///android_asset/impressum.html"); + mWebView.loadUrl("https://www.ownchat.de/index.php/datenschutzerklaerung-app/"); } @OnClick(R.id.impressum_toolbar_back_arrow) 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 4a312c7..89436d4 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 @@ -373,6 +373,8 @@ public class LoginPresenter extends MVPAbstractPresenter implements L mStatus = serviceObject.get("Status").getAsInt(); } + + return Observable.just(false); }) .compose(RxUtils.provideDefaultTransformer()) @@ -496,7 +498,6 @@ public class LoginPresenter extends MVPAbstractPresenter implements L @Override public void tryToLogout(String deviceId, MainNavigation mainNavigation) { - // TODO: what if the password save checkbox is set and the user logs out? if (mSavePasswordPreference.get()) { return; } @@ -546,7 +547,7 @@ public class LoginPresenter extends MVPAbstractPresenter implements L deleteData(true); shouldClearAllPrefs = true; - // TODO: Der Chat-Code wurde deaktiviert + getView().showError(R.string.error_deactivated_chat_code); } if(result == 1) { diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/main/MainActivity.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/main/MainActivity.java index d978c53..f6c8c20 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/main/MainActivity.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/page/main/MainActivity.java @@ -24,6 +24,7 @@ import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; +import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.content.Intent; @@ -38,6 +39,7 @@ import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; @@ -105,25 +107,23 @@ public class MainActivity extends AppCompatActivity implements MainNavigation, L InjectionHelper.getMainComponent(this).inject(this); EventBus.getDefault().register(this); setSupportActionBar(mToolbar); + + View headerLayout = mNavigationView.getHeaderView(0); + CircleImageView avatar = headerLayout.findViewById(R.id.menu_profile_imageView); + Glide.with(this).load(mAvatarPreference.get()).into(avatar); + ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, mDrawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) { @Override public void onDrawerOpened(View drawerView) { super.onDrawerOpened(drawerView); + Glide.with(MainActivity.this).load(mAvatarPreference.get()).into(avatar); Utils.hideKeyboard(MainActivity.this); } }; mDrawer.closeDrawer(GravityCompat.START); mDrawer.addDrawerListener(toggle); - View headerLayout = mNavigationView.getHeaderView(0); - CircleImageView avatar = (CircleImageView) headerLayout.findViewById(R.id.menu_profile_imageView); - Glide.with(this).load(mAvatarPreference.get()).into(avatar); - - mDrawer.setOnTouchListener((view, motionEvent) -> { - Glide.with(view.getContext()).load(mAvatarPreference.get()).into(avatar); - return false; - }); toggle.syncState(); ArrayAdapter adapter = new NavigationMenuAdapter(this, getResources().getStringArray(R.array.nav_menu), this); @@ -132,6 +132,7 @@ public class MainActivity extends AppCompatActivity implements MainNavigation, L onNewIntent(getIntent()); } + @SuppressLint("MissingSuperCall") @Override protected void onSaveInstanceState(Bundle outState) {} 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 814ad0c..b280193 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 @@ -3,9 +3,11 @@ package de.beyondsoft.ownchat.page.profile; import com.bumptech.glide.Glide; import de.beyondsoft.ownchat.R; import de.beyondsoft.ownchat.data.AppPrefsConstants; +import de.beyondsoft.ownchat.data.prefs.LongPreference; import de.beyondsoft.ownchat.data.prefs.StringPreference; import de.beyondsoft.ownchat.di.InjectionHelper; import de.beyondsoft.ownchat.model.User; +import de.beyondsoft.ownchat.utils.Constants; import de.beyondsoft.ownchat.utils.DialogHandler; import de.beyondsoft.ownchat.utils.FileUtils; import de.beyondsoft.ownchat.utils.RuntimePermissionBaseFragment; @@ -78,6 +80,10 @@ public class ProfileFragment extends RuntimePermissionBaseFragment implements Pr @Named(AppPrefsConstants.PHONE_NUMBER) StringPreference mPhoneNumberPreference; + @Inject + @Named(AppPrefsConstants.UPLOAD_MAX_SIZE) + LongPreference mUploadMaxSizePreference; + @Inject ProfilePresenter mProfilePresenter; @@ -100,6 +106,7 @@ public class ProfileFragment extends RuntimePermissionBaseFragment implements Pr super.onViewCreated(view, savedInstanceState); ButterKnife.bind(this, view); getActivity().setTitle(R.string.title_profile); + Glide.with(getActivity()) .load(mAvatarPreference.get()) .placeholder(R.drawable.avatar) @@ -154,6 +161,15 @@ public class ProfileFragment extends RuntimePermissionBaseFragment implements Pr } mProgressBar.setVisibility(View.VISIBLE); + + File file = new File(uri.getPath()); + + if(mUploadMaxSizePreference.get() > 0 && file.length() > mUploadMaxSizePreference.get()) { + mProgressBar.setVisibility(View.GONE); + showError(R.string.error_file_is_too_large); + return; + } + mCallback.uploadImage(uri); } } 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 c70f3b2..19fa5b6 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 @@ -4,6 +4,7 @@ import de.beyondsoft.ownchat.R; import de.beyondsoft.ownchat.data.AppPrefsConstants; import de.beyondsoft.ownchat.data.api.common.ErrorHandlingResponseConverter; import de.beyondsoft.ownchat.data.api.services.ProfileService; +import de.beyondsoft.ownchat.data.prefs.LongPreference; import de.beyondsoft.ownchat.data.prefs.StringPreference; import de.beyondsoft.ownchat.di.scopes.ActivityScope; import de.beyondsoft.ownchat.model.User; @@ -18,6 +19,7 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.support.annotation.NonNull; +import android.view.View; import java.io.ByteArrayOutputStream; import java.io.File; @@ -49,6 +51,10 @@ class ProfilePresenter extends MVPAbstractPresenter implements Prof @Named(AppPrefsConstants.LOGIN_TOKEN) StringPreference mToken; + @Inject + @Named(AppPrefsConstants.UPLOAD_MAX_SIZE) + LongPreference mUploadMaxSizePreference; + @Inject Context mContext; @@ -107,53 +113,21 @@ class ProfilePresenter extends MVPAbstractPresenter implements Prof return; } - File file = getView().getFile(uri); + File file = FileUtils.getFile(mContext, uri); + String mimeType = FileUtils.getMimeType(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(); - } + if(mUploadMaxSizePreference.get() > 0 && file.length() > mUploadMaxSizePreference.get()) { + getView().showError(R.string.error_file_is_too_large); + return; } - File scaledImage = new File(tmpFileLocation); - boolean scaledImageExists = scaledImage.exists(); + if(mimeType != null && (mimeType.equals(Constants.MIME_TYPE_JPEG) || mimeType.equals(Constants.MIME_TYPE_PNG)) && (file.length() > (1000L * 1024L))) { + FileUtils.clearTmpFilesDir(mContext); - RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), scaledImageExists ? scaledImage : file); + file = FileUtils.scaleImageDown(file, mContext); + } + + RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file); MultipartBody.Part part = MultipartBody.Part.createFormData("picture", file.getName(), requestBody); final Subscription subscription = mProfileService.uploadProfileImage(part) @@ -186,5 +160,4 @@ class ProfilePresenter extends MVPAbstractPresenter implements Prof getView().refreshImage(imageResponse); getView().showError(R.string.profile_updated); } - } diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/utils/FileUtils.java b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/utils/FileUtils.java index 273557a..d009a11 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/utils/FileUtils.java +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/java/de/beyondsoft/ownchat/utils/FileUtils.java @@ -21,6 +21,7 @@ import android.content.Intent; import android.database.Cursor; import android.database.DatabaseUtils; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Environment; @@ -30,10 +31,15 @@ import android.provider.MediaStore; import android.util.Log; import android.webkit.MimeTypeMap; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileFilter; +import java.io.FileOutputStream; +import java.io.IOException; import java.text.DecimalFormat; import java.util.Comparator; +import java.util.HashMap; +import java.util.Map; /** * @author Peli @@ -50,11 +56,616 @@ public class FileUtils { public static final String MIME_TYPE_AUDIO = "audio/*"; public static final String MIME_TYPE_TEXT = "text/*"; - public static final String MIME_TYPE_IMAGE = "image/*"; + private static final String MIME_TYPE_IMAGE = "image/*"; public static final String MIME_TYPE_VIDEO = "video/*"; public static final String MIME_TYPE_APP = "application/*"; - public static final String HIDDEN_PREFIX = "."; + private static final String HIDDEN_PREFIX = "."; + + private static final HashMap types = new HashMap<>(); + private static HashMap getTypes() { + if(types.size() == 0) { + Log.d("GETTING_MIME_TYPES", "types have a size of 0"); + types.put("323","text/h323"); + types.put("3g2","video/3gpp2"); + types.put("3gp","video/3gpp"); + types.put("3gp2","video/3gpp2"); + types.put("3gpp","video/3gpp"); + types.put("7z","application/x-7z-compressed"); + types.put("aa","audio/audible"); + types.put("AAC","audio/aac"); + types.put("aaf","application/octet-stream"); + types.put("aax","audio/vnd.audible.aax"); + types.put("ac3","audio/ac3"); + types.put("aca","application/octet-stream"); + types.put("accda","application/msaccess.addin"); + types.put("accdb","application/msaccess"); + types.put("accdc","application/msaccess.cab"); + types.put("accde","application/msaccess"); + types.put("accdr","application/msaccess.runtime"); + types.put("accdt","application/msaccess"); + types.put("accdw","application/msaccess.webapplication"); + types.put("accft","application/msaccess.ftemplate"); + types.put("acx","application/internet-property-stream"); + types.put("AddIn","text/xml"); + types.put("ade","application/msaccess"); + types.put("adobebridge","application/x-bridge-url"); + types.put("adp","application/msaccess"); + types.put("ADT","audio/vnd.dlna.adts"); + types.put("ADTS","audio/aac"); + types.put("afm","application/octet-stream"); + types.put("ai","application/postscript"); + types.put("aif","audio/aiff"); + types.put("aifc","audio/aiff"); + types.put("aiff","audio/aiff"); + types.put("air","application/vnd.adobe.air-application-installer-package+zip"); + types.put("amc","application/mpeg"); + types.put("anx","application/annodex"); + types.put("apk","application/vnd.android.package-archive"); + types.put("application","application/x-ms-application"); + types.put("art","image/x-jg"); + types.put("asa","application/xml"); + types.put("asax","application/xml"); + types.put("ascx","application/xml"); + types.put("asd","application/octet-stream"); + types.put("asf","video/x-ms-asf"); + types.put("ashx","application/xml"); + types.put("asi","application/octet-stream"); + types.put("asm","text/plain"); + types.put("asmx","application/xml"); + types.put("aspx","application/xml"); + types.put("asr","video/x-ms-asf"); + types.put("asx","video/x-ms-asf"); + types.put("atom","application/atom+xml"); + types.put("au","audio/basic"); + types.put("avi","video/x-msvideo"); + types.put("axa","audio/annodex"); + types.put("axs","application/olescript"); + types.put("axv","video/annodex"); + types.put("bas","text/plain"); + types.put("bcpio","application/x-bcpio"); + types.put("bin","application/octet-stream"); + types.put("bmp","image/bmp"); + types.put("c","text/plain"); + types.put("cab","application/octet-stream"); + types.put("caf","audio/x-caf"); + types.put("calx","application/vnd.ms-office.calx"); + types.put("cat","application/vnd.ms-pki.seccat"); + types.put("cc","text/plain"); + types.put("cd","text/plain"); + types.put("cdda","audio/aiff"); + types.put("cdf","application/x-cdf"); + types.put("cer","application/x-x509-ca-cert"); + types.put("cfg","text/plain"); + types.put("chm","application/octet-stream"); + types.put("class","application/x-java-applet"); + types.put("clp","application/x-msclip"); + types.put("cmd","text/plain"); + types.put("cmx","image/x-cmx"); + types.put("cnf","text/plain"); + types.put("cod","image/cis-cod"); + types.put("config","application/xml"); + types.put("contact","text/x-ms-contact"); + types.put("coverage","application/xml"); + types.put("cpio","application/x-cpio"); + types.put("cpp","text/plain"); + types.put("crd","application/x-mscardfile"); + types.put("crl","application/pkix-crl"); + types.put("crt","application/x-x509-ca-cert"); + types.put("cs","text/plain"); + types.put("csdproj","text/plain"); + types.put("csh","application/x-csh"); + types.put("csproj","text/plain"); + types.put("css","text/css"); + types.put("csv","text/csv"); + types.put("cur","application/octet-stream"); + types.put("cxx","text/plain"); + types.put("dat","application/octet-stream"); + types.put("datasource","application/xml"); + types.put("dbproj","text/plain"); + types.put("dcr","application/x-director"); + types.put("def","text/plain"); + types.put("deploy","application/octet-stream"); + types.put("der","application/x-x509-ca-cert"); + types.put("dgml","application/xml"); + types.put("dib","image/bmp"); + types.put("dif","video/x-dv"); + types.put("dir","application/x-director"); + types.put("disco","text/xml"); + types.put("divx","video/divx"); + types.put("dll","application/x-msdownload"); + types.put("dll.config","text/xml"); + types.put("dlm","text/dlm"); + types.put("doc","application/msword"); + types.put("docm","application/vnd.ms-word.document.macroEnabled.12"); + types.put("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + types.put("dot","application/msword"); + types.put("dotm","application/vnd.ms-word.template.macroEnabled.12"); + types.put("dotx","application/vnd.openxmlformats-officedocument.wordprocessingml.template"); + types.put("dsp","application/octet-stream"); + types.put("dsw","text/plain"); + types.put("dtd","text/xml"); + types.put("dtsConfig","text/xml"); + types.put("dv","video/x-dv"); + types.put("dvi","application/x-dvi"); + types.put("dwf","drawing/x-dwf"); + types.put("dwp","application/octet-stream"); + types.put("dxr","application/x-director"); + types.put("eml","message/rfc822"); + types.put("emz","application/octet-stream"); + types.put("eot","application/vnd.ms-fontobject"); + types.put("eps","application/postscript"); + types.put("etl","application/etl"); + types.put("etx","text/x-setext"); + types.put("evy","application/envoy"); + types.put("exe","application/octet-stream"); + types.put("exe.config","text/xml"); + types.put("fdf","application/vnd.fdf"); + types.put("fif","application/fractals"); + types.put("filters","application/xml"); + types.put("fla","application/octet-stream"); + types.put("flac","audio/flac"); + types.put("flr","x-world/x-vrml"); + types.put("flv","video/x-flv"); + types.put("fsscript","application/fsharp-script"); + types.put("fsx","application/fsharp-script"); + types.put("generictest","application/xml"); + types.put("gif","image/gif"); + types.put("group","text/x-ms-group"); + types.put("gsm","audio/x-gsm"); + types.put("gtar","application/x-gtar"); + types.put("gz","application/x-gzip"); + types.put("h","text/plain"); + types.put("hdf","application/x-hdf"); + types.put("hdml","text/x-hdml"); + types.put("hhc","application/x-oleobject"); + types.put("hhk","application/octet-stream"); + types.put("hhp","application/octet-stream"); + types.put("hlp","application/winhlp"); + types.put("hpp","text/plain"); + types.put("hqx","application/mac-binhex40"); + types.put("hta","application/hta"); + types.put("htc","text/x-component"); + types.put("htm","text/html"); + types.put("html","text/html"); + types.put("htt","text/webviewhtml"); + types.put("hxa","application/xml"); + types.put("hxc","application/xml"); + types.put("hxd","application/octet-stream"); + types.put("hxe","application/xml"); + types.put("hxf","application/xml"); + types.put("hxh","application/octet-stream"); + types.put("hxi","application/octet-stream"); + types.put("hxk","application/xml"); + types.put("hxq","application/octet-stream"); + types.put("hxr","application/octet-stream"); + types.put("hxs","application/octet-stream"); + types.put("hxt","text/html"); + types.put("hxv","application/xml"); + types.put("hxw","application/octet-stream"); + types.put("hxx","text/plain"); + types.put("i","text/plain"); + types.put("ico","image/x-icon"); + types.put("ics","application/octet-stream"); + types.put("idl","text/plain"); + types.put("ief","image/ief"); + types.put("iii","application/x-iphone"); + types.put("inc","text/plain"); + types.put("inf","application/octet-stream"); + types.put("ini","text/plain"); + types.put("inl","text/plain"); + types.put("ins","application/x-internet-signup"); + types.put("ipa","application/x-itunes-ipa"); + types.put("ipg","application/x-itunes-ipg"); + types.put("ipproj","text/plain"); + types.put("ipsw","application/x-itunes-ipsw"); + types.put("iqy","text/x-ms-iqy"); + types.put("isp","application/x-internet-signup"); + types.put("ite","application/x-itunes-ite"); + types.put("itlp","application/x-itunes-itlp"); + types.put("itms","application/x-itunes-itms"); + types.put("itpc","application/x-itunes-itpc"); + types.put("IVF","video/x-ivf"); + types.put("jar","application/java-archive"); + types.put("java","application/octet-stream"); + types.put("jck","application/liquidmotion"); + types.put("jcz","application/liquidmotion"); + types.put("jfif","image/pjpeg"); + types.put("jnlp","application/x-java-jnlp-file"); + types.put("jpb","application/octet-stream"); + types.put("jpe","image/jpeg"); + types.put("jpeg","image/jpeg"); + types.put("jpg","image/jpeg"); + types.put("js","application/javascript"); + types.put("json","application/json"); + types.put("jsx","text/jscript"); + types.put("jsxbin","text/plain"); + types.put("latex","application/x-latex"); + types.put("library-ms","application/windows-library+xml"); + types.put("lit","application/x-ms-reader"); + types.put("loadtest","application/xml"); + types.put("lpk","application/octet-stream"); + types.put("lsf","video/x-la-asf"); + types.put("lst","text/plain"); + types.put("lsx","video/x-la-asf"); + types.put("lzh","application/octet-stream"); + types.put("m13","application/x-msmediaview"); + types.put("m14","application/x-msmediaview"); + types.put("m1v","video/mpeg"); + types.put("m2t","video/vnd.dlna.mpeg-tts"); + types.put("m2ts","video/vnd.dlna.mpeg-tts"); + types.put("m2v","video/mpeg"); + types.put("m3u","audio/x-mpegurl"); + types.put("m3u8","audio/x-mpegurl"); + types.put("m4a","audio/m4a"); + types.put("m4b","audio/m4b"); + types.put("m4p","audio/m4p"); + types.put("m4r","audio/x-m4r"); + types.put("m4v","video/x-m4v"); + types.put("mac","image/x-macpaint"); + types.put("mak","text/plain"); + types.put("man","application/x-troff-man"); + types.put("manifest","application/x-ms-manifest"); + types.put("map","text/plain"); + types.put("master","application/xml"); + types.put("mda","application/msaccess"); + types.put("mdb","application/x-msaccess"); + types.put("mde","application/msaccess"); + types.put("mdp","application/octet-stream"); + types.put("me","application/x-troff-me"); + types.put("mfp","application/x-shockwave-flash"); + types.put("mht","message/rfc822"); + types.put("mhtml","message/rfc822"); + types.put("mid","audio/mid"); + types.put("midi","audio/mid"); + types.put("mix","application/octet-stream"); + types.put("mk","text/plain"); + types.put("mmf","application/x-smaf"); + types.put("mno","text/xml"); + types.put("mny","application/x-msmoney"); + types.put("mod","video/mpeg"); + types.put("mov","video/quicktime"); + types.put("movie","video/x-sgi-movie"); + types.put("mp2","video/mpeg"); + types.put("mp2v","video/mpeg"); + types.put("mp3","audio/mpeg"); + types.put("mp4","video/mp4"); + types.put("mp4v","video/mp4"); + types.put("mpa","video/mpeg"); + types.put("mpe","video/mpeg"); + types.put("mpeg","video/mpeg"); + types.put("mpf","application/vnd.ms-mediapackage"); + types.put("mpg","video/mpeg"); + types.put("mpp","application/vnd.ms-project"); + types.put("mpv2","video/mpeg"); + types.put("mqv","video/quicktime"); + types.put("ms","application/x-troff-ms"); + types.put("msi","application/octet-stream"); + types.put("mso","application/octet-stream"); + types.put("mts","video/vnd.dlna.mpeg-tts"); + types.put("mtx","application/xml"); + types.put("mvb","application/x-msmediaview"); + types.put("mvc","application/x-miva-compiled"); + types.put("mxp","application/x-mmxp"); + types.put("nc","application/x-netcdf"); + types.put("nsc","video/x-ms-asf"); + types.put("nws","message/rfc822"); + types.put("ocx","application/octet-stream"); + types.put("oda","application/oda"); + types.put("odb","application/vnd.oasis.opendocument.database"); + types.put("odc","application/vnd.oasis.opendocument.chart"); + types.put("odf","application/vnd.oasis.opendocument.formula"); + types.put("odg","application/vnd.oasis.opendocument.graphics"); + types.put("odh","text/plain"); + types.put("odi","application/vnd.oasis.opendocument.image"); + types.put("odl","text/plain"); + types.put("odm","application/vnd.oasis.opendocument.text-master"); + types.put("odp","application/vnd.oasis.opendocument.presentation"); + types.put("ods","application/vnd.oasis.opendocument.spreadsheet"); + types.put("odt","application/vnd.oasis.opendocument.text"); + types.put("oga","audio/ogg"); + types.put("ogg","audio/ogg"); + types.put("ogv","video/ogg"); + types.put("ogx","application/ogg"); + types.put("one","application/onenote"); + types.put("onea","application/onenote"); + types.put("onepkg","application/onenote"); + types.put("onetmp","application/onenote"); + types.put("onetoc","application/onenote"); + types.put("onetoc2","application/onenote"); + types.put("opus","audio/ogg"); + types.put("orderedtest","application/xml"); + types.put("osdx","application/opensearchdescription+xml"); + types.put("otf","application/font-sfnt"); + types.put("otg","application/vnd.oasis.opendocument.graphics-template"); + types.put("oth","application/vnd.oasis.opendocument.text-web"); + types.put("otp","application/vnd.oasis.opendocument.presentation-template"); + types.put("ots","application/vnd.oasis.opendocument.spreadsheet-template"); + types.put("ott","application/vnd.oasis.opendocument.text-template"); + types.put("oxt","application/vnd.openofficeorg.extension"); + types.put("p10","application/pkcs10"); + types.put("p12","application/x-pkcs12"); + types.put("p7b","application/x-pkcs7-certificates"); + types.put("p7c","application/pkcs7-mime"); + types.put("p7m","application/pkcs7-mime"); + types.put("p7r","application/x-pkcs7-certreqresp"); + types.put("p7s","application/pkcs7-signature"); + types.put("pbm","image/x-portable-bitmap"); + types.put("pcast","application/x-podcast"); + types.put("pct","image/pict"); + types.put("pcx","application/octet-stream"); + types.put("pcz","application/octet-stream"); + types.put("pdf","application/pdf"); + types.put("pfb","application/octet-stream"); + types.put("pfm","application/octet-stream"); + types.put("pfx","application/x-pkcs12"); + types.put("pgm","image/x-portable-graymap"); + types.put("pic","image/pict"); + types.put("pict","image/pict"); + types.put("pkgdef","text/plain"); + types.put("pkgundef","text/plain"); + types.put("pko","application/vnd.ms-pki.pko"); + types.put("pls","audio/scpls"); + types.put("pma","application/x-perfmon"); + types.put("pmc","application/x-perfmon"); + types.put("pml","application/x-perfmon"); + types.put("pmr","application/x-perfmon"); + types.put("pmw","application/x-perfmon"); + types.put("png","image/png"); + types.put("pnm","image/x-portable-anymap"); + types.put("pnt","image/x-macpaint"); + types.put("pntg","image/x-macpaint"); + types.put("pnz","image/png"); + types.put("pot","application/vnd.ms-powerpoint"); + types.put("potm","application/vnd.ms-powerpoint.template.macroEnabled.12"); + types.put("potx","application/vnd.openxmlformats-officedocument.presentationml.template"); + types.put("ppa","application/vnd.ms-powerpoint"); + types.put("ppam","application/vnd.ms-powerpoint.addin.macroEnabled.12"); + types.put("ppm","image/x-portable-pixmap"); + types.put("pps","application/vnd.ms-powerpoint"); + types.put("ppsm","application/vnd.ms-powerpoint.slideshow.macroEnabled.12"); + types.put("ppsx","application/vnd.openxmlformats-officedocument.presentationml.slideshow"); + types.put("ppt","application/vnd.ms-powerpoint"); + types.put("pptm","application/vnd.ms-powerpoint.presentation.macroEnabled.12"); + types.put("pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation"); + types.put("prf","application/pics-rules"); + types.put("prm","application/octet-stream"); + types.put("prx","application/octet-stream"); + types.put("ps","application/postscript"); + types.put("psc1","application/PowerShell"); + types.put("psd","application/octet-stream"); + types.put("psess","application/xml"); + types.put("psm","application/octet-stream"); + types.put("psp","application/octet-stream"); + types.put("pub","application/x-mspublisher"); + types.put("pwz","application/vnd.ms-powerpoint"); + types.put("qht","text/x-html-insertion"); + types.put("qhtm","text/x-html-insertion"); + types.put("qt","video/quicktime"); + types.put("qti","image/x-quicktime"); + types.put("qtif","image/x-quicktime"); + types.put("qtl","application/x-quicktimeplayer"); + types.put("qxd","application/octet-stream"); + types.put("ra","audio/x-pn-realaudio"); + types.put("ram","audio/x-pn-realaudio"); + types.put("rar","application/x-rar-compressed"); + types.put("ras","image/x-cmu-raster"); + types.put("rat","application/rat-file"); + types.put("rc","text/plain"); + types.put("rc2","text/plain"); + types.put("rct","text/plain"); + types.put("rdlc","application/xml"); + types.put("reg","text/plain"); + types.put("resx","application/xml"); + types.put("rf","image/vnd.rn-realflash"); + types.put("rgb","image/x-rgb"); + types.put("rgs","text/plain"); + types.put("rm","application/vnd.rn-realmedia"); + types.put("rmi","audio/mid"); + types.put("rmp","application/vnd.rn-rn_music_package"); + types.put("roff","application/x-troff"); + types.put("rpm","audio/x-pn-realaudio-plugin"); + types.put("rqy","text/x-ms-rqy"); + types.put("rtf","application/rtf"); + types.put("rtx","text/richtext"); + types.put("ruleset","application/xml"); + types.put("s","text/plain"); + types.put("safariextz","application/x-safari-safariextz"); + types.put("scd","application/x-msschedule"); + types.put("scr","text/plain"); + types.put("sct","text/scriptlet"); + types.put("sd2","audio/x-sd2"); + types.put("sdp","application/sdp"); + types.put("sea","application/octet-stream"); + types.put("searchConnector-ms","application/windows-search-connector+xml"); + types.put("setpay","application/set-payment-initiation"); + types.put("setreg","application/set-registration-initiation"); + types.put("settings","application/xml"); + types.put("sgimb","application/x-sgimb"); + types.put("sgml","text/sgml"); + types.put("sh","application/x-sh"); + types.put("shar","application/x-shar"); + types.put("shtml","text/html"); + types.put("sit","application/x-stuffit"); + types.put("sitemap","application/xml"); + types.put("skin","application/xml"); + types.put("sldm","application/vnd.ms-powerpoint.slide.macroEnabled.12"); + types.put("sldx","application/vnd.openxmlformats-officedocument.presentationml.slide"); + types.put("slk","application/vnd.ms-excel"); + types.put("sln","text/plain"); + types.put("slupkg-ms","application/x-ms-license"); + types.put("smd","audio/x-smd"); + types.put("smi","application/octet-stream"); + types.put("smx","audio/x-smd"); + types.put("smz","audio/x-smd"); + types.put("snd","audio/basic"); + types.put("snippet","application/xml"); + types.put("snp","application/octet-stream"); + types.put("sol","text/plain"); + types.put("sor","text/plain"); + types.put("spc","application/x-pkcs7-certificates"); + types.put("spl","application/futuresplash"); + types.put("spx","audio/ogg"); + types.put("src","application/x-wais-source"); + types.put("srf","text/plain"); + types.put("SSISDeploymentManifest","text/xml"); + types.put("ssm","application/streamingmedia"); + types.put("sst","application/vnd.ms-pki.certstore"); + types.put("stl","application/vnd.ms-pki.stl"); + types.put("sv4cpio","application/x-sv4cpio"); + types.put("sv4crc","application/x-sv4crc"); + types.put("svc","application/xml"); + types.put("svg","image/svg+xml"); + types.put("swf","application/x-shockwave-flash"); + types.put("t","application/x-troff"); + types.put("tar","application/x-tar"); + types.put("tcl","application/x-tcl"); + types.put("testrunconfig","application/xml"); + types.put("testsettings","application/xml"); + types.put("tex","application/x-tex"); + types.put("texi","application/x-texinfo"); + types.put("texinfo","application/x-texinfo"); + types.put("tgz","application/x-compressed"); + types.put("thmx","application/vnd.ms-officetheme"); + types.put("thn","application/octet-stream"); + types.put("tif","image/tiff"); + types.put("tiff","image/tiff"); + types.put("tlh","text/plain"); + types.put("tli","text/plain"); + types.put("toc","application/octet-stream"); + types.put("tr","application/x-troff"); + types.put("trm","application/x-msterminal"); + types.put("trx","application/xml"); + types.put("ts","video/vnd.dlna.mpeg-tts"); + types.put("tsv","text/tab-separated-values"); + types.put("ttf","application/font-sfnt"); + types.put("tts","video/vnd.dlna.mpeg-tts"); + types.put("txt","text/plain"); + types.put("u32","application/octet-stream"); + types.put("uls","text/iuls"); + types.put("user","text/plain"); + types.put("ustar","application/x-ustar"); + types.put("vb","text/plain"); + types.put("vbdproj","text/plain"); + types.put("vbk","video/mpeg"); + types.put("vbproj","text/plain"); + types.put("vbs","text/vbscript"); + types.put("vcf","text/x-vcard"); + types.put("vcproj","application/xml"); + types.put("vcs","text/plain"); + types.put("vcxproj","application/xml"); + types.put("vddproj","text/plain"); + types.put("vdp","text/plain"); + types.put("vdproj","text/plain"); + types.put("vdx","application/vnd.ms-visio.viewer"); + types.put("vml","text/xml"); + types.put("vscontent","application/xml"); + types.put("vsct","text/xml"); + types.put("vsd","application/vnd.visio"); + types.put("vsi","application/ms-vsi"); + types.put("vsix","application/vsix"); + types.put("vsixlangpack","text/xml"); + types.put("vsixmanifest","text/xml"); + types.put("vsmdi","application/xml"); + types.put("vspscc","text/plain"); + types.put("vss","application/vnd.visio"); + types.put("vsscc","text/plain"); + types.put("vssettings","text/xml"); + types.put("vssscc","text/plain"); + types.put("vst","application/vnd.visio"); + types.put("vstemplate","text/xml"); + types.put("vsto","application/x-ms-vsto"); + types.put("vsw","application/vnd.visio"); + types.put("vsx","application/vnd.visio"); + types.put("vtx","application/vnd.visio"); + types.put("wav","audio/wav"); + types.put("wave","audio/wav"); + types.put("wax","audio/x-ms-wax"); + types.put("wbk","application/msword"); + types.put("wbmp","image/vnd.wap.wbmp"); + types.put("wcm","application/vnd.ms-works"); + types.put("wdb","application/vnd.ms-works"); + types.put("wdp","image/vnd.ms-photo"); + types.put("webarchive","application/x-safari-webarchive"); + types.put("webm","video/webm"); + types.put("webp","image/webp"); + types.put("webtest","application/xml"); + types.put("wiq","application/xml"); + types.put("wiz","application/msword"); + types.put("wks","application/vnd.ms-works"); + types.put("WLMP","application/wlmoviemaker"); + types.put("wlpginstall","application/x-wlpg-detect"); + types.put("wlpginstall3","application/x-wlpg3-detect"); + types.put("wm","video/x-ms-wm"); + types.put("wma","audio/x-ms-wma"); + types.put("wmd","application/x-ms-wmd"); + types.put("wmf","application/x-msmetafile"); + types.put("wml","text/vnd.wap.wml"); + types.put("wmlc","application/vnd.wap.wmlc"); + types.put("wmls","text/vnd.wap.wmlscript"); + types.put("wmlsc","application/vnd.wap.wmlscriptc"); + types.put("wmp","video/x-ms-wmp"); + types.put("wmv","video/x-ms-wmv"); + types.put("wmx","video/x-ms-wmx"); + types.put("wmz","application/x-ms-wmz"); + types.put("woff","application/font-woff"); + types.put("wpl","application/vnd.ms-wpl"); + types.put("wps","application/vnd.ms-works"); + types.put("wri","application/x-mswrite"); + types.put("wrl","x-world/x-vrml"); + types.put("wrz","x-world/x-vrml"); + types.put("wsc","text/scriptlet"); + types.put("wsdl","text/xml"); + types.put("wvx","video/x-ms-wvx"); + types.put("x","application/directx"); + types.put("xaf","x-world/x-vrml"); + types.put("xaml","application/xaml+xml"); + types.put("xap","application/x-silverlight-app"); + types.put("xbap","application/x-ms-xbap"); + types.put("xbm","image/x-xbitmap"); + types.put("xdr","text/plain"); + types.put("xht","application/xhtml+xml"); + types.put("xhtml","application/xhtml+xml"); + types.put("xla","application/vnd.ms-excel"); + types.put("xlam","application/vnd.ms-excel.addin.macroEnabled.12"); + types.put("xlc","application/vnd.ms-excel"); + types.put("xld","application/vnd.ms-excel"); + types.put("xlk","application/vnd.ms-excel"); + types.put("xll","application/vnd.ms-excel"); + types.put("xlm","application/vnd.ms-excel"); + types.put("xls","application/vnd.ms-excel"); + types.put("xlsb","application/vnd.ms-excel.sheet.binary.macroEnabled.12"); + types.put("xlsm","application/vnd.ms-excel.sheet.macroEnabled.12"); + types.put("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); + types.put("xlt","application/vnd.ms-excel"); + types.put("xltm","application/vnd.ms-excel.template.macroEnabled.12"); + types.put("xltx","application/vnd.openxmlformats-officedocument.spreadsheetml.template"); + types.put("xlw","application/vnd.ms-excel"); + types.put("xml","text/xml"); + types.put("xmta","application/xml"); + types.put("xof","x-world/x-vrml"); + types.put("XOML","text/plain"); + types.put("xpm","image/x-xpixmap"); + types.put("xps","application/vnd.ms-xpsdocument"); + types.put("xrm-ms","text/xml"); + types.put("xsc","application/xml"); + types.put("xsd","text/xml"); + types.put("xsf","text/xml"); + types.put("xsl","text/xml"); + types.put("xslt","text/xml"); + types.put("xsn","application/octet-stream"); + types.put("xss","application/xml"); + types.put("xspf","application/xspf+xml"); + types.put("xtp","application/octet-stream"); + types.put("xwd","image/x-xwindowdump"); + types.put("z","application/x-compress"); + types.put("zip","application/zip"); + } else { + Log.d("GETTING_MIME_TYPES", "types have a size greater than 0"); + } + + return types; + } /** * Gets the extension of a file name, like ".png" or ".jpg". @@ -62,7 +673,7 @@ public class FileUtils { * @return Extension including the dot("."); "" if there is no extension; * null if uri was null. */ - public static String getExtension(String uri) { + private static String getExtension(String uri) { if (uri == null) { return null; } @@ -80,7 +691,7 @@ public class FileUtils { /** * @return Whether the URI is a local one. */ - public static boolean isLocal(String url) { + private static boolean isLocal(String url) { if (url != null && !url.startsWith("http://") && !url.startsWith("https://")) { return true; } @@ -91,7 +702,7 @@ public class FileUtils { * @return True if Uri is a MediaStore Uri. * @author paulburke */ - public static boolean isMediaUri(Uri uri) { + private static boolean isMediaUri(Uri uri) { return "media".equalsIgnoreCase(uri.getAuthority()); } @@ -100,7 +711,7 @@ public class FileUtils { * * @return uri */ - public static Uri getUri(File file) { + private static Uri getUri(File file) { if (file != null) { return Uri.fromFile(file); } @@ -138,8 +749,12 @@ public class FileUtils { public static String getMimeType(File file) { String extension = getExtension(file.getName().toLowerCase()); + if(extension.contains(".")) { + extension = extension.substring(1); + } + if (extension.length() > 0) { - return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.substring(1)); + return getTypes().get(extension); } return "application/octet-stream"; @@ -465,13 +1080,10 @@ public class FileUtils { * * @author paulburke */ - public static Comparator sComparator = new Comparator() { - @Override - public int compare(File f1, File f2) { - // Sort alphabetically by lower case, which is much cleaner - return f1.getName().toLowerCase().compareTo( - f2.getName().toLowerCase()); - } + public static Comparator sComparator = (f1, f2) -> { + // Sort alphabetically by lower case, which is much cleaner + return f1.getName().toLowerCase().compareTo( + f2.getName().toLowerCase()); }; /** @@ -479,13 +1091,10 @@ public class FileUtils { * * @author paulburke */ - public static FileFilter sFileFilter = new FileFilter() { - @Override - public boolean accept(File file) { - final String fileName = file.getName(); - // Return files only (not directories) and skip hidden files - return file.isFile() && !fileName.startsWith(HIDDEN_PREFIX); - } + public static FileFilter sFileFilter = file -> { + final String fileName = file.getName(); + // Return files only (not directories) and skip hidden files + return file.isFile() && !fileName.startsWith(HIDDEN_PREFIX); }; /** @@ -493,13 +1102,10 @@ public class FileUtils { * * @author paulburke */ - public static FileFilter sDirFilter = new FileFilter() { - @Override - public boolean accept(File file) { - final String fileName = file.getName(); - // Return directories only and skip hidden directories - return file.isDirectory() && !fileName.startsWith(HIDDEN_PREFIX); - } + public static FileFilter sDirFilter = file -> { + final String fileName = file.getName(); + // Return directories only and skip hidden directories + return file.isDirectory() && !fileName.startsWith(HIDDEN_PREFIX); }; /** @@ -565,9 +1171,7 @@ public class FileUtils { return extension; } - // TODO: a method that creates a tmp_file directory, and methods to clear it and store files in there - - public static String getTmpFilesDir(Context context) { + private static String getTmpFilesDir(Context context) { String dirName = context.getExternalFilesDir(null) + "/" + Constants.TMP_FILE_DIR; boolean mkdir = new File(dirName).mkdir(); @@ -575,17 +1179,60 @@ public class FileUtils { return dirName; } - public static boolean clearTmpFilesDir(Context context) { + public static void clearTmpFilesDir(Context context) { File dir = new File(getTmpFilesDir(context)); if(!dir.exists()) { - return true; + return; } for(File f : dir.listFiles()) { boolean delete = f.delete(); } + } - return true; + public static File scaleImageDown(File image, Context context) { + File tmpFile = null; + + try { + String tmpFileLocation = FileUtils.getTmpFilesDir(context) + "/" + image.getName(); + FileUtils.clearTmpFilesDir(context); + + BitmapFactory.Options options = new BitmapFactory.Options(); + + Bitmap bitmapToScale = BitmapFactory.decodeFile(image.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); + tmpFile = new File(tmpFileLocation); + //noinspection ResultOfMethodCallIgnored + 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(); + } + + return tmpFile; } } \ No newline at end of file diff --git a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/activity_main.xml b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/activity_main.xml index 3239fec..f945951 100644 --- a/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/activity_main.xml +++ b/BeWoPlanerChat/bewoplaner-chat-android/app/src/main/res/layout/activity_main.xml @@ -1,4 +1,5 @@ -Kontakte Profil Abmelden - Impressum + Datenschutzerklärung Open navigation drawer @@ -72,7 +72,7 @@ Bitte erlauben Sie den Zugriff auf Ihre Dateien. - Impressum + Datenschutzerklärung Fertig Keine Dateien gefunden