view app/src/main/java/com/five_ten_sg/connectbot/service/ConnectionNotifier.java @ 526:9a8862b87f6e stable-1.9.4-8

bump version
author Carl Byington <carl@five-ten-sg.com>
date Sat, 01 Jun 2024 10:27:22 -0600
parents cbdb219e9ff5
children
line wrap: on
line source

/*
 * ConnectBot: simple, powerful, open-source SSH client for Android
 * Copyright 2010 Kenny Root, Jeffrey Sharkey
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.five_ten_sg.connectbot.service;

import com.five_ten_sg.connectbot.ConsoleActivity;
import com.five_ten_sg.connectbot.R;
import com.five_ten_sg.connectbot.bean.HostBean;
import com.five_ten_sg.connectbot.util.HostDatabase;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.BitmapFactory;
import android.os.Build;

/**
 * @author Kenny Root
 *
 * Based on the concept from jasta's blog post.
 */
public class ConnectionNotifier {
    private static final int ONLINE_NOTIFICATION = 1;
    private static final int ACTIVITY_NOTIFICATION = 2;
    private NotificationChannel chan = null;

    protected NotificationManager getNotificationManager(Context context) {
        return (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    }

    protected Notification newActivityNotification(Context context, HostBean host) {
        int rgb;
        if (HostDatabase.COLOR_RED.equals(host.getColor()))
            rgb = Color.RED;
        else if (HostDatabase.COLOR_GREEN.equals(host.getColor()))
            rgb = Color.GREEN;
        else if (HostDatabase.COLOR_BLUE.equals(host.getColor()))
            rgb = Color.BLUE;
        else
            rgb = Color.WHITE;
        Resources res = context.getResources();
        Intent notificationIntent = new Intent(context, ConsoleActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        Notification.Builder nb;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelID   = "com.five_ten_sg.connectbot.service";
            String channelName = "background";
            if (chan == null) {
                chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
                chan.setLightColor(R.color.colorAccent);
                chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
                NotificationManager manager = getNotificationManager(context);
                assert manager != null;
                manager.createNotificationChannel(chan);
            }
            nb = new Notification.Builder(context, channelID);
        }
        else {
            nb = new Notification.Builder(context);
        }

        Notification notification = nb
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(res.getString(R.string.notification_text, host.getNickname()))
            .setContentIntent(contentIntent)
            .setWhen(System.currentTimeMillis())
            .setSmallIcon(R.drawable.notification_icon)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon))
            .setLights(rgb, 300, 1000)
            .build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        return notification;
    }

    protected Notification newRunningNotification(Context context) {
        Resources res = context.getResources();
        Intent notificationIntent = new Intent(context, ConsoleActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, ONLINE_NOTIFICATION, notificationIntent, PendingIntent.FLAG_IMMUTABLE);

        Notification.Builder nb;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelID   = "com.five_ten_sg.connectbot.service";
            String channelName = "background";
            if (chan == null) {
                chan = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_NONE);
                chan.setLightColor(R.color.colorAccent);
                chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
                NotificationManager manager = getNotificationManager(context);
                assert manager != null;
                manager.createNotificationChannel(chan);
            }
            nb = new Notification.Builder(context, channelID);
        }
        else {
            nb = new Notification.Builder(context);
        }

        Notification notification = nb
            .setContentTitle(res.getString(R.string.app_name))
            .setContentText(res.getString(R.string.app_is_running))
            .setContentIntent(contentIntent)
            .setWhen(0)
            .setOngoing(true)
            .setSmallIcon(R.drawable.notification_icon)
            .setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.notification_icon))
            .build();
        notification.flags |= Notification.FLAG_NO_CLEAR;
        return notification;

    }

    public void showActivityNotification(Service context, HostBean host) {
        getNotificationManager(context).notify(ACTIVITY_NOTIFICATION, newActivityNotification(context, host));
    }

    public void hideActivityNotification(Service context) {
        getNotificationManager(context).cancel(ACTIVITY_NOTIFICATION);
    }

    public void showRunningNotification(Service context) {
        context.startForeground(ONLINE_NOTIFICATION, newRunningNotification(context));
    }

    public void hideRunningNotification(Service context) {
        context.stopForeground(true);
    }
}