瀏覽代碼

优化公司设备连接过程

cs 10 月之前
父節點
當前提交
9186eaff66

+ 13 - 0
app/src/main/java/org/las2mile/okio/OkIoClient.java

@@ -73,6 +73,9 @@ public class OkIoClient extends Thread{
                             HartBeatMsgResp msgResp = new HartBeatMsgResp();
                             msgResp.decodeData(packet.mBaseMsgBuffer, (short) packet.mBaseMsgBuffer.size());
                             Ln.e("client heartbeat end----------------------------------"+msgResp.toString());
+                            if(mOnOkIoMsgListener != null){
+                                mOnOkIoMsgListener.onOkIoMsg(msgResp);
+                            }
                             requestLogFile();
                         }
                     }
@@ -161,4 +164,14 @@ public class OkIoClient extends Thread{
         reqLogFileMsg.setLogPath("/sdcard/log.txt");
         send(reqLogFileMsg);
     }
+
+    OnOkIoMsgListener mOnOkIoMsgListener;
+
+    public void setOnOkIoMsgListener(OnOkIoMsgListener onOkIoMsgListener) {
+        this.mOnOkIoMsgListener = onOkIoMsgListener;
+    }
+
+    public interface OnOkIoMsgListener{
+        void onOkIoMsg(BaseMsg msg);
+    }
 }

+ 85 - 7
app/src/main/java/org/las2mile/scrcpy/MainActivity.java

@@ -40,6 +40,12 @@ import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.util.Enumeration;
 
+import android.app.ProgressDialog;
+import android.os.Handler;
+import android.os.Message;
+import org.las2mile.okio.message.BaseMsg;
+import org.las2mile.okio.message.resp.HartBeatMsgResp;
+
 
 public class MainActivity extends Activity implements Scrcpy.ServiceCallbacks, SensorEventListener {
     private static final String PREFERENCE_KEY = "default";
@@ -69,6 +75,44 @@ public class MainActivity extends Activity implements Scrcpy.ServiceCallbacks, S
     private LinearLayout linearLayout;
     private static boolean no_control = false;
 
+    ProgressDialog mainProgressDialog = null;
+    static final int ADB_CONNECT_START = 1;
+    static final int ADB_CONNECT_REAL_START = 2;
+    static final int ADB_CONNECT_SUC = 3;
+    static final int ADB_CONNECT_FAIL = 4;
+    static final int ADB_CONNECT_FAIL_HIDE = 5;
+    static final int SERVER_ENV_PREPARED = 6;
+    static final int GOT_REMOTE_VERSION = 7;
+    Handler mainHandler = new Handler(new Handler.Callback() {
+        @Override
+        public boolean handleMessage(Message message) {
+            int what = message.what;
+            Log.e("scrcpy",mainHandler+" Asset ADB_CONNECT_SUC "+what);
+            if(what == ADB_CONNECT_START){
+                showProgress();
+                mainHandler.sendEmptyMessageDelayed(ADB_CONNECT_REAL_START,500);
+            } else if(what == ADB_CONNECT_REAL_START){
+                sendCommands.SendAdbCommands(context, fileBase64, serverAdr, Integer.parseInt(serverPort), local_ip, videoBitrate, Math.max(screenHeight, screenWidth));   
+            } else if(what == ADB_CONNECT_SUC){
+                setProgressMsg("与公司设备连接成功,正在初始化公司设备...");
+            } else if(what == ADB_CONNECT_FAIL){
+                setProgressMsg("与公司设备连接失败, 请确认IP与端口是否正确,再次点击重连。");
+                mainHandler.sendEmptyMessageDelayed(ADB_CONNECT_FAIL_HIDE,2000);
+            } else if(what == ADB_CONNECT_FAIL_HIDE){
+                hideProgress();
+            }  else if(what == SERVER_ENV_PREPARED){
+                setProgressMsg("公司设备初始化完成,即将投屏...");
+                // hideProgress();
+                start_screen_copy_magic();
+            } else if(what == GOT_REMOTE_VERSION){
+                hideProgress();
+                HartBeatMsgResp hartBeatMsgResp = (HartBeatMsgResp)message.obj;
+                Toast.makeText(context, hartBeatMsgResp.remoteInfo, Toast.LENGTH_SHORT).show();
+            }
+            return false;
+        }
+    });
+
     private final ServiceConnection serviceConnection = new ServiceConnection() {
         @Override
         public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
@@ -131,6 +175,33 @@ public class MainActivity extends Activity implements Scrcpy.ServiceCallbacks, S
 
     }
 
+    @Override
+    public void onOkIoMsg(BaseMsg baseMsg){
+        Message msg = new Message();
+        msg.what = GOT_REMOTE_VERSION;
+        msg.obj = baseMsg;
+        mainHandler.sendMessage(msg);
+    }
+
+    private void showProgress(){
+        Log.e("scrcpy","Asset showProgress");
+        mainProgressDialog = new ProgressDialog(context);
+        mainProgressDialog.setTitle("提示");
+        mainProgressDialog.setMessage("正在连接公司设备..."); 
+        mainProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
+        mainProgressDialog.setIndeterminate(true);
+        mainProgressDialog.setCancelable(false);
+        mainProgressDialog.setCanceledOnTouchOutside(false);
+        mainProgressDialog.show();
+    }
+
+    private void setProgressMsg(String msg){
+        mainProgressDialog.setMessage(msg);
+    }
+
+    private void hideProgress(){
+        mainProgressDialog.dismiss();
+    }
 
     @SuppressLint("SourceLockedOrientationActivity")
     public void scrcpy_main(){
@@ -146,16 +217,22 @@ public class MainActivity extends Activity implements Scrcpy.ServiceCallbacks, S
         } catch (IOException e) {
             Log.e("Asset Manager", e.getMessage());
         }
-        sendCommands = new SendCommands();
+        sendCommands = new SendCommands(mainHandler);
 
         startButton.setOnClickListener(v -> {
             local_ip = wifiIpAddress();
             getAttributes();
             if (!serverAdr.isEmpty()) {
-                if (sendCommands.SendAdbCommands(context, fileBase64, serverAdr, Integer.parseInt(serverPort), local_ip, videoBitrate, Math.max(screenHeight, screenWidth)) == 0) {
-                    start_screen_copy_magic();
-                } else {
-                    Toast.makeText(context, "Network OR ADB connection failed", Toast.LENGTH_SHORT).show();
+                //原生逻辑
+                // if (sendCommands.SendAdbCommands(context, fileBase64, serverAdr, Integer.parseInt(serverPort), local_ip, videoBitrate, Math.max(screenHeight, screenWidth)) == 0) {
+                //     start_screen_copy_magic();
+                // } else {
+                //     Toast.makeText(context, "Network OR ADB connection failed", Toast.LENGTH_SHORT).show();
+                // }
+
+                //开始与adbd建立连接
+                if(first_time){
+                    mainHandler.sendEmptyMessage(ADB_CONNECT_START);
                 }
             } else {
                 Toast.makeText(context, "Server Address Empty", Toast.LENGTH_SHORT).show();
@@ -210,6 +287,7 @@ public class MainActivity extends Activity implements Scrcpy.ServiceCallbacks, S
         }
         float remote_device_aspect_ratio = remote_device_height/remote_device_width;
 
+        //改变渲染的边距
         if (!landscape) {                                                            //Portrait
             float this_device_aspect_ratio = this_dev_height/this_dev_width;
             if (remote_device_aspect_ratio > this_device_aspect_ratio) {
@@ -296,8 +374,8 @@ public class MainActivity extends Activity implements Scrcpy.ServiceCallbacks, S
 
     @SuppressLint("ClickableViewAccessibility")
     private void start_screen_copy_magic() {
-//        Log.e("Scrcpy: ","Starting scrcpy service");
-            setContentView(R.layout.surface);
+       Log.e("Scrcpy: ","Starting scrcpy service");
+            setContentView(R.layout.surface);//开始渲染
             final View decorView = getWindow().getDecorView();
             decorView.setSystemUiVisibility(
                     View.SYSTEM_UI_FLAG_LAYOUT_STABLE

+ 11 - 0
app/src/main/java/org/las2mile/scrcpy/Scrcpy.java

@@ -26,6 +26,8 @@ import java.net.Socket;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.las2mile.okio.OkIoClient;
+import org.las2mile.okio.message.BaseMsg;
+import org.las2mile.okio.OkIoClient.OnOkIoMsgListener;
 
 public class Scrcpy extends Service {
 
@@ -77,6 +79,14 @@ public class Scrcpy extends Service {
         thread.start();
         //手机-设备交互客户端
         OkIoClient okIoClient = new OkIoClient(serverAdr);
+        okIoClient.setOnOkIoMsgListener(new OnOkIoMsgListener() {
+            @Override
+            public void onOkIoMsg(BaseMsg msg) {
+                if (serviceCallbacks != null) {
+                    serviceCallbacks.onOkIoMsg(msg);
+                }
+            }
+        });
         okIoClient.start();
     }
 
@@ -245,6 +255,7 @@ public class Scrcpy extends Service {
 
     public interface ServiceCallbacks {
         void loadNewRotation();
+        void onOkIoMsg(BaseMsg msg);
     }
 
     public class MyServiceBinder extends Binder {

+ 36 - 19
app/src/main/java/org/las2mile/scrcpy/SendCommands.java

@@ -18,15 +18,18 @@ import java.security.NoSuchAlgorithmException;
 import java.security.spec.InvalidKeySpecException;
 import okio.Buffer;
 
+import android.os.Handler;
+
 public class SendCommands {
 
     private Thread thread = null;
     private Context context;
     private int status;
+    private Handler mHandler;
 
 
-    public SendCommands() {
-
+    public SendCommands(Handler handler) {
+        mHandler =  handler;
     }
 
     public static AdbBase64 getBase64Impl() {
@@ -77,24 +80,27 @@ public class SendCommands {
                 try {
                     adbWrite(ip,serverPort, fileBase64, command.toString());
                 } catch (IOException e) {
+                    mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_FAIL);
                     e.printStackTrace();
                 }
             }
         });
         thread.start();
-        int count = 0;
-        while (status == 1 && count < 100) {
-            Log.e("ADB", "adb Connecting..."+count);
-            try {
-                Thread.sleep(300);//等待app_process进程运行,app_process进程启动比较迟,故可以多休眠一段时间
-                count ++;
-            } catch (InterruptedException e) {
-                e.printStackTrace();
-            }
-        }
-        if(count == 100){
-            status = 2;
-        }
+
+        //原生逻辑
+        // int count = 0;
+        // while (status == 1 && count < 100) {
+        //     Log.e("ADB", "adb Connecting..."+count);
+        //     try {
+        //         Thread.sleep(300);//等待app_process进程运行,app_process进程启动比较迟,故可以多休眠一段时间
+        //         count ++;
+        //     } catch (InterruptedException e) {
+        //         e.printStackTrace();
+        //     }
+        // }
+        // if(count == 100){
+        //     status = 2;
+        // }
         return status;
     }
 
@@ -106,9 +112,11 @@ public class SendCommands {
         AdbStream stream = null;
 
         try {
+            Log.e("ADB", "adb setupCrypto...");
             crypto = setupCrypto();
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
+            mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_FAIL);
             return;
         } catch (IOException e) {
             e.printStackTrace();
@@ -116,22 +124,24 @@ public class SendCommands {
         }
 
         try {
+            Log.e("ADB", "adb new Socket..."+ ip + ":" + serverPort );
             sock = new Socket(ip, serverPort);
             Log.e("scrcpy"," ADB socket connection successful");
+            mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_SUC);
         } catch (UnknownHostException e) {
             status = 2;
             throw new UnknownHostException(ip + " is no valid ip address");
         } catch (ConnectException e) {
             status = 2;
-            throw new ConnectException("Device at " + ip + ":" + 5555 + " has no adb enabled or connection is refused");
+            throw new ConnectException("Device at " + ip + ":" + serverPort + " has no adb enabled or connection is refused");
         } catch (NoRouteToHostException e) {
             status = 2;
-            throw new NoRouteToHostException("Couldn't find adb device at " + ip + ":" + 5555);
+            throw new NoRouteToHostException("Couldn't find adb device at " + ip + ":" + serverPort);
         } catch (IOException e) {
             e.printStackTrace();
             status = 2;
         }
-
+        Log.e("ADB", "adb new Socket ok?..."+status+" "+(sock != null));
         if (sock != null && status ==1) {
             try {
                 adb = AdbConnection.create(sock, crypto);
@@ -140,6 +150,7 @@ public class SendCommands {
                 e.printStackTrace();
             } catch (IOException | InterruptedException e) {
                 e.printStackTrace();
+                mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_FAIL);
                 return;
             }
         }
@@ -151,6 +162,7 @@ public class SendCommands {
             } catch (IOException | InterruptedException e) {
                 e.printStackTrace();
                 status = 2;
+                mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_FAIL);
                 return;
             }
         }
@@ -160,6 +172,7 @@ public class SendCommands {
                 stream.write(" " + '\n');
             } catch (IOException | InterruptedException e) {
                 e.printStackTrace();
+                mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_FAIL);
                 return;
             }
         }
@@ -186,6 +199,8 @@ public class SendCommands {
             }
         }
 
+        Log.e("ADB", "adbb response ok");
+
         if (stream != null && status ==1) {
             int len = fileBase64.length;
             byte[] filePart = new byte[4056];
@@ -229,12 +244,14 @@ public class SendCommands {
             } catch (IOException | InterruptedException e) {
                 e.printStackTrace();
                 status =2;
+                mHandler.sendEmptyMessage(MainActivity.ADB_CONNECT_FAIL);
                 return;
             }
 	}
+        Log.e("ADB", "adb write jar end "+status);
         if (status ==1);
         status = 0;
-
+        mHandler.sendEmptyMessage(MainActivity.SERVER_ENV_PREPARED);
     }
 
 }