/**
   * this function will initialize the HuanXin SDK
   *
   * @return boolean true if caller can continue to call HuanXin related APIs after calling onInit,
   *     otherwise false.
   *     <p>环信初始化SDK帮助函数 返回true如果正确初始化,否则false,如果返回为false,请在后续的调用中不要调用任何和环信相关的代码
   *     <p>for example: 例子:
   *     <p>public class DemoHXSDKHelper extends HXSDKHelper
   *     <p>HXHelper = new DemoHXSDKHelper(); if(HXHelper.onInit(context)){ // do HuanXin related
   *     work }
   */
  public synchronized boolean onInit(Context context) {
    if (sdkInited) {
      return true;
    }

    appContext = context;

    // create HX SDK model
    hxModel = createModel();

    // create a defalut HX SDK model in case subclass did not provide the model
    if (hxModel == null) {
      hxModel = new DefaultHXSDKModel(appContext);
    }

    int pid = android.os.Process.myPid();
    String processAppName = getAppName(pid);

    Log.d(TAG, "process app name : " + processAppName);

    // 如果app启用了远程的service,此application:onCreate会被调用2次
    // 为了防止环信SDK被初始化2次,加此判断会保证SDK被初始化1次
    // 默认的app会在以包名为默认的process name下运行,如果查到的process name不是app的process name就立即返回
    if (processAppName == null || !processAppName.equalsIgnoreCase(hxModel.getAppProcessName())) {
      Log.e(TAG, "enter the service process!");

      // 则此application::onCreate 是被service 调用的,直接返回
      return false;
    }

    // 初始化环信SDK,一定要先调用init()
    EMChat.getInstance().init(context);

    // 设置sandbox测试环境
    if (hxModel.isSandboxMode()) {
      EMChat.getInstance().setEnv(EMEnvMode.EMSandboxMode);
    }

    if (hxModel.isDebugMode()) {
      // set debug mode in development process
      EMChat.getInstance().setDebugMode(true);
    }

    Log.d(TAG, "initialize EMChat SDK");

    initHXOptions();
    initListener();

    syncGroupsListeners = new ArrayList<HXSyncListener>();
    syncContactsListeners = new ArrayList<HXSyncListener>();

    isGroupsSyncedWithServer = hxModel.isGroupsSynced();
    isContactsSyncedWithServer = hxModel.isContactSynced();

    sdkInited = true;
    return true;
  }
  synchronized void reset() {
    isSyncingGroupsWithServer = false;
    isSyncingContactsWithServer = false;

    hxModel.setGroupsSynced(false);
    hxModel.setContactSynced(false);

    isGroupsSyncedWithServer = false;
    isContactsSyncedWithServer = false;

    alreadyNotified = false;
  }
Beispiel #3
0
 public void setHXId(String hxId) {
   if (hxId != null) {
     if (hxModel.saveHXId(hxId)) {
       this.hxId = hxId;
     }
   }
 }
Beispiel #4
0
  /**
   * please make sure you have to get EMChatOptions by following method and set related options
   * EMChatOptions options = EMChatManager.getInstance().getChatOptions();
   */
  protected void initHXOptions() {
    Log.d(TAG, "init HuanXin Options");

    // 获取到EMChatOptions对象
    EMChatOptions options = EMChatManager.getInstance().getChatOptions();
    // 默认添加好友时,是不需要验证的,改成需要验证
    options.setAcceptInvitationAlways(hxModel.getAcceptInvitationAlways());
    // 默认环信是不维护好友关系列表的,如果app依赖环信的好友关系,把这个属性设置为true
    options.setUseRoster(hxModel.getUseHXRoster());
    // 设置是否需要已读回执
    options.setRequireAck(hxModel.getRequireReadAck());
    // 设置是否需要已送达回执
    options.setRequireDeliveryAck(hxModel.getRequireDeliveryAck());
    // 设置从db初始化加载时, 每个conversation需要加载msg的个数
    options.setNumberOfMessagesLoaded(1);

    notifier = createNotifier();
    notifier.init(appContext);

    notifier.setNotificationInfoProvider(getNotificationListener());
  }
Beispiel #5
0
  /** 手机震动和声音提示 */
  public void viberateAndPlayTone(EMMessage message) {
    if (message != null) {
      if (EMChatManager.getInstance().isSlientMessage(message)) {
        return;
      }
    }

    com.easemob.applib.model.HXSDKModel model = HXSDKHelper.getInstance().getModel();
    if (!model.getSettingMsgNotification()) {
      return;
    }

    if (System.currentTimeMillis() - lastNotifiyTime < 1000) {
      // received new messages within 2 seconds, skip play ringtone
      return;
    }

    try {
      lastNotifiyTime = System.currentTimeMillis();

      // 判断是否处于静音模式
      if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
        EMLog.e(TAG, "in slient mode now");
        return;
      }

      if (model.getSettingMsgVibrate()) {
        long[] pattern = new long[] {0, 180, 80, 120};
        vibrator.vibrate(pattern, -1);
      }

      if (model.getSettingMsgSound()) {
        if (ringtone == null) {
          Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

          ringtone = RingtoneManager.getRingtone(appContext, notificationUri);
          if (ringtone == null) {
            EMLog.d(TAG, "cant find ringtone at:" + notificationUri.getPath());
            return;
          }
        }

        if (!ringtone.isPlaying()) {
          String vendor = Build.MANUFACTURER;

          ringtone.play();
          // for samsung S3, we meet a bug that the phone will
          // continue ringtone without stop
          // so add below special handler to stop it after 3s if
          // needed
          if (vendor != null && vendor.toLowerCase().contains("samsung")) {
            Thread ctlThread =
                new Thread() {
                  public void run() {
                    try {
                      Thread.sleep(3000);
                      if (ringtone.isPlaying()) {
                        ringtone.stop();
                      }
                    } catch (Exception e) {
                    }
                  }
                };
            ctlThread.run();
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Beispiel #6
0
 public void setPassword(String password) {
   if (hxModel.savePassword(password)) {
     this.password = password;
   }
 }
Beispiel #7
0
 public String getPassword() {
   if (password == null) {
     password = hxModel.getPwd();
   }
   return password;
 }
Beispiel #8
0
 public String getHXId() {
   if (hxId == null) {
     hxId = hxModel.getHXId();
   }
   return hxId;
 }