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

232 lines
9.5 KiB
Java
Raw Normal View History

2013-10-25 17:19:00 +02:00
/*
2013-12-20 20:25:49 +01:00
* This is the source code of Telegram for Android v. 1.3.2.
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).
*
* Copyright Nikolai Kudashov, 2013.
*/
package org.telegram.ui.Components;
2013-10-25 17:19:00 +02:00
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ExifInterface;
2013-10-25 17:19:00 +02:00
import android.net.Uri;
import android.os.Bundle;
2013-10-25 17:19:00 +02:00
import android.provider.MediaStore;
2015-09-24 22:52:02 +02:00
import org.telegram.messenger.AndroidUtilities;
import org.telegram.messenger.ImageLoader;
import org.telegram.messenger.MediaController;
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
2015-02-01 19:51:02 +01:00
public class AvatarUpdater implements NotificationCenter.NotificationCenterDelegate, PhotoCropActivity.PhotoEditActivityDelegate {
2015-04-09 20:00:14 +02:00
2013-10-25 17:19:00 +02:00
public String currentPicturePath;
private TLRPC.PhotoSize smallPhoto;
private TLRPC.PhotoSize bigPhoto;
public String uploadingAvatar = null;
File picturePath = null;
public BaseFragment parentFragment = null;
2013-10-25 17:19:00 +02:00
public AvatarUpdaterDelegate delegate;
private boolean clearAfterUpdate = false;
public boolean returnOnly = false;
public interface AvatarUpdaterDelegate {
void didUploadedPhoto(TLRPC.InputFile file, TLRPC.PhotoSize small, TLRPC.PhotoSize big);
2013-10-25 17:19:00 +02:00
}
public void clear() {
if (uploadingAvatar != null) {
clearAfterUpdate = true;
} else {
parentFragment = null;
delegate = null;
}
}
public void openCamera() {
try {
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) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(image));
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) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", e);
2013-10-25 17:19:00 +02:00
}
}
public void openGallery() {
PhotoAlbumPickerActivity fragment = new PhotoAlbumPickerActivity(true, null);
fragment.setDelegate(new PhotoAlbumPickerActivity.PhotoAlbumPickerActivityDelegate() {
@Override
public void didSelectPhotos(ArrayList<String> photos, ArrayList<String> captions, ArrayList<MediaController.SearchImage> webPhotos) {
if (!photos.isEmpty()) {
Bitmap bitmap = ImageLoader.loadBitmap(photos.get(0), 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) {
FileLog.e("tmessages", e);
}
}
@Override
public boolean didSelectVideo(String path) {
return true;
}
});
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 {
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) {
2013-12-20 20:25:49 +01:00
FileLog.e("tmessages", 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);
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) {
FileLog.e("tmessages", 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
public void sendButtonPressed(int index) {
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);
}
}, 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;
}
smallPhoto = ImageLoader.scaleAndSaveImage(bitmap, 100, 100, 80, false);
2014-11-17 23:04:31 +01:00
bigPhoto = ImageLoader.scaleAndSaveImage(bitmap, 800, 800, 80, false, 320, 320);
2013-10-25 17:19:00 +02:00
if (bigPhoto != null && smallPhoto != null) {
if (returnOnly) {
if (delegate != null) {
delegate.didUploadedPhoto(null, smallPhoto, bigPhoto);
}
} else {
2013-11-04 13:31:01 +01:00
UserConfig.saveConfig(false);
uploadingAvatar = FileLoader.getInstance().getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg";
NotificationCenter.getInstance().addObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload);
NotificationCenter.getInstance().addObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload);
FileLoader.getInstance().uploadFile(uploadingAvatar, false, true);
2013-10-25 17:19:00 +02:00
}
}
}
@Override
2015-02-01 19:51:02 +01:00
public void didFinishEdit(Bitmap bitmap, Bundle args) {
processBitmap(bitmap);
}
2013-10-25 17:19:00 +02:00
@Override
public void didReceivedNotification(int id, final Object... args) {
if (id == NotificationCenter.FileDidUpload) {
2013-10-25 17:19:00 +02:00
String location = (String)args[0];
if (uploadingAvatar != null && location.equals(uploadingAvatar)) {
2015-05-21 23:27:27 +02:00
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload);
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload);
if (delegate != null) {
delegate.didUploadedPhoto((TLRPC.InputFile)args[1], smallPhoto, bigPhoto);
}
uploadingAvatar = null;
if (clearAfterUpdate) {
parentFragment = null;
delegate = null;
}
2013-10-25 17:19:00 +02:00
}
} else if (id == NotificationCenter.FileDidFailUpload) {
2013-10-25 17:19:00 +02:00
String location = (String)args[0];
if (uploadingAvatar != null && location.equals(uploadingAvatar)) {
2015-05-21 23:27:27 +02:00
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload);
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload);
uploadingAvatar = null;
if (clearAfterUpdate) {
parentFragment = null;
delegate = null;
}
2013-10-25 17:19:00 +02:00
}
}
}
}