-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added readme, copyright and some code cleanups.
- Loading branch information
Showing
14 changed files
with
980 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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 | ||
|
@@ -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); | ||
|
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/macdems/disturbnow/DisturbAlarm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
19 changes: 19 additions & 0 deletions
19
app/src/main/java/com/macdems/disturbnow/MainActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
@@ -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; | ||
} | ||
|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
Oops, something went wrong.