HuaWeiSDK.java 8.6 KB
package com.sunvote.sdk;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.IBinder;
import android.os.RemoteException;

import com.sunvote.basebridge.IPadSystem;

import java.util.List;

public class HuaWeiSDK {

    private Activity activity;
    private IPadSystem iPadSystem;

    private ServiceConnection serviceConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            iPadSystem = IPadSystem.Stub.asInterface(service);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            iPadSystem = null;
        }
    };

    private HuaWeiSDK(Activity activity) {
        this.activity = activity;
    }

    private static HuaWeiSDK instance = null ;

    public static HuaWeiSDK getInstance(Activity activity){
        if(instance == null){
            synchronized(HuaWeiSDK.class){
                if(instance == null){
                    instance = new HuaWeiSDK(activity);
                }
            }
         }
         instance.activity = activity;
         return instance;
    }

    /**
     * 必须在同一个activity里面开启和关闭,如果在一个里面没有关闭,而在另外一个开启,会报错。
     * 建议:在activity onCreate 里面调用open,在ondestroy 里面调用close
     * 调用功能前 必须先open,不调用了 需要close
     */
    public void open() {
        bindService();
    }

    /**
     * 必须在同一个activity里面开启和关闭,如果在一个里面没有关闭,而在另外一个开启,会报错。
     * 建议:在activity onCreate 里面调用open,在ondestroy 里面调用close
     * 调用功能前 必须先open,不调用了 需要close
     */
    public void close(){
        activity.unbindService(serviceConnection);
    }

    private void bindService() {
        Intent intent = new Intent();
        intent.setPackage("com.sunvote.basebridge");
        intent.setAction("com.sunvote.basebridge.service.PadSystemService");
        Intent tintent = getExplicitIntent(activity, intent);
        if(tintent != null){
            activity.bindService(getExplicitIntent(activity, intent), serviceConnection, Context.BIND_AUTO_CREATE);
        }
    }

    public Intent getExplicitIntent(Context context, Intent implicitIntent) {
        PackageManager pm = context.getPackageManager();
        List<ResolveInfo> resolveInfos = pm.queryIntentServices(implicitIntent, 0);
        if (resolveInfos == null || resolveInfos.size() != 1) {
            return null;
        }
        Intent explicitIntent = null;
        ResolveInfo info = resolveInfos.get(0);
        String packageName = info.serviceInfo.packageName;
        String className = info.serviceInfo.name;
        ComponentName component = new ComponentName(packageName, className);
        explicitIntent = new Intent(implicitIntent);
        explicitIntent.setComponent(component);
        return explicitIntent;
    }

    /**
     * 控制系统关机
     */
    public boolean powerOffXPad() {
        if (iPadSystem != null) {
            try {
                return iPadSystem.powerOffXPad();
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 控制系统重启
     *
     * @return true 成功
     * false 失败
     */
    public boolean rebootXPad() {
        if (iPadSystem != null) {
            try {
                return iPadSystem.rebootXPad();
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 控制系统禁/启用通知栏
     *
     * @return true 成功
     * false 失败
     */
    public boolean setNavgation(boolean visible) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setNavgation(visible);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 是否激活
     *
     * @return true 成功
     * false 失败
     */
    public boolean isActiveMe() {
        if (iPadSystem != null) {
            try {
                return iPadSystem.isActiveMe();
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }


    public boolean setStatusBarExpandPanelDisabled(boolean disable) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setStatusBarExpandPanelDisabled(disable);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    public boolean setNavigationBarDisabled(boolean disable) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setNavigationBarDisabled(disable);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 控制系统禁/启用Home键
     *
     * @return true 成功
     * false 失败
     */
    public boolean setHomeButtonDisabled(boolean disable) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setHomeButtonDisabled(disable);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 控制系统禁/启用BACK键
     *
     * @return true 成功
     * false 失败
     */
    public boolean setBackButtonDisabled(boolean disable) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setBackButtonDisabled(disable);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 控制系统禁/启用power off键
     *
     * @return true 成功
     * false 失败
     */
    public boolean setPowerDisabled(boolean disable) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setPowerDisabled(disable);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 控制系统禁/启用最近使用栏
     *
     * @return true 成功
     * false 失败
     */
    public boolean setTaskButtonDisabled(boolean disable) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.setTaskButtonDisabled(disable);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    public boolean installPackage(String packagePath) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.installPackage(packagePath);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    public boolean uninstallPackage(String packageName, boolean keepData) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.uninstallPackage(packageName, keepData);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    public boolean clearPackageData(String packageName) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.clearPackageData(packageName);
            } catch (RemoteException e) {
                return false;
            }
        }
        return false;
    }

    /**
     * 注册开机启动程序
     * packageName 启动名称
     * activi 是否激活
     *
     * @return 0 表示应用成功
     * 1 下次生效
     * 2 表示已成功
     * -1 失败
     */
    public int registerBootStarted(String packageName, String activity, boolean activi) {
        if (iPadSystem != null) {
            try {
                return iPadSystem.registerBootStarted(packageName, activity, activi);
            } catch (RemoteException e) {
                return -1;
            }
        }
        return -1;
    }

    /**
     * 移除其他所有开机启动程序
     *
     * @return 0 成功
     * <0 失败
     * >=0 成功
     */
    public int removeAllBootStarted() {
        if (iPadSystem != null) {
            try {
                return iPadSystem.removeAllBootStarted();
            } catch (RemoteException e) {
                return -1;
            }
        }
        return -1;
    }
}