Files
Xuewang365OSNeutral/app/src/main/java/com/uiui/zyos/manager/RemoteManager.java
Fanhuitong 8b98116d87 version:1.1.8
fix:
update:优化禁用
2023-04-04 18:17:30 +08:00

309 lines
10 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.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;
import com.tencent.mmkv.MMKV;
import com.uiui.zyos.BuildConfig;
import com.uiui.zyos.bean.MapBean;
import com.uiui.zyos.config.CommonConfig;
import com.uiui.zy.IGetInfoInterface;
import java.lang.reflect.Type;
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.class.getSimpleName();
@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 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.onConnected();
}
getLocation();
try {
Log.e(TAG, "onServiceConnected: macaddr = " + mGetInfoInterface.getSerial());
} 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 onConnected();
}
private static Set<ConnectedListener> mListeners = new HashSet<>();
public static void setListener(ConnectedListener listener) {
mListeners.add(listener);
if (mServiceConnected) {
listener.onConnected();
}
}
public static void removeListener(ConnectedListener listener) {
mListeners.remove(listener);
}
public void aliyunPushInit() {
if (TextUtils.isEmpty(getSerial())) {
Log.e(TAG, "aliyunPushInit: empty");
return;
}
String account = getSerial();
CloudPushService pushService = PushServiceFactory.getCloudPushService();
pushService.bindAccount(account, new CommonCallback() {
@Override
public void onSuccess(String s) {
Log.e("AliyunPush", "bind account " + account + " success\n");
}
@Override
public void onFailed(String errorCode, String errorMsg) {
Log.e("AliyunPush", "bind account " + account + " failed." +
"errorCode: " + errorCode + ", errorMsg:" + errorMsg);
}
});
String alias = getSerial();
pushService.addAlias(account, new CommonCallback() {
@Override
public void onSuccess(String s) {
Log.e("AliyunPush", "add alias " + alias + " success\n");
}
@Override
public void onFailed(String errorCode, String errorMsg) {
Log.e("AliyunPush", "add alias " + alias + " failed." +
"errorCode: " + errorCode + ", errorMsg:" + errorMsg + "\n");
}
});
}
public void tpushInit() {
List<XGPushManager.AccountInfo> accountInfoList = new ArrayList<>();
accountInfoList.add(new XGPushManager.AccountInfo(XGPushManager.AccountType.CUSTOM.getValue(), RemoteManager.getInstance().getSerial()));
XGPushManager.upsertAccounts(mContext, accountInfoList, new XGIOperateCallback() {
@Override
public void onSuccess(Object data, int flag) {
Log.e("TPush", "onSuccess, data:" + data + ", flag:" + flag);
}
@Override
public void onFail(Object data, int errCode, String msg) {
Log.e("TPush", "onFail, data:" + data + ", code:" + errCode + ", msg:" + msg);
}
});
}
/**
* @return 获取sn
*/
public String getSerial() {
// if (BuildConfig.DEBUG) {
// return "1234567890ab";
// }
if (mGetInfoInterface != null) {
try {
return mGetInfoInterface.getSerial();
} catch (Exception e) {
Log.e(TAG, "getSerial: " + e.getMessage());
}
} else {
bindInfoService();
}
return "";
}
public void getLocation() {
if (mGetInfoInterface != null) {
try {
String jsonString = mGetInfoInterface.getMapResult();
mMMKV.encode(CommonConfig.MAP_LOCATION_JSON_KEY, jsonString);
} catch (Exception e) {
Log.e(TAG, "getMapResult: " + e.getMessage());
}
} else {
bindInfoService();
}
}
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 MapBean getMapBean() {
String jsonString = mMMKV.decodeString(CommonConfig.MAP_LOCATION_JSON_KEY);
if (TextUtils.isEmpty(jsonString)) {
return null;
} else {
Gson gson = new Gson();
Type type = new TypeToken<MapBean>() {
}.getType();
MapBean mapBean = gson.fromJson(jsonString, type);
mMMKV.encode(CommonConfig.MAP_ADDRESS_KEY, mapBean.getAddress() + mapBean.getLocationDescribe());
mMMKV.encode(CommonConfig.MAP_LONGITUDE_KEY, mapBean.getLongitude());
mMMKV.encode(CommonConfig.MAP_LATITUDE_KEY, mapBean.getLatitude());
return mapBean;
}
}
public String getCity() {
MapBean mapBean = getMapBean();
if (mapBean == null) {
getLocation();
return "北京";
} else {
return mapBean.getCity();
}
}
public String getCityDistrict() {
MapBean mapBean = getMapBean();
if (mapBean == null) {
getLocation();
return "北京";
} else {
return mapBean.getCity() + "\t" + mapBean.getDistrict();
}
}
public double getLongitude() {
MapBean mapBean = getMapBean();
if (mapBean == null) {
getLocation();
return 0.0;
} else {
return mapBean.getLongitude();
}
}
public double getLatitude() {
MapBean mapBean = getMapBean();
if (mapBean == null) {
getLocation();
return 0.0;
} else {
return mapBean.getLatitude();
}
}
public String getLocationTude() {
MapBean mapBean = getMapBean();
if (mapBean == null) {
getLocation();
return "0.0";
} else {
return mapBean.getLongitude() + "," + mapBean.getLatitude();
}
}
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) {
try {
mGetInfoInterface.setDefaultLauncher(pkg);
} catch (RemoteException e) {
Log.e(TAG, "setDefaultDesktop: " + e.getMessage());
e.printStackTrace();
}
}
}