257 lines
9.0 KiB
Java
257 lines
9.0 KiB
Java
package com.uiui.zyos.manager;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.ServiceConnection;
|
|
import android.os.IBinder;
|
|
import android.os.RemoteException;
|
|
import android.provider.Settings;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.alibaba.sdk.android.push.CloudPushService;
|
|
import com.alibaba.sdk.android.push.CommonCallback;
|
|
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
|
|
import com.tencent.bugly.crashreport.CrashReport;
|
|
import com.tencent.mmkv.MMKV;
|
|
import com.uiui.zy.IGetInfoInterface;
|
|
import com.uiui.zyos.config.CommonConfig;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
|
|
public class RemoteManager {
|
|
private static final String TAG = "RemoteManager";
|
|
|
|
@SuppressLint("StaticFieldLeak")
|
|
private static RemoteManager sInstance;
|
|
private Context mContext;
|
|
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
|
|
private static boolean mServiceConnected = false;
|
|
|
|
private IGetInfoInterface mGetInfoInterface;
|
|
private ServiceConnection mServiceConnection;
|
|
|
|
private static final String SN_AIDL_NAME = "com.uiui.zy.IGetInfoInterface";
|
|
public static final String SN_PACKAGE_NAME = "com.uiui.zy";
|
|
private static final String SN_SERVICE_NAME = "com.uiui.zy.service.RemoteService";
|
|
|
|
private static final String SN_KEY = "sn_serial_key";
|
|
|
|
private RemoteManager(Context context) {
|
|
if (context == null) {
|
|
throw new RuntimeException("Context is NULL");
|
|
}
|
|
this.mContext = context;
|
|
mServiceConnection = new ServiceConnection() {
|
|
@Override
|
|
public void onServiceConnected(ComponentName name, IBinder service) {
|
|
Log.e(TAG, "onServiceConnected: mIGetInfoConnection");
|
|
mGetInfoInterface = IGetInfoInterface.Stub.asInterface(service);
|
|
mServiceConnected = true;
|
|
for (ConnectedListener listener : mListeners) {
|
|
listener.onRemoteConnected();
|
|
}
|
|
try {
|
|
String sn = mGetInfoInterface.getSerial();
|
|
CrashReport.setDeviceId(mContext, sn);
|
|
mMMKV.encode(SN_KEY, sn);
|
|
Log.e(TAG, "onServiceConnected: sn = " + sn);
|
|
} catch (RemoteException e) {
|
|
e.printStackTrace();
|
|
}
|
|
try {
|
|
List<String> pkgs = mGetInfoInterface.getAdminApp();
|
|
Log.e(TAG, "onServiceConnected: pkgs = " + pkgs);
|
|
mMMKV.encode(CommonConfig.ADMIN_APP_LIST, new HashSet<>(pkgs));
|
|
} catch (RemoteException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
Log.e(TAG, "onServiceConnected: " + getSerial());
|
|
aliyunPushInit();
|
|
}
|
|
|
|
@Override
|
|
public void onServiceDisconnected(ComponentName name) {
|
|
Log.e(TAG, "onServiceDisconnected: mIGetInfoConnection");
|
|
//置空,重连
|
|
mGetInfoInterface = null;
|
|
mServiceConnected = false;
|
|
bindInfoService();
|
|
}
|
|
};
|
|
bindInfoService();
|
|
}
|
|
|
|
public static void init(Context context) {
|
|
if (sInstance == null) {
|
|
Log.e(TAG, "init: ");
|
|
sInstance = new RemoteManager(context);
|
|
}
|
|
}
|
|
|
|
public static RemoteManager getInstance() {
|
|
if (sInstance == null) {
|
|
throw new IllegalStateException("You must be init RemoteManager first");
|
|
}
|
|
return sInstance;
|
|
}
|
|
|
|
private void bindInfoService() {
|
|
if (mGetInfoInterface == null) {
|
|
//这是连接aidl服务的代码
|
|
Intent intent = new Intent();
|
|
intent.setAction(SN_AIDL_NAME);
|
|
intent.setPackage(SN_PACKAGE_NAME);
|
|
intent.setComponent(new ComponentName(SN_PACKAGE_NAME, SN_SERVICE_NAME));
|
|
mContext.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
|
|
}
|
|
}
|
|
|
|
public interface ConnectedListener {
|
|
void onRemoteConnected();
|
|
}
|
|
|
|
private static Set<ConnectedListener> mListeners = new HashSet<>();
|
|
|
|
public static void setListener(ConnectedListener listener) {
|
|
mListeners.add(listener);
|
|
if (mServiceConnected) {
|
|
listener.onRemoteConnected();
|
|
}
|
|
}
|
|
|
|
public static void removeListener(ConnectedListener listener) {
|
|
mListeners.remove(listener);
|
|
}
|
|
|
|
public void aliyunPushInit() {
|
|
String sn = getSerial();
|
|
if (TextUtils.isEmpty(sn)) {
|
|
Log.e(TAG, "aliyunPushInit: empty");
|
|
return;
|
|
}
|
|
PushServiceFactory.init(mContext);
|
|
CloudPushService pushService = PushServiceFactory.getCloudPushService();
|
|
pushService.register(mContext, new CommonCallback() {
|
|
@Override
|
|
public void onSuccess(String response) {
|
|
Log.e("AliyunPush", "init cloudchannel success");
|
|
Log.e("AliyunPush", "init cloudchannel success " + pushService.getDeviceId());
|
|
pushService.bindAccount(sn, new CommonCallback() {
|
|
@Override
|
|
public void onSuccess(String s) {
|
|
Log.e("AliyunPush", "bind account " + sn + " success\n");
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(String errorCode, String errorMsg) {
|
|
Log.e("AliyunPush", "bind account " + sn + " failed." +
|
|
"errorCode: " + errorCode + ", errorMsg:" + errorMsg);
|
|
}
|
|
});
|
|
pushService.addAlias(sn, new CommonCallback() {
|
|
@Override
|
|
public void onSuccess(String s) {
|
|
Log.e("AliyunPush", "add alias " + sn + " success\n");
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(String errorCode, String errorMsg) {
|
|
Log.e("AliyunPush", "add alias " + sn + " failed." +
|
|
"errorCode: " + errorCode + ", errorMsg:" + errorMsg + "\n");
|
|
}
|
|
});
|
|
}
|
|
|
|
@Override
|
|
public void onFailed(String errorCode, String errorMessage) {
|
|
Log.e("AliyunPush", "init cloudchannel failed -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return 获取sn
|
|
*/
|
|
public String getSerial() {
|
|
if (mGetInfoInterface != null) {
|
|
try {
|
|
return mGetInfoInterface.getSerial();
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "getSerial: " + e.getMessage());
|
|
}
|
|
} else {
|
|
bindInfoService();
|
|
}
|
|
return mMMKV.decodeString(SN_KEY, "");
|
|
}
|
|
|
|
public String getAppUsedStatistics() {
|
|
if (mGetInfoInterface != null) {
|
|
try {
|
|
String jsonString = mGetInfoInterface.getAppUsedStatistics();
|
|
return jsonString;
|
|
} catch (Exception e) {
|
|
Log.e(TAG, "getAppUsedStatistics: " + e.getMessage());
|
|
}
|
|
} else {
|
|
bindInfoService();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public boolean getSnIsActivation() {
|
|
try {
|
|
return mGetInfoInterface.SnIsActivation();
|
|
} catch (RemoteException e) {
|
|
e.printStackTrace();
|
|
Log.e(TAG, "getSnIsActivation: " + e.getMessage());
|
|
int is_activation = Settings.Global.getInt(mContext.getContentResolver(), CommonConfig.UIUI_ACTIVATION_KEY, 0);
|
|
return is_activation == 1;
|
|
}
|
|
}
|
|
|
|
public void setDefaultDesktop(String pkg) {
|
|
if (mGetInfoInterface != null) {
|
|
try {
|
|
mGetInfoInterface.setDefaultLauncher(pkg);
|
|
} catch (RemoteException e) {
|
|
Log.e(TAG, "setDefaultDesktop: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public List<String> getAdminApp() {
|
|
if (mGetInfoInterface != null) {
|
|
try {
|
|
List<String> pkgs = mGetInfoInterface.getAdminApp();
|
|
return pkgs;
|
|
} catch (RemoteException e) {
|
|
Log.e(TAG, "setDefaultDesktop: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
Set<String> packages = mMMKV.decodeStringSet(CommonConfig.ADMIN_APP_LIST, new HashSet<>());
|
|
return new ArrayList<>(packages);
|
|
}
|
|
|
|
public boolean putSecureInt(String key, int value) {
|
|
if (mGetInfoInterface != null) {
|
|
try {
|
|
return mGetInfoInterface.putSecureInt(key, value);
|
|
} catch (RemoteException e) {
|
|
Log.e(TAG, "putSecureInt: " + e.getMessage());
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|