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
|
|
|
*/
|
|
|
|
|
2014-12-01 18:56:31 +01: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;
|
2014-02-11 15:32:09 +01:00
|
|
|
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;
|
2014-03-04 21:04:57 +01:00
|
|
|
import org.telegram.ui.LaunchActivity;
|
2015-02-26 02:32:51 +01:00
|
|
|
import org.telegram.ui.PhotoAlbumPickerActivity;
|
2014-02-11 15:32:09 +01:00
|
|
|
import org.telegram.ui.PhotoCropActivity;
|
2014-11-13 21:10:14 +01:00
|
|
|
import org.telegram.ui.ActionBar.BaseFragment;
|
2015-02-26 02:32:51 +01:00
|
|
|
import org.telegram.ui.PhotoViewer;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
2015-02-26 02:32:51 +01:00
|
|
|
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);
|
2015-02-26 02:32:51 +01:00
|
|
|
fragment.setDelegate(new PhotoAlbumPickerActivity.PhotoAlbumPickerActivityDelegate() {
|
|
|
|
@Override
|
2017-12-08 18:35:59 +01:00
|
|
|
public void didSelectPhotos(ArrayList<SendMessagesHelper.SendingMediaInfo> photos) {
|
2015-02-26 02:32:51 +01:00
|
|
|
if (!photos.isEmpty()) {
|
2017-12-08 18:35:59 +01:00
|
|
|
Bitmap bitmap = ImageLoader.loadBitmap(photos.get(0).path, null, 800, 800, true);
|
2015-02-26 02:32:51 +01:00
|
|
|
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);
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
parentFragment.presentFragment(fragment);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-03-04 20:29:32 +01: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();
|
2014-03-04 20:29:32 +01:00
|
|
|
if (activity == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-04 01:31:48 +02:00
|
|
|
Bundle args = new Bundle();
|
2014-03-04 20:29:32 +01:00
|
|
|
if (path != null) {
|
2014-06-04 01:31:48 +02:00
|
|
|
args.putString("photoPath", path);
|
2014-03-04 20:29:32 +01:00
|
|
|
} else if (uri != null) {
|
2014-06-04 01:31:48 +02:00
|
|
|
args.putParcelable("photoUri", uri);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-06-04 01:31:48 +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) {
|
2014-03-26 21:16:28 +01:00
|
|
|
if (requestCode == 13) {
|
2015-02-26 02:32:51 +01:00
|
|
|
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) {
|
2015-02-26 02:32:51 +01:00
|
|
|
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);
|
2015-02-26 02:32:51 +01:00
|
|
|
}
|
|
|
|
final ArrayList<Object> arrayList = new ArrayList<>();
|
2015-05-03 13:48:36 +02:00
|
|
|
arrayList.add(new MediaController.PhotoEntry(0, 0, 0, currentPicturePath, orientation, false));
|
2015-02-26 02:32:51 +01:00
|
|
|
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) {
|
2015-02-26 02:32:51 +01:00
|
|
|
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;
|
|
|
|
}
|
2015-05-03 13:48:36 +02:00
|
|
|
}, null);
|
2015-05-21 23:27:27 +02:00
|
|
|
AndroidUtilities.addMediaToGallery(currentPicturePath);
|
2013-10-25 17:19:00 +02:00
|
|
|
currentPicturePath = null;
|
2014-03-26 21:16:28 +01:00
|
|
|
} else if (requestCode == 14) {
|
2014-03-04 20:29:32 +01:00
|
|
|
if (data == null || data.getData() == null) {
|
2013-12-20 20:25:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-03-04 20:29:32 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 15:32:09 +01:00
|
|
|
@Override
|
2015-10-29 18:10:07 +01:00
|
|
|
public void didFinishEdit(Bitmap bitmap) {
|
2014-02-11 15:32:09 +01:00
|
|
|
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) {
|
2014-08-22 16:24:33 +02:00
|
|
|
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
|
|
|
}
|
2014-08-22 16:24:33 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|