Android Open Source - JiangHomeStyle_Android_Phone Version Update






From Project

Back to project page JiangHomeStyle_Android_Phone.

License

The source code is released under:

Apache License

If you think the Android project JiangHomeStyle_Android_Phone 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 com.cidesign.jianghomestylephone.version;
//  ww  w . j ava  2  s.com
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.cidesign.jianghomestylephone.MainActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;

public class VersionUpdate
{
  private static final String TAG = VersionUpdate.class.getSimpleName();

  public ProgressDialog pBar;
  private Handler handler = new Handler();

  private int newVerCode = 0;
  private String newVerName = "";

  private Context ctx;

  private Activity activity;
  private AQuery aq;

  public VersionUpdate(Context ctx, Activity activity, AQuery aq)
  {
    this.ctx = ctx;
    this.activity = activity;
    this.aq = aq;
  }

  public void getServerVerCode()
  {
    try
    {
      String url = VersionConfig.UPDATE_SERVER + VersionConfig.UPDATE_VERJSON;

      long expire = 10 * 1000;
      
      aq.ajax(url, String.class, expire, new AjaxCallback<String>()
      {
        @Override
        public void callback(String url, String html, AjaxStatus status)
        {
          if (html != null)
          {
            JSONArray array;
            try
            {
              array = new JSONArray(html);
              if (array.length() > 0)
              {
                JSONObject obj = array.getJSONObject(0);
                try
                {
                  newVerCode = Integer.parseInt(obj.getString("verCode"));
                  newVerName = obj.getString("verName");
                  int vercode = VersionConfig.getVerCode(ctx);

                  if (newVerCode > vercode)
                  {
                    doNewVersionUpdate();
                  }
                  else
                  {
                    startMainActivity();
                  }
                }
                catch (Exception e)
                {
                  newVerCode = -1;
                  newVerName = "";
                }
              }
            }
            catch (JSONException e1)
            {
              startMainActivity();
              e1.printStackTrace();
            }
          }
          else
          {
            new Handler().postDelayed(new Runnable(){
              public void run(){
                startMainActivity();
              }
            }, 2000);
            
          }
        }
      });

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

  private void notNewVersionShow()
  {
    int verCode = VersionConfig.getVerCode(ctx);
    String verName = VersionConfig.getVerName(ctx);
    StringBuffer sb = new StringBuffer();
    sb.append("?????:");
    sb.append(verName);
    sb.append(" Code:");
    sb.append(verCode);
    sb.append(",\n????????,???????!");
    Dialog dialog = new AlertDialog.Builder(ctx).setTitle("???????").setMessage(sb.toString())// ????????
        .setPositiveButton("???",// ??????????
            new DialogInterface.OnClickListener()
            {

              @Override
              public void onClick(DialogInterface dialog, int which)
              {
                dialog.dismiss();
              }

            }).create();// ????
    // ????????
    dialog.show();
  }

  private void doNewVersionUpdate()
  {
    int verCode = VersionConfig.getVerCode(ctx);
    String verName = VersionConfig.getVerName(ctx);
    StringBuffer sb = new StringBuffer();
    sb.append("?????:");
    sb.append(verName);
    sb.append(" Code:");
    sb.append(verCode);
    sb.append(", ??????:");
    sb.append(newVerName);
    sb.append(" Code:");
    sb.append(newVerCode);
    sb.append(", ???????");
    
    Dialog dialog = new AlertDialog.Builder(activity).setTitle("???????").setMessage(sb.toString())
    // ????????
        .setPositiveButton("????",// ??????????
            new DialogInterface.OnClickListener()
            {

              @Override
              public void onClick(DialogInterface dialog, int which)
              {
                pBar = new ProgressDialog(activity);
                pBar.setTitle("????????");
                pBar.setMessage("?????...");
                pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                pBar.show();
                downFile(VersionConfig.UPDATE_SERVER + VersionConfig.UPDATE_APKNAME);
              }

            }).setNegativeButton("???????", new DialogInterface.OnClickListener()
        {
          public void onClick(DialogInterface dialog, int whichButton)
          {
            startMainActivity();
            // ???"???"?????????????
            dialog.dismiss();
          }
        }).create();// ????
    // ????????
    dialog.show();
  }

  void downFile(final String url)
  {
    new Thread()
    {
      public void run()
      {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet(url);
        HttpResponse response;
        try
        {
          response = client.execute(get);
          HttpEntity entity = response.getEntity();
          long length = entity.getContentLength();
          InputStream is = entity.getContent();
          FileOutputStream fileOutputStream = null;
          if (is != null)
          {

            File file = new File(Environment.getExternalStorageDirectory(), VersionConfig.UPDATE_SAVENAME);
            fileOutputStream = new FileOutputStream(file);

            byte[] buf = new byte[1024];
            int ch = -1;
            int count = 0;
            while ((ch = is.read(buf)) != -1)
            {
              fileOutputStream.write(buf, 0, ch);
              count += ch;
              if (length > 0)
              {
              }
            }

          }
          fileOutputStream.flush();
          if (fileOutputStream != null)
          {
            fileOutputStream.close();
          }
          down();
        }
        catch (ClientProtocolException e)
        {
          e.printStackTrace();
        }
        catch (IOException e)
        {
          e.printStackTrace();
        }
      }

    }.start();

  }

  private void down()
  {
    handler.post(new Runnable()
    {
      public void run()
      {
        pBar.cancel();
        update();
      }
    });

  }

  private void update()
  {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), VersionConfig.UPDATE_SAVENAME)),
        "application/vnd.android.package-archive");
    activity.startActivity(intent);
  }

  private void startMainActivity()
  {
    activity.startActivity(new Intent(ctx, MainActivity.class));
    if (activity != null)
    {
      activity.finish();
    }
  }
}




Java Source Code List

com.cidesign.jianghomestylephone.DetailActivity.java
com.cidesign.jianghomestylephone.JiangActivity.java
com.cidesign.jianghomestylephone.MainActivity.java
com.cidesign.jianghomestylephone.SplashActivity.java
com.cidesign.jianghomestylephone.adapter.CommunityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.HumanityViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LandscapeViewpagerAdapter.java
com.cidesign.jianghomestylephone.adapter.LayoutCaculateAdapter.java
com.cidesign.jianghomestylephone.adapter.StoryViewpagerAdapter.java
com.cidesign.jianghomestylephone.async.AsyncDownTask.java
com.cidesign.jianghomestylephone.async.AsyncInitCommunityData.java
com.cidesign.jianghomestylephone.async.AsyncInitData.java
com.cidesign.jianghomestylephone.async.AsyncInitHomeData.java
com.cidesign.jianghomestylephone.async.AsyncInitHumanityData.java
com.cidesign.jianghomestylephone.async.AsyncInitLandscapeData.java
com.cidesign.jianghomestylephone.async.AsyncInitStoryData.java
com.cidesign.jianghomestylephone.db.DatabaseConfigUtil.java
com.cidesign.jianghomestylephone.db.DatabaseHelper.java
com.cidesign.jianghomestylephone.entity.ArticleEntity.java
com.cidesign.jianghomestylephone.entity.FileListEntity.java
com.cidesign.jianghomestylephone.entity.LayoutEntity.java
com.cidesign.jianghomestylephone.entity.RelativeLayoutRulesEntity.java
com.cidesign.jianghomestylephone.http.ArticalOperation.java
com.cidesign.jianghomestylephone.http.DownLoadThread.java
com.cidesign.jianghomestylephone.service.DownloadService.java
com.cidesign.jianghomestylephone.tools.CategoryDataLoadingLogic.java
com.cidesign.jianghomestylephone.tools.FileOperationTools.java
com.cidesign.jianghomestylephone.tools.JiangCategory.java
com.cidesign.jianghomestylephone.tools.LayoutMarginSetting.java
com.cidesign.jianghomestylephone.tools.LoadingDataFromDB.java
com.cidesign.jianghomestylephone.tools.LoadingImageTools.java
com.cidesign.jianghomestylephone.tools.MD5Tools.java
com.cidesign.jianghomestylephone.tools.StorageUtils.java
com.cidesign.jianghomestylephone.tools.TimeTools.java
com.cidesign.jianghomestylephone.tools.WidgetCache.java
com.cidesign.jianghomestylephone.tools.XmlParseTools.java
com.cidesign.jianghomestylephone.version.NetworkTool.java
com.cidesign.jianghomestylephone.version.VersionConfig.java
com.cidesign.jianghomestylephone.version.VersionUpdate.java
com.cidesign.jianghomestylephone.widget.CommunityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.CustomScrollView.java
com.cidesign.jianghomestylephone.widget.HScrollViewTouchLogic.java
com.cidesign.jianghomestylephone.widget.HumanityRelativeLayout.java
com.cidesign.jianghomestylephone.widget.LandscapeRelativeLayout.java
com.cidesign.jianghomestylephone.widget.PopMenu.java
com.cidesign.jianghomestylephone.widget.StoryRelativeLayout.java
org.apache.tools.zip.AbstractUnicodeExtraField.java
org.apache.tools.zip.AsiExtraField.java
org.apache.tools.zip.CentralDirectoryParsingZipExtraField.java
org.apache.tools.zip.ExtraFieldUtils.java
org.apache.tools.zip.FallbackZipEncoding.java
org.apache.tools.zip.GeneralPurposeBit.java
org.apache.tools.zip.JarMarker.java
org.apache.tools.zip.NioZipEncoding.java
org.apache.tools.zip.Simple8BitZipEncoding.java
org.apache.tools.zip.UnicodeCommentExtraField.java
org.apache.tools.zip.UnicodePathExtraField.java
org.apache.tools.zip.UnixStat.java
org.apache.tools.zip.UnparseableExtraFieldData.java
org.apache.tools.zip.UnrecognizedExtraField.java
org.apache.tools.zip.UnsupportedZipFeatureException.java
org.apache.tools.zip.Zip64ExtendedInformationExtraField.java
org.apache.tools.zip.Zip64Mode.java
org.apache.tools.zip.Zip64RequiredException.java
org.apache.tools.zip.ZipConstants.java
org.apache.tools.zip.ZipEightByteInteger.java
org.apache.tools.zip.ZipEncodingHelper.java
org.apache.tools.zip.ZipEncoding.java
org.apache.tools.zip.ZipEntry.java
org.apache.tools.zip.ZipExtraField.java
org.apache.tools.zip.ZipFile.java
org.apache.tools.zip.ZipLong.java
org.apache.tools.zip.ZipOutputStream.java
org.apache.tools.zip.ZipShort.java
org.apache.tools.zip.ZipUtil.java