Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

SQ-55/notifications settings #136

Merged
merged 10 commits into from Mar 16, 2017
8 changes: 7 additions & 1 deletion app/src/main/AndroidManifest.xml
Expand Up @@ -36,7 +36,13 @@
android:windowSoftInputMode="adjustResize"
android:launchMode="singleTop" />

<receiver android:name="net.squanchy.notification.NotificationAlarmReceiver"
<activity
android:name="net.squanchy.settings.SettingsActivity"
android:theme="@style/Theme.Squanchy.Settings"
android:label="@string/settings_label" />

<receiver
android:name="net.squanchy.notification.NotificationAlarmReceiver"
android:directBootAware="true">
<intent-filter>
<action android:name="android.intent.action.LOCKED_BOOT_COMPLETED" />
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/net/squanchy/navigation/Navigator.java
Expand Up @@ -7,6 +7,7 @@

import net.squanchy.eventdetails.EventDetailsActivity;
import net.squanchy.search.SearchActivity;
import net.squanchy.settings.SettingsActivity;

public class Navigator {

Expand All @@ -33,4 +34,8 @@ public void toSpeakerDetails(String... speakerIds) {
public void toSearch() {
context.startActivity(new Intent(context, SearchActivity.class));
}

public void toSettings() {
context.startActivity(new Intent(context, SettingsActivity.class));
}
}
Expand Up @@ -5,9 +5,12 @@
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import java.util.Comparator;

import net.squanchy.R;
import net.squanchy.schedule.domain.view.Event;

import org.joda.time.LocalDateTime;
Expand All @@ -17,9 +20,11 @@
public class NotificationService extends IntentService {

private static final int NOTIFICATION_INTERVAL_MINUTES = 10;
private static final boolean SHOW_NOTIFICATIONS_DEFAULT = true;

private NotificationCreator notificationCreator;
private Notifier notifier;
private SharedPreferences preferences;

public NotificationService() {
super(NotificationService.class.getSimpleName());
Expand All @@ -30,6 +35,7 @@ public void onCreate() {
super.onCreate();
notificationCreator = new NotificationCreator(this);
notifier = Notifier.from(this);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
}

@Override
Expand All @@ -40,19 +46,26 @@ protected void onHandleIntent(Intent intent) {
Observable<Event> sortedFavourites = favourites.sorted(byStartDate());

LocalDateTime now = new LocalDateTime();
sortedFavourites
.filter(event -> event.startTime().isAfter(now))
.filter(event -> isBeforeOrEqualTo(event.startTime(), notificationIntervalEnd))
.toList()
.map(notificationCreator::createFrom)
.subscribe(notifier::showNotifications);
if (shouldShowNotifications()) {
sortedFavourites
.filter(event -> event.startTime().isAfter(now))
.filter(event -> isBeforeOrEqualTo(event.startTime(), notificationIntervalEnd))
.toList()
.map(notificationCreator::createFrom)
.subscribe(notifier::showNotifications);
}

sortedFavourites
.filter(event -> event.startTime().isAfter(notificationIntervalEnd))
.take(1)
.subscribe(this::scheduleNextAlarm);
}

private boolean shouldShowNotifications() {
String notificationPreferenceKey = getString(R.string.about_to_start_notification_preference_key);
return preferences.getBoolean(notificationPreferenceKey, SHOW_NOTIFICATIONS_DEFAULT);
}

private boolean isBeforeOrEqualTo(LocalDateTime start, LocalDateTime notificationIntervalEnd) {
return start.isBefore(notificationIntervalEnd) || start.isEqual(notificationIntervalEnd);
}
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/net/squanchy/schedule/SchedulePageView.java
Expand Up @@ -85,13 +85,18 @@ private static Activity unwrapToActivityContext(Context context) {
private void setupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle(R.string.activity_schedule);
toolbar.inflateMenu(R.menu.search_icon);
toolbar.inflateMenu(R.menu.schedule_menu);
toolbar.setOnMenuItemClickListener(item -> {
if (item.getItemId() == R.id.action_search) {
navigate.toSearch();
return true;
switch (item.getItemId()) {
case R.id.action_search:
navigate.toSearch();
return true;
case R.id.action_settings:
navigate.toSettings();
return true;
default:
return false;
}
return false;
});
}

Expand Down
39 changes: 39 additions & 0 deletions app/src/main/java/net/squanchy/settings/SettingsActivity.java
@@ -0,0 +1,39 @@
package net.squanchy.settings;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import net.squanchy.R;
import net.squanchy.fonts.TypefaceStyleableActivity;

public class SettingsActivity extends TypefaceStyleableActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_settings);

setupToolbar();

getFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new SettingsFragment())
.commit();
}

private void setupToolbar() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/net/squanchy/settings/SettingsFragment.java
@@ -0,0 +1,16 @@
package net.squanchy.settings;

import android.os.Bundle;
import android.preference.PreferenceFragment;

import net.squanchy.R;

public class SettingsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.settings_preferences);
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/drawable/ic_settings.xml
@@ -0,0 +1,15 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillColor="?colorControlNormal"
android:pathData="M20.574 13.893c-.07-.337-.434-2.013-.673-2.124-.093-.043-.475-.067-.93-.08a7.966 7.966 0 0 0-.3-2.95A9.974 9.974 0 0 1 8.26 4.77c-.981 2.391-2.851 4.321-5.211 5.371-.03.28-.05.57-.05.86 0 4.41 3.59 8 8 8 .264 0 .526-.013.783-.038.056.439.574 1.598.723 1.925-.491.074-.994.113-1.506.113-5.52 0-10-4.48-10-10S5.48 1 11 1s10 4.48 10 10a9.99 9.99 0 0 1-.426 2.893zM14 10.75a1.25 1.25 0 1 1 0 2.5 1.25 1.25 0 0 1 0-2.5zm-6 0a1.25 1.25 0 1 1 0 2.5 1.25 1.25 0 0 1 0-2.5z" />

<path
android:fillColor="?colorControlNormal"
android:pathData="M21.715 18.49c.02-.16.035-.32.035-.49 0-.17-.015-.33-.035-.49l1.055-.825a.252.252 0 0 0 .06-.32l-1-1.73c-.06-.11-.195-.15-.305-.11l-1.245.5c-.26-.2-.54-.365-.845-.49l-.19-1.325A.244.244 0 0 0 19 13h-2c-.125 0-.23.09-.245.21l-.19 1.325a3.841 3.841 0 0 0-.845.49l-1.245-.5a.244.244 0 0 0-.305.11l-1 1.73a.247.247 0 0 0 .06.32l1.055.825c-.02.16-.035.325-.035.49 0 .165.015.33.035.49l-1.055.825a.252.252 0 0 0-.06.32l1 1.73c.06.11.195.15.305.11l1.245-.5c.26.2.54.365.845.49l.19 1.325c.015.12.12.21.245.21h2c.125 0 .23-.09.245-.21l.19-1.325c.305-.125.585-.295.845-.49l1.245.5c.115.045.245 0 .305-.11l1-1.73a.252.252 0 0 0-.06-.32l-1.055-.825zM18 19.75c-.965 0-1.75-.785-1.75-1.75s.785-1.75 1.75-1.75 1.75.785 1.75 1.75-.785 1.75-1.75 1.75z" />

</vector>
29 changes: 29 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="net.squanchy.settings.settings.SettingsActivity">

<android.support.design.widget.AppBarLayout
style="@style/Squanchy.Appbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<FrameLayout
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could've put the <fragment ... /> here directly I suppose, but whatevs

android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
17 changes: 17 additions & 0 deletions app/src/main/res/menu/schedule_menu.xml
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/action_settings"
android:title="@string/settings_label"
android:icon="@drawable/ic_settings"
app:showAsAction="ifRoom" />

<item
android:id="@+id/action_search"
android:title="@string/search_label"
android:icon="@drawable/ic_search"
app:showAsAction="ifRoom" />

</menu>
6 changes: 6 additions & 0 deletions app/src/main/res/values/preference_keys.xml
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="about_to_start_notification_preference_key">about_to_start_notification__preference_key</string>

</resources>
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -39,4 +39,10 @@
<string name="tweet_date_today">Today</string>
<string name="tweet_date_yesterday">Yesterday</string>

<string name="settings_label">Settings</string>
<string name="settings_notifications_category">Notifications</string>
<string name="settings_notifications_about_to_start_title">Upcoming events notification</string>
<string name="settings_notifications_about_to_start_summary_on">Show notifications for favourite events starting soon</string>
<string name="settings_notifications_about_to_start_summary_off">Do not show notifications for favourite events starting soon</string>

</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/themes.xml
Expand Up @@ -46,4 +46,6 @@

<style name="Theme.Squanchy.Search" parent="Theme.Squanchy.PrimaryStatusBar" />

<style name="Theme.Squanchy.Settings" parent="Theme.Squanchy.PrimaryStatusBar" />

</resources>
15 changes: 15 additions & 0 deletions app/src/main/res/xml/settings_preferences.xml
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="@string/settings_notifications_category">

<SwitchPreference
android:defaultValue="true"
android:key="@string/about_to_start_notification_preference_key"
android:title="@string/settings_notifications_about_to_start_title"
android:summaryOn="@string/settings_notifications_about_to_start_summary_on"
android:summaryOff="@string/settings_notifications_about_to_start_summary_off" />

</PreferenceCategory>

</PreferenceScreen>