33import android .app .AlertDialog ;
44import android .app .TimePickerDialog ;
55import android .content .Context ;
6+ import android .content .Intent ;
67import android .content .SharedPreferences ;
8+ import android .media .RingtoneManager ;
9+ import android .net .Uri ;
710import android .os .Bundle ;
811import android .preference .PreferenceManager ;
912import android .text .format .DateFormat ;
1013import android .view .View ;
14+ import android .widget .Button ;
1115import android .widget .CheckBox ;
1216import android .widget .CompoundButton ;
1317import android .widget .EditText ;
1620import android .widget .TimePicker ;
1721
1822import com .eveningoutpost .dexdrip .models .AlertType ;
23+ import com .eveningoutpost .dexdrip .models .UserError ;
1924import com .eveningoutpost .dexdrip .services .MissedReadingService ;
25+ import com .eveningoutpost .dexdrip .utilitymodels .AlertPlayer ;
26+ import com .eveningoutpost .dexdrip .utilitymodels .Pref ;
2027import com .eveningoutpost .dexdrip .utils .ActivityWithMenu ;
2128
2229
2330public class MissedReadingActivity extends ActivityWithMenu {
2431 public static String menu_name = "Missed reading" ;
2532 private Context mContext ;
33+ private Button buttonalertMp3 ;
2634
2735 private CheckBox checkboxEnableAlert ;
2836 private CheckBox checkboxAllDay ;
2937 private CheckBox checkboxEnableReraise ;
38+ private String audioPath ; // Local representation of the path to the sound file
39+ private EditText alertMp3File ; // Sound file title
3040
3141 private LinearLayout layoutTimeBetween ;
3242 private LinearLayout timeInstructions ;
@@ -49,6 +59,8 @@ public class MissedReadingActivity extends ActivityWithMenu {
4959 private int endHour = 23 ;
5060 private int endMinute = 59 ;
5161 private int missedMinutes = 59 ;
62+ private final static String TAG = AlertPlayer .class .getSimpleName ();
63+ EditAlertActivity editAlert = new EditAlertActivity ();
5264
5365
5466 @ Override
@@ -59,9 +71,18 @@ protected void onCreate(Bundle savedInstanceState) {
5971
6072 viewTimeStart = (TextView ) findViewById (R .id .missed_reading_time_start );
6173 viewTimeEnd = (TextView ) findViewById (R .id .missed_reading_time_end );
74+ buttonalertMp3 = (Button )findViewById (R .id .Button_mra_mp3_file );
6275 checkboxAllDay = (CheckBox ) findViewById (R .id .missed_reading_all_day );
6376 checkboxEnableAlert = (CheckBox ) findViewById (R .id .missed_reading_enable_alert );
6477 checkboxEnableReraise = (CheckBox ) findViewById (R .id .missed_reading_enable_alerts_reraise );
78+ /** xDrip used to use the other alerts sound file for the missed readings alert.
79+ * To avoid causing an unexpected behavior for a previous user of xDrip, the missed reading alert
80+ * by default uses the same sound file as the other alerts alert.
81+ **/
82+ if (Pref .getString ("bg_missed_alerts_sound" , null ) == null ) { // If missed reading sound file has never been set
83+ Pref .setString ("bg_missed_alerts_sound" , Pref .getString ("other_alerts_sound" , "content://settings/system/alarm_alert" )); // Set it to the other alerts sound
84+ }
85+ alertMp3File = (EditText ) findViewById (R .id .bg_missed_alerts_sound );
6586
6687 layoutTimeBetween = (LinearLayout ) findViewById (R .id .missed_reading_time_between );
6788 timeInstructions = (LinearLayout ) findViewById (R .id .missed_reading_instructions );
@@ -83,6 +104,7 @@ protected void onCreate(Bundle savedInstanceState) {
83104 boolean enableAlert = prefs .getBoolean ("bg_missed_alerts" ,false );
84105 boolean allDay = prefs .getBoolean ("missed_readings_all_day" ,true );
85106 boolean enableReraise = prefs .getBoolean ("bg_missed_alerts_enable_alerts_reraise" ,false );
107+ audioPath = Pref .getString ("bg_missed_alerts_sound" , null );
86108
87109 checkboxAllDay .setChecked (allDay );
88110 checkboxEnableAlert .setChecked (enableAlert );
@@ -95,9 +117,15 @@ protected void onCreate(Bundle savedInstanceState) {
95117 bgMissedMinutes .setText (prefs .getString ("bg_missed_minutes" , "30" ));
96118 bgMissedSnoozeMin .setText ("" + MissedReadingService .getOtherAlertSnoozeMinutes (prefs , "bg_missed_alerts" ));
97119 bgMissedReraiseSec .setText (prefs .getString ("bg_missed_alerts_reraise_sec" , "60" ));
120+ if (!audioPath .equals ("" )) {
121+ alertMp3File .setText (editAlert .shortPath (audioPath ));
122+ } else {
123+ alertMp3File .setText ("Silent" );
124+ }
98125
99126 addListenerOnButtons ();
100127 enableAllControls ();
128+ alertMp3File .setKeyListener (null );
101129 }
102130
103131 @ Override
@@ -225,6 +253,35 @@ public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinut
225253 viewTimeEnd .setOnClickListener (endTimeListener );
226254 timeInstructionsEnd .setOnClickListener (endTimeListener );
227255
256+ buttonalertMp3 .setOnClickListener (new View .OnClickListener () {
257+ public void onClick (View v ) {
258+ AlertDialog .Builder builder = new AlertDialog .Builder (mContext );
259+ Intent intent = new Intent (RingtoneManager .ACTION_RINGTONE_PICKER );
260+ intent .putExtra (RingtoneManager .EXTRA_RINGTONE_SHOW_SILENT , true );
261+ intent .putExtra (RingtoneManager .EXTRA_RINGTONE_SHOW_DEFAULT , true );
262+ intent .putExtra (RingtoneManager .EXTRA_RINGTONE_TYPE , RingtoneManager .TYPE_ALL );
263+ startActivityForResult (intent , 999 );
264+ AlertDialog dialog = builder .create ();
265+ dialog .show ();
266+ }
267+ }); //- See more at: http://blog.kerul.net/2011/12/pick-file-using-intentactiongetcontent.html#sthash.c8xtIr1Y.dpuf
268+
269+
270+ }
271+
272+ public void onActivityResult (int requestCode , int resultCode , Intent data ) {
273+ if (resultCode == RESULT_OK ) {
274+ Uri uri = data .getParcelableExtra (RingtoneManager .EXTRA_RINGTONE_PICKED_URI );
275+ if (uri != null ) {
276+ audioPath = uri .toString ();
277+ alertMp3File .setText (editAlert .shortPath (audioPath ));
278+ } else {
279+ audioPath = "" ;
280+ alertMp3File .setText ("Silent" );
281+ }
282+ UserError .Log .d (TAG , "Selected sound path: " + audioPath );
283+ Pref .setString ("bg_missed_alerts_sound" , audioPath ); // Update the sound file preference
284+ }
228285 }
229286
230287 public void setTimeRanges () {
0 commit comments