0

I have an array of Urls which I would synchronously. In fact I'm in an asynctask with a for loop. I tried using downloadmanager and broadcast receiver without success (the manager use another thread so I can't unregister each time, a leak append).

This is my Asynctask :

    package com.ylly.hypred.splashScreen.asynctask;

import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.WindowManager;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.ylly.hypred.R;
import com.ylly.hypred.api.CallAPI;
import com.ylly.hypred.api.model.MediaAPIModel;
import com.ylly.hypred.api.model.MediaArrayAPIModel;
import com.ylly.hypred.utils.FileManager;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by YLLY on 17-11-2015.
 */
public class LoadMedias extends AsyncTask<String, Void, String> {
    private MediaArrayAPIModel mediaAPIModels;
    private Context context;
    private ProgressDialog progressDialog;
    private boolean isCallFromUpdate;
    private final String TAG = "LoadMedias";
    private OnLoadingIsFinished mCallback;

    /**
     *
     */
    public interface OnLoadingIsFinished {
        void callProcess();
    }

    /**
     *
     * @param context
     * @param urls
     * @param isCallFromUpdate
     */
    public LoadMedias(Context context, MediaArrayAPIModel urls, boolean isCallFromUpdate) {
        this.context = context;
        this.mediaAPIModels = urls;
        progressDialog = new ProgressDialog(context);
        this.isCallFromUpdate = isCallFromUpdate;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (!isCallFromUpdate)
            progressDialog.setTitle(context.getString(R.string.fm_etape_trois_sur_trois));

        progressDialog.setMessage(context.getString(R.string.fm_enregistrement));
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        progressDialog.setMax(mediaAPIModels.getMediaImagePIModels().size() + mediaAPIModels.getMediaFileAPIModels().size());
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        mCallback.callProcess();
    }

    @Override
    protected String doInBackground(String... arg0) {
        try {
            loadMediaImages(mediaAPIModels.getMediaImagePIModels(), progressDialog);
            loadMediaFiles(context, mediaAPIModels.getMediaFileAPIModels(), progressDialog);
        } catch (Exception e) {
            Log.d(TAG, e.toString());
        }
        return "";
    }

    /**
     *
     * @param onLoadingIsFinished
     */
    public void setOnLoadingIsFinished(OnLoadingIsFinished onLoadingIsFinished) {
        this.mCallback = onLoadingIsFinished;
    }

    /**
     * @param mediaAPIModels
     * @param progressDialog
     */
    private void loadMediaImages(ArrayList<MediaAPIModel> mediaAPIModels, ProgressDialog progressDialog) {
        String type;
        for (int i = 0; i < mediaAPIModels.size(); i++) {
            type = mediaAPIModels.get(i).getUrl().substring(mediaAPIModels.get(i).getUrl().lastIndexOf("."));
            if (type.equals(".png") || type.equals(".jpg") || type.equals(".jpeg")) {
                ImageLoader.getInstance().loadImageSync(CallAPI.mBaseUrl + mediaAPIModels.get(i).getUrl());
                i++;
                progressDialog.setProgress(progressDialog.getProgress() + 1);
            }
        }
    }

    /**
     * @param context
     * @param mediaAPIModels
     * @param progressDialog
     */
    private void loadMediaFiles(Context context, ArrayList<MediaAPIModel> mediaAPIModels, final ProgressDialog progressDialog) {
        {
            String name;
            String mime;
            for (int i = 0; i < mediaAPIModels.size(); i++) {
                if (!FileManager.getInstance().getFileFromLocal(context, mediaAPIModels.get(i).getUrl(), false)) {
                    name = mediaAPIModels.get(i).getUrl().substring(mediaAPIModels.get(i).getUrl().lastIndexOf("/") + 1); //on sépare le nom du fichier du reste de l'url
                    mime = name.substring(name.lastIndexOf(".")); //on recupere l'extension du fichier

                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(CallAPI.mBaseUrl + mediaAPIModels.get(i).getUrl()));
                    request.setDescription(context.getResources().getString(R.string.downloading) + " name");
                    request.setTitle(context.getResources().getString(R.string.app_name));
                    request.setMimeType(mime);
                    // in order for this if to run, you must use the android 3.2 to compile your app
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                        request.allowScanningByMediaScanner();
                        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                    }

                    request.setDestinationInExternalPublicDir("test" + File.separatorChar + "doc", name);

                    // get download service and enqueue file
                    DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);

                    context.registerReceiver(new BroadcastReceiver() {
                        @Override
                        public void onReceive(Context context, Intent intent) {
                            context.unregisterReceiver(this);
                            Log.d("DOWNLOAD", "unregister");
                            progressDialog.setProgress(progressDialog.getProgress() + 1);
                        }
                    }, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
                } else {
                    i++;
                    progressDialog.setProgress(progressDialog.getProgress() + 1);
                }
            }
        }
    }
}

The cache loading works.

Thanks in advance ;)

Kupris
  • 165
  • 1
  • 12
  • 1
    Um, use an HTTP client API, such as `HttpUrlConnection` or ones from a third-party library (e.g., OkHttp). And *seriously* reconsider using an `AsyncTask` here, unless you do not care about whether the files get downloaded. – CommonsWare Nov 17 '15 at 15:47
  • It would be better to add a step in the main thread with actual operation (downloadManager) ? – Kupris Nov 17 '15 at 15:52
  • @CommonsWare please explain why this is such a bad practice. I wouldn't personally use a AsyncTask here but I would like to know why I really shouldn't. – Jonas Borggren Nov 17 '15 at 15:59
  • AsyncTask will manage the threading for you, it will make the download in a background thread and will notify you in the main thread when it finish, thats why you should use it, anyway @CommonsWare said "unless you do not care about whether the files get downloaded" – Nanoc Nov 17 '15 at 16:00
  • @JonasB: `AsyncTask` is really for fairly disposable stuff (e.g., downloading Twitter avatars for your timeline in your Twitter client). If the user leaves your UI (e.g., presses HOME), Android may terminate your process before your work is done. The longer that work takes, the more likely this becomes. Use an `IntentService`, to signal to the OS that you're actively doing work on behalf of the user, and your process is more likely to hang around long enough to complete a list of downloads. – CommonsWare Nov 17 '15 at 16:03

0 Answers0