Skip to content

Commit

Permalink
BroadcastReceiver
Browse files Browse the repository at this point in the history
splits alarm and bedtime actions into separate broadcast receivers, and restrict actions specific to both; the `BedtimeBroadcastReceiver` is exported with the `READ_CALCULATOR` permission.
  • Loading branch information
forrestguice committed Jul 19, 2024
1 parent 12447f7 commit 0adae7f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 8 deletions.
14 changes: 10 additions & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,6 @@
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
<!--<action android:name="android.intent.action.TIME_SET" />-->
<action android:name="suntimeswidget.alarm.start_bedtime" />
<action android:name="suntimeswidget.alarm.pause_bedtime" />
<action android:name="suntimeswidget.alarm.resume_bedtime" />
<action android:name="suntimeswidget.alarm.dismiss_bedtime" />
</intent-filter>
<intent-filter>
<action android:name="suntimeswidget.alarm.show" />
Expand All @@ -402,6 +398,16 @@
<data android:scheme="content" />
</intent-filter>
</receiver>
<receiver android:name=".alarmclock.ui.bedtime.BedtimeBroadcastReceiver"
android:exported="true"
android:permission="suntimes.permission.READ_CALCULATOR">
<intent-filter>
<action android:name="suntimeswidget.alarm.start_bedtime" />
<action android:name="suntimeswidget.alarm.pause_bedtime" />
<action android:name="suntimeswidget.alarm.resume_bedtime" />
<action android:name="suntimeswidget.alarm.dismiss_bedtime" />
</intent-filter>
</receiver>

<!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!-- Alarm Tile -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,13 @@ public class AlarmNotifications extends BroadcastReceiver

public static final int NOTIFICATION_BEDTIME_ACTIVE_ID = -1000;

public static final String[] ALARM_ACTIONS = new String[] {
ACTION_SHOW, ACTION_SILENT, ACTION_DISMISS, ACTION_SNOOZE,
ACTION_SCHEDULE, ACTION_RESCHEDULE, ACTION_RESCHEDULE1,
ACTION_DISABLE, ACTION_TIMEOUT, ACTION_DELETE,
ACTION_UPDATE_UI, ACTION_LOCATION_CHANGED,
};

private static SuntimesUtils utils = new SuntimesUtils();

/**
Expand All @@ -143,10 +150,22 @@ public void onReceive(final Context context, Intent intent)
Uri data = intent.getData();
Log.d(TAG, "onReceive: " + action + ", " + data);
if (action != null) {
context.startService(NotificationService.getNotificationIntent(context, action, data, intent.getExtras()));
if (actionIsPermitted(action)) {
context.startService(NotificationService.getNotificationIntent(context, action, data, intent.getExtras()));
} else Log.e(TAG, "onReceive: `" + action + "` is not on the list of permitted actions! Ignoring...");
} else Log.w(TAG, "onReceive: null action!");
}

protected boolean actionIsPermitted(String action)
{
for (String a : ALARM_ACTIONS) {
if (a.equals(action)) {
return true;
}
}
return false;
}

/**
*/
public static void showTimeUntilToast(Context context, View view, @NonNull AlarmClockItem item) {
Expand Down Expand Up @@ -1826,7 +1845,7 @@ NotificationService getService() {
}
}

private static Intent getNotificationIntent(Context context, String action, Uri data, @Nullable Bundle extras)
public static Intent getNotificationIntent(Context context, String action, Uri data, @Nullable Bundle extras)
{
Intent intent = new Intent(context, NotificationService.class);
intent.setAction(action);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
Copyright (C) 2024 Forrest Guice
This file is part of SuntimesWidget.
SuntimesWidget 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.
SuntimesWidget 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 SuntimesWidget. If not, see <http://www.gnu.org/licenses/>.
*/

package com.forrestguice.suntimeswidget.alarmclock.ui.bedtime;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications;

import static com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications.ACTION_BEDTIME;
import static com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications.ACTION_BEDTIME_DISMISS;
import static com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications.ACTION_BEDTIME_PAUSE;
import static com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications.ACTION_BEDTIME_RESUME;

public class BedtimeBroadcastReceiver extends BroadcastReceiver
{
public static final String TAG = "BedtimeReceiver";

public static final String[] BEDTIME_ACTIONS = new String[] {
ACTION_BEDTIME, ACTION_BEDTIME_PAUSE, ACTION_BEDTIME_RESUME, ACTION_BEDTIME_DISMISS
};

@Override
public void onReceive(final Context context, Intent intent)
{
final String action = intent.getAction();
Uri data = intent.getData();
Log.d(TAG, "onReceive: " + action + ", " + data);
if (action != null) {
if (actionIsPermitted(action)) {
context.startService(AlarmNotifications.NotificationService.getNotificationIntent(context, action, data, intent.getExtras()));
} else Log.e(TAG, "onReceive: `" + action + "` is not on the list of permitted actions! Ignoring...");
} else Log.w(TAG, "onReceive: null action!");
}

protected boolean actionIsPermitted(String action)
{
for (String a : BEDTIME_ACTIONS) {
if (a.equals(action)) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import com.forrestguice.suntimeswidget.alarmclock.AlarmNotifications;
import com.forrestguice.suntimeswidget.alarmclock.ui.bedtime.BedtimeActivity;
import com.forrestguice.suntimeswidget.alarmclock.ui.bedtime.BedtimeBroadcastReceiver;
import com.forrestguice.suntimeswidget.calculator.SuntimesClockData;
import com.forrestguice.suntimeswidget.calculator.SuntimesEquinoxSolsticeData;
import com.forrestguice.suntimeswidget.calculator.SuntimesMoonData;
Expand Down Expand Up @@ -900,13 +901,13 @@ public ContentValues toContentValues()
{
case TRIGGER_BEDTIME:
launchType = LaunchType.BROADCAST;
launchString = AlarmNotifications.class.getName();
launchString = BedtimeBroadcastReceiver.class.getName();
launchAction = AlarmNotifications.ACTION_BEDTIME;
break;

case DISMISS_BEDTIME:
launchType = LaunchType.BROADCAST;
launchString = AlarmNotifications.class.getName();
launchString = BedtimeBroadcastReceiver.class.getName();
launchAction = AlarmNotifications.ACTION_BEDTIME_DISMISS;
break;

Expand Down

0 comments on commit 0adae7f

Please sign in to comment.