package com.chinaece.gaia.service;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import android.app.Service;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.IBinder;
import android.widget.RemoteViews;
import com.chinaece.gaia.R;
import com.chinaece.gaia.constant.Gaia;
import com.chinaece.gaia.db.DataStorage;
import com.chinaece.gaia.gui.PendingsActivity;
import com.chinaece.gaia.gui.quick.WidgetProvider;
import com.chinaece.gaia.http.OAHttpApi;
import com.chinaece.gaia.types.AppType;
import com.chinaece.gaia.types.PendingType;
import com.chinaece.gaia.util.NotificationCenter;
public class PendingService extends Service {
public static final String EXTRA_UPDATE_RATE = "update-rate";
private String tip = "待办事项提醒";
private String title = "通知";
private String token;
private URL formatUrl;
private String content = "有新待办事项";
private Collection<PendingType> Lastpendinglist;
private String strAppids;
public static int listsize;
private Bitmap icon;
public Bitmap getIcon() {
return icon;
}
public int getListsize() {
return listsize;
}
public int onStartCommand(final Intent intent, int flags, final int startId) {
new Thread(null, new Runnable() {
@Override
public void run() {
doServiceStart(intent, startId);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}, "Background").start();
return Service.START_REDELIVER_INTENT;
}
public void doServiceStart(Intent intent, int startId) {
while (true) {
doRequestService();
try {
Thread.sleep(6000 * 20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void doRequestService() {
try {
DataStorage.load(getApplicationContext());
if (DataStorage.properties.size() <= 1) {
stopService(new Intent(PendingService.this,
PendingService.class));
return;
} else {
token = DataStorage.properties.getProperty("token");
formatUrl = new URL(DataStorage.properties.getProperty("url"));
}
OAHttpApi OaApi = new OAHttpApi(formatUrl.toString());
if (strAppids == null && Gaia.APPLIST == null
&& !OaApi.getApps(token)) {
stopService(new Intent(PendingService.this,
PendingService.class));
return;
} else if (strAppids == null) {
JSONArray appids = new JSONArray();
for (AppType app : Gaia.APPLIST) {
appids.put(app.getAppid());
}
strAppids = appids.toString();
}
//
// Collection<PendingType> pendinglist = OaApi.getPending(
// token.toString(), strAppids);
Collection<PendingType> pendinglists = new ArrayList<PendingType>();
try {
JSONArray jsa = new JSONArray(strAppids);
for(int i = 0;i<jsa.length();i++){
JSONArray appids = new JSONArray();
appids.put(jsa.get(i));
OAHttpApi OaApi1 = new OAHttpApi(formatUrl.toString());
Collection<PendingType> pendinglist = OaApi1.getPending(token,
appids.toString());
if(pendinglist != null){
pendinglists.addAll(pendinglist);
}
else{
try{
NotificationCenter.clearNotification(65536);
}catch (Exception e) {
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
if (pendinglists != null&& PendingService.this.Lastpendinglist != null) {
ArrayList arrayList=new ArrayList();
for (int i = 0; i < PendingService.this.Lastpendinglist.size(); i++) {
arrayList.add(((ArrayList<PendingType>) PendingService.this.Lastpendinglist).get(i).getDocid());
}
for (int j = 0; j < pendinglists.size(); j++) {
String str2 = ((ArrayList<PendingType>) pendinglists).get(j).getDocid();
int judge =0;
for (int k = 0; k < arrayList.size(); k++) {
if(str2.equals(arrayList.get(k))){
judge=1;
}
}
if(judge==0){
Intent mintent = new Intent(getApplicationContext(),PendingsActivity.class);
NotificationCenter.sendPendingsNotification(mintent,getApplicationContext(), tip, title, content);
}
}
}
PendingService.this.Lastpendinglist = pendinglists;
listsize=pendinglists.size();
} catch (MalformedURLException e) {
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}