@Override
  protected void onHandleIntent(Intent intent) {
    Map<String, String> eventBody = new HashMap<>();
    try {
      // get caller data
      Bundle iArgs = intent.getExtras();
      // src
      String src = iArgs.getString(AppConstants.K.SRC.name());
      eventBody.put(AppConstants.K.SRC.name(), src);

      // srcID
      String srcID = iArgs.getString(AppConstants.K.SRC_ID.name());
      eventBody.put(AppConstants.K.SRC_ID.name(), srcID);

      String notifUID = iArgs.getString(AppConstants.K.NOTIF.name());
      eventBody.put(AppConstants.K.NOTIF.name(), notifUID);

      if (StringUtils.isNotBlank(notifUID)) {
        // LOCAL UPDATE
        ArrayList<ContentProviderOperation> delOps = new ArrayList<>();
        delOps.add(
            ContentProviderOperation.newDelete(SchemaNotifications.CONTENT_URI)
                .withSelection(SchemaNotifications.COLUMN_UID + "=?", new String[] {notifUID})
                .build());
        getContentResolver().applyBatch(AppConstants.API.CONTENT_AUTHORITY.getValue(), delOps);
      } else {
        return;
      }

      // REMOTE UPDATE
      String notifURI = StringUtils.join(new String[] {NOTIFICATIONS.getValue(), notifUID}, "/");

      // body part of request
      Map<String, String> reqBody = new HashMap<>();
      if (!Utils.isLoggedIn()) {
        // UNIQUE ID FOR THIS INSTALL
        reqBody.put(PARAMS.REF.getValue(), Utils.uniqueID());
      }

      ReqSetBody req = new ReqSetBody();
      req.setToken(Utils.getAppToken(false));
      req.setCmd(PARAMS.COMMAND.getValue());
      req.setBody(reqBody);

      // headers
      HttpHeaders headers = new HttpHeaders();
      headers.setContentType(MediaType.APPLICATION_JSON);
      headers.setAccept(RestClient.INSTANCE.jsonMediaType());

      HttpEntity<ReqSetBody> payloadEntity = new HttpEntity<>(req, headers);
      RestClient.INSTANCE.handle().exchange(notifURI, HttpMethod.POST, payloadEntity, Resp.class);
    } catch (Exception e) {
      // muted
    } finally {
      BusProvider.INSTANCE.getBus().post(new Events.SingleNotifsUPDATEEvent(eventBody));
    }
  }
Esempio n. 2
0
  @Override
  protected void onHandleIntent(Intent intent) {
    Map<String, String> eventData = new HashMap<>();

    StorageComponent component = DaggerStorageComponent.create();
    ReqSetBody loginReq = component.provideReqSetBody();

    // headers
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.setAccept(RestClient.INSTANCE.jsonMediaType());
    try {
      eventData.put(AppConstants.K.provider.name(), intent.getStringExtra("provider"));

      Map<String, String> reqBody = (Map<String, String>) intent.getSerializableExtra("user");
      loginReq.setBody(reqBody);

      // set base args
      loginReq.setToken(Utils.getAppToken(true));
      loginReq.setCmd("externalSignIn");

      // call remote
      HttpEntity<ReqSetBody> payloadEntity = new HttpEntity<>(loginReq, headers);
      ResponseEntity<RespLogin> resp =
          RestClient.INSTANCE
              .handle()
              .exchange(
                  AppConstants.API.USERS_EXT.getValue(),
                  HttpMethod.POST,
                  payloadEntity,
                  RespLogin.class);
      User user = resp.getBody().getBody();
      Gson gson = new Gson();
      String userStr = gson.toJson(user);

      // register login type
      Utils.PreferencesManager.getInstance()
          .setValue(
              AppConstants.API.PREF_AUTH_PROVIDER.getValue(), intent.getStringExtra("provider"));
      Utils.PreferencesManager.getInstance()
          .setValue(AppConstants.API.PREF_AUTH_USER.getValue(), userStr);

      // broadcast event
      BusProvider.INSTANCE.getBus().post(new Events.AuthSuccessEvent(eventData));
    } catch (Exception e) {
      Resp resp = Utils.parseAsRespSilently(e);
      if (resp != null) {
        eventData.put(AppConstants.K.MESSAGE.name(), resp.getMessage());
      }
      BusProvider.INSTANCE.getBus().post(new Events.AuthFailEvent(eventData));
    }
  }