Lab 6 X
Lab 6 X
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="7"
android:columnCount="2"
android:orientation="horizontal">
<EditText
android:id="@+id/main_primary_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:hint="@string/main_primary_title"
android:text="@string/main_primary_title" />
<Button
android:id="@+id/main_primary_send1"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:text="@string/main_primary_send1" />
<Button
android:id="@+id/main_primary_send2"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:text="@string/main_primary_send2" />
<ImageButton
android:id="@+id/main_primary_config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:src="@android:drawable/ic_menu_preferences"
android:contentDescription="@string/main_primary_config" />
<Space
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal" />
<EditText
android:id="@+id/main_secondary_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:hint="@string/main_secondary_title"
android:text="@string/main_secondary_title" />
<Button
android:id="@+id/main_secondary_send1"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:text="@string/main_secondary_send1" />
<Button
android:id="@+id/main_secondary_send2"
android:layout_columnWeight="1"
android:layout_height="wrap_content"
android:text="@string/main_secondary_send2"/>
<ImageButton
android:id="@+id/main_secondary_config"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="2"
android:layout_gravity="fill_horizontal"
android:src="@android:drawable/ic_menu_preferences"
android:contentDescription="@string/main_secondary_config" />
</GridLayout>
<LinearLayout
android:id="@+id/main_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:text="@string/btnA_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/btnA"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Plik: MainActivity
package utp.edu.notification;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
/**
* Display main screen for sample. Displays controls for sending test notifica-
tions.
*/
public class MainActivity extends Activity {
private static final String TAG = MainActivity.class.getSimpleName();
/*
* A view model for interacting with the UI elements.
*/
private MainNotifUi Notifui;
/*
* A
*/
private NotificationHelper noti;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noti = new NotificationHelper(this);
Notifui = new MainNotifUi(findViewById(R.id.activity_main));
}
/**
* Send activity notifications.
*
* @param id The ID of the notification to create
* @param title The title of the notification
*/
@RequiresApi(api = Build.VERSION_CODES.Q)
public void sendNotification(int id, String title) {
NotificationCompat.Builder nb = null;
switch (id) {
case NOTI_PRIMARY1:
nb = noti.getNotification1(title, getString(R.string.pri-
mary1_body), "Kanał1 P1");
break;
case NOTI_PRIMARY2:
nb = noti.getNotification1(title, getString(R.string.pri-
mary2_body), "Kanał1 P2");
break;
case NOTI_SECONDARY1:
nb = noti.getNotification2(title, getString(R.string.second-
ary1_body), "Kanał2 P1");
break;
case NOTI_SECONDARY2:
nb = noti.getNotification2(title, getString(R.string.second-
ary2_body), "Kanał2 P2");
break;
}
if (nb != null) {
noti.notify(id, nb);
}
}
/**
* Send Intent to load system Notification Settings for this app.
*/
public void goToNotificationSettings() {
Intent i = new Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
startActivity(i);
}
/**
* Send intent to load system Notification Settings UI for a particular chan-
nel.
*
* @param channel Name of channel to configure
*/
public void goToNotificationSettings(String channel) {
Intent i = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
i.putExtra(Settings.EXTRA_CHANNEL_ID, channel);
startActivity(i);
}
/**
* View model for interacting with Activity UI elements. (Keeps core logic for
sample
* seperate.)
*/
class MainNotifUi implements View.OnClickListener {
final TextView titlePrimary;
final TextView titleSecondary;
titleSecondary = root.findViewById(R.id.main_secondary_title);
root.findViewById(R.id.main_secondary_send1).setOnClickListener(this);
root.findViewById(R.id.main_secondary_send2).setOnClickListener(this);
root.findViewById(R.id.main_secondary_config).setOnClick-
Listener(this);
root.findViewById(R.id.btnA).setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.Q)
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.main_primary_send1:
sendNotification(NOTI_PRIMARY1, getTitlePrimaryText());
break;
case R.id.main_primary_send2:
sendNotification(NOTI_PRIMARY2, getTitlePrimaryText());
break;
case R.id.main_primary_config:
goToNotificationSettings(NotificationHelper.PRIMARY_CHANNEL);
break;
case R.id.main_secondary_send1:
sendNotification(NOTI_SECONDARY1, getTitleSecondaryText());
break;
case R.id.main_secondary_send2:
sendNotification(NOTI_SECONDARY2, getTitleSecondaryText());
break;
case R.id.main_secondary_config:
goToNotificationSettings(NotificationHelper.SECONDARY_CHAN-
NEL);
break;
case R.id.btnA:
goToNotificationSettings();
break;
default:
Log.e(TAG, "Unknown click event.");
break;
}
}
}
}
Plik: NotificationHelper
package utp.edu.notification;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
/**
* Helper class to manage notification channels, and create notifications.
*/
class NotificationHelper extends ContextWrapper {
private NotificationManager manager;
public static final String PRIMARY_CHANNEL = "default";
public static final String SECONDARY_CHANNEL = "second";
/**
* Registers notification channels, which can be used later by individual no-
tifications.
*
* @param ctx The application context
*/
@TargetApi(Build.VERSION_CODES.Q)
chan1.setLightColor(Color.GREEN);
chan1.setAllowBubbles(false);
long[] longs = {45L, 1007L};
chan1.setVibrationPattern(longs);
chan1.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
getManager().createNotificationChannel(chan1);
/**
* Get a notification of type 1
* <p>
* Provide the builder rather than the notification it's self as useful for
making notification
* changes.
*
* @param title the title of the notification
* @param body the body text for the notification
* @return the builder as it keeps a reference to the notification (since API
24)
*/
@RequiresApi(api = Build.VERSION_CODES.Q)
/**
* Build notification for secondary channel.
*
* @param title Title for notification.
* @param body Message for notification.
* @return A Notification.Builder configured with the selected channel and de-
tails
*/
@RequiresApi(api = Build.VERSION_CODES.Q)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(getString(R.string.long_dummy_text))
.setBigContentTitle("Big Content Title")
.setSummaryText("Summary Text"))
.setContentText(body)
.setSmallIcon(getSmallIcon())
.setColor(Color.YELLOW)
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setOnlyAlertOnce(false)
.addAction(R.mipmap.ic_launcher, body, actionIntent);
/**
* Send a notification.
*
* @param id The ID of the notification
* @param notification The notification object
*/
@RequiresApi(api = Build.VERSION_CODES.Q)
/**
* Get the small icon for this app
*
* @return The small icon resource id
*/
@RequiresApi(api = Build.VERSION_CODES.Q)
return android.R.drawable.stat_notify_chat;
}
@RequiresApi(api = Build.VERSION_CODES.Q)
return android.R.drawable.ic_dialog_email;
}
/**
* Get the notification manager.
* <p>
* Utility method as this helper works with it a lot.
*
* @return The system service NotificationManager
*/
@RequiresApi(api = Build.VERSION_CODES.Q)
Plik: NotificationReceiver
package utp.edu.notification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;