package io.github.emanual.app;

import io.github.emanual.app.api.EmanualAPI;
import io.github.emanual.app.utils.AndroidUtils;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.IBinder;
import android.util.Log;

import com.loopj.android.http.JsonHttpResponseHandler;

public class CoreService extends Service {
	public static final String Action_CheckVersion = "Action_CheckVersion";

	@Override public int onStartCommand(Intent intent, int flags, int startId) {
		String action = null;
		if (intent != null) {
			action = intent.getAction();
		}
		if (action != null) {
			Log.d("debug", "CoreService-->action-->" + action);
			if (action.equals(Action_CheckVersion)) {
				checkVersion();
			}
		}
		return Service.START_NOT_STICKY;
	}

	private void checkVersion() {
		EmanualAPI.getVersionInfo(new JsonHttpResponseHandler() {
			@Override public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				Intent intent = new Intent(Action_CheckVersion);
				try {

					int version_code = response.getInt("version_code");
					String version_name = response.getString("version_name");
					String download_url = response.getString("download_url");
					String update_time = response.getString("update_time");
					
					String change_log = String.format("版本:%s  更新于%s\n", version_name,update_time);
					JSONArray change_logs = response.getJSONArray("change_log");
					for (int i = 0; i < change_logs.length(); i++) {
						JSONObject obj = change_logs.getJSONObject(i);
						change_log += String.format(" * %s\n", obj.getString("content"));
					}
					//应该交由接受端判断如何处理
					intent.putExtra("version_code", version_code);
					intent.putExtra("version_name", version_name);
					intent.putExtra("change_log", change_log);
					intent.putExtra("download_url", download_url);
					sendBroadcast(intent);
 
				} catch (JSONException e) {
					e.printStackTrace();
				} 
			}
		});
	}

	@Override public IBinder onBind(Intent intent) {
		return null;
	}

}

@@ -36,443 +36,330 @@
 ort 
-io.github.emanual.app.api.EmanualAPI;%0Aimport io.github.emanual.app.utils.AndroidUtils;%0A%0Aimport org.apache.http.Header;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport android.app.Service;%0Aimport android.content.Intent;%0Aimport android.content.pm.PackageManager.NameNotFoundException;%0Aimport android.os.IBinder;%0Aimport android.util.Log;%0A%0Aimport com.loopj.android.http.JsonHttpResponseHandler
+android.app.Service;%0Aimport android.content.Intent;%0Aimport android.os.IBinder;%0Aimport android.util.Log;%0A%0Aimport com.loopj.android.http.JsonHttpResponseHandler;%0A%0Aimport org.apache.http.Header;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0Aimport io.github.emanual.app.api.EmanualAPI
 ;%0A%0Ap
@@ -1320,24 +1320,86 @@
 ate_time%22);%0A
+                    String size = response.getString(%22size%22);%0A
 %09%09%09%09%09%0A%09%09%09%09%09S
@@ -1445,16 +1445,23 @@
   %E6%9B%B4%E6%96%B0%E4%BA%8E%25s%5C
+n%E5%A4%A7%E5%B0%8F:%25s%5C
 n%22, vers
@@ -1469,16 +1469,17 @@
 on_name,
+ 
 update_t
@@ -1481,16 +1481,22 @@
 ate_time
+, size
 );%0A%09%09%09%09%09
@@ -1967,16 +1967,67 @@
 d_url);%0A
+                    intent.putExtra(%22size%22, size);%0A
 %09%09%09%09%09sen

update info: add size field