Android Open Source - C2Framework C2 Downloads Content Provider






From Project

Back to project page C2Framework.

License

The source code is released under:

Apache License

If you think the Android project C2Framework listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package gaia.c2.content.tools;
// w ww  .  j  a  va2 s .co  m
import android.content.Context;
import android.os.Bundle;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

import gaia.c2.content.C2ContentProvider;
import gaia.c2.content.QueryHandler;
import gaia.c2.content.tools.model.DownloadStatus;

/**
 * Created by kmr on 4/21/14.
 */
public class C2DownloadsContentProvider extends C2ContentProvider {
    public static final String TICK_EVERY_PCNT = "TickEvery";
    public static final String DOWNLOAD_URL = "DownloadUrl";
    public static final String TICKET = "Ticket";

    public C2DownloadsContentProvider(Context androidParentContext) {
        super(androidParentContext);
    }

    @Override
    public String getMimeType() {
        return DownloadStatus.MIME_TYPE;
    }

    @Override
    public String getAuthority() {
        return "gaia.downloader";
    }

    @Override
    public void onCreate() {
        super.onCreate();

        on("download", new QueryHandler() {
            @Override
            public void handle(String method, Bundle parameters) throws Exception {
                URL url = new URL(parameters.getString(DOWNLOAD_URL));
                String ticket = parameters.getString(TICKET);

                if (ticket == null) {
                    ticket = "*";
                }

                int tickPcnt = parameters.getInt(TICK_EVERY_PCNT, 1);

                downloadProcess(url, ticket, tickPcnt);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void downloadProcess(URL url, String ticket, int tick) throws IOException {
        final URLConnection urlConnection = url.openConnection();
        long currentDownloadedCount = 0;
        final long currentDownloadSize = urlConnection.getContentLength();
        final File tmpFile = File.createTempFile("c2dwn__" + ticket, ".any", getContext().getCacheDir());

        final OutputStream os = new BufferedOutputStream(new FileOutputStream(tmpFile));
        final InputStream is = urlConnection.getInputStream();

        byte buffer[] = new byte[2048];
        int readedCount = 0;
        int curPcnt = 0;

        while ((readedCount = is.read(buffer)) != -1) {
            currentDownloadedCount += readedCount;
            os.write(buffer, 0, readedCount);

            final double asDoubleDownloadCount = currentDownloadedCount;
            final double asDoubleDownloadSize = currentDownloadSize;

            final int pcnt = (int) (((float ) (asDoubleDownloadCount / asDoubleDownloadSize)) * 100.0);
            if (pcnt % tick == 0 && pcnt != curPcnt) {
                curPcnt = pcnt;

                DownloadStatus ds = new DownloadStatus(ticket, currentDownloadedCount, currentDownloadSize, false, tmpFile);
                broadcast(ds);
            }
        }
        os.flush();
        os.close();
        is.close();

        DownloadStatus ds = new DownloadStatus(ticket, currentDownloadSize, currentDownloadSize, true, tmpFile);
        broadcast(ds);
    }
}




Java Source Code List

gaia.c2.content.C2ContentProvider.java
gaia.c2.content.C2ContentService.java
gaia.c2.content.QueryHandler.java
gaia.c2.content.android.CallContentProvider.java
gaia.c2.content.android.DialContentProvider.java
gaia.c2.content.android.VibrationContentProvider.java
gaia.c2.content.android.model.Call.java
gaia.c2.content.android.model.Dial.java
gaia.c2.content.android.model.Vibration.java
gaia.c2.content.model.ContextDependentModel.java
gaia.c2.content.sqlite.C2SQLiteContentProvider.java
gaia.c2.content.sqlite.C2SQLiteHelper.java
gaia.c2.content.tools.C2DownloadsContentProvider.java
gaia.c2.content.tools.model.DownloadStatus.java
gaia.c2.context.C2Context.java
gaia.c2.context.DefaultC2Context.java
gaia.c2.context.view.C2Message.java
gaia.c2.context.view.C2View.java
gaia.c2.receivers.ContextBroadcastReceiver.java
gaia.c2.receivers.ContextEventReceiver.java