コード例 #1
0
ファイル: SinkholeService.java プロジェクト: an0n981/NetGuard
  private Notification getForegroundNotification(int allowed, int blocked) {
    Intent main = new Intent(this, ActivityMain.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_security_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_started))
            .setContentIntent(pi)
            .setCategory(Notification.CATEGORY_STATUS)
            .setVisibility(Notification.VISIBILITY_SECRET)
            .setPriority(Notification.PRIORITY_MIN)
            .setColor(tv.data)
            .setOngoing(true)
            .setAutoCancel(false);

    if (allowed > 0 || blocked > 0) {
      NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
      notification.bigText(getString(R.string.msg_started));
      notification.setSummaryText(getString(R.string.msg_packages, allowed, blocked));
      return notification.build();
    } else return builder.build();
  }
コード例 #2
0
  public static void showNotActivatedNotification(String packageName, String appName) {
    Intent intent = new Intent(sContext, WelcomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra("fragment", 1);

    PendingIntent pModulesTab =
        PendingIntent.getActivity(
            sContext, PENDING_INTENT_OPEN_MODULES, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String title = sContext.getString(R.string.module_is_not_activated_yet);
    NotificationCompat.Builder builder =
        new NotificationCompat.Builder(sContext)
            .setContentTitle(title)
            .setContentText(appName)
            .setTicker(title)
            .setContentIntent(pModulesTab)
            .setVibrate(new long[] {0})
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_notification)
            .setColor(sContext.getResources().getColor(R.color.colorPrimary));

    if (Build.VERSION.SDK_INT >= 21) builder.setPriority(2);

    Intent iActivateAndReboot = new Intent(sContext, RebootReceiver.class);
    iActivateAndReboot.putExtra(RebootReceiver.EXTRA_ACTIVATE_MODULE, packageName);
    PendingIntent pActivateAndReboot =
        PendingIntent.getBroadcast(
            sContext,
            PENDING_INTENT_ACTIVATE_MODULE_AND_REBOOT,
            iActivateAndReboot,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Intent iActivate = new Intent(sContext, RebootReceiver.class);
    iActivate.putExtra(RebootReceiver.EXTRA_ACTIVATE_MODULE, packageName);
    iActivate.putExtra(RebootReceiver.EXTRA_ACTIVATE_MODULE_AND_RETURN, true);
    PendingIntent pActivate =
        PendingIntent.getBroadcast(
            sContext, PENDING_INTENT_ACTIVATE_MODULE, iActivate, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle(title);
    notiStyle.bigText(sContext.getString(R.string.module_is_not_activated_yet_detailed, appName));
    builder.setStyle(notiStyle);

    // Only show the quick activation button if any module has been
    // enabled before,
    // to ensure that the user know the way to disable the module later.
    if (!ModuleUtil.getInstance().getEnabledModules().isEmpty()) {
      builder.addAction(
          R.drawable.ic_menu_refresh,
          sContext.getString(R.string.activate_and_reboot),
          pActivateAndReboot);
      builder.addAction(R.drawable.ic_save, sContext.getString(R.string.activate_only), pActivate);
    }

    sNotificationManager.notify(
        packageName, NOTIFICATION_MODULE_NOT_ACTIVATED_YET, builder.build());
  }
コード例 #3
0
  /**
   * Big Text Style Notification
   *
   * @return Notification
   * @see CreateNotification
   */
  private Notification setBigTextStyleNotification() {
    Bitmap remote_picture = null;

    // Create the style object with BigTextStyle subclass.
    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle("Big Text Expanded");
    notiStyle.setSummaryText("Nice big text.");

    try {
      remote_picture = BitmapFactory.decodeStream((InputStream) new URL(sample_url).getContent());
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Add the big text to the style.
    CharSequence bigText =
        "This is an example of a large string to demo how much "
            + "text you can show in a 'Big Text Style' notification.";
    notiStyle.bigText(bigText);

    // Creates an explicit intent for an ResultActivity to receive.
    Intent resultIntent = new Intent(this, ResultActivity.class);

    // This ensures that the back button follows the recommended convention for the back key.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

    // Adds the back stack for the Intent (but not the Intent itself).
    stackBuilder.addParentStack(ResultActivity.class);

    // Adds the Intent that starts the Activity to the top of the stack.
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    return new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setAutoCancel(true)
        .setLargeIcon(remote_picture)
        .setContentIntent(resultPendingIntent)
        .addAction(R.drawable.ic_launcher, "One", resultPendingIntent)
        .addAction(R.drawable.ic_launcher, "Two", resultPendingIntent)
        .addAction(R.drawable.ic_launcher, "Three", resultPendingIntent)
        .setContentTitle("Big Text Normal")
        .setContentText("This is an example of a Big Text Style.")
        .setStyle(notiStyle)
        .build();
  }
コード例 #4
0
 private void initBigTextStyle(String title, String content) {
   bigTextStyle = new NotificationCompat.BigTextStyle();
   bigTextStyle.setBigContentTitle(title);
   bigTextStyle.bigText(content);
 }
コード例 #5
0
  private void setNotificationMessage(
      int notId, Bundle extras, NotificationCompat.Builder mBuilder) {
    String message = extras.getString(MESSAGE);

    String style = extras.getString(STYLE, STYLE_TEXT);
    if (STYLE_INBOX.equals(style)) {
      setNotification(notId, message);

      mBuilder.setContentText(fromHtml(message));

      ArrayList<String> messageList = messageMap.get(notId);
      Integer sizeList = messageList.size();
      if (sizeList > 1) {
        String sizeListMessage = sizeList.toString();
        String stacking = sizeList + " more";
        if (extras.getString(SUMMARY_TEXT) != null) {
          stacking = extras.getString(SUMMARY_TEXT);
          stacking = stacking.replace("%n%", sizeListMessage);
        }
        NotificationCompat.InboxStyle notificationInbox =
            new NotificationCompat.InboxStyle()
                .setBigContentTitle(fromHtml(extras.getString(TITLE)))
                .setSummaryText(fromHtml(stacking));

        for (int i = messageList.size() - 1; i >= 0; i--) {
          notificationInbox.addLine(fromHtml(messageList.get(i)));
        }

        mBuilder.setStyle(notificationInbox);
      } else {
        NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
        if (message != null) {
          bigText.bigText(fromHtml(message));
          bigText.setBigContentTitle(fromHtml(extras.getString(TITLE)));
          mBuilder.setStyle(bigText);
        }
      }
    } else if (STYLE_PICTURE.equals(style)) {
      setNotification(notId, "");

      NotificationCompat.BigPictureStyle bigPicture = new NotificationCompat.BigPictureStyle();
      bigPicture.bigPicture(getBitmapFromURL(extras.getString(PICTURE)));
      bigPicture.setBigContentTitle(fromHtml(extras.getString(TITLE)));
      bigPicture.setSummaryText(fromHtml(extras.getString(SUMMARY_TEXT)));

      mBuilder.setContentTitle(fromHtml(extras.getString(TITLE)));
      mBuilder.setContentText(fromHtml(message));

      mBuilder.setStyle(bigPicture);
    } else {
      setNotification(notId, "");

      NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();

      if (message != null) {
        mBuilder.setContentText(fromHtml(message));

        bigText.bigText(fromHtml(message));
        bigText.setBigContentTitle(fromHtml(extras.getString(TITLE)));

        String summaryText = extras.getString(SUMMARY_TEXT);
        if (summaryText != null) {
          bigText.setSummaryText(fromHtml(summaryText));
        }

        mBuilder.setStyle(bigText);
      }
      /*
      else {
          mBuilder.setContentText("<missing message content>");
      }
      */
    }
  }
コード例 #6
0
  private void showNotification(
      Context context,
      int notificationId,
      int smallIconId,
      String title,
      String contentText,
      String bigTitle,
      String bigContentText,
      String summaryText,
      String ticker,
      Intent intent) {
    NotificationManager notificationManager =
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setSound(soundUri);

    if (smallIconId == 0) {
      builder.setSmallIcon(R.mipmap.ic_launcher);
    } else {
      builder.setSmallIcon(smallIconId);
    }
    builder.setWhen(System.currentTimeMillis());
    // builder.setNumber(10);

    if (!StringUtils.isEmptyString(ticker)) {
      builder.setTicker(ticker);
    }

    if (StringUtils.isEmptyString(title)) {
      builder.setContentTitle(PackageUtils.getApplicationName(context));
    } else {
      builder.setContentTitle(title);
    }
    builder.setContentText(contentText);
    builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
    builder.setAutoCancel(true);

    // big title and text
    if (!StringUtils.isEmptyString(bigTitle) && !StringUtils.isEmptyString(bigContentText)) {
      NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle(builder);
      if (!StringUtils.isEmptyString(summaryText)) {
        style.setSummaryText(summaryText);
      }
      style.setBigContentTitle(bigTitle);
      style.bigText(bigContentText);

      builder.setStyle(style);
    }

    if (intent != null) {
      intent.setFlags(
          intent.FLAG_ACTIVITY_CLEAR_TOP
              | Intent.FLAG_ACTIVITY_SINGLE_TOP
              | Intent.FLAG_ACTIVITY_CLEAR_TASK
              | Intent.FLAG_ACTIVITY_NEW_TASK);
      PendingIntent pendingIntent =
          PendingIntent.getActivity(
              context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      builder.setContentIntent(pendingIntent);
    }

    notificationManager.notify(notificationId, builder.build());
  }
コード例 #7
0
  public void postNotification(int id) {

    if (notificationItems.size() == 0) {
      return;
    }

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
    mBuilder.setContentTitle(notificationTitle);
    mBuilder.setSmallIcon(R.drawable.ic_stat_icon);
    mBuilder.setLargeIcon(
        BitmapFactory.decodeResource(
            context.getResources(), R.drawable.ic_action_notification_dark));

    if (notificationItems.size() > 1) {
      // inbox style
      NotificationCompat.InboxStyle inbox = new NotificationCompat.InboxStyle();

      try {
        inbox.setBigContentTitle(notificationTitle);
      } catch (Exception e) {

      }

      if (notificationItems.size() <= 5) {
        for (String s : notificationItems) {
          inbox.addLine(Html.fromHtml(s));
        }
      } else {
        for (int i = 0; i < 5; i++) {
          inbox.addLine(Html.fromHtml(notificationItems.get(i)));
        }

        int extra = notificationItems.size() - 5;
        if (extra > 1) {
          inbox.setSummaryText("+" + extra + " " + context.getString(R.string.items));
        } else {
          inbox.setSummaryText("+" + extra + " " + context.getString(R.string.item));
        }
      }

      mBuilder.setStyle(inbox);
      mBuilder.setContentText(notificationItems.size() + " " + context.getString(R.string.items));
    } else {
      // big text style
      NotificationCompat.BigTextStyle bigText = new NotificationCompat.BigTextStyle();
      bigText.bigText(Html.fromHtml(notificationItems.get(0)));
      bigText.setBigContentTitle(notificationTitle);

      mBuilder.setStyle(bigText);
      mBuilder.setContentText(Html.fromHtml(notificationItems.get(0)));
    }

    if (!useSecondAccount) {
      mBuilder.setContentIntent(
          PendingIntent.getActivity(context, 0, new Intent(context, RedirectToActivity.class), 0));
    }

    if (settings.vibrate) {
      mBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
    }

    if (settings.sound) {
      try {
        mBuilder.setSound(Uri.parse(settings.ringtone));
      } catch (Exception e) {
        mBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
      }
    }

    if (settings.led) {
      mBuilder.setLights(0xFFFFFF, 1000, 1000);
    }

    if (settings.wakeScreen) {
      PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
      final PowerManager.WakeLock wakeLock =
          pm.newWakeLock(
              (PowerManager.SCREEN_BRIGHT_WAKE_LOCK
                  | PowerManager.FULL_WAKE_LOCK
                  | PowerManager.ACQUIRE_CAUSES_WAKEUP),
              "TAG");
      wakeLock.acquire(5000);
    }

    // Pebble notification
    if (sharedPrefs.getBoolean("pebble_notification", false)) {
      NotificationUtils.sendAlertToPebble(context, notificationTitle, notificationItems.get(0));
    }

    // Light Flow notification
    NotificationUtils.sendToLightFlow(context, notificationTitle, notificationItems.get(0));

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(id, mBuilder.build());
  }