package com.algobase.service; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.GregorianCalendar; import java.util.Date; import java.util.List; import java.text.SimpleDateFormat; import android.os.Bundle; import android.os.Build; import android.app.ActivityManager; import android.app.ActivityManager.RunningServiceInfo; import android.content.Context; import android.content.Intent; import android.util.Log; import com.google.android.gms.wearable.MessageEvent; import com.google.android.gms.wearable.DataEventBuffer; import com.google.android.gms.wearable.WearableListenerService; import com.algobase.share.dialog.*; import com.algobase.share.system.*; public class ListenerService extends WearableListenerService { public static final String WEARABLE_MESSAGE = "Wearable Message"; Sender sender; File current_gps_file; boolean dataServiceRunning = false; File stracks_folder = null; File log_folder = null; File log_file = null; FileWriter log_writer = null; void writeLog(FileWriter writer, String msg) { try { writer.write(msg,0,msg.length()); writer.write('\n'); writer.flush(); } catch(IOException e){ } } void log(String msg) { Log.v("sTracksWearableListener",msg); if (log_writer != null) { writeLog(log_writer, msg); } } public void showToast(String msg) { final MyToast toast = new MyToast(this,msg); toast.setTextColor(0xffffffff); toast.setBackgroundColor(0xff777777); toast.setBorderColor(0xffffffff); toast.show(); } boolean isDataServiceRunning() { Context context = this; ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List L = manager.getRunningServices(Integer.MAX_VALUE); for (RunningServiceInfo rsi : L) { String class_name = rsi.service.getClassName(); if (class_name.equals(DataService.class.getName())) { return true; } } return false; } void startDataService(String cmd, String param) { //Context context = getBaseContext(); Context context = this; Intent intent = new Intent(context, DataService.class); if (cmd != null) intent.putExtra("cmd",cmd); if (param != null) intent.putExtra("param1",param); if (Build.VERSION.SDK_INT < 26) startService(intent); else startForegroundService(intent); } void startServer() { GregorianCalendar cal = new GregorianCalendar(); Date date = cal.getTime(); final String trk_name = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(date); new MyThread() { public void run() { if (!dataServiceRunning) startDataService("connect",null); if (!current_gps_file.exists()) { //showToast("START " + trk_name); sleep(500); startDataService("begin",trk_name); sleep(500); startDataService("start",null); } } }.start(); } @Override public void onCreate() { super.onCreate(); stracks_folder = getFilesDir(); log_folder = new File(stracks_folder,"log"); log_file = new File(log_folder, "listener_log.txt"); current_gps_file = new File(stracks_folder,"gps/current_track.gps"); // test if data server is running dataServiceRunning = false; //Context context = getBaseContext(); Context context = this; ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List L= manager.getRunningServices(Integer.MAX_VALUE); for (RunningServiceInfo rsi : L) { String class_name = rsi.service.getClassName(); if (class_name.equals(DataService.class.getName())) dataServiceRunning = true; } try { log_writer = new FileWriter(log_file); } catch(IOException ex) {} sender = new Sender(context); log("onCreate"); } @Override public void onDestroy() { log("onDestroyed"); super.onDestroy(); } @Override public void onDataChanged(DataEventBuffer dataEvents) { super.onDataChanged(dataEvents); log("onDataChanged"); } @Override public void onMessageReceived(MessageEvent messageEvent) { super.onMessageReceived(messageEvent); String path = messageEvent.getPath(); log("onMessageReceived: path = " + path); if (!path.equals("/message_path")) { super.onMessageReceived(messageEvent); return; } String msg = new String(messageEvent.getData()); log("msg = " + msg); if (msg.equals("STATUS")) { if (dataServiceRunning) { if (current_gps_file.exists()) sender.sendMessage("STATUS: recording"); else sender.sendMessage("STATUS: running"); } else sender.sendMessage("STATUS: stopped"); return; } if (msg.equals("START")) { startServer(); // return; } // broadcast message to DataService Intent intent = new Intent(); intent.setAction(WEARABLE_MESSAGE); intent.putExtra("message", msg); sendBroadcast(intent); } }