77import  android .content .Context ;
88import  android .content .Intent ;
99import  android .content .SharedPreferences ;
10+ import  android .graphics .Bitmap ;
11+ import  android .graphics .Color ;
12+ import  android .graphics .DashPathEffect ;
1013import  android .os .Build ;
1114import  android .util .Log ;
1215
16+ import  com .github .mikephil .charting .charts .LineChart ;
17+ import  com .github .mikephil .charting .data .Entry ;
18+ import  com .github .mikephil .charting .data .LineData ;
19+ import  com .github .mikephil .charting .data .LineDataSet ;
20+ import  com .github .mikephil .charting .formatter .LargeValueFormatter ;
21+ import  com .github .mikephil .charting .interfaces .datasets .ILineDataSet ;
22+ 
1323import  org .json .JSONException ;
1424
25+ import  java .text .SimpleDateFormat ;
26+ import  java .util .ArrayList ;
27+ import  java .util .Calendar ;
28+ import  java .util .Date ;
29+ import  java .util .Locale ;
30+ import  java .util .Map ;
1531import  java .util .concurrent .ExecutionException ;
1632
1733import  androidx .annotation .NonNull ;
34+ import  androidx .core .content .ContextCompat ;
1835import  javinator9889 .bitcoinpools .BitCoinApp ;
1936import  javinator9889 .bitcoinpools .Constants ;
2037import  javinator9889 .bitcoinpools .DataLoaderScreen ;
38+ import  javinator9889 .bitcoinpools .FragmentViews .CustomMarkerView ;
39+ import  javinator9889 .bitcoinpools .JSONTools .JSONTools ;
2140import  javinator9889 .bitcoinpools .MainActivity ;
2241import  javinator9889 .bitcoinpools .NetTools .net ;
2342import  javinator9889 .bitcoinpools .R ;
2443
44+ import  static  javinator9889 .bitcoinpools .Constants .API_URL ;
45+ 
2546/** 
2647 * Created by Javinator9889 on 23/01/2018. 
2748 * Based on: https://stackoverflow.com/a/46991229 
@@ -33,9 +54,11 @@ class NotificationHandler {
3354    private  static  boolean  NOTIFIED_LOW  = false ;
3455    private  static  int  SPECIFIC_VALUE  = 0 ;
3556    private  static  float  MPU ;
57+     private  Context  mContext ;
3658
37-     private  NotificationHandler () {
59+     private  NotificationHandler (@ NonNull   Context   context ) {
3860        final  SharedPreferences  sp  = BitCoinApp .getSharedPreferences ();
61+         mContext  = context ;
3962        Log .d (Constants .LOG .NTAG , Constants .LOG .CREATING_NOTIFICATION );
4063        NOTIFICATIONS_ENABLED  = sp .getBoolean (Constants .SHARED_PREFERENCES .NOTIFICATIONS_ENABLED ,
4164                false );
@@ -88,6 +111,9 @@ void putNotification() {
88111            }
89112            if  (notify ) {
90113                Log .d (Constants .LOG .NTAG , Constants .LOG .NOTIFYING );
114+                 updatePreferences ();
115+                 final  LineChart  chart  = new  LineChart (mContext );
116+                 Bitmap  chartBitmap  = generateLineChart (chart );
91117                String  name  = BitCoinApp .getAppContext ().getString (R .string .alerts );
92118                String  description  = BitCoinApp .getAppContext ().getString (R .string .description );
93119                Notification .Builder  notification ;
@@ -126,6 +152,8 @@ void putNotification() {
126152                            .setStyle (new  Notification .BigTextStyle ()
127153                                    .bigText (notificationTextLong ));
128154                }
155+                 if  (chartBitmap  != null )
156+                     notification .setLargeIcon (chartBitmap );
129157                notification .setContentIntent (clickIntent );
130158                assert  notificationManager  != null ;
131159                notificationManager .notify (Constants .NOTIFICATION_ID , notification .build ());
@@ -134,9 +162,73 @@ void putNotification() {
134162        }
135163    }
136164
165+     private  Bitmap  generateLineChart (@ NonNull  final  LineChart  lineChart ) {
166+         Map <Date , Float > pricesMap ;
167+         Calendar  start  = Calendar .getInstance ();
168+         start .add (Calendar .DAY_OF_MONTH , -7 );
169+         String  startDate  = String .format (Locale .US , "%d-%02d-%02d" ,
170+                 start .get (Calendar .YEAR ),
171+                 start .get (Calendar .MONTH ),
172+                 start .get (Calendar .DAY_OF_MONTH ));
173+         String  url  = API_URL  + "?start="  + startDate  + "&end="  +
174+                 new  SimpleDateFormat ("yyyy-MM-dd" , Locale .US ).format (Calendar .getInstance ().getTime ());
175+         pricesMap  = getValuesByDatedURL (url );
176+         if  (pricesMap  == null )
177+             return  null ;
178+         lineChart .setDrawGridBackground (false );
179+         lineChart .getDescription ().setEnabled (false );
180+         CustomMarkerView  markerView  = new  CustomMarkerView (mContext , R .layout .marker_view );
181+         markerView .setChartView (lineChart );
182+         lineChart .setMarker (markerView );
183+         ArrayList <Entry > values  = new  ArrayList <>(pricesMap .size ());
184+         int  i  = 0 ;
185+         for  (Date  currentDate  : pricesMap .keySet ()) {
186+             values .add (new  Entry (i , pricesMap .get (currentDate )));
187+             ++i ;
188+         }
189+         LineDataSet  lineDataSet  = new  LineDataSet (values , mContext .getString (R .string 
190+                 .latest_7_days ));
191+         lineDataSet .setDrawIcons (false );
192+         lineDataSet .enableDashedLine (10f , 5f , 0f );
193+         lineDataSet .enableDashedHighlightLine (10f , 5f , 0f );
194+         lineDataSet .setColor (Color .BLACK );
195+         lineDataSet .setCircleColor (Color .BLACK );
196+         lineDataSet .setLineWidth (1f );
197+         lineDataSet .setCircleRadius (3f );
198+         lineDataSet .setDrawCircleHole (false );
199+         lineDataSet .setValueTextSize (9f );
200+         lineDataSet .setDrawFilled (true );
201+         lineDataSet .setFormLineWidth (1f );
202+         lineDataSet .setFormLineDashEffect (new  DashPathEffect (new  float []{10f , 5f },
203+                 0f ));
204+         lineDataSet .setFormSize (15.f );
205+         lineDataSet .setMode (LineDataSet .Mode .CUBIC_BEZIER );
206+         lineDataSet .setFillDrawable (ContextCompat .getDrawable (mContext , R .drawable .fade_red ));
207+         lineDataSet .setDrawCircles (false );
208+         ArrayList <ILineDataSet > dataSets  = new  ArrayList <>(1 );
209+         dataSets .add (lineDataSet );
210+         LineData  data  = new  LineData (dataSets );
211+         lineChart .setData (data );
212+         lineChart .getAxisLeft ().setValueFormatter (new  LargeValueFormatter ());
213+         lineChart .invalidate ();
214+         return  lineChart .getChartBitmap ();
215+     }
216+ 
217+     private  Map <Date , Float > getValuesByDatedURL (@ NonNull  String  url ) {
218+         net  netRequest  = new  net ();
219+         netRequest .execute (url );
220+         try  {
221+             return  JSONTools .sortDateByValue (
222+                     JSONTools .convert2DateHashMap (netRequest .get ().getJSONObject ("bpi" )));
223+         } catch  (InterruptedException  | ExecutionException  |
224+                 JSONException  | NullPointerException  ignored ) {
225+             return  null ;
226+         }
227+     }
228+ 
137229    @ NonNull 
138-     static  NotificationHandler  newInstance () {
139-         return  new  NotificationHandler ();
230+     static  NotificationHandler  newInstance (@ NonNull   Context   context ) {
231+         return  new  NotificationHandler (context );
140232    }
141233
142234    private  static  float  initMPU () {
0 commit comments