Android Open Source - egotrip Beta Update Manager






From Project

Back to project page egotrip.

License

The source code is released under:

Apache License

If you think the Android project egotrip listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.myegotrip.egotrip.net;
/*from ww  w. jav  a  2  s  .co m*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;

import net.myegotrip.egotrip.ControlWindow;
import net.myegotrip.egotrip.DownloadProgressHandler;
import net.myegotrip.egotrip.ReleaseConfig;
import net.myegotrip.egotrip.StartupActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Message;
import android.util.Log;

public class BetaUpdateManager {

  private static String TAG = "EGOTRIP-UpdateManager";

  private final File LOCALFILE = new File(Environment.getExternalStorageDirectory(), "egotrip.apk");

  private StartupActivity act;
  public BetaUpdateManager(StartupActivity act) {
    this.act = act;
  }
  private boolean downloadUpdate() {
    try {
      Log.d(TAG, "Update download started");
      Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(ReleaseConfig.UPDATEUSERNAME, ReleaseConfig.UPDATEPASSWORD.toCharArray());
        }
      });

      URL url = new URL(ReleaseConfig.UPDATEURL);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);
      urlConnection.connect();
      FileOutputStream fileOutput = new FileOutputStream(LOCALFILE);
      InputStream inputStream = urlConnection.getInputStream();
      int totalSize = urlConnection.getContentLength();
      int downloadedSize = 0;
      Authenticator.setDefault(null);
      byte[] buffer = new byte[1024];
      int bufferLength = 0;
      while ((bufferLength = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bufferLength);
        downloadedSize += bufferLength;
        // this is where you would do something to report the
        // prgress,
        // like this maybe
        // updateProgress(downloadedSize, totalSize);
        Message m = new Message();
        m.what = DownloadProgressHandler.HANDLER_DOWNLOADUPDATEPROGRESS;
        m.arg1 = downloadedSize;
        m.arg2 = totalSize;
        Log.d(TAG, "Downloaded " + downloadedSize + " of " + totalSize);
        act.getHandler().sendMessage(m);

      }
      fileOutput.close();
      Log.d(TAG, "Update download complete");
      return true;

      // catch some possible errors...
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return false;
  }

  public boolean updateAvailable() {
    if(!ReleaseConfig.ENABLE_UNSIGNED_AUTOUPDATE){
      return false;
    }
    
    try {
      int curVersion = act.getPackageManager().getPackageInfo(act.getApplicationContext().getPackageName(), 0).versionCode;
      Log.d(TAG, "Current Application Version is : " + curVersion);

      Authenticator.setDefault(new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
          return new PasswordAuthentication(ReleaseConfig.UPDATEUSERNAME, ReleaseConfig.UPDATEPASSWORD.toCharArray());
        }
      });

      URL url = new URL(ReleaseConfig.UPDATEVERSIONURL);
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      urlConnection.setRequestMethod("GET");
      urlConnection.setDoOutput(true);
      urlConnection.connect();

      ByteArrayOutputStream out = new ByteArrayOutputStream();

      InputStream inputStream = urlConnection.getInputStream();
      ;
      Authenticator.setDefault(null);
      byte[] buffer = new byte[1024];
      int bufferLength = 0;
      while ((bufferLength = inputStream.read(buffer)) > 0) {
        out.write(buffer, 0, bufferLength);

      }
      out.close();

      String onlineVersionString = new String(out.toByteArray()).trim();
      int onlineVersion = Integer.parseInt(onlineVersionString);
      Log.d(TAG, "Online Application Version is : " + onlineVersion);
      if (onlineVersion > curVersion) {
        return true;
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return false;
  }

  public boolean startUpdateInstallation() {
    if (downloadUpdate()) {
      installUpdate();
      return true;
    } else {

      Log.d(TAG, "Update download problem");
      return false;
    }
  }

  private void installUpdate() {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(LOCALFILE), "application/vnd.android.package-archive");
    act.startActivity(intent);

  }
}




Java Source Code List

net.myegotrip.egotrip.CommonGPSServiceFunctions.java
net.myegotrip.egotrip.ControlHandler.java
net.myegotrip.egotrip.ControlWindow.java
net.myegotrip.egotrip.DbListener.java
net.myegotrip.egotrip.DbTools.java
net.myegotrip.egotrip.DownloadProgressHandler.java
net.myegotrip.egotrip.FallbackDefaults.java
net.myegotrip.egotrip.GPSService.java
net.myegotrip.egotrip.Installation.java
net.myegotrip.egotrip.LocationUpdate.java
net.myegotrip.egotrip.MapViewActivity.java
net.myegotrip.egotrip.PrefActivity.java
net.myegotrip.egotrip.ReleaseConfig.java
net.myegotrip.egotrip.StartupActivity.java
net.myegotrip.egotrip.TaskDoneListener.java
net.myegotrip.egotrip.Tools.java
net.myegotrip.egotrip.TripManager.java
net.myegotrip.egotrip.help.HelpActivity.java
net.myegotrip.egotrip.help.TopicActivity.java
net.myegotrip.egotrip.image.ImageHandler.java
net.myegotrip.egotrip.map.MockLocationProvider.java
net.myegotrip.egotrip.map.PlacemarkOverlay.java
net.myegotrip.egotrip.map.Placemark.java
net.myegotrip.egotrip.map.RouteOverlay.java
net.myegotrip.egotrip.map.RoutePoint.java
net.myegotrip.egotrip.map.Trip.java
net.myegotrip.egotrip.metadata.EgotripMetadata.java
net.myegotrip.egotrip.metadata.GenericMetadata.java
net.myegotrip.egotrip.metadata.Icon.java
net.myegotrip.egotrip.metadata.Image.java
net.myegotrip.egotrip.metadata.MetadataManager.java
net.myegotrip.egotrip.metadata.Text.java
net.myegotrip.egotrip.net.BetaUpdateManager.java
net.myegotrip.egotrip.net.ProtocolConstants.java
net.myegotrip.egotrip.net.ServerReply.java
net.myegotrip.egotrip.net.Uploader.java
net.myegotrip.egotrip.profile.ProfileActivity.java
net.myegotrip.egotrip.profile.ProfilePrefActivity.java
net.myegotrip.egotrip.profile.ProfileView.java
net.myegotrip.egotrip.utils.DebugActivity.java
net.myegotrip.egotrip.utils.Debug.java
net.myegotrip.egotrip.utils.GuiUtils.java
net.myegotrip.egotrip.utils.IconItem.java
net.myegotrip.egotrip.utils.TwoDScrollView.java
net.myegotrip.egotrip.utils.XYScaleGestureDetector.java