Java Code Examples for androidx.core.app.NotificationCompat#BigTextStyle

The following examples show how to use androidx.core.app.NotificationCompat#BigTextStyle . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 6 votes vote down vote up
private void showAutoStartNotification() {
    Intent main = new Intent(this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_APPROVE, true);
    PendingIntent pi = PendingIntent.getActivity(this, NOTIFY_AUTOSTART, main, PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
    builder.setSmallIcon(R.drawable.ic_error_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_autostart))
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(false)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET);

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_autostart));

    NotificationManagerCompat.from(this).notify(NOTIFY_AUTOSTART, notification.build());
}
 
Example 2
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 6 votes vote down vote up
private void showDisabledNotification() {
    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.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
    builder.setSmallIcon(R.drawable.ic_error_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_revoked))
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(false)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET);

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_revoked));

    NotificationManagerCompat.from(this).notify(NOTIFY_DISABLED, notification.build());
}
 
Example 3
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 6 votes vote down vote up
private void showAutoStartNotification() {
    Intent main = new Intent(this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_APPROVE, true);
    PendingIntent pi = PendingIntent.getActivity(this, NOTIFY_AUTOSTART, main, PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
    builder.setSmallIcon(R.drawable.ic_error_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_autostart))
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(false)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET);

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_autostart));

    NotificationManagerCompat.from(this).notify(NOTIFY_AUTOSTART, notification.build());
}
 
Example 4
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 6 votes vote down vote up
private void showErrorNotification(String message) {
    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.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
    builder.setSmallIcon(R.drawable.ic_error_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_error, message))
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(false)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET);

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_error, message));
    notification.setSummaryText(message);

    NotificationManagerCompat.from(this).notify(NOTIFY_ERROR, notification.build());
}
 
Example 5
Source File: ShadowsocksNotification.java    From Maying with Apache License 2.0 6 votes vote down vote up
public ShadowsocksNotification(Service service, String profileName, boolean visible) {
    this.service = service;
    this.profileName = profileName;
    this.visible = visible;

    keyGuard = (KeyguardManager) service.getSystemService(Context.KEYGUARD_SERVICE);
    nm = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
    pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE);

    // init notification builder
    initNotificationBuilder();
    style = new NotificationCompat.BigTextStyle(builder);

    // init with update action
    initWithUpdateAction();

    // register lock receiver
    registerLockReceiver(service, visible);
}
 
Example 6
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 6 votes vote down vote up
private void showErrorNotification(String message) {
    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.colorOff, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
    builder.setSmallIcon(R.drawable.ic_error_white_24dp)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(getString(R.string.msg_error, message))
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(false)
            .setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET);

    NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
    notification.bigText(getString(R.string.msg_error, message));
    notification.setSummaryText(message);

    NotificationManagerCompat.from(this).notify(NOTIFY_ERROR, notification.build());
}
 
Example 7
Source File: NotificationService.java    From android-permissions with MIT License 6 votes vote down vote up
public Notification buildNotification(String title, String message, Intent intent, PendingIntent deleteIntent) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, message.hashCode(), intent, FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setSmallIcon(android.R.mipmap.sym_def_app_icon)
            .setContentIntent(pendingIntent);

    notificationBuilder.setDeleteIntent(deleteIntent);

    NotificationCompat.BigTextStyle bigTextNotification = new NotificationCompat.BigTextStyle(notificationBuilder)
            .bigText(message)
            .setBigContentTitle(title);

    return bigTextNotification.build();
}
 
Example 8
Source File: NotificationUtil.java    From EdXposedManager with GNU General Public License v3.0 6 votes vote down vote up
static void showModuleInstallingNotification(String appName) {
    String title = sContext.getString(R.string.install_load);
    String message = sContext.getString(R.string.install_load_apk, appName);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(sContext, MeowCatApplication.TAG).setContentTitle(title).setContentText(message)
            .setVibrate(new long[]{0}).setProgress(0, 0, true)
            .setSmallIcon(R.drawable.ic_notification).setOngoing(true);

    if (prefs.getBoolean(COLORED_NOTIFICATION, false))
        builder.setColor(XposedApp.getColor(sContext));

    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle(title);
    notiStyle.bigText(message);
    builder.setStyle(notiStyle).setChannelId(NOTIFICATION_MODULES_CHANNEL);

    sNotificationManager.notify(null, NOTIFICATION_MODULE_INSTALLING, builder.build());
}
 
Example 9
Source File: ShadowsocksNotification.java    From ShadowsocksRR with Apache License 2.0 6 votes vote down vote up
public ShadowsocksNotification(Service service, String profileName, boolean visible) {
    this.service = service;
    this.profileName = profileName;
    this.visible = visible;

    keyGuard = (KeyguardManager) service.getSystemService(Context.KEYGUARD_SERVICE);
    nm = (NotificationManager) service.getSystemService(Context.NOTIFICATION_SERVICE);
    pm = (PowerManager) service.getSystemService(Context.POWER_SERVICE);

    // init notification builder
    initNotificationBuilder();
    style = new NotificationCompat.BigTextStyle(builder);

    // init with update action
    initWithUpdateAction();

    // register lock receiver
    registerLockReceiver(service, visible);
}
 
Example 10
Source File: NotificationUtil.java    From EdXposedManager with GNU General Public License v3.0 5 votes vote down vote up
public static void showInstallerUpdateNotification() {
    Intent intent = new Intent(sContext, WelcomeActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.putExtra(FRAGMENT_ID, 0);

    PendingIntent pInstallTab = PendingIntent.getActivity(sContext, PENDING_INTENT_OPEN_INSTALL,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String title = sContext.getString(R.string.app_name);
    String message = sContext.getString(R.string.newVersion);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(sContext, MeowCatApplication.TAG).setContentTitle(title).setContentText(message)
            .setTicker(title).setContentIntent(pInstallTab)
            .setVibrate(new long[]{0}).setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_notification);

    if (prefs.getBoolean(HEADS_UP, true))
        builder.setPriority(2);

    if (prefs.getBoolean(COLORED_NOTIFICATION, false))
        builder.setColor(XposedApp.getColor(sContext));

    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle(title);
    notiStyle.bigText(message);
    builder.setStyle(notiStyle).setChannelId(NOTIFICATION_UPDATE_CHANNEL);

    sNotificationManager.notify(null, NOTIFICATION_INSTALLER_UPDATE, builder.build());
}
 
Example 11
Source File: NotificationManager.java    From QuickDevFramework with Apache License 2.0 5 votes vote down vote up
/**
 * set big content text for notification
 * @param builder builder
 * @param title the notification title when expanded
 * @param contentText notification content text when expanded
 * @param summaryText can be seen as a subtitle, not very useful
 * */
public static NotificationCompat.Builder setBigText(NotificationCompat.Builder builder,
                                                    String title,
                                                    String summaryText,
                                                    String contentText) {
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(title);
    bigTextStyle.bigText(contentText);
    if (!TextUtils.isEmpty(summaryText)) {
        bigTextStyle.setSummaryText(summaryText);
    }
    builder.setStyle(bigTextStyle);
    return builder;
}
 
Example 12
Source File: SmsSendingService.java    From dhis2-android-capture-app with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Notification makeNotification(SendingStatus currentStatus) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, SMS_NOTIFICATION_CHANNEL_ID);
    String text = StatusText.getTextForStatus(getResources(), currentStatus);
    String title = StatusText.getTextSubmissionType(getResources(), inputArguments);
    builder.setContentText(text);
    builder.setContentTitle(title);
    NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
    bigTextStyle.setBigContentTitle(title);
    bigTextStyle.bigText(text);
    builder.setStyle(bigTextStyle);
    builder.setWhen(System.currentTimeMillis());
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setPriority(Notification.PRIORITY_MAX);
    return builder.build();
}
 
Example 13
Source File: NotificationUtil.java    From EdXposedManager with GNU General Public License v3.0 5 votes vote down vote up
private static void showModuleInstallNotification(String title, String message, String path, boolean error) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            sContext, MeowCatApplication.TAG).setContentTitle(title).setContentText(message)
            .setVibrate(new long[]{0}).setAutoCancel(true)
            .setSmallIcon(R.drawable.ic_notification);

    if (error) {
        Intent iInstallApk = new Intent(sContext, ApkReceiver.class);
        iInstallApk.putExtra(ApkReceiver.EXTRA_APK_PATH, path);
        PendingIntent pInstallApk = PendingIntent.getBroadcast(sContext, PENDING_INTENT_INSTALL_APK, iInstallApk, PendingIntent.FLAG_UPDATE_CURRENT);

        builder.addAction(new NotificationCompat.Action.Builder(0, sContext.getString(R.string.installation_apk_normal), pInstallApk).build());
    }

    if (prefs.getBoolean(HEADS_UP, true))
        builder.setPriority(2);

    if (prefs.getBoolean(COLORED_NOTIFICATION, false))
        builder.setColor(XposedApp.getColor(sContext));

    NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
    notiStyle.setBigContentTitle(title);
    notiStyle.bigText(message);
    builder.setStyle(notiStyle).setChannelId(NOTIFICATION_MODULES_CHANNEL);

    sNotificationManager.notify(null, NOTIFICATION_MODULE_INSTALLATION, builder.build());

    new android.os.Handler().postDelayed(() -> cancel(NOTIFICATION_MODULE_INSTALLATION), 10 * 1000);
}
 
Example 14
Source File: MainActivity.java    From wearable with Apache License 2.0 5 votes vote down vote up
/**
 * using the bigtext notification.
 */
void bigTextNoti() {
    //create the intent to launch the notiactivity, then the pentingintent.
    Intent viewIntent = new Intent(this, NotiActivity.class);
    viewIntent.putExtra("NotiID", "Notification ID is " + notificationID);

    PendingIntent viewPendingIntent =
        PendingIntent.getActivity(this, 0, viewIntent, 0);

    BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
    bigStyle.bigText("Big text style.\n"
        + "We should have more room to add text for the user to read, instead of a short message.");


    //Now create the notification.  We must use the NotificationCompat or it will not work on the wearable.
    NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this, id)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Simple Noti")
            .setContentText("This is a simple notification")
            .setContentIntent(viewPendingIntent)
            .setChannelId(id)
            .setStyle(bigStyle);

    // Get an instance of the NotificationManager service
    NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);

    // Build the notification and issues it with notification manager.
    notificationManager.notify(notificationID, notificationBuilder.build());
    notificationID++;
}
 
Example 15
Source File: NotificationUtil.java    From Hify with MIT License 4 votes vote down vote up
private void showSmallNotification(String timeStamp, String user_image, int icon, String title, String message, PendingIntent resultPendingIntent, String notification_type) {

        int id;
        NotificationCompat.Builder mBuilder;
        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(mContext);

        switch (notification_type){

            case "like":
                id=100;
                mBuilder = new NotificationCompat.Builder(mContext, "like_channel");
                break;
            case "comment":
                id=200;
                mBuilder = new NotificationCompat.Builder(mContext, "comments_channel");
                break;
            case "forum":
                id=(int)System.currentTimeMillis();
                mBuilder = new NotificationCompat.Builder(mContext, "forum_channel");
                break;
            case "Message":
                id=(int)System.currentTimeMillis();
                mBuilder = new NotificationCompat.Builder(mContext, "flash_message");
                break;
            default:
                id=(int)System.currentTimeMillis();
                mBuilder = new NotificationCompat.Builder(mContext, "hify_other_channel");
        }

        NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
        bigTextStyle.setBigContentTitle(title);
        bigTextStyle.bigText(message);

        Notification notification;

        notification = mBuilder
                .setAutoCancel(true)
                .setContentTitle(title)
                .setTicker(title)
                .setContentIntent(resultPendingIntent)
                .setColorized(true)
                .setWhen(getTimeMilliSec(timeStamp))
                .setShowWhen(true)
                .setSound(Uri.parse("android.resource://"+mContext.getPackageName()+"/"+R.raw.hify_sound))
                .setColor(Color.parseColor("#2591FC"))
                .setStyle(bigTextStyle)
                .setLargeIcon(getCircularBitmap(getBitmapFromURL(user_image)))
                .setSmallIcon(icon)
                .setContentText(message)
                .build();


        notificationManagerCompat.notify(id, notification);

    }
 
Example 16
Source File: ReactNativeUtil.java    From react-native-azurenotificationhub with MIT License 4 votes vote down vote up
public static NotificationCompat.BigTextStyle getBigTextStyle(String bigText) {
    return new NotificationCompat.BigTextStyle().bigText(bigText);
}
 
Example 17
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
public void notifyNewApplication(int uid) {
    if (uid < 0)
        return;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        // Get application name
        String name = TextUtils.join(", ", Util.getApplicationNames(uid, this));

        // Get application info
        PackageManager pm = getPackageManager();
        String[] packages = pm.getPackagesForUid(uid);
        if (packages == null || packages.length < 1)
            throw new PackageManager.NameNotFoundException(Integer.toString(uid));
        boolean internet = Util.hasInternet(uid, this);

        // Build notification
        Intent main = new Intent(this, ActivityMain.class);
        main.putExtra(ActivityMain.EXTRA_REFRESH, true);
        main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
        PendingIntent pi = PendingIntent.getActivity(this, uid, main, PendingIntent.FLAG_UPDATE_CURRENT);

        TypedValue tv = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
        builder.setSmallIcon(R.drawable.ic_security_white_24dp)
                .setContentIntent(pi)
                .setColor(tv.data)
                .setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            builder.setContentTitle(name)
                    .setContentText(getString(R.string.msg_installed_n));
        else
            builder.setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.msg_installed, name));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                    .setVisibility(NotificationCompat.VISIBILITY_SECRET);

        // Get defaults
        SharedPreferences prefs_wifi = getSharedPreferences("wifi", Context.MODE_PRIVATE);
        SharedPreferences prefs_other = getSharedPreferences("other", Context.MODE_PRIVATE);
        boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true));
        boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true));

        // Build Wi-Fi action
        Intent riWifi = new Intent(this, ServiceSinkhole.class);
        riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi");
        riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi);

        PendingIntent piWifi = PendingIntent.getService(this, uid, riWifi, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action wAction = new NotificationCompat.Action.Builder(
                wifi ? R.drawable.wifi_on : R.drawable.wifi_off,
                getString(wifi ? R.string.title_allow_wifi : R.string.title_block_wifi),
                piWifi
        ).build();
        builder.addAction(wAction);

        // Build mobile action
        Intent riOther = new Intent(this, ServiceSinkhole.class);
        riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other");
        riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other);
        PendingIntent piOther = PendingIntent.getService(this, uid + 10000, riOther, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action oAction = new NotificationCompat.Action.Builder(
                other ? R.drawable.other_on : R.drawable.other_off,
                getString(other ? R.string.title_allow_other : R.string.title_block_other),
                piOther
        ).build();
        builder.addAction(oAction);

        // Show notification
        if (internet)
            NotificationManagerCompat.from(this).notify(uid, builder.build());
        else {
            NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                expanded.bigText(getString(R.string.msg_installed_n));
            else
                expanded.bigText(getString(R.string.msg_installed, name));
            expanded.setSummaryText(getString(R.string.title_internet));
            NotificationManagerCompat.from(this).notify(uid, expanded.build());
        }

    } catch (PackageManager.NameNotFoundException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
}
 
Example 18
Source File: ServiceSinkhole.java    From NetGuard with GNU General Public License v3.0 4 votes vote down vote up
private Notification getEnforcingNotification(int allowed, int blocked, int hosts) {
    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, "foreground");
    builder.setSmallIcon(isLockedDown(last_metered) ? R.drawable.ic_lock_outline_white_24dp : R.drawable.ic_security_white_24dp)
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(true)
            .setAutoCancel(false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        builder.setContentTitle(getString(R.string.msg_started));
    else
        builder.setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.msg_started));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET)
                .setPriority(NotificationCompat.PRIORITY_MIN);

    if (allowed >= 0)
        last_allowed = allowed;
    else
        allowed = last_allowed;
    if (blocked >= 0)
        last_blocked = blocked;
    else
        blocked = last_blocked;
    if (hosts >= 0)
        last_hosts = hosts;
    else
        hosts = last_hosts;

    if (allowed >= 0 || blocked >= 0 || hosts >= 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            if (Util.isPlayStoreInstall(this))
                builder.setContentText(getString(R.string.msg_packages, allowed, blocked));
            else
                builder.setContentText(getString(R.string.msg_hosts, allowed, blocked, hosts));
            return builder.build();
        } else {
            NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
            notification.bigText(getString(R.string.msg_started));
            if (Util.isPlayStoreInstall(this))
                notification.setSummaryText(getString(R.string.msg_packages, allowed, blocked));
            else
                notification.setSummaryText(getString(R.string.msg_hosts, allowed, blocked, hosts));
            return notification.build();
        }
    } else
        return builder.build();
}
 
Example 19
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
private Notification getEnforcingNotification(int allowed, int blocked, int hosts) {
    Intent main = new Intent(this, ActivityMain.class);
    PendingIntent pi = PendingIntent.getActivity(this, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);

    Intent i = new Intent(INTENT_PAUSE);
    i.setPackage(this.getPackageName());
    PendingIntent pauseIntent = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSinkhole.this);
    int pause = Integer.parseInt(prefs.getString("pause", "10"));

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "foreground");
    builder.setSmallIcon(isLockedDown(last_metered) ? R.drawable.ic_lock_outline_white_24dp : R.drawable.ic_rocket_white)
            .setContentIntent(pi)
            .setColor(tv.data)
            .setOngoing(true)
            .setAutoCancel(false)
            .addAction(R.drawable.ic_pause_white_24dp, getResources().getQuantityString(
                    R.plurals.pause, pause, pause), pauseIntent);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        builder.setContentTitle(getString(R.string.msg_started));
    else
        builder.setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.msg_started));

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                .setVisibility(NotificationCompat.VISIBILITY_SECRET)
                .setPriority(NotificationCompat.PRIORITY_MIN);

    if (allowed >= 0)
        last_allowed = allowed;
    else
        allowed = last_allowed;
    if (blocked >= 0)
        last_blocked = blocked;
    else
        blocked = last_blocked;
    if (hosts >= 0)
        last_hosts = hosts;
    else
        hosts = last_hosts;

    if (allowed >= 0 || blocked >= 0 || hosts >= 0) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            /*if (Util.isPlayStoreInstall(this))
                builder.setContentText(getString(R.string.msg_packages, allowed, blocked));
            else
                builder.setContentText(getString(R.string.msg_hosts, allowed, blocked, hosts));*/
            return builder.build();
        } else {
            NotificationCompat.BigTextStyle notification = new NotificationCompat.BigTextStyle(builder);
            notification.bigText(getString(R.string.msg_started));
            /*if (Util.isPlayStoreInstall(this))
                notification.setSummaryText(getString(R.string.msg_packages, allowed, blocked));
            else
                notification.setSummaryText(getString(R.string.msg_hosts, allowed, blocked, hosts));*/
            return notification.build();
        }
    } else
        return builder.build();
}
 
Example 20
Source File: ServiceSinkhole.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
public void notifyNewApplication(int uid) {
    if (uid < 0)
        return;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    try {
        // Get application name
        String name = TextUtils.join(", ", Util.getApplicationNames(uid, this));

        // Get application info
        PackageManager pm = getPackageManager();
        String[] packages = pm.getPackagesForUid(uid);
        if (packages == null || packages.length < 1)
            throw new PackageManager.NameNotFoundException(Integer.toString(uid));
        boolean internet = Util.hasInternet(uid, this);

        // Build notification
        Intent main = new Intent(this, ActivityMain.class);
        main.putExtra(ActivityMain.EXTRA_REFRESH, true);
        main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
        PendingIntent pi = PendingIntent.getActivity(this, uid, main, PendingIntent.FLAG_UPDATE_CURRENT);

        TypedValue tv = new TypedValue();
        getTheme().resolveAttribute(R.attr.colorPrimary, tv, true);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notify");
        builder.setSmallIcon(R.drawable.ic_rocket_white)
                .setContentIntent(pi)
                .setColor(tv.data)
                .setAutoCancel(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            builder.setContentTitle(name)
                    .setContentText(getString(R.string.msg_installed_n));
        else
            builder.setContentTitle(getString(R.string.app_name))
                    .setContentText(getString(R.string.msg_installed, name));

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            builder.setCategory(NotificationCompat.CATEGORY_STATUS)
                    .setVisibility(NotificationCompat.VISIBILITY_SECRET);

        // Get defaults
        SharedPreferences prefs_wifi = getSharedPreferences("wifi", Context.MODE_PRIVATE);
        SharedPreferences prefs_other = getSharedPreferences("other", Context.MODE_PRIVATE);
        boolean wifi = prefs_wifi.getBoolean(packages[0], prefs.getBoolean("whitelist_wifi", true));
        boolean other = prefs_other.getBoolean(packages[0], prefs.getBoolean("whitelist_other", true));

        // Build Wi-Fi action
        Intent riWifi = new Intent(this, ServiceSinkhole.class);
        riWifi.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riWifi.putExtra(ServiceSinkhole.EXTRA_NETWORK, "wifi");
        riWifi.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riWifi.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riWifi.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !wifi);

        PendingIntent piWifi = PendingIntent.getService(this, uid, riWifi, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action wAction = new NotificationCompat.Action.Builder(
                wifi ? R.drawable.wifi_on : R.drawable.wifi_off,
                getString(wifi ? R.string.title_allow_wifi : R.string.title_block_wifi),
                piWifi
        ).build();
        builder.addAction(wAction);

        // Build mobile action
        Intent riOther = new Intent(this, ServiceSinkhole.class);
        riOther.putExtra(ServiceSinkhole.EXTRA_COMMAND, ServiceSinkhole.Command.set);
        riOther.putExtra(ServiceSinkhole.EXTRA_NETWORK, "other");
        riOther.putExtra(ServiceSinkhole.EXTRA_UID, uid);
        riOther.putExtra(ServiceSinkhole.EXTRA_PACKAGE, packages[0]);
        riOther.putExtra(ServiceSinkhole.EXTRA_BLOCKED, !other);
        PendingIntent piOther = PendingIntent.getService(this, uid + 10000, riOther, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Action oAction = new NotificationCompat.Action.Builder(
                other ? R.drawable.other_on : R.drawable.other_off,
                getString(other ? R.string.title_allow_other : R.string.title_block_other),
                piOther
        ).build();
        builder.addAction(oAction);

        // Show notification
        if (internet)
            NotificationManagerCompat.from(this).notify(uid, builder.build());
        else {
            NotificationCompat.BigTextStyle expanded = new NotificationCompat.BigTextStyle(builder);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
                expanded.bigText(getString(R.string.msg_installed_n));
            else
                expanded.bigText(getString(R.string.msg_installed, name));
            expanded.setSummaryText(getString(R.string.title_internet));
            NotificationManagerCompat.from(this).notify(uid, expanded.build());
        }

    } catch (PackageManager.NameNotFoundException ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
}