Skip to content

Commit

Permalink
improved notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
salman660 committed Sep 16, 2023
1 parent c7be78e commit adcbf8b
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


<application
android:allowBackup="true"
Expand Down
89 changes: 88 additions & 1 deletion app/src/main/java/com/apptic/namaztimings/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class MainActivity extends AppCompatActivity {
private static final String NOTIFICATION_CHANNEL_ID = "NamazTimingsChannel";
private static final int DND_REQUEST_CODE = 7333; // Use any unique value
private static final int NOTIFICATION_PERMISSION_REQUEST_CODE = 1001;
private static final int PRAYER_NOTIFICATION_ID = 2;
private static final int PRAYER_NOTIFICATION_ID = 400;

private boolean isAnimationRunning = true; // Initially set to true to start animation

Expand Down Expand Up @@ -90,6 +90,13 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Calculate prayer times for a whole month (you can implement this part)
List<Date> prayerTimesForTheMonth = calculatePrayerTimesForMonth();

// Call the method to schedule prayer time notifications
schedulePrayerTimeNotifications(prayerTimesForTheMonth);


settingsicon = findViewById(R.id.settingsicon);

// Add OnClickListener to the settingsicon ImageView
Expand Down Expand Up @@ -218,6 +225,86 @@ public void run() {
}


private List<Date> calculatePrayerTimesForMonth() {
List<Date> prayerTimesForMonth = new ArrayList<>();

// Get the saved time zone from SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
String savedTimeZone = sharedPreferences.getString("SelectedTimeZone", "");

// Get the saved latitude and longitude from SharedPreferences
String savedLatitude = sharedPreferences.getString("SelectedLatitude", "");
String savedLongitude = sharedPreferences.getString("SelectedLongitude", "");

// Set the time zone for the formatter
SimpleDateFormat formatter = new SimpleDateFormat("hh:mm a");
if (!savedTimeZone.isEmpty()) {
formatter.setTimeZone(TimeZone.getTimeZone(savedTimeZone));
} else {
// Set a default time zone (e.g., "Asia/Karachi") if the saved time zone is empty
formatter.setTimeZone(TimeZone.getTimeZone("Asia/Karachi"));
}

double latitude, longitude;

if (!savedLatitude.isEmpty() && !savedLongitude.isEmpty()) {
// Use the saved coordinates if available
latitude = Double.parseDouble(savedLatitude);
longitude = Double.parseDouble(savedLongitude);
} else {
// Use dummy coordinates if saved coordinates are not available
latitude = 0.0; // Replace with your dummy latitude
longitude = 0.0; // Replace with your dummy longitude
}

CalculationMethod calculationMethod = CalculationMethod.KARACHI;

// Create a calendar instance for the current date
Calendar calendar = Calendar.getInstance();

// Loop through each day of the month
for (int day = 1; day <= calendar.getActualMaximum(Calendar.DAY_OF_MONTH); day++) {
// Set the date to the current day
calendar.set(Calendar.DAY_OF_MONTH, day);

// Calculate prayer times for the current day
DateComponents dateComponents = DateComponents.from(calendar.getTime());
PrayerTimes prayerTimes = new PrayerTimes(new Coordinates(latitude, longitude), dateComponents, calculationMethod.getParameters());

// Add the prayer times for the current day to the list
prayerTimesForMonth.add(prayerTimes.fajr);
prayerTimesForMonth.add(prayerTimes.sunrise);
prayerTimesForMonth.add(prayerTimes.dhuhr);
prayerTimesForMonth.add(prayerTimes.asr);
prayerTimesForMonth.add(prayerTimes.maghrib);
prayerTimesForMonth.add(prayerTimes.isha);
}

return prayerTimesForMonth;
}

private void schedulePrayerTimeNotifications(List<Date> prayerTimesForMonth) {
// Get the AlarmManager
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

// Create a BroadcastReceiver to handle the prayer time notifications
Intent intent = new Intent(this, PrayerNotificationReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

// Schedule notifications for each prayer time
for (int i = 0; i < prayerTimesForMonth.size(); i++) {
Date prayerTime = prayerTimesForMonth.get(i);

// Calculate the time difference between the current prayer time and now
long timeDifferenceMillis = prayerTime.getTime() - System.currentTimeMillis();

if (timeDifferenceMillis > 0) {
// Schedule the notification for this prayer time
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + timeDifferenceMillis, pendingIntent);
}
}
}

private Date getNextPrayerTime() {
Coordinates coordinates = new Coordinates(29.3544, 71.6911); // Replace with your coordinates
DateComponents dateComponents = DateComponents.from(new Date());
Expand Down

0 comments on commit adcbf8b

Please sign in to comment.