package com.uiuios.aios.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.text.TextUtils; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.tencent.bugly.crashreport.CrashReport; import com.tencent.mmkv.MMKV; import com.uiuios.aios.BuildConfig; import com.uiuios.aios.bean.MapBean; import com.uiuios.aios.config.CommonConfig; import com.uiuios.aios.disklrucache.CacheHelper; import com.uiuios.sn.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(); private static final String SN_KEY = "sn_serial_key"; @SuppressLint("StaticFieldLeak") private static RemoteManager sInstance; private Context mContext; private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE); private CacheHelper mCacheHelper; private IGetInfoInterface mGetInfoInterface; private ServiceConnection mIGetInfoConnection; private RemoteManager(Context context) { if (context == null) { throw new RuntimeException("Context is NULL"); } this.mContext = context; this.mCacheHelper = new CacheHelper(context); mIGetInfoConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName name, IBinder service) { Log.e(TAG, "onServiceConnected: mIGetInfoConnection"); mGetInfoInterface = IGetInfoInterface.Stub.asInterface(service); for (ConnectedListener listener : mListeners) { if (listener != null) { listener.onConnected(); } } try { String sn = mGetInfoInterface.getSerial(); CrashReport.setDeviceModel(mContext, sn); mMMKV.encode(SN_KEY, sn); Log.e(TAG, "onServiceConnected: sn = " + sn); } catch (RemoteException e) { e.printStackTrace(); } getLocation(); } @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "onServiceDisconnected: mIGetInfoConnection"); mGetInfoInterface = null; 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; } public interface ConnectedListener { void onConnected(); } Set mListeners = new HashSet<>(); public void setListener(ConnectedListener listener) { mListeners.add(listener); if (mGetInfoInterface != null) { listener.onConnected(); } } private void bindInfoService() { if (mGetInfoInterface == null) { //这是连接aidl服务的代码 Intent intent = new Intent(); intent.setAction("com.uiuios.sn.IGetInfoInterface"); intent.setPackage("com.uiuios.sn"); intent.setComponent(new ComponentName("com.uiuios.sn", "com.uiuios.sn.service.RemoteService")); mContext.bindService(intent, mIGetInfoConnection, Context.BIND_AUTO_CREATE); } } /** * @return 获取sn */ public String getSerial() { // if (BuildConfig.DEBUG) return "MTK0002306120556370"; if (mGetInfoInterface != null) { try { return mGetInfoInterface.getSerial(); } catch (Exception e) { Log.e(TAG, "getSerial: " + e.getMessage()); } } else { bindInfoService(); } return mMMKV.decodeString(SN_KEY, ""); } 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 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() { }.getType(); MapBean mapBean = gson.fromJson(jsonString, type); mMMKV.encode(CommonConfig.MAP_ADDRESS_KEY, mapBean.getAddress() + mapBean.getLocationDescribe()); mMMKV.encode(CommonConfig.MAP_LONGITUDE_KEY, String.valueOf(mapBean.getLongitude())); mMMKV.encode(CommonConfig.MAP_LATITUDE_KEY, String.valueOf(mapBean.getLatitude())); mMMKV.encode(CommonConfig.MAP_PROVINCE_KEY, mapBean.getProvince()); mMMKV.encode(CommonConfig.MAP_CITY_KEY, mapBean.getCity()); mMMKV.encode(CommonConfig.MAP_DISTRICT_KEY, mapBean.getDistrict()); mMMKV.encode(CommonConfig.MAP_STREET_KEY, mapBean.getTown() + mapBean.getStreet()); mMMKV.encode(CommonConfig.MAP_LOCATION_DESCRIBE_KEY, mapBean.getStreet() + mapBean.getLocationDescribe()); 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 String getDistrict() { getLocation(); MapBean mapBean = getMapBean(); if (mapBean == null) { return "北京"; } else { return 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 putSystemInt(String name, int value) { if (mGetInfoInterface != null) { try { return mGetInfoInterface.SystemPutInt(name, value); } catch (Exception e) { Log.e(TAG, "putSystemInt: " + e.getMessage()); } } else { bindInfoService(); } return false; } public void killBackgroundProcesses(String pkg) { if (mGetInfoInterface != null) { try { mGetInfoInterface.killBackgroundProcesses(pkg); } catch (Exception e) { Log.e(TAG, "killBackgroundProcesses: " + e.getMessage()); } } else { bindInfoService(); } } public String getConnectWifiSsid() { if (mGetInfoInterface != null) { try { return mGetInfoInterface.getWifiSsid(); } catch (Exception e) { Log.e(TAG, "killBackgroundProcesses: " + e.getMessage()); } } else { bindInfoService(); } return "获取失败"; } public String getBluetoothDeviceName() { if (mGetInfoInterface != null) { try { return mGetInfoInterface.getBluetoothSsid(); } catch (Exception e) { Log.e(TAG, "killBackgroundProcesses: " + e.getMessage()); } } else { bindInfoService(); } return "获取失败"; } public void openLauncher3() { if (mGetInfoInterface != null) { try { mGetInfoInterface.openLauncher3(); } catch (Exception e) { Log.e(TAG, "openLauncher3: " + e.getMessage()); } } else { bindInfoService(); } } public void setDefaultDesktop(String pkgName, String className) { if (mGetInfoInterface != null) { try { mGetInfoInterface.setDefaultDesktop(pkgName, className); } catch (Exception e) { Log.e(TAG, "setDefaultDesktop: " + e.getMessage()); } } else { bindInfoService(); } } }