Skip to content

Commit

Permalink
add bluetooth button
Browse files Browse the repository at this point in the history
  • Loading branch information
pgp committed Dec 13, 2021
1 parent 3e8daf4 commit a750168
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<application
android:allowBackup="true"
android:icon="@drawable/data"
Expand All @@ -22,6 +24,7 @@
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="it.pgp.currenttoggles.appwidget.action.ON_DEMAND_WIFI" />
<action android:name="it.pgp.currenttoggles.appwidget.action.ON_DEMAND_DATA" />
<action android:name="it.pgp.currenttoggles.appwidget.action.ON_DEMAND_BLUETOOTH" />
<action android:name="it.pgp.currenttoggles.appwidget.action.ON_DEMAND_AIRPLANE" />
</intent-filter>

Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/it/pgp/currenttoggles/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
}

public static void toggleDataOrWifi(Context context, String channel, II ii) { // channel: "data" or "wifi
public static void toggleDataWifiBluetooth(Context context, String channel, II ii) { // channel: "data" or "wifi
String[][] cmdsAndErrors = {
{channel + " currently DISABLED -> enabling...", "enable"},
{channel + " currently ENABLED -> disabling...", "disable"}
Expand Down Expand Up @@ -70,10 +70,13 @@ public static void toggleAirplane(Context context) {
public void toggle(View v) {
switch(v.getId()) {
case R.id.toggleData:
toggleDataOrWifi(this, "data", Misc::isDataConnectionEnabled);
toggleDataWifiBluetooth(this, "data", Misc::isDataConnectionEnabled);
break;
case R.id.toggleWifi:
toggleDataOrWifi(this, "wifi", Misc::isWifiEnabled);
toggleDataWifiBluetooth(this, "wifi", Misc::isWifiEnabled);
break;
case R.id.toggleBt:
toggleDataWifiBluetooth(this, "bluetooth", Misc::isBluetoothEnabled);
break;
case R.id.toggleAirplane:
toggleAirplane(context);
Expand Down
27 changes: 21 additions & 6 deletions app/src/main/java/it/pgp/currenttoggles/MainWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@

public class MainWidget extends AppWidgetProvider {

public static final String LOG_PREFIX = "CURRENTTOGGLES";

private static final String onDemandWifi = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_WIFI";
private static final String onDemandData = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_DATA";
private static final String onDemandBluetooth = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_BLUETOOTH";
private static final String onDemandAirplane = "it.pgp.currenttoggles.appwidget.action.ON_DEMAND_AIRPLANE";

public static void updateAllDirect(Context context) {
Expand Down Expand Up @@ -47,6 +50,14 @@ public static void updateAllDirect(Context context) {
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.toggle_wifi, pi);

ii = new Intent(context, MainWidget.class);
ii.setAction(onDemandBluetooth);
ii.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, w_ids);
pi = PendingIntent.getBroadcast(
context, appWidgetId, ii,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.toggle_bt, pi);

ii = new Intent(context, MainWidget.class);
ii.setAction(onDemandAirplane);
ii.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, w_ids);
Expand All @@ -63,21 +74,25 @@ public static void updateAllDirect(Context context) {
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String a = intent.getAction();
Log.d("XRE_RHSS","onReceive action: "+intent.getAction());
Log.d(LOG_PREFIX,"onReceive action: "+intent.getAction());
if (a == null) return;
if(MainActivity.context == null) MainActivity.refreshAppContext(context);
try {
switch(a) {
case onDemandWifi:
Log.d("XRE_RHSS","onDemand Wifi");
MainActivity.toggleDataOrWifi(context, "wifi", Misc::isWifiEnabled);
Log.d(LOG_PREFIX,"onDemand Wifi");
MainActivity.toggleDataWifiBluetooth(context, "wifi", Misc::isWifiEnabled);
break;
case onDemandData:
Log.d("XRE_RHSS","onDemand Data");
MainActivity.toggleDataOrWifi(context, "data", Misc::isDataConnectionEnabled);
Log.d(LOG_PREFIX,"onDemand Data");
MainActivity.toggleDataWifiBluetooth(context, "data", Misc::isDataConnectionEnabled);
break;
case onDemandBluetooth:
Log.d(LOG_PREFIX,"onDemand Bluetooth");
MainActivity.toggleDataWifiBluetooth(context, "bluetooth", Misc::isBluetoothEnabled);
break;
case onDemandAirplane:
Log.d("XRE_RHSS","onDemand Airplane");
Log.d(LOG_PREFIX,"onDemand Airplane");
MainActivity.toggleAirplane(context);
break;
default:
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/it/pgp/currenttoggles/utils/Misc.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.pgp.currenttoggles.utils;

import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
Expand All @@ -25,4 +26,8 @@ public static boolean isWifiEnabled(Context context) {
public static boolean isAirplaneModeEnabled(Context context) {
return Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
}

public static boolean isBluetoothEnabled(Context unused){
return BluetoothAdapter.getDefaultAdapter().isEnabled();
}
}
Binary file added app/src/main/res/drawable/bt.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
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="Airplane"
android:onClick="toggle"
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/layout/buttons_widget.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
android:layout_width="0dp"
android:layout_height="match_parent" />

<ImageButton
android:id="@+id/toggle_bt"
android:src="@drawable/bt"
android:background="@null"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />

<ImageButton
android:id="@+id/toggle_airplane"
android:src="@drawable/airplane"
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml

This file was deleted.

Binary file removed app/src/main/res/mipmap-hdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-mdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
Binary file not shown.
Binary file not shown.

0 comments on commit a750168

Please sign in to comment.