From 2d91c133a811f54b7ed32a29430036f1c1812006 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sun, 16 Dec 2018 10:41:04 -0800 Subject: [PATCH] new file (forgotten earlier) --- .../org/eehouse/android/xw4/Channels.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 xwords4/android/app/src/main/java/org/eehouse/android/xw4/Channels.java diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Channels.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Channels.java new file mode 100644 index 000000000..9fdf57cab --- /dev/null +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Channels.java @@ -0,0 +1,72 @@ +/* -*- compile-command: "find-and-gradle.sh insXw4Deb"; -*- */ +/* + * Copyright 2010 - 2014 by Eric House (xwords@eehouse.org). All + * rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package org.eehouse.android.xw4; + +import android.os.Build; +import android.content.Context; +import java.util.HashSet; +import java.util.Set; +import android.app.NotificationChannel; +import android.app.NotificationManager; + +public class Channels { + + enum ID { + FOREGROUND(R.string.foreground_channel_expl, + NotificationManager.IMPORTANCE_LOW), + GAME_EVENT(R.string.gameevent_channel_expl, + NotificationManager.IMPORTANCE_LOW); + + private int mExpl; + private int mImportance; + private ID( int expl, int imp ) + { + mExpl = expl; + mImportance = imp; + } + + public int getDesc() { return mExpl; } + private int getImportance() { return mImportance; } + } + + private static Set sChannelsMade = new HashSet<>(); + public static String getChannelID( Context context, ID id ) + { + final String name = id.toString(); + if ( ! sChannelsMade.contains( id ) ) { + sChannelsMade.add( id ); + if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ) { + NotificationManager notMgr = (NotificationManager) + context.getSystemService( Context.NOTIFICATION_SERVICE ); + + NotificationChannel channel = notMgr.getNotificationChannel( name ); + if ( channel == null ) { + String channelDescription = context.getString( id.getDesc() ); + channel = new NotificationChannel( name, channelDescription, + id.getImportance() ); + channel.enableVibration( true ); + notMgr.createNotificationChannel( channel ); + } + } + } + return name; + } +}