package com.usai.redant.raimage;

import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

import org.json.JSONObject;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class RedAntApplication extends Application
{
    private static RedAntApplication instance;  
    public static ReadWriteLock lock = new ReentrantReadWriteLock(true);  
    public static ReadWriteLock loglock = new ReentrantReadWriteLock(true);  
    public static Lock logwritelock   = loglock.writeLock();   
    public static Lock writeLock   = lock.writeLock();   
    public static String user;
    public static String password;
    public static String active_address;
    public static String station_name;
	public static String device_id;
	public static JSONObject server_info;

    public static RedAntApplication getInstance() {  
        return instance;  
    }  
	public static void startalarm()
	{

		Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
		//iAlarm.putExtra("caller", caller);
//		iAlarm.setAction("com.usai.apex.push");
		PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
				iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

		long firstime = SystemClock.elapsedRealtime();
//		UpdateLastAlermTime();
		AlarmManager am = (AlarmManager) getInstance().getSystemService(
				Context.ALARM_SERVICE);

		// 5分钟一个周期，不停的发送广播
		am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstime,
				300 * 1000, sender);
		Log.d("redant pop","start alarm");
	}

	public static void cancelalarm()
	{

		// 启动完成
		Intent iAlarm = new Intent("REDANT.POP.RETRY_UPLOAD");
//		iAlarm.setAction("com.usai.apex.push");
		PendingIntent sender = PendingIntent.getBroadcast(getInstance(), 0,
				iAlarm, PendingIntent.FLAG_UPDATE_CURRENT);

		AlarmManager am = (AlarmManager) getInstance().getSystemService(
				Context.ALARM_SERVICE);

		am.cancel(sender);
		Log.d("redant pop","cancel alarm");
	}
    @Override  
    public void onCreate() {  
        // TODO Auto-generated method stub
		Log.d("_RAIMAGE", "onCreate: RedAntApplication");
		super.onCreate();
        instance = this;



//		TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
//		String DEVICE_ID = tm.getDeviceId();
//		if(TextUtils.isEmpty(RedAntApplication.user))
//			DEVICE_ID= Settings.System.getString(getContentResolver(), Settings.System.ANDROID_ID);


		device_id=Settings.Secure.getString(getContentResolver(),Settings.Secure.ANDROID_ID);
        
		SharedPreferences pref = RedAntApplication.getInstance()
				.getSharedPreferences("RA Image", 0);

		String aa = pref.getString("aa", null);
		String name = pref.getString("station name", null);
		if(!TextUtils.isEmpty(aa))
			active_address = aa;
		if(!TextUtils.isEmpty(name))
			station_name = name;
        
        
		Intent intentservice = new Intent();
		intentservice.setClass( this, UploadService.class);
//		if (intent.getExtras() != null)
//			intentservice.putExtras(intent.getExtras());
//		intentservice.setAction(intent.getAction());
		startService(intentservice);
		




		
		
    }

	@Override
	public void onTerminate() {
		Log.d("_RAIMAGE", "onTerminate: RedAntApplication");
		super.onTerminate();
	}

	@Override
	protected void finalize() throws Throwable {

		Log.d("_RAIMAGE", "finalize: RedAntApplication");
		super.finalize();
	}
}
