package com.ic.work.hardware;
+import android.os.SystemClock;
+
import com.ic.work.domain.TemperatureItem;
import com.ic.work.utils.HardwareUtils;
import com.ic.work.utils.ICLogger;
import com.ic.work.utils.IcLog;
import com.ic.work.utils.TemperatureUtil;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-
/**
* 获取温度的工具类
*/
HardwareUtils.disablePedometer();
if (HardwareUtils.enableTemperature()) {
IcLog.i(TAG, "getNdkTemperatureValue enableTemperature");
- long begin = System.currentTimeMillis();
- while (true) {
- if (System.currentTimeMillis() - begin > 1000) {
- break;
- }
- }
+ SystemClock.sleep(1000);
}
- //这里做超时处理
- ExecutorService executor = Executors.newSingleThreadExecutor();
- while (true) {
- Future<?> submit = executor.submit(() -> {
- IcLog.d(TAG, "do temperature....");
- long begin = System.currentTimeMillis();
- while (true) {
- if (System.currentTimeMillis() - begin > 1000) {
- break;
- }
+ do {
+ IcLog.d(TAG, "do temperature....");
+ SystemClock.sleep(1000);
+ long getStartTime = System.currentTimeMillis();
+ lastWristTemp = HardwareUtils.getNdkTemperatureOutValue();
+ TemperatureItem temperatureItem = TemperatureUtil.doTempTest();
+ if (temperatureItem != null) {
+ lastWristTemp = Double.parseDouble(temperatureItem.getBodyTemp());
+ lastTempOut = Double.parseDouble(temperatureItem.getEvnTemp());
+
+ if (lastTempOut > 60 || lastTempOut < 1) {
+ lastTempOut = 0.0f;
}
- long getStartTime = System.currentTimeMillis();
- lastWristTemp = HardwareUtils.getNdkTemperatureOutValue();
- TemperatureItem temperatureItem = TemperatureUtil.doTempTest();
- if (temperatureItem != null) {
- lastWristTemp = Double.parseDouble(temperatureItem.getBodyTemp());
- lastTempOut = Double.parseDouble(temperatureItem.getEvnTemp());
-
- if (lastTempOut > 60 || lastTempOut < 1) {
- lastTempOut = 0.0f;
- }
- if (lastTempOut > 0.0f) {
- lastBodyTemp = TemperatureUtil.getBodyTemp(lastWristTemp, lastTempOut);
- } else {
- lastBodyTemp = TemperatureUtil.getBodyTemp(lastWristTemp, 26.0f);
- }
- IcLog.d(TAG, "testing..." + lastWristTemp + " " + lastBodyTemp + " " + lastTempOut);
- //前10秒不更新内容,后10秒再更新数据
- if (delayTimesInSeconds <= 10 && mListener != null) {
- IcLog.d(TAG, "update temperature info..thread name is ." + Thread.currentThread().getName());
- //获取
- IcLog.d(TAG, "lastWristTemp == > " + lastWristTemp);
- IcLog.d(TAG, "lastBodyTemp == > " + lastBodyTemp);
- IcLog.d(TAG, "lastTempOut == > " + lastTempOut);
- mListener.onTemperatureUpdate(lastWristTemp, lastBodyTemp, lastTempOut);
- }
- ICLogger.d("get temperature data use time == > " + (System.currentTimeMillis() - getStartTime));
+ if (lastTempOut > 0.0f) {
+ lastBodyTemp = TemperatureUtil.getBodyTemp(lastWristTemp, lastTempOut);
+ } else {
+ lastBodyTemp = TemperatureUtil.getBodyTemp(lastWristTemp, 26.0f);
}
- });
-
- try {
- ICLogger.d("start to get temperature data...");
- submit.get(10, TimeUnit.SECONDS);
- ICLogger.d("to get temperature end...");
- } catch (Exception e) {
- ICLogger.d("get temperature data time out...");
- continue;
+ IcLog.d(TAG, "testing..." + lastWristTemp + " " + lastBodyTemp + " " + lastTempOut);
+ //前10秒不更新内容,后10秒再更新数据
+ if (delayTimesInSeconds <= 10 && mListener != null) {
+ IcLog.d(TAG, "update temperature info..thread name is ." + Thread.currentThread().getName());
+ //获取
+ IcLog.d(TAG, "lastWristTemp == > " + lastWristTemp);
+ IcLog.d(TAG, "lastBodyTemp == > " + lastBodyTemp);
+ IcLog.d(TAG, "lastTempOut == > " + lastTempOut);
+ mListener.onTemperatureUpdate(lastWristTemp, lastBodyTemp, lastTempOut);
+ }
+ ICLogger.d("get temperature data use time == > " + (System.currentTimeMillis() - getStartTime));
}
delayTimesInSeconds--;
IcLog.d(TAG, "delayTimesInSeconds " + delayTimesInSeconds);
//延时delay_times_in_seconds秒后停止检测。
- if (delayTimesInSeconds <= 0) {
- break;
- }
- }
- executor.shutdown();
+ } while (delayTimesInSeconds > 0);
} catch (Exception e) {
IcLog.e(TAG, "TemperatureRunnable :", e);
} finally {
import android.content.Context;
import android.content.pm.PackageManager;
+import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
+import android.util.Log;
import java.io.File;
import java.io.IOException;
+import static android.os.Environment.DIRECTORY_DOCUMENTS;
import static android.os.Environment.MEDIA_MOUNTED;
/**
*/
public final class StorageUtils {
- private static final String EXTERNAL_STORAGE_PERMISSION = "android.permission.WRITE_EXTERNAL_STORAGE";
- private static final String INDIVIDUAL_DIR_NAME = "uil-images";
-
- private StorageUtils() {
- }
-
- public static String getExternalStorageLogDir() {
-
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- File dir = new File(Environment.getExternalStorageDirectory().getPath(), "Android/ic/icprotocol");
- String path = dir.getPath();
- if (!dir.exists()) {
- dir.mkdirs();
- }
- return path;
- }
- return "/storage/emulated/0/Android/ic/icprotocol";
- }
- /**
- * Returns application cache directory. Cache directory will be created on SD card
- * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted and app has appropriate permission. Else -
- * Android defines cache directory on device's file system.
- *
- * @param context Application context
- * @return Cache {@link File directory}.<br />
- * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
- * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
- */
- public static File getCacheDirectory(Context context) {
- return getCacheDirectory(context, true);
- }
-
- /**
- * Returns application cache directory. Cache directory will be created on SD card
- * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
- * on device's file system depending incoming parameters.
- *
- * @param context Application context
- * @param preferExternal Whether prefer external location for cache
- * @return Cache {@link File directory}.<br />
- * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
- * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
- */
- public static File getCacheDirectory(Context context, boolean preferExternal) {
- File appCacheDir = null;
- if (preferExternal && MEDIA_MOUNTED
- .equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context) && haveFreeSpace()) {
- appCacheDir = getExternalCacheDir(context);
- }
- if (appCacheDir == null) {
- appCacheDir = context.getCacheDir();
- }
- if (appCacheDir == null) {
- String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
- appCacheDir = new File(cacheDirPath);
- }
- return appCacheDir;
- }
-
- /**
- * Returns individual application cache directory (for only image caching from ImageLoader). Cache directory will be
- * created on SD card <i>("/Android/data/[app_package_name]/cache/uil-images")</i> if card is mounted and app has
- * appropriate permission. Else - Android defines cache directory on device's file system.
- *
- * @param context Application context
- * @return Cache {@link File directory}
- */
- public static File getIndividualCacheDirectory(Context context) {
- File cacheDir = getCacheDirectory(context);
- File individualCacheDir = new File(cacheDir, INDIVIDUAL_DIR_NAME);
- if (!individualCacheDir.exists()) {
- if (!individualCacheDir.mkdir()) {
- individualCacheDir = cacheDir;
- }
- }
- return individualCacheDir;
- }
-
- /**
- * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
- * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system.
- *
- * @param context Application context
- * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
- * @return Cache {@link File directory}
- */
- public static File getOwnCacheDirectory(Context context, String cacheDir) {
- File appCacheDir = null;
- if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
- appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
- }
- if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
- appCacheDir = context.getCacheDir();
- }
- return appCacheDir;
- }
-
- private static File getExternalCacheDir(Context context) {
- File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
- File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
- if (!appCacheDir.exists()) {
- if (!appCacheDir.mkdirs()) {
- return null;
- }
- try {
- new File(appCacheDir, ".nomedia").createNewFile();
- } catch (IOException e) {
- }
- }
- return appCacheDir;
- }
-
- private static boolean hasExternalStoragePermission(Context context) {
- int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
- return perm == PackageManager.PERMISSION_GRANTED;
- }
-
- private static boolean haveFreeSpace() {
+ private static final String EXTERNAL_STORAGE_PERMISSION = "android.permission.WRITE_EXTERNAL_STORAGE";
+ private static final String INDIVIDUAL_DIR_NAME = "uil-images";
+ private static final String TAG = "StorageUtils";
+
+ private StorageUtils() {
+ }
+
+ public static String getExternalStorageLogDir() {
+
+
+ //判断版本,如果是4.4的版本则使用旧的API
+ //如果是8.1的版本,使用publish的API
+ if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
+ File dir;
+ Log.d(TAG, "current system version is == > " + Build.VERSION.SDK_INT);
+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
+ dir = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_DOCUMENTS), "Android/ic/icWorkService");
+ } else {
+ dir = new File(Environment.getExternalStorageDirectory().getPath(), "Android/ic/icprotocol");
+ }
+
+ String path = dir.getPath();
+ if (!dir.exists()) {
+ dir.mkdirs();
+ }
+ return path;
+ }
+
+ return "/storage/emulated/0/Android/ic/icWorkService";
+ }
+
+ /**
+ * Returns application cache directory. Cache directory will be created on SD card
+ * <i>("/Android/data/[app_package_name]/cache")</i> if card is mounted and app has appropriate permission. Else -
+ * Android defines cache directory on device's file system.
+ *
+ * @param context Application context
+ * @return Cache {@link File directory}.<br />
+ * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
+ * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
+ */
+ public static File getCacheDirectory(Context context) {
+ return getCacheDirectory(context, true);
+ }
+
+ /**
+ * Returns application cache directory. Cache directory will be created on SD card
+ * <i>("/Android/data/[app_package_name]/cache")</i> (if card is mounted and app has appropriate permission) or
+ * on device's file system depending incoming parameters.
+ *
+ * @param context Application context
+ * @param preferExternal Whether prefer external location for cache
+ * @return Cache {@link File directory}.<br />
+ * <b>NOTE:</b> Can be null in some unpredictable cases (if SD card is unmounted and
+ * {@link Context#getCacheDir() Context.getCacheDir()} returns null).
+ */
+ public static File getCacheDirectory(Context context, boolean preferExternal) {
+ File appCacheDir = null;
+ if (preferExternal && MEDIA_MOUNTED
+ .equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context) && haveFreeSpace()) {
+ appCacheDir = getExternalCacheDir(context);
+ }
+ if (appCacheDir == null) {
+ appCacheDir = context.getCacheDir();
+ }
+ if (appCacheDir == null) {
+ String cacheDirPath = "/data/data/" + context.getPackageName() + "/cache/";
+ appCacheDir = new File(cacheDirPath);
+ }
+ return appCacheDir;
+ }
+
+ /**
+ * Returns individual application cache directory (for only image caching from ImageLoader). Cache directory will be
+ * created on SD card <i>("/Android/data/[app_package_name]/cache/uil-images")</i> if card is mounted and app has
+ * appropriate permission. Else - Android defines cache directory on device's file system.
+ *
+ * @param context Application context
+ * @return Cache {@link File directory}
+ */
+ public static File getIndividualCacheDirectory(Context context) {
+ File cacheDir = getCacheDirectory(context);
+ File individualCacheDir = new File(cacheDir, INDIVIDUAL_DIR_NAME);
+ if (!individualCacheDir.exists()) {
+ if (!individualCacheDir.mkdir()) {
+ individualCacheDir = cacheDir;
+ }
+ }
+ return individualCacheDir;
+ }
+
+ /**
+ * Returns specified application cache directory. Cache directory will be created on SD card by defined path if card
+ * is mounted and app has appropriate permission. Else - Android defines cache directory on device's file system.
+ *
+ * @param context Application context
+ * @param cacheDir Cache directory path (e.g.: "AppCacheDir", "AppDir/cache/images")
+ * @return Cache {@link File directory}
+ */
+ public static File getOwnCacheDirectory(Context context, String cacheDir) {
+ File appCacheDir = null;
+ if (MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) {
+ appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir);
+ }
+ if (appCacheDir == null || (!appCacheDir.exists() && !appCacheDir.mkdirs())) {
+ appCacheDir = context.getCacheDir();
+ }
+ return appCacheDir;
+ }
+
+ private static File getExternalCacheDir(Context context) {
+ File dataDir = new File(new File(Environment.getExternalStorageDirectory(), "Android"), "data");
+ File appCacheDir = new File(new File(dataDir, context.getPackageName()), "cache");
+ if (!appCacheDir.exists()) {
+ if (!appCacheDir.mkdirs()) {
+ return null;
+ }
+ try {
+ new File(appCacheDir, ".nomedia").createNewFile();
+ } catch (IOException e) {
+ }
+ }
+ return appCacheDir;
+ }
+
+ private static boolean hasExternalStoragePermission(Context context) {
+ int perm = context.checkCallingOrSelfPermission(EXTERNAL_STORAGE_PERMISSION);
+ return perm == PackageManager.PERMISSION_GRANTED;
+ }
+
+ private static boolean haveFreeSpace() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
StatFs st = new StatFs(Environment.getExternalStorageDirectory().getPath());
int blockSize = st.getBlockSize();