package io.github.emanual.java.app;

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

import com.loopj.android.http.JsonHttpResponseHandler;
import io.github.emanual.java.app.api.JavaAPI;
import io.github.emanual.java.app.utils.AndroidUtils;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.IBinder;
import android.util.Log;

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 = intent.getAction();
		Log.d("debug", "CoreService-->action-->"+action);
		if (action.equals(Action_CheckVersion)) {
			checkVersion();
		}
		return Service.START_STICKY;
	}

	private void checkVersion() {
		JavaAPI.getVersionInfo(new JsonHttpResponseHandler() {
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				Intent intent = new Intent(Action_CheckVersion);
				try {
					
					int versionCode = response.getInt("versionCode");
					String versionName = response.getString("versionName");
					String description = response.getString("description");
					String url = response.getString("url");
					
					if (versionCode > AndroidUtils
							.getAppVersionCode(getApplicationContext())) {
						intent.putExtra("versionCode", versionCode);
						intent.putExtra("versionName", versionName);
						intent.putExtra("description", description);
						intent.putExtra("url", url);
						sendBroadcast(intent);
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (NameNotFoundException e) {
					e.printStackTrace();
				}
			}
		});
	}

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

}

@@ -712,16 +712,41 @@
 ion();%0D%0A
+%09%09if(action !=  null)%7B%0D%0A%09
 %09%09Log.d(
@@ -792,16 +792,17 @@
 on);%0D%0A%09%09
+%09
 if (acti
@@ -839,16 +839,17 @@
 ) %7B%0D%0A%09%09%09
+%09
 checkVer
@@ -857,16 +857,22 @@
 ion();%0D%0A
+%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%09r

fix #8