package com.jqmobile.core.android.time.impl;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
public class Function {
private Context mcontext;
boolean lockFalg = false;
private long gTime;
private SharedPreferences sp;
private LocationManager locationManager;
private ConnectivityManager connManager;
private static final int TYPE_CM_CU_WAP = 4;// �ƶ���ͨwap10.0.0.172
private static final int TYPE_CT_WAP = 5;// ����wap 10.0.0.200
private String[] NTPhost = new String[] { "ntp.sjtu.edu.cn",
"s1a.time.edu.cn", "s1b.time.edu.cn", "s1c.time.edu.cn",
"s1d.time.edu.cn", "s1e.time.edu.cn", "s2a.time.edu.cn",
"s2b.time.edu.cn", "s2c.time.edu.cn", "s2d.time.edu.cn",
"s2e.time.edu.cn", "s2f.time.edu.cn", "s2g.time.edu.cn",
"s2h.time.edu.cn", "s2j.time.edu.cn", "s2k.time.edu.cn",
"s2m.time.edu.cn", "pool.ntp.org" };
public Function(Context context) {
mcontext = context;
sp = mcontext.getSharedPreferences("SP", 0);
}
public long syncTime() throws Exception {
long time;
time = getNetTime();
if (time != 0) {
sp.edit().putLong("localityTime", new Date().getTime()).commit();
sp.edit().putLong("time", time).commit();
return time;
} else {
throw new Exception("���糬ʱ������ȡ����ʱ��");
}
}
public long getTime() {
return sp.getLong("time", 0);
}
public long getLocalityTime() {
return sp.getLong("localityTime",0);
}
public long getTimeScale() {
return sp.getLong("timeScale", new Date().getTime());
}
public void setTimeScale(long timeScale) {
sp.edit().putLong("timeScale", timeScale);
}
public long getNetTime() {
// ʹ��GPS
locationManager = (LocationManager) mcontext
.getSystemService(Context.LOCATION_SERVICE); // �ж�GPS�Ƿ�������
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Log.i("time_info", "����ͨ��GPS��ȡʱ��...");
long time;
time = mGPS();
if (time != 0) {
return time;
}
}
connManager = (ConnectivityManager) mcontext
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (!isNetworkConnected() && !isWifiConnected() && !isMobileConnected()) {
setNetworkMethod(mcontext);
} else {
// �ж��Dz���ʹ�õ�wap����
int type = CheckApnType.checkNetworkType(mcontext);
if (type == TYPE_CM_CU_WAP || type == TYPE_CT_WAP) {
setApnMethod(mcontext);
}
// ʹ��URL
long time;
time = mURL();
Log.i("time_info", "����ͨ��URL��ȡʱ��...");
if (time != 0) {
Log.i("time_info", "ͨ��URL�����ȡʱ��");
return time;
} else {
Log.i("time_info", "δ��ͨ��URL�����ȡʱ��");
}
// ʹ��NTP
time = mNTP();
Log.i("time_info", "����ͨ��NTP��ȡʱ��...");
if (time != 0) {
Log.i("time_info", "ͨ��NTP�����ȡʱ��");
return time;
} else {
Log.i("time_info", "δ��ͨ��NTP�����ȡʱ��");
}
}
return 0;
};
private long mGPS() {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 1, new LocationListener() {
/** * λ����Ϣ�仯ʱ���� */
public void onLocationChanged(Location location) {
gTime = location.getTime();
Log.i("time_info", "ʱ�䣺" + gTime);
return;
}
/** * GPS״̬�仯ʱ���� */
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
/** * GPS����ʱ���� */
public void onProviderEnabled(String provider) {
}
/** * GPS����ʱ���� */
public void onProviderDisabled(String provider) {
}
});
return gTime;
}
private long mNTP() {
SntpClient client = new SntpClient();
long time = 0;
for (int i = 0; i < NTPhost.length; i++) {
if (client.requestTime(NTPhost[i], 50)) {
time = client.getNtpTime();
break;
}
}
return time;
}
private long mURL() {
URL url;
long time;
try {
url = new URL("http://www.baidu.cn");// ȡ����Դ����
URLConnection uc = url.openConnection();// ������Ӷ���
uc.connect(); // ��������
uc.setConnectTimeout(1000);
uc.setReadTimeout(3000);
time = uc.getDate();
// ȡ����վ����ʱ��
return time;
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
private boolean isNetworkConnected() {
NetworkInfo networkinfo = connManager.getActiveNetworkInfo();
if (networkinfo != null) {
return networkinfo.isConnected();
}
return false;
}
private boolean isWifiConnected() {
NetworkInfo mWifi = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWifi != null) {
return mWifi.isConnected();
}
return false;
}
private boolean isMobileConnected() {
NetworkInfo mMobile = connManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobile != null) {
return mMobile.isConnected();
}
return false;
}
private void setNetworkMethod(final Context context) {
// ��ʾ�Ի���
AlertDialog.Builder builder = new Builder(context);
builder.setTitle("����������ʾ")
.setMessage("�������Ӳ�����,�Ƿ��������?")
.setPositiveButton("����", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent = null;
// �ж��ֻ�ϵͳ�İ汾 ��API����10 ����3.0�����ϰ汾
if (android.os.Build.VERSION.SDK_INT > 10) {
intent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);
} else {
intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
context.startActivity(intent);
}
})
.setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
private void setApnMethod(final Context context) {
// ��ʾ�Ի���
AlertDialog.Builder builder = new Builder(context);
builder.setTitle("APN�������þ���")
.setMessage("��ǰʹ�õ���wap����,����ʹ�ÿ��ܻ������ź���������ͬ��ʱ��,�Ƿ�����Ϊnet����?")
.setPositiveButton("����", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
// �ж��ֻ�ϵͳ�İ汾 ��API����10 ����3.0�����ϰ汾
if (android.os.Build.VERSION.SDK_INT > 10) {
Intent intent = new Intent(
android.provider.Settings.ACTION_APN_SETTINGS);
context.startActivity(intent);
} else {
Intent intent = new Intent();
ComponentName component = new ComponentName(
"com.android.settings",
"com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
context.startActivity(intent);
}
}
})
.setNegativeButton("ȡ��", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
}