Skip to content

Commit

Permalink
Merge branch 'feat/option-on-off-tv' into feature/kiosk-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ghassenbenzahra123 committed Jun 19, 2024
2 parents adc7473 + c8cca8a commit f7eeaef
Show file tree
Hide file tree
Showing 16 changed files with 770 additions and 145 deletions.
77 changes: 16 additions & 61 deletions android/app/src/main/kotlin/com/flyweb/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.app.admin.DevicePolicyManager
import java.io.IOException
import android.app.KeyguardManager
import android.content.Context
import java.io.BufferedReader
import java.io.DataOutputStream
import java.io.InputStreamReader
import android.os.AsyncTask
import android.util.Log

class MainActivity : FlutterActivity() {
private lateinit var mAdminComponentName: ComponentName
private lateinit var mDevicePolicyManager: DevicePolicyManager
Expand All @@ -43,8 +51,6 @@ class MainActivity : FlutterActivity() {
}
}
"checkRoot" -> result.success(checkRoot())
"toggleScreenOn" -> toggleScreenOn(call, result)
"toggleScreenOff" -> toggleScreenOff(call, result)
"toggleBoxScreenOff" -> toggleBoxScreenOff(call, result)
"toggleBoxScreenOn" -> toggleBoxScreenOn(call, result)
"clearAppData" -> {
Expand Down Expand Up @@ -92,7 +98,7 @@ class MainActivity : FlutterActivity() {
AsyncTask.execute {
try {
val timezone = call.argument<String>("timezone")
executeCommand("service call alarm 3 s16 $timezone", result)
executeCommand(listOf("service call alarm 3 s16 $timezone"), result)
} catch (e: Exception) {
handleCommandException(e, result)
}
Expand All @@ -118,48 +124,17 @@ class MainActivity : FlutterActivity() {

Log.i("SU_COMMAND", "Wifi Command output: cmd wifi connect-network $ssid $security $password")

executeCommand("cmd wifi connect-network $ssid $security $password", result)
executeCommand(listOf("cmd wifi connect-network $ssid $security $password"), result)

} catch (e: Exception) {
handleCommandException(e, result)
}
}
}

private fun executeCommand(command: String, result: MethodChannel.Result) {
try {
val suProcess = Runtime.getRuntime().exec("su")
val os = DataOutputStream(suProcess.outputStream)

os.writeBytes(command + "\n")
os.flush()
os.close()

val output = BufferedReader(InputStreamReader(suProcess.inputStream)).readText()
val error = BufferedReader(InputStreamReader(suProcess.errorStream)).readText()

Log.i("SU_COMMAND", "Command output: $output")
Log.e("SU_COMMAND", "Command error: $error")

val exitCode = suProcess.waitFor()

if (exitCode != 0) {
Log.e("SU_COMMAND", "Command failed with exit code $exitCode.")
result.success(false)

} else {
Log.i("SU_COMMAND", "Command executed successfully.")
result.success(true)
}
} catch (e: Exception) {
handleCommandException(e, result)
}
}

private fun handleCommandException(e: Exception, result: MethodChannel.Result) {
Log.e("SU_COMMAND", "Exception while executing command: ${e.message}")
result.error("CMD_ERROR", "Exception while executing command", null)
}

private fun clearDataRestart(): Boolean {
return try {
Expand All @@ -176,19 +151,7 @@ class MainActivity : FlutterActivity() {
}
}

private fun toggleScreenOn(call: MethodCall, result: MethodChannel.Result) {
AsyncTask.execute {
try {
val commands = listOf(
"input keyevent 82",
"am start -W -n com.mawaqit.androidtv/.MainActivity"
)
executeCommands(commands, result) // Lock the device
} catch (e: Exception) {
handleCommandExceptions(e, result)
}
}
}


private fun toggleBoxScreenOff(call: MethodCall, result: MethodChannel.Result) {
AsyncTask.execute {
Expand All @@ -198,7 +161,7 @@ class MainActivity : FlutterActivity() {
"cd /sys/class/hdmi/hdmi/attr",
"echo 0 > phy_power"
)
executeCommands(commands, result) // Lock the device
executeCommand(commands, result) // Lock the device
} catch (e: Exception) {
handleCommandExceptions(e, result)
}
Expand All @@ -214,24 +177,16 @@ class MainActivity : FlutterActivity() {
"echo 1 > phy_power",
"am start -W -n com.mawaqit.androidtv/.MainActivity"
)
executeCommands(commands, result)
executeCommand(commands, result)
} catch (e: Exception) {
handleCommandExceptions(e, result)
}
}
}

private fun toggleScreenOff(call: MethodCall, result: MethodChannel.Result) {
AsyncTask.execute {
try {
executeCommands(listOf("input keyevent 26"), result) // Lock the device
} catch (e: Exception) {
handleCommandExceptions(e, result)
}
}
}

private fun executeCommands(commands: List<String>, result: MethodChannel.Result) {

private fun executeCommand(commands: List<String>, result: MethodChannel.Result) {
try {
Log.d("SU_COMMAND", "Executing commands: ${commands.joinToString(separator = " && ")}")

Expand Down Expand Up @@ -269,4 +224,4 @@ class MainActivity : FlutterActivity() {
private fun handleCommandExceptions(e: Exception, result: MethodChannel.Result) {
result.error("Exception", "An exception occurred: $e", null)
}
}
}
12 changes: 11 additions & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,22 @@
"wifiFailure":"Failed to connect to Wifi.",
"timezoneSuccess":"Timezone set successfully.",
"timezoneFailure":"Failed to set timezone.",
"screenLock":"Screen on/off",
"screenLockConfig":"Configure screen on/off",
"screenLockMode":"Screen on/off mode",
"screenLockDesc":"Turn on/off TV before and after each prayer to save energy",
"screenLockDesc2":"This feature turn on/off the device before and after each prayer adhan",
"before":"minutes before each prayer time",
"after":"minutes after each prayer time",
"updateAvailable": "Update available",
"seeMore": "See more",
"whatIsNew": "What's new",
"update": "Update",
"automaticUpdate": "Notify update",
"automaticUpdateDescription": "Enable notify update to receive the latest features and improvements",
"later": "Later"
"later": "Later",
"powerOnScreen":"Power on the screen",
"powerOffScreen":"Power off the screen",
"deviceSettings":"Device Settings"
}

16 changes: 16 additions & 0 deletions lib/src/const/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ abstract class RandomHadithConstant {
static const String kHadithLanguage = "hadith_language";
static const String kLastHadithXMLFetchLanguage = "last_hadith_xml_language";
}

class TurnOnOffTvConstant {
static const String kLastEventDate = "lastEventDate";
static const String kIsEventsSet = "isEventsSet";
static const String kActivateToggleFeature = "activateToggleFeature";
static const String kScheduledTimersKey = "scheduledTimers";

/// native methods calls
static const String kNativeMethodsChannel = "nativeMethodsChannel";
static const String kCheckRoot = "checkRoot";
static const String kToggleBoxScreenOff = "toggleBoxScreenOff";
static const String kToggleBoxScreenOn = "toggleBoxScreenOn";

static const String kMinuteBeforeKey = 'selectedMinuteBefore';
static const String kMinuteAfterKey = 'selectedMinuteAfter';
}
4 changes: 2 additions & 2 deletions lib/src/helpers/AppDate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ class AppDateTime {
static final DateTime _initialDebugTime = DateTime(
_initialRealTime.year,
_initialRealTime.month,
_initialRealTime.day - 50,
_initialRealTime.day,
13,
48,
00,
-10,
);

static final Duration _timeDifference = _initialDebugTime.difference(_initialRealTime);
Expand Down
2 changes: 2 additions & 0 deletions lib/src/helpers/TimeShiftManager.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:developer';
import 'package:shared_preferences/shared_preferences.dart';

import '../../main.dart';
Expand Down Expand Up @@ -47,6 +48,7 @@ class TimeShiftManager {

// Initialize time-related properties from shared preferences or current time.
Future<void> initializeTimes() async {
log('Initializing TimeShiftManager...');
final SharedPreferences prefs = await SharedPreferences.getInstance();

_shift = prefs.getInt(_shiftKey) ?? 0;
Expand Down
Loading

0 comments on commit f7eeaef

Please sign in to comment.