diff --git a/.idea/deploymentTargetDropDown.xml b/.idea/deploymentTargetDropDown.xml index 9d1b5b7..0c0c338 100644 --- a/.idea/deploymentTargetDropDown.xml +++ b/.idea/deploymentTargetDropDown.xml @@ -2,24 +2,8 @@ - - - - - - - - - - - - - - - - - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 8978d23..0ad17cb 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,3 +1,4 @@ + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e95e14b..4cace37 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ - + = 33) { + if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.POST_NOTIFICATIONS}, 101); + } + } + } @Override diff --git a/app/src/main/java/uk/kagurach/android101/MainActivity2.java b/app/src/main/java/uk/kagurach/android101/MainActivity2.java index 46355e4..7aed458 100644 --- a/app/src/main/java/uk/kagurach/android101/MainActivity2.java +++ b/app/src/main/java/uk/kagurach/android101/MainActivity2.java @@ -28,7 +28,6 @@ public class MainActivity2 extends AppCompatActivity { v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); - Log.d("K","2"); } @Override @@ -73,7 +72,10 @@ public class MainActivity2 extends AppCompatActivity { if (_r>=0&&_g>=0&&_b>=0&&_r<=255&&_g<=255&&_b<=255){ t.setTextColor(Color.rgb(_r,_g,_b)); }else{ - Log.e("page2.setcolor","wrong color"); + NotificationHelper helper = new NotificationHelper(this); + helper.createNotificationChannel(); + helper.pushNotification("Page 2: Error Setting Color", + "The color: R="+_r+" G="+_g+" B="+_b+" is invalid!"); } } diff --git a/app/src/main/java/uk/kagurach/android101/NotificationHelper.kt b/app/src/main/java/uk/kagurach/android101/NotificationHelper.kt new file mode 100644 index 0000000..4f2bff0 --- /dev/null +++ b/app/src/main/java/uk/kagurach/android101/NotificationHelper.kt @@ -0,0 +1,67 @@ +package uk.kagurach.android101 + +import android.Manifest +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.content.pm.PackageManager +import androidx.core.app.ActivityCompat +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat +import androidx.core.content.ContextCompat.getSystemService +import kotlin.random.Random + +class NotificationHelper(_ctx: Context) { + val CHANNEL_ID = "DBG_PUSHER" + val name = "Debug Pusher" + var ctx: Context = _ctx + var notifyID = 1; + var rng = Random(114514); + private val notificationManager = + getSystemService(ctx,NotificationManager::class.java) as NotificationManager + + public fun pushNotification(title:String,content:String) { + val builder = NotificationCompat.Builder(ctx, CHANNEL_ID) + .setSmallIcon(R.drawable.ic_launcher_foreground) + .setContentTitle(title) + .setContentText(content) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) + .setAutoCancel(true) + .setStyle(NotificationCompat.BigTextStyle() + .bigText(content)) + + with(NotificationManagerCompat.from(ctx)) { + if (ActivityCompat.checkSelfPermission( + ctx, + Manifest.permission.POST_NOTIFICATIONS + ) != PackageManager.PERMISSION_GRANTED + ) { + return + } + notify(notifyID, builder.build()) + val lastNotifyID = notifyID; + while (notifyID==lastNotifyID){ + notifyID = rng.nextInt() + if (notifyID<=0){ + notifyID = -notifyID + notifyID += 1 + } + rng = Random(notifyID) + } + } + } + + + protected fun createNotificationChannel() { + // Create the NotificationChannel, but only on API 26+ because + // the NotificationChannel class is not in the Support Library. + val importance = NotificationManager.IMPORTANCE_DEFAULT + val channel = NotificationChannel(CHANNEL_ID, name, importance).apply {} + // Register the channel with the system. + notificationManager.createNotificationChannel(channel) + } + + protected fun deleteNotificationChannel(){ + notificationManager.deleteNotificationChannel(CHANNEL_ID) + } +} \ No newline at end of file diff --git a/app/src/main/java/uk/kagurach/android101/Page3.java b/app/src/main/java/uk/kagurach/android101/Page3.java index 9f38ab0..5191650 100644 --- a/app/src/main/java/uk/kagurach/android101/Page3.java +++ b/app/src/main/java/uk/kagurach/android101/Page3.java @@ -9,7 +9,6 @@ import androidx.core.view.ViewCompat; import androidx.core.view.WindowInsetsCompat; public class Page3 extends AppCompatActivity { - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -21,4 +20,5 @@ public class Page3 extends AppCompatActivity { return insets; }); } + } \ No newline at end of file