11package apps .jizzu .simpletodo .activity ;
22
3+ import android .app .Notification ;
4+ import android .app .NotificationChannel ;
5+ import android .app .NotificationManager ;
6+ import android .app .PendingIntent ;
37import android .appwidget .AppWidgetManager ;
48import android .content .ComponentName ;
59import android .content .Context ;
610import android .content .Intent ;
11+ import android .graphics .Color ;
712import android .os .Bundle ;
813import android .os .Handler ;
914import android .support .design .widget .FloatingActionButton ;
15+ import android .support .v4 .app .NotificationCompat ;
1016import android .support .v4 .content .ContextCompat ;
1117import android .support .v7 .app .AppCompatActivity ;
1218import android .support .v7 .widget .LinearLayoutManager ;
3440import apps .jizzu .simpletodo .adapter .RecyclerViewAdapter ;
3541import apps .jizzu .simpletodo .adapter .RecyclerViewEmptySupport ;
3642import apps .jizzu .simpletodo .alarm .AlarmHelper ;
43+ import apps .jizzu .simpletodo .alarm .AlarmReceiver ;
3744import apps .jizzu .simpletodo .database .DBHelper ;
3845import apps .jizzu .simpletodo .model .ModelTask ;
3946import apps .jizzu .simpletodo .settings .SettingsActivity ;
4956import static android .content .ContentValues .TAG ;
5057
5158
52- public class MainActivity extends AppCompatActivity {
59+ public class MainActivity extends AppCompatActivity implements RecyclerViewAdapter . UpdateNotificationDataCallback {
5360
5461 private RecyclerViewEmptySupport mRecyclerView ;
5562 public RecyclerViewAdapter mAdapter ;
@@ -60,6 +67,7 @@ public class MainActivity extends AppCompatActivity {
6067 private RecyclerView .LayoutManager mLayoutManager ;
6168 public static boolean mSearchViewIsOpen ;
6269 private MaterialSearchView mSearchView ;
70+ private NotificationManager mNotificationManager ;
6371 public static boolean mShowAnimation ;
6472 public static boolean mActivityIsShown ;
6573
@@ -74,7 +82,7 @@ protected void onCreate(Bundle savedInstanceState) {
7482
7583 // Set up "What's New" screen
7684 WhatsNew whatsNew = WhatsNew .newInstance (
77- new WhatsNewItem (getString (R .string .whats_new_item_1_title ), getString (R .string .whats_new_item_1_text ), R . drawable . whats_new_release )
85+ new WhatsNewItem (getString (R .string .whats_new_item_1_title ), getString (R .string .whats_new_item_1_text ))
7886 );
7987 whatsNew .setTitleColor (ContextCompat .getColor (this , R .color .colorAccent ));
8088 whatsNew .setTitleText (getString (R .string .whats_new_title ));
@@ -111,7 +119,22 @@ protected void onCreate(Bundle savedInstanceState) {
111119 mRecyclerView .setEmptyView (mEmptyView );
112120 mFab = findViewById (R .id .fab );
113121
114- ItemTouchHelper .Callback callback = new ListItemTouchHelper (mAdapter , mRecyclerView );
122+ mAdapter .registerCallback (this );
123+
124+ ItemTouchHelper .Callback callback = new ListItemTouchHelper (mAdapter , mRecyclerView ) {
125+ @ Override
126+ public boolean onMove (RecyclerView recyclerView , RecyclerView .ViewHolder viewHolder , RecyclerView .ViewHolder target ) {
127+ super .onMove (recyclerView , viewHolder , target );
128+ updateGeneralNotification ();
129+ return true ;
130+ }
131+
132+ @ Override
133+ public void onSwiped (RecyclerView .ViewHolder viewHolder , int direction ) {
134+ super .onSwiped (viewHolder , direction );
135+ updateGeneralNotification ();
136+ }
137+ };
115138 ItemTouchHelper touchHelper = new ItemTouchHelper (callback );
116139 touchHelper .attachToRecyclerView (mRecyclerView );
117140
@@ -126,7 +149,7 @@ protected void onCreate(Bundle savedInstanceState) {
126149 @ Override
127150 public boolean onQueryTextSubmit (String query ) {
128151 Log .d (TAG , "onQueryTextSubmit" );
129- return false ;
152+ return true ;
130153 }
131154
132155 @ Override
@@ -259,6 +282,103 @@ public void updateWidget() {
259282 sendBroadcast (intent );
260283 }
261284
285+ /**
286+ * Updates general notification data.
287+ */
288+ public void updateGeneralNotification () {
289+ if (mPreferenceHelper .getBoolean (PreferenceHelper .GENERAL_NOTIFICATION_IS_ON )) {
290+ if (mAdapter .getItemCount () != 0 ) {
291+ showGeneralNotification ();
292+ } else {
293+ removeGeneralNotification ();
294+ }
295+ } else {
296+ removeGeneralNotification ();
297+ }
298+ }
299+
300+ /**
301+ * Set up and show general notification.
302+ */
303+ public void showGeneralNotification () {
304+ StringBuilder stringBuilder = new StringBuilder ();
305+
306+ Intent resultIntent = new Intent (this , MainActivity .class );
307+ PendingIntent resultPendingIntent = PendingIntent .getActivity (this , 0 , resultIntent , PendingIntent .FLAG_UPDATE_CURRENT );
308+
309+ for (ModelTask task : RecyclerViewAdapter .mItems ) {
310+ stringBuilder .append ("• " ).append (task .getTitle ());
311+
312+ if (task .getPosition () < mAdapter .getItemCount () - 1 ) {
313+ stringBuilder .append ("\n \n " );
314+ }
315+ }
316+
317+ String notificationTitle = "" ;
318+ switch (mAdapter .getItemCount () % 10 ) {
319+ case 1 :
320+ notificationTitle = getString (R .string .general_notification_1 ) + " " + mAdapter .getItemCount () + " " + getString (R .string .general_notification_2 );
321+ break ;
322+
323+ case 2 :
324+ case 3 :
325+ case 4 :
326+ notificationTitle = getString (R .string .general_notification_1 ) + " " + mAdapter .getItemCount () + " " + getString (R .string .general_notification_3 );
327+ break ;
328+
329+ case 0 :
330+ case 5 :
331+ case 6 :
332+ case 7 :
333+ case 8 :
334+ case 9 :
335+ notificationTitle = getString (R .string .general_notification_1 ) + " " + mAdapter .getItemCount () + " " + getString (R .string .general_notification_4 );
336+ break ;
337+ }
338+
339+ mNotificationManager = (NotificationManager ) getSystemService (NOTIFICATION_SERVICE );
340+
341+ // Set NotificationChannel for Android Oreo
342+ if (android .os .Build .VERSION .SDK_INT >= android .os .Build .VERSION_CODES .O ) {
343+ NotificationChannel channel = new NotificationChannel (AlarmReceiver .CHANNEL_ID , "SimpleToDo Notifications" ,
344+ NotificationManager .IMPORTANCE_HIGH );
345+ channel .enableLights (true );
346+ channel .setLightColor (Color .GREEN );
347+ channel .enableVibration (true );
348+ mNotificationManager .createNotificationChannel (channel );
349+ }
350+
351+ NotificationCompat .Builder builder = new NotificationCompat .Builder (this , AlarmReceiver .CHANNEL_ID )
352+ .setContentTitle (notificationTitle )
353+ .setContentText (stringBuilder .toString ())
354+ .setNumber (mAdapter .getItemCount ())
355+ .setStyle (new NotificationCompat .BigTextStyle ().bigText (stringBuilder .toString ()))
356+ .setColor (ContextCompat .getColor (this , R .color .colorAccent ))
357+ .setSmallIcon (R .drawable .ic_check_circle_white_24dp )
358+ .setContentIntent (resultPendingIntent )
359+ .setOngoing (true );
360+
361+ Notification notification = builder .build ();
362+ mNotificationManager .notify (1 , notification );
363+ }
364+
365+ /**
366+ * Removes general notification.
367+ */
368+ public void removeGeneralNotification () {
369+ if (mNotificationManager != null ) {
370+ mNotificationManager .cancel (1 );
371+ }
372+ }
373+
374+ /**
375+ * Updates general notification data when user click the "Cancel" snackbar button.
376+ */
377+ @ Override
378+ public void updateData () {
379+ updateGeneralNotification ();
380+ }
381+
262382 @ Override
263383 public boolean onCreateOptionsMenu (Menu menu ) {
264384 getMenuInflater ().inflate (R .menu .menu , menu );
@@ -327,6 +447,7 @@ public void run() {
327447 mFab .setVisibility (View .VISIBLE );
328448 }
329449 MyApplication .activityResumed ();
450+ updateGeneralNotification ();
330451 }
331452
332453 @ Override
@@ -365,6 +486,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
365486 long id = mHelper .saveTask (task );
366487 task .setId (id );
367488 mAdapter .addItem (task );
489+ updateGeneralNotification ();
368490 }
369491
370492 @ Override
0 commit comments