Skip to content

Commit

Permalink
Changing the brightness level to minimum when the Test starts
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchay97-dev committed Dec 10, 2024
1 parent 47ac6ed commit e5fa0e7
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 14 deletions.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.BATTERY_STATS" />
Expand Down
97 changes: 83 additions & 14 deletions app/src/main/java/com/facebook/encapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> mp4Files = new ArrayList<>();

public static String getFilenameExtension(String filename) {
Expand All @@ -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<String> nonGrantedPerms = new ArrayList<>();
try {
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
});

Expand All @@ -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) {
Expand Down

0 comments on commit e5fa0e7

Please sign in to comment.