package com.xixun.xixunplayer; import android.annotation.SuppressLint; import android.app.Service; import android.content.Intent; import android.graphics.BitmapFactory; import android.os.Environment; import android.os.IBinder; import android.os.RemoteException; import com.xixun.util.PlayerInfo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.nio.charset.StandardCharsets; import java.util.Base64; import gnph.util.IOs; import gnph.util.JSMap; import gnph.util.URLConn; public class Server extends Service { @Override public IBinder onBind(Intent intent) { return binder; } PlayerInfo.Stub binder = new PlayerInfo.Stub() { @Override public String getProgramName() throws RemoteException { return null; } @Override public String getVersion() throws RemoteException { return null; } @Override public void setScreenWidth(int w) throws RemoteException { } @Override public void setScreenHeight(int h) throws RemoteException { } @Override public void taskScreenshot(String cmdId) throws RemoteException { } @Override public void setExternalTemperature(float t) throws RemoteException { } @Override public void setInternalTemperature(float t) throws RemoteException { } @Override public void setHumidity(float h) throws RemoteException { } @Override public boolean forcePlayProgram(String pid) throws RemoteException { return false; } @Override public boolean finishForcePlay() throws RemoteException { return false; } @Override public String getCurProgramId() throws RemoteException { return null; } @Override public void setUSBProgramPwd(String pwd) throws RemoteException { } @SuppressLint("ResourceType") @Override public String executeJosnCommand(String jsonstr) throws RemoteException { System.out.println("Server executeJsonCommand ..."+jsonstr);//{"_type":"DeleteTask","id":"652522a0e81d1e000009201a","sendTo":"yzd-player"} String id = null; try { var json = JSMap.from(jsonstr.getBytes(StandardCharsets.UTF_8)); var _type = json.stnn("_type"); id = json.stnn("id"); if(_type.equals("DeleteTask")) { MainActivity.ins.runOnUiThread(() -> MainActivity.ins.delProgFile()); return new JSMap( "_type", "DataCallback", "result", "received command", "cardId", Util.getCardId(), "commandId", id ).toString(); } else if(_type.equals("PlayerStateCommand")) { var descs = Util.getState(MainActivity.ins.state); return new JSMap( "_type", "Success", "cardId", Util.getCardId(), "commandId", id, "code", MainActivity.ins.state, "des_en", descs[0], "des", descs[1] ).toString(); } else if(_type.equals("SetPlayerBackground")) { var url = json.str("url"); if(url==null) new File(Util.backImgFile).delete(); else IOs.writeClose(new FileOutputStream(Util.backImgFile), new URLConn(url).in()); MainActivity.ins.runOnUiThread(() -> { MainActivity.ins.backView.cosImg = url==null ? null : BitmapFactory.decodeFile(Util.backImgFile); MainActivity.ins.backView.invalidate(); }); return new JSMap( "_type", "Success", "cardId", Util.getCardId(), "commandId", id ).toString(); } else if(_type.equals("GetPlayerBackground")) { var backImg = new File(Util.backImgFile); var img = Base64.getEncoder().encodeToString(IOs.readBytesClose(backImg.exists() ? new FileInputStream(backImg) : MainActivity.ins.getResources().openRawResource(R.drawable.back))); return new JSMap( "_type", "DataCallback", "cardId", Util.getCardId(), "commandId", id, "img", img ).toString(); } return new JSMap( "_type", "Error", "errorMessage", "Unknow Type", "cardId", Util.getCardId(), "commandId", id ).toString(); } catch (Exception e) { return new JSMap( "_type", "Error", "errorMessage", e.toString(), "cardId", Util.getCardId(), "commandId", id ).toString(); } } @Override public void pausePlayer(boolean b) throws RemoteException { } @Override public boolean isPause() throws RemoteException { return false; } @Override public boolean clearTasks() throws RemoteException { System.out.println("Server clearTasks ..."); return true; } @Override public int countOfPrograms(int type) throws RemoteException { return 0; } @Override public void playInsertTask(String pid) throws RemoteException { } @Override public void stopInsertTask(String pid) throws RemoteException { } @Override public String getProgramTask() throws RemoteException { return null; } @Override public void setUploadLogUrl(String playLog) throws RemoteException { } @Override public String getUploadLogUrl() throws RemoteException { return null; } }; }