/*
========================================================================================
Filename: 20_BatteryLow.txt
Program 20: Create an app to broadcast a system intent – Battery Low.
Files Included: MainActivity.java, BatteryReceiver.java
========================================================================================

WHAT ALL TO DO / SETUP INSTRUCTIONS:
----------------------------------------------------------------------------------------
1. UI Components to Add (in res/layout/activity_main.xml):
   Add 3 TextView:
     - tvTitle: 'Battery Low Broadcast'
     - tvInstruction: 'Reduce the battery level below 15% and observe the Toast message.'
     - tvBattery: 'Battery Percentage : --%'

2. Layout Configuration:
   - LinearLayout (vertical, gravity='center', padding 20dp)

3. Menu Configuration:
   - None required.

4. Themes Configuration (res/values/themes.xml):
   - Default theme.

5. AndroidManifest.xml:
   <activity android:name=".MainActivity" android:exported="true" />

6. Additional Files & Resources Required:
   - Create BatteryReceiver.java; registered dynamically with Intent.ACTION_BATTERY_LOW and Intent.ACTION_BATTERY_CHANGED.
========================================================================================
*/

// ======================================================================================
// File: MainActivity.java
// ======================================================================================
package com.example.batterylow;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    final BatteryReceiver receiver=new BatteryReceiver();
    TextView tvBattery;
    @Override
    protected void onCreate(Bundle b) {
        super.onCreate(b);
        setContentView(R.layout.activity_main);
        tvBattery=findViewById(R.id.tvBattery);
        IntentFilter f=new IntentFilter();
        f.addAction(Intent.ACTION_BATTERY_LOW);
        f.addAction(Intent.ACTION_BATTERY_CHANGED);
        if(android.os.Build.VERSION.SDK_INT>=33)registerReceiver(receiver,f,Context.RECEIVER_EXPORTED);
        else registerReceiver(receiver,f);
        Intent s=registerReceiver(null,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        if(s!=null) {
            int level=s.getIntExtra(BatteryManager.EXTRA_LEVEL,-1),scale=s.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
            int p=(int)((level*100f)/scale);
            tvBattery.setText("Battery Percentage : "+p+"%");
        }
    }

    @Override
    protected void onDestroy() {
        try {
            unregisterReceiver(receiver);
        }

        catch(Exception ignored) {
        }

        super.onDestroy();
    }
}

// ======================================================================================
// File: BatteryReceiver.java
// ======================================================================================
package com.example.batterylow;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.widget.Toast;

public class BatteryReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context c,Intent i) {
        if(Intent.ACTION_BATTERY_LOW.equals(i.getAction()))Toast.makeText(c,"Battery Low Broadcast Received!",Toast.LENGTH_LONG).show();
        if(Intent.ACTION_BATTERY_CHANGED.equals(i.getAction())) {
            int l=i.getIntExtra(BatteryManager.EXTRA_LEVEL,-1),s=i.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
            int p=(int)((l*100f)/s);
            if(p<=15)Toast.makeText(c,"Battery Low! ("+p+"%)",Toast.LENGTH_LONG).show();
        }
    }
}
