| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,113 @@ |
| 1 |
+/* |
|
| 2 |
+ * Copyright (c) 2023 Samson Achiaga |
|
| 3 |
+ * |
|
| 4 |
+ * Licensed under the Apache License, Version 2.0 (the "License"); |
|
| 5 |
+ * you may not use this file except in compliance with the License. |
|
| 6 |
+ * You may obtain a copy of the License at |
|
| 7 |
+ * |
|
| 8 |
+ * http://www.apache.org/licenses/LICENSE-2.0 |
|
| 9 |
+ * |
|
| 10 |
+ * Unless required by applicable law or agreed to in writing, software |
|
| 11 |
+ * distributed under the License is distributed on an "AS IS" BASIS, |
|
| 12 |
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
| 13 |
+ * See the License for the specific language governing permissions and |
|
| 14 |
+ * limitations under the License. |
|
| 15 |
+ */ |
|
| 16 |
+ |
|
| 17 |
+package com.certified.audionote.utils |
|
| 18 |
+ |
|
| 19 |
+import android.app.Activity |
|
| 20 |
+import android.app.AlarmManager |
|
| 21 |
+import android.app.PendingIntent |
|
| 22 |
+import android.content.Context |
|
| 23 |
+import android.content.Intent |
|
| 24 |
+import android.os.Build |
|
| 25 |
+import android.util.Log |
|
| 26 |
+import com.certified.audionote.R |
|
| 27 |
+import com.certified.audionote.model.Note |
|
| 28 |
+import com.certified.audionote.ui.AlertReceiver |
|
| 29 |
+import java.text.SimpleDateFormat |
|
| 30 |
+import java.util.Calendar |
|
| 31 |
+import java.util.Date |
|
| 32 |
+import java.util.Locale |
|
| 33 |
+import java.util.concurrent.TimeUnit |
|
| 34 |
+ |
|
| 35 |
+val colors = listOf( |
|
| 36 |
+ -504764, -740056, -1544140, -2277816, -3246217, -4024195, |
|
| 37 |
+ -4224594, -7305542, -7551917, -7583749, -10712898, -10896368, -10965321, |
|
| 38 |
+ -11419154, -14654801 |
|
| 39 |
+) |
|
| 40 |
+ |
|
| 41 |
+fun filePath(activity: Activity) = activity.getExternalFilesDir("/")?.absolutePath
|
|
| 42 |
+ |
|
| 43 |
+fun currentDate(): Calendar = Calendar.getInstance() |
|
| 44 |
+ |
|
| 45 |
+fun formatDate(date: Long, context: Context): String {
|
|
| 46 |
+ |
|
| 47 |
+ val now = Date() |
|
| 48 |
+ val seconds = TimeUnit.MILLISECONDS.toSeconds(now.time - date) |
|
| 49 |
+ val minutes = TimeUnit.MILLISECONDS.toMinutes(now.time - date) |
|
| 50 |
+ val hours = TimeUnit.MILLISECONDS.toHours(now.time - date) |
|
| 51 |
+ val days = TimeUnit.MILLISECONDS.toDays(now.time - date) |
|
| 52 |
+ |
|
| 53 |
+ return when {
|
|
| 54 |
+ seconds < 60 -> context.getString(R.string.just_now) |
|
| 55 |
+ minutes == 1L -> context.getString(R.string.a_minute_ago) |
|
| 56 |
+ minutes in 2..59L -> "$minutes ${context.getString(R.string.minutes_ago)}"
|
|
| 57 |
+ hours == 1L -> context.getString(R.string.an_hour_ago) |
|
| 58 |
+ hours in 2..23 -> "$hours ${context.getString(R.string.hours_ago)}"
|
|
| 59 |
+ days == 1L -> context.getString(R.string.a_day_ago) |
|
| 60 |
+ else -> formatSimpleDate(date) |
|
| 61 |
+ } |
|
| 62 |
+} |
|
| 63 |
+ |
|
| 64 |
+fun formatReminderDate(date: Long): String = |
|
| 65 |
+ SimpleDateFormat("dd MMM, yyyy h:mm a", Locale.getDefault()).format(date)
|
|
| 66 |
+ |
|
| 67 |
+fun formatSimpleDate(date: Long): String = |
|
| 68 |
+ SimpleDateFormat("dd/MM/yy", Locale.getDefault()).format(date)
|
|
| 69 |
+ |
|
| 70 |
+fun formatDateOnly(date: Long): String = |
|
| 71 |
+ SimpleDateFormat("dd MMM, yyyy", Locale.getDefault()).format(date)
|
|
| 72 |
+ |
|
| 73 |
+fun formatTime(date: Long): String = SimpleDateFormat("h:mm a", Locale.getDefault()).format(date)
|
|
| 74 |
+ |
|
| 75 |
+fun startAlarm(context: Context, time: Long, note: Note) {
|
|
| 76 |
+ val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager |
|
| 77 |
+ val intent = Intent(context, AlertReceiver::class.java) |
|
| 78 |
+ intent.apply {
|
|
| 79 |
+ putExtra("noteId", note.id)
|
|
| 80 |
+ putExtra("noteTitle", note.title)
|
|
| 81 |
+ putExtra("noteDescription", note.description)
|
|
| 82 |
+ putExtra("noteColor", note.color)
|
|
| 83 |
+ putExtra("noteLastModificationDate", note.lastModificationDate)
|
|
| 84 |
+ putExtra("noteSize", note.size)
|
|
| 85 |
+ putExtra("noteAudioLength", note.audioLength)
|
|
| 86 |
+ putExtra("noteFilePath", note.filePath)
|
|
| 87 |
+ putExtra("noteStarted", note.started)
|
|
| 88 |
+ putExtra("noteReminder", note.reminder)
|
|
| 89 |
+ } |
|
| 90 |
+ val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
| 91 |
+ PendingIntent.getBroadcast(context, note.id, intent, PendingIntent.FLAG_IMMUTABLE) |
|
| 92 |
+ } else {
|
|
| 93 |
+ PendingIntent.getBroadcast(context, note.id, intent, 0) |
|
| 94 |
+ } |
|
| 95 |
+ alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent) |
|
| 96 |
+ Log.d("TAG", "startAlarm: Alarm started")
|
|
| 97 |
+} |
|
| 98 |
+ |
|
| 99 |
+fun cancelAlarm(context: Context, noteId: Int) {
|
|
| 100 |
+ val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager |
|
| 101 |
+ val intent = Intent(context, AlertReceiver::class.java) |
|
| 102 |
+ val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
| 103 |
+ PendingIntent.getBroadcast(context, noteId, intent, PendingIntent.FLAG_IMMUTABLE) |
|
| 104 |
+ } else {
|
|
| 105 |
+ PendingIntent.getBroadcast(context, noteId, intent, 0) |
|
| 106 |
+ } |
|
| 107 |
+ alarmManager.cancel(pendingIntent) |
|
| 108 |
+ Log.d("TAG", "cancelAlarm: Alarm canceled")
|
|
| 109 |
+} |
|
| 110 |
+ |
|
| 111 |
+fun roundOffDecimal(number: Double): String {
|
|
| 112 |
+ return "%.2f".format(number) |
|
| 113 |
+} |
|
| 0 | 114 |
\ No newline at end of file |