/*
========================================================================================
Filename: 30_AsynchronousTask.txt
Program 30: Create an app to perform asynchronous task.
Files Included: MainActivity.java
========================================================================================

WHAT ALL TO DO / SETUP INSTRUCTIONS:
----------------------------------------------------------------------------------------
1. UI Components to Add (in res/layout/activity_main.xml):
   Add 2 TextView:
     - Title: 'Asynchronous Task'
     - statusText: 'Click the button to start'
   Add 1 Button:
     - startButton: Text: 'Start Task', onClick: 'startTask'

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

3. Menu Configuration:
   - None required.

4. Themes Configuration (res/values/themes.xml):
   - Theme.MaterialComponents.DayNight.NoActionBar in themes.xml.

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

6. Additional Files & Resources Required:
   - Performs background execution using ExecutorService / Handler or AsyncTask simulation, showing progress updates.
========================================================================================
*/

// ======================================================================================
// File: MainActivity.java
// ======================================================================================
package com.example.asynchronoustask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends AppCompatActivity {
    TextView statusText;
    Button startButton;
    ExecutorService executor;
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        statusText = findViewById(R.id.statusText);
        startButton = findViewById(R.id.startButton);
        executor = Executors.newSingleThreadExecutor();
        handler = new Handler(Looper.getMainLooper());
    }
    public void startTask(View view) {
        // Disable button
        startButton.setEnabled(false);
        startButton.setText("Task Running...");
        startButton.setAlpha(0.5f);
        statusText.setText("Task is running...");
        executor.execute(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            handler.post(() -> {
                statusText.setText("Task completed!");
                // Enable button again
                startButton.setEnabled(true);
                startButton.setText("Start Task");
                startButton.setAlpha(1.0f);
            });
        });
    }
    @Override
    protected void onDestroy() {
        executor.shutdown();
        super.onDestroy();
    }
}
