package org.las2mile.scrcpy; import android.annotation.SuppressLint; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.content.pm.ActivityInfo; import android.content.res.AssetManager; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.os.Bundle; import android.os.IBinder; import android.os.SystemClock; import android.util.Base64; import android.util.DisplayMetrics; import android.util.Log; import android.view.Display; import android.view.Surface; import android.view.SurfaceView; import android.view.View; import android.view.ViewConfiguration; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.Switch; import android.widget.Toast; import java.io.IOException; import java.io.InputStream; import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; 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; import org.las2mile.okio.message.resp.LogFileEndMsgResp; import org.las2mile.okio.message.resp.LogFileMsgResp; import android.widget.TextView; import java.util.Set; import androidx.fragment.app.FragmentActivity; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.net.Uri; import android.provider.Settings; import android.graphics.Color; import org.las2mile.okio.utils.ZipUtils; import com.lzf.easyfloat.EasyFloat; import com.lzf.easyfloat.enums.SidePattern; import android.view.Gravity; import android.widget.ImageView; import org.las2mile.ui.DownloadTaskDialog; import org.las2mile.ui.MessageEvent; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.las2mile.okio.message.req.LogFileMsgReq; import android.text.TextUtils; import com.hjq.permissions.OnPermissionCallback; import com.hjq.permissions.Permission; import com.hjq.permissions.XXPermissions; import java.util.List; import com.hjq.toast.Toaster; import com.blankj.utilcode.util.BarUtils; import org.las2mile.okio.utils.SystemUtils; import android.widget.CheckBox; import android.os.Build; import java.util.ArrayList; import com.blankj.utilcode.util.NetworkUtils; import com.tbruyelle.rxpermissions2.RxPermissions; import android.view.Window; import android.view.WindowManager; public class MainActivity extends FragmentActivity implements Scrcpy.ServiceCallbacks, SensorEventListener { private static final String PREFERENCE_KEY = "default"; private static final String PREFERENCE_SPINNER_RESOLUTION = "spinner_resolution"; private static final String PREFERENCE_SPINNER_BITRATE = "spinner_bitrate"; private static int screenWidth; private static int screenHeight; private static boolean landscape = false; private static boolean first_time = true; private static boolean result_of_Rotation = false; private static boolean serviceBound = false; private static boolean nav = false; SensorManager sensorManager; private SendCommands sendCommands; private int videoBitrate; private String local_ip; private Context context; private String serverAdr = null; private String serverPort = null; private SurfaceView surfaceView; private Surface surface; private Scrcpy scrcpy; private long timestamp = 0; private byte[] fileBase64; private static float remote_device_width; private static float remote_device_height; private LinearLayout linearLayout; private static boolean no_control = false; private RxPermissions rxPermissions; 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; static final int GOT_REMOTE_LOG_PROCEDURE = 8; static final int GOT_REMOTE_LOG_PROCEDURE_END = 9; static final int GOT_REMOTE_LOG_PROCEDURE_START = 10; private static final String LOG_TAG = "TelephonyManagerSub"; private static final boolean DISABLE_BG_RUNNING = true; private static final boolean ENABLE_SPEED_UP_I_FRAME_FRESH_INTERVAL = false; Handler mainHandler = new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message message) { int what = message.what; Log.e(LOG_TAG," handle message "+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(); } //原型代码可以删掉 /* else if(what == GOT_REMOTE_VERSION){ hideProgress(); HartBeatMsgResp hartBeatMsgResp = (HartBeatMsgResp)message.obj; Toast.makeText(context, hartBeatMsgResp.remoteInfo, Toast.LENGTH_SHORT).show(); TextView tv_remote_device_info = findViewById(R.id.tv_remote_device_info); String remoteVer = String.format(getResources().getString(R.string.str_remote_dev_info),hartBeatMsgResp.remoteInfo); tv_remote_device_info.setText(remoteVer); showFloatViewWhenRender(); } else if(what == GOT_REMOTE_LOG_PROCEDURE){ String str_save_log_to_local = (String)message.obj; TextView tv_save_log_to_local = findViewById(R.id.tv_save_log_to_local); tv_save_log_to_local.setText(str_save_log_to_local); } else if (what == GOT_REMOTE_LOG_PROCEDURE_END){ Boolean ok = (Boolean)message.obj; String str_remote_log_dlst = String.format(getResources().getString(R.string.str_remote_log_dlst),ok ? "成功":"失败"); TextView tv_remote_log_dlst = findViewById(R.id.tv_remote_log_dlst); tv_remote_log_dlst.setTextColor(ok ? Color.BLUE : Color.RED); tv_remote_log_dlst.setText(str_remote_log_dlst); } else if (what == GOT_REMOTE_LOG_PROCEDURE_START){ TextView tv_remote_log_dlst = findViewById(R.id.tv_remote_log_dlst); tv_remote_log_dlst.setTextColor(Color.GRAY); String str_remote_log_dlst = String.format(getResources().getString(R.string.str_remote_log_dlst),"下载中"); tv_remote_log_dlst.setText(str_remote_log_dlst); }*/ return false; } }); private void initDownloadBtn(){ // final Button btn_dl_status = findViewById(R.id.btn_dl_status); // btn_dl_status.setOnClickListener(v -> { // Message msg = new Message(); // msg.what = GOT_REMOTE_LOG_PROCEDURE_START; // mainHandler.sendMessage(msg); // scrcpy.downloadLog(); // }); // final Button btn_dl_imgs = findViewById(R.id.btn_dl_imgs); // btn_dl_imgs.setOnClickListener(v -> { // Message msg = new Message(); // msg.what = GOT_REMOTE_LOG_PROCEDURE_START; // mainHandler.sendMessage(msg); // scrcpy.downloadPics(); // }); } private final ServiceConnection serviceConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName componentName, IBinder iBinder) { scrcpy = ((Scrcpy.MyServiceBinder) iBinder).getService(); scrcpy.setServiceCallbacks(MainActivity.this); serviceBound = true; if (first_time) { scrcpy.start(surface, serverAdr, screenHeight, screenWidth); int count = 100; while (count!=0 && !scrcpy.check_socket_connection()){ count --; try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } if (count == 0){ if (serviceBound) { scrcpy.StopService(); unbindService(serviceConnection); serviceBound = false; scrcpy_main(); } Toast.makeText(context, serviceBound+" Connection Timed out", Toast.LENGTH_SHORT).show(); //连接超时 mainHandler.sendEmptyMessage(ADB_CONNECT_FAIL); scrcpy.StopService(); }else{ int[] rem_res = scrcpy.get_remote_device_resolution(); remote_device_height = rem_res[1]; remote_device_width = rem_res[0]; first_time = false; } } else { scrcpy.setParms(surface, screenWidth, screenHeight); } set_display_nd_touch(); } @Override public void onServiceDisconnected(ComponentName componentName) { serviceBound = false; } }; public MainActivity() { } @SuppressLint("SourceLockedOrientationActivity") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); Log.e(LOG_TAG,"手机SDK版本:"+Build.VERSION.SDK_INT); XXPermissions xXPermissions = XXPermissions.with(this); xXPermissions.permission(Permission.MANAGE_EXTERNAL_STORAGE); if (Build.VERSION.SDK_INT < 33/*Build.VERSION_CODES.TIRAMISU*/) { xXPermissions.permission(Permission.ACCESS_FINE_LOCATION); } else { xXPermissions.permission(Permission.ACCESS_FINE_LOCATION); xXPermissions.permission(Permission.NEARBY_WIFI_DEVICES); } xXPermissions.request(new OnPermissionCallback() { @Override public void onGranted( List permissions, boolean allGranted) { if (!allGranted) { toast("获取部分权限成功,但部分权限未正常授予"); return; } //toast("获取SD卡权限成功"); } @Override public void onDenied( List permissions, boolean doNotAskAgain) { if (doNotAskAgain) { toast("被永久拒绝授权,请手动授予SD卡权限"); // 如果是被永久拒绝就跳转到应用权限系统设置页面 XXPermissions.startPermissionActivity(context, permissions); } else { toast("获取SD卡权限失败"); } } }); View decorView = getWindow().getDecorView(); decorView.setOnSystemUiVisibilityChangeListener(visibility -> BarUtils.setNavBarVisibility(MainActivity.this, false)); BarUtils.setStatusBarVisibility(MainActivity.this, false); if (first_time) { scrcpy_main(); } else { this.context = this; start_screen_copy_magic(); } sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE); Sensor proximity; proximity = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); sensorManager.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL); } @Override public void onOkIoMsg(BaseMsg baseMsg){ if(baseMsg instanceof LogFileMsgResp){ //原型代码可以删掉 // LogFileMsgResp logFileMsgResp = (LogFileMsgResp)baseMsg; // String sizeFmt = ZipUtils.formetFileSize(logFileMsgResp.fileSize); // String percentStr = logFileMsgResp.writenPercent+"%";//Math.round((logFileMsgResp.pktNo+1)*1.0f/logFileMsgResp.pktNums * 100)+"%"; // String str_save_log_to_local = String.format(getResources().getString(R.string.str_save_log_to_local),logFileMsgResp.zipPath,sizeFmt,percentStr,""); // Message msg = new Message(); // msg.what = GOT_REMOTE_LOG_PROCEDURE; // msg.obj = str_save_log_to_local; // mainHandler.sendMessage(msg); } else if(baseMsg instanceof HartBeatMsgResp){ Message msg = new Message(); msg.what = GOT_REMOTE_VERSION; msg.obj = baseMsg; mainHandler.sendMessage(msg); } else if(baseMsg instanceof LogFileEndMsgResp){ //原型代码可以删掉 // LogFileEndMsgResp logFileEndMsgResp = (LogFileEndMsgResp)baseMsg; // Message msg = new Message(); // msg.what = GOT_REMOTE_LOG_PROCEDURE_END; // msg.obj = logFileEndMsgResp.allDownloaded; // mainHandler.sendMessage(msg); } } @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { //Log.e("Scrcpy: ","activity DownloadTaskDialog onMessageEvent "+event.what); if(event.what == MessageEvent.EVENT_GOT_REMOTE_VERSION){ Log.e(LOG_TAG,"onMessageEvent "+landscape); if(!landscape){//机器是竖屏的,在此显示悬浮窗 showFloatViewWhenRender(); } } else if(event.what == MessageEvent.EVENT_CLICK_IMG_BTN){ scrcpy.downloadPics(); } else if(event.what == MessageEvent.EVENT_CLICK_LOG_BTN){ scrcpy.downloadLog(); } } private void showProgress(){ Log.e(LOG_TAG,"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(){ if(mainProgressDialog != null && mainProgressDialog.isShowing()){ mainProgressDialog.dismiss(); } } @SuppressLint("SourceLockedOrientationActivity") public void scrcpy_main(){ setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Log.e(LOG_TAG,"scrcpy_main"); setContentView(R.layout.activity_main); final Button startButton = findViewById(R.id.button_start); AssetManager assetManager = getAssets(); try { InputStream input_Stream = assetManager.open("scrcpy-server.jar"); byte[] buffer = new byte[input_Stream.available()]; input_Stream.read(buffer); fileBase64 = Base64.encode(buffer, 2); } catch (IOException e) { Log.e(LOG_TAG,"Asset Manager "+e.getMessage()); } sendCommands = new SendCommands(mainHandler); // if(!SystemUtils.isDateValid()){ // startButton.setEnabled(false); // ((TextView)findViewById(R.id.tv_tip_info)).setText("软件需升级。"); // toast("软件需升级。"); // } else { // SystemUtils.enableStartLegalNotice(MainActivity.this,startButton); // } startButton.setOnClickListener(v -> { boolean isWifiOk = SystemUtils.isHotSpot(MainActivity.this); TextView tv_tip_info = findViewById(R.id.tv_tip_info); if(!isWifiOk){ tv_tip_info.setText("请先将手机连接到恺恩设备的wifi热点。"); return; }else { tv_tip_info.setText(""); } local_ip = wifiIpAddress(); getAttributes(); if ((!serverAdr.isEmpty())&& (!TextUtils.isEmpty(serverPort))) { //原生逻辑 // 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 Port Empty", Toast.LENGTH_SHORT).show(); } }); get_saved_preferences(); } public void get_saved_preferences(){ this.context = this; final EditText editTextServerHost = findViewById(R.id.editText_server_host); final Switch aSwitch0 = findViewById(R.id.switch0); final Switch aSwitch1 = findViewById(R.id.switch1); final EditText editTextServerPort = findViewById(R.id.editText_server_port); editTextServerHost.setText(context.getSharedPreferences(PREFERENCE_KEY, 0).getString("Server Address", "")); editTextServerPort.setText(context.getSharedPreferences(PREFERENCE_KEY, 0).getString("Server Port", "")); //设置默认端口 if(TextUtils.isEmpty(context.getSharedPreferences(PREFERENCE_KEY, 0).getString("Server Port", ""))){ editTextServerPort.setText("15555"); } aSwitch0.setChecked(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("No Control", false)); aSwitch1.setChecked(context.getSharedPreferences(PREFERENCE_KEY, 0).getBoolean("Nav Switch", false)); setSpinner(R.array.options_resolution_keys, R.id.spinner_video_resolution, PREFERENCE_SPINNER_RESOLUTION); setSpinner(R.array.options_bitrate_keys, R.id.spinner_video_bitrate, PREFERENCE_SPINNER_BITRATE); if(aSwitch0.isChecked()){ aSwitch1.setVisibility(View.GONE); } aSwitch0.setOnClickListener(v -> { if(aSwitch0.isChecked()){ aSwitch1.setVisibility(View.GONE); }else{ aSwitch1.setVisibility(View.VISIBLE); } }); TextView app_ver_right = (TextView)findViewById(R.id.app_ver_right); String str_ver_right = String.format(getResources().getString(R.string.str_ver_right),BuildConfig.BUILD_TIME,BuildConfig.VALID_DAYS); app_ver_right.setText(str_ver_right); } @SuppressLint("ClickableViewAccessibility") public void set_display_nd_touch() { DisplayMetrics metrics = new DisplayMetrics(); if (ViewConfiguration.get(context).hasPermanentMenuKey()) { getWindowManager().getDefaultDisplay().getMetrics(metrics); } else { final Display display = getWindowManager().getDefaultDisplay(); display.getRealMetrics(metrics); } float this_dev_height = metrics.heightPixels; float this_dev_width = metrics.widthPixels; if (nav && !no_control){ if (landscape){ this_dev_width = this_dev_width - 96; }else { //100 is the height of nav bar but need multiples of 8. this_dev_height = this_dev_height - 96; } } float remote_device_aspect_ratio = remote_device_height/remote_device_width; Log.e(LOG_TAG,"set_display_nd_touch "+landscape); //改变渲染的边距 if (!landscape) { //Portrait float this_device_aspect_ratio = this_dev_height/this_dev_width; if (remote_device_aspect_ratio > this_device_aspect_ratio) { linearLayout.setPadding((int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_width)/2),0,(int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_width)/2),0); } else if (remote_device_aspect_ratio < this_device_aspect_ratio) { linearLayout.setPadding(0,(int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_width)/2),0,(int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_width)/2)); } }else{ //Landscape float this_device_aspect_ratio = this_dev_width/this_dev_height; if (remote_device_aspect_ratio > this_device_aspect_ratio) { linearLayout.setPadding(0,(int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_height)/2),0,(int) (((remote_device_aspect_ratio - this_device_aspect_ratio)*this_dev_height)/2)); } else if (remote_device_aspect_ratio < this_device_aspect_ratio) { linearLayout.setPadding(((int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_height))/2),0,((int) (((this_device_aspect_ratio - remote_device_aspect_ratio)*this_dev_height))/2),0); } showFloatViewWhenRender(); //机器是横屏的,在此处显示悬浮窗 } if (!no_control) { surfaceView.setOnTouchListener((v, event) -> scrcpy.touchevent(event, surfaceView.getWidth(), surfaceView.getHeight())); } if (nav && !no_control) { final Button backButton = findViewById(R.id.back_button); final Button homeButton = findViewById(R.id.home_button); final Button appswitchButton = findViewById(R.id.appswitch_button); if(backButton != null){ backButton.setOnClickListener(v -> scrcpy.sendKeyevent(4)); } if(homeButton != null){ homeButton.setOnClickListener(v -> scrcpy.sendKeyevent(3)); } if(appswitchButton != null){ appswitchButton.setOnClickListener(v -> scrcpy.sendKeyevent(187)); } final Button debug_button = findViewById(R.id.debug_button); if(debug_button != null){ debug_button.setOnClickListener(v -> showDownloadDialog()); } } } private void showDownloadDialog(){ DownloadTaskDialog dialog = new DownloadTaskDialog(this); dialog.show(); } public void toast(CharSequence text) { Toaster.show(text); } private void setSpinner(final int textArrayOptionResId, final int textViewResId, final String preferenceId) { final Spinner spinner = findViewById(textViewResId); ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(this, textArrayOptionResId, android.R.layout.simple_spinner_item); arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(arrayAdapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putInt(preferenceId, position).apply(); } @Override public void onNothingSelected(AdapterView parent) { context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putInt(preferenceId, 0).apply(); } }); spinner.setSelection(context.getSharedPreferences(PREFERENCE_KEY, 0).getInt(preferenceId, 0)); } private void getAttributes() { final EditText editTextServerHost = findViewById(R.id.editText_server_host); serverAdr = editTextServerHost.getText().toString(); //Log.e("Scrcpy1","getAttributes "+serverAdr); if(TextUtils.isEmpty(serverAdr)){ serverAdr = "192.168.43.1"; } final EditText editTextServerPort = findViewById(R.id.editText_server_port); serverPort = editTextServerPort.getText().toString(); context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putString("Server Port", serverPort).apply(); context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putString("Server Address", serverAdr).apply(); final Spinner videoResolutionSpinner = findViewById(R.id.spinner_video_resolution); final Spinner videoBitrateSpinner = findViewById(R.id.spinner_video_bitrate); final Switch a_Switch0 = findViewById(R.id.switch0); no_control = a_Switch0.isChecked(); final Switch a_Switch1 = findViewById(R.id.switch1); nav = a_Switch1.isChecked(); nav = true;//默认打开导航栏 context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putBoolean("No Control", no_control).apply(); context.getSharedPreferences(PREFERENCE_KEY, 0).edit().putBoolean("Nav Switch", nav).apply(); final String[] videoResolutions = getResources().getStringArray(R.array.options_resolution_values)[videoResolutionSpinner.getSelectedItemPosition()].split(","); screenHeight = Integer.parseInt(videoResolutions[0]); screenWidth = Integer.parseInt(videoResolutions[1]); videoBitrate = getResources().getIntArray(R.array.options_bitrate_values)[videoBitrateSpinner.getSelectedItemPosition()]; } private void swapDimensions() { int temp = screenHeight; screenHeight = screenWidth; screenWidth = temp; } @SuppressLint("ClickableViewAccessibility") private void start_screen_copy_magic() { Log.e(LOG_TAG,"start_screen_copy_magic"); setContentView(R.layout.surface);//开始渲染 final View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); surfaceView = findViewById(R.id.decoder_surface); surface = surfaceView.getHolder().getSurface(); final LinearLayout nav_bar = findViewById(R.id.nav_button_bar); if(nav && !no_control) { nav_bar.setVisibility(LinearLayout.VISIBLE); }else { nav_bar.setVisibility(LinearLayout.GONE); } linearLayout = findViewById(R.id.container1); start_Scrcpy_service(); initDownloadBtn();//loadNewRotation 屏幕旋转需要重新初始化 } protected String wifiIpAddress() { //https://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device-from-code try { InetAddress ipv4 = null; InetAddress ipv6 = null; for (Enumeration en = NetworkInterface .getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface int_f = en.nextElement(); for (Enumeration enumIpAddr = int_f .getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (inetAddress instanceof Inet6Address) { ipv6 = inetAddress; continue; } if (inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { ipv4 = inetAddress; continue; } return inetAddress.getHostAddress(); } } if (ipv6 != null) { return ipv6.getHostAddress(); } if (ipv4 != null) { return ipv4.getHostAddress(); } return null; } catch (SocketException ex) { ex.printStackTrace(); } return null; } private void start_Scrcpy_service() { Intent intent = new Intent(this, Scrcpy.class); startService(intent); bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE); } @SuppressLint("SourceLockedOrientationActivity") @Override public void loadNewRotation() { hideProgress();//横屏机器需要在此隐藏进度对话框 Log.e(LOG_TAG,"loadNewRotation "+first_time); if (first_time){ int[] rem_res = scrcpy.get_remote_device_resolution(); remote_device_height = rem_res[1]; remote_device_width = rem_res[0]; first_time = false; } unbindService(serviceConnection); serviceBound = false; result_of_Rotation = true; landscape = !landscape; swapDimensions(); if (landscape) { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); } else { setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); } } @Override protected void onPause() { super.onPause(); Log.e(LOG_TAG,"onPause "+first_time +" result_of_Rotation "+result_of_Rotation); EventBus.getDefault().unregister(this); if (serviceBound) { scrcpy.pause(); } if(DISABLE_BG_RUNNING && !result_of_Rotation){ boolean isGranted = SystemUtils.isGranted(this); Log.e(LOG_TAG,isGranted+" onPause bg "+serviceBound); if (serviceBound) { scrcpy.StopService(); unbindService(serviceConnection); android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } } } @Override protected void onResume() { super.onResume(); Log.e(LOG_TAG,"onResume "+first_time); EventBus.getDefault().register(this); boolean isGranted = SystemUtils.isGranted(this); if(!isGranted){ toast(" 请分配SD卡读写与WiFi访问权限"); } if (!first_time && !result_of_Rotation) { final View decorView = getWindow().getDecorView(); decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); if (serviceBound) { linearLayout = findViewById(R.id.container1); scrcpy.resume(); if(ENABLE_SPEED_UP_I_FRAME_FRESH_INTERVAL){ scrcpy.sendKeyevent(187); } } } result_of_Rotation = false; } @Override public void onBackPressed() { if (timestamp == 0) { timestamp = SystemClock.uptimeMillis(); Toast.makeText(context, "Press again to exit", Toast.LENGTH_SHORT).show(); } else { long now = SystemClock.uptimeMillis(); if (now < timestamp + 1000) { timestamp = 0; if (serviceBound) { scrcpy.StopService(); unbindService(serviceConnection); } android.os.Process.killProcess(android.os.Process.myPid()); System.exit(1); } timestamp = 0; } } @Override public void onSensorChanged(SensorEvent sensorEvent) { if (sensorEvent.sensor.getType() == Sensor.TYPE_PROXIMITY) { if (sensorEvent.values[0] == 0) { if (serviceBound) { scrcpy.sendKeyevent(28); } } else { if (serviceBound) { scrcpy.sendKeyevent(29); } } } } @Override public void onAccuracyChanged(Sensor sensor, int i) { } private void showPermissionDeniedDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("请前往设置界面打开SD卡读写权限"); builder.setPositiveButton("去设置", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:"+getPackageName())); intent.addCategory(Intent.CATEGORY_DEFAULT); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); } }); builder.setNegativeButton("取消",new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }); builder.show(); } private void showFloatViewWhenRender() { // Log.e("Scrcpy1","showFloatViewWhenRender "+first_time); // String tag = "trans"; // EasyFloat.with(this).setTag(tag).setLayout(R.layout.float_layout,view->{ // view.findViewById(R.id.img_show_content).setOnClickListener(view1 ->{ // //EasyFloat.dismiss(tag, true); // DownloadTaskDialog dialog = new DownloadTaskDialog(this); // dialog.show(); // }); // }).setGravity(Gravity.CENTER|Gravity.START).show(); } }