Skip to content

Commit 901ca89

Browse files
committed
use two buttons for flashlight management (one for ON, one for OFF) on Android 14+
1 parent 9f7fc32 commit 901ca89

File tree

6 files changed

+336
-5
lines changed

6 files changed

+336
-5
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ android {
99
applicationId "it.pgp.currenttoggles"
1010
minSdkVersion 21 // for sdk 19 and lower, just use the great PowerToggles :)
1111
targetSdkVersion 28 // do not raise this, otherwise wifi toggle won't work without root on api 29+
12-
versionCode 126240109
13-
versionName "1.2.6"
12+
versionCode 127250125
13+
versionName "1.2.7"
1414
}
1515

1616
buildTypes {

app/src/main/java/it/pgp/currenttoggles/MainActivity.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,28 @@ public static void toggleFlashlight(Context context) {
306306
}
307307
}
308308

309+
public static void setTorch(Context context, boolean b) {
310+
CameraManager camManager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
311+
String cameraId;
312+
try {
313+
cameraId = camManager.getCameraIdList()[0];
314+
}
315+
catch(Exception e) {
316+
e.printStackTrace();
317+
postToast(context, "Unable to access flashlight");
318+
return;
319+
}
320+
try {
321+
camManager.setTorchMode(cameraId, b);
322+
String resultMsg = "Flashlight "+(b?"ON":"OFF");
323+
showToast(context, resultMsg, b);
324+
}
325+
catch(Exception e) {
326+
e.printStackTrace();
327+
postToast(context, "Unable to toggle flashlight status");
328+
}
329+
}
330+
309331
public static void toggleEnergySaving(Context context) {
310332
String getCmd = "settings get global low_power";
311333
String[] msgs = {"Energy saving currently DISABLED -> enabling...", "Energy saving currently ENABLED -> disabling..."};
@@ -405,8 +427,11 @@ public void toggle(View v) {
405427
toggleAutoScreenBrightness(this);
406428
break;
407429
case R.id.toggleFlashlight:
408-
toggleFlashlight(this);
430+
if(Build.VERSION.SDK_INT >= 34) setTorch(this, true);
431+
else toggleFlashlight(this);
409432
break;
433+
case R.id.toggleFlashlight2:
434+
setTorch(this, false);
410435
case R.id.toggleAirplane:
411436
toggleAirplane(this);
412437
break;

app/src/main/java/it/pgp/currenttoggles/MainWidget.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class MainWidget extends AppWidgetProvider {
3535
private static final String onDemandAutoBrightness = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_AUTO_BR";
3636
private static final String displayOptions = "it.pgp.currenttoggles.appwidget.action.DISPLAY_OPTIONS";
3737
private static final String onDemandFlashlight = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_FLASH";
38+
private static final String onDemandFlashlight2 = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_FLASH_2";
3839
private static final String onDemandAirplane = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_AIRPLANE";
3940
private static final String airplaneOptions = "it.pgp.currenttoggles.appwidget.action.AIRPLANE_OPTIONS";
4041
private static final String onDemandES = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_ES";
@@ -51,7 +52,11 @@ public class MainWidget extends AppWidgetProvider {
5152
m.put(onDemandBluetooth, R.id.toggle_bt);
5253
m.put(onDemandGps, R.id.toggle_gps);
5354
m.put(onDemandAutoBrightness, R.id.toggle_auto_brightness);
54-
m.put(onDemandFlashlight, R.id.toggle_flashlight);
55+
if(Build.VERSION.SDK_INT >= 34) {
56+
m.put(onDemandFlashlight, R.id.flashlight_on);
57+
m.put(onDemandFlashlight2, R.id.flashlight_off);
58+
}
59+
else m.put(onDemandFlashlight, R.id.toggle_flashlight);
5560
m.put(onDemandAirplane, R.id.toggle_airplane);
5661
m.put(onDemandES, R.id.toggle_es);
5762
m.put(onDemandTurnOffScreen, R.id.turnoff_screen);
@@ -185,7 +190,12 @@ public void onReceive(Context context, Intent intent) {
185190
break;
186191
case onDemandFlashlight:
187192
Log.d(LOG_PREFIX,"onDemand Flashlight");
188-
MainActivity.toggleFlashlight(context);
193+
if(Build.VERSION.SDK_INT >= 34) MainActivity.setTorch(context, true);
194+
else MainActivity.toggleFlashlight(context);
195+
break;
196+
case onDemandFlashlight2:
197+
Log.d(LOG_PREFIX,"onDemand Flashlight 2");
198+
MainActivity.setTorch(context, false);
189199
break;
190200
case onDemandAirplane:
191201
Log.d(LOG_PREFIX,"onDemand Airplane");
3.31 KB
Loading
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
android:orientation="vertical"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".MainActivity">
8+
<Button
9+
android:text="Data"
10+
android:onClick="toggle"
11+
android:id="@+id/toggleData"
12+
android:layout_width="match_parent"
13+
android:layout_height="wrap_content" />
14+
<Button
15+
android:text="Hotspot"
16+
android:onClick="toggle"
17+
android:id="@+id/toggleHotspot"
18+
android:layout_width="match_parent"
19+
android:layout_height="wrap_content" />
20+
<Button
21+
android:text="Wifi"
22+
android:onClick="toggle"
23+
android:id="@+id/toggleWifi"
24+
android:layout_width="match_parent"
25+
android:layout_height="wrap_content" />
26+
<Button
27+
android:text="Bluetooth"
28+
android:onClick="toggle"
29+
android:id="@+id/toggleBt"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content" />
32+
<Button
33+
android:text="GPS"
34+
android:onClick="toggle"
35+
android:id="@+id/toggleGps"
36+
android:layout_width="match_parent"
37+
android:layout_height="wrap_content" />
38+
<Button
39+
android:text="Auto/Manual brightness"
40+
android:onClick="toggle"
41+
android:id="@+id/toggleAutoBrightness"
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content" />
44+
<LinearLayout
45+
android:orientation="horizontal"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content">
48+
<Button
49+
android:text="Flashlight ON"
50+
android:onClick="toggle"
51+
android:id="@+id/toggleFlashlight"
52+
android:layout_weight="1"
53+
android:layout_width="0dp"
54+
android:layout_height="match_parent" />
55+
<Button
56+
android:text="Flashlight OFF"
57+
android:onClick="toggle"
58+
android:id="@+id/toggleFlashlight2"
59+
android:layout_weight="1"
60+
android:layout_width="0dp"
61+
android:layout_height="match_parent" />
62+
</LinearLayout>
63+
<Button
64+
android:text="Airplane"
65+
android:onClick="toggle"
66+
android:id="@+id/toggleAirplane"
67+
android:layout_width="match_parent"
68+
android:layout_height="wrap_content" />
69+
<Button
70+
android:text="Energy saving"
71+
android:onClick="toggle"
72+
android:id="@+id/toggleES"
73+
android:layout_width="match_parent"
74+
android:layout_height="wrap_content" />
75+
<Button
76+
android:text="Turn off screen"
77+
android:onClick="toggle"
78+
android:id="@+id/turnOffScreen"
79+
android:layout_width="match_parent"
80+
android:layout_height="wrap_content" />
81+
</LinearLayout>
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:orientation="horizontal"
3+
android:layout_width="match_parent"
4+
android:layout_height="wrap_content"
5+
android:background="@color/deep_blue" >
6+
7+
<LinearLayout
8+
android:orientation="vertical"
9+
android:layout_weight="1"
10+
android:layout_width="0dp"
11+
android:layout_height="match_parent" >
12+
13+
<ImageButton
14+
android:id="@+id/toggle_data"
15+
android:src="@drawable/data"
16+
style="@android:style/Widget.Holo.ImageButton"
17+
android:layout_weight="2.5"
18+
android:layout_width="match_parent"
19+
android:layout_height="0dp" />
20+
21+
<ImageButton
22+
android:id="@+id/data_options"
23+
style="@android:style/Widget.Holo.ImageButton"
24+
android:layout_weight="1"
25+
android:layout_width="match_parent"
26+
android:layout_height="0dp" />
27+
</LinearLayout>
28+
29+
<LinearLayout
30+
android:orientation="vertical"
31+
android:layout_weight="1"
32+
android:layout_width="0dp"
33+
android:layout_height="match_parent" >
34+
35+
<ImageButton
36+
android:id="@+id/toggle_hotspot"
37+
android:src="@drawable/hotspot"
38+
style="@android:style/Widget.Holo.ImageButton"
39+
android:layout_weight="2.5"
40+
android:layout_width="match_parent"
41+
android:layout_height="0dp" />
42+
43+
<ImageButton
44+
android:id="@+id/hotspot_options"
45+
style="@android:style/Widget.Holo.ImageButton"
46+
android:layout_weight="1"
47+
android:layout_width="match_parent"
48+
android:layout_height="0dp" />
49+
50+
</LinearLayout>
51+
52+
<LinearLayout
53+
android:orientation="vertical"
54+
android:layout_weight="1"
55+
android:layout_width="0dp"
56+
android:layout_height="match_parent" >
57+
58+
<ImageButton
59+
android:id="@+id/toggle_wifi"
60+
android:src="@drawable/wifi"
61+
style="@android:style/Widget.Holo.ImageButton"
62+
android:layout_weight="2.5"
63+
android:layout_width="match_parent"
64+
android:layout_height="0dp" />
65+
66+
<ImageButton
67+
android:id="@+id/wifi_options"
68+
style="@android:style/Widget.Holo.ImageButton"
69+
android:layout_weight="1"
70+
android:layout_width="match_parent"
71+
android:layout_height="0dp" />
72+
</LinearLayout>
73+
74+
<LinearLayout
75+
android:orientation="vertical"
76+
android:layout_weight="1"
77+
android:layout_width="0dp"
78+
android:layout_height="match_parent" >
79+
80+
<ImageButton
81+
android:id="@+id/toggle_bt"
82+
android:src="@drawable/bt"
83+
style="@android:style/Widget.Holo.ImageButton"
84+
android:layout_weight="2.5"
85+
android:layout_width="match_parent"
86+
android:layout_height="0dp" />
87+
88+
<ImageButton
89+
android:id="@+id/bluetooth_options"
90+
style="@android:style/Widget.Holo.ImageButton"
91+
android:layout_weight="1"
92+
android:layout_width="match_parent"
93+
android:layout_height="0dp" />
94+
</LinearLayout>
95+
96+
<LinearLayout
97+
android:orientation="vertical"
98+
android:layout_weight="1"
99+
android:layout_width="0dp"
100+
android:layout_height="match_parent" >
101+
102+
<ImageButton
103+
android:id="@+id/toggle_gps"
104+
android:src="@drawable/gps"
105+
style="@android:style/Widget.Holo.ImageButton"
106+
android:layout_weight="2.5"
107+
android:layout_width="match_parent"
108+
android:layout_height="0dp" />
109+
110+
<ImageButton
111+
android:id="@+id/gps_options"
112+
style="@android:style/Widget.Holo.ImageButton"
113+
android:layout_weight="1"
114+
android:layout_width="match_parent"
115+
android:layout_height="0dp" />
116+
</LinearLayout>
117+
118+
<LinearLayout
119+
android:orientation="vertical"
120+
android:layout_weight="1"
121+
android:layout_width="0dp"
122+
android:layout_height="match_parent" >
123+
124+
<ImageButton
125+
android:id="@+id/toggle_auto_brightness"
126+
android:src="@drawable/brightness"
127+
style="@android:style/Widget.Holo.ImageButton"
128+
android:layout_weight="2.5"
129+
android:layout_width="match_parent"
130+
android:layout_height="0dp" />
131+
132+
<ImageButton
133+
android:id="@+id/display_options"
134+
style="@android:style/Widget.Holo.ImageButton"
135+
android:layout_weight="1"
136+
android:layout_width="match_parent"
137+
android:layout_height="0dp" />
138+
</LinearLayout>
139+
140+
<LinearLayout
141+
android:orientation="vertical"
142+
android:layout_weight="1"
143+
android:layout_width="0dp"
144+
android:layout_height="match_parent" >
145+
146+
<ImageButton
147+
android:id="@+id/flashlight_on"
148+
android:src="@drawable/flash"
149+
style="@android:style/Widget.Holo.ImageButton"
150+
android:layout_weight="1"
151+
android:layout_width="match_parent"
152+
android:layout_height="0dp" />
153+
154+
<ImageButton
155+
android:id="@+id/flashlight_off"
156+
android:src="@drawable/flash0"
157+
style="@android:style/Widget.Holo.ImageButton"
158+
android:layout_weight="1"
159+
android:layout_width="match_parent"
160+
android:layout_height="0dp" />
161+
</LinearLayout>
162+
163+
<LinearLayout
164+
android:orientation="vertical"
165+
android:layout_weight="1"
166+
android:layout_width="0dp"
167+
android:layout_height="match_parent" >
168+
169+
<ImageButton
170+
android:id="@+id/toggle_airplane"
171+
android:src="@drawable/airplane"
172+
style="@android:style/Widget.Holo.ImageButton"
173+
android:layout_weight="2.5"
174+
android:layout_width="match_parent"
175+
android:layout_height="0dp" />
176+
177+
<ImageButton
178+
android:id="@+id/airplane_options"
179+
style="@android:style/Widget.Holo.ImageButton"
180+
android:layout_weight="1"
181+
android:layout_width="match_parent"
182+
android:layout_height="0dp" />
183+
</LinearLayout>
184+
185+
<LinearLayout
186+
android:orientation="vertical"
187+
android:layout_weight="1"
188+
android:layout_width="0dp"
189+
android:layout_height="match_parent" >
190+
191+
<ImageButton
192+
android:id="@+id/toggle_es"
193+
android:src="@drawable/battery"
194+
style="@android:style/Widget.Holo.ImageButton"
195+
android:layout_weight="2.5"
196+
android:layout_width="match_parent"
197+
android:layout_height="0dp" />
198+
199+
<ImageButton
200+
android:id="@+id/es_options"
201+
style="@android:style/Widget.Holo.ImageButton"
202+
android:layout_weight="1"
203+
android:layout_width="match_parent"
204+
android:layout_height="0dp" />
205+
</LinearLayout>
206+
207+
<ImageButton
208+
android:id="@+id/turnoff_screen"
209+
android:src="@android:drawable/picture_frame"
210+
style="@android:style/Widget.Holo.ImageButton"
211+
android:layout_weight="1"
212+
android:layout_width="0dp"
213+
android:layout_height="match_parent" />
214+
215+
</LinearLayout>

0 commit comments

Comments
 (0)