package com.uiui.zyappstore.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInstaller;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.uiui.zyappstore.utils.ToastUtil;


public class InstallResultReceiver extends BroadcastReceiver {
    private static final String TAG = "InstallResultReceiver";

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        //throw new UnsupportedOperationException("Not yet implemented");


        if (intent != null) {
            final int status = intent.getIntExtra(PackageInstaller.EXTRA_STATUS,
                    PackageInstaller.STATUS_FAILURE);
            if (status == PackageInstaller.STATUS_SUCCESS) {
                // success
                String PACKAGE_NAME = intent.getStringExtra("android.content.pm.extra.PACKAGE_NAME");

                Log.e(TAG, "APP Install Success!");
            } else {
                String msg = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
            }
        }
//        String s = intent.getAction();
//        Log.e("fht", s);
//        Bundle extras = intent.getExtras();
//        Set<String> ks = extras.keySet();
//        Iterator<String> iterator = ks.iterator();
//        while (iterator.hasNext()) {
//            Log.d("KEY", iterator.next());
//        }
        String STATUS = intent.getStringExtra(PackageInstaller.EXTRA_STATUS);
        String PACKAGE_NAME = intent.getStringExtra(PackageInstaller.EXTRA_PACKAGE_NAME);
        String SESSION_ID = intent.getStringExtra(PackageInstaller.EXTRA_SESSION_ID);
        String STATUS_MESSAGE = intent.getStringExtra(PackageInstaller.EXTRA_STATUS_MESSAGE);
        String LEGACY_STATUS = intent.getStringExtra("android.content.pm.extra.LEGACY_STATUS");
//        Log.e("fht", STATUS);
//        Log.e("fht", PACKAGE_NAME);
//        Log.e("fht", SESSION_ID);
//        Log.e("fht", LEGACY_STATUS);
//        Log.e("fht", STATUS_MESSAGE);
        if (STATUS_MESSAGE != null && STATUS_MESSAGE.equals("INSTALL_SUCCEEDED")) {
            ToastUtil.show(PACKAGE_NAME + "安装成功");
        }
    }
}
