diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java index 9c385881..0222fbe9 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java @@ -160,4 +160,14 @@ public class IITC_JSInterface { } }); } + + @JavascriptInterface + public void updateIitc(final String fileUrl) { + mIitc.runOnUiThread(new Runnable() { + @Override + public void run() { + mIitc.updateIitc(fileUrl); + } + }); + } } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 01f86c1b..186a3b94 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -3,6 +3,7 @@ package com.cradle.iitc_mobile; import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; +import android.app.ProgressDialog; import android.app.SearchManager; import android.content.Context; import android.content.DialogInterface; @@ -15,6 +16,7 @@ import android.location.LocationListener; import android.location.LocationManager; import android.net.Uri; import android.os.Bundle; +import android.os.Environment; import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; @@ -28,6 +30,7 @@ import android.widget.SearchView; import android.widget.Toast; import com.cradle.iitc_mobile.IITC_NavigationHelper.Pane; +import com.cradle.iitc_mobile.async.DownloadIitcUpdate; import java.io.File; import java.io.IOException; @@ -38,6 +41,7 @@ import java.util.Stack; public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeListener, LocationListener { private static final int REQUEST_LOGIN = 1; + public static final int REQUEST_UPDATE_FINISHED = 2; private IITC_WebView mIitcWebView; private final String mIntelUrl = "https://www.ingress.com/intel"; @@ -53,6 +57,7 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis private SharedPreferences mSharedPrefs; private IITC_NavigationHelper mNavigationHelper; private IITC_MapSettings mMapSettings; + private ProgressDialog mProgressDialog; // Used for custom back stack handling private final Stack mBackStack = new Stack(); @@ -106,6 +111,13 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis // Clear the back stack mBackStack.clear(); + // init update progress dialog + mProgressDialog = new ProgressDialog(this); + mProgressDialog.setMessage("Downloading IITCm update..."); + mProgressDialog.setIndeterminate(true); + mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); + mProgressDialog.setCancelable(true); + handleIntent(getIntent(), true); } @@ -570,6 +582,10 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis // authentication activity has returned. mLogin will continue authentication mLogin.onActivityResult(resultCode, data); break; + case REQUEST_UPDATE_FINISHED: + // clean up update apk + File file = new File(Environment.getExternalStorageDirectory().toString() + "/iitc_update.apk"); + if (file != null) file.delete(); default: super.onActivityResult(requestCode, resultCode, data); @@ -626,6 +642,17 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis } } + public void updateIitc(String url) { + final DownloadIitcUpdate updateTask = new DownloadIitcUpdate(this); + updateTask.execute(url); + mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + updateTask.cancel(true); + } + }); + } + /** * @see getNavigationHelper() * @deprecated ActionBar related stuff should be handled by IITC_NavigationHelper @@ -643,4 +670,8 @@ public class IITC_Mobile extends Activity implements OnSharedPreferenceChangeLis public IITC_MapSettings getMapSettings() { return mMapSettings; } + + public ProgressDialog getProgressDialog() { + return mProgressDialog; + } } diff --git a/mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java b/mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java new file mode 100644 index 00000000..f2f64fb6 --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/async/DownloadIitcUpdate.java @@ -0,0 +1,99 @@ +package com.cradle.iitc_mobile.async; + +import android.app.ProgressDialog; +import android.content.Intent; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Environment; +import android.util.Log; + +import com.cradle.iitc_mobile.IITC_Mobile; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URL; +import java.net.URLConnection; + +/** + * Background Async Task to download file + * */ +public class DownloadIitcUpdate extends AsyncTask { + + private final IITC_Mobile mIitc; + private final ProgressDialog mProgressDialog; + + public DownloadIitcUpdate(IITC_Mobile iitcm) { + mIitc = iitcm; + mProgressDialog = iitcm.getProgressDialog(); + } + + @Override + protected void onPreExecute() { + super.onPreExecute(); + mProgressDialog.show(); + } + + @Override + protected String doInBackground(String... fileUrl) { + int count; + try { + URL url = new URL(fileUrl[0]); + URLConnection connection = url.openConnection(); + connection.connect(); + int lengthOfFile = connection.getContentLength(); + + // input stream to read file - with 8k buffer + InputStream input = new BufferedInputStream(url.openStream(), 8192); + + // Output stream to write file + OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + + "/iitc_update.apk"); + + byte data[] = new byte[8192]; + + long total = 0; + + while ((count = input.read(data)) != -1) { + total += count; + // publishing the progress.... + publishProgress((int)(total*100)/lengthOfFile); + + // writing data to file + output.write(data, 0, count); + } + + output.flush(); + + // closing streams + output.close(); + input.close(); + + } catch (Exception e) { + Log.e("iitcm:", e.getMessage()); + } + + return null; + } + + protected void onProgressUpdate(Integer... progress) { + super.onProgressUpdate(progress); + // if we get here, length is known, now set indeterminate to false + mProgressDialog.setIndeterminate(false); + mProgressDialog.setMax(100); + mProgressDialog.setProgress(progress[0]); + } + + @Override + protected void onPostExecute(String fileUrl) { + // dismiss the dialog after the file was downloaded + mProgressDialog.dismiss(); + + String iitcPath = Environment.getExternalStorageDirectory().toString() + "/iitc_update.apk"; + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setDataAndType(Uri.fromFile(new File(iitcPath)), "application/vnd.android.package-archive"); + mIitc.startActivityForResult(intent, mIitc.REQUEST_UPDATE_FINISHED); + } +} \ No newline at end of file