NIMSDK-AOS  10.8.0
NosTokenSceneConfig.java
浏览该文件的文档.
1 package com.netease.nimlib.sdk;
2 
3 import android.text.TextUtils;
4 
5 import com.netease.nimlib.NimNosSceneKeyConstant;
6 
7 import java.util.HashMap;
8 
9 /**
10  * SDK nos token场景配置,不配置的话,走默认值,默认值只有以下3种场景:
11  *
12  * @see NimNosSceneKeyConstant#NIM_DEFAULT_PROFILE
13  * @see NimNosSceneKeyConstant#NIM_DEFAULT_IM
14  * @see NimNosSceneKeyConstant#NIM_SYSTEM_NOS_SCENE
15  * <p>
16  * <p>
17  * 默认场景对应的默认过期时间为{@link #NEVER_EXPIRE},用户可以修改默认场景的过期时间。
18  * 同时用户可以新增自己的场景并指定对应的过期时间(sceneKey-> expireTimeByDay),目前最多10个.
19  * <p>
20  * 用户发送消息或直接上传文件等情况下都可以任意指定相应的场景 ,不指定走默认的。
21  * 如果要指定相应的场景,只需要指定相应的sceneKey 即可
22  * <p>
23  * 强烈建议在配置时,将相应的sceneKey常量化,以方便使用,参考{@link NimNosSceneKeyConstant}
24  */
25 public class NosTokenSceneConfig {
26 
27  /**
28  * 永不过期,资源一直在于服务器
29  */
30  public static final long NEVER_EXPIRE = 0L;
31 
32  public static final long DAY_SECOND = 60 * 60 * 24L;
33  private static final int MIN_CUSTOM_SCENE_COUNT = 10;
34  private static final int DEFAULT_CUSTOM_SCENE_COUNT = MIN_CUSTOM_SCENE_COUNT;
35  private final static HashMap<String, Long> DEFAULT_SCENE = new HashMap<>();
36 
37  private static int sDefaultSceneCount;
38  private static NosTokenSceneConfig sDefault = new NosTokenSceneConfig();
39 
40  private HashMap<String, Long> mNosTokenScene;
41 
42  private int maxCustomNosSceneCount = DEFAULT_CUSTOM_SCENE_COUNT;
43 
44  static {
45  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_DEFAULT_PROFILE, NEVER_EXPIRE);
46  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_DEFAULT_IM, NEVER_EXPIRE);
47  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_SYSTEM_NOS_SCENE, NEVER_EXPIRE);
48  DEFAULT_SCENE.put(NimNosSceneKeyConstant.NIM_SECURITY_PREFIX, NEVER_EXPIRE);
49  sDefaultSceneCount = DEFAULT_SCENE.size();
50  }
51 
52 
54  mNosTokenScene = new HashMap<>();
55  mNosTokenScene.putAll(DEFAULT_SCENE);
56  }
57 
58  /**
59  * 获取自定义NOS场景最大数量
60  * @return
61  */
63  return maxCustomNosSceneCount;
64  }
65 
66  /**
67  * 更新默认场景 {@link NimNosSceneKeyConstant#NIM_DEFAULT_PROFILE}对应的过期时间
68  *
69  * @param expireTime 过期时间,0表示永不过期,单位: 天
70  * @return NosTokenSceneConfig
71  */
72  public void updateDefaultProfileSceneExpireTime(int expireTime) {
73  if (expireTime < 0) {
74  throw new IllegalArgumentException("expireTimeByDay must >= 0");
75  }
76  mNosTokenScene.put(NimNosSceneKeyConstant.NIM_DEFAULT_PROFILE, expireTime * DAY_SECOND);
77  }
78 
79  /**
80  * 更新默认场景 {@link NimNosSceneKeyConstant#NIM_DEFAULT_IM }对应的过期时间
81  *
82  * @param expireTime 过期时间,0表示永不过期,单位: 天
83  * @return NosTokenSceneConfig
84  */
85  public void updateDefaultIMSceneExpireTime(int expireTime) {
86  if (expireTime < 0) {
87  throw new IllegalArgumentException("expireTimeByDay must >= 0");
88  }
89  mNosTokenScene.put(NimNosSceneKeyConstant.NIM_DEFAULT_IM, expireTime * DAY_SECOND);
90  }
91 
92 
93  /**
94  * 增加自定义场景
95  *
96  * @param sceneKey 自定义场景key,使用时需要带上相应的sceneKey
97  * @param expireTime 过期时间,0表示永不过期,单位: 天
98  * @return NosTokenSceneConfig
99  */
100  public NosTokenSceneConfig appendCustomScene(String sceneKey, int expireTime) {
101  return appendCustomSceneByMilliseconds(sceneKey, expireTime * DAY_SECOND);
102  }
103 
104  public NosTokenSceneConfig appendCustomSceneByMilliseconds(String sceneKey, long expireTime) {
105  if (expireTime < 0 || TextUtils.isEmpty(sceneKey)) {
106  throw new IllegalArgumentException("sceneKey must not empty and expireTimeByDay must >= 0");
107  }
108  if (mNosTokenScene.size() - sDefaultSceneCount >= maxCustomNosSceneCount) {
109  throw new IllegalStateException("the custom scene count must <= " + maxCustomNosSceneCount);
110  }
111  if (NimNosSceneKeyConstant.NIM_SYSTEM_NOS_SCENE.equals(sceneKey)) {
112  throw new IllegalArgumentException("the \"nim_system_nos_scene\" scene cannot be modified");
113  }
114  mNosTokenScene.put(sceneKey, expireTime);
115  return this;
116  }
117 
118  /**
119  * 获取所有场景副本
120  *
121  * @return 包含所有场景的HashMap
122  */
123  public HashMap<String, Long> getNosTokenScene() {
124  HashMap<String, Long> copy = new HashMap<>(mNosTokenScene.size());
125  copy.putAll(mNosTokenScene);
126  return copy;
127  }
128 
130  sDefault.mNosTokenScene = DEFAULT_SCENE;
131  return sDefault;
132  }
133 }
void updateDefaultProfileSceneExpireTime(int expireTime)
更新默认场景 NimNosSceneKeyConstant#NIM_DEFAULT_PROFILE对应的过期时间
static final long NEVER_EXPIRE
永不过期,资源一直在于服务器
NosTokenSceneConfig appendCustomSceneByMilliseconds(String sceneKey, long expireTime)
NosTokenSceneConfig appendCustomScene(String sceneKey, int expireTime)
增加自定义场景
HashMap< String, Long > getNosTokenScene()
获取所有场景副本
int getMaxCustomNosSceneCount()
获取自定义NOS场景最大数量
SDK nos token场景配置,不配置的话,走默认值,默认值只有以下3种场景:
void updateDefaultIMSceneExpireTime(int expireTime)
更新默认场景 NimNosSceneKeyConstant#NIM_DEFAULT_IM对应的过期时间