Created
November 17, 2023 12:58
-
-
Save RobertApikyan/acfc4ae037bb00782e999c325cff8aff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eview.ecare; | |
import android.content.ComponentName; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.ServiceConnection; | |
import android.os.Bundle; | |
import android.os.DeadObjectException; | |
import android.os.Handler; | |
import android.os.IBinder; | |
import android.os.Message; | |
import android.os.Messenger; | |
import android.os.RemoteException; | |
import android.util.Log; | |
import com.hfh.wellbe.smartwatch.utils.sensors.accelerometer.Ev05_MoveDetector; | |
import com.hfh.wellbe.smartwatch.utils.sensors.accelerometer.MotionSample; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import java.util.List; | |
public class eCareServiceInterface { | |
public static String TAG = "eCareServiceInterface"; | |
public static final int MSG_ALERT_FALLDOWN_SETTINGS = 1; | |
public static final int MSG_ALERT_FALLDOWN_QUERY = 2; | |
public static final int MSG_ECARE_REGISTER_MOTION = 20; | |
public static final int MSG_ECARE_RECEIVE_MOTION = 21; | |
public static final int MSG_ECARE_QUERY_MOTION = 22; | |
private Context context; | |
private Messenger mServerMessage; | |
private List<eCareServiceListener> mFwServiceListener = new ArrayList<>(); | |
public eCareServiceInterface(Context context) { | |
this.context = context; | |
} | |
private Messenger mReplayToMessage = new Messenger(new Handler() { | |
@Override | |
public void handleMessage(Message msg) { | |
Log.d(TAG, "handleMessage what=" + msg.what); | |
Bundle bundle = msg.getData(); | |
switch (msg.what) { | |
case MSG_ALERT_FALLDOWN_SETTINGS: | |
Log.d(TAG, "fall settings replay"); | |
break; | |
case MSG_ALERT_FALLDOWN_QUERY: | |
Log.d(TAG, "fall down settings enable=" + bundle.getBoolean("enable") | |
+ ",dial=" + bundle.getBoolean("dial") | |
+ ",level=" + bundle.getInt("level")); | |
break; | |
case MSG_ECARE_REGISTER_MOTION:{ | |
Log.d(TAG,"register motion"); | |
break; | |
} | |
case MSG_ECARE_RECEIVE_MOTION:{ | |
int motion = bundle.getInt("motion",-1); | |
MotionSample.getInstance(context).receiveMotionState(motion); | |
break; | |
} | |
case MSG_ECARE_QUERY_MOTION:{ | |
boolean motion = bundle.getBoolean("motion_state"); | |
MotionSample.getInstance(context).motionStateResult(motion); | |
break; | |
} | |
} | |
} | |
}); | |
private ServiceConnection mServiceConn = new ServiceConnection() { | |
@Override | |
public void onServiceConnected(ComponentName name, IBinder service) { | |
mServerMessage = new Messenger(service); | |
notifyServiceUsable(true); | |
} | |
@Override | |
public void onServiceDisconnected(ComponentName name) { | |
notifyServiceUsable(false); | |
mServerMessage = null; | |
} | |
}; | |
public interface eCareServiceListener { | |
boolean onServiceUsable(boolean usable); | |
} | |
private void notifyServiceUsable(boolean usable) { | |
Iterator<eCareServiceListener> iterator = mFwServiceListener.iterator(); | |
while (iterator.hasNext()) { | |
eCareServiceListener item = iterator.next(); | |
boolean remove = item.onServiceUsable(usable); | |
if (remove) { | |
iterator.remove(); | |
Log.d(TAG, "FwService remove1 num=" + mFwServiceListener.size()); | |
} | |
} | |
} | |
public void bindService() { | |
Intent intent = new Intent(); | |
intent.setAction("com.eview.ecare.eCareService"); | |
intent.setPackage("com.eview.ecare"); | |
context.bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE); | |
} | |
public void unBindService() { | |
context.unbindService(mServiceConn); | |
} | |
public boolean sendMessage(Message message) { | |
message.replyTo = mReplayToMessage; | |
if (mServerMessage == null) { | |
Log.e(TAG, "Fw Service unbind msg=" + message); | |
bindService(); | |
return false; | |
} | |
try { | |
mServerMessage.send(message); | |
return true; | |
} catch (DeadObjectException e) { | |
e.printStackTrace(); | |
bindService(); | |
Log.e(TAG, "error=" + e.getMessage()); | |
} catch (RemoteException e) { | |
e.printStackTrace(); | |
Log.e(TAG, "error=" + e.getMessage()); | |
} | |
return false; | |
} | |
public void addServerListener(eCareServiceListener listener) { | |
Log.d(TAG, "FwService add num=" + mFwServiceListener.size()); | |
if (mFwServiceListener.contains(listener)) | |
return; | |
mFwServiceListener.add(listener); | |
} | |
private static eCareServiceInterface serviceInterface; | |
public static eCareServiceInterface getServiceInterface(Context context) { | |
if (serviceInterface == null) | |
serviceInterface = new eCareServiceInterface(context); | |
return serviceInterface; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment