LoaderActivity.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /******************************************************************************/
  2. package EE_PACKAGE;
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.NativeActivity;
  6. import android.app.PendingIntent;
  7. import android.content.BroadcastReceiver;
  8. import android.content.ComponentName;
  9. import android.content.DialogInterface;
  10. import android.content.DialogInterface.OnShowListener;
  11. import android.content.Context;
  12. import android.content.Intent;
  13. import android.content.IntentSender.SendIntentException;
  14. import android.content.pm.PackageManager.NameNotFoundException;
  15. import android.content.pm.ResolveInfo;
  16. import android.content.ServiceConnection;
  17. import android.content.SharedPreferences;
  18. import android.content.SharedPreferences.Editor;
  19. import android.location.Location;
  20. import android.location.LocationListener;
  21. import android.os.Bundle;
  22. import android.os.Handler;
  23. import android.os.IBinder;
  24. import android.os.Message;
  25. import android.os.Messenger;
  26. import android.os.RemoteException;
  27. import android.preference.PreferenceManager;
  28. import android.provider.Settings;
  29. import android.provider.Settings.Secure;
  30. import android.text.Editable;
  31. import android.text.InputType;
  32. import android.text.TextUtils;
  33. import android.text.TextWatcher;
  34. import android.view.Gravity;
  35. import android.view.inputmethod.InputMethodManager;
  36. import android.view.inputmethod.EditorInfo;
  37. import android.view.KeyEvent;
  38. import android.view.View;
  39. import android.view.ViewGroup;
  40. import android.view.ViewGroup.MarginLayoutParams;
  41. import android.view.Window;
  42. import android.view.WindowManager;
  43. import android.widget.Button;
  44. import android.widget.EditText;
  45. import android.widget.FrameLayout;
  46. import android.widget.LinearLayout;
  47. import android.widget.PopupWindow;
  48. import android.widget.ProgressBar;
  49. import android.widget.RelativeLayout;
  50. import android.widget.TextView;
  51. import com.android.vending.billing.IInAppBillingService;
  52. import com.google.android.gms.ads.*;
  53. import com.google.android.vending.expansion.downloader.DownloadProgressInfo;
  54. import com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;
  55. import com.google.android.vending.expansion.downloader.DownloaderServiceMarshaller;
  56. import com.google.android.vending.expansion.downloader.Helpers;
  57. import com.google.android.vending.expansion.downloader.IDownloaderClient;
  58. import com.google.android.vending.expansion.downloader.IDownloaderService;
  59. import com.google.android.vending.expansion.downloader.IStub;
  60. import com.google.android.vending.expansion.downloader.impl.DownloaderService;
  61. import java.io.IOException;
  62. import java.security.InvalidKeyException;
  63. import java.security.KeyFactory;
  64. import java.security.MessageDigest;
  65. import java.security.NoSuchAlgorithmException;
  66. import java.security.PublicKey;
  67. import java.security.Signature;
  68. import java.security.SignatureException;
  69. import java.security.spec.InvalidKeySpecException;
  70. import java.security.spec.X509EncodedKeySpec;
  71. import java.util.ArrayList;
  72. import java.util.List;
  73. /******************************************************************************/
  74. public class LoaderActivity extends Activity implements IDownloaderClient
  75. {
  76. public static class EsenthelDownloaderService extends DownloaderService // !! if changing name of this class update AndroidManifest.xml !!
  77. {
  78. public static final byte[] SALT=new byte[]{104, 43, 93, 85, 109, 101, 44, 18, 18, 19, 55, 22, 108, 29, 63, 64, 5};
  79. @Override public String getPublicKey() {return "EE_LICENSE_KEY";}
  80. @Override public byte[] getSALT() {return SALT;}
  81. @Override public String getAlarmReceiverClassName() {return EsenthelAlarmReceiver.class.getName();}
  82. }
  83. public static class EsenthelAlarmReceiver extends BroadcastReceiver // !! if changing name of this class update AndroidManifest.xml !!
  84. {
  85. @Override public void onReceive(Context context, Intent intent)
  86. {
  87. try
  88. {
  89. DownloaderClientMarshaller.startDownloadServiceIfRequired(context, intent, EsenthelDownloaderService.class);
  90. }catch(NameNotFoundException e) {e.printStackTrace();}
  91. }
  92. }
  93. ProgressBar mProgressBar;
  94. TextView mStatusText, mProgressFraction, mProgressPercent, mAverageSpeed, mTimeRemaining;
  95. View mDashboard, mCellMessage;
  96. Button mPauseButton, mWiFiSettingsButton;
  97. boolean mStatePaused;
  98. IStub mDownloaderClientStub;
  99. IDownloaderService mRemoteService;
  100. private final void setState(int newState)
  101. {
  102. mStatusText.setText(Helpers.getDownloaderStringResourceIDFromState(newState));
  103. }
  104. private final void setButtonPausedState(boolean paused)
  105. {
  106. mStatePaused=paused;
  107. mPauseButton.setText(paused ? "Resume" : "Pause");
  108. }
  109. private final void start()
  110. {
  111. Intent intent=new Intent(this, EsenthelActivity.class);
  112. startActivity(intent);
  113. finish();
  114. }
  115. @Override public void onStart()
  116. {
  117. super.onStart();
  118. if(mDownloaderClientStub!=null)mDownloaderClientStub.connect(this);
  119. }
  120. @Override public void onStop()
  121. {
  122. super.onStop();
  123. if(mDownloaderClientStub!=null)mDownloaderClientStub.disconnect(this);
  124. }
  125. @Override public void onCreate(Bundle savedInstanceState)
  126. {
  127. super.onCreate(savedInstanceState);
  128. if(!EE_DOWNLOAD_EXPANSION)start();else
  129. try
  130. {
  131. // Build PendingIntent used to open this activity from Notification
  132. Intent launchIntent=LoaderActivity.this.getIntent();
  133. Intent notifierIntent=new Intent(LoaderActivity.this, LoaderActivity.this.getClass());
  134. notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP);
  135. notifierIntent.setAction(launchIntent.getAction());
  136. if(launchIntent.getCategories()!=null)
  137. for(String category:launchIntent.getCategories())
  138. notifierIntent.addCategory(category);
  139. PendingIntent pendingIntent=PendingIntent.getActivity(LoaderActivity.this, 0, notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT);
  140. // Request to start the download
  141. int startResult =DownloaderClientMarshaller.startDownloadServiceIfRequired(this, pendingIntent, EsenthelDownloaderService.class);
  142. if( startResult==DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED)start();else
  143. {
  144. mDownloaderClientStub=DownloaderClientMarshaller.CreateStub(this, EsenthelDownloaderService.class);
  145. setContentView(R.layout.loader);
  146. mProgressBar=(ProgressBar) findViewById(R.id.progressBar);
  147. mStatusText=(TextView) findViewById(R.id.statusText);
  148. mProgressFraction=(TextView) findViewById(R.id.progressAsFraction);
  149. mProgressPercent=(TextView) findViewById(R.id.progressAsPercentage);
  150. mAverageSpeed=(TextView) findViewById(R.id.progressAverageSpeed);
  151. mTimeRemaining=(TextView) findViewById(R.id.progressTimeRemaining);
  152. mDashboard=findViewById(R.id.downloaderDashboard);
  153. mCellMessage=findViewById(R.id.approveCellular);
  154. mPauseButton=(Button) findViewById(R.id.pauseButton);
  155. mWiFiSettingsButton=(Button) findViewById(R.id.wifiSettingsButton);
  156. mPauseButton.setOnClickListener(new View.OnClickListener()
  157. {
  158. @Override public void onClick(View view)
  159. {
  160. if(mRemoteService!=null)
  161. {
  162. if(mStatePaused)mRemoteService.requestContinueDownload();
  163. else mRemoteService.requestPauseDownload ();
  164. }
  165. setButtonPausedState(!mStatePaused);
  166. }
  167. });
  168. mWiFiSettingsButton.setOnClickListener(new View.OnClickListener()
  169. {
  170. @Override public void onClick(View v) {startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));}
  171. });
  172. Button resumeOnCell=(Button) findViewById(R.id.resumeOverCellular);
  173. resumeOnCell.setOnClickListener(new View.OnClickListener()
  174. {
  175. @Override public void onClick(View view)
  176. {
  177. if(mRemoteService!=null)
  178. {
  179. mRemoteService.setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR);
  180. mRemoteService.requestContinueDownload();
  181. }
  182. mCellMessage.setVisibility(View.GONE);
  183. }
  184. });
  185. }
  186. }
  187. catch(NameNotFoundException e) {EsenthelActivity.log("Cannot find own package!"); e.printStackTrace();}
  188. }
  189. @Override public void onServiceConnected(Messenger m)
  190. {
  191. mRemoteService=DownloaderServiceMarshaller.CreateProxy(m);
  192. mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
  193. }
  194. @Override public void onDownloadStateChanged(int newState)
  195. {
  196. setState(newState);
  197. boolean showDashboard=true;
  198. boolean showCellMessage=false;
  199. boolean paused;
  200. boolean indeterminate;
  201. switch(newState)
  202. {
  203. case IDownloaderClient.STATE_IDLE: // service is listening, so it's safe to start making calls via mRemoteService
  204. paused=false;
  205. indeterminate=true;
  206. break;
  207. case IDownloaderClient.STATE_CONNECTING:
  208. case IDownloaderClient.STATE_FETCHING_URL:
  209. showDashboard=true;
  210. paused=false;
  211. indeterminate=true;
  212. break;
  213. case IDownloaderClient.STATE_DOWNLOADING:
  214. paused=false;
  215. showDashboard=true;
  216. indeterminate=false;
  217. break;
  218. case IDownloaderClient.STATE_FAILED_CANCELED:
  219. case IDownloaderClient.STATE_FAILED:
  220. case IDownloaderClient.STATE_FAILED_FETCHING_URL:
  221. case IDownloaderClient.STATE_FAILED_UNLICENSED:
  222. paused=true;
  223. showDashboard=false;
  224. indeterminate=false;
  225. break;
  226. case IDownloaderClient.STATE_PAUSED_NEED_CELLULAR_PERMISSION:
  227. case IDownloaderClient.STATE_PAUSED_WIFI_DISABLED_NEED_CELLULAR_PERMISSION:
  228. showDashboard=false;
  229. paused=true;
  230. indeterminate=false;
  231. showCellMessage=true;
  232. break;
  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. break;
  247. default:
  248. paused=true;
  249. indeterminate=true;
  250. showDashboard=true;
  251. break;
  252. }
  253. mDashboard.setVisibility(showDashboard ? View.VISIBLE : View.GONE);
  254. mCellMessage.setVisibility(showCellMessage ? View.VISIBLE : View.GONE);
  255. mProgressBar.setIndeterminate(indeterminate);
  256. setButtonPausedState(paused);
  257. if(newState==IDownloaderClient.STATE_COMPLETED)start();
  258. }
  259. @Override public void onDownloadProgress(DownloadProgressInfo progress)
  260. {
  261. mAverageSpeed.setText(getString(R.string.kilobytes_per_second, Helpers.getSpeedString(progress.mCurrentSpeed)));
  262. mTimeRemaining.setText(getString(R.string.time_remaining, Helpers.getTimeRemaining(progress.mTimeRemaining)));
  263. progress.mOverallTotal=progress.mOverallTotal;
  264. mProgressBar.setMax((int) (progress.mOverallTotal >> 8));
  265. mProgressBar.setProgress((int) (progress.mOverallProgress >> 8));
  266. mProgressPercent.setText(Long.toString(progress.mOverallProgress * 100 / progress.mOverallTotal) + "%");
  267. mProgressFraction.setText(Helpers.getDownloadProgressString(progress.mOverallProgress, progress.mOverallTotal));
  268. }
  269. }
  270. /******************************************************************************/