Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gameblox.gocil;
- import android.app.ActionBar;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.res.Configuration;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.os.Messenger;
- import android.os.SystemClock;
- import android.support.v7.app.ActionBarActivity;
- import android.util.DisplayMetrics;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.ImageButton;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.ProgressBar;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.util.zip.CRC32;
- import expansion.com.google.android.vending.expansion.downloader.Constants;
- import expansion.com.google.android.vending.expansion.downloader.DownloadProgressInfo;
- import expansion.com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;
- import expansion.com.google.android.vending.expansion.downloader.DownloaderServiceMarshaller;
- import expansion.com.google.android.vending.expansion.downloader.Helpers;
- import expansion.com.google.android.vending.expansion.downloader.IDownloaderClient;
- import expansion.com.google.android.vending.expansion.downloader.IDownloaderService;
- import expansion.com.google.android.vending.expansion.downloader.IStub;
- import zip_file.com.android.vending.expansion.zipfile.ZipResourceFile;
- /**
- * Created by vamsikrishna on 12-Feb-15.
- */
- public class SplashActivity extends Activity implements IDownloaderClient {
- private IDownloaderService mRemoteService;
- private IStub mDownloaderClientStub;
- private int mState;
- private boolean mCancelValidation;
- private ProgressBar mDownloadProgressBar;
- private TextView mProgressPercentTextView;
- private View mDownloadViewGroup;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.splash);
- Thread timerThread = new Thread(){
- public void run(){
- try{
- sleep(3000);
- }catch(InterruptedException e){
- e.printStackTrace();
- }finally{
- startActivity(new Intent(SplashActivity.this, MainActivity.class));
- }
- }
- };
- timerThread.start();
- mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(SplashActivity.this, DownloadService.class);
- /**
- * Before we do anything, are the files we expect already here and
- * delivered (presumably by Market) For free titles, this is probably
- * worth doing. (so no Market request is necessary)
- */
- if (!expansionFilesDelivered()) {
- try {
- Intent launchIntent = SplashActivity.this.getIntent();
- Intent intentToLaunchThisActivityFromNotification = new Intent(SplashActivity.this, SplashActivity.this.getClass());
- intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
- intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());
- if (launchIntent.getCategories() != null) {
- for (String category : launchIntent.getCategories()) {
- intentToLaunchThisActivityFromNotification.addCategory(category);
- }
- }
- // Build PendingIntent used to open this activity from
- // Notification
- PendingIntent pendingIntent = PendingIntent.getActivity(SplashActivity.this, 0, intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);
- // Request to start the download
- int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent, DownloadService.class);
- if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
- // The DownloaderService has started downloading the files, show progress
- initializeDownloadUI();
- return;
- } // otherwise, download not needed so we fall through to the app
- } catch (PackageManager.NameNotFoundException e) {
- Log.e("ERRO", "Cannot find package!", e);
- }
- } else {
- validateXAPKZipFiles();
- }
- }
- private void initializeDownloadUI() {
- mDownloadProgressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
- mProgressPercentTextView = (TextView) findViewById(R.id.downloadProgressPercentTextView);
- }
- @Override
- protected void onPause() {
- // TODO Auto-generated method stub
- super.onPause();
- finish();
- }
- // region Expansion Downloader
- private static class XAPKFile {
- public final boolean mIsMain;
- public final int mFileVersion;
- public final long mFileSize;
- XAPKFile(boolean isMain, int fileVersion, long fileSize) {
- mIsMain = isMain;
- mFileVersion = fileVersion;
- mFileSize = fileSize;
- }
- }
- private static final XAPKFile[] xAPKS = {
- new XAPKFile(
- true, // true signifies a main file
- 2, // the version of the APK that the file was uploaded against
- 47529382L // the length of the file in bytes
- )
- };
- static private final float SMOOTHING_FACTOR = 0.005f;
- /**
- * Connect the stub to our service on start.
- */
- @Override
- protected void onStart() {
- if (null != mDownloaderClientStub) {
- mDownloaderClientStub.connect(this);
- }
- super.onStart();
- }
- /**
- * Disconnect the stub from our service on stop
- */
- @Override
- protected void onStop() {
- if (null != mDownloaderClientStub) {
- mDownloaderClientStub.disconnect(this);
- }
- super.onStop();
- }
- /**
- * Critical implementation detail. In onServiceConnected we create the
- * remote service and marshaler. This is how we pass the client information
- * back to the service so the client can be properly notified of changes. We
- * must do this every time we reconnect to the service.
- */
- @Override
- public void onServiceConnected(Messenger m) {
- mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
- mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
- }
- /**
- * The download state should trigger changes in the UI --- it may be useful
- * to show the state as being indeterminate at times. This sample can be
- * considered a guideline.
- */
- @Override
- public void onDownloadStateChanged(int newState) {
- setState(newState);
- boolean showDashboard = true;
- boolean showCellMessage = false;
- boolean paused;
- boolean indeterminate;
- switch (newState) {
- case IDownloaderClient.STATE_IDLE:
- // STATE_IDLE means the service is listening, so it's
- // safe to start making calls via mRemoteService.
- paused = false;
- indeterminate = true;
- break;
- case IDownloaderClient.STATE_CONNECTING:
- case IDownloaderClient.STATE_FETCHING_URL:
- showDashboard = true;
- paused = false;
- indeterminate = true;
- break;
- case IDownloaderClient.STATE_DOWNLOADING:
- paused = false;
- showDashboard = true;
- indeterminate = false;
- break;
- case IDownloaderClient.STATE_FAILED_CANCELED:
- case IDownloaderClient.STATE_FAILED:
- case IDownloaderClient.STATE_FAILED_FETCHING_URL:
- case IDownloaderClient.STATE_FAILED_UNLICENSED:
- paused = true;
- showDashboard = false;
- indeterminate = false;
- break;
- case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
- case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
- showDashboard = false;
- paused = true;
- indeterminate = false;
- showCellMessage = true;
- break;
- case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
- paused = true;
- indeterminate = false;
- break;
- case IDownloaderClient.STATE_PAUSED_ROAMING:
- case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
- paused = true;
- indeterminate = false;
- break;
- case IDownloaderClient.STATE_COMPLETED:
- showDashboard = false;
- paused = false;
- indeterminate = false;
- validateXAPKZipFiles();
- return;
- default:
- paused = true;
- indeterminate = true;
- showDashboard = true;
- }
- int newDashboardVisibility = showDashboard ? View.VISIBLE : View.GONE;
- if (mDownloadViewGroup.getVisibility() != newDashboardVisibility) {
- mDownloadViewGroup.setVisibility(newDashboardVisibility);
- }
- mDownloadProgressBar.setIndeterminate(indeterminate);
- }
- /**
- * Sets the state of the various controls based on the progressinfo object
- * sent from the downloader service.
- */
- @Override
- public void onDownloadProgress(DownloadProgressInfo progress) {
- mDownloadProgressBar.setMax((int) (progress.mOverallTotal >> 8));
- mDownloadProgressBar.setProgress((int) (progress.mOverallProgress >> 8));
- mProgressPercentTextView.setText(Long.toString(progress.mOverallProgress * 100 / progress.mOverallTotal) + "%");
- }
- /**
- * Go through each of the Expansion APK files and open each as a zip file.
- * Calculate the CRC for each file and return false if any fail to match.
- *
- * @return true if XAPKZipFile is successful
- */
- void validateXAPKZipFiles() {
- AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {
- @Override
- protected void onPreExecute() {
- mDownloadViewGroup.setVisibility(View.VISIBLE);
- super.onPreExecute();
- }
- @Override
- protected Boolean doInBackground(Object... params) {
- for (XAPKFile xf : xAPKS) {
- String fileName = Helpers.getExpansionAPKFileName(SplashActivity.this, xf.mIsMain, xf.mFileVersion);
- if (!Helpers.doesFileExist(SplashActivity.this, fileName, xf.mFileSize, false))
- return false;
- fileName = Helpers.generateSaveFileName(SplashActivity.this, fileName);
- ZipResourceFile zrf;
- byte[] buf = new byte[1024 * 256];
- try {
- zrf = new ZipResourceFile(fileName);
- ZipResourceFile.ZipEntryRO[] entries = zrf.getAllEntries();
- /**
- * First calculate the total compressed length
- */
- long totalCompressedLength = 0;
- for (ZipResourceFile.ZipEntryRO entry : entries) {
- totalCompressedLength += entry.mCompressedLength;
- }
- float averageVerifySpeed = 0;
- long totalBytesRemaining = totalCompressedLength;
- long timeRemaining;
- /**
- * Then calculate a CRC for every file in the Zip file,
- * comparing it to what is stored in the Zip directory.
- * Note that for compressed Zip files we must extract
- * the contents to do this comparison.
- */
- for (ZipResourceFile.ZipEntryRO entry : entries) {
- if (-1 != entry.mCRC32) {
- long length = entry.mUncompressedLength;
- CRC32 crc = new CRC32();
- DataInputStream dis = null;
- try {
- dis = new DataInputStream(zrf.getInputStream(entry.mFileName));
- long startTime = SystemClock.uptimeMillis();
- while (length > 0) {
- int seek = (int) (length > buf.length ? buf.length : length);
- dis.readFully(buf, 0, seek);
- crc.update(buf, 0, seek);
- length -= seek;
- long currentTime = SystemClock.uptimeMillis();
- long timePassed = currentTime - startTime;
- if (timePassed > 0) {
- float currentSpeedSample = (float) seek / (float) timePassed;
- if (0 != averageVerifySpeed) {
- averageVerifySpeed = SMOOTHING_FACTOR * currentSpeedSample + (1 - SMOOTHING_FACTOR) * averageVerifySpeed;
- } else {
- averageVerifySpeed = currentSpeedSample;
- }
- totalBytesRemaining -= seek;
- timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
- this.publishProgress(new DownloadProgressInfo(totalCompressedLength, totalCompressedLength - totalBytesRemaining, timeRemaining, averageVerifySpeed));
- }
- startTime = currentTime;
- if (mCancelValidation)
- return true;
- }
- if (crc.getValue() != entry.mCRC32) {
- Log.e(Constants.TAG, "CRC does not match for entry: " + entry.mFileName);
- Log.e(Constants.TAG, "In file: " + entry.getZipFileName());
- return false;
- }
- } finally {
- if (null != dis) {
- dis.close();
- }
- }
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- }
- return true;
- }
- @Override
- protected void onProgressUpdate(DownloadProgressInfo... values) {
- onDownloadProgress(values[0]);
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(Boolean result) {
- if (result) {
- mDownloadViewGroup.setVisibility(View.GONE);
- } else {
- mDownloadViewGroup.setVisibility(View.VISIBLE);
- }
- super.onPostExecute(result);
- }
- };
- validationTask.execute(new Object());
- }
- boolean expansionFilesDelivered() {
- for (XAPKFile xf : xAPKS) {
- String fileName = Helpers.getExpansionAPKFileName(this, xf.mIsMain, xf.mFileVersion);
- if (!Helpers.doesFileExist(this, fileName, xf.mFileSize, false))
- return false;
- }
- return true;
- }
- private void setState(int newState) {
- if (mState != newState) {
- mState = newState;
- }
- }
- @Override
- protected void onDestroy() {
- this.mCancelValidation = true;
- super.onDestroy();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement