Adds time picker for notifications
This commit is contained in:
parent
91d1249b56
commit
76c832ede9
|
@ -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 <http://www.gnu.org/licenses>. */
|
||||
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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -83,6 +83,48 @@
|
|||
android:layout_width="wrap_content"
|
||||
android:text="@string/set_notification_news"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<TextView
|
||||
android:text="@string/settings_title_hour"
|
||||
style="?attr/shapeBorder"
|
||||
android:paddingBottom="10dp"
|
||||
android:paddingTop="10dp"
|
||||
android:layout_marginBottom="10dp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
<LinearLayout
|
||||
style="?attr/shapeBorder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="center_vertical"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingBottom="10dp"
|
||||
android:orientation="horizontal">
|
||||
<TextView
|
||||
android:text="@string/settings_time_from"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
<Button
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:focusableInTouchMode="false"
|
||||
android:id="@+id/settings_time_from"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
<TextView
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:text="@string/settings_time_to"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
<Button
|
||||
android:layout_marginStart="10dp"
|
||||
android:layout_marginLeft="10dp"
|
||||
android:focusableInTouchMode="false"
|
||||
android:id="@+id/settings_time_to"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" />
|
||||
</LinearLayout>
|
||||
<!-- MORE OPTIONS SETTINGS -->
|
||||
<TextView
|
||||
android:text="@string/settings_title_more_options"
|
||||
|
@ -93,7 +135,6 @@
|
|||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
|
|
@ -247,6 +247,11 @@
|
|||
<string name="set_header_picture_overlay">Choisissez une image d\'entête</string>
|
||||
<string name="note_no_space">Vous avez atteint les 160 caractères autorisés !</string>
|
||||
<string name="username_no_space">Vous avez atteint les 30 caractères autorisés !</string>
|
||||
<string name="settings_title_hour">Plage horaire pour les notifications :</string>
|
||||
<string name="settings_time_from">Entre</string>
|
||||
<string name="settings_time_to">et</string>
|
||||
<string name="settings_time_greater">L\'horaire doit être plus grand que %1$s</string>
|
||||
<string name="settings_time_lower">L\'horaire doit être plus petit que %1$s</string>
|
||||
|
||||
<string name="embedded_browser">Utiliser le navigateur intégré</string>
|
||||
<string name="use_javascript">Activer Javascript</string>
|
||||
|
|
|
@ -251,6 +251,11 @@
|
|||
<string name="note_no_space">You have reached the 160 characters allowed!</string>
|
||||
<string name="username_no_space">You have reached the 30 characters allowed!</string>
|
||||
|
||||
<string name="settings_title_hour">Time slot for notifications:</string>
|
||||
<string name="settings_time_from">Between</string>
|
||||
<string name="settings_time_to">and</string>
|
||||
<string name="settings_time_greater">The time must be greater than %1$s</string>
|
||||
<string name="settings_time_lower">The time must be lower than %1$s</string>
|
||||
<string name="embedded_browser">Use the built-in browser</string>
|
||||
<string name="use_javascript">Enable Javascript</string>
|
||||
<string name="use_cookies">Allow third-party cookies</string>
|
||||
|
|
Loading…
Reference in New Issue