feat: 增加密码连接
This commit is contained in:
@@ -15,6 +15,8 @@ import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.RadioGroup;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -62,6 +64,8 @@ public class MainActivity extends AppCompatActivity {
|
||||
private final Gson gson = new Gson();
|
||||
private String myDeviceId;
|
||||
private String targetDeviceId;
|
||||
private String pendingAuthType;
|
||||
private String pendingAuthValue;
|
||||
|
||||
private final Handler statsHandler = new Handler(Looper.getMainLooper());
|
||||
private final Runnable statsRunnable = new Runnable() {
|
||||
@@ -105,7 +109,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
myDeviceId = DeviceUtils.getSerialNumber(this);
|
||||
etDeviceId.setText(myDeviceId);
|
||||
|
||||
btnConnect.setOnClickListener(v -> connectToControlled());
|
||||
btnConnect.setOnClickListener(v -> showAuthDialog());
|
||||
btnDisconnect.setOnClickListener(v -> disconnect());
|
||||
|
||||
// 初始化 EGL 和远端视频渲染
|
||||
@@ -210,6 +214,56 @@ public class MainActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
|
||||
/** 弹出鉴权输入对话框:选择动态验证码或固定密码,输入后发起连接。 */
|
||||
private void showAuthDialog() {
|
||||
String serverUrl = etServerUrl.getText().toString().trim();
|
||||
String target = etTargetDeviceId.getText().toString().trim();
|
||||
String myId = etDeviceId.getText().toString().trim();
|
||||
if (serverUrl.isEmpty() || target.isEmpty() || myId.isEmpty()) {
|
||||
Toast.makeText(this, "请先填写服务器地址、设备ID和目标设备ID", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
View view = getLayoutInflater().inflate(R.layout.dialog_auth_input, null);
|
||||
RadioGroup rg = view.findViewById(R.id.rg_auth_type);
|
||||
EditText et = view.findViewById(R.id.et_auth_value);
|
||||
TextView tip = view.findViewById(R.id.tv_auth_tip);
|
||||
|
||||
rg.setOnCheckedChangeListener((group, checkedId) -> {
|
||||
if (checkedId == R.id.rb_password) {
|
||||
et.setHint("请输入固定密码");
|
||||
tip.setText("固定密码为被控端设置的固定密码");
|
||||
} else {
|
||||
et.setHint("请输入动态验证码");
|
||||
tip.setText("动态验证码为被控端界面显示的 6 位大小写字母与数字组合");
|
||||
}
|
||||
});
|
||||
|
||||
AlertDialog dialog = new AlertDialog.Builder(this)
|
||||
.setTitle("连接鉴权")
|
||||
.setView(view)
|
||||
.setNegativeButton("取消", null)
|
||||
.setPositiveButton("连接", null)
|
||||
.create();
|
||||
|
||||
// 校验失败时不关闭对话框,引导用户重新输入
|
||||
dialog.setOnShowListener(d -> dialog.getButton(AlertDialog.BUTTON_POSITIVE)
|
||||
.setOnClickListener(v -> {
|
||||
boolean isPassword = (rg.getCheckedRadioButtonId() == R.id.rb_password);
|
||||
String authType = isPassword ? "PASSWORD" : "CODE";
|
||||
String authValue = et.getText().toString().trim();
|
||||
if (authValue.isEmpty()) {
|
||||
Toast.makeText(MainActivity.this, "请输入验证码或密码", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
pendingAuthType = authType;
|
||||
pendingAuthValue = authValue;
|
||||
dialog.dismiss();
|
||||
connectToControlled();
|
||||
}));
|
||||
dialog.show();
|
||||
}
|
||||
|
||||
private void connectToControlled() {
|
||||
String serverUrl = etServerUrl.getText().toString().trim();
|
||||
targetDeviceId = etTargetDeviceId.getText().toString().trim();
|
||||
@@ -283,7 +337,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
});
|
||||
}
|
||||
});
|
||||
webRtcClient.createOffer(targetDeviceId, remoteVideoView);
|
||||
webRtcClient.createOffer(targetDeviceId, remoteVideoView, pendingAuthType, pendingAuthValue);
|
||||
}
|
||||
|
||||
private void handleSignalMessage(SignalMessage message) {
|
||||
@@ -330,17 +384,29 @@ public class MainActivity extends AppCompatActivity {
|
||||
*/
|
||||
private void handleConnectionRejected(SignalMessage message) {
|
||||
String targetId = message.getToDeviceId();
|
||||
String payload = message.getPayload();
|
||||
String text = (payload != null && !payload.isEmpty())
|
||||
? payload
|
||||
: "目标被控端拒绝了连接请求";
|
||||
String finalText = text;
|
||||
String reason = parseReason(message.getPayload());
|
||||
String finalText = reason;
|
||||
runOnUiThread(() -> {
|
||||
tvStatus.setText("状态: " + finalText);
|
||||
Toast.makeText(MainActivity.this, finalText, Toast.LENGTH_LONG).show();
|
||||
updateUI(false);
|
||||
});
|
||||
Log.w(TAG, "Connection request to " + targetId + " was rejected by controlled device");
|
||||
Log.w(TAG, "Connection request to " + targetId + " was rejected: " + reason);
|
||||
}
|
||||
|
||||
/** 解析拒绝原因:优先读取 JSON 中的 reason 字段,否则直接返回原文。 */
|
||||
private String parseReason(String payload) {
|
||||
if (payload == null || payload.isEmpty()) {
|
||||
return "目标被控端拒绝了连接请求";
|
||||
}
|
||||
try {
|
||||
JsonObject obj = gson.fromJson(payload, JsonObject.class);
|
||||
if (obj != null && obj.has("reason") && !obj.get("reason").isJsonNull()) {
|
||||
return obj.get("reason").getAsString();
|
||||
}
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user