Skip to content

Commit

Permalink
Added readme, copyright and some code cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
macdems committed Oct 29, 2017
1 parent 366eac8 commit f05b1d7
Show file tree
Hide file tree
Showing 14 changed files with 980 additions and 72 deletions.
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Do Now Disturb

This tiny program adds a custom tile to turn on/off the ‘Do Not Disturb’
mode. Once the silent mode is turned on, user can select automatic
turn off time.

Hence this application is similar to the popular [Shush!](https://play.google.com/store/apps/details?id=com.publicobject.shush&hl=pl)
application, however it is designed for the new Android ‘Do Not Disturb’
mode.

## Credits

Inspiration and ideas:

* [Shush!](https://play.google.com/store/apps/details?id=com.publicobject.shush&hl=pl)

* [Do Not Disturb Tiles](https://github.com/bskup/donotdisturbtiles)

## License

Copyright © 2017 Maciej Dems <[email protected]>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

18 changes: 18 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Do Now Disturb
Copyright (C) 2017 Maciej Dems <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.macdems.disturbnow">

Expand Down
54 changes: 21 additions & 33 deletions app/src/main/java/com/macdems/disturbnow/DNDTileService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
/*
* Do Now Disturb
* Copyright (C) 2017 Maciej Dems <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package com.macdems.disturbnow;

import java.util.Calendar;
import java.util.Objects;

import android.app.NotificationManager;
Expand All @@ -15,8 +33,7 @@
import android.util.Log;
import android.widget.TimePicker;

public class DNDTileService extends TileService
implements TimeDialog.OnTimeSetListener {
public class DNDTileService extends TileService {

private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -111,39 +128,10 @@ private void setTileToMatchCurrentState() {
}

private void selectTime() {
Calendar time = Calendar.getInstance();
int hour = (time.get(Calendar.HOUR_OF_DAY) + 1) % 24;
int minute = 5 * ((time.get(Calendar.MINUTE) + 4) / 5);
if (minute >= 60) {
hour += 1;
minute -= 60;
}
TimeDialog timeDialog;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean theme = pref.getBoolean("dark_theme", false);
timeDialog = new TimeDialog(getApplicationContext(),
theme? R.style.AppTheme_Dialog_Dark : R.style.AppTheme_Dialog_Light,
this, hour, minute, true);
//timeDialog.setTitle(R.string.select_end_time);
TimeDialog timeDialog = new TimeDialog(getApplicationContext());
showDialog(timeDialog);
}

public void onTimeSet(TimePicker timePicker, int hour, int minute) {
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());

int currentHour = time.get(Calendar.HOUR_OF_DAY);
if (hour < currentHour || (hour == currentHour && minute <= time.get(Calendar.MINUTE))) {
time.add(Calendar.DATE, 1);
}
time.set(Calendar.HOUR_OF_DAY, hour);
time.set(Calendar.MINUTE, minute);
time.set(Calendar.SECOND, 0);

Context context = getApplicationContext();
DisturbAlarm.setupAlarm(time, context);
}

private void cancelAlarm() {
Context context = getApplicationContext();
DisturbAlarm.cancelAlarm(context);
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/macdems/disturbnow/DisturbAlarm.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* DoNowDisturb
* Copyright (C) 2017 Maciej Dems <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package com.macdems.disturbnow;

import android.app.AlarmManager;
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/macdems/disturbnow/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* DoNowDisturb
* Copyright (C) 2017 Maciej Dems <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package com.macdems.disturbnow;

import android.app.NotificationManager;
Expand Down
104 changes: 65 additions & 39 deletions app/src/main/java/com/macdems/disturbnow/TimeDialog.java
Original file line number Diff line number Diff line change
@@ -1,61 +1,78 @@
/*
* DoNowDisturb
* Copyright (C) 2017 Maciej Dems <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/

package com.macdems.disturbnow;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.TypedValue;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TimePicker;

import java.util.Calendar;

class TimeDialog extends AlertDialog implements DialogInterface.OnClickListener,
TimePicker.OnTimeChangedListener {
private static final String HOUR = "hour";
private static final String MINUTE = "minute";
private static final String IS_24_HOUR = "is24hour";

private final TimePicker mTimePicker;
private final OnTimeSetListener mTimeSetListener;

interface OnTimeSetListener {
void onTimeSet(TimePicker view, int hourOfDay, int minute);
}

public TimeDialog(Context context, OnTimeSetListener listener, int hourOfDay, int minute,
boolean is24HourView) {
this(context, 0, listener, hourOfDay, minute, is24HourView);
}

private static int resolveDialogTheme(Context context, int resId) {
if (resId == 0) {
final TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.timePickerDialogTheme, outValue, true);
return outValue.resourceId;
} else {
return resId;
}
private static int getDialogTheme(Context context) {
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
if (pref.getBoolean("dark_theme", false))
return R.style.AppTheme_Dialog_Dark;
else
return R.style.AppTheme_Dialog_Light;
}

TimeDialog(Context context, int themeResId, OnTimeSetListener listener,
int hourOfDay, int minute, boolean is24HourView) {
super(context, resolveDialogTheme(context, themeResId));

mTimeSetListener = listener;
TimeDialog(Context context) {
super(context, getDialogTheme(context));

final Context themeContext = getContext();
final LayoutInflater inflater = LayoutInflater.from(themeContext);
final View view = inflater.inflate(R.layout.time_dialog, null);
setView(view);
setButton(BUTTON_POSITIVE, themeContext.getString(android.R.string.ok), this);
setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.keep_silent), this);
//setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);

//setTitle(R.string.select_end_time);

Calendar time = Calendar.getInstance();
int hour = (time.get(Calendar.HOUR_OF_DAY) + 1) % 24;
int minute = 5 * ((time.get(Calendar.MINUTE) + 4) / 5);
if (minute >= 60) {
hour += 1;
minute -= 60;
}

mTimePicker = (TimePicker) view.findViewById(R.id.timePicker);
mTimePicker.setIs24HourView(is24HourView);
mTimePicker.setCurrentHour(hourOfDay);
mTimePicker.setCurrentMinute(minute);
mTimePicker.setIs24HourView(DateFormat.is24HourFormat(context));
mTimePicker.setHour(hour);
mTimePicker.setMinute(minute);
mTimePicker.setOnTimeChangedListener(this);
}

Expand All @@ -68,28 +85,37 @@ public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case BUTTON_POSITIVE:
if (mTimeSetListener != null) {
mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
mTimePicker.getCurrentMinute());
}
setAlarm();
break;
case BUTTON_NEGATIVE:
cancel();
break;
}
}

public void updateTime(int hourOfDay, int minuteOfHour) {
mTimePicker.setCurrentHour(hourOfDay);
mTimePicker.setCurrentMinute(minuteOfHour);
private void setAlarm() {
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());

int hour = mTimePicker.getHour();
int minute = mTimePicker.getMinute();
int currentHour = time.get(Calendar.HOUR_OF_DAY);
if (hour < currentHour || (hour == currentHour && minute <= time.get(Calendar.MINUTE))) {
time.add(Calendar.DATE, 1);
}
time.set(Calendar.HOUR_OF_DAY, hour);
time.set(Calendar.MINUTE, minute);
time.set(Calendar.SECOND, 0);

DisturbAlarm.setupAlarm(time, getContext());
}

@NonNull
@Override
public Bundle onSaveInstanceState() {
final Bundle state = super.onSaveInstanceState();
state.putInt(HOUR, mTimePicker.getCurrentHour());
state.putInt(MINUTE, mTimePicker.getCurrentMinute());
state.putInt(HOUR, mTimePicker.getHour());
state.putInt(MINUTE, mTimePicker.getMinute());
state.putBoolean(IS_24_HOUR, mTimePicker.is24HourView());
return state;
}
Expand All @@ -100,7 +126,7 @@ public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
final int hour = savedInstanceState.getInt(HOUR);
final int minute = savedInstanceState.getInt(MINUTE);
mTimePicker.setIs24HourView(savedInstanceState.getBoolean(IS_24_HOUR));
mTimePicker.setCurrentHour(hour);
mTimePicker.setCurrentMinute(minute);
mTimePicker.setHour(hour);
mTimePicker.setMinute(minute);
}
}
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/ic_disturbnow.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Do Now Disturb
Copyright (C) 2017 Maciej Dems <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="24"
android:viewportHeight="24"
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/res/layout/main.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Do Now Disturb
Copyright (C) 2017 Maciej Dems <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
Expand Down
Loading

0 comments on commit f05b1d7

Please sign in to comment.