Ejemplo n.º 1
0
  public void showNotifications(List<String> contacts) {
    if (!(ActivityMonitor.getInstance().getCurrentActivity() instanceof ConversationsActivity)) {
      Conversation[] conversations =
          Database.getInstance(applicationContext).getConversations(preferences.getDid());
      for (Conversation conversation : conversations) {
        if (!conversation.isUnread()
            || !contacts.contains(conversation.getContact())
            || (ActivityMonitor.getInstance().getCurrentActivity() instanceof ConversationActivity
                && ((ConversationActivity) ActivityMonitor.getInstance().getCurrentActivity())
                    .getContact()
                    .equals(conversation.getContact()))) {
          continue;
        }

        String smsContact = Utils.getContactName(applicationContext, conversation.getContact());
        if (smsContact == null) {
          smsContact = Utils.getFormattedPhoneNumber(conversation.getContact());
        }

        String allSmses = "";
        String mostRecentSms = "";
        boolean initial = true;
        for (Message message : conversation.getMessages()) {
          if (message.getType() == Message.Type.INCOMING && message.isUnread()) {
            if (initial) {
              allSmses = message.getText();
              mostRecentSms = message.getText();
              initial = false;
            } else {
              allSmses = message.getText() + "\n" + allSmses;
            }
          } else {
            break;
          }
        }

        NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(applicationContext);

        notificationBuilder.setContentTitle(smsContact);
        notificationBuilder.setContentText(mostRecentSms);
        notificationBuilder.setSmallIcon(R.drawable.ic_chat_white_24dp);
        notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
        notificationBuilder.setSound(
            Uri.parse(Preferences.getInstance(applicationContext).getNotificationSound()));
        notificationBuilder.setLights(0xFFAA0000, 1000, 5000);
        if (Preferences.getInstance(applicationContext).getNotificationVibrateEnabled()) {
          notificationBuilder.setVibrate(new long[] {0, 250, 250, 250});
        } else {
          notificationBuilder.setVibrate(new long[] {0});
        }
        notificationBuilder.setColor(0xFFAA0000);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(allSmses));

        Bitmap largeIconBitmap;
        try {
          largeIconBitmap =
              MediaStore.Images.Media.getBitmap(
                  applicationContext.getContentResolver(),
                  Uri.parse(
                      Utils.getContactPhotoUri(applicationContext, conversation.getContact())));
          largeIconBitmap = Bitmap.createScaledBitmap(largeIconBitmap, 256, 256, false);
          largeIconBitmap = Utils.applyCircularMask(largeIconBitmap);
          notificationBuilder.setLargeIcon(largeIconBitmap);
        } catch (Exception ignored) {

        }

        Intent intent = new Intent(applicationContext, ConversationActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(applicationContext);
        stackBuilder.addParentStack(ConversationActivity.class);
        stackBuilder.addNextIntent(intent);
        notificationBuilder.setContentIntent(
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT));

        Intent replyIntent = new Intent(applicationContext, ConversationQuickReplyActivity.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
          replyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
        } else {
          //noinspection deprecation
          replyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        }
        replyIntent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        PendingIntent replyPendingIntent =
            PendingIntent.getActivity(
                applicationContext, 0, replyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action.Builder replyAction =
            new NotificationCompat.Action.Builder(
                R.drawable.ic_reply_white_24dp,
                applicationContext.getString(R.string.notifications_button_reply),
                replyPendingIntent);
        notificationBuilder.addAction(replyAction.build());

        Intent markAsReadIntent = new Intent(applicationContext, MarkAsReadReceiver.class);
        markAsReadIntent.putExtra(
            applicationContext.getString(R.string.conversation_extra_contact),
            conversation.getContact());
        PendingIntent markAsReadPendingIntent =
            PendingIntent.getBroadcast(
                applicationContext, 0, markAsReadIntent, PendingIntent.FLAG_CANCEL_CURRENT);
        NotificationCompat.Action.Builder markAsReadAction =
            new NotificationCompat.Action.Builder(
                R.drawable.ic_drafts_white_24dp,
                applicationContext.getString(R.string.notifications_button_mark_read),
                markAsReadPendingIntent);
        notificationBuilder.addAction(markAsReadAction.build());

        int id;
        if (notificationIds.get(conversation.getContact()) != null) {
          id = notificationIds.get(conversation.getContact());
        } else {
          id = notificationIdCount++;
          notificationIds.put(conversation.getContact(), id);
        }
        NotificationManager notificationManager =
            (NotificationManager) applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, notificationBuilder.build());
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Enable SMS notifications by configuring the VoIP.ms URL callback, registering for GCM and
   * making the appropriate changes to the application preferences.
   *
   * @param activity The source activity.
   */
  public void enableNotifications(final Activity activity) {
    if (preferences.getEmail().equals("")
        || preferences.getPassword().equals("")
        || preferences.getDid().equals("")) {
      Utils.showInfoDialog(
          activity,
          applicationContext.getString(R.string.notifications_callback_username_password_did));
      return;
    }

    final ProgressDialog progressDialog = new ProgressDialog(activity);
    progressDialog.setMessage(activity.getString(R.string.notifications_callback_progress));
    progressDialog.setCancelable(false);
    progressDialog.show();

    new AsyncTask<Boolean, Void, Boolean>() {
      @Override
      protected Boolean doInBackground(Boolean... params) {
        try {
          String url =
              "https://www.voip.ms/api/v1/rest.php?"
                  + "api_username="******"UTF-8")
                  + "&"
                  + "api_password="******"UTF-8")
                  + "&"
                  + "method=setSMS"
                  + "&"
                  + "did="
                  + URLEncoder.encode(preferences.getDid(), "UTF-8")
                  + "&"
                  + "enable=1"
                  + "&"
                  + "url_callback_enable=1"
                  + "&"
                  + "url_callback="
                  + URLEncoder.encode(
                      "http://voipmssms-kourlas.rhcloud.com/sms_callback?did={TO}", "UTF-8")
                  + "&"
                  + "url_callback_retry=0";

          JSONObject result = Utils.getJson(url);
          String status = result.optString("status");
          return !(status == null || !status.equals("success"));
        } catch (Exception ex) {
          return false;
        }
      }

      @Override
      protected void onPostExecute(Boolean success) {
        progressDialog.hide();

        DialogInterface.OnClickListener gcmOnClickListener =
            new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                gcm.registerForGcm(activity, true, true);
              }
            };

        if (!success) {
          Utils.showAlertDialog(
              activity,
              null,
              applicationContext.getString(R.string.notifications_callback_fail),
              applicationContext.getString(R.string.ok),
              gcmOnClickListener,
              null,
              null);
        } else {
          Utils.showAlertDialog(
              activity,
              null,
              applicationContext.getString(R.string.notifications_callback_success),
              applicationContext.getString(R.string.ok),
              gcmOnClickListener,
              null,
              null);
        }
      }
    }.execute();
  }