package com.action.actionmdm;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;

import java.io.File;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;


public class MDMApi {
    private static final String TAG = "act-mdm";
    private Context mContext;
    private ContentResolver resolver = null;
    private static final Uri ACTION_MDM_URI = Uri.parse("content://com.action.mdm_provider");
    private static final String GET_MEID_IMEI = "get_meid_imei";
    private static final String GET_DISABLE_INSTALL_PKGS = "get_disable_install_pkgs";
    private static final String SET_DISABLE_INSTALL_PKGS = "set_disable_install_pkgs";
    private static final String IS_DISABLE_INSTALL_ALL_APP = "is_disable_install_all_app";
    private static final String SET_DISABLE_INSTALL_ALL_APP = "set_disable_install_all_app";
    private static final String IS_DISABLED_STATUSBAR = "is_disabled_statusbar";
    private static final String SET_DISABLED_STATUSBAR = "set_disabled_statusbar";
    private static final String GET_NOT_DISPLAY_DESKTOP_ICONS = "get_not_display_desktop_icons";
    private static final String SET_NOT_DISPLAY_DESKTOP_ICONS = "set_not_display_desktop_icons";
    private static final String UNINSTALLER_APP = "uninstaller_app";
    private static final String UNINSTALLER_PKG = "uninstaller_pkg";
    private static final String UNINSTALLER_ALLUSERS = "uninstaller_allusers";
    private static final String UNINSTALLER_SILENCE = "uninstaller_pkg_silence";
    private static final String SET_TIME = "set_time";
    private static final String REBOOT_DEVICE_KEY = "reboot_device";
    private static final String SHUTDOWN_DEVICE_KEY = "shutdown_device";
    private static final String SET_TRICOLOR_LAMP = "set_tricolor_lamp";
    private static final String DEFAULT_OPEN = "default_open";

    public MDMApi(Context context) {
        this.mContext = context;
        this.resolver = this.mContext.getContentResolver();
    }

    public List<String> getMeidAndImei() {
        List<String> res = null;
        String meidImei = null;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras = this.resolver.call(ACTION_MDM_URI, "get_meid_imei", (String) null, extras);
            if (extras != null) {
                meidImei = extras.getString("get_meid_imei", (String) null);
            }

            if (!TextUtils.isEmpty(meidImei)) {
                String[] ids = meidImei.split(",");
                res = Arrays.asList(ids);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

        return res;
    }

    public List<String> getDisableInstallPkgs() {
        List<String> res = null;
        String pkgs = null;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras = this.resolver.call(ACTION_MDM_URI, "get_disable_install_pkgs", (String) null, extras);
            if (extras != null) {
                pkgs = extras.getString("get_disable_install_pkgs", (String) null);
            }

            if (!TextUtils.isEmpty(pkgs)) {
                String[] pkgArray = pkgs.split(",");
                res = Arrays.asList(pkgArray);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

        return res;
    }

    public void setDisableInstallPkgs(List<String> pkgList) {
        if (pkgList != null && pkgList.size() > 0) {
            if (this.resolver == null) {
                this.resolver = this.mContext.getContentResolver();
            }

            String pkgs = "";

            String pkg;
            for (Iterator i$ = pkgList.iterator(); i$.hasNext(); pkgs = pkgs + pkg + ",") {
                pkg = (String) i$.next();
            }

            int len = pkgs.length();
            pkgs = pkgs.substring(0, len - 1);
            if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
                Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
                Bundle extras = new Bundle();
                extras.putString("set_disable_install_pkgs", pkgs);
                this.resolver.call(ACTION_MDM_URI, "set_disable_install_pkgs", (String) null, extras);
            } else {
                Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
            }

        }
    }

    public boolean isDisableInstallAllApk() {
        boolean isDisable = false;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras = this.resolver.call(ACTION_MDM_URI, "is_disable_install_all_app", (String) null, extras);
            if (extras != null) {
                isDisable = extras.getBoolean("is_disable_install_all_app", false);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

        return isDisable;
    }

    public void setDisableInstallAllApk(boolean isDisable) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putBoolean("set_disable_install_all_app", isDisable);
            this.resolver.call(ACTION_MDM_URI, "set_disable_install_all_app", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public boolean isDisableStatusBar() {
        boolean isDisable = false;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras = this.resolver.call(ACTION_MDM_URI, "is_disabled_statusbar", (String) null, extras);
            if (extras != null) {
                isDisable = extras.getBoolean("is_disabled_statusbar", false);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

        return isDisable;
    }

    public void setDisableStatusBar(boolean isDisable) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putBoolean("set_disabled_statusbar", isDisable);
            this.resolver.call(ACTION_MDM_URI, "set_disabled_statusbar", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public List<String> getNotDisplayDesktopIcons() {
        List<String> res = null;
        String pkgs = null;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras = this.resolver.call(ACTION_MDM_URI, "get_not_display_desktop_icons", (String) null, extras);
            if (extras != null) {
                pkgs = extras.getString("get_not_display_desktop_icons", (String) null);
            }

            if (!TextUtils.isEmpty(pkgs)) {
                String[] pkgArray = pkgs.split(",");
                res = Arrays.asList(pkgArray);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

        return res;
    }

    public void setNotDisplayDesktopIcons(List<String> pkgList) {
        if (pkgList != null && pkgList.size() > 0) {
            if (this.resolver == null) {
                this.resolver = this.mContext.getContentResolver();
            }

            String pkgs = "";

            String pkg;
            for (Iterator i$ = pkgList.iterator(); i$.hasNext(); pkgs = pkgs + pkg + ",") {
                pkg = (String) i$.next();
            }

            int len = pkgs.length();
            pkgs = pkgs.substring(0, len - 1);
            if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
                Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
                Bundle extras = new Bundle();
                extras.putString("set_not_display_desktop_icons", pkgs);
                this.resolver.call(ACTION_MDM_URI, "set_not_display_desktop_icons", (String) null, extras);
            } else {
                Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
            }

        }
    }

    public void uninstallApp(String pkg, boolean allUsers, boolean isSilence) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putString("uninstaller_pkg", pkg);
            extras.putBoolean("uninstaller_allusers", allUsers);
            extras.putBoolean("uninstaller_pkg_silence", isSilence);
            this.resolver.call(ACTION_MDM_URI, "uninstaller_app", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public void setTime(long timeMillis) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putLong("set_time", timeMillis);
            this.resolver.call(ACTION_MDM_URI, "set_time", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public void reboot() {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            this.resolver.call(ACTION_MDM_URI, "reboot_device", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public void shutdown() {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            this.resolver.call(ACTION_MDM_URI, "shutdown_device", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public void setTricolor(int value) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }

        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putInt("set_tricolor_lamp", value);
            this.resolver.call(ACTION_MDM_URI, "set_tricolor_lamp", (String) null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }

    }

    public void install(String path, boolean runAfterInstall) {
        File apkFile = new File(path);
        if (!apkFile.exists()) {
            return;
        }

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 24) {
            Log.w(TAG, "版本大于 N ，开始使用 fileProvider 进行安装…");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            // 安装结束是否打开应用
            intent.putExtra(DEFAULT_OPEN, runAfterInstall);
            Uri contentUri = FileProvider.getUriForFile(mContext,
                    "com.wubin.otg.otgmaster.fileprovider", apkFile);
            intent.setDataAndType(contentUri,
                    "application/vnd.android.package-archive-silence");
        } else {
            Log.w(TAG, "正常进行安装…");
            intent.setDataAndType(Uri.fromFile(apkFile),
                    "application/vnd.android.package-archive-silence");
        }
        mContext.startActivity(intent);
    }

    public boolean hasAdbEnable() {
        boolean isEnabled = false;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }
        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras = this.resolver.call(ACTION_MDM_URI, "get_adb_state", null, extras);
            if (extras != null) {
                isEnabled = extras.getBoolean("get_adb_state", false);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }
        return isEnabled;
    }

    public void setAdbEnable(boolean enable) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }
        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putBoolean("set_adb_state", enable);
            this.resolver.call(ACTION_MDM_URI, "set_adb_state", null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }
    }

    public boolean hasVirtualKeyEnable(String key) {
        boolean isEnabled = false;
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }
        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putString("get_key_state", key);
            extras = this.resolver.call(ACTION_MDM_URI, "get_key_state", null, extras);
            if (extras != null) {
                isEnabled = extras.getBoolean("get_key_state", false);
            }
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }
        return !isEnabled;
    }

    public void setVirtualKeyEnable(String key, boolean enable) {
        if (this.resolver == null) {
            this.resolver = this.mContext.getContentResolver();
        }
        if (this.resolver.acquireContentProviderClient(ACTION_MDM_URI) != null) {
            Log.d("act-mdm", "find provider " + ACTION_MDM_URI);
            Bundle extras = new Bundle();
            extras.putString("set_key_state_key", key);
            extras.putBoolean("set_key_state_value", !enable);
            this.resolver.call(ACTION_MDM_URI, "set_key_state", null, extras);
        } else {
            Log.d("act-mdm", "Could Not Find Provider " + ACTION_MDM_URI);
        }
    }

}
