|
1 | 1 | package tech.jiangtao.support.kit; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.content.Intent; |
4 | 5 | import io.realm.Realm; |
5 | 6 | import io.realm.RealmConfiguration; |
| 7 | +import java.lang.annotation.Annotation; |
| 8 | +import java.util.Properties; |
| 9 | +import java.util.UUID; |
| 10 | +import tech.jiangtao.support.kit.annotation.ChatRouter; |
| 11 | +import tech.jiangtao.support.kit.annotation.GroupChatRouter; |
| 12 | +import tech.jiangtao.support.kit.annotation.InvitedRouter; |
| 13 | +import tech.jiangtao.support.kit.service.SupportService; |
| 14 | +import tech.jiangtao.support.kit.service.XMPPService; |
| 15 | +import tech.jiangtao.support.kit.util.PropertyUtils; |
6 | 16 | import xiaofei.library.hermeseventbus.HermesEventBus; |
7 | 17 |
|
8 | 18 | /** |
@@ -40,29 +50,110 @@ public class SupportIM { |
40 | 50 | public static final String MUC_GROUP = "muc-group"; |
41 | 51 | public static final String GROUP = "group"; |
42 | 52 |
|
43 | | - private static void initialize(Context context){ |
| 53 | + private static String MM_APPID = "MM_APPID"; |
| 54 | + private static String MM_AppSecret = "MM_APPSECRET"; |
| 55 | + private static String PORT = "PORT"; |
| 56 | + private static String SERVICE_NAME = "SERVICE_NAME"; |
| 57 | + |
| 58 | + // server |
| 59 | + private static String HOST = "HOST"; |
| 60 | + public static String RESOURCE_ADDRESS_CONFIG = "RESOURCE_ADDRESS"; |
| 61 | + private static String API_ADDRESS_CONFIG = "API_ADDRESS"; |
| 62 | + |
| 63 | + // company |
| 64 | + private static String HOST1 = "HOST1"; |
| 65 | + public static String RESOURCE_ADDRESS1 = "RESOURCE_ADDRESS1"; |
| 66 | + private static String API_ADDRESS1 = "API_ADDRESS1"; |
| 67 | + |
| 68 | + // home |
| 69 | + private static String HOST2 = "HOST2"; |
| 70 | + public static String RESOURCE_ADDRESS2 = "RESOURCE_ADDRESS2"; |
| 71 | + private static String API_ADDRESS2 = "API_ADDRESS2"; |
| 72 | + |
| 73 | + private static void initialize(Context context) { |
| 74 | + HermesEventBus.getDefault().init(context); |
| 75 | + } |
| 76 | + |
| 77 | + public static void initialize(Context context, String resourceName) { |
| 78 | + initValue(context, resourceName); |
| 79 | + String resource = UUID.randomUUID().toString(); |
| 80 | + //---------------------------------------配置中心-------------------------------- |
| 81 | + //------------------------------------------------------------------------------ |
| 82 | + initialize(context, SERVICE_NAME, resource, HOST, Integer.parseInt(PORT), RESOURCE_ADDRESS, |
| 83 | + API_ADDRESS); |
| 84 | + Realm.init(context); |
44 | 85 | HermesEventBus.getDefault().init(context); |
45 | 86 | } |
46 | 87 |
|
47 | | - private static void initialize(Context context, String serviceName){ |
| 88 | + private static void initValue(Context context, String resourceName) { |
| 89 | + Properties properties = PropertyUtils.getProperties(context, resourceName); |
| 90 | + MM_APPID = properties.getProperty(MM_APPID); |
| 91 | + MM_AppSecret = properties.getProperty(MM_AppSecret); |
| 92 | + PORT = properties.getProperty(PORT); |
| 93 | + SERVICE_NAME = properties.getProperty(SERVICE_NAME); |
| 94 | + |
| 95 | + // 线上 |
| 96 | + HOST = properties.getProperty(HOST); |
| 97 | + RESOURCE_ADDRESS = properties.getProperty(RESOURCE_ADDRESS_CONFIG); |
| 98 | + API_ADDRESS = properties.getProperty(API_ADDRESS_CONFIG); |
| 99 | + |
| 100 | + // 公司 |
| 101 | + HOST1 = properties.getProperty(HOST1); |
| 102 | + RESOURCE_ADDRESS1 = properties.getProperty(RESOURCE_ADDRESS1); |
| 103 | + API_ADDRESS1 = properties.getProperty(API_ADDRESS1); |
| 104 | + |
| 105 | + // 家里 |
| 106 | + HOST2 = properties.getProperty(HOST2); |
| 107 | + RESOURCE_ADDRESS2 = properties.getProperty(RESOURCE_ADDRESS2); |
| 108 | + API_ADDRESS2 = properties.getProperty(API_ADDRESS2); |
| 109 | + } |
| 110 | + |
| 111 | + private static void initializeContext(Context context, String serviceName) { |
48 | 112 | initialize(context); |
49 | 113 | mDomain = serviceName; |
50 | 114 | } |
51 | 115 |
|
52 | | - private static void initialize(Context context, String serviceName, String resource){ |
53 | | - initialize(context,serviceName); |
| 116 | + private static void initialize(Context context, String serviceName, String resource) { |
| 117 | + initializeContext(context, serviceName); |
54 | 118 | mResource = resource; |
55 | 119 | } |
56 | 120 |
|
57 | | - private static void initialize(Context context, String serviceName, String resource, String host){ |
58 | | - initialize(context,serviceName,resource); |
| 121 | + private static void initialize(Context context, String serviceName, String resource, |
| 122 | + String host) { |
| 123 | + initialize(context, serviceName, resource); |
59 | 124 | mHost = host; |
60 | 125 | } |
61 | 126 |
|
62 | | - public static void initialize(Context context,String serviceName,String resource,String host,int port,String resourceAddress,String apiAddress){ |
63 | | - initialize(context,serviceName,resource,host); |
| 127 | + public static void initialize(Context context, String serviceName, String resource, String host, |
| 128 | + int port, String resourceAddress, String apiAddress) { |
| 129 | + initialize(context, serviceName, resource, host); |
64 | 130 | RESOURCE_ADDRESS = resourceAddress; |
65 | 131 | API_ADDRESS = apiAddress; |
66 | 132 | mPort = port; |
| 133 | + |
| 134 | + Class clazz = context.getApplicationContext().getClass(); |
| 135 | + Annotation[] annotations = clazz.getAnnotations(); |
| 136 | + Class chatClazz = null; |
| 137 | + Class groupChatClazz = null; |
| 138 | + Class invitedClass = null; |
| 139 | + for (int i = 0; i < annotations.length; i++) { |
| 140 | + Annotation annotation = annotations[i]; |
| 141 | + if (annotation instanceof ChatRouter) { |
| 142 | + chatClazz = ((ChatRouter) annotation).router(); |
| 143 | + } |
| 144 | + if (annotation instanceof GroupChatRouter) { |
| 145 | + groupChatClazz = ((GroupChatRouter) annotation).router(); |
| 146 | + } |
| 147 | + if (annotation instanceof InvitedRouter) { |
| 148 | + invitedClass = ((InvitedRouter) annotation).router(); |
| 149 | + } |
| 150 | + } |
| 151 | + Intent intent = new Intent(context, XMPPService.class); |
| 152 | + intent.putExtra(XMPPService.CHAT_CLASS, chatClazz); |
| 153 | + intent.putExtra(XMPPService.GROUP_CHAT_CLASS, groupChatClazz); |
| 154 | + intent.putExtra(XMPPService.INVITED_CLASS, invitedClass); |
| 155 | + context.startService(intent); |
| 156 | + Intent intent1 = new Intent(context, SupportService.class); |
| 157 | + context.startService(intent1); |
67 | 158 | } |
68 | 159 | } |
0 commit comments