Skip to content

Commit

Permalink
use two buttons for flashlight management (one for ON, one for OFF) o…
Browse files Browse the repository at this point in the history
…n Android 14+
  • Loading branch information
pgp committed Jan 25, 2025
1 parent 9f7fc32 commit 901ca89
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
applicationId "it.pgp.currenttoggles"
minSdkVersion 21 // for sdk 19 and lower, just use the great PowerToggles :)
targetSdkVersion 28 // do not raise this, otherwise wifi toggle won't work without root on api 29+
versionCode 126240109
versionName "1.2.6"
versionCode 127250125
versionName "1.2.7"
}

buildTypes {
Expand Down
27 changes: 26 additions & 1 deletion app/src/main/java/it/pgp/currenttoggles/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,28 @@ public static void toggleFlashlight(Context context) {
}
}

public static void setTorch(Context context, boolean b) {
CameraManager camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
String cameraId;
try {
cameraId = camManager.getCameraIdList()[0];
}
catch(Exception e) {
e.printStackTrace();
postToast(context, "Unable to access flashlight");
return;
}
try {
camManager.setTorchMode(cameraId, b);
String resultMsg = "Flashlight "+(b?"ON":"OFF");
showToast(context, resultMsg, b);
}
catch(Exception e) {
e.printStackTrace();
postToast(context, "Unable to toggle flashlight status");
}
}

public static void toggleEnergySaving(Context context) {
String getCmd = "settings get global low_power";
String[] msgs = {"Energy saving currently DISABLED -> enabling...", "Energy saving currently ENABLED -> disabling..."};
Expand Down Expand Up @@ -405,8 +427,11 @@ public void toggle(View v) {
toggleAutoScreenBrightness(this);
break;
case R.id.toggleFlashlight:
toggleFlashlight(this);
if(Build.VERSION.SDK_INT >= 34) setTorch(this, true);
else toggleFlashlight(this);
break;
case R.id.toggleFlashlight2:
setTorch(this, false);
case R.id.toggleAirplane:
toggleAirplane(this);
break;
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/it/pgp/currenttoggles/MainWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class MainWidget extends AppWidgetProvider {
private static final String onDemandAutoBrightness = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_AUTO_BR";
private static final String displayOptions = "it.pgp.currenttoggles.appwidget.action.DISPLAY_OPTIONS";
private static final String onDemandFlashlight = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_FLASH";
private static final String onDemandFlashlight2 = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_FLASH_2";
private static final String onDemandAirplane = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_AIRPLANE";
private static final String airplaneOptions = "it.pgp.currenttoggles.appwidget.action.AIRPLANE_OPTIONS";
private static final String onDemandES = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_ES";
Expand All @@ -51,7 +52,11 @@ public class MainWidget extends AppWidgetProvider {
m.put(onDemandBluetooth, R.id.toggle_bt);
m.put(onDemandGps, R.id.toggle_gps);
m.put(onDemandAutoBrightness, R.id.toggle_auto_brightness);
m.put(onDemandFlashlight, R.id.toggle_flashlight);
if(Build.VERSION.SDK_INT >= 34) {
m.put(onDemandFlashlight, R.id.flashlight_on);
m.put(onDemandFlashlight2, R.id.flashlight_off);
}
else m.put(onDemandFlashlight, R.id.toggle_flashlight);
m.put(onDemandAirplane, R.id.toggle_airplane);
m.put(onDemandES, R.id.toggle_es);
m.put(onDemandTurnOffScreen, R.id.turnoff_screen);
Expand Down Expand Up @@ -185,7 +190,12 @@ public void onReceive(Context context, Intent intent) {
break;
case onDemandFlashlight:
Log.d(LOG_PREFIX,"onDemand Flashlight");
MainActivity.toggleFlashlight(context);
if(Build.VERSION.SDK_INT >= 34) MainActivity.setTorch(context, true);
else MainActivity.toggleFlashlight(context);
break;
case onDemandFlashlight2:
Log.d(LOG_PREFIX,"onDemand Flashlight 2");
MainActivity.setTorch(context, false);
break;
case onDemandAirplane:
Log.d(LOG_PREFIX,"onDemand Airplane");
Expand Down
Binary file added app/src/main/res/drawable/flash0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions app/src/main/res/layout-v34/activity_main.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:text="Data"
android:onClick="toggle"
android:id="@+id/toggleData"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Hotspot"
android:onClick="toggle"
android:id="@+id/toggleHotspot"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Wifi"
android:onClick="toggle"
android:id="@+id/toggleWifi"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Bluetooth"
android:onClick="toggle"
android:id="@+id/toggleBt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="GPS"
android:onClick="toggle"
android:id="@+id/toggleGps"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Auto/Manual brightness"
android:onClick="toggle"
android:id="@+id/toggleAutoBrightness"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Flashlight ON"
android:onClick="toggle"
android:id="@+id/toggleFlashlight"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<Button
android:text="Flashlight OFF"
android:onClick="toggle"
android:id="@+id/toggleFlashlight2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
<Button
android:text="Airplane"
android:onClick="toggle"
android:id="@+id/toggleAirplane"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Energy saving"
android:onClick="toggle"
android:id="@+id/toggleES"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Turn off screen"
android:onClick="toggle"
android:id="@+id/turnOffScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
215 changes: 215 additions & 0 deletions app/src/main/res/layout-v34/buttons_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/deep_blue" >

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_data"
android:src="@drawable/data"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/data_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_hotspot"
android:src="@drawable/hotspot"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/hotspot_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_wifi"
android:src="@drawable/wifi"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/wifi_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_bt"
android:src="@drawable/bt"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/bluetooth_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_gps"
android:src="@drawable/gps"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/gps_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_auto_brightness"
android:src="@drawable/brightness"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/display_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/flashlight_on"
android:src="@drawable/flash"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/flashlight_off"
android:src="@drawable/flash0"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_airplane"
android:src="@drawable/airplane"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/airplane_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" >

<ImageButton
android:id="@+id/toggle_es"
android:src="@drawable/battery"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="2.5"
android:layout_width="match_parent"
android:layout_height="0dp" />

<ImageButton
android:id="@+id/es_options"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>

<ImageButton
android:id="@+id/turnoff_screen"
android:src="@android:drawable/picture_frame"
style="@android:style/Widget.Holo.ImageButton"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />

</LinearLayout>

0 comments on commit 901ca89

Please sign in to comment.