diff --git a/app/src/main/java/net/headlezz/notificationlogger/DispatchableNotification.java b/app/src/main/java/net/headlezz/notificationlogger/DispatchableNotification.java new file mode 100644 index 0000000..0370268 --- /dev/null +++ b/app/src/main/java/net/headlezz/notificationlogger/DispatchableNotification.java @@ -0,0 +1,140 @@ +package net.headlezz.notificationlogger; + + +import android.app.Notification; +import android.content.Context; +import android.support.v4.app.NotificationManagerCompat; +import android.support.v7.app.NotificationCompat; + +import java.util.Date; + +public class DispatchableNotification { + + private Context mContext; + private int mId; + private Notification mNotification; + + + private DispatchableNotification(Context mContext, String mMessage, String mTitle, int mId, boolean mSound, boolean mVibrate, boolean mBlink, boolean mAutoCancel, int mIcon, String mCategory) { + this.mContext = mContext; + this.mId = mId; + + int defaults; + if(mSound && mVibrate && mBlink) + defaults = Notification.DEFAULT_ALL; + else { + if(mSound) { + if(mVibrate) + defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; + else if(mBlink) + defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS; + else + defaults = Notification.DEFAULT_SOUND; + } else if(mVibrate) { + if(mBlink) + defaults = Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS; + else + defaults = Notification.DEFAULT_VIBRATE; + } else + defaults = Notification.DEFAULT_LIGHTS; + } + + mNotification = new NotificationCompat.Builder(mContext) + .setContentText(mMessage) + .setContentTitle(mTitle) + .setSmallIcon(mIcon) + .setCategory(mCategory) + .setAutoCancel(mAutoCancel) + .setDefaults(defaults) + .build(); + } + + public void dispatch() { + NotificationManagerCompat.from(mContext).notify(mId, mNotification); + } + + public void shedule(Date date) { + // TODO + } + + public static class Builder { + private Context mContext; + + private String mMessage; + private String mTitle; + private int mId; + + private boolean mSound; + private boolean mVibrate; + private boolean mBlink; + private boolean mAutoCancel; + + private int mIcon; + private String mCategory; + + public Builder(Context context) { + mContext = context; + } + + public Builder setMessage(String message) { + this.mMessage = message; + return this; + } + + public Builder setTitle(String title) { + this.mTitle = title; + return this; + } + + public Builder setId(int id) { + this.mId = id; + return this; + } + + public Builder setSound(boolean sound) { + this.mSound = sound; + return this; + } + + public Builder setVibrate(boolean vibrate) { + this.mVibrate = vibrate; + return this; + } + + public Builder setBlink(boolean blink) { + this.mBlink = blink; + return this; + } + + public Builder setAutoCancel(boolean autoCancel) { + this.mAutoCancel = autoCancel; + return this; + } + + public Builder setIcon(int icon) { + this.mIcon = icon; + return this; + } + + public Builder setCategory(String category) { + this.mCategory = category; + return this; + } + + public DispatchableNotification build() { + return new DispatchableNotification( + mContext, + mMessage, + mTitle, + mId, + mSound, + mVibrate, + mBlink, + mAutoCancel, + mIcon, + mCategory + ); + } + } + +} diff --git a/app/src/main/java/net/headlezz/notificationlogger/NotificationCreationFragment.java b/app/src/main/java/net/headlezz/notificationlogger/NotificationCreationFragment.java index 9b64072..0d55bf5 100644 --- a/app/src/main/java/net/headlezz/notificationlogger/NotificationCreationFragment.java +++ b/app/src/main/java/net/headlezz/notificationlogger/NotificationCreationFragment.java @@ -3,48 +3,60 @@ package net.headlezz.notificationlogger; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v4.app.NotificationCompat; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Spinner; +import android.widget.Toast; import butterknife.Bind; import butterknife.ButterKnife; -public class NotificationCreationFragment extends Fragment { +public class NotificationCreationFragment extends Fragment implements View.OnClickListener { + + // TODO intent + // TODO check id size @Bind(R.id.create_etId) - EditText mEtId; + EditText etId; @Bind(R.id.create_etTitle) - EditText mEtTitle; + EditText etTitle; @Bind(R.id.create_etMessage) - EditText mEtMessage; + EditText etMessage; @Bind(R.id.create_cbAutoCancel) - CheckBox mCbAutoCancel; + CheckBox cbAutoCancel; @Bind(R.id.create_cbBlink) - CheckBox mCbBlink; + CheckBox cbBlink; @Bind(R.id.create_cbSound) - CheckBox mCbSound; + CheckBox cbSound; @Bind(R.id.create_cbVibrate) - CheckBox mCbVibrate; + CheckBox cbVibrate; @Bind(R.id.create_spCategory) - Spinner mSpCategory; + Spinner spCategory; @Bind(R.id.create_spIntent) - Spinner mSpIntent; + Spinner spIntent; @Bind(R.id.create_spIcon) - Spinner mSpIcon; + Spinner spIcon; + + @Bind(R.id.create_btDispatch) + Button btDispatch; + + @Bind(R.id.create_btSchedule) + Button btSchedule; final int[] mNotificationIcons = new int[]{ R.drawable.ic_adb, @@ -57,6 +69,23 @@ public class NotificationCreationFragment extends Fragment { R.drawable.ic_sms }; + final String[] categories = new String[] { + NotificationCompat.CATEGORY_ALARM, + NotificationCompat.CATEGORY_CALL, + NotificationCompat.CATEGORY_EMAIL, + NotificationCompat.CATEGORY_ERROR, + NotificationCompat.CATEGORY_EVENT, + NotificationCompat.CATEGORY_MESSAGE, + NotificationCompat.CATEGORY_PROGRESS, + NotificationCompat.CATEGORY_PROMO, + NotificationCompat.CATEGORY_RECOMMENDATION, + NotificationCompat.CATEGORY_SERVICE, + NotificationCompat.CATEGORY_SOCIAL, + NotificationCompat.CATEGORY_STATUS, + NotificationCompat.CATEGORY_SYSTEM, + NotificationCompat.CATEGORY_TRANSPORT + }; + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -65,13 +94,36 @@ public class NotificationCreationFragment extends Fragment { ArrayAdapter categoryAdapter = ArrayAdapter.createFromResource(getContext(), R.array.create_categories, android.R.layout.simple_spinner_item); categoryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - mSpCategory.setAdapter(categoryAdapter); + spCategory.setAdapter(categoryAdapter); SpinnerIconAdapter iconAdapter = new SpinnerIconAdapter(getContext(), getResources().getStringArray(R.array.create_icons), mNotificationIcons); iconAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - mSpIcon.setAdapter(iconAdapter); + spIcon.setAdapter(iconAdapter); + + btDispatch.setOnClickListener(this); return view; } + + @Override + public void onClick(View v) { + // TODO check input + DispatchableNotification dn = new DispatchableNotification.Builder(getContext()) + .setTitle(etTitle.getText().toString()) + .setMessage(etMessage.getText().toString()) + .setIcon(Integer.valueOf(etId.getText().toString())) + .setIcon(mNotificationIcons[spIcon.getSelectedItemPosition()]) + .setSound(cbSound.isChecked()) + .setVibrate(cbVibrate.isChecked()) + .setBlink(cbBlink.isChecked()) + .setAutoCancel(cbAutoCancel.isChecked()) + .setCategory(categories[spCategory.getSelectedItemPosition()]) + .build(); + + if(v.getId() == R.id.create_btDispatch) + dn.dispatch(); + else + Toast.makeText(getContext(), "Not implemented", Toast.LENGTH_LONG) + } } diff --git a/app/src/main/res/layout/notification_creation_frag.xml b/app/src/main/res/layout/notification_creation_frag.xml index 530175a..6686e9e 100644 --- a/app/src/main/res/layout/notification_creation_frag.xml +++ b/app/src/main/res/layout/notification_creation_frag.xml @@ -129,7 +129,7 @@ android:layout_marginTop="16dp">