diff --git a/app/release/app-release.apk b/app/release/app-release.apk index c360e35..c01a2b4 100644 Binary files a/app/release/app-release.apk and b/app/release/app-release.apk differ diff --git a/app/src/main/java/com/zcshou/gogogo/MainActivity.java b/app/src/main/java/com/zcshou/gogogo/MainActivity.java index 260de17..b2d3f72 100644 --- a/app/src/main/java/com/zcshou/gogogo/MainActivity.java +++ b/app/src/main/java/com/zcshou/gogogo/MainActivity.java @@ -21,6 +21,7 @@ import android.net.NetworkInfo; import android.net.Uri; import android.os.Build; import android.os.Bundle; +import android.os.SystemClock; import android.provider.Settings; import android.util.Log; import android.view.Gravity; @@ -92,7 +93,6 @@ import org.json.JSONException; import org.json.JSONObject; import java.math.BigDecimal; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; @@ -100,7 +100,8 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.TimeZone; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import mapapi.overlayutil.PoiOverlay; @@ -108,6 +109,7 @@ import com.zcshou.log4j.LogUtil; import com.zcshou.service.GoGoGoService; import com.zcshou.database.HistoryLocationDataBaseHelper; import com.zcshou.database.HistorySearchDataBaseHelper; +import com.zcshou.service.GoSntpClient; import com.zcshou.utils.MapUtils; import static com.zcshou.gogogo.R.drawable; @@ -182,6 +184,8 @@ public class MainActivity extends AppCompatActivity private boolean isSubmit; private SuggestionSearch mSuggestionSearch; + Date mDate; + //log debug private static final Logger log = Logger.getLogger(MainActivity.class); @@ -300,20 +304,11 @@ public class MainActivity extends AppCompatActivity setSearchSuggestListener(); setUserLimitInfo(); - } - private long getLocalTimeStamp() throws ParseException { - SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + TimeTask timeTask = new TimeTask(); - dff.setTimeZone(TimeZone.getTimeZone("GMT+08")); - - Date date = dff.parse(dff.format(new Date())); - - if (date != null) { - return date.getTime() / 1000; - } else { - return mTS; - } + ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); + threadExecutor.submit(timeTask); } @SuppressLint("InflateParams") @@ -1068,69 +1063,66 @@ public class MainActivity extends AppCompatActivity faBtnStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - try { - if (getLocalTimeStamp() < mTS) { // 时间限制 - //悬浮窗权限判断 - if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(getApplicationContext())) { - showEnableFloatWindowDialog(); + if (mDate != null && mDate.getTime()/1000 < mTS) { // 时间限制 + //悬浮窗权限判断 + if (Build.VERSION.SDK_INT >= 23 && !Settings.canDrawOverlays(getApplicationContext())) { + showEnableFloatWindowDialog(); + } else { + isGPSOpen = isGpsOpened(); + if (!isGPSOpen) { + showEnableGpsDialog(); } else { - isGPSOpen = isGpsOpened(); - if (!isGPSOpen) { - showEnableGpsDialog(); + //gps是否开启 + if (!isMapLoc) { + // 如果GPS定位开启,则打开定位图层 + openMapLocateLayer(); + isMapLoc = true; + } + + if (!isAllowMockLocation()) { + showEnableMockLocationDialog(); } else { - //gps是否开启 - if (!isMapLoc) { - // 如果GPS定位开启,则打开定位图层 - openMapLocateLayer(); - isMapLoc = true; - } + if (!isMockServStart && !isServiceRun) { + Log.d("DEBUG", "Current Baidu LatLng: " + curMapLatLng.longitude + " " + curMapLatLng.latitude); + log.debug("Current Baidu LatLng: " + curMapLatLng.longitude + " " + curMapLatLng.latitude); - if (!isAllowMockLocation()) { - showEnableMockLocationDialog(); - } else { - if (!isMockServStart && !isServiceRun) { - Log.d("DEBUG", "Current Baidu LatLng: " + curMapLatLng.longitude + " " + curMapLatLng.latitude); - log.debug("Current Baidu LatLng: " + curMapLatLng.longitude + " " + curMapLatLng.latitude); + markSelectedPosition(); - markSelectedPosition(); + //start mock location service + Intent mockLocServiceIntent = new Intent(MainActivity.this, GoGoGoService.class); + mockLocServiceIntent.putExtra("CurLatLng", curLatLng); + mockLocServiceIntent.putExtra("DT", mDate.getTime() / 1000); - //start mock location service - Intent mockLocServiceIntent = new Intent(MainActivity.this, GoGoGoService.class); - mockLocServiceIntent.putExtra("CurLatLng", curLatLng); + //save record + recordGetPositionInfo(); - //save record - recordGetPositionInfo(); - - //insert end - if (Build.VERSION.SDK_INT >= 26) { - startForegroundService(mockLocServiceIntent); - Log.d("DEBUG", "startForegroundService: GoGoGoService"); - log.debug("startForegroundService: GoGoGoService"); - } else { - startService(mockLocServiceIntent); - Log.d("DEBUG", "startService: GoGoGoService"); - log.debug("startService: GoGoGoService"); - } - - isMockServStart = true; - Snackbar.make(view, "位置模拟已开启", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - faBtnStart.hide(); - faBtnStop.show(); - //track + //insert end + if (Build.VERSION.SDK_INT >= 26) { + startForegroundService(mockLocServiceIntent); + Log.d("DEBUG", "startForegroundService: GoGoGoService"); + log.debug("startForegroundService: GoGoGoService"); } else { - Snackbar.make(view, "位置模拟已在运行", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - faBtnStart.hide(); - faBtnStop.show(); - isMockServStart = true; + startService(mockLocServiceIntent); + Log.d("DEBUG", "startService: GoGoGoService"); + log.debug("startService: GoGoGoService"); } + + isMockServStart = true; + Snackbar.make(view, "位置模拟已开启", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + faBtnStart.hide(); + faBtnStop.show(); + //track + } else { + Snackbar.make(view, "位置模拟已在运行", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + faBtnStart.hide(); + faBtnStop.show(); + isMockServStart = true; } } } } - } catch (ParseException e) { - e.printStackTrace(); } } }); @@ -1809,6 +1801,27 @@ public class MainActivity extends AppCompatActivity } } + private class TimeTask implements Runnable { + private String[] ntpServerPool = {"ntp1.aliyun.com", "ntp2.aliyun.com", "ntp3.aliyun.com", "ntp4.aliyun.com", "ntp5.aliyun.com", "ntp6.aliyun.com", "ntp7.aliyun.com", + "cn.pool.ntp.org", "cn.ntp.org.cn", "sg.pool.ntp.org", "tw.pool.ntp.org", "jp.pool.ntp.org", "hk.pool.ntp.org", "th.pool.ntp.org", + "time.windows.com", "time.nist.gov", "time.apple.com", "time.asia.apple.com", + "dns1.synet.edu.cn", "news.neu.edu.cn", "dns.sjtu.edu.cn", "dns2.synet.edu.cn", "ntp.glnet.edu.cn", "s2g.time.edu.cn", + "ntp-sz.chl.la", "ntp.gwadar.cn", "3.asia.pool.ntp.org"}; + + @Override + public void run() { + GoSntpClient GoSntpClient = new GoSntpClient(); + for (String serverHost : ntpServerPool) { + if (GoSntpClient.requestTime(serverHost, 30000)) { + long now = GoSntpClient.getNtpTime() + SystemClock.elapsedRealtime() - GoSntpClient.getNtpTimeReference(); + mDate = new Date(now); + Log.d("MainActivity", mDate.toString()); + break; + } + } + } + } + public void DisplayToast(String str) { Toast toast = Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG); toast.setGravity(Gravity.TOP, 0, 220); diff --git a/app/src/main/java/com/zcshou/gogogo/WelcomeActivity.java b/app/src/main/java/com/zcshou/gogogo/WelcomeActivity.java index fea449a..d654885 100644 --- a/app/src/main/java/com/zcshou/gogogo/WelcomeActivity.java +++ b/app/src/main/java/com/zcshou/gogogo/WelcomeActivity.java @@ -15,17 +15,17 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.preference.PreferenceManager; -import android.view.Gravity; +import android.os.SystemClock; +import android.util.Log; import android.view.View; import android.widget.Button; -import android.widget.Toast; -import java.text.ParseException; -import java.text.SimpleDateFormat; +import com.zcshou.service.GoSntpClient; + import java.util.ArrayList; -import java.util.Date; import java.util.Locale; -import java.util.TimeZone; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; public class WelcomeActivity extends AppCompatActivity { private Button startBtn; @@ -33,6 +33,7 @@ public class WelcomeActivity extends AppCompatActivity { private static final long mTS = 1630972800; int cnt; boolean isPermission; + boolean isLimit; static final int SDK_PERMISSION_REQUEST = 127; ArrayList ReqPermissions = new ArrayList<>(); @@ -52,43 +53,30 @@ public class WelcomeActivity extends AppCompatActivity { PreferenceManager.setDefaultValues(this, R.xml.preferences_main, false); isPermission = false; + isLimit = true; - try { - if (getLocalTimeStamp() < mTS) { - cnt = Integer.parseInt(getResources().getString (R.string.welcome_btn_cnt)); - time = new TimeCount(cnt, 1000); - startBtn = findViewById(R.id.startButton); - //startBtn.setClickable(false); - startBtn.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - time.cancel(); - startMainActivity(); - } - }); - - requestNeedPermissions(); - } else { - WelcomeActivity.this.finish(); + cnt = Integer.parseInt(getResources().getString (R.string.welcome_btn_cnt)); + time = new TimeCount(cnt, 1000); + startBtn = findViewById(R.id.startButton); + startBtn.setClickable(false); + startBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + time.cancel(); + startMainActivity(); } - } catch (ParseException e) { - e.printStackTrace(); - WelcomeActivity.this.finish(); - } - } + }); - private long getLocalTimeStamp() throws ParseException { - SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); + if (isNetworkAvailable()) { + TimeTask timeTask = new TimeTask(); - dff.setTimeZone(TimeZone.getTimeZone("GMT+08")); - - Date date = dff.parse(dff.format(new Date())); - - if (date != null) { - return date.getTime() / 1000; + ExecutorService threadExecutor = Executors.newSingleThreadExecutor(); + threadExecutor.submit(timeTask); } else { - return mTS; + startBtn.setText("网络不可用"); } + + requestNeedPermissions(); } //WIFI是否可用 @@ -135,7 +123,7 @@ public class WelcomeActivity extends AppCompatActivity { } private void startMainActivity() { - if (isPermission) { + if (isPermission && isLimit) { Intent intent = new Intent(WelcomeActivity.this, MainActivity.class); startActivity(intent); WelcomeActivity.this.finish(); @@ -180,43 +168,13 @@ public class WelcomeActivity extends AppCompatActivity { if (ReqPermissions.size() > 0) { requestPermissions(ReqPermissions.toArray(new String[0]), SDK_PERMISSION_REQUEST); } else { - //网络是否可用 - if (isNetworkAvailable()) { - time.start(); - isPermission = true; - } else { - startBtn.setClickable(false); - startBtn.setText("网络不可用"); - DisplayToast("网络连接不可用,请检查网络连接设置"); - } + isPermission = true; } } else { - //网络是否可用 - if (isNetworkAvailable()) { - time.start(); - isPermission = true; - } else { - startBtn.setClickable(false); - startBtn.setText("网络不可用"); - DisplayToast("网络连接不可用,请检查网络连接设置"); - } + isPermission = true; } } -// @TargetApi(23) -// private boolean addPermission(ArrayList permissionsList, String permission) { -// if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请 -// if (shouldShowRequestPermissionRationale(permission)) { -// return true; -// } else { -// permissionsList.add(permission); -// return false; -// } -// } else { -// return true; -// } -// } - @TargetApi(23) @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { @@ -229,17 +187,8 @@ public class WelcomeActivity extends AppCompatActivity { } if (i >= ReqPermissions.size()) { - //网络是否可用 - if (isNetworkAvailable()) { - time.start(); - isPermission = true; - } else { - startBtn.setClickable(false); - startBtn.setText("网络不可用"); - DisplayToast("网络连接不可用,请检查网络连接设置"); - } + isPermission = true; } else { - startBtn.setClickable(false); isPermission = false; startBtn.setText("权限不足"); } @@ -265,10 +214,30 @@ public class WelcomeActivity extends AppCompatActivity { } } - public void DisplayToast(String str) { - Toast toast = Toast.makeText(WelcomeActivity.this, str, Toast.LENGTH_LONG); - toast.setGravity(Gravity.TOP, 0, 220); - toast.show(); + private class TimeTask implements Runnable { + private String[] ntpServerPool = {"ntp1.aliyun.com", "ntp2.aliyun.com", "ntp3.aliyun.com", "ntp4.aliyun.com", "ntp5.aliyun.com", "ntp6.aliyun.com", "ntp7.aliyun.com", + "cn.pool.ntp.org", "cn.ntp.org.cn", "sg.pool.ntp.org", "tw.pool.ntp.org", "jp.pool.ntp.org", "hk.pool.ntp.org", "th.pool.ntp.org", + "time.windows.com", "time.nist.gov", "time.apple.com", "time.asia.apple.com", + "dns1.synet.edu.cn", "news.neu.edu.cn", "dns.sjtu.edu.cn", "dns2.synet.edu.cn", "ntp.glnet.edu.cn", "s2g.time.edu.cn", + "ntp-sz.chl.la", "ntp.gwadar.cn", "3.asia.pool.ntp.org"}; + + @Override + public void run() { + GoSntpClient GoSntpClient = new GoSntpClient(); + int i; + for (i = 0; i < ntpServerPool.length; i++) { + if (GoSntpClient.requestTime(ntpServerPool[i], 30000)) { + long now = GoSntpClient.getNtpTime() + SystemClock.elapsedRealtime() - GoSntpClient.getNtpTimeReference(); + if (now /1000 < mTS) { + isLimit = true; + } + break; + } + } + if (i < ntpServerPool.length) { + time.start(); + } + } } } diff --git a/app/src/main/java/com/zcshou/service/GoGoGoService.java b/app/src/main/java/com/zcshou/service/GoGoGoService.java index b7cccb6..3619a9b 100644 --- a/app/src/main/java/com/zcshou/service/GoGoGoService.java +++ b/app/src/main/java/com/zcshou/service/GoGoGoService.java @@ -33,11 +33,6 @@ import com.zcshou.gogogo.R; import org.apache.log4j.Logger; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -import java.util.TimeZone; import java.util.UUID; public class GoGoGoService extends Service { @@ -50,7 +45,7 @@ public class GoGoGoService extends Service { private boolean isStop = true; // 是否启动了模拟位置 private String curLatLng = "117.027707&36.667662";// 模拟位置的经纬度字符串 private static final long mTS = 1630972800; - private long mDT = 0; + private long mDT = 1630972800; // 摇杆相关 private JoyStick mJoyStick; @@ -131,95 +126,89 @@ public class GoGoGoService extends Service { Log.d("GoGoGoService", "onStartCommand"); log.debug("onStartCommand"); - try { - mDT = getLocalTimeStamp(); - if (mDT < mTS) { - String channelId = "channel_01"; - String name = "channel_name"; - NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - Notification notification; + String channelId = "channel_01"; + String name = "channel_name"; + NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + Notification notification; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel mChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_LOW); - Log.i("GoGoGoService", mChannel.toString()); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel mChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_LOW); + Log.i("GoGoGoService", mChannel.toString()); - if (notificationManager != null) { - notificationManager.createNotificationChannel(mChannel); - } - - notification = new NotificationCompat.Builder(this, channelId) - .setChannelId(channelId) - .setContentTitle("GoGoGo") - .setContentText("GoGoGo service is running") - .setSmallIcon(R.mipmap.ic_launcher).build(); - } else { - NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "M_CH_ID") - .setContentTitle("GoGoGo") - .setContentText("GoGoGo service is running") - .setSmallIcon(R.mipmap.ic_launcher) - .setOngoing(true) - .setChannelId(channelId);//无效 - notification = notificationBuilder.build(); - } - - startForeground(1, notification); - - // get location info from mainActivity - curLatLng = intent.getStringExtra("CurLatLng"); - - Log.d("GoGoGoService", "LatLng from Main is " + curLatLng); - log.debug("LatLng from Main is " + curLatLng); - - //start to refresh location - isStop = false; - - // 开启摇杆 - if (!isJoyStick) { - mJoyStick = new JoyStick(this); - mJoyStick.setListener(new JoyStick.JoyStickClickListener() { - @Override - public void clickAngleInfo(double angle, double speed) { - mSpeed = speed * 3.6; // 转换为 km/h, 1米/秒(m/s)=3.6千米/时(km/h) - double lng; - double lat; - if (mDT < mTS) { - // 注意:这里的 x y 与 圆中角度的对应问题(以 X 轴正向为 0 度) - double x = Math.cos(angle * 2 * Math.PI / 360); // 注意安卓使用的是弧度 - double y = Math.sin(angle * 2 * Math.PI / 360); // 注意安卓使用的是弧度 - - // 根据当前的经纬度和距离,计算下一个经纬度 - // Latitude: 1 deg = 110.574 km // 纬度的每度的距离大约为 110.574km - // Longitude: 1 deg = 111.320*cos(latitude) km // 经度的每度的距离从0km到111km不等 - // 具体见:http://wp.mlab.tw/?p=2200 - - String[] latLngStr = curLatLng.split("&"); - - double lngDegree = mSpeed * x / (111.320 * Math.cos(Math.abs(Double.parseDouble(latLngStr[1])) * Math.PI / 180)); - double latDegree = mSpeed * y / 110.574; - - lng = Double.parseDouble(latLngStr[0]) + lngDegree / 1000; // 为啥 / 1000 ? 按照速度算下来,这里偏大 - lat = Double.parseDouble(latLngStr[1]) + latDegree / 1000; // 为啥 / 1000 ? 按照速度算下来,这里偏大 - } else { - lng = Math.random() * (180 - 1 + 1); - lat = Math.random() * (90 - 1 + 1); - } - curLatLng = lng + "&" + lat; - } - - @Override - public void setCurrentSpeed(double speed) { - mSpeed = speed * 3.6; // 转换为 km/h, 1米/秒(m/s)=3.6千米/时(km/h) - } - - }); - - mJoyStick.show(); - - isJoyStick = true; - } + if (notificationManager != null) { + notificationManager.createNotificationChannel(mChannel); } - } catch (ParseException e) { - e.printStackTrace(); + + notification = new NotificationCompat.Builder(this, channelId) + .setChannelId(channelId) + .setContentTitle("GoGoGo") + .setContentText("GoGoGo service is running") + .setSmallIcon(R.mipmap.ic_launcher).build(); + } else { + NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "M_CH_ID") + .setContentTitle("GoGoGo") + .setContentText("GoGoGo service is running") + .setSmallIcon(R.mipmap.ic_launcher) + .setOngoing(true) + .setChannelId(channelId);//无效 + notification = notificationBuilder.build(); + } + + startForeground(1, notification); + + // get location info from mainActivity + curLatLng = intent.getStringExtra("CurLatLng"); + mDT = intent.getLongExtra("DT", mDT); + + Log.d("GoGoGoService", "LatLng from Main is " + curLatLng); + log.debug("LatLng from Main is " + curLatLng); + + //start to refresh location + isStop = false; + + // 开启摇杆 + if (!isJoyStick) { + mJoyStick = new JoyStick(this); + mJoyStick.setListener(new JoyStick.JoyStickClickListener() { + @Override + public void clickAngleInfo(double angle, double speed) { + mSpeed = speed * 3.6; // 转换为 km/h, 1米/秒(m/s)=3.6千米/时(km/h) + double lng; + double lat; + if (mDT < mTS) { + // 注意:这里的 x y 与 圆中角度的对应问题(以 X 轴正向为 0 度) + double x = Math.cos(angle * 2 * Math.PI / 360); // 注意安卓使用的是弧度 + double y = Math.sin(angle * 2 * Math.PI / 360); // 注意安卓使用的是弧度 + + // 根据当前的经纬度和距离,计算下一个经纬度 + // Latitude: 1 deg = 110.574 km // 纬度的每度的距离大约为 110.574km + // Longitude: 1 deg = 111.320*cos(latitude) km // 经度的每度的距离从0km到111km不等 + // 具体见:http://wp.mlab.tw/?p=2200 + + String[] latLngStr = curLatLng.split("&"); + + double lngDegree = mSpeed * x / (111.320 * Math.cos(Math.abs(Double.parseDouble(latLngStr[1])) * Math.PI / 180)); + double latDegree = mSpeed * y / 110.574; + + lng = Double.parseDouble(latLngStr[0]) + lngDegree / 1000; // 为啥 / 1000 ? 按照速度算下来,这里偏大 + lat = Double.parseDouble(latLngStr[1]) + latDegree / 1000; // 为啥 / 1000 ? 按照速度算下来,这里偏大 + } else { + lng = Math.random() * (180 - 1 + 1); + lat = Math.random() * (90 - 1 + 1); + } + curLatLng = lng + "&" + lat; + } + + @Override + public void setCurrentSpeed(double speed) { + mSpeed = speed * 3.6; // 转换为 km/h, 1米/秒(m/s)=3.6千米/时(km/h) + } + + }); + + mJoyStick.show(); + + isJoyStick = true; } return super.onStartCommand(intent, flags, startId); @@ -254,20 +243,6 @@ public class GoGoGoService extends Service { super.onDestroy(); } - private long getLocalTimeStamp() throws ParseException { - SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()); - - dff.setTimeZone(TimeZone.getTimeZone("GMT+08")); - - Date date = dff.parse(dff.format(new Date())); - - if (date != null) { - return date.getTime() / 1000; - } else { - return mTS; - } - } - //generate a location public Location generateLocation(LatLng latLng) { Location loc = new Location("gps"); diff --git a/app/src/main/java/com/zcshou/SNTP/SntpClient.java b/app/src/main/java/com/zcshou/service/GoSntpClient.java similarity index 96% rename from app/src/main/java/com/zcshou/SNTP/SntpClient.java rename to app/src/main/java/com/zcshou/service/GoSntpClient.java index bc83f73..6dd1545 100644 --- a/app/src/main/java/com/zcshou/SNTP/SntpClient.java +++ b/app/src/main/java/com/zcshou/service/GoSntpClient.java @@ -13,10 +13,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.zcshou.service; -package com.zcshou.SNTP; - -import android.net.Network; import android.os.SystemClock; import android.util.Log; @@ -30,14 +28,14 @@ import java.util.Arrays; * Simple SNTP client class for retrieving network time. * * Sample usage: - *
SntpClient client = new SntpClient();
+ * 
GoSntpClient client = new GoSntpClient();
  * if (client.requestTime("time.foo.com")) {
  *     long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
  * }
  * 
*/ -public class SntpClient { - private static final String TAG = "SntpClient"; +public class GoSntpClient { + private static final String TAG = "GoSntpClient"; private static final boolean DBG = true; private static final int REFERENCE_TIME_OFFSET = 16; @@ -127,7 +125,7 @@ public class SntpClient { // extract the results final byte leap = (byte) ((buffer[0] >> 6) & 0x3); final byte mode = (byte) (buffer[0] & 0x7); - final int stratum = (int) (buffer[1] & 0xff); + final int stratum = (buffer[1] & 0xff); final long originateTime = readTimeStamp(buffer, ORIGINATE_TIME_OFFSET); final long receiveTime = readTimeStamp(buffer, RECEIVE_TIME_OFFSET); final long transmitTime = readTimeStamp(buffer, TRANSMIT_TIME_OFFSET); @@ -267,7 +265,7 @@ public class SntpClient { buffer[offset++] = (byte) (seconds >> 24); buffer[offset++] = (byte) (seconds >> 16); buffer[offset++] = (byte) (seconds >> 8); - buffer[offset++] = (byte) (seconds >> 0); + buffer[offset++] = (byte) (seconds); long fraction = milliseconds * 0x100000000L / 1000L; // write fraction in big endian format @@ -275,6 +273,6 @@ public class SntpClient { buffer[offset++] = (byte) (fraction >> 16); buffer[offset++] = (byte) (fraction >> 8); // low order bits should be random data - buffer[offset++] = (byte) (Math.random() * 255.0); + buffer[offset] = (byte) (Math.random() * 255.0); } }