Browse Source

Migrate Profile bio and url views to RamboTextViewV2

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
cc9cab9c15
  1. 241
      app/src/main/java/awais/instagrabber/asyncs/DownloadAsync.java
  2. 95
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
  3. 8
      app/src/main/res/layout/fragment_profile.xml

241
app/src/main/java/awais/instagrabber/asyncs/DownloadAsync.java

@ -1,241 +0,0 @@
// package awais.instagrabber.asyncs;
//
// import android.app.PendingIntent;
// import android.content.ContentResolver;
// import android.content.Context;
// import android.content.Intent;
// import android.content.res.Resources;
// import android.graphics.Bitmap;
// import android.graphics.BitmapFactory;
// import android.media.MediaMetadataRetriever;
// import android.media.MediaScannerConnection;
// import android.net.Uri;
// import android.os.AsyncTask;
// import android.os.Build;
// import android.util.Log;
// import android.util.Pair;
//
// import androidx.annotation.NonNull;
// import androidx.annotation.Nullable;
// import androidx.annotation.StringRes;
// import androidx.core.app.NotificationCompat;
// import androidx.core.app.NotificationManagerCompat;
// import androidx.core.content.FileProvider;
//
// import java.io.BufferedInputStream;
// import java.io.File;
// import java.io.FileOutputStream;
// import java.io.InputStream;
// import java.net.URL;
// import java.net.URLConnection;
// import java.util.concurrent.atomic.AtomicReference;
//
// import awais.instagrabber.BuildConfig;
// import awais.instagrabber.R;
// import awais.instagrabber.interfaces.FetchListener;
// import awais.instagrabber.utils.Constants;
// import awais.instagrabber.utils.Utils;
//
// import static awais.instagrabber.utils.Constants.NOTIF_GROUP_NAME;
// import static awais.instagrabber.utils.Utils.logCollector;
// import static awaisomereport.LogCollector.LogFile;
//
// public final class DownloadAsync extends AsyncTask<Void, Float, File> {
// private static final String TAG = "DownloadAsync";
//
// private static int lastNotifId = 1;
// private final int currentNotifId;
// private final AtomicReference<Context> context;
// private final File outFile;
// private final String url;
// private final FetchListener<File> fetchListener;
// private final Resources resources;
// private final NotificationCompat.Builder downloadNotif;
// private String shortCode, username;
// private final NotificationManagerCompat notificationManager;
//
// public DownloadAsync(@NonNull final Context context,
// final String url,
// final File outFile,
// final FetchListener<File> fetchListener) {
// this.context = new AtomicReference<>(context);
// this.resources = context.getResources();
// this.url = url;
// this.outFile = outFile;
// this.fetchListener = fetchListener;
// this.shortCode = this.username = resources.getString(R.string.downloader_started);
// this.currentNotifId = ++lastNotifId;
// if (++lastNotifId + 1 == Integer.MAX_VALUE) lastNotifId = 1;
//
// @StringRes final int titleRes = R.string.downloader_downloading_post;
// downloadNotif = new NotificationCompat.Builder(context, Constants.DOWNLOAD_CHANNEL_ID)
// .setCategory(NotificationCompat.CATEGORY_STATUS)
// .setSmallIcon(R.mipmap.ic_launcher)
// .setContentText(shortCode == null ? username : shortCode)
// .setOngoing(true)
// .setProgress(100, 0, false)
// .setAutoCancel(false)
// .setOnlyAlertOnce(true)
// .setContentTitle(resources.getString(titleRes));
//
// notificationManager = NotificationManagerCompat.from(context.getApplicationContext());
// notificationManager.notify(currentNotifId, downloadNotif.build());
// }
//
// public DownloadAsync setItems(final String shortCode, final String username) {
// this.shortCode = shortCode;
// this.username = username;
// if (downloadNotif != null) downloadNotif.setContentText(this.shortCode == null ? this.username : this.shortCode);
// return this;
// }
//
// @Nullable
// @Override
// protected File doInBackground(final Void... voids) {
// try {
// final URLConnection urlConnection = new URL(url).openConnection();
// final long fileSize = Build.VERSION.SDK_INT >= 24 ? urlConnection.getContentLengthLong() :
// urlConnection.getContentLength();
// float totalRead = 0;
//
// try (final BufferedInputStream bis = new BufferedInputStream(urlConnection.getInputStream());
// final FileOutputStream fos = new FileOutputStream(outFile)) {
// final byte[] buffer = new byte[0x2000];
//
// int count;
// boolean deletedIPTC = false;
// while ((count = bis.read(buffer, 0, 0x2000)) != -1) {
// totalRead = totalRead + count;
//
// if (!deletedIPTC) {
// int iptcStart = -1;
// int fbmdStart = -1;
// int fbmdBytesLen = -1;
//
// for (int i = 0; i < buffer.length; ++i) {
// if (buffer[i] == (byte) 0xFF && buffer[i + 1] == (byte) 0xED)
// iptcStart = i;
// else if (buffer[i] == (byte) 'F' && buffer[i + 1] == (byte) 'B'
// && buffer[i + 2] == (byte) 'M' && buffer[i + 3] == (byte) 'D') {
// fbmdStart = i;
// fbmdBytesLen = buffer[i - 10] << 24 | (buffer[i - 9] & 0xFF) << 16 |
// (buffer[i - 8] & 0xFF) << 8 | (buffer[i - 7] & 0xFF) |
// (buffer[i - 6] & 0xFF);
// break;
// }
// }
//
// if (iptcStart != -1 && fbmdStart != -1 && fbmdBytesLen != -1) {
// final int fbmdDataLen = (iptcStart + (fbmdStart - iptcStart) + (fbmdBytesLen - iptcStart)) - 4;
//
// fos.write(buffer, 0, iptcStart);
// fos.write(buffer, fbmdDataLen + iptcStart, count - fbmdDataLen - iptcStart);
//
// publishProgress(totalRead * 100f / fileSize);
//
// deletedIPTC = true;
// continue;
// }
// }
//
// fos.write(buffer, 0, count);
// publishProgress(totalRead * 100f / fileSize);
// }
// fos.flush();
// }
//
// return outFile;
// } catch (final Exception e) {
// if (logCollector != null)
// logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "doInBackground",
// new Pair<>("context", context.get()),
// new Pair<>("resources", resources),
// new Pair<>("lastNotifId", lastNotifId),
// new Pair<>("downloadNotif", downloadNotif),
// new Pair<>("currentNotifId", currentNotifId),
// new Pair<>("notificationManager", notificationManager));
// if (BuildConfig.DEBUG) Log.e(TAG, "", e);
// }
// return null;
// }
//
// @Override
// protected void onPreExecute() {
// if (fetchListener != null) fetchListener.doBefore();
// }
//
// @Override
// protected void onProgressUpdate(@NonNull final Float... values) {
// if (downloadNotif != null) {
// downloadNotif.setProgress(100, values[0].intValue(), false);
// notificationManager.notify(currentNotifId, downloadNotif.build());
// }
// }
//
// @Override
// protected void onPostExecute(final File result) {
// if (result != null) {
// final Context context = this.context.get();
// context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(result.getAbsoluteFile())));
// MediaScannerConnection.scanFile(context, new String[]{result.getAbsolutePath()}, null, null);
//
// if (notificationManager != null) {
// final Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", result);
//
// final ContentResolver contentResolver = context.getContentResolver();
// Bitmap bitmap = null;
// if (Utils.isImage(uri, contentResolver)) {
// try (final InputStream inputStream = contentResolver.openInputStream(uri)) {
// bitmap = BitmapFactory.decodeStream(inputStream);
// } catch (final Exception e) {
// if (logCollector != null)
// logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "onPostExecute::bitmap_1");
// if (BuildConfig.DEBUG) Log.e(TAG, "", e);
// }
// }
//
// if (bitmap == null) {
// final MediaMetadataRetriever retriever = new MediaMetadataRetriever();
// try {
// try {
// retriever.setDataSource(context, uri);
// } catch (final Exception e) {
// retriever.setDataSource(result.getAbsolutePath());
// }
// bitmap = retriever.getFrameAtTime();
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
// try {
// retriever.close();
// } catch (final Exception e) {
// if (logCollector != null)
// logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "onPostExecute::bitmap_2");
// }
// } catch (final Exception e) {
// if (BuildConfig.DEBUG) Log.e("AWAISKING_APP", "", e);
// if (logCollector != null)
// logCollector.appendException(e, LogFile.ASYNC_DOWNLOADER, "onPostExecute::bitmap_3");
// }
// }
//
// final String downloadComplete = resources.getString(R.string.downloader_complete);
//
// downloadNotif.setContentText(null).setContentTitle(downloadComplete).setProgress(0, 0, false)
// .setWhen(System.currentTimeMillis()).setOngoing(false).setOnlyAlertOnce(false).setAutoCancel(true)
// .setGroup(NOTIF_GROUP_NAME).setGroupSummary(true).setContentIntent(
// PendingIntent.getActivity(context, 2020, new Intent(Intent.ACTION_VIEW, uri)
// .addFlags(
// Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
// .putExtra(Intent.EXTRA_STREAM, uri), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT));
//
// if (bitmap != null)
// downloadNotif.setStyle(new NotificationCompat.BigPictureStyle().setBigContentTitle(downloadComplete).bigPicture(bitmap))
// .setLargeIcon(bitmap).setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
//
// notificationManager.cancel(currentNotifId);
// notificationManager.notify(currentNotifId + 1, downloadNotif.build());
// }
// }
//
// if (fetchListener != null) fetchListener.onResult(result);
// }
// }

95
app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java

@ -9,7 +9,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
@ -20,7 +19,6 @@ import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.OnBackPressedCallback;
@ -90,6 +88,7 @@ import awais.instagrabber.webservices.ServiceCallback;
import awais.instagrabber.webservices.StoriesService;
import static androidx.core.content.PermissionChecker.checkSelfPermission;
import static awais.instagrabber.fragments.HashTagFragment.ARG_HASHTAG;
import static awais.instagrabber.utils.DownloadUtils.WRITE_PERMISSION;
import static awais.instagrabber.utils.Utils.settingsHelper;
@ -566,12 +565,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
} else {
binding.favCb.setVisibility(View.GONE);
}
// final ControllerListener<ImageInfo> listener = new BaseControllerListener<ImageInfo>() {
// @Override
// public void onFinalImageSet(final String id, final ImageInfo imageInfo, final Animatable animatable) {
// startPostponedEnterTransition();
// }
// };
binding.mainProfileImage.setImageURI(profileModel.getHdProfilePic());
final long followersCount = profileModel.getFollowersCount();
@ -604,33 +597,40 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
binding.mainFullName.setText(TextUtils.isEmpty(profileModel.getName()) ? profileModel.getUsername()
: profileModel.getName());
CharSequence biography = profileModel.getBiography();
if (TextUtils.hasMentions(biography)) {
biography = TextUtils.getMentionText(biography);
binding.mainBiography.setText(biography, TextView.BufferType.SPANNABLE);
binding.mainBiography.setMentionClickListener(mentionClickListener);
} else {
final String biography = profileModel.getBiography();
if (!TextUtils.isEmpty(biography)) {
binding.mainBiography.setText(biography);
binding.mainBiography.setMentionClickListener(null);
binding.mainBiography.addOnHashtagListener(autoLinkItem -> {
final NavController navController = NavHostFragment.findNavController(this);
final Bundle bundle = new Bundle();
final String originalText = autoLinkItem.getOriginalText().trim();
bundle.putString(ARG_HASHTAG, originalText);
navController.navigate(R.id.action_global_hashTagFragment, bundle);
});
binding.mainBiography.addOnMentionClickListener(autoLinkItem -> {
final String originalText = autoLinkItem.getOriginalText().trim();
navigateToProfile(originalText);
});
binding.mainBiography.addOnEmailClickListener(autoLinkItem -> Utils.openEmailAddress(getContext(),
autoLinkItem.getOriginalText().trim()));
binding.mainBiography.addOnURLClickListener(autoLinkItem -> Utils.openURL(getContext(), autoLinkItem.getOriginalText().trim()));
binding.mainBiography.setOnLongClickListener(v -> {
if (context != null) Utils.copyText(context, biography);
return true;
});
}
binding.mainBiography.setOnLongClickListener(v -> {
if (context != null) Utils.copyText(context, profileModel.getBiography());
return true;
});
final String url = profileModel.getUrl();
if (TextUtils.isEmpty(url)) {
binding.mainUrl.setVisibility(View.GONE);
} else {
binding.mainUrl.setVisibility(View.VISIBLE);
binding.mainUrl.setText(TextUtils.getSpannableUrl(url));
binding.mainUrl.setMovementMethod(LinkMovementMethod.getInstance());
binding.mainUrl.setText(url);
binding.mainUrl.addOnURLClickListener(autoLinkItem -> Utils.openURL(getContext(), autoLinkItem.getOriginalText().trim()));
binding.mainUrl.setOnLongClickListener(v -> {
if (context != null) Utils.copyText(context, url);
return true;
});
}
binding.mainFullName.setSelected(true);
binding.mainBiography.setEnabled(true);
binding.mainBiography.setClickable(true);
if (!profileModel.isReallyPrivate()) {
binding.mainFollowing.setClickable(true);
binding.mainFollowers.setClickable(true);
@ -904,47 +904,6 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
.init();
binding.swipeRefreshLayout.setRefreshing(true);
postsSetupDone = true;
// postsAdapter = new PostsAdapter((postModel, position) -> {
// if (postsAdapter.isSelecting()) {
// if (actionMode == null) return;
// final String title = getString(R.string.number_selected,
// postsAdapter.getSelectedModels().size());
// actionMode.setTitle(title);
// return;
// }
// if (checkAndResetAction()) return;
// final List<PostModel> postModels = postsViewModel.getList().getValue();
// if (postModels == null || postModels.size() == 0) return;
// if (postModels.get(0) == null) return;
// final String postId = isLoggedIn ? postModels.get(0).getPostId() : postModels.get(0).getShortCode();
// final boolean isId = isLoggedIn && postId != null;
// final String[] idsOrShortCodes = new String[postModels.size()];
// for (int i = 0; i < postModels.size(); i++) {
// idsOrShortCodes[i] = isId ? postModels.get(i).getPostId()
// : postModels.get(i).getShortCode();
// }
// final NavDirections action = ProfileFragmentDirections.actionGlobalPostViewFragment(
// position,
// idsOrShortCodes,
// isId);
// NavHostFragment.findNavController(this).navigate(action);
//
// }, (model, position) -> {
// if (!postsAdapter.isSelecting()) {
// checkAndResetAction();
// return true;
// }
// if (onBackPressedCallback.isEnabled()) {
// return true;
// }
// final OnBackPressedDispatcher onBackPressedDispatcher = fragmentActivity.getOnBackPressedDispatcher();
// onBackPressedCallback.setEnabled(true);
// actionMode = fragmentActivity.startActionMode(multiSelectAction);
// final String title = getString(R.string.number_selected, 1);
// actionMode.setTitle(title);
// onBackPressedDispatcher.addCallback(getViewLifecycleOwner(), onBackPressedCallback);
// return true;
// });
}
private void updateSwipeRefreshState() {

8
app/src/main/res/layout/fragment_profile.xml

@ -16,7 +16,7 @@
<com.google.android.material.appbar.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
app:layout_scrollFlags="scroll|snap">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/infoContainer"
@ -126,7 +126,7 @@
app:layout_constraintTop_toTopOf="@id/mainFullName"
tools:visibility="gone" />
<awais.instagrabber.customviews.RamboTextView
<awais.instagrabber.customviews.RamboTextViewV2
android:id="@+id/mainBiography"
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -139,7 +139,7 @@
app:layout_constraintTop_toBottomOf="@id/mainFullName"
tools:text="THE GLORIOUS (step)OWNER OF THIS APP. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec id justo lorem. In malesuada feugiat ornare. Suspendisse et mauris imperdiet, luctus augue eget, tempus eros. Cras vitae molestie ipsum. " />
<awais.instagrabber.customviews.RamboTextView
<awais.instagrabber.customviews.RamboTextViewV2
android:id="@+id/mainUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
@ -153,7 +153,7 @@
app:layout_constraintTop_toBottomOf="@id/mainBiography"
tools:text="https://austinhuang.me/"
tools:textColor="@android:color/holo_blue_dark"
tools:visibility="gone" />
tools:visibility="visible" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btnFollow"

Loading…
Cancel
Save