Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.94 KB | None | 0 0
  1. package com.gameblox.gocil;
  2.  
  3. import android.app.ActionBar;
  4. import android.app.Activity;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. import android.support.v7.app.ActionBarActivity;
  8.  
  9. import android.app.PendingIntent;
  10. import android.content.Intent;
  11. import android.content.pm.PackageManager;
  12. import android.content.res.Configuration;
  13. import android.graphics.Bitmap;
  14. import android.graphics.BitmapFactory;
  15. import android.os.AsyncTask;
  16. import android.os.Bundle;
  17. import android.os.Messenger;
  18. import android.os.SystemClock;
  19. import android.support.v7.app.ActionBarActivity;
  20. import android.util.DisplayMetrics;
  21. import android.util.Log;
  22. import android.view.Menu;
  23. import android.view.MenuItem;
  24. import android.view.View;
  25. import android.widget.ImageButton;
  26. import android.widget.ImageView;
  27. import android.widget.LinearLayout;
  28. import android.widget.ProgressBar;
  29. import android.widget.RelativeLayout;
  30. import android.widget.TextView;
  31.  
  32. import java.io.DataInputStream;
  33. import java.io.IOException;
  34. import java.util.zip.CRC32;
  35.  
  36. import expansion.com.google.android.vending.expansion.downloader.Constants;
  37. import expansion.com.google.android.vending.expansion.downloader.DownloadProgressInfo;
  38. import expansion.com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;
  39. import expansion.com.google.android.vending.expansion.downloader.DownloaderServiceMarshaller;
  40. import expansion.com.google.android.vending.expansion.downloader.Helpers;
  41. import expansion.com.google.android.vending.expansion.downloader.IDownloaderClient;
  42. import expansion.com.google.android.vending.expansion.downloader.IDownloaderService;
  43. import expansion.com.google.android.vending.expansion.downloader.IStub;
  44. import zip_file.com.android.vending.expansion.zipfile.ZipResourceFile;
  45.  
  46.  
  47. /**
  48. * Created by vamsikrishna on 12-Feb-15.
  49. */
  50. public class SplashActivity extends Activity implements IDownloaderClient {
  51. private IDownloaderService mRemoteService;
  52. private IStub mDownloaderClientStub;
  53. private int mState;
  54. private boolean mCancelValidation;
  55. private ProgressBar mDownloadProgressBar;
  56. private TextView mProgressPercentTextView;
  57. private View mDownloadViewGroup;
  58.  
  59. @Override
  60. protected void onCreate(Bundle savedInstanceState) {
  61. // TODO Auto-generated method stub
  62. super.onCreate(savedInstanceState);
  63. setContentView(R.layout.splash);
  64.  
  65. Thread timerThread = new Thread(){
  66. public void run(){
  67. try{
  68. sleep(3000);
  69. }catch(InterruptedException e){
  70. e.printStackTrace();
  71. }finally{
  72. startActivity(new Intent(SplashActivity.this, MainActivity.class));
  73. }
  74. }
  75. };
  76. timerThread.start();
  77. mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(SplashActivity.this, DownloadService.class);
  78.  
  79. /**
  80. * Before we do anything, are the files we expect already here and
  81. * delivered (presumably by Market) For free titles, this is probably
  82. * worth doing. (so no Market request is necessary)
  83. */
  84. if (!expansionFilesDelivered()) {
  85.  
  86. try {
  87. Intent launchIntent = SplashActivity.this.getIntent();
  88. Intent intentToLaunchThisActivityFromNotification = new Intent(SplashActivity.this, SplashActivity.this.getClass());
  89. intentToLaunchThisActivityFromNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
  90. intentToLaunchThisActivityFromNotification.setAction(launchIntent.getAction());
  91.  
  92. if (launchIntent.getCategories() != null) {
  93. for (String category : launchIntent.getCategories()) {
  94. intentToLaunchThisActivityFromNotification.addCategory(category);
  95. }
  96. }
  97.  
  98. // Build PendingIntent used to open this activity from
  99. // Notification
  100. PendingIntent pendingIntent = PendingIntent.getActivity(SplashActivity.this, 0, intentToLaunchThisActivityFromNotification, PendingIntent.FLAG_UPDATE_CURRENT);
  101. // Request to start the download
  102. int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent, DownloadService.class);
  103.  
  104. if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
  105. // The DownloaderService has started downloading the files, show progress
  106. initializeDownloadUI();
  107. return;
  108. } // otherwise, download not needed so we fall through to the app
  109. } catch (PackageManager.NameNotFoundException e) {
  110. Log.e("ERRO", "Cannot find package!", e);
  111. }
  112. } else {
  113. validateXAPKZipFiles();
  114. }
  115.  
  116. }
  117.  
  118. private void initializeDownloadUI() {
  119.  
  120. mDownloadProgressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);
  121. mProgressPercentTextView = (TextView) findViewById(R.id.downloadProgressPercentTextView);
  122.  
  123. }
  124. @Override
  125. protected void onPause() {
  126. // TODO Auto-generated method stub
  127. super.onPause();
  128. finish();
  129. }
  130. // region Expansion Downloader
  131. private static class XAPKFile {
  132. public final boolean mIsMain;
  133. public final int mFileVersion;
  134. public final long mFileSize;
  135.  
  136. XAPKFile(boolean isMain, int fileVersion, long fileSize) {
  137. mIsMain = isMain;
  138. mFileVersion = fileVersion;
  139. mFileSize = fileSize;
  140. }
  141. }
  142.  
  143. private static final XAPKFile[] xAPKS = {
  144. new XAPKFile(
  145. true, // true signifies a main file
  146. 2, // the version of the APK that the file was uploaded against
  147. 47529382L // the length of the file in bytes
  148. )
  149. };
  150. static private final float SMOOTHING_FACTOR = 0.005f;
  151.  
  152. /**
  153. * Connect the stub to our service on start.
  154. */
  155. @Override
  156. protected void onStart() {
  157. if (null != mDownloaderClientStub) {
  158. mDownloaderClientStub.connect(this);
  159. }
  160. super.onStart();
  161. }
  162.  
  163. /**
  164. * Disconnect the stub from our service on stop
  165. */
  166. @Override
  167. protected void onStop() {
  168. if (null != mDownloaderClientStub) {
  169. mDownloaderClientStub.disconnect(this);
  170. }
  171. super.onStop();
  172. }
  173.  
  174. /**
  175. * Critical implementation detail. In onServiceConnected we create the
  176. * remote service and marshaler. This is how we pass the client information
  177. * back to the service so the client can be properly notified of changes. We
  178. * must do this every time we reconnect to the service.
  179. */
  180. @Override
  181. public void onServiceConnected(Messenger m) {
  182. mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
  183. mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
  184. }
  185.  
  186. /**
  187. * The download state should trigger changes in the UI --- it may be useful
  188. * to show the state as being indeterminate at times. This sample can be
  189. * considered a guideline.
  190. */
  191. @Override
  192. public void onDownloadStateChanged(int newState) {
  193. setState(newState);
  194. boolean showDashboard = true;
  195. boolean showCellMessage = false;
  196. boolean paused;
  197. boolean indeterminate;
  198. switch (newState) {
  199. case IDownloaderClient.STATE_IDLE:
  200. // STATE_IDLE means the service is listening, so it's
  201. // safe to start making calls via mRemoteService.
  202. paused = false;
  203. indeterminate = true;
  204. break;
  205. case IDownloaderClient.STATE_CONNECTING:
  206. case IDownloaderClient.STATE_FETCHING_URL:
  207. showDashboard = true;
  208. paused = false;
  209. indeterminate = true;
  210. break;
  211. case IDownloaderClient.STATE_DOWNLOADING:
  212. paused = false;
  213. showDashboard = true;
  214. indeterminate = false;
  215. break;
  216.  
  217. case IDownloaderClient.STATE_FAILED_CANCELED:
  218. case IDownloaderClient.STATE_FAILED:
  219. case IDownloaderClient.STATE_FAILED_FETCHING_URL:
  220. case IDownloaderClient.STATE_FAILED_UNLICENSED:
  221. paused = true;
  222. showDashboard = false;
  223. indeterminate = false;
  224. break;
  225. case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
  226. case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
  227. showDashboard = false;
  228. paused = true;
  229. indeterminate = false;
  230. showCellMessage = true;
  231. break;
  232.  
  233. case IDownloaderClient.STATE_PAUSED_BY_REQUEST:
  234. paused = true;
  235. indeterminate = false;
  236. break;
  237. case IDownloaderClient.STATE_PAUSED_ROAMING:
  238. case IDownloaderClient.STATE_PAUSED_SDCARD_UNAVAILABLE:
  239. paused = true;
  240. indeterminate = false;
  241. break;
  242. case IDownloaderClient.STATE_COMPLETED:
  243. showDashboard = false;
  244. paused = false;
  245. indeterminate = false;
  246. validateXAPKZipFiles();
  247. return;
  248. default:
  249. paused = true;
  250. indeterminate = true;
  251. showDashboard = true;
  252. }
  253. int newDashboardVisibility = showDashboard ? View.VISIBLE : View.GONE;
  254. if (mDownloadViewGroup.getVisibility() != newDashboardVisibility) {
  255. mDownloadViewGroup.setVisibility(newDashboardVisibility);
  256. }
  257. mDownloadProgressBar.setIndeterminate(indeterminate);
  258. }
  259.  
  260. /**
  261. * Sets the state of the various controls based on the progressinfo object
  262. * sent from the downloader service.
  263. */
  264. @Override
  265. public void onDownloadProgress(DownloadProgressInfo progress) {
  266. mDownloadProgressBar.setMax((int) (progress.mOverallTotal >> 8));
  267. mDownloadProgressBar.setProgress((int) (progress.mOverallProgress >> 8));
  268. mProgressPercentTextView.setText(Long.toString(progress.mOverallProgress * 100 / progress.mOverallTotal) + "%");
  269. }
  270.  
  271. /**
  272. * Go through each of the Expansion APK files and open each as a zip file.
  273. * Calculate the CRC for each file and return false if any fail to match.
  274. *
  275. * @return true if XAPKZipFile is successful
  276. */
  277. void validateXAPKZipFiles() {
  278. AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {
  279.  
  280. @Override
  281. protected void onPreExecute() {
  282. mDownloadViewGroup.setVisibility(View.VISIBLE);
  283. super.onPreExecute();
  284. }
  285.  
  286. @Override
  287. protected Boolean doInBackground(Object... params) {
  288. for (XAPKFile xf : xAPKS) {
  289. String fileName = Helpers.getExpansionAPKFileName(SplashActivity.this, xf.mIsMain, xf.mFileVersion);
  290. if (!Helpers.doesFileExist(SplashActivity.this, fileName, xf.mFileSize, false))
  291. return false;
  292. fileName = Helpers.generateSaveFileName(SplashActivity.this, fileName);
  293. ZipResourceFile zrf;
  294. byte[] buf = new byte[1024 * 256];
  295. try {
  296. zrf = new ZipResourceFile(fileName);
  297. ZipResourceFile.ZipEntryRO[] entries = zrf.getAllEntries();
  298. /**
  299. * First calculate the total compressed length
  300. */
  301. long totalCompressedLength = 0;
  302. for (ZipResourceFile.ZipEntryRO entry : entries) {
  303. totalCompressedLength += entry.mCompressedLength;
  304. }
  305. float averageVerifySpeed = 0;
  306. long totalBytesRemaining = totalCompressedLength;
  307. long timeRemaining;
  308. /**
  309. * Then calculate a CRC for every file in the Zip file,
  310. * comparing it to what is stored in the Zip directory.
  311. * Note that for compressed Zip files we must extract
  312. * the contents to do this comparison.
  313. */
  314. for (ZipResourceFile.ZipEntryRO entry : entries) {
  315. if (-1 != entry.mCRC32) {
  316. long length = entry.mUncompressedLength;
  317. CRC32 crc = new CRC32();
  318. DataInputStream dis = null;
  319. try {
  320. dis = new DataInputStream(zrf.getInputStream(entry.mFileName));
  321.  
  322. long startTime = SystemClock.uptimeMillis();
  323. while (length > 0) {
  324. int seek = (int) (length > buf.length ? buf.length : length);
  325. dis.readFully(buf, 0, seek);
  326. crc.update(buf, 0, seek);
  327. length -= seek;
  328. long currentTime = SystemClock.uptimeMillis();
  329. long timePassed = currentTime - startTime;
  330. if (timePassed > 0) {
  331. float currentSpeedSample = (float) seek / (float) timePassed;
  332. if (0 != averageVerifySpeed) {
  333. averageVerifySpeed = SMOOTHING_FACTOR * currentSpeedSample + (1 - SMOOTHING_FACTOR) * averageVerifySpeed;
  334. } else {
  335. averageVerifySpeed = currentSpeedSample;
  336. }
  337. totalBytesRemaining -= seek;
  338. timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
  339. this.publishProgress(new DownloadProgressInfo(totalCompressedLength, totalCompressedLength - totalBytesRemaining, timeRemaining, averageVerifySpeed));
  340. }
  341. startTime = currentTime;
  342. if (mCancelValidation)
  343. return true;
  344. }
  345. if (crc.getValue() != entry.mCRC32) {
  346. Log.e(Constants.TAG, "CRC does not match for entry: " + entry.mFileName);
  347. Log.e(Constants.TAG, "In file: " + entry.getZipFileName());
  348. return false;
  349. }
  350. } finally {
  351. if (null != dis) {
  352. dis.close();
  353. }
  354. }
  355. }
  356. }
  357. } catch (IOException e) {
  358. e.printStackTrace();
  359. return false;
  360. }
  361. }
  362. return true;
  363. }
  364.  
  365. @Override
  366. protected void onProgressUpdate(DownloadProgressInfo... values) {
  367. onDownloadProgress(values[0]);
  368. super.onProgressUpdate(values);
  369. }
  370.  
  371. @Override
  372. protected void onPostExecute(Boolean result) {
  373. if (result) {
  374. mDownloadViewGroup.setVisibility(View.GONE);
  375. } else {
  376. mDownloadViewGroup.setVisibility(View.VISIBLE);
  377. }
  378. super.onPostExecute(result);
  379. }
  380.  
  381. };
  382. validationTask.execute(new Object());
  383. }
  384.  
  385. boolean expansionFilesDelivered() {
  386. for (XAPKFile xf : xAPKS) {
  387. String fileName = Helpers.getExpansionAPKFileName(this, xf.mIsMain, xf.mFileVersion);
  388. if (!Helpers.doesFileExist(this, fileName, xf.mFileSize, false))
  389. return false;
  390. }
  391. return true;
  392. }
  393.  
  394. private void setState(int newState) {
  395. if (mState != newState) {
  396. mState = newState;
  397. }
  398. }
  399.  
  400.  
  401. @Override
  402. protected void onDestroy() {
  403. this.mCancelValidation = true;
  404. super.onDestroy();
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement