diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsNotificationsFragment.java index e4cc546fc..b7f21c46b 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/SettingsNotificationsFragment.java @@ -13,20 +13,34 @@ package fr.gouv.etalab.mastodon.fragments; * * You should have received a copy of the GNU General Public License along with Thomas Schneider; if not, * see . */ +import android.annotation.SuppressLint; +import android.app.TimePickerDialog; import android.content.Context; import android.content.SharedPreferences; +import android.net.ParseException; +import android.os.Build; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.widget.SwitchCompat; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; +import android.widget.DatePicker; +import android.widget.TimePicker; +import android.widget.Toast; + +import java.text.SimpleDateFormat; +import java.util.Date; import fr.gouv.etalab.mastodon.helper.Helper; import mastodon.etalab.gouv.fr.mastodon.R; +import static fr.gouv.etalab.mastodon.helper.Helper.compareDate; + /** * Created by Thomas on 29/04/2017. @@ -38,7 +52,7 @@ public class SettingsNotificationsFragment extends Fragment { private Context context; @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_settings_notifications, container, false); context = getContext(); @@ -55,6 +69,10 @@ public class SettingsNotificationsFragment extends Fragment { boolean notif_hometimeline = sharedpreferences.getBoolean(Helper.SET_NOTIF_HOMETIMELINE, true); + final String time_from = sharedpreferences.getString(Helper.SET_TIME_FROM, "07:00"); + final String time_to = sharedpreferences.getString(Helper.SET_TIME_TO, "22:00"); + + final CheckBox set_notif_follow = (CheckBox) rootView.findViewById(R.id.set_notif_follow); final CheckBox set_notif_follow_add = (CheckBox) rootView.findViewById(R.id.set_notif_follow_add); final CheckBox set_notif_follow_ask = (CheckBox) rootView.findViewById(R.id.set_notif_follow_ask); @@ -66,6 +84,61 @@ public class SettingsNotificationsFragment extends Fragment { final SwitchCompat switchCompatWIFI = (SwitchCompat) rootView.findViewById(R.id.set_wifi_only); final SwitchCompat switchCompatSilent = (SwitchCompat) rootView.findViewById(R.id.set_silence); + final Button settings_time_from = (Button) rootView.findViewById(R.id.settings_time_from); + final Button settings_time_to = (Button) rootView.findViewById(R.id.settings_time_to); + + settings_time_from.setText(time_from); + settings_time_to.setText(time_to); + + + settings_time_from.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String[] datetime = time_from.split(":"); + new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() { + @Override + public void onTimeSet(TimePicker view, int hourOfDay, int minute) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + String hours = (String.valueOf(hourOfDay).length() == 1) ? "0"+String.valueOf(hourOfDay):String.valueOf(hourOfDay); + String minutes = (String.valueOf(minute).length() == 1) ? "0"+String.valueOf(minute):String.valueOf(minute); + String newDate = hours + ":" + minutes; + if( compareDate(context, newDate, false) ) { + editor.putString(Helper.SET_TIME_FROM, newDate); + editor.apply(); + settings_time_from.setText(newDate); + }else { + String ateRef = sharedpreferences.getString(Helper.SET_TIME_TO, "22:00"); + Toast.makeText(context, context.getString(R.string.settings_time_lower, ateRef), Toast.LENGTH_LONG).show(); + } + } + }, Integer.valueOf(datetime[0]), Integer.valueOf(datetime[1]), true).show(); + } + }); + + settings_time_to.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + String[] datetime = time_to.split(":"); + new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() { + @Override + public void onTimeSet(TimePicker view, int hourOfDay, int minute) { + SharedPreferences.Editor editor = sharedpreferences.edit(); + String hours = (String.valueOf(hourOfDay).length() ==1) ? "0"+String.valueOf(hourOfDay):String.valueOf(hourOfDay); + String minutes = (String.valueOf(minute).length() ==1) ? "0"+String.valueOf(minute):String.valueOf(minute); + String newDate = hours + ":" + minutes; + if( compareDate(context, newDate, true) ) { + editor.putString(Helper.SET_TIME_TO, newDate); + editor.apply(); + settings_time_to.setText(newDate); + }else { + String ateRef = sharedpreferences.getString(Helper.SET_TIME_FROM, "07:00"); + Toast.makeText(context, context.getString(R.string.settings_time_greater, ateRef), Toast.LENGTH_LONG).show(); + } + } + }, Integer.valueOf(datetime[0]), Integer.valueOf(datetime[1]), true).show(); + } + }); + set_notif_follow.setChecked(notif_follow); set_notif_follow_add.setChecked(notif_add); set_notif_follow_ask.setChecked(notif_ask); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index e31f4e4fc..9cc000b7c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -17,6 +17,7 @@ package fr.gouv.etalab.mastodon.helper; +import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.DownloadManager; @@ -52,6 +53,7 @@ import android.text.TextPaint; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.util.DisplayMetrics; +import android.util.Log; import android.view.MenuItem; import android.view.SubMenu; import android.view.View; @@ -88,6 +90,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -163,6 +166,8 @@ public class Helper { public static final String SET_NOTIFICATIONS_PER_PAGE = "set_notifications_per_page"; public static final String SET_ATTACHMENT_ACTION = "set_attachment_action"; public static final String SET_THEME = "set_theme"; + public static final String SET_TIME_FROM = "set_time_from"; + public static final String SET_TIME_TO = "set_time_to"; public static final int ATTACHMENT_ALWAYS = 1; public static final int ATTACHMENT_WIFI = 2; public static final int ATTACHMENT_ASK = 3; @@ -1142,4 +1147,75 @@ public class Helper { locale = locale.split("_")[0]; return locale; } + + + /** + * Compare date with these in shared pref. + * @param context Context + * @param newDate String + * @param shouldBeGreater boolean if date passed as a parameter should be greater + * @return boolean + */ + public static boolean compareDate(Context context, String newDate, boolean shouldBeGreater){ + String dateRef; + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + if (shouldBeGreater) { + dateRef = sharedpreferences.getString(Helper.SET_TIME_FROM, "07:00"); + }else { + dateRef = sharedpreferences.getString(Helper.SET_TIME_TO, "22:00"); + } + try{ + Locale userLocale; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + userLocale = context.getResources().getConfiguration().getLocales().get(0); + } else { + //noinspection deprecation + userLocale = context.getResources().getConfiguration().locale; + } + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", userLocale); + Date newDateD = formatter.parse(newDate); + Date dateRefD = formatter.parse(dateRef); + if (shouldBeGreater) { + return (newDateD.after(dateRefD)); + }else { + return (newDateD.before(dateRefD)); + } + } catch (java.text.ParseException e) { + return false; + } + } + + + /** + * Tells if the the service can notify depending of the current hour and minutes + * @param context Context + * @return boolean + */ + public static boolean canNotify(Context context){ + final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String dateIni = sharedpreferences.getString(Helper.SET_TIME_FROM, "07:00"); + String dateEnd = sharedpreferences.getString(Helper.SET_TIME_TO, "22:00"); + Calendar now = Calendar.getInstance(); + int hour = now.get(Calendar.HOUR_OF_DAY); + int minute = now.get(Calendar.MINUTE); + String hourS = String.valueOf(hour).length() == 1?"0"+String.valueOf(hour):String.valueOf(hour); + String minuteS = String.valueOf(minute).length() == 1?"0"+String.valueOf(minute):String.valueOf(minute); + String currentDate = hourS + ":" + minuteS; + try{ + Locale userLocale; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + userLocale = context.getResources().getConfiguration().getLocales().get(0); + } else { + //noinspection deprecation + userLocale = context.getResources().getConfiguration().locale; + } + SimpleDateFormat formatter = new SimpleDateFormat("HH:mm", userLocale); + Date dateIniD = formatter.parse(dateIni); + Date dateEndD = formatter.parse(dateEnd); + Date currentDateD = formatter.parse(currentDate); + return currentDateD.before(dateEndD)&¤tDateD.after(dateIniD); + } catch (java.text.ParseException e) { + return true; + } + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java index 23f1c319a..113d2163e 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/HomeTimelineSyncJob.java @@ -55,6 +55,7 @@ import mastodon.etalab.gouv.fr.mastodon.R; import static fr.gouv.etalab.mastodon.helper.Helper.HOME_TIMELINE_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID; +import static fr.gouv.etalab.mastodon.helper.Helper.canNotify; import static fr.gouv.etalab.mastodon.helper.Helper.notify_user; @@ -97,7 +98,8 @@ public class HomeTimelineSyncJob extends Job implements OnRetrieveHomeTimelineSe */ private void callAsynchronousTask() { - + if( !canNotify(getContext())) + return; final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean notif_hometimeline = sharedpreferences.getBoolean(Helper.SET_NOTIF_HOMETIMELINE, true); //User disagree with home timeline refresh diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java index 2bb29c142..cc3b07f3d 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java @@ -54,6 +54,7 @@ import fr.gouv.etalab.mastodon.sqlite.Sqlite; import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION; import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT; import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID; +import static fr.gouv.etalab.mastodon.helper.Helper.canNotify; import static fr.gouv.etalab.mastodon.helper.Helper.notify_user; @@ -98,6 +99,8 @@ public class NotificationsSyncJob extends Job implements OnRetrieveNotifications * Task in background starts here. */ private void callAsynchronousTask() { + if( !canNotify(getContext())) + return; SQLiteDatabase db = Sqlite.getInstance(getContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); //If an Internet connection and user agrees with notification refresh final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); diff --git a/app/src/main/res/layout/fragment_settings_notifications.xml b/app/src/main/res/layout/fragment_settings_notifications.xml index 5a3dc3c3c..2adc1384b 100644 --- a/app/src/main/res/layout/fragment_settings_notifications.xml +++ b/app/src/main/res/layout/fragment_settings_notifications.xml @@ -83,6 +83,48 @@ android:layout_width="wrap_content" android:text="@string/set_notification_news" android:layout_height="wrap_content" /> + + + + +