Home | History | Annotate | Download | only in notificationchannels
      1 /*
      2 * Copyright 2017 The Android Open Source Project
      3 *
      4 * Licensed under the Apache License, Version 2.0 (the "License");
      5 * you may not use this file except in compliance with the License.
      6 * You may obtain a copy of the License at
      7 *
      8 *     http://www.apache.org/licenses/LICENSE-2.0
      9 *
     10 * Unless required by applicable law or agreed to in writing, software
     11 * distributed under the License is distributed on an "AS IS" BASIS,
     12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 * See the License for the specific language governing permissions and
     14 * limitations under the License.
     15 */
     16 
     17 package com.example.android.notificationchannels
     18 
     19 import android.app.Activity
     20 import android.content.Intent
     21 import android.os.Bundle
     22 import android.provider.Settings
     23 import android.util.Log
     24 import android.view.View
     25 import android.widget.Button
     26 import kotlinx.android.synthetic.main.activity_main.*
     27 
     28 /**
     29  * Display main screen for sample. Displays controls for sending test notifications.
     30  */
     31 class MainActivity : Activity() {
     32 
     33     private lateinit var ui: MainUi
     34 
     35     /*
     36      * Class for managing notifications
     37      */
     38     private lateinit var helper: NotificationHelper
     39 
     40     override fun onCreate(savedInstanceState: Bundle?) {
     41         super.onCreate(savedInstanceState)
     42         setContentView(R.layout.activity_main)
     43         ui = MainUi(activity_main)
     44         helper = NotificationHelper(this)
     45     }
     46 
     47     /**
     48      * Send activity notifications.
     49 
     50      * @param id The ID of the notification to create
     51      * *
     52      * @param title The title of the notification
     53      */
     54     fun sendNotification(id: Int, title: String) {
     55         when (id) {
     56             NOTI_PRIMARY1 -> helper.notify(
     57                     id, helper.getNotification1(title, getString(R.string.primary1_body)))
     58             NOTI_PRIMARY2 -> helper.notify(
     59                     id, helper.getNotification1(title, getString(R.string.primary2_body)))
     60             NOTI_SECONDARY1 -> helper.notify(
     61                     id, helper.getNotification2(title, getString(R.string.secondary1_body)))
     62             NOTI_SECONDARY2 -> helper.notify(
     63                     id, helper.getNotification2(title, getString(R.string.secondary2_body)))
     64         }
     65     }
     66 
     67     /**
     68      * Send Intent to load system Notification Settings for this app.
     69      */
     70     fun goToNotificationSettings() {
     71         val i = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS)
     72         i.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
     73         startActivity(i)
     74     }
     75 
     76     /**
     77      * Send intent to load system Notification Settings UI for a particular channel.
     78 
     79      * @param channel Name of channel to configure
     80      */
     81     fun goToNotificationSettings(channel: String) {
     82         val i = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
     83         i.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
     84         i.putExtra(Settings.EXTRA_CHANNEL_ID, channel)
     85         startActivity(i)
     86     }
     87 
     88     /**
     89      * View model for interacting with Activity UI elements. (Keeps core logic for sample
     90      * seperate.)
     91      */
     92     internal inner class MainUi (root: View) : View.OnClickListener {
     93 
     94         init {
     95             main_primary_send1.setOnClickListener(this)
     96             main_primary_send2.setOnClickListener(this)
     97             main_primary_config.setOnClickListener(this)
     98 
     99             main_secondary_send1.setOnClickListener(this)
    100             main_secondary_send2.setOnClickListener(this)
    101             main_secondary_config.setOnClickListener(this)
    102 
    103             (root.findViewById<View>(R.id.btnA) as Button).setOnClickListener(this)
    104         }
    105 
    106         private val titlePrimaryText: String
    107             get() {
    108                 if (main_primary_title != null) {
    109                     return main_primary_title.text.toString()
    110                 }
    111                 return ""
    112             }
    113 
    114         private val titleSecondaryText: String
    115             get() {
    116                 if (main_primary_title != null) {
    117                     return main_secondary_title.text.toString()
    118                 }
    119                 return ""
    120             }
    121 
    122         override fun onClick(view: View) {
    123             when (view.id) {
    124                 R.id.main_primary_send1 -> sendNotification(NOTI_PRIMARY1, titlePrimaryText)
    125                 R.id.main_primary_send2 -> sendNotification(NOTI_PRIMARY2, titlePrimaryText)
    126                 R.id.main_primary_config -> goToNotificationSettings(NotificationHelper.PRIMARY_CHANNEL)
    127 
    128                 R.id.main_secondary_send1 -> sendNotification(NOTI_SECONDARY1, titleSecondaryText)
    129                 R.id.main_secondary_send2 -> sendNotification(NOTI_SECONDARY2, titleSecondaryText)
    130                 R.id.main_secondary_config -> goToNotificationSettings(NotificationHelper.SECONDARY_CHANNEL)
    131                 R.id.btnA -> goToNotificationSettings()
    132                 else -> Log.e(TAG, "Unknown click event.")
    133             }
    134         }
    135     }
    136 
    137     companion object {
    138         private val TAG = MainActivity::class.java.simpleName
    139 
    140         private val NOTI_PRIMARY1 = 1100
    141         private val NOTI_PRIMARY2 = 1101
    142         private val NOTI_SECONDARY1 = 1200
    143         private val NOTI_SECONDARY2 = 1201
    144     }
    145 }
    146