Allow the long press for download in another activities

This commit is contained in:
tom79 2019-05-06 07:44:47 +02:00
parent 643cb78d55
commit 25a2972e89
3 changed files with 90 additions and 79 deletions

View File

@ -177,6 +177,7 @@ import static fr.gouv.etalab.mastodon.sqlite.Sqlite.importDB;
public abstract class BaseMainActivity extends BaseActivity
implements NavigationView.OnNavigationItemSelectedListener, OnUpdateAccountInfoInterface, OnRetrieveMetaDataInterface, OnRetrieveInstanceInterface, OnRetrieveRemoteAccountInterface, OnRetrieveEmojiAccountInterface, OnFilterActionInterface, OnSyncTimelineInterface {
private FloatingActionButton toot, delete_all, add_new;
private HashMap<String, String> tagTile = new HashMap<>();
private HashMap<String, Integer> tagItem = new HashMap<>();
@ -204,8 +205,7 @@ public abstract class BaseMainActivity extends BaseActivity
private final int PICK_IMPORT = 5556;
private AlertDialog.Builder dialogBuilderOptin;
private List<ManageTimelines> timelines;
private FetchConfiguration fetchConfiguration;
private Fetch fetch;
public static HashMap<Integer, Fragment> mPageReferenceMap = new HashMap<>();
private static boolean notificationChecked = false;
@ -254,7 +254,6 @@ public abstract class BaseMainActivity extends BaseActivity
finish();
return;
}
initializeDownload();
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
switch (theme){
case Helper.THEME_LIGHT:
@ -2136,83 +2135,8 @@ public abstract class BaseMainActivity extends BaseActivity
}
}
private void initializeDownload(){
fetchConfiguration = new FetchConfiguration.Builder(this)
.setDownloadConcurrentLimit(3)
.build();
fetch = Fetch.Impl.getInstance(fetchConfiguration);
FetchListener fetchListener = new FetchListener() {
@Override
public void onWaitingNetwork(@NotNull Download download) {
}
@Override
public void onStarted(@NotNull Download download, @NotNull List<? extends DownloadBlock> list, int i) {
}
@Override
public void onResumed(@NotNull Download download) {
}
@Override
public void onRemoved(@NotNull Download download) {
}
@Override
public void onQueued(@NotNull Download download, boolean b) {
}
@Override
public void onProgress(@NotNull Download download, long l, long l1) {
}
@Override
public void onPaused(@NotNull Download download) {
}
@Override
public void onError(@NotNull Download download, @NotNull Error error, @Nullable Throwable throwable) {
Toasty.error(getApplicationContext(), getString(R.string.toast_error),Toast.LENGTH_LONG).show();
}
@Override
public void onDownloadBlockUpdated(@NotNull Download download, @NotNull DownloadBlock downloadBlock, int i) {
}
@Override
public void onDeleted(@NotNull Download download) {
}
@Override
public void onCompleted(@NotNull Download download) {
if( download.getFileUri().getPath() != null) {
String url = download.getUrl();
final String fileName = URLUtil.guessFileName(url, null, null);
final SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String mime = Helper.getMimeType(url);
File file = new File(download.getFileUri().getPath());
final Intent intent = new Intent();
Random r = new Random();
final int notificationIdTmp = r.nextInt(10000);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + file.getAbsolutePath() );
intent.setDataAndType(uri, mime);
Helper.notify_user(getApplicationContext(), intent, notificationIdTmp, BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher), Helper.NotifType.STORE, getString(R.string.save_over), getString(R.string.download_from, fileName));
}
}
@Override
public void onCancelled(@NotNull Download download) {
}
@Override
public void onAdded(@NotNull Download download) {
}
};
fetch.addListener(fetchListener);
}
public void download(String file, String url){
final Request request = new Request(url, file);
request.setPriority(Priority.HIGH);
request.setNetworkType(NetworkType.ALL);
fetch.enqueue(request, updatedRequest -> {
//Request was successfully enqueued for download.
}, error -> {
});
}
public boolean getFloatingVisibility(){

View File

@ -2950,7 +2950,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
public boolean onLongClick(View v) {
String myDir = sharedpreferences.getString(Helper.SET_FOLDER_RECORD, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath());
String fileName = URLUtil.guessFileName(attachment.getUrl(), null, null);
((MainActivity)context).download(myDir+"/"+fileName, attachment.getUrl());
Helper.download(context,myDir+"/"+fileName, attachment.getUrl());
return true;
}
});

View File

@ -118,8 +118,18 @@ import com.jaredrummler.materialspinner.MaterialSpinner;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;
import com.tonyodev.fetch2.Download;
import com.tonyodev.fetch2.Error;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchConfiguration;
import com.tonyodev.fetch2.FetchListener;
import com.tonyodev.fetch2.NetworkType;
import com.tonyodev.fetch2.Priority;
import com.tonyodev.fetch2.Request;
import com.tonyodev.fetch2core.DownloadBlock;
import org.conscrypt.Conscrypt;
import org.jetbrains.annotations.NotNull;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
@ -3859,5 +3869,82 @@ public class Helper {
}
public static void download(Context context, String file, String url){
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(context)
.setDownloadConcurrentLimit(3)
.build();
Fetch fetch = Fetch.Impl.getInstance(fetchConfiguration);
FetchListener fetchListener = new FetchListener() {
@Override
public void onWaitingNetwork(@NotNull Download download) {
}
@Override
public void onStarted(@NotNull Download download, @NotNull List<? extends DownloadBlock> list, int i) {
}
@Override
public void onResumed(@NotNull Download download) {
}
@Override
public void onRemoved(@NotNull Download download) {
}
@Override
public void onQueued(@NotNull Download download, boolean b) {
}
@Override
public void onProgress(@NotNull Download download, long l, long l1) {
}
@Override
public void onPaused(@NotNull Download download) {
}
@Override
public void onError(@NotNull Download download, @NotNull Error error, @org.jetbrains.annotations.Nullable Throwable throwable) {
Toasty.error(context, context.getString(R.string.toast_error),Toast.LENGTH_LONG).show();
}
@Override
public void onDownloadBlockUpdated(@NotNull Download download, @NotNull DownloadBlock downloadBlock, int i) {
}
@Override
public void onDeleted(@NotNull Download download) {
}
@Override
public void onCompleted(@NotNull Download download) {
if( download.getFileUri().getPath() != null) {
String url = download.getUrl();
final String fileName = URLUtil.guessFileName(url, null, null);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
String mime = Helper.getMimeType(url);
File file = new File(download.getFileUri().getPath());
final Intent intent = new Intent();
Random r = new Random();
final int notificationIdTmp = r.nextInt(10000);
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri uri = Uri.parse("file://" + file.getAbsolutePath() );
intent.setDataAndType(uri, mime);
Helper.notify_user(context, intent, notificationIdTmp, BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher), Helper.NotifType.STORE, context.getString(R.string.save_over), context.getString(R.string.download_from, fileName));
}
}
@Override
public void onCancelled(@NotNull Download download) {
}
@Override
public void onAdded(@NotNull Download download) {
}
};
fetch.addListener(fetchListener);
final Request request = new Request(url, file);
request.setPriority(Priority.HIGH);
request.setNetworkType(NetworkType.ALL);
fetch.enqueue(request, updatedRequest -> {
//Request was successfully enqueued for download.
}, error -> {
});
}
}