NekoX/TMessagesProj/src/main/java/org/telegram/ui/Components/ImageUpdater.java

284 lines
12 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2015-10-29 18:10:07 +01:00
* This is the source code of Telegram for Android v. 3.x.x.
2013-10-25 17:19:00 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2017-03-31 01:58:05 +02:00
* Copyright Nikolai Kudashov, 2013-2017.
2013-10-25 17:19:00 +02:00
*/
package org.telegram.ui.Components;
2013-10-25 17:19:00 +02:00
2015-10-29 18:10:07 +01:00
import android.Manifest;
2013-10-25 17:19:00 +02:00
import android.app.Activity;
import android.content.Intent;
2015-10-29 18:10:07 +01:00
import android.content.pm.PackageManager;
2013-10-25 17:19:00 +02:00
import android.graphics.Bitmap;
2018-07-30 04:07:02 +02:00
import android.support.media.ExifInterface;
2013-10-25 17:19:00 +02:00
import android.net.Uri;
2015-10-29 18:10:07 +01:00
import android.os.Build;
import android.os.Bundle;
2013-10-25 17:19:00 +02:00
import android.provider.MediaStore;
2016-10-11 13:57:01 +02:00
import android.support.v4.content.FileProvider;
2013-10-25 17:19:00 +02:00
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
2016-10-11 13:57:01 +02:00
import org.telegram.messenger.BuildConfig;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.ImageLoader;
import org.telegram.messenger.MediaController;
2017-12-08 18:35:59 +01:00
import org.telegram.messenger.SendMessagesHelper;
2018-07-30 04:07:02 +02:00
import org.telegram.messenger.SharedConfig;
2017-03-31 01:58:05 +02:00
import org.telegram.messenger.VideoEditedInfo;
import org.telegram.tgnet.ConnectionsManager;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.TLRPC;
2013-10-25 17:19:00 +02:00
import org.telegram.messenger.FileLoader;
2013-12-20 20:25:49 +01:00
import org.telegram.messenger.FileLog;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.NotificationCenter;
2013-10-25 17:19:00 +02:00
import org.telegram.messenger.UserConfig;
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.PhotoAlbumPickerActivity;
import org.telegram.ui.PhotoCropActivity;
2014-11-13 21:10:14 +01:00
import org.telegram.ui.ActionBar.BaseFragment;
import org.telegram.ui.PhotoViewer;
2013-10-25 17:19:00 +02:00
import java.io.File;
import java.util.ArrayList;
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
public class ImageUpdater implements NotificationCenter.NotificationCenterDelegate, PhotoCropActivity.PhotoEditActivityDelegate {
2015-04-09 20:00:14 +02:00
2018-07-30 04:07:02 +02:00
public BaseFragment parentFragment;
public ImageUpdaterDelegate delegate;
private int currentAccount = UserConfig.selectedAccount;
2013-10-25 17:19:00 +02:00
public String currentPicturePath;
private TLRPC.PhotoSize smallPhoto;
private TLRPC.PhotoSize bigPhoto;
2018-07-30 04:07:02 +02:00
public String uploadingImage;
2013-10-25 17:19:00 +02:00
File picturePath = null;
2018-07-30 04:07:02 +02:00
private boolean clearAfterUpdate;
public boolean returnOnly;
public boolean encryptedDocument;
2013-10-25 17:19:00 +02:00
2018-07-30 04:07:02 +02:00
public interface ImageUpdaterDelegate {
void didUploadedPhoto(TLRPC.InputFile file, TLRPC.PhotoSize small, TLRPC.PhotoSize big, TLRPC.TL_secureFile secureFile);
2013-10-25 17:19:00 +02:00
}
public void clear() {
2018-07-30 04:07:02 +02:00
if (uploadingImage != null) {
2013-10-25 17:19:00 +02:00
clearAfterUpdate = true;
} else {
parentFragment = null;
delegate = null;
}
}
public void openCamera() {
2017-07-08 18:32:04 +02:00
if (parentFragment == null || parentFragment.getParentActivity() == null) {
return;
}
2013-10-25 17:19:00 +02:00
try {
2017-07-08 18:32:04 +02:00
if (Build.VERSION.SDK_INT >= 23 && parentFragment.getParentActivity().checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
parentFragment.getParentActivity().requestPermissions(new String[]{Manifest.permission.CAMERA}, 19);
return;
}
2013-10-25 17:19:00 +02:00
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
2015-05-21 23:27:27 +02:00
File image = AndroidUtilities.generatePicturePath();
2013-10-25 17:19:00 +02:00
if (image != null) {
2016-10-11 13:57:01 +02:00
if (Build.VERSION.SDK_INT >= 24) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(parentFragment.getParentActivity(), BuildConfig.APPLICATION_ID + ".provider", image));
takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
}
2013-10-25 17:19:00 +02:00
currentPicturePath = image.getAbsolutePath();
}
2014-10-01 00:36:18 +02:00
parentFragment.startActivityForResult(takePictureIntent, 13);
2013-10-25 17:19:00 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2013-10-25 17:19:00 +02:00
}
}
public void openGallery() {
2018-07-30 04:07:02 +02:00
if (parentFragment == null) {
return;
}
2015-10-29 18:10:07 +01:00
if (Build.VERSION.SDK_INT >= 23 && parentFragment != null && parentFragment.getParentActivity() != null) {
if (parentFragment.getParentActivity().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
parentFragment.getParentActivity().requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 4);
return;
}
}
2016-10-11 13:57:01 +02:00
PhotoAlbumPickerActivity fragment = new PhotoAlbumPickerActivity(true, false, false, null);
fragment.setDelegate(new PhotoAlbumPickerActivity.PhotoAlbumPickerActivityDelegate() {
@Override
2017-12-08 18:35:59 +01:00
public void didSelectPhotos(ArrayList<SendMessagesHelper.SendingMediaInfo> photos) {
if (!photos.isEmpty()) {
2017-12-08 18:35:59 +01:00
Bitmap bitmap = ImageLoader.loadBitmap(photos.get(0).path, null, 800, 800, true);
processBitmap(bitmap);
}
}
@Override
public void startPhotoSelectActivity() {
try {
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
parentFragment.startActivityForResult(photoPickerIntent, 14);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
}
});
parentFragment.presentFragment(fragment);
2013-10-25 17:19:00 +02:00
}
private void startCrop(String path, Uri uri) {
2013-10-25 17:19:00 +02:00
try {
2017-03-31 01:58:05 +02:00
LaunchActivity activity = (LaunchActivity) parentFragment.getParentActivity();
if (activity == null) {
return;
}
Bundle args = new Bundle();
if (path != null) {
args.putString("photoPath", path);
} else if (uri != null) {
args.putParcelable("photoUri", uri);
2013-10-25 17:19:00 +02:00
}
PhotoCropActivity photoCropActivity = new PhotoCropActivity(args);
photoCropActivity.setDelegate(this);
activity.presentFragment(photoCropActivity);
2013-10-25 17:19:00 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-02-01 19:51:02 +01:00
Bitmap bitmap = ImageLoader.loadBitmap(path, uri, 800, 800, true);
2013-10-25 17:19:00 +02:00
processBitmap(bitmap);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 13) {
PhotoViewer.getInstance().setParentActivity(parentFragment.getParentActivity());
int orientation = 0;
try {
ExifInterface ei = new ExifInterface(currentPicturePath);
int exif = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
2017-03-31 01:58:05 +02:00
switch (exif) {
case ExifInterface.ORIENTATION_ROTATE_90:
orientation = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
orientation = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
orientation = 270;
break;
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
final ArrayList<Object> arrayList = new ArrayList<>();
arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false));
PhotoViewer.getInstance().openPhotoForSelect(arrayList, 0, 1, new PhotoViewer.EmptyPhotoViewerProvider() {
@Override
2017-03-31 01:58:05 +02:00
public void sendButtonPressed(int index, VideoEditedInfo videoEditedInfo) {
String path = null;
MediaController.PhotoEntry photoEntry = (MediaController.PhotoEntry) arrayList.get(0);
if (photoEntry.imagePath != null) {
path = photoEntry.imagePath;
} else if (photoEntry.path != null) {
path = photoEntry.path;
}
Bitmap bitmap = ImageLoader.loadBitmap(path, null, 800, 800, true);
processBitmap(bitmap);
}
2016-10-11 13:57:01 +02:00
@Override
public boolean allowCaption() {
return false;
}
2017-12-08 18:35:59 +01:00
@Override
public boolean canScrollAway() {
return false;
}
}, null);
2015-05-21 23:27:27 +02:00
AndroidUtilities.addMediaToGallery(currentPicturePath);
2013-10-25 17:19:00 +02:00
currentPicturePath = null;
} else if (requestCode == 14) {
if (data == null || data.getData() == null) {
2013-12-20 20:25:49 +01:00
return;
}
startCrop(null, data.getData());
2013-10-25 17:19:00 +02:00
}
}
}
private void processBitmap(Bitmap bitmap) {
if (bitmap == null) {
return;
}
2018-07-30 04:07:02 +02:00
if (encryptedDocument) {
bigPhoto = ImageLoader.scaleAndSaveImage(bitmap, 2048, 2048, 89, false, 320, 320);
if (delegate != null) {
TLRPC.TL_secureFile secureFile = new TLRPC.TL_secureFile();
secureFile.dc_id = (int) bigPhoto.location.volume_id;
secureFile.id = bigPhoto.location.local_id;
secureFile.date = (int) (System.currentTimeMillis() / 1000);
delegate.didUploadedPhoto(null, null, null, secureFile);
}
SharedConfig.saveConfig();
} else {
smallPhoto = ImageLoader.scaleAndSaveImage(bitmap, 100, 100, 80, false);
bigPhoto = ImageLoader.scaleAndSaveImage(bitmap, 800, 800, 80, false, 320, 320);
bitmap.recycle();
if (bigPhoto != null && smallPhoto != null) {
if (returnOnly) {
if (delegate != null) {
delegate.didUploadedPhoto(null, smallPhoto, bigPhoto, null);
}
} else {
UserConfig.getInstance(currentAccount).saveConfig(false);
uploadingImage = FileLoader.getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg";
NotificationCenter.getInstance(currentAccount).addObserver(ImageUpdater.this, NotificationCenter.FileDidUpload);
NotificationCenter.getInstance(currentAccount).addObserver(ImageUpdater.this, NotificationCenter.FileDidFailUpload);
FileLoader.getInstance(currentAccount).uploadFile(uploadingImage, false, true, ConnectionsManager.FileTypePhoto);
2013-10-25 17:19:00 +02:00
}
}
}
}
@Override
2015-10-29 18:10:07 +01:00
public void didFinishEdit(Bitmap bitmap) {
processBitmap(bitmap);
}
2013-10-25 17:19:00 +02:00
@Override
2018-07-30 04:07:02 +02:00
public void didReceivedNotification(int id, int account, Object... args) {
if (id == NotificationCenter.FileDidUpload) {
2017-03-31 01:58:05 +02:00
String location = (String) args[0];
2018-07-30 04:07:02 +02:00
if (uploadingImage != null && location.equals(uploadingImage)) {
NotificationCenter.getInstance(currentAccount).removeObserver(ImageUpdater.this, NotificationCenter.FileDidUpload);
NotificationCenter.getInstance(currentAccount).removeObserver(ImageUpdater.this, NotificationCenter.FileDidFailUpload);
2015-05-21 23:27:27 +02:00
if (delegate != null) {
2018-07-30 04:07:02 +02:00
delegate.didUploadedPhoto((TLRPC.InputFile) args[1], smallPhoto, bigPhoto, null);
2015-05-21 23:27:27 +02:00
}
2018-07-30 04:07:02 +02:00
uploadingImage = null;
2015-05-21 23:27:27 +02:00
if (clearAfterUpdate) {
parentFragment = null;
delegate = null;
}
2013-10-25 17:19:00 +02:00
}
} else if (id == NotificationCenter.FileDidFailUpload) {
2017-03-31 01:58:05 +02:00
String location = (String) args[0];
2018-07-30 04:07:02 +02:00
if (uploadingImage != null && location.equals(uploadingImage)) {
NotificationCenter.getInstance(currentAccount).removeObserver(ImageUpdater.this, NotificationCenter.FileDidUpload);
NotificationCenter.getInstance(currentAccount).removeObserver(ImageUpdater.this, NotificationCenter.FileDidFailUpload);
uploadingImage = null;
2015-05-21 23:27:27 +02:00
if (clearAfterUpdate) {
parentFragment = null;
delegate = null;
}
2013-10-25 17:19:00 +02:00
}
}
}
}