From f49aef0fd2b07fc743cda3f2445731e0751a2499 Mon Sep 17 00:00:00 2001 From: Mystro256 Date: Sat, 16 Jan 2021 23:11:02 -0500 Subject: [PATCH] Add some comments So I don't forget what I did --- .../java/com/mystro256/autooffbluetooth/BTReceiver.java | 7 +++++++ .../java/com/mystro256/autooffbluetooth/MainActivity.java | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/app/src/main/java/com/mystro256/autooffbluetooth/BTReceiver.java b/app/src/main/java/com/mystro256/autooffbluetooth/BTReceiver.java index e92b7b3..8da8c51 100644 --- a/app/src/main/java/com/mystro256/autooffbluetooth/BTReceiver.java +++ b/app/src/main/java/com/mystro256/autooffbluetooth/BTReceiver.java @@ -7,16 +7,23 @@ import android.content.Intent; public class BTReceiver extends BroadcastReceiver { + + // "OnReceive" is an abstract function called when there's BT activity + // or state change; we can use this to try to catch a disconnection. When + // called, if no devices are connected then we can disable BT. @Override public void onReceive(Context context, Intent intent) { + // Check if BT adapter is valid and hasn't already been disabled if (BluetoothAdapter.getDefaultAdapter() != null && BluetoothAdapter.getDefaultAdapter().isEnabled()) { int[] profiles = {BluetoothProfile.A2DP, BluetoothProfile.HEADSET, BluetoothProfile.HEALTH}; for (int profileId : profiles) { + // If any device is connected, return to avoid disabling BT if (BluetoothAdapter.getDefaultAdapter().getProfileConnectionState(profileId) == BluetoothProfile.STATE_CONNECTED) { return; } } + // This point should only be reacted if nothing is connected BluetoothAdapter.getDefaultAdapter().disable(); } } diff --git a/app/src/main/java/com/mystro256/autooffbluetooth/MainActivity.java b/app/src/main/java/com/mystro256/autooffbluetooth/MainActivity.java index d704bf3..fb0f2c0 100644 --- a/app/src/main/java/com/mystro256/autooffbluetooth/MainActivity.java +++ b/app/src/main/java/com/mystro256/autooffbluetooth/MainActivity.java @@ -17,6 +17,7 @@ public class MainActivity extends AppCompatActivity { protected void onActivityResult(int requestCode, int resultCode, Intent data) { + // Call back if user agrees to disabling battery optimization if (requestCode == DISABLE_BATTERY_OPTIMIZATION) { if (resultCode == RESULT_OK) { hideBatteryOptimization(); @@ -24,6 +25,8 @@ protected void onActivityResult(int requestCode, int resultCode, } } + // By default, a battery optimization warning amd button is shown; this + // function hides the warning/button and shows the welcome text instead private void hideBatteryOptimization() { TextView WelcomeText = findViewById(R.id.WelcomeText); TextView batteryOptimizationText = findViewById(R.id.batteryOptimizationText); @@ -38,6 +41,9 @@ private void hideBatteryOptimization() { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + + // Hide battery optimization warning if battery optimization is + // already disabled or user is running something older than android M PowerManager powMan = (PowerManager) getSystemService(POWER_SERVICE); String packageName = getPackageName(); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || @@ -50,6 +56,7 @@ public void disableBatteryOptimization(View v) { Intent intent = new Intent(); String packageName = getPackageName(); + // Request disabling battery optimization and request a call back intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); startActivityForResult(intent, DISABLE_BATTERY_OPTIMIZATION);