Nagram/TMessagesProj/src/main/java/org/telegram/ui/PhotoPickerActivity.java

1102 lines
49 KiB
Java
Raw Normal View History

2014-06-12 01:13:15 +00:00
/*
* This is the source code of Telegram for Android v. 1.4.x.
* 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.
*/
package org.telegram.ui;
import android.app.Activity;
2015-03-26 17:34:47 +00:00
import android.app.AlertDialog;
2014-06-12 01:13:15 +00:00
import android.content.Context;
2015-03-26 17:34:47 +00:00
import android.content.DialogInterface;
2015-01-02 22:15:07 +00:00
import android.graphics.Bitmap;
2014-06-12 01:13:15 +00:00
import android.os.Build;
2015-01-02 22:15:07 +00:00
import android.util.Base64;
import android.view.Gravity;
2014-06-12 01:13:15 +00:00
import android.view.LayoutInflater;
2014-10-07 20:14:27 +00:00
import android.view.MotionEvent;
2014-06-12 01:13:15 +00:00
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
2015-01-02 22:15:07 +00:00
import android.widget.AbsListView;
2014-06-12 01:13:15 +00:00
import android.widget.AdapterView;
2015-01-02 22:15:07 +00:00
import android.widget.EditText;
import android.widget.FrameLayout;
2014-06-12 01:13:15 +00:00
import android.widget.GridView;
2015-01-02 22:15:07 +00:00
import android.widget.ProgressBar;
2014-06-12 01:13:15 +00:00
import android.widget.TextView;
2015-01-02 22:15:07 +00:00
import org.json.JSONArray;
import org.json.JSONObject;
import org.telegram.android.AndroidUtilities;
import org.telegram.android.LocaleController;
import org.telegram.android.MediaController;
2015-01-02 22:15:07 +00:00
import org.telegram.android.MessagesStorage;
import org.telegram.android.NotificationCenter;
2015-01-02 22:15:07 +00:00
import org.telegram.android.volley.AuthFailureError;
import org.telegram.android.volley.Request;
import org.telegram.android.volley.RequestQueue;
import org.telegram.android.volley.Response;
import org.telegram.android.volley.VolleyError;
import org.telegram.android.volley.toolbox.JsonObjectRequest;
import org.telegram.android.volley.toolbox.Volley;
import org.telegram.messenger.ApplicationLoader;
2015-01-02 22:15:07 +00:00
import org.telegram.messenger.BuildVars;
import org.telegram.messenger.FileLog;
2014-06-12 01:13:15 +00:00
import org.telegram.messenger.R;
import org.telegram.messenger.TLRPC;
import org.telegram.android.MessageObject;
2015-01-02 22:15:07 +00:00
import org.telegram.messenger.Utilities;
import org.telegram.ui.ActionBar.ActionBarMenu;
import org.telegram.ui.ActionBar.ActionBarMenuItem;
import org.telegram.ui.Adapters.BaseFragmentAdapter;
2014-11-13 20:10:14 +00:00
import org.telegram.ui.ActionBar.ActionBar;
import org.telegram.ui.ActionBar.BaseFragment;
2015-01-02 22:15:07 +00:00
import org.telegram.ui.Cells.PhotoPickerPhotoCell;
import org.telegram.ui.Components.BackupImageView;
2015-01-02 22:15:07 +00:00
import org.telegram.ui.Components.PhotoPickerBottomLayout;
2014-06-12 01:13:15 +00:00
2015-01-02 22:15:07 +00:00
import java.net.URLEncoder;
2014-06-12 01:13:15 +00:00
import java.util.ArrayList;
import java.util.HashMap;
2015-01-02 22:15:07 +00:00
import java.util.Locale;
import java.util.Map;
2014-06-12 01:13:15 +00:00
public class PhotoPickerActivity extends BaseFragment implements NotificationCenter.NotificationCenterDelegate, PhotoViewer.PhotoViewerProvider {
2014-06-12 01:13:15 +00:00
public interface PhotoPickerActivityDelegate {
void selectedPhotosChanged();
void actionButtonPressed(boolean canceled);
2014-06-12 01:13:15 +00:00
}
2015-01-02 22:15:07 +00:00
private RequestQueue requestQueue;
private int type;
private HashMap<String, MediaController.SearchImage> selectedWebPhotos;
private HashMap<Integer, MediaController.PhotoEntry> selectedPhotos;
private ArrayList<MediaController.SearchImage> recentImages;
private ArrayList<MediaController.SearchImage> searchResult = new ArrayList<>();
private HashMap<String, MediaController.SearchImage> searchResultKeys = new HashMap<>();
private HashMap<String, MediaController.SearchImage> searchResultUrls = new HashMap<>();
private boolean searching;
private String nextSearchBingString;
2015-02-01 18:51:02 +00:00
private boolean giphySearchEndReached = true;
2015-01-02 22:15:07 +00:00
private String lastSearchString;
private boolean loadingRecent;
private MediaController.AlbumEntry selectedAlbum;
2014-06-12 01:13:15 +00:00
private GridView listView;
private ListAdapter listAdapter;
2015-01-02 22:15:07 +00:00
private PhotoPickerBottomLayout photoPickerBottomLayout;
private FrameLayout progressView;
2014-06-12 01:13:15 +00:00
private TextView emptyView;
2015-01-02 22:15:07 +00:00
private ActionBarMenuItem searchItem;
2014-06-12 01:13:15 +00:00
private int itemWidth = 100;
2015-01-02 22:15:07 +00:00
private boolean sendPressed;
private boolean singlePhoto;
2014-06-12 01:13:15 +00:00
private PhotoPickerActivityDelegate delegate;
public PhotoPickerActivity(int type, MediaController.AlbumEntry selectedAlbum, HashMap<Integer, MediaController.PhotoEntry> selectedPhotos, HashMap<String, MediaController.SearchImage> selectedWebPhotos, ArrayList<MediaController.SearchImage> recentImages, boolean onlyOnePhoto) {
2015-01-02 22:15:07 +00:00
super();
this.selectedAlbum = selectedAlbum;
this.selectedPhotos = selectedPhotos;
this.selectedWebPhotos = selectedWebPhotos;
this.type = type;
this.recentImages = recentImages;
this.singlePhoto = onlyOnePhoto;
2015-01-02 22:15:07 +00:00
}
2014-06-12 01:13:15 +00:00
@Override
public boolean onFragmentCreate() {
NotificationCenter.getInstance().addObserver(this, NotificationCenter.closeChats);
2015-01-02 22:15:07 +00:00
NotificationCenter.getInstance().addObserver(this, NotificationCenter.recentImagesDidLoaded);
if (selectedAlbum == null) {
requestQueue = Volley.newRequestQueue(ApplicationLoader.applicationContext);
if (recentImages.isEmpty()) {
MessagesStorage.getInstance().loadWebRecent(type);
loadingRecent = true;
}
}
2014-06-12 01:13:15 +00:00
return super.onFragmentCreate();
}
@Override
public void onFragmentDestroy() {
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.closeChats);
2015-01-02 22:15:07 +00:00
NotificationCenter.getInstance().removeObserver(this, NotificationCenter.recentImagesDidLoaded);
if (requestQueue != null) {
requestQueue.cancelAll("search");
requestQueue.stop();
}
2014-06-12 01:13:15 +00:00
super.onFragmentDestroy();
}
2015-01-02 22:15:07 +00:00
@SuppressWarnings("unchecked")
2014-06-12 01:13:15 +00:00
@Override
public View createView(LayoutInflater inflater) {
2014-06-12 01:13:15 +00:00
if (fragmentView == null) {
2014-11-11 22:16:17 +00:00
actionBar.setBackgroundColor(0xff333333);
actionBar.setItemsBackground(R.drawable.bar_selector_picker);
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null) {
actionBar.setTitle(selectedAlbum.bucketName);
} else if (type == 0) {
actionBar.setTitle(LocaleController.getString("SearchImagesTitle", R.string.SearchImagesTitle));
} else if (type == 1) {
actionBar.setTitle(LocaleController.getString("SearchGifsTitle", R.string.SearchGifsTitle));
}
2014-11-11 22:16:17 +00:00
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
2014-06-12 01:13:15 +00:00
@Override
public void onItemClick(int id) {
if (id == -1) {
2015-01-02 22:15:07 +00:00
if (Build.VERSION.SDK_INT < 11) {
listView.setAdapter(null);
listView = null;
listAdapter = null;
2014-06-12 01:13:15 +00:00
}
2015-01-02 22:15:07 +00:00
finishFragment();
2014-06-12 01:13:15 +00:00
}
}
});
2015-01-02 22:15:07 +00:00
if (selectedAlbum == null) {
ActionBarMenu menu = actionBar.createMenu();
searchItem = menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
@Override
public void onSearchExpand() {
}
@Override
public boolean onSearchCollapse() {
2015-01-02 22:15:07 +00:00
finishFragment();
return false;
2015-01-02 22:15:07 +00:00
}
2014-06-12 01:13:15 +00:00
2015-01-02 22:15:07 +00:00
@Override
public void onTextChanged(EditText editText) {
if (editText.getText().length() == 0) {
searchResult.clear();
searchResultKeys.clear();
lastSearchString = null;
nextSearchBingString = null;
2015-02-01 18:51:02 +00:00
giphySearchEndReached = true;
2015-01-02 22:15:07 +00:00
searching = false;
requestQueue.cancelAll("search");
if (type == 0) {
emptyView.setText(LocaleController.getString("NoRecentPhotos", R.string.NoRecentPhotos));
} else if (type == 1) {
emptyView.setText(LocaleController.getString("NoRecentGIFs", R.string.NoRecentGIFs));
}
updateSearchInterface();
}
}
2014-06-12 01:13:15 +00:00
2015-01-02 22:15:07 +00:00
@Override
public void onSearchPressed(EditText editText) {
if (editText.getText().toString().length() == 0) {
return;
}
searchResult.clear();
searchResultKeys.clear();
nextSearchBingString = null;
2015-02-01 18:51:02 +00:00
giphySearchEndReached = true;
2015-01-02 22:15:07 +00:00
if (type == 0) {
searchBingImages(editText.getText().toString(), 0, 53);
} else if (type == 1) {
2015-02-01 18:51:02 +00:00
searchGiphyImages(editText.getText().toString(), 0, 53);
2015-01-02 22:15:07 +00:00
}
lastSearchString = editText.getText().toString();
if (lastSearchString.length() == 0) {
lastSearchString = null;
if (type == 0) {
emptyView.setText(LocaleController.getString("NoRecentPhotos", R.string.NoRecentPhotos));
} else if (type == 1) {
emptyView.setText(LocaleController.getString("NoRecentGIFs", R.string.NoRecentGIFs));
}
} else {
emptyView.setText(LocaleController.getString("NoResult", R.string.NoResult));
}
updateSearchInterface();
}
});
}
if (selectedAlbum == null) {
if (type == 0) {
searchItem.getSearchField().setHint(LocaleController.getString("SearchImagesTitle", R.string.SearchImagesTitle));
} else if (type == 1) {
searchItem.getSearchField().setHint(LocaleController.getString("SearchGifsTitle", R.string.SearchGifsTitle));
}
}
fragmentView = new FrameLayout(getParentActivity());
FrameLayout frameLayout = (FrameLayout) fragmentView;
frameLayout.setBackgroundColor(0xff000000);
listView = new GridView(getParentActivity());
listView.setPadding(AndroidUtilities.dp(4), AndroidUtilities.dp(4), AndroidUtilities.dp(4), AndroidUtilities.dp(4));
listView.setClipToPadding(false);
listView.setDrawSelectorOnTop(true);
listView.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
listView.setHorizontalScrollBarEnabled(false);
listView.setVerticalScrollBarEnabled(false);
listView.setNumColumns(GridView.AUTO_FIT);
listView.setVerticalSpacing(AndroidUtilities.dp(4));
listView.setHorizontalSpacing(AndroidUtilities.dp(4));
listView.setSelector(R.drawable.list_selector);
frameLayout.addView(listView);
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) listView.getLayoutParams();
layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.bottomMargin = singlePhoto ? 0 : AndroidUtilities.dp(48);
2015-01-02 22:15:07 +00:00
listView.setLayoutParams(layoutParams);
listView.setAdapter(listAdapter = new ListAdapter(getParentActivity()));
AndroidUtilities.setListViewEdgeEffectColor(listView, 0xff333333);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
ArrayList<Object> arrayList = null;
if (selectedAlbum != null) {
arrayList = (ArrayList) selectedAlbum.photos;
} else {
if (searchResult.isEmpty() && lastSearchString == null) {
arrayList = (ArrayList) recentImages;
} else {
arrayList = (ArrayList) searchResult;
}
}
if (i < 0 || i >= arrayList.size()) {
return;
}
PhotoViewer.getInstance().setParentActivity(getParentActivity());
PhotoViewer.getInstance().openPhotoForSelect(arrayList, i, singlePhoto ? 1 : 0, PhotoPickerActivity.this);
2015-01-02 22:15:07 +00:00
}
});
2015-03-26 17:34:47 +00:00
if (selectedAlbum == null) {
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
if (searchResult.isEmpty() && lastSearchString == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
builder.setMessage(LocaleController.getString("ClearSearch", R.string.ClearSearch));
builder.setPositiveButton(LocaleController.getString("ClearButton", R.string.ClearButton).toUpperCase(), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
recentImages.clear();
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
MessagesStorage.getInstance().clearWebRecent(type);
}
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
showAlertDialog(builder);
return true;
}
return false;
}
});
}
2015-01-02 22:15:07 +00:00
emptyView = new TextView(getParentActivity());
emptyView.setTextColor(0xff808080);
emptyView.setTextSize(20);
emptyView.setGravity(Gravity.CENTER);
emptyView.setVisibility(View.GONE);
if (selectedAlbum != null) {
emptyView.setText(LocaleController.getString("NoPhotos", R.string.NoPhotos));
} else {
if (type == 0) {
emptyView.setText(LocaleController.getString("NoRecentPhotos", R.string.NoRecentPhotos));
} else if (type == 1) {
emptyView.setText(LocaleController.getString("NoRecentGIFs", R.string.NoRecentGIFs));
}
}
frameLayout.addView(emptyView);
layoutParams = (FrameLayout.LayoutParams) emptyView.getLayoutParams();
layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.bottomMargin = singlePhoto ? 0 : AndroidUtilities.dp(48);
2015-01-02 22:15:07 +00:00
emptyView.setLayoutParams(layoutParams);
2014-10-07 20:14:27 +00:00
emptyView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
2014-06-12 01:13:15 +00:00
2015-01-02 22:15:07 +00:00
if (selectedAlbum == null) {
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
if (i == SCROLL_STATE_TOUCH_SCROLL) {
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
}
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (visibleItemCount != 0 && firstVisibleItem + visibleItemCount > totalItemCount - 2 && !searching) {
if (type == 0 && nextSearchBingString != null) {
searchBingImages(lastSearchString, searchResult.size(), 54);
2015-02-01 18:51:02 +00:00
} else if (type == 1 && !giphySearchEndReached) {
searchGiphyImages(searchItem.getSearchField().getText().toString(), searchResult.size(), 54);
2015-01-02 22:15:07 +00:00
}
}
}
});
progressView = new FrameLayout(getParentActivity());
progressView.setVisibility(View.GONE);
frameLayout.addView(progressView);
layoutParams = (FrameLayout.LayoutParams) progressView.getLayoutParams();
layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.bottomMargin = singlePhoto ? 0 : AndroidUtilities.dp(48);
2015-01-02 22:15:07 +00:00
progressView.setLayoutParams(layoutParams);
ProgressBar progressBar = new ProgressBar(getParentActivity());
progressView.addView(progressBar);
layoutParams = (FrameLayout.LayoutParams) progressBar.getLayoutParams();
layoutParams.width = FrameLayout.LayoutParams.WRAP_CONTENT;
layoutParams.height = FrameLayout.LayoutParams.WRAP_CONTENT;
layoutParams.gravity = Gravity.CENTER;
progressBar.setLayoutParams(layoutParams);
updateSearchInterface();
}
photoPickerBottomLayout = new PhotoPickerBottomLayout(getParentActivity());
frameLayout.addView(photoPickerBottomLayout);
layoutParams = (FrameLayout.LayoutParams) photoPickerBottomLayout.getLayoutParams();
layoutParams.width = FrameLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = AndroidUtilities.dp(48);
layoutParams.gravity = Gravity.BOTTOM;
photoPickerBottomLayout.setLayoutParams(layoutParams);
photoPickerBottomLayout.cancelButton.setOnClickListener(new View.OnClickListener() {
2014-06-12 01:13:15 +00:00
@Override
public void onClick(View view) {
2015-01-02 22:15:07 +00:00
delegate.actionButtonPressed(true);
2014-06-12 01:13:15 +00:00
finishFragment();
}
});
2015-01-02 22:15:07 +00:00
photoPickerBottomLayout.doneButton.setOnClickListener(new View.OnClickListener() {
2014-06-12 01:13:15 +00:00
@Override
public void onClick(View view) {
sendSelectedPhotos();
}
});
if (singlePhoto) {
photoPickerBottomLayout.setVisibility(View.GONE);
}
2014-06-12 01:13:15 +00:00
2015-01-02 22:15:07 +00:00
listView.setEmptyView(emptyView);
photoPickerBottomLayout.updateSelectedCount(selectedPhotos.size() + selectedWebPhotos.size(), true);
2014-06-12 01:13:15 +00:00
} else {
ViewGroup parent = (ViewGroup)fragmentView.getParent();
if (parent != null) {
parent.removeView(fragmentView);
}
}
return fragmentView;
}
@Override
public void onResume() {
super.onResume();
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
2015-01-02 22:15:07 +00:00
if (searchItem != null) {
searchItem.openSearch();
getParentActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
2014-06-12 01:13:15 +00:00
fixLayout();
}
@Override
public void onConfigurationChanged(android.content.res.Configuration newConfig) {
super.onConfigurationChanged(newConfig);
fixLayout();
}
@SuppressWarnings("unchecked")
@Override
public void didReceivedNotification(int id, Object... args) {
2015-01-02 22:15:07 +00:00
if (id == NotificationCenter.closeChats) {
2014-06-12 01:13:15 +00:00
removeSelfFromStack();
2015-01-02 22:15:07 +00:00
} else if (id == NotificationCenter.recentImagesDidLoaded) {
if (selectedAlbum == null && type == (Integer) args[0]) {
recentImages = (ArrayList<MediaController.SearchImage>) args[1];
loadingRecent = false;
updateSearchInterface();
}
2014-06-12 01:13:15 +00:00
}
}
2015-01-02 22:15:07 +00:00
private PhotoPickerPhotoCell getCellForIndex(int index) {
2014-06-12 01:13:15 +00:00
int count = listView.getChildCount();
for (int a = 0; a < count; a++) {
View view = listView.getChildAt(a);
2015-01-02 22:15:07 +00:00
if (view instanceof PhotoPickerPhotoCell) {
PhotoPickerPhotoCell cell = (PhotoPickerPhotoCell) view;
int num = (Integer)cell.photoImage.getTag();
if (selectedAlbum != null) {
if (num < 0 || num >= selectedAlbum.photos.size()) {
continue;
}
} else {
ArrayList<MediaController.SearchImage> array = null;
if (searchResult.isEmpty() && lastSearchString == null) {
array = recentImages;
} else {
array = searchResult;
}
if (num < 0 || num >= array.size()) {
continue;
}
2014-06-12 01:13:15 +00:00
}
if (num == index) {
2015-01-02 22:15:07 +00:00
return cell;
2014-06-12 01:13:15 +00:00
}
}
}
return null;
}
2015-01-02 22:15:07 +00:00
@Override
public PhotoViewer.PlaceProviderObject getPlaceForPhoto(MessageObject messageObject, TLRPC.FileLocation fileLocation, int index) {
PhotoPickerPhotoCell cell = getCellForIndex(index);
if (cell != null) {
int coords[] = new int[2];
cell.photoImage.getLocationInWindow(coords);
PhotoViewer.PlaceProviderObject object = new PhotoViewer.PlaceProviderObject();
object.viewX = coords[0];
object.viewY = coords[1] - AndroidUtilities.statusBarHeight;
object.parentView = listView;
object.imageReceiver = cell.photoImage.imageReceiver;
object.thumb = object.imageReceiver.getBitmap();
cell.checkBox.setVisibility(View.GONE);
return object;
}
return null;
}
@Override
public void updatePhotoAtIndex(int index) {
PhotoPickerPhotoCell cell = getCellForIndex(index);
if (cell != null) {
if (selectedAlbum != null) {
cell.photoImage.setOrientation(0, true);
MediaController.PhotoEntry photoEntry = selectedAlbum.photos.get(index);
if (photoEntry.thumbPath != null) {
cell.photoImage.setImage(photoEntry.thumbPath, null, cell.getContext().getResources().getDrawable(R.drawable.nophotos));
} else if (photoEntry.path != null) {
cell.photoImage.setOrientation(photoEntry.orientation, true);
cell.photoImage.setImage("thumb://" + photoEntry.imageId + ":" + photoEntry.path, null, cell.getContext().getResources().getDrawable(R.drawable.nophotos));
} else {
cell.photoImage.setImageResource(R.drawable.nophotos);
}
} else {
ArrayList<MediaController.SearchImage> array = null;
if (searchResult.isEmpty() && lastSearchString == null) {
array = recentImages;
} else {
array = searchResult;
}
MediaController.SearchImage photoEntry = array.get(index);
if (photoEntry.thumbPath != null) {
cell.photoImage.setImage(photoEntry.thumbPath, null, cell.getContext().getResources().getDrawable(R.drawable.nophotos));
} else if (photoEntry.thumbUrl != null && photoEntry.thumbUrl.length() > 0) {
cell.photoImage.setImage(photoEntry.thumbUrl, null, cell.getContext().getResources().getDrawable(R.drawable.nophotos));
} else {
cell.photoImage.setImageResource(R.drawable.nophotos);
}
}
}
}
2015-01-02 22:15:07 +00:00
@Override
public Bitmap getThumbForPhoto(MessageObject messageObject, TLRPC.FileLocation fileLocation, int index) {
PhotoPickerPhotoCell cell = getCellForIndex(index);
if (cell != null) {
return cell.photoImage.imageReceiver.getBitmap();
}
return null;
}
2014-06-12 01:13:15 +00:00
@Override
public void willSwitchFromPhoto(MessageObject messageObject, TLRPC.FileLocation fileLocation, int index) {
int count = listView.getChildCount();
for (int a = 0; a < count; a++) {
View view = listView.getChildAt(a);
2014-10-23 07:55:32 +00:00
if (view.getTag() == null) {
continue;
}
2015-01-02 22:15:07 +00:00
PhotoPickerPhotoCell cell = (PhotoPickerPhotoCell) view;
2014-06-12 01:13:15 +00:00
int num = (Integer)view.getTag();
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null) {
if (num < 0 || num >= selectedAlbum.photos.size()) {
continue;
}
} else {
ArrayList<MediaController.SearchImage> array = null;
if (searchResult.isEmpty() && lastSearchString == null) {
array = recentImages;
} else {
array = searchResult;
}
if (num < 0 || num >= array.size()) {
continue;
}
2014-06-12 01:13:15 +00:00
}
if (num == index) {
2015-01-02 22:15:07 +00:00
cell.checkBox.setVisibility(View.VISIBLE);
2014-06-12 01:13:15 +00:00
break;
}
}
}
@Override
public void willHidePhotoViewer() {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
}
@Override
public boolean isPhotoChecked(int index) {
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null) {
return !(index < 0 || index >= selectedAlbum.photos.size()) && selectedPhotos.containsKey(selectedAlbum.photos.get(index).imageId);
} else {
ArrayList<MediaController.SearchImage> array = null;
if (searchResult.isEmpty() && lastSearchString == null) {
array = recentImages;
} else {
array = searchResult;
}
return !(index < 0 || index >= array.size()) && selectedWebPhotos.containsKey(array.get(index).id);
2014-06-12 01:13:15 +00:00
}
}
@Override
public void setPhotoChecked(int index) {
2015-01-02 22:15:07 +00:00
boolean add = true;
if (selectedAlbum != null) {
if (index < 0 || index >= selectedAlbum.photos.size()) {
return;
}
MediaController.PhotoEntry photoEntry = selectedAlbum.photos.get(index);
if (selectedPhotos.containsKey(photoEntry.imageId)) {
selectedPhotos.remove(photoEntry.imageId);
add = false;
} else {
selectedPhotos.put(photoEntry.imageId, photoEntry);
}
2014-06-12 01:13:15 +00:00
} else {
2015-01-02 22:15:07 +00:00
MediaController.SearchImage photoEntry = null;
ArrayList<MediaController.SearchImage> array = null;
if (searchResult.isEmpty() && lastSearchString == null) {
array = recentImages;
} else {
array = searchResult;
}
if (index < 0 || index >= array.size()) {
return;
}
photoEntry = array.get(index);
if (selectedWebPhotos.containsKey(photoEntry.id)) {
selectedWebPhotos.remove(photoEntry.id);
add = false;
} else {
selectedWebPhotos.put(photoEntry.id, photoEntry);
}
2014-06-12 01:13:15 +00:00
}
int count = listView.getChildCount();
for (int a = 0; a < count; a++) {
View view = listView.getChildAt(a);
2015-01-02 22:15:07 +00:00
int num = (Integer) view.getTag();
2014-06-12 01:13:15 +00:00
if (num == index) {
2015-01-02 22:15:07 +00:00
((PhotoPickerPhotoCell) view).checkBox.setChecked(add, false);
2014-06-12 01:13:15 +00:00
break;
}
}
2015-01-02 22:15:07 +00:00
photoPickerBottomLayout.updateSelectedCount(selectedPhotos.size() + selectedWebPhotos.size(), true);
delegate.selectedPhotosChanged();
2014-06-12 01:13:15 +00:00
}
@Override
public void cancelButtonPressed() {
2015-01-02 22:15:07 +00:00
delegate.actionButtonPressed(true);
2014-06-12 01:13:15 +00:00
finishFragment();
}
@Override
public void sendButtonPressed(int index) {
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null) {
if (selectedPhotos.isEmpty()) {
if (index < 0 || index >= selectedAlbum.photos.size()) {
return;
}
MediaController.PhotoEntry photoEntry = selectedAlbum.photos.get(index);
selectedPhotos.put(photoEntry.imageId, photoEntry);
}
} else if (selectedPhotos.isEmpty()) {
ArrayList<MediaController.SearchImage> array = null;
if (searchResult.isEmpty() && lastSearchString == null) {
array = recentImages;
} else {
array = searchResult;
}
if (index < 0 || index >= array.size()) {
2014-06-12 01:13:15 +00:00
return;
}
2015-01-02 22:15:07 +00:00
MediaController.SearchImage photoEntry = array.get(index);
selectedWebPhotos.put(photoEntry.id, photoEntry);
2014-06-12 01:13:15 +00:00
}
sendSelectedPhotos();
}
@Override
public int getSelectedCount() {
2015-01-02 22:15:07 +00:00
return selectedPhotos.size() + selectedWebPhotos.size();
}
@Override
public void onOpenAnimationEnd() {
super.onOpenAnimationEnd();
if (searchItem != null) {
AndroidUtilities.showKeyboard(searchItem.getSearchField());
}
}
private void updateSearchInterface() {
if (listAdapter != null) {
listAdapter.notifyDataSetChanged();
}
if (searching && searchResult.isEmpty() || loadingRecent && lastSearchString == null) {
progressView.setVisibility(View.VISIBLE);
listView.setEmptyView(null);
emptyView.setVisibility(View.GONE);
} else {
progressView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
listView.setEmptyView(emptyView);
}
}
2015-02-01 18:51:02 +00:00
private void searchGiphyImages(String query, int offset, final int count) {
2015-01-02 22:15:07 +00:00
if (searching) {
searching = false;
requestQueue.cancelAll("search");
}
try {
searching = true;
String url = String.format(Locale.US, "https://api.giphy.com/v1/gifs/search?q=%s&offset=%d&limit=%d&api_key=141Wa2KDAfNfxu", URLEncoder.encode(query, "UTF-8"), offset, count);
2015-01-02 22:15:07 +00:00
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray result = response.getJSONArray("data");
try {
JSONObject pagination = response.getJSONObject("pagination");
int total_count = pagination.getInt("total_count");
2015-02-01 18:51:02 +00:00
giphySearchEndReached = searchResult.size() + result.length() >= total_count;
2015-01-02 22:15:07 +00:00
} catch (Exception e) {
FileLog.e("tmessages", e);
}
boolean added = false;
for (int a = 0; a < result.length(); a++) {
try {
JSONObject object = result.getJSONObject(a);
String id = object.getString("id");
if (searchResultKeys.containsKey(id)) {
continue;
}
added = true;
JSONObject images = object.getJSONObject("images");
JSONObject thumb = images.getJSONObject("downsized_still");
JSONObject original = images.getJSONObject("original");
MediaController.SearchImage bingImage = new MediaController.SearchImage();
bingImage.id = id;
bingImage.width = original.getInt("width");
bingImage.height = original.getInt("height");
bingImage.size = original.getInt("size");
bingImage.imageUrl = original.getString("url");
bingImage.thumbUrl = thumb.getString("url");
bingImage.type = 1;
searchResult.add(bingImage);
searchResultKeys.put(id, bingImage);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
if (!added) {
2015-02-01 18:51:02 +00:00
giphySearchEndReached = true;
2015-01-02 22:15:07 +00:00
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
searching = false;
updateSearchInterface();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
FileLog.e("tmessages", "Error: " + error.getMessage());
2015-02-01 18:51:02 +00:00
giphySearchEndReached = true;
2015-01-02 22:15:07 +00:00
searching = false;
updateSearchInterface();
}
});
jsonObjReq.setTag("search");
requestQueue.add(jsonObjReq);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
private void searchBingImages(String query, int offset, int count) {
if (searching) {
searching = false;
requestQueue.cancelAll("search");
}
try {
searching = true;
String url;
if (nextSearchBingString != null) {
url = nextSearchBingString;
} else {
url = String.format(Locale.US, "https://api.datamarket.azure.com/Bing/Search/v1/Image?Query='%s'&$skip=%d&$top=%d&$format=json&Adult='Off'", URLEncoder.encode(query, "UTF-8"), offset, count);
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
nextSearchBingString = null;
try {
JSONObject d = response.getJSONObject("d");
JSONArray result = d.getJSONArray("results");
try {
nextSearchBingString = d.getString("__next");
} catch (Exception e) {
nextSearchBingString = null;
FileLog.e("tmessages", e);
}
for (int a = 0; a < result.length(); a++) {
try {
JSONObject object = result.getJSONObject(a);
String id = Utilities.MD5(object.getString("MediaUrl"));
if (searchResultKeys.containsKey(id)) {
continue;
}
MediaController.SearchImage bingImage = new MediaController.SearchImage();
bingImage.id = id;
bingImage.width = object.getInt("Width");
bingImage.height = object.getInt("Height");
bingImage.size = object.getInt("FileSize");
bingImage.imageUrl = object.getString("MediaUrl");
JSONObject thumbnail = object.getJSONObject("Thumbnail");
bingImage.thumbUrl = thumbnail.getString("MediaUrl");
searchResult.add(bingImage);
searchResultKeys.put(id, bingImage);
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
} catch (Exception e) {
FileLog.e("tmessages", e);
}
searching = false;
if (nextSearchBingString != null && !nextSearchBingString.contains("json")) {
nextSearchBingString += "&$format=json";
}
updateSearchInterface();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
FileLog.e("tmessages", "Error: " + error.getMessage());
nextSearchBingString = null;
searching = false;
updateSearchInterface();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
String auth = "Basic " + Base64.encodeToString((BuildVars.BING_SEARCH_KEY + ":" + BuildVars.BING_SEARCH_KEY).getBytes(), Base64.NO_WRAP);
headers.put("Authorization", auth);
return headers;
}
};
jsonObjReq.setTag("search");
requestQueue.add(jsonObjReq);
} catch (Exception e) {
FileLog.e("tmessages", e);
nextSearchBingString = null;
searching = false;
updateSearchInterface();
}
2014-06-12 01:13:15 +00:00
}
public void setDelegate(PhotoPickerActivityDelegate delegate) {
this.delegate = delegate;
}
private void sendSelectedPhotos() {
2015-01-02 22:15:07 +00:00
if (selectedPhotos.isEmpty() && selectedWebPhotos.isEmpty() || delegate == null || sendPressed) {
2014-06-12 01:13:15 +00:00
return;
}
2014-06-12 19:55:13 +00:00
sendPressed = true;
2015-01-02 22:15:07 +00:00
delegate.actionButtonPressed(false);
2014-06-12 01:13:15 +00:00
finishFragment();
}
private void fixLayout() {
if (listView != null) {
ViewTreeObserver obs = listView.getViewTreeObserver();
obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
fixLayoutInternal();
if (listView != null) {
listView.getViewTreeObserver().removeOnPreDrawListener(this);
}
return false;
}
});
}
}
private void fixLayoutInternal() {
if (getParentActivity() == null) {
return;
}
2014-06-12 19:55:13 +00:00
int position = listView.getFirstVisiblePosition();
WindowManager manager = (WindowManager) ApplicationLoader.applicationContext.getSystemService(Activity.WINDOW_SERVICE);
2014-06-12 01:13:15 +00:00
int rotation = manager.getDefaultDisplay().getRotation();
int columnsCount = 2;
2015-01-02 22:15:07 +00:00
if (AndroidUtilities.isTablet()) {
columnsCount = 3;
2014-06-12 01:13:15 +00:00
} else {
if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
2015-01-02 22:15:07 +00:00
columnsCount = 5;
} else {
columnsCount = 3;
2014-06-12 01:13:15 +00:00
}
}
listView.setNumColumns(columnsCount);
if (AndroidUtilities.isTablet()) {
itemWidth = (AndroidUtilities.dp(490) - ((columnsCount + 1) * AndroidUtilities.dp(4))) / columnsCount;
} else {
itemWidth = (AndroidUtilities.displaySize.x - ((columnsCount + 1) * AndroidUtilities.dp(4))) / columnsCount;
}
2014-06-12 01:13:15 +00:00
listView.setColumnWidth(itemWidth);
listAdapter.notifyDataSetChanged();
2014-06-12 19:55:13 +00:00
listView.setSelection(position);
2014-06-12 01:13:15 +00:00
2015-01-02 22:15:07 +00:00
if (selectedAlbum == null) {
emptyView.setPadding(0, 0, 0, (int)((AndroidUtilities.displaySize.y - AndroidUtilities.getCurrentActionBarHeight()) * 0.4f));
2014-06-12 01:13:15 +00:00
}
}
private class ListAdapter extends BaseFragmentAdapter {
2014-06-12 01:13:15 +00:00
private Context mContext;
public ListAdapter(Context context) {
mContext = context;
}
@Override
public boolean areAllItemsEnabled() {
2015-01-02 22:15:07 +00:00
return selectedAlbum != null;
2014-06-12 01:13:15 +00:00
}
@Override
public boolean isEnabled(int i) {
2015-01-02 22:15:07 +00:00
if (selectedAlbum == null) {
if (searchResult.isEmpty() && lastSearchString == null) {
return i < recentImages.size();
} else {
return i < searchResult.size();
}
}
2014-06-12 01:13:15 +00:00
return true;
}
@Override
public int getCount() {
2015-01-02 22:15:07 +00:00
if (selectedAlbum == null) {
if (searchResult.isEmpty() && lastSearchString == null) {
return recentImages.size();
} else if (type == 0) {
return searchResult.size() + (nextSearchBingString == null ? 0 : 1);
} else if (type == 1) {
2015-02-01 18:51:02 +00:00
return searchResult.size() + (giphySearchEndReached ? 0 : 1);
2015-01-02 22:15:07 +00:00
}
2014-06-12 01:13:15 +00:00
}
2015-01-02 22:15:07 +00:00
return selectedAlbum.photos.size();
2014-06-12 01:13:15 +00:00
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
2015-01-02 22:15:07 +00:00
int viewType = getItemViewType(i);
if (viewType == 0) {
PhotoPickerPhotoCell cell = (PhotoPickerPhotoCell) view;
2014-06-12 01:13:15 +00:00
if (view == null) {
2015-01-02 22:15:07 +00:00
view = new PhotoPickerPhotoCell(mContext);
cell = (PhotoPickerPhotoCell) view;
cell.checkFrame.setOnClickListener(new View.OnClickListener() {
2014-06-12 01:13:15 +00:00
@Override
public void onClick(View v) {
int index = (Integer) ((View) v.getParent()).getTag();
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null) {
MediaController.PhotoEntry photoEntry = selectedAlbum.photos.get(index);
2015-01-02 22:15:07 +00:00
if (selectedPhotos.containsKey(photoEntry.imageId)) {
selectedPhotos.remove(photoEntry.imageId);
2015-02-01 18:51:02 +00:00
photoEntry.imagePath = null;
photoEntry.thumbPath = null;
updatePhotoAtIndex(index);
2015-01-02 22:15:07 +00:00
} else {
selectedPhotos.put(photoEntry.imageId, photoEntry);
}
((PhotoPickerPhotoCell) v.getParent()).checkBox.setChecked(selectedPhotos.containsKey(photoEntry.imageId), true);
2014-06-12 01:13:15 +00:00
} else {
2015-01-02 22:15:07 +00:00
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
MediaController.SearchImage photoEntry = null;
if (searchResult.isEmpty() && lastSearchString == null) {
photoEntry = recentImages.get((Integer)((View)v.getParent()).getTag());
} else {
photoEntry = searchResult.get((Integer)((View)v.getParent()).getTag());
}
if (selectedWebPhotos.containsKey(photoEntry.id)) {
selectedWebPhotos.remove(photoEntry.id);
photoEntry.imagePath = null;
photoEntry.thumbPath = null;
updatePhotoAtIndex(index);
2015-01-02 22:15:07 +00:00
} else {
selectedWebPhotos.put(photoEntry.id, photoEntry);
}
((PhotoPickerPhotoCell) v.getParent()).checkBox.setChecked(selectedWebPhotos.containsKey(photoEntry.id), true);
2014-06-12 01:13:15 +00:00
}
2015-01-02 22:15:07 +00:00
photoPickerBottomLayout.updateSelectedCount(selectedPhotos.size() + selectedWebPhotos.size(), true);
delegate.selectedPhotosChanged();
2014-06-12 01:13:15 +00:00
}
});
cell.checkFrame.setVisibility(singlePhoto ? View.GONE : View.VISIBLE);
2014-06-12 01:13:15 +00:00
}
2015-01-02 22:15:07 +00:00
cell.itemWidth = itemWidth;
BackupImageView imageView = ((PhotoPickerPhotoCell) view).photoImage;
2014-06-12 01:13:15 +00:00
imageView.setTag(i);
view.setTag(i);
2015-01-02 22:15:07 +00:00
boolean showing = false;
imageView.setOrientation(0, true);
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null) {
MediaController.PhotoEntry photoEntry = selectedAlbum.photos.get(i);
if (photoEntry.thumbPath != null) {
imageView.setImage(photoEntry.thumbPath, null, mContext.getResources().getDrawable(R.drawable.nophotos));
} else if (photoEntry.path != null) {
imageView.setOrientation(photoEntry.orientation, true);
2015-01-02 22:15:07 +00:00
imageView.setImage("thumb://" + photoEntry.imageId + ":" + photoEntry.path, null, mContext.getResources().getDrawable(R.drawable.nophotos));
} else {
imageView.setImageResource(R.drawable.nophotos);
}
cell.checkBox.setChecked(selectedPhotos.containsKey(photoEntry.imageId), false);
showing = PhotoViewer.getInstance().isShowingImage(photoEntry.path);
2014-06-12 01:13:15 +00:00
} else {
2015-01-02 22:15:07 +00:00
MediaController.SearchImage photoEntry = null;
if (searchResult.isEmpty() && lastSearchString == null) {
photoEntry = recentImages.get(i);
} else {
photoEntry = searchResult.get(i);
}
if (photoEntry.thumbPath != null) {
imageView.setImage(photoEntry.thumbPath, null, mContext.getResources().getDrawable(R.drawable.nophotos));
} else if (photoEntry.thumbUrl != null && photoEntry.thumbUrl.length() > 0) {
2015-01-02 22:15:07 +00:00
imageView.setImage(photoEntry.thumbUrl, null, mContext.getResources().getDrawable(R.drawable.nophotos));
} else {
imageView.setImageResource(R.drawable.nophotos);
}
cell.checkBox.setChecked(selectedWebPhotos.containsKey(photoEntry.id), false);
showing = PhotoViewer.getInstance().isShowingImage(photoEntry.thumbUrl);
2014-06-12 01:13:15 +00:00
}
imageView.imageReceiver.setVisible(!showing, false);
cell.checkBox.setVisibility(singlePhoto || showing ? View.GONE : View.VISIBLE);
2015-01-02 22:15:07 +00:00
} else if (viewType == 1) {
if (view == null) {
LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = li.inflate(R.layout.media_loading_layout, viewGroup, false);
}
ViewGroup.LayoutParams params = view.getLayoutParams();
params.width = itemWidth;
params.height = itemWidth;
view.setLayoutParams(params);
2014-06-12 01:13:15 +00:00
}
return view;
}
@Override
public int getItemViewType(int i) {
2015-01-02 22:15:07 +00:00
if (selectedAlbum != null || searchResult.isEmpty() && lastSearchString == null && i < recentImages.size() || i < searchResult.size()) {
return 0;
2014-06-12 01:13:15 +00:00
}
2015-01-02 22:15:07 +00:00
return 1;
2014-06-12 01:13:15 +00:00
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public boolean isEmpty() {
if (selectedAlbum != null) {
return selectedAlbum.photos.isEmpty();
2015-01-02 22:15:07 +00:00
} else {
if (searchResult.isEmpty() && lastSearchString == null) {
return recentImages.isEmpty();
} else {
return searchResult.isEmpty();
}
2014-06-12 01:13:15 +00:00
}
}
}
}