package com.uiui.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.text.TextUtils; import android.util.Log; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import com.tencent.mmkv.MMKV; import com.uiui.aios.bean.MapBean; import com.uiui.aios.config.CommonConfig; import com.uiui.aios.disklrucache.CacheHelper; import com.uiui.sn.IGetInfoInterface; import java.lang.reflect.Type; public class RemoteManager { private static final String TAG = RemoteManager.class.getSimpleName(); @SuppressLint("StaticFieldLeak") private static RemoteManager sInstance; private Context mContext; private MMKV mMMKV = MMKV.defaultMMKV(); private CacheHelper mCacheHelper; private IGetInfoInterface getInfoInterface; 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"); getInfoInterface = IGetInfoInterface.Stub.asInterface(service); getLocation(); } @Override public void onServiceDisconnected(ComponentName name) { Log.e(TAG, "onServiceDisconnected: mIGetInfoConnection"); getInfoInterface = 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; } private void bindInfoService() { if (getInfoInterface == null) { //这是连接aidl服务的代码 Intent intent = new Intent(); intent.setAction("com.uiui.sn.IGetInfoInterface"); intent.setPackage("com.uiui.sn"); intent.setComponent(new ComponentName("com.uiui.sn", "com.uiui.sn.service.RemoteService")); mContext.bindService(intent, mIGetInfoConnection, Context.BIND_AUTO_CREATE); } } public void getLocation() { if (getInfoInterface != null) { try { String jsonString = getInfoInterface.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, 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(); } } }