1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
package com.example.mylauncher;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;
import android.widget.ListView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.net.Uri;
import android.net.wifi.WifiManager;
import android.net.wifi.ScanResult;
import android.content.Context;
import android.Manifest;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.net.NetworkRequest;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiNetworkSpecifier;
import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration;
import android.os.Build;
import android.text.InputType;
import android.annotation.TargetApi;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.AdapterView;
import androidx.annotation.RequiresApi;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import java.util.Set;
import java.util.HashSet;
public class MainActivity extends Activity {
private ListView menuList;
private LinearLayout menuContainer;
private EditText searchBar;
private TextView itemCount;
private List<String> menuItems;
private boolean isInAppsMenu = false;
private boolean isInPowerMenu = false;
private WifiManager wifiManager;
private List<String> wifiNetworks;
private boolean isInWifiMenu = false;
private static final int WIFI_PERMISSION_REQUEST = 100;
private List<AppInfo> appInfoList; // All available apps
private List<AppInfo> filteredAppInfoList; // Apps after filtering
private static final String[] DEFAULT_MENU = {
"💻 t-mode",
"📱 Apps",
"📂 Files",
"Web Browser",
"📞 Dialer",
"✉️ Texts",
"👤 Contacts",
"📷 Camera",
"🌐 Networks",
"🔊 Audio",
"⚙️ Config",
"⚡ Power",
"❌ Close Menu"
};
@Override
public void onBackPressed() {
// Do nothing to disable the back button
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menuContainer = findViewById(R.id.menu_container);
menuList = findViewById(R.id.menu_list);
searchBar = findViewById(R.id.search_bar);
itemCount = findViewById(R.id.item_count);
if (menuContainer == null || menuList == null ||
searchBar == null || itemCount == null) {
finish();
return;
}
menuItems = new ArrayList<>(Arrays.asList(DEFAULT_MENU));
loadApps();
showMainMenu();
searchBar.addTextChangedListener(new SearchTextWatcher(this));
menuList.requestFocus(); // Ensure the ListView has focus
// Replace the previous EditText listeners with these new ones
searchBar.setOnEditorActionListener((v, actionId, event) -> {
// Handle both hardware and software keyboard Enter presses
if (event != null || actionId == EditorInfo.IME_ACTION_DONE ||
actionId == EditorInfo.IME_ACTION_GO ||
actionId == EditorInfo.IME_ACTION_SEARCH) {
handleEnterKeyPress();
return true;
}
return false;
});
// Add key listener for hardware keyboard
searchBar.setOnKeyListener((v, keyCode, event) -> {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER) {
handleEnterKeyPress();
return true;
}
return false;
});
}
public void filterList(String query) {
List<String> filteredNames = new ArrayList<>();
if (isInAppsMenu && appInfoList != null) {
filteredAppInfoList = new ArrayList<>();
for (AppInfo appInfo : appInfoList) {
if (appInfo.name.toLowerCase().contains(query)) {
filteredAppInfoList.add(appInfo);
filteredNames.add(appInfo.name);
}
}
filteredNames.add("Back");
} else if (isInPowerMenu) {
// For simplicity, we won't filter the power menu options
filteredNames = Arrays.asList(
"Lock",
"Reboot",
"Shutdown",
"Back"
);
} else if (isInWifiMenu) {
// Filter Wi-Fi networks
filteredNames = new ArrayList<>();
for (String network : wifiNetworks) {
if (network.toLowerCase().contains(query)) {
filteredNames.add(network);
}
}
filteredNames.add("Back");
} else {
filteredAppInfoList = null; // Not applicable
for (String item : menuItems) {
if (item.toLowerCase().contains(query)) {
filteredNames.add(item);
}
}
}
setMenuItems(filteredNames);
}
private void setMenuItems(List<String> items) {
CustomAdapter adapter = new CustomAdapter(this, items);
menuList.setAdapter(adapter);
updateCount();
// Update the OnItemClickListener
if (isInAppsMenu) {
menuList.setOnItemClickListener(new AppsMenuItemClickListener(this));
} else if (isInPowerMenu) {
menuList.setOnItemClickListener(new PowerMenuItemClickListener(this));
} else if (isInWifiMenu) {
menuList.setOnItemClickListener(new WifiMenuItemClickListener(this));
} else {
menuList.setOnItemClickListener(new MainMenuItemClickListener(this));
}
// Set the first item as selected by default
menuList.setSelection(0);
menuList.setItemChecked(0, true);
}
private void loadApps() {
try {
PackageManager pm = getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
appInfoList = new ArrayList<>();
for (ResolveInfo resolveInfo : resolveInfos) {
String name = resolveInfo.loadLabel(pm).toString();
appInfoList.add(new AppInfo(name, resolveInfo));
}
} catch (Exception e) {
appInfoList = new ArrayList<>();
}
}
public void showMainMenu() {
isInAppsMenu = false;
isInPowerMenu = false;
isInWifiMenu = false;
setMenuItems(menuItems);
searchBar.setText("");
}
private void showAppMenu() {
if (appInfoList == null || appInfoList.isEmpty()) {
Toast.makeText(this, "No apps found", Toast.LENGTH_SHORT).show();
return;
}
isInAppsMenu = true;
isInPowerMenu = false;
isInWifiMenu = false;
filteredAppInfoList = new ArrayList<>(appInfoList); // Start with all apps
List<String> appNames = new ArrayList<>();
for (AppInfo appInfo : filteredAppInfoList) {
appNames.add(appInfo.name);
}
appNames.add("Back");
setMenuItems(appNames);
searchBar.setText("");
}
private void showPowerMenu() {
isInPowerMenu = true;
isInAppsMenu = false;
isInWifiMenu = false;
List<String> powerOptions = Arrays.asList(
"Lock",
"Reboot",
"Shutdown",
"Back"
);
setMenuItems(powerOptions);
searchBar.setText("");
}
private void showWifiMenu() {
if (checkWifiPermissions()) {
scanWifiNetworks();
}
}
private boolean checkWifiPermissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
WIFI_PERMISSION_REQUEST);
return false;
}
return true;
}
private void scanWifiNetworks() {
wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
wifiManager.setWifiEnabled(true);
}
Set<String> wifiNetworkSet = new HashSet<>();
wifiManager.startScan(); // Start a new scan
List<ScanResult> scanResults = wifiManager.getScanResults();
for (ScanResult scanResult : scanResults) {
if (scanResult.SSID != null && !scanResult.SSID.isEmpty()) {
wifiNetworkSet.add(scanResult.SSID);
}
}
wifiNetworks = new ArrayList<>(wifiNetworkSet);
isInWifiMenu = true;
isInAppsMenu = false;
isInPowerMenu = false;
setMenuItems(wifiNetworks);
searchBar.setText("");
}
public void connectToWifi(String ssid) {
final String finalSsid = ssid; // Declare ssid as final
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Connect to " + finalSsid);
// Set up the input
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
input.setHint("Password");
builder.setView(input);
// Set up the buttons
builder.setPositiveButton("Connect", new ConnectButtonClickListener(this, finalSsid, input));
builder.setNegativeButton("Cancel", new CancelButtonClickListener());
builder.show();
}
@RequiresApi(api = Build.VERSION_CODES.Q)
public void connectToWifiAndroidQ(final String ssid, final String password) {
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder()
.setSsid(ssid);
if (password != null && !password.isEmpty()) {
builder.setWpa2Passphrase(password);
}
WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();
NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier(wifiNetworkSpecifier)
.build();
final ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
final WifiNetworkCallback networkCallback = new WifiNetworkCallback(this, ssid, connectivityManager);
connectivityManager.requestNetwork(networkRequest, networkCallback);
}
public void connectToWifiPreAndroidQ(final String ssid, final String password) {
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
if (password != null && !password.isEmpty()) {
wifiConfig.preSharedKey = "\"" + password + "\"";
} else {
wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
int netId = wifiManager.addNetwork(wifiConfig);
if (netId != -1) {
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
runOnUiThread(new WifiConnectedRunnable(this, ssid));
} else {
runOnUiThread(new WifiConnectionFailedRunnable(this));
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == WIFI_PERMISSION_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
scanWifiNetworks();
} else {
Toast.makeText(this, "WiFi scanning requires location permission", Toast.LENGTH_LONG).show();
}
}
}
public void launchAppByName(String appName) {
if (filteredAppInfoList != null) {
for (AppInfo appInfo : filteredAppInfoList) {
if (appInfo.name.equals(appName)) {
launchApp(appInfo.resolveInfo);
break;
}
}
} else {
// Fallback to searching in the full list
for (AppInfo appInfo : appInfoList) {
if (appInfo.name.equals(appName)) {
launchApp(appInfo.resolveInfo);
break;
}
}
}
}
private void launchApp(ResolveInfo app) {
try {
Intent intent = getPackageManager().getLaunchIntentForPackage(app.activityInfo.packageName);
if (intent != null) {
startActivity(intent);
} else {
Toast.makeText(this, "Unable to launch app", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(this, "Error launching app", Toast.LENGTH_SHORT).show();
}
}
private void updateCount() {
if (menuList.getAdapter() != null) {
itemCount.setText(String.valueOf(menuList.getAdapter().getCount()));
}
}
public void handleMenuSelection(String item) {
if (isInWifiMenu) {
if (item.equals("Back")) {
showMainMenu();
} else {
connectToWifi(item);
}
return;
}
switch (item) {
case "💻 t-mode":
Intent termuxIntent = getPackageManager().getLaunchIntentForPackage("com.termux.x11");
if (termuxIntent != null) {
startActivity(termuxIntent);
} else {
Toast.makeText(this, "Termux X11 not installed", Toast.LENGTH_SHORT).show();
}
break;
case "📱 Apps":
showAppMenu();
break;
case "Web Browser":
Intent browserIntent = getPackageManager().getLaunchIntentForPackage("app.vanadium.browser");
if (browserIntent != null) {
startActivity(browserIntent);
} else {
Toast.makeText(this, "Vanadium browser not found", Toast.LENGTH_SHORT).show();
}
break;
case "📂 Files":
Intent filesIntent = new Intent(Intent.ACTION_VIEW);
filesIntent.setDataAndType(Uri.parse("file:///"), "*/*");
if (filesIntent.resolveActivity(getPackageManager()) != null) {
startActivity(filesIntent);
} else {
Toast.makeText(this, "No file manager found", Toast.LENGTH_SHORT).show();
}
break;
case "📞 Dialer":
startActivity(new Intent(Intent.ACTION_DIAL));
break;
case "✉️ Texts":
Intent msgIntent = new Intent(Intent.ACTION_MAIN);
msgIntent.addCategory(Intent.CATEGORY_APP_MESSAGING);
if (msgIntent.resolveActivity(getPackageManager()) != null) {
startActivity(msgIntent);
} else {
Toast.makeText(this, "No messaging app found", Toast.LENGTH_SHORT).show();
}
break;
case "👤 Contacts":
Intent contactsIntent = new Intent(Intent.ACTION_VIEW);
contactsIntent.setType("vnd.android.cursor.dir/contact");
if (contactsIntent.resolveActivity(getPackageManager()) != null) {
startActivity(contactsIntent);
} else {
Toast.makeText(this, "No contacts app found", Toast.LENGTH_SHORT).show();
}
break;
case "📷 Camera":
Intent cameraIntent = getPackageManager().getLaunchIntentForPackage("app.grapheneos.camera");
if (cameraIntent != null) {
startActivity(cameraIntent);
} else {
startActivity(new Intent(MediaStore.ACTION_IMAGE_CAPTURE));
}
break;
case "🌐 Networks":
showWifiMenu();
break;
case "🔊 Audio":
startActivity(new Intent(Settings.ACTION_SOUND_SETTINGS));
break;
case "⚙️ Config":
startActivity(new Intent(Settings.ACTION_SETTINGS));
break;
case "⚡ Power":
showPowerMenu();
break;
case "❌ Close Menu":
menuContainer.setVisibility(View.GONE);
break;
default:
Toast.makeText(this, "Unknown selection", Toast.LENGTH_SHORT).show();
}
}
public void handlePowerOption(String option) {
switch (option) {
case "Lock":
executeRootCommand("input keyevent 26");
break;
case "Reboot":
executeRootCommand("reboot");
break;
case "Shutdown":
executeRootCommand("reboot -p");
break;
default:
Toast.makeText(this, "Unknown power option", Toast.LENGTH_SHORT).show();
}
}
private void executeRootCommand(String command) {
try {
Process suProcess = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
suProcess.waitFor();
suProcess.destroy();
} catch (Exception e) {
Toast.makeText(this, "Error executing command: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
int action = event.getAction();
int keyCode = event.getKeyCode();
if (action == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
return onKeyDown(keyCode, event); // Handle volume keys
default:
break;
}
}
return super.dispatchKeyEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (menuList == null || menuList.getAdapter() == null) return super.onKeyDown(keyCode, event);
// Get current state
int currentPosition = menuList.getSelectedItemPosition();
int itemCount = menuList.getAdapter().getCount();
// If nothing is selected, start at first item
if (currentPosition == -1) {
currentPosition = 0;
}
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (currentPosition < itemCount - 1) {
currentPosition++;
}
menuList.requestFocusFromTouch();
menuList.setSelection(currentPosition);
menuList.setItemChecked(currentPosition, true);
menuList.requestFocus();
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
if (currentPosition > 0) {
currentPosition--;
}
menuList.requestFocusFromTouch();
menuList.setSelection(currentPosition);
menuList.setItemChecked(currentPosition, true);
menuList.requestFocus();
return true;
case KeyEvent.KEYCODE_POWER:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_DPAD_CENTER:
// Handle item selection
View selectedView = menuList.getChildAt(currentPosition - menuList.getFirstVisiblePosition());
if (selectedView != null) {
menuList.performItemClick(
selectedView,
currentPosition,
menuList.getAdapter().getItemId(currentPosition)
);
}
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
// Add new method to handle Enter key press
private void handleEnterKeyPress() {
if (menuList.getAdapter() != null && menuList.getAdapter().getCount() > 0) {
String selectedItem = (String) menuList.getAdapter().getItem(0);
// Execute the action before clearing focus
if (isInAppsMenu) {
if (!selectedItem.equals("Back")) {
// Post the action to ensure it runs after the current operation
menuList.post(() -> {
launchAppByName(selectedItem);
});
} else {
showMainMenu();
}
} else if (isInWifiMenu) {
if (!selectedItem.equals("Back")) {
menuList.post(() -> {
connectToWifi(selectedItem);
});
} else {
showMainMenu();
}
} else if (isInPowerMenu) {
if (!selectedItem.equals("Back")) {
menuList.post(() -> {
handlePowerOption(selectedItem);
});
} else {
showMainMenu();
}
} else {
menuList.post(() -> {
handleMenuSelection(selectedItem);
});
}
// Clear focus after posting the action
searchBar.clearFocus();
// Hide keyboard explicitly
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(searchBar.getWindowToken(), 0);
}
}
}
|