3

I'm trying to use the google download manager for downloading main and patch expansion files which are both uploaded with the apk. I'm pretty much following the google example. The download process starts but immediately stops with 'STATE_COMPLETED'. No error but the files are still missing. The App only works if I manually copy the files to the device.

This is my Download Activity:

public class DownloadActivity extends Activity implements IDownloaderClient {

    private IStub mDownloaderClientStub;
    private IDownloaderService mRemoteService;

    private boolean downloadDoneRegistered = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        startDownload();
    }

    @Override
    protected void onResume() {
        if (null != mDownloaderClientStub) {
            mDownloaderClientStub.connect(this);
        }
        super.onResume();
    }

    @Override
    protected void onStop() {
        if (null != mDownloaderClientStub) {
            mDownloaderClientStub.disconnect(this);
        }
        super.onStop();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.download, menu);
        return true;
    }

    // IDownloaderClient

    @Override
    public void onServiceConnected(Messenger m) {
        mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
        mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
    }

    @Override
    public void onDownloadStateChanged(int newState) { //todo
        switch (newState) {
            case IDownloaderClient.STATE_IDLE:
            case IDownloaderClient.STATE_CONNECTING:
            case IDownloaderClient.STATE_FETCHING_URL:
            case IDownloaderClient.STATE_DOWNLOADING:
                break;
            case IDownloaderClient.STATE_FAILED_CANCELED:
            case IDownloaderClient.STATE_FAILED:
            case IDownloaderClient.STATE_FAILED_FETCHING_URL:
            case IDownloaderClient.STATE_FAILED_UNLICENSED:
                downloadFailed();
                break;
            case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
            case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
            case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
            case IDownloaderClient.STATE_PAUSED_ROAMING:
            case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
                break;
            case IDownloaderClient.STATE_COMPLETED:
                downloadDone();
                return;
            default:
                break;
        }
    }

    @Override
    public void onDownloadProgress(DownloadProgressInfo progress) {
        float p = (float)progress.mOverallProgress;
        if (progress.mOverallTotal>0) {
            p /= (float)progress.mOverallTotal;
        } else {
            p = 0.0f;
        }
        String s = String.format(getString(R.string.download_progress).replace("#","%"),100.0f*p);
        D.L(this,s);
        setProgressBar(p);
        setText(s);
    }

    // 

    private void startDownload() {
        D.L(this,getString(R.string.download_checkfiles));
        setProgressBar(0.0f);
        setText(getString(R.string.download_checkfiles));
        if (!expansionFilesDelivered()) {
            D.L(this,"expansion files not downloaded so far");
            setProgressBar(0.0f);
            setText(String.format(getString(R.string.download_progress).replace("#","%"),0.0f));
            try {
                Intent launchIntent = DownloadActivity.this.getIntent();
                Intent intentToLaunchThisActivityFromNotification = new Intent(DownloadActivity.this,DownloadActivity.this.getClass());
                intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());
                PendingIntent pendingIntent = PendingIntent.getActivity(DownloadActivity.this,0,intentToLaunchThisActivityFromNotification,PendingIntent.FLAG_UPDATE_CURRENT);
                D.L(this,"start download service");
                int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this,pendingIntent,DownloadService.class);
                D.L(this,""+startResult+" <-compare-> "+DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED+"=NO_DOWNLOAD_REQUIRED");
                if (startResult!=DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
                    D.L(this,"-> download required, create stub");
                    setText(String.format(getString(R.string.download_progress).replace("#","%"),0.0f));
                    mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this,DownloadService.class);
                    return;
                } else {
                    D.L(this,"no download required");
                    downloadDone();
                }
            } catch (NameNotFoundException e) {
                D.L(this,e.getMessage());
                downloadFailed();
            }
        } else {
            // validate apk zip files(?)
            D.L(this,"expansion files valid, no download required");
            downloadDone();
        }
    }

    boolean expansionFilesDelivered() {
        if ((Download.MAIN_EXISTS) &&
            (!Helpers.doesFileExist(this,Helpers.getExpansionAPKFileName(this,true,Download.MAIN_VERSION),Download.MAIN_SIZE,false))) {
            return false;
        }
        if ((Download.PATCH_EXISTS) &&
            (!Helpers.doesFileExist(this,Helpers.getExpansionAPKFileName(this,false,Download.PATCH_VERSION),Download.PATCH_SIZE,false))) {
            return false;
        }
        return true;
    }

    private void setProgressBar(float value) {
        ((ProgressBar)findViewById(R.id.progressBar)).setProgress((int)(1000.0f*value));
    }

    private void setText(String text) {
        ((TextView)findViewById(R.id.textView)).setText(text);
    }

    private void downloadDone() {
        if (!downloadDoneRegistered) {
            downloadDoneRegistered = true;
            D.L(this,"downloadDone(): download terminated");
            setProgressBar(1.0f);
            setText(getString(R.string.download_completed));
            boolean fileAccess = true;
            if (Download.MAIN_EXISTS) {
                Download.SetMainFile(safeFileAccess(Helpers.getExpansionAPKFileName(this,true,Download.MAIN_VERSION)));
                fileAccess = fileAccess && (Download.GetMainFile()!=null);
            } else {
                D.L(this,"no main expansion file");
            }
            if (Download.PATCH_EXISTS) {
                Download.SetPatchFile(safeFileAccess(Helpers.getExpansionAPKFileName(this,false,Download.PATCH_VERSION)));
                fileAccess = fileAccess && (Download.GetPatchFile()!=null);
            } else {
                D.L(this,"no patch expansion file");
            }
            if (fileAccess) {
                D.L(this,"file access passed");
                T.StartActivity(this,SplashActivity.class,true);
            } else {
                fileAccessFailed();
            }
        }
    }

    private File safeFileAccess(String fileName) {
        D.L(this,"try to access file...");
        File r = new File(
                Environment.getExternalStorageDirectory()+File.separator+
                "Android"+File.separator+
                "obb"+File.separator+
                getPackageName() ,
                fileName);
        D.L(this,"  "+r);
        if (r.exists()) {
            D.L(this,"  passed");
        } else {
            D.L(this,"  failed");
            r = null;
        }
        return r;
    }

    private void downloadFailed() {
        D.L(this,getString(R.string.download_failed));
        setText(getString(R.string.download_failed));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.download_failed_dialogtext));
            builder.setPositiveButton(getString(R.string.download_dialog_okay),new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    finish();
                }
            });
            builder.create().show();
    }

    private void fileAccessFailed() {
        D.L(this,getString(R.string.download_accessfailed));
        setText(getString(R.string.download_accessfailed));
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(getString(R.string.download_accessfailed_dialogtext));
            builder.setPositiveButton(getString(R.string.download_dialog_okay),new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    finish();
                }
            });
            builder.create().show();
    }

}

My Alarm Receiver:

public class DownloadAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            DownloaderClientMarshaller.startDownloadServiceIfRequired(context,intent,DownloadService.class);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}

My Download Service:

public class DownloadService extends DownloaderService {

    @Override
    public String getPublicKey() {
        return Download.BASE64_PUBLIC_KEY;
    }

    @Override
    public byte[] getSALT() {
        return Download.SALT;
    }

    @Override
    public String getAlarmReceiverClassName() {
        return DownloadAlarmReceiver.class.getName();
    }

}

Some data stuff:

public class Download {
    private static Download Instance = new Download();
    public static Download GetInstance() { return Instance; }

    //todo
    public static final String BASE64_PUBLIC_KEY = "..actual key..";
    public static final byte[] SALT = new byte[] {..numbers..};
    public static final boolean MAIN_EXISTS = true;
    public static final int MAIN_VERSION = 2;
    public static final long MAIN_SIZE = 20971520L;
    public static final boolean PATCH_EXISTS = true;
    public static final int PATCH_VERSION = 2;
    public static final long PATCH_SIZE = 10485760L;

…

Any ideas?

  • Did you solve your problem? – jimpanzer Jan 20 '15 at 14:09
  • I had this - just had version mismatch: check your manifest app version number matches that of the version on Google play with the expansion file. Also see http://stackoverflow.com/questions/18144732/expansion-file-downloaded-but-not-there – wordy Aug 22 '15 at 09:15

0 Answers0