Skip to content

Commit

Permalink
[TL;DR]
Browse files Browse the repository at this point in the history
Changes: Long clicking release item will now copy it's url to clipboard.
Fixed: k0shk0sh#2925, k0shk0sh#2901, k0shk0sh#2915, k0shk0sh#2868, k0shk0sh#2875.
Fixed: Download file name appending "token?=".

# Conflicts:
#	app/build.gradle
#	build.gradle
  • Loading branch information
LightDestory committed Aug 19, 2021
1 parent 0faebbc commit e2243ff
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 25 deletions.
17 changes: 15 additions & 2 deletions app/src/main/java/com/fastaccess/provider/rest/RestProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.fastaccess.BuildConfig;
import com.fastaccess.R;
Expand Down Expand Up @@ -96,11 +99,18 @@ public static void downloadFile(@NonNull Context context, @NonNull String url) {
downloadFile(context, url, null);
}

/** AuthToken seems to be issue for not downloading any binary files. Source files seems to be downloaded fine.
*
* Suppose if we remove it then we fix the above but then downloading private releases seems to not work then.
* In such case we pass :open: as extension.
*
* @param extension If it is :open: then open the custom tab activity
*/
public static void downloadFile(@NonNull Context context, @NonNull String url, @Nullable String extension) {
try {
if (InputHelper.isEmpty(url)) return;
boolean isEnterprise = LinkParserHelper.isEnterprise(url);
if (url.endsWith(".apk")) {
if (extension != null && extension.equals(":open:")) {
Activity activity = ActivityHelper.getActivity(context);
if (activity != null) {
ActivityHelper.startCustomTab(activity, url);
Expand All @@ -111,10 +121,13 @@ public static void downloadFile(@NonNull Context context, @NonNull String url, @
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
String authToken = isEnterprise ? PrefGetter.getEnterpriseToken() : PrefGetter.getToken();
if (!TextUtils.isEmpty(authToken)) {
// Do not set auth token for "releases" url.
if (!TextUtils.isEmpty(authToken) && !url.contains("releases/")) {
request.addRequestHeader("Authorization", authToken.startsWith("Basic") ? authToken : "token " + authToken);
}
String fileName = new File(url).getName();
// Remove the fileName which appends '?token='
if (fileName.contains("?token=")) fileName = fileName.substring(0, fileName.indexOf("?token="));
if (!InputHelper.isEmpty(extension)) {
fileName += extension;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.fastaccess.helper.PrefGetter

/**
* Created by Kosh on 11 Apr 2017, 10:02 PM
* Migrated by Kaustubh on 27th Dec 2020, 1:34 PM
*/
object LinkParserHelper {
const val HOST_DEFAULT = "github.com"
Expand All @@ -24,7 +25,7 @@ object LinkParserHelper {
"explore", "dashboard", "repositories", "logout", "sessions", "site", "security",
"contact", "about", "logos", "login", "pricing", "")

val escapeSymbols: Map<String, String> = mapOf(
private val escapeSymbols: Map<String, String> = mapOf(
"&quot;" to "\"", "&apos;" to "'",
"&lt;" to "<", "&gt;" to ">", "&amp;" to "&")

Expand Down Expand Up @@ -88,14 +89,14 @@ object LinkParserHelper {

@JvmStatic
fun getEndpoint(url: String): String {
var url = url
if (url.startsWith("http://")) {
url = url.replace("http://", "https://")
var urlString = url
if (urlString.startsWith("http://")) {
urlString = urlString.replace("http://", "https://")
}
if (!url.startsWith("https://")) {
url = "https://$url"
if (!urlString.startsWith("https://")) {
urlString = "https://$urlString"
}
return getEnterpriseUrl(url)
return getEnterpriseUrl(urlString)
}

private fun getEnterpriseUrl(url: String): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.fastaccess.ui.modules.repos.code.releases;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -33,6 +37,7 @@
import java.util.List;

import butterknife.BindView;
import es.dmoral.toasty.Toasty;

/**
* Created by Kosh on 03 Dec 2016, 3:56 PM
Expand Down Expand Up @@ -152,7 +157,9 @@ public static RepoReleasesFragment newInstance(@NonNull String repoId, @NonNull
ArrayList<SimpleUrlsModel> mapped = Stream.of(item.getAssets())
.filter(value -> value != null && value.getBrowserDownloadUrl() != null)
.map(assetsModel -> new SimpleUrlsModel(String.format("%s (%s)", assetsModel.getName(), assetsModel.getDownloadCount()),
assetsModel.getBrowserDownloadUrl()))
assetsModel.getBrowserDownloadUrl(),
// A filter tag to open this as customTab, see [RestProvider.downloadFile].
":open:"))
.collect(Collectors.toCollection(ArrayList::new));
if (mapped != null && !mapped.isEmpty()) {
models.addAll(mapped);
Expand Down Expand Up @@ -188,6 +195,15 @@ public static RepoReleasesFragment newInstance(@NonNull String repoId, @NonNull
}
}

@Override
public void onItemLongSelected(SimpleUrlsModel item) {
Activity activity = getActivity();
if (activity == null) return;
ClipboardManager clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newRawUri(item.url, Uri.parse(item.url)));
Toasty.info(activity, activity.getString(R.string.success_copied)).show();
}

@Override public void onScrollTop(int index) {
super.onScrollTop(index);
if (recycler != null) recycler.scrollToPosition(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
interface RepoReleasesMvp {

interface View extends BaseMvp.FAView, SwipeRefreshLayout.OnRefreshListener,
android.view.View.OnClickListener, ListDialogView.onSimpleItemSelection<SimpleUrlsModel> {
android.view.View.OnClickListener, ListDialogView.onSimpleItemLongSelection<SimpleUrlsModel>, ListDialogView.onSimpleItemSelection<SimpleUrlsModel> {
void onNotifyAdapter(@Nullable List<Release> items, int page);

@NonNull OnLoadMore getLoadMore();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ class ProjectPagerActivity : BaseActivity<ProjectPagerMvp.View, ProjectPagerPres

override fun isTransparent(): Boolean = true

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
val repoId = presenter.repoId
if (repoId != null && !repoId.isNullOrBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ class FullScreenFileChangeActivity : BaseActivity<FullScreenFileChangeMvp.View,
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.submit -> {
sendResult()
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class WikiActivity : BaseActivity<WikiMvp.View, WikiPresenter>(), WikiMvp.View {
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item?.itemId) {
R.id.menu -> {
drawerLayout.openDrawer(Gravity.END)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ class TrendingActivity : BaseActivity<TrendingMvp.View, TrendingPresenter>(), Tr
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem?): Boolean {
return when (item?.itemId) {
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.menu -> {
drawerLayout.openDrawer(Gravity.END)
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public interface onSimpleItemSelection<O extends Parcelable> {
void onItemSelected(O item);
}

public interface onSimpleItemLongSelection<O extends Parcelable> {
void onItemLongSelected(O item);
}

@Nullable private onSimpleItemSelection onSimpleItemSelection;
@Nullable private onSimpleItemLongSelection onSimpleItemLongSelection;

@Override protected int fragmentLayout() {
return R.layout.simple_list_dialog;
Expand All @@ -65,16 +70,24 @@ public interface onSimpleItemSelection<O extends Parcelable> {

@Override public void onAttach(@NotNull Context context) {
super.onAttach(context);
if (getParentFragment() != null && getParentFragment() instanceof onSimpleItemSelection) {
onSimpleItemSelection = (onSimpleItemSelection) getParentFragment();
} else if (context instanceof onSimpleItemSelection) {
onSimpleItemSelection = (onSimpleItemSelection) context;
if (getParentFragment() != null) {
if (getParentFragment() instanceof onSimpleItemSelection)
onSimpleItemSelection = (onSimpleItemSelection) getParentFragment();
if (getParentFragment() instanceof onSimpleItemLongSelection)
onSimpleItemLongSelection = (onSimpleItemLongSelection) getParentFragment();
} else {
if (context instanceof onSimpleItemSelection)
onSimpleItemSelection = (onSimpleItemSelection) context;
if (context instanceof onSimpleItemLongSelection)
onSimpleItemLongSelection = (onSimpleItemLongSelection) context;
}

}

@Override public void onDetach() {
super.onDetach();
onSimpleItemSelection = null;
onSimpleItemLongSelection = null;
}

@NonNull @Override public TiPresenter providePresenter() {
Expand All @@ -88,7 +101,11 @@ public interface onSimpleItemSelection<O extends Parcelable> {
dismiss();
}

@Override public void onItemLongClick(int position, View v, O item) {}
@SuppressWarnings("unchecked") @Override public void onItemLongClick(int position, View v, O item) {
if (onSimpleItemLongSelection != null) {
onSimpleItemLongSelection.onItemLongSelected(item);
}
}

public void initArguments(@NonNull String title, @NonNull ArrayList<O> objects) {
setArguments(Bundler.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="?android:attr/textColorSecondary"
tools:text="1000"
tools:visibility="gone"/>
tools:text="1000"/>
</LinearLayout>

<com.fastaccess.ui.widgets.ForegroundImageView
Expand Down

0 comments on commit e2243ff

Please sign in to comment.