Note that calling this method from within the run method of + * a repeating timer task absolutely guarantees that the timer task will + * not run again. + * + *
This method may be called repeatedly; the second and subsequent + * calls have no effect. + * + * @return true if this task is scheduled for one-time execution and has + * not yet run, or this task is scheduled for repeated execution. + * Returns false if the task was scheduled for one-time execution + * and has already run, or if the task was never scheduled, or if + * the task was already cancelled. (Loosely speaking, this method + * returns true if it prevents one or more scheduled + * executions from taking place.) + */ + public boolean cancel() { + synchronized(lock) { + boolean result = (state == SCHEDULED); + state = CANCELLED; + return result; + } + } + + /** + * Returns the scheduled execution time of the most recent + * actual execution of this task. (If this method is invoked + * while task execution is in progress, the return value is the scheduled + * execution time of the ongoing task execution.) + * + *
This method is typically invoked from within a task's run method, to + * determine whether the current execution of the task is sufficiently + * timely to warrant performing the scheduled activity: + *
{@code + * public void run() { + * if (System.currentTimeMillis() - scheduledExecutionTime() >= + * MAX_TARDINESS) + * return; // Too late; skip this execution. + * // Perform the task + * } + * }+ * This method is typically not used in conjunction with + * fixed-delay execution repeating tasks, as their scheduled + * execution times are allowed to drift over time, and so are not terribly + * significant. + * + * @return the time at which the most recent execution of this task was + * scheduled to occur, in the format returned by Date.getTime(). + * The return value is undefined if the task has yet to commence + * its first execution. + * @see Date#getTime() + */ + public long scheduledExecutionTime() { + synchronized(lock) { + return (period < 0 ? nextExecutionTime + period + : nextExecutionTime - period); + } + } + + public void setIsDate(boolean isDate){ + this.isDate =isDate; + } + + public boolean getIsDate(){ + return isDate; + } +} + diff --git a/kurolibrary/src/main/java/com/trs88/kurolibrary/util/DistanceUtil.java b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/DistanceUtil.java new file mode 100644 index 0000000..ac8424e --- /dev/null +++ b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/DistanceUtil.java @@ -0,0 +1,56 @@ +package com.trs88.kurolibrary.util; + +import com.trs88.kurolibrary.log.KuroLog; + +/** + * 定位距离的工具类 + */ +public class DistanceUtil { + private final static double EARTH_RADIUS =6378.137; + + + private static double rad(double d) { + return d * Math.PI / 180.0; + } + /** + * 计算两个经纬度之间的距离,单位米 + * @param lat1 + * @param lng1 + * @param lat2 + * @param lng2 + * @return + */ + public static double getDistance(double lat1, double lng1, double lat2, double lng2) { + double radLat1 = rad(lat1); + double radLat2 = rad(lat2); + double a = radLat1 - radLat2; + double b = rad(lng1) - rad(lng2); + double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + + Math.cos(radLat1) * Math.cos(radLat2) + * Math.pow(Math.sin(b / 2), 2))); + s = s * EARTH_RADIUS; + s = Math.round(s * 10000d) / 10000d; + s = s * 1000; + KuroLog.v("getDistance 两点间距离: "+s); +// Toast.makeText(App.getInstance(),"curLatitude :"+lat2+" ,curLongitude: "+lng2+ " ,getDistance 两点间距离: "+s ,Toast.LENGTH_SHORT).show(); + return s; + } + + + /** + * 判断是否在范围内 + * @param fenceLatitude + * @param fenceLongitude + * @param curLatitude + * @param curLongitude + * @param radius + * @return + */ + public static boolean inRadius(double fenceLatitude, double fenceLongitude, double curLatitude, double curLongitude, int radius){ + if (getDistance(fenceLatitude,fenceLongitude,curLatitude,curLongitude)>=radius){ + return false; + }else { + return true; + } + } +} diff --git a/kurolibrary/src/main/java/com/trs88/kurolibrary/util/KuroDataBus.kt b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/KuroDataBus.kt new file mode 100644 index 0000000..ee3edc9 --- /dev/null +++ b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/KuroDataBus.kt @@ -0,0 +1,87 @@ +package com.trs88.kurolibrary.util + +import androidx.lifecycle.* +import java.util.concurrent.ConcurrentHashMap + + +object KuroDataBus { + private val eventMap =ConcurrentHashMap
+ * 假设: 9点20调用call生成一个线程T1,T1会睡眠2分钟 9点21再次调用call生成一个线程A2 由于T1没有睡醒,call方法就被再次调用,所以T1的{@link LastThreadListener}
+ * 中的call不会执行,只执行了T2的 {@link LastThreadListener}中的call 最后结果:{@link LastThread}中的call 调用了两次,但是 {@link LastThreadListener}中的call只执行了一次
+ *
+ * @param obj 允许携带的参数,@link {@link LastThreadListener}
+ */
+ public void call(Object obj) {
+ start(obj);
+ }
+
+ /**
+ * 在call 之前调用,线程睡醒后才会执行{@link LastThreadListener }中方法的call
+ *
+ * @param sleep 毫秒
+ */
+ public void setSleep(long sleep) {
+ this.sleep = sleep;
+ }
+
+ /**
+ * 携带参数
+ *
+ * @param value
+ */
+ public void setValue(Object value) {
+ this.value = value;
+ }
+
+ /**
+ * 停止所有没睡醒的线程
+ */
+ public void cancel() {
+ allowExcuteVersion();
+ stop();
+ }
+
+ // 真正处理逻辑
+ private void start(final Object obj) {
+ allowExcuteVersion();
+ stop();
+
+ curr = new Thread(new Runnable() {
+
+ public void run() {
+ if (sleep > 0) {
+ try {
+ Thread.sleep(sleep);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ return;
+ }
+ }
+ Thread currentThread = Thread.currentThread();
+
+ boolean return1 = isReturn(currentThread);
+ if (return1) {
+ if (listenr != null) {
+ listenr.call(END, value);
+ }
+ return;
+ }
+
+ if (listenr != null) {
+ listenr.call(START, obj);
+ }
+
+ }
+ });
+ curr.setName(count + "");
+ curr.start();
+ }
+
+ // 最新线程的标记
+ private void allowExcuteVersion() {
+ count++;
+ }
+
+ // 停止当前的线程
+ private void stop() {
+ if (curr != null) {
+ try {
+ curr.stop();
+ } catch (Exception e) {
+ }
+ }
+ }
+
+ /**
+ * 是否调用线程的{@link LastThreadListener}中call
+ *
+ * @param thread
+ * @return
+ */
+ private boolean isReturn(Thread thread) {
+ String name = thread.getName();
+ int curr = Integer.valueOf(name);
+ if (curr < count) {
+ return true;
+ }
+ return false;
+ }
+
+}
diff --git a/kurolibrary/src/main/java/com/trs88/kurolibrary/util/LastThreadListener.java b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/LastThreadListener.java
new file mode 100644
index 0000000..fa6b957
--- /dev/null
+++ b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/LastThreadListener.java
@@ -0,0 +1,5 @@
+package com.trs88.kurolibrary.util;
+
+public interface LastThreadListener {
+ public Object call(int flag, Object obj);
+}
diff --git a/kurolibrary/src/main/java/com/trs88/kurolibrary/util/MainHandler.kt b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/MainHandler.kt
new file mode 100644
index 0000000..1a57944
--- /dev/null
+++ b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/MainHandler.kt
@@ -0,0 +1,21 @@
+package com.trs88.kurolibrary.util
+
+import android.os.Handler
+import android.os.Looper
+import android.os.Message
+
+object MainHandler {
+ private val handler = Handler(Looper.getMainLooper())
+ fun post(runnable: Runnable){
+ handler.post { runnable }
+ }
+
+ fun postDelay(delayMills:Long,runnable: Runnable){
+ handler.postDelayed(runnable,delayMills)
+ }
+
+ fun sendAtFrontOfQueue(runnable: Runnable){
+ val msg = Message.obtain(handler,runnable)
+ handler.sendMessageAtFrontOfQueue(msg)
+ }
+}
\ No newline at end of file
diff --git a/kurolibrary/src/main/java/com/trs88/kurolibrary/util/SPUtil.kt b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/SPUtil.kt
new file mode 100644
index 0000000..110c696
--- /dev/null
+++ b/kurolibrary/src/main/java/com/trs88/kurolibrary/util/SPUtil.kt
@@ -0,0 +1,59 @@
+package com.trs88.kurolibrary.util
+
+import android.annotation.SuppressLint
+import android.app.Application
+import android.content.Context
+import android.content.SharedPreferences
+import com.trs88.kurolibrary.activity.AppGlobals
+
+@SuppressLint("ApplySharedPref")
+object SPUtil {
+ private const val CACHE_FILE = "Kuro_sp"
+ fun putString(key: String, value: String) {
+ val shared = getShared()
+ shared?.edit()?.putString(key, value)?.commit()
+ }
+
+ fun getString(key: String): String? {
+ val shared = getShared()
+ if (shared != null) {
+ return shared.getString(key, null)
+ }
+ return null
+ }
+
+ fun putBoolean(key: String, value: Boolean) {
+ val shared = getShared()
+ shared?.edit()?.putBoolean(key, value)?.commit()
+ }
+
+ fun getBoolean(key: String): Boolean {
+ val shared = getShared()
+ if (shared != null) {
+ return shared.getBoolean(key, false)
+ }
+ return false
+ }
+
+ fun putInt(key: String, value: Int) {
+ val shared = getShared()
+ shared?.edit()?.putInt(key, value)?.commit()
+ }
+
+ fun getInt(key: String): Int {
+ val shared = getShared()
+ if (shared != null) {
+ return shared.getInt(key, 0)
+ }
+ return 0
+ }
+
+
+ private fun getShared(): SharedPreferences? {
+ val application: Application? = AppGlobals.get()
+ if (application != null) {
+ return application.getSharedPreferences(CACHE_FILE, Context.MODE_PRIVATE)
+ }
+ return null
+ }
+}
\ No newline at end of file
diff --git a/kurolibrary/src/main/res/layout/kurolog_item.xml b/kurolibrary/src/main/res/layout/kurolog_item.xml
new file mode 100644
index 0000000..96bb6d4
--- /dev/null
+++ b/kurolibrary/src/main/res/layout/kurolog_item.xml
@@ -0,0 +1,17 @@
+
+