From e5fa0e7eb86443fc40885eceb3e9fc04385b4287 Mon Sep 17 00:00:00 2001 From: Sanchay Sinha <100801@ittiam.com> Date: Tue, 10 Dec 2024 19:37:16 +0530 Subject: [PATCH] Changing the brightness level to minimum when the Test starts --- app/src/main/AndroidManifest.xml | 1 + .../com/facebook/encapp/MainActivity.java | 97 ++++++++++++++++--- 2 files changed, 84 insertions(+), 14 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 16fb84b..7c665e5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + diff --git a/app/src/main/java/com/facebook/encapp/MainActivity.java b/app/src/main/java/com/facebook/encapp/MainActivity.java index 3408401..44094f1 100644 --- a/app/src/main/java/com/facebook/encapp/MainActivity.java +++ b/app/src/main/java/com/facebook/encapp/MainActivity.java @@ -119,6 +119,15 @@ public static boolean isStable() { return mStable; } + @Override + protected void onResume() { + super.onResume(); + + // Call the methods to check and request permissions + checkAndRequestAllFilesAccessPermission(); + checkAndRequestWriteSettingsPermission(); + } + private List mp4Files = new ArrayList<>(); public static String getFilenameExtension(String filename) { @@ -127,6 +136,38 @@ public static String getFilenameExtension(String filename) { return extension; } + /** + * Check and request ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION + */ + private void checkAndRequestAllFilesAccessPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + if (!Environment.isExternalStorageManager()) { + // Request All Files Access permission + Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); + intent.setData(Uri.parse("package:" + getPackageName())); + startActivity(intent); + } else { + Toast.makeText(this, "All Files Access permission already granted", Toast.LENGTH_SHORT).show(); + } + } + } + + /** + * Check and request ACTION_MANAGE_WRITE_SETTINGS + */ + + + private void checkAndRequestWriteSettingsPermission() { + if (!Settings.System.canWrite(this)) { + // Request Write Settings permission + Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); + intent.setData(Uri.parse("package:" + getPackageName())); + startActivity(intent); + } else { + Toast.makeText(this, "Write Settings permission already granted", Toast.LENGTH_SHORT).show(); + } + } + private static String[] retrieveNotGrantedPermissions(Context context) { ArrayList nonGrantedPerms = new ArrayList<>(); try { @@ -249,20 +290,20 @@ protected void onCreate(Bundle savedInstanceState) { useNewMethod = !mExtraData.getBoolean(CliSettings.OLD_AUTH_METHOD, false); } - if (Build.VERSION.SDK_INT >= 30 && useNewMethod && !Environment.isExternalStorageManager()) { - Log.d(TAG, "Check ExternalStorageManager"); - // request the external storage manager permission - Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); - Uri uri = Uri.fromParts("package", getPackageName(), null); - intent.setData(uri); - try { - startActivity(intent); - } catch (android.content.ActivityNotFoundException ex) { - Log.e(TAG, "No activity found for handling the permission intent: " + ex.getLocalizedMessage()); - // System.exit(-1); - Toast.makeText(this, "Missing MANAGE_APP_ALL_FILES_ACCESS_PERMISSION request,", Toast.LENGTH_LONG).show(); - } - } +// if (Build.VERSION.SDK_INT >= 30 && useNewMethod && !Environment.isExternalStorageManager()) { +// Log.d(TAG, "Check ExternalStorageManager"); +// // request the external storage manager permission +// Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); +// Uri uri = Uri.fromParts("package", getPackageName(), null); +// intent.setData(uri); +// try { +// startActivity(intent); +// } catch (android.content.ActivityNotFoundException ex) { +// Log.e(TAG, "No activity found for handling the permission intent: " + ex.getLocalizedMessage()); +// // System.exit(-1); +// Toast.makeText(this, "Missing MANAGE_APP_ALL_FILES_ACCESS_PERMISSION request,", Toast.LENGTH_LONG).show(); +// } +// } mTable = findViewById(R.id.viewTable); Log.d(TAG, "Passed all permission checks"); @@ -336,6 +377,8 @@ public void onReceive(Context context, Intent intent) { batteryStatsTextView.setText("Battery difference: "); testStatusTextView.setText("Test is running..."); + setScreenBrightness(1); + File externalDir = null; try { externalDir = this.getExternalFilesDir(null); @@ -449,6 +492,7 @@ public void onReceive(Context context, Intent intent) { endBatteryTextView.setText("After batteryInMicroAmps: " + endbattery); batteryStatsTextView.setText("Battery difference: " + (startbattery - endbattery)); testStatusTextView.setText("Test completed."); + setScreenBrightness(255); // saveResultsToFile(startbatteryInMicroAmps[0], endbatteryInMicroAmps[0]); }); @@ -473,6 +517,31 @@ public void onReceive(Context context, Intent intent) { }); } + private void setScreenBrightness(int brightnessLevel) { + try { + // Ensure the brightness level is within the valid range + if (brightnessLevel < 0) brightnessLevel = 0; + if (brightnessLevel > 255) brightnessLevel = 255; + + Settings.System.putInt( + getContentResolver(), + Settings.System.SCREEN_BRIGHTNESS_MODE, + Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL + ); + + // Update the system brightness setting + Settings.System.putInt( + getContentResolver(), + Settings.System.SCREEN_BRIGHTNESS, + brightnessLevel + ); + Toast.makeText(this, "Brightness set to " + brightnessLevel, Toast.LENGTH_SHORT).show(); + } catch (Exception e) { + e.printStackTrace(); + Toast.makeText(this, "Failed to change brightness", Toast.LENGTH_SHORT).show(); + } + } + private int getChargeCounter() { BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE); if (batteryManager != null) {