BarInsta/app/src/main/java/awais/instagrabber/activities/ProfileViewer.java

229 lines
10 KiB
Java
Raw Normal View History

2020-07-01 17:08:28 +00:00
package awais.instagrabber.activities;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
2020-07-20 16:12:38 +00:00
import android.util.Log;
2020-07-01 17:08:28 +00:00
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import java.io.File;
import awais.instagrabber.R;
import awais.instagrabber.asyncs.DownloadAsync;
import awais.instagrabber.asyncs.ProfilePictureFetcher;
import awais.instagrabber.databinding.ActivityProfileBinding;
import awais.instagrabber.dialogs.ProfileSettingsDialog;
import awais.instagrabber.interfaces.FetchListener;
2020-07-25 21:10:26 +00:00
import awais.instagrabber.models.HashtagModel;
2020-07-01 17:08:28 +00:00
import awais.instagrabber.models.ProfileModel;
import awais.instagrabber.models.enums.ProfilePictureFetchMode;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.Utils;
import static awais.instagrabber.utils.Constants.PROFILE_FETCH_MODE;
public final class ProfileViewer extends BaseLanguageActivity {
private final ProfilePictureFetchMode[] fetchModes = {
ProfilePictureFetchMode.INSTADP,
2020-07-25 21:10:26 +00:00
ProfilePictureFetchMode.INSTAFULLSIZE
2020-07-01 17:08:28 +00:00
};
private ActivityProfileBinding profileBinding;
private ProfileModel profileModel;
2020-07-25 21:10:26 +00:00
private HashtagModel hashtagModel;
2020-07-01 17:08:28 +00:00
private MenuItem menuItemDownload;
private String profilePicUrl;
private FragmentManager fragmentManager;
private FetchListener<String> fetchListener;
private boolean errorHandled = false;
private boolean fallbackToProfile = false;
2020-07-20 16:12:38 +00:00
private boolean destroyed = false;
2020-07-01 17:08:28 +00:00
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
profileBinding = ActivityProfileBinding.inflate(getLayoutInflater());
setContentView(profileBinding.getRoot());
setSupportActionBar(profileBinding.toolbar.toolbar);
final Intent intent = getIntent();
2020-07-25 21:10:26 +00:00
if (intent == null || (!intent.hasExtra(Constants.EXTRAS_PROFILE) && !intent.hasExtra(Constants.EXTRAS_HASHTAG))
|| ((profileModel = (ProfileModel) intent.getSerializableExtra(Constants.EXTRAS_PROFILE)) == null
&& (hashtagModel = (HashtagModel) intent.getSerializableExtra(Constants.EXTRAS_HASHTAG)) == null)) {
2020-07-01 17:08:28 +00:00
Utils.errorFinish(this);
return;
}
fragmentManager = getSupportFragmentManager();
2020-07-25 21:10:26 +00:00
final String id = hashtagModel != null ? hashtagModel.getId() : profileModel.getId();
final String username = hashtagModel != null ? hashtagModel.getName() : profileModel.getUsername();
2020-07-01 17:08:28 +00:00
profileBinding.toolbar.toolbar.setTitle(username);
profileBinding.progressView.setVisibility(View.VISIBLE);
profileBinding.imageViewer.setVisibility(View.VISIBLE);
profileBinding.imageViewer.setZoomable(true);
profileBinding.imageViewer.setZoomTransitionDuration(420);
profileBinding.imageViewer.setMaximumScale(7.2f);
final int fetchIndex = Math.min(2, Math.max(0, Utils.settingsHelper.getInteger(PROFILE_FETCH_MODE)));
final ProfilePictureFetchMode fetchMode = fetchModes[fetchIndex];
fetchListener = profileUrl -> {
profilePicUrl = profileUrl;
if (!fallbackToProfile && Utils.isEmpty(profilePicUrl)) {
fallbackToProfile = true;
2020-07-25 21:10:26 +00:00
new ProfilePictureFetcher(username, id, fetchListener, fetchMode, profilePicUrl, hashtagModel != null).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2020-07-01 17:08:28 +00:00
return;
}
if (errorHandled && fallbackToProfile || Utils.isEmpty(profilePicUrl))
2020-07-25 21:10:26 +00:00
profilePicUrl = hashtagModel != null ? hashtagModel.getSdProfilePic() : profileModel.getHdProfilePic();
2020-07-01 17:08:28 +00:00
2020-07-20 16:12:38 +00:00
if (destroyed == true) return;
2020-07-01 17:08:28 +00:00
2020-07-20 16:12:38 +00:00
final RequestManager glideRequestManager = Glide.with(this);
2020-07-20 15:56:44 +00:00
2020-07-01 17:08:28 +00:00
glideRequestManager.load(profilePicUrl).addListener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable final GlideException e, final Object model, final Target<Drawable> target, final boolean isFirstResource) {
fallbackToProfile = true;
if (!errorHandled) {
errorHandled = true;
2020-07-25 21:10:26 +00:00
new ProfilePictureFetcher(username, id, fetchListener, fetchModes[Math.min(2, Math.max(0, fetchIndex + 1))], profilePicUrl, hashtagModel != null)
2020-07-01 17:08:28 +00:00
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
2020-07-25 21:10:26 +00:00
glideRequestManager.load(profilePicUrl).into(profileBinding.imageViewer);
2020-07-01 17:08:28 +00:00
showImageInfo();
}
profileBinding.progressView.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(final Drawable resource, final Object model, final Target<Drawable> target, final DataSource dataSource, final boolean isFirstResource) {
if (menuItemDownload != null) menuItemDownload.setEnabled(true);
showImageInfo();
profileBinding.progressView.setVisibility(View.GONE);
return false;
}
private void showImageInfo() {
final Drawable drawable = profileBinding.imageViewer.getDrawable();
if (drawable != null) {
final StringBuilder info = new StringBuilder(getString(R.string.profile_viewer_imageinfo, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));
if (drawable instanceof BitmapDrawable) {
final Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
if (bitmap != null) {
final String colorDepthPrefix = getString(R.string.profile_viewer_colordepth_prefix);
switch (bitmap.getConfig()) {
case ALPHA_8:
info.append(colorDepthPrefix).append(" 8-bits(A)");
break;
case RGB_565:
info.append(colorDepthPrefix).append(" 16-bits-A");
break;
case ARGB_4444:
info.append(colorDepthPrefix).append(" 16-bits+A");
break;
case ARGB_8888:
info.append(colorDepthPrefix).append(" 32-bits+A");
break;
case RGBA_F16:
info.append(colorDepthPrefix).append(" 64-bits+A");
break;
case HARDWARE:
info.append(colorDepthPrefix).append(" auto");
break;
}
}
}
profileBinding.imageInfo.setText(info);
profileBinding.imageInfo.setVisibility(View.VISIBLE);
}
}
}).into(profileBinding.imageViewer);
};
2020-07-25 21:10:26 +00:00
new ProfilePictureFetcher(username, id, fetchListener, fetchMode, profilePicUrl, hashtagModel != null)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
2020-07-01 17:08:28 +00:00
}
private void downloadProfilePicture() {
int error = 0;
if (profileModel != null) {
final File dir = new File(Environment.getExternalStorageDirectory(), "Download");
if (dir.exists() || dir.mkdirs()) {
final File saveFile = new File(dir, profileModel.getUsername() + '_' + System.currentTimeMillis()
+ Utils.getExtensionFromModel(profilePicUrl, profileModel));
new DownloadAsync(this,
profilePicUrl,
saveFile,
result -> {
final int toastRes = result != null && result.exists() ?
R.string.downloader_downloaded_in_folder : R.string.downloader_error_download_file;
Toast.makeText(this, toastRes, Toast.LENGTH_SHORT).show();
}).setItems(null, profileModel.getUsername()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else error = 1;
} else error = 2;
if (error == 1) Toast.makeText(this, R.string.downloader_error_creating_folder, Toast.LENGTH_SHORT).show();
else if (error == 2) Toast.makeText(this, R.string.downloader_unknown_error, Toast.LENGTH_SHORT).show();
}
2020-07-20 16:12:38 +00:00
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
destroyed = true;
}
2020-07-01 17:08:28 +00:00
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
final MenuItem.OnMenuItemClickListener menuItemClickListener = item -> {
if (item == menuItemDownload) {
downloadProfilePicture();
} else {
new ProfileSettingsDialog().show(fragmentManager, "settings");
}
return true;
};
menu.findItem(R.id.action_search).setVisible(false);
menuItemDownload = menu.findItem(R.id.action_download);
menuItemDownload.setVisible(true);
menuItemDownload.setEnabled(false);
menuItemDownload.setOnMenuItemClickListener(menuItemClickListener);
final MenuItem menuItemSettings = menu.findItem(R.id.action_settings);
menuItemSettings.setVisible(true);
menuItemSettings.setOnMenuItemClickListener(menuItemClickListener);
return true;
}
}