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.
|
|
|
|
*/
|
|
|
|
|
2014-12-01 18:56:31 +01:00
|
|
|
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;
|
2015-02-26 02:32:51 +01:00
|
|
|
import android.media.ExifInterface;
|
2013-10-25 17:19:00 +02:00
|
|
|
import android.net.Uri;
|
2014-02-11 15:32:09 +01:00
|
|
|
import android.os.Bundle;
|
2013-10-25 17:19:00 +02:00
|
|
|
import android.provider.MediaStore;
|
|
|
|
|
2014-07-03 00:39:05 +02:00
|
|
|
import org.telegram.android.AndroidUtilities;
|
2014-08-22 16:24:33 +02:00
|
|
|
import org.telegram.android.ImageLoader;
|
2015-02-26 02:32:51 +01:00
|
|
|
import org.telegram.android.MediaController;
|
2014-02-28 23:28:25 +01:00
|
|
|
import org.telegram.messenger.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;
|
2014-08-22 16:24:33 +02:00
|
|
|
import org.telegram.android.NotificationCenter;
|
2013-10-25 17:19:00 +02:00
|
|
|
import org.telegram.messenger.UserConfig;
|
|
|
|
import org.telegram.messenger.Utilities;
|
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
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
public class AvatarUpdater implements NotificationCenter.NotificationCenterDelegate, PhotoCropActivity.PhotoEditActivityDelegate {
|
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;
|
2014-02-11 15:32:09 +01:00
|
|
|
public BaseFragment parentFragment = null;
|
2013-10-25 17:19:00 +02:00
|
|
|
public AvatarUpdaterDelegate delegate;
|
|
|
|
private boolean clearAfterUpdate = false;
|
|
|
|
public boolean returnOnly = false;
|
|
|
|
|
|
|
|
public static abstract interface AvatarUpdaterDelegate {
|
2013-12-20 20:25:49 +01:00
|
|
|
public abstract 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);
|
|
|
|
File image = Utilities.generatePicturePath();
|
|
|
|
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() {
|
2015-02-26 02:32:51 +01:00
|
|
|
PhotoAlbumPickerActivity fragment = new PhotoAlbumPickerActivity(true);
|
|
|
|
fragment.setDelegate(new PhotoAlbumPickerActivity.PhotoAlbumPickerActivityDelegate() {
|
|
|
|
@Override
|
|
|
|
public void didSelectPhotos(ArrayList<String> photos) {
|
|
|
|
if (!photos.isEmpty()) {
|
|
|
|
Bitmap bitmap = ImageLoader.loadBitmap(photos.get(0), null, 800, 800, true);
|
|
|
|
processBitmap(bitmap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void didSelectWebPhotos(ArrayList<MediaController.SearchImage> photos) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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 {
|
2014-06-04 01:31:48 +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) {
|
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) {
|
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);
|
|
|
|
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));
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
Utilities.addMediaToGallery(currentPicturePath);
|
|
|
|
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;
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
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);
|
2014-09-25 05:54:35 +02:00
|
|
|
uploadingAvatar = FileLoader.getInstance().getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg";
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().addObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload);
|
|
|
|
NotificationCenter.getInstance().addObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload);
|
2014-09-01 17:43:03 +02:00
|
|
|
FileLoader.getInstance().uploadFile(uploadingAvatar, false, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 15:32:09 +01:00
|
|
|
@Override
|
2015-02-01 19:51:02 +01:00
|
|
|
public void didFinishEdit(Bitmap bitmap, Bundle args) {
|
2014-02-11 15:32:09 +01:00
|
|
|
processBitmap(bitmap);
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, final Object... args) {
|
2014-08-22 16:24:33 +02:00
|
|
|
if (id == NotificationCenter.FileDidUpload) {
|
2013-10-25 17:19:00 +02:00
|
|
|
String location = (String)args[0];
|
|
|
|
if (uploadingAvatar != null && location.equals(uploadingAvatar)) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload);
|
|
|
|
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (delegate != null) {
|
2013-12-20 20:25:49 +01:00
|
|
|
delegate.didUploadedPhoto((TLRPC.InputFile)args[1], smallPhoto, bigPhoto);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
uploadingAvatar = null;
|
|
|
|
if (clearAfterUpdate) {
|
|
|
|
parentFragment = null;
|
|
|
|
delegate = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-08-22 16:24:33 +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)) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidUpload);
|
|
|
|
NotificationCenter.getInstance().removeObserver(AvatarUpdater.this, NotificationCenter.FileDidFailUpload);
|
2013-10-25 17:19:00 +02:00
|
|
|
uploadingAvatar = null;
|
|
|
|
if (clearAfterUpdate) {
|
|
|
|
parentFragment = null;
|
|
|
|
delegate = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|