package com.reyun.sdktestdemo;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.provider.Telephony;
import android.telephony.CellInfo;
import android.telephony.CellLocation;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.reyun.wandun.WdCallback;
import com.reyun.wandun.WdMain;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

public class MainActivity extends AppCompatActivity {
    private TextView mText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mText = findViewById(R.id.data);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION
            }, 100);
        }

        WifiManager wm = (WifiManager) getApplicationContext().getSystemService(Service.WIFI_SERVICE);
        WifiInfo wi = wm.getConnectionInfo();
        String ssid = wi.getSSID();
        String bssid = wi.getBSSID();
        int ip = wi.getIpAddress();

        PackageManager pm =getPackageManager();
        int uid = getApplicationInfo().uid;
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        ResolveInfo ri = pm.resolveActivity(intent, 0);
        String ss = ri.activityInfo.packageName;
        try {
            getFilesDir().getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            PackageInfo pi = pm.getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
            byte[] b = pi.signatures[0].toByteArray();
            CertificateFactory cf = CertificateFactory.getInstance("X509");
            Certificate ci = cf.generateCertificate(new ByteArrayInputStream(b));
        } catch (PackageManager.NameNotFoundException | CertificateException e) {
            e.printStackTrace();
        }

        CameraManager camera = (CameraManager) getSystemService(Service.CAMERA_SERVICE);
        try {
            String[] list = camera.getCameraIdList();
            CameraCharacteristics cc = camera.getCameraCharacteristics("0");
            Log.d("haha", "onCreate: ");
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == 100) {
            String aa = System.getProperty("http.agent");
            //初始化sdk环境
            WdMain wm = WdMain.getInstance();
            wm.init(getApplicationContext(), "test-a341fsfr3123ddadfs");
            //同步获取唯一id
            wm.getWdId(new WdCallback() {
                @Override
                public void onWdId(final String wdId) {
                    Log.d("demo", "onWdId: " + wdId);
                    mText.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mText.setText(wdId);
                        }
                    }, 0);
                }
            /*
            @Override
            public void onError(String errorMsg) {
                Log.d(TAG, "onError: " + errorMsg);
            }*/
            });
        }
    }
}

