Skip to content

Commit c9e993c

Browse files
authored
Add drone login credential management (#90)
(cherry picked from commit 839a2b2)
1 parent 691c842 commit c9e993c

1 file changed

Lines changed: 96 additions & 1 deletion

File tree

app/src/main/java/com/openipc/pixelpilot/VideoActivity.java

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ public void run() {
112112
private ConstraintSet constraintSet;
113113
private WfbNgLink wfbLink;
114114

115+
private static final String PREF_DRONE_USERNAME = "drone_username";
116+
private static final String PREF_DRONE_PASSWORD = "drone_password";
117+
115118
public boolean getVRSetting() {
116119
return getSharedPreferences("general", Context.MODE_PRIVATE).getBoolean("vr-mode", false);
117120
}
@@ -908,6 +911,13 @@ private void setupDroneSubMenu(PopupMenu popup) {
908911
startBrowser();
909912
return true;
910913
});
914+
915+
// Add a new option to manage login credentials
916+
MenuItem loginCredentials = drone.add("Login Credentials");
917+
loginCredentials.setOnMenuItemClickListener(item -> {
918+
showLoginCredentialsDialog();
919+
return true;
920+
});
911921
}
912922

913923
/**
@@ -1529,6 +1539,88 @@ private void copyGSKey() {
15291539
}
15301540
}
15311541

1542+
private void showLoginCredentialsDialog() {
1543+
// Create a LinearLayout to hold our EditText fields
1544+
android.widget.LinearLayout layout = new android.widget.LinearLayout(this);
1545+
layout.setOrientation(android.widget.LinearLayout.VERTICAL);
1546+
layout.setPadding(50, 30, 50, 30); // Add some padding around the content
1547+
1548+
// EditText for username
1549+
final android.widget.EditText usernameEditText = new android.widget.EditText(this);
1550+
usernameEditText.setHint("Username");
1551+
usernameEditText.setText(getDroneUsername()); // Pre-fill with current saved username
1552+
layout.addView(usernameEditText);
1553+
1554+
// EditText for password
1555+
final android.widget.EditText passwordEditText = new android.widget.EditText(this);
1556+
passwordEditText.setHint("Password");
1557+
// Mask the password input
1558+
passwordEditText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
1559+
passwordEditText.setText(getDronePassword()); // Pre-fill with current saved password
1560+
layout.addView(passwordEditText);
1561+
1562+
// CheckBox to toggle password visibility
1563+
final android.widget.CheckBox showPasswordCheckBox = new android.widget.CheckBox(this);
1564+
showPasswordCheckBox.setText("Show Password");
1565+
layout.addView(showPasswordCheckBox);
1566+
1567+
// Set a listener for the CheckBox
1568+
showPasswordCheckBox.setOnCheckedChangeListener((buttonView, isChecked) -> {
1569+
if (isChecked) {
1570+
// If checked, show the password (visible_password)
1571+
passwordEditText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
1572+
} else {
1573+
// If unchecked, hide the password (password)
1574+
passwordEditText.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
1575+
}
1576+
// Move the cursor to the end of the text to prevent it from jumping to the beginning
1577+
passwordEditText.setSelection(passwordEditText.getText().length());
1578+
});
1579+
1580+
// Build and show the AlertDialog
1581+
new android.app.AlertDialog.Builder(this)
1582+
.setTitle("Drone Login Credentials")
1583+
.setView(layout) // Set our custom layout
1584+
.setPositiveButton("Save", (dialog, which) -> {
1585+
// Save the new values to SharedPreferences
1586+
setDroneUsername(usernameEditText.getText().toString());
1587+
setDronePassword(passwordEditText.getText().toString());
1588+
Toast.makeText(this, "Drone credentials saved.", Toast.LENGTH_SHORT).show();
1589+
})
1590+
.setNegativeButton("Cancel", (dialog, which) -> {
1591+
dialog.cancel(); // Dismiss the dialog
1592+
})
1593+
.show();
1594+
}
1595+
1596+
// Helper method to retrieve the drone username
1597+
// Provides a default "root" if not yet set, for initial compatibility.
1598+
private String getDroneUsername() {
1599+
return getSharedPreferences("general", Context.MODE_PRIVATE).getString(PREF_DRONE_USERNAME, "root");
1600+
}
1601+
1602+
// Helper method to save the drone username
1603+
private void setDroneUsername(String username) {
1604+
SharedPreferences prefs = getSharedPreferences("general", Context.MODE_PRIVATE);
1605+
SharedPreferences.Editor editor = prefs.edit();
1606+
editor.putString(PREF_DRONE_USERNAME, username);
1607+
editor.apply();
1608+
}
1609+
1610+
// Helper method to retrieve the drone password
1611+
// Provides a default "12345" if not yet set, for initial compatibility.
1612+
private String getDronePassword() {
1613+
return getSharedPreferences("general", Context.MODE_PRIVATE).getString(PREF_DRONE_PASSWORD, "12345");
1614+
}
1615+
1616+
// Helper method to save the drone password
1617+
private void setDronePassword(String password) {
1618+
SharedPreferences prefs = getSharedPreferences("general", Context.MODE_PRIVATE);
1619+
SharedPreferences.Editor editor = prefs.edit();
1620+
editor.putString(PREF_DRONE_PASSWORD, password);
1621+
editor.apply();
1622+
}
1623+
15321624
@SuppressLint("SetJavaScriptEnabled")
15331625
public void startBrowser() {
15341626
WebView view = new WebView(this);
@@ -1552,7 +1644,10 @@ public void startBrowser() {
15521644
@Override
15531645
public void onReceivedHttpAuthRequest(
15541646
WebView view, HttpAuthHandler handler, String host, String realm) {
1555-
handler.proceed("root", "12345");
1647+
// Retrieve username and password from SharedPreferences
1648+
String username = getDroneUsername();
1649+
String password = getDronePassword();
1650+
handler.proceed(username, password);
15561651
}
15571652
});
15581653
}

0 commit comments

Comments
 (0)