mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-01 13:57:10 +01:00
Add PullNotificationAlarmReceiver, another implementation of pull notifications. Fix a bug in NotificationPreferenceFragment.
This commit is contained in:
parent
8422858ec9
commit
6d8dcc8fe7
@ -14,6 +14,8 @@
|
||||
|
||||
<uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
|
||||
<application
|
||||
android:name=".Infinity"
|
||||
android:allowBackup="true"
|
||||
@ -345,6 +347,8 @@
|
||||
android:name=".Service.SubmitPostService"
|
||||
android:enabled="true"
|
||||
android:exported="false" />
|
||||
|
||||
<receiver android:process=":remote" android:name=".PullNotificationAlarmReceiver" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -335,6 +335,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
||||
}
|
||||
|
||||
if (enableNotification) {
|
||||
//PullNotificationAlarmReceiver.setNotificationAlarm(this, notificationInterval);
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build();
|
||||
@ -348,6 +349,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
||||
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
|
||||
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
|
||||
} else {
|
||||
//PullNotificationAlarmReceiver.cancelAlarm(this);
|
||||
workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
|
||||
}
|
||||
|
||||
@ -361,6 +363,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
||||
mKarma = account.getKarma();
|
||||
|
||||
if (enableNotification) {
|
||||
//PullNotificationAlarmReceiver.setNotificationAlarm(this, notificationInterval);
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build();
|
||||
@ -374,6 +377,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
||||
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
|
||||
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
|
||||
} else {
|
||||
//PullNotificationAlarmReceiver.cancelAlarm(this);
|
||||
workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
|
||||
}
|
||||
|
||||
@ -391,6 +395,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
||||
}
|
||||
|
||||
if (enableNotification) {
|
||||
//PullNotificationAlarmReceiver.setNotificationAlarm(this, notificationInterval);
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build();
|
||||
@ -404,6 +409,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
|
||||
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
|
||||
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
|
||||
} else {
|
||||
//PullNotificationAlarmReceiver.cancelAlarm(this);
|
||||
workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
|
||||
}
|
||||
|
||||
|
@ -197,4 +197,6 @@ public interface AppComponent {
|
||||
void inject(DownloadLocationPreferenceFragment downloadLocationPreferenceFragment);
|
||||
|
||||
void inject(SubmitCrosspostActivity submitCrosspostActivity);
|
||||
|
||||
void inject(PullNotificationAlarmReceiver pullNotificationAlarmReceiver);
|
||||
}
|
||||
|
@ -0,0 +1,106 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
import androidx.work.Constraints;
|
||||
import androidx.work.ExistingWorkPolicy;
|
||||
import androidx.work.NetworkType;
|
||||
import androidx.work.OneTimeWorkRequest;
|
||||
import androidx.work.WorkManager;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
|
||||
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
|
||||
import retrofit2.Retrofit;
|
||||
|
||||
public class PullNotificationAlarmReceiver extends BroadcastReceiver {
|
||||
|
||||
@Inject
|
||||
@Named("oauth_without_authenticator")
|
||||
Retrofit mOauthWithoutAuthenticatorRetrofit;
|
||||
@Inject
|
||||
@Named("no_oauth")
|
||||
Retrofit mRetrofit;
|
||||
@Inject
|
||||
RedditDataRoomDatabase mRedditDataRoomDatabase;
|
||||
@Inject
|
||||
@Named("default")
|
||||
SharedPreferences mSharedPreferences;
|
||||
@Inject
|
||||
CustomThemeWrapper mCustomThemeWrapper;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent receivedIntent) {
|
||||
((Infinity) context.getApplicationContext()).getAppComponent().inject(this);
|
||||
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
PowerManager.WakeLock wl = null;
|
||||
if (pm != null) {
|
||||
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ml.docilealligator.infinityforreddit:notification");
|
||||
wl.acquire(2*60*1000L /*2 minutes*/);
|
||||
}
|
||||
|
||||
Log.i("asasdfsdaf", "time: " + System.currentTimeMillis());
|
||||
|
||||
NotificationManagerCompat testNotificationManager = NotificationUtils.getNotificationManager(context);
|
||||
|
||||
NotificationCompat.Builder testSummaryBuilder = NotificationUtils.buildSummaryNotification(context,
|
||||
testNotificationManager, "test",
|
||||
Long.toString(System.currentTimeMillis()),
|
||||
NotificationUtils.CHANNEL_ID_NEW_MESSAGES, NotificationUtils.CHANNEL_NEW_MESSAGES,
|
||||
NotificationUtils.getAccountGroupName("test"), Color.BLACK);
|
||||
testNotificationManager.notify(NotificationUtils.getSummaryIdUnreadMessage(12), testSummaryBuilder.build());
|
||||
|
||||
WorkManager workManager = WorkManager.getInstance(context);
|
||||
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build();
|
||||
|
||||
OneTimeWorkRequest pullNotificationRequest =
|
||||
new OneTimeWorkRequest.Builder(PullNotificationWorker.class)
|
||||
.setConstraints(constraints)
|
||||
.build();
|
||||
|
||||
workManager.enqueueUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
|
||||
ExistingWorkPolicy.KEEP, pullNotificationRequest);
|
||||
|
||||
if (wl != null) {
|
||||
wl.release();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setNotificationAlarm(Context context, long notificationInterval) {
|
||||
AlarmManager am =(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
if (am != null) {
|
||||
Intent intent = new Intent(context, PullNotificationAlarmReceiver.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NotificationUtils.PULL_NOTIFICATION_ALARM_RECEIVER_REQUEST_CODE, intent, 0);
|
||||
if ((notificationInterval == 15 || notificationInterval == 30)) {
|
||||
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 60 * 1000, 1000 * 60 * notificationInterval, pendingIntent); // Millisec * Second * Minute
|
||||
} else {
|
||||
am.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, 60 * 1000, 1000 * 60 * 60 * notificationInterval, pendingIntent); // Millisec * Second * Minute
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cancelAlarm(Context context) {
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
if (alarmManager != null) {
|
||||
Intent intent = new Intent(context, AlarmManager.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NotificationUtils.PULL_NOTIFICATION_ALARM_RECEIVER_REQUEST_CODE, intent, 0);
|
||||
alarmManager.cancel(pendingIntent);
|
||||
}
|
||||
}
|
||||
}
|
@ -4,7 +4,9 @@ import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Color;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
@ -22,6 +24,7 @@ import java.util.Calendar;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
@ -36,6 +39,7 @@ import ml.docilealligator.infinityforreddit.Message.Message;
|
||||
import ml.docilealligator.infinityforreddit.Message.ParseMessage;
|
||||
import ml.docilealligator.infinityforreddit.Utils.APIUtils;
|
||||
import ml.docilealligator.infinityforreddit.Utils.JSONUtils;
|
||||
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Response;
|
||||
@ -67,6 +71,17 @@ public class PullNotificationWorker extends Worker {
|
||||
@NonNull
|
||||
@Override
|
||||
public Result doWork() {
|
||||
Log.i("asasdfsdaf", "time: " + System.currentTimeMillis());
|
||||
|
||||
NotificationManagerCompat testNotificationManager = NotificationUtils.getNotificationManager(context);
|
||||
|
||||
NotificationCompat.Builder testSummaryBuilder = NotificationUtils.buildSummaryNotification(context,
|
||||
testNotificationManager, "test",
|
||||
context.getString(R.string.notification_new_messages, 12),
|
||||
NotificationUtils.CHANNEL_ID_NEW_MESSAGES, NotificationUtils.CHANNEL_NEW_MESSAGES,
|
||||
NotificationUtils.getAccountGroupName("test"), Color.BLACK);
|
||||
testNotificationManager.notify(new Random().nextInt(10000), testSummaryBuilder.build());
|
||||
|
||||
try {
|
||||
List<Account> accounts = mRedditDataRoomDatabase.accountDao().getAllAccounts();
|
||||
int color = mCustomThemeWrapper.getColorPrimaryLightTheme();
|
||||
@ -202,7 +217,7 @@ public class PullNotificationWorker extends Worker {
|
||||
return Result.retry();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
return Result.failure();
|
||||
return Result.retry();
|
||||
}
|
||||
return Result.success();
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ import ml.docilealligator.infinityforreddit.API.DownloadFile;
|
||||
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
|
||||
import ml.docilealligator.infinityforreddit.Event.DownloadMediaEvent;
|
||||
import ml.docilealligator.infinityforreddit.Infinity;
|
||||
import ml.docilealligator.infinityforreddit.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.R;
|
||||
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
||||
import okhttp3.ResponseBody;
|
||||
|
@ -44,7 +44,7 @@ import ml.docilealligator.infinityforreddit.API.DownloadFile;
|
||||
import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper;
|
||||
import ml.docilealligator.infinityforreddit.Event.DownloadRedditVideoEvent;
|
||||
import ml.docilealligator.infinityforreddit.Infinity;
|
||||
import ml.docilealligator.infinityforreddit.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.R;
|
||||
import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils;
|
||||
import okhttp3.ResponseBody;
|
||||
|
@ -40,7 +40,7 @@ import ml.docilealligator.infinityforreddit.Event.SubmitTextOrLinkPostEvent;
|
||||
import ml.docilealligator.infinityforreddit.Event.SubmitVideoOrGifPostEvent;
|
||||
import ml.docilealligator.infinityforreddit.Flair;
|
||||
import ml.docilealligator.infinityforreddit.Infinity;
|
||||
import ml.docilealligator.infinityforreddit.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.Utils.NotificationUtils;
|
||||
import ml.docilealligator.infinityforreddit.Post.Post;
|
||||
import ml.docilealligator.infinityforreddit.Post.SubmitPost;
|
||||
import ml.docilealligator.infinityforreddit.R;
|
||||
|
@ -87,18 +87,21 @@ public class DownloadLocationPreferenceFragment extends PreferenceFragmentCompat
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
if (resultCode == Activity.RESULT_OK && data != null) {
|
||||
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
|
||||
if (requestCode == IMAGE_DOWNLOAD_LOCATION_REQUEST_CODE) {
|
||||
activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
sharedPreferences.edit().putString(SharedPreferencesUtils.IMAGE_DOWNLOAD_LOCATION, data.getDataString()).apply();
|
||||
if (imageDownloadLocationPreference != null) {
|
||||
imageDownloadLocationPreference.setSummary(data.getDataString());
|
||||
}
|
||||
} else if (requestCode == GIF_DOWNLOAD_LOCATION_REQUEST_CODE) {
|
||||
activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
sharedPreferences.edit().putString(SharedPreferencesUtils.GIF_DOWNLOAD_LOCATION, data.getDataString()).apply();
|
||||
if (gifDownloadLocationPreference != null) {
|
||||
gifDownloadLocationPreference.setSummary(data.getDataString());
|
||||
}
|
||||
} else if (requestCode == VIDEO_DOWNLOAD_LOCATION_REQUEST_CODE) {
|
||||
activity.getContentResolver().takePersistableUriPermission(data.getData(), Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
sharedPreferences.edit().putString(SharedPreferencesUtils.VIDEO_DOWNLOAD_LOCATION, data.getDataString()).apply();
|
||||
if (videoDownloadLocationPreference != null) {
|
||||
videoDownloadLocationPreference.setSummary(data.getDataString());
|
||||
|
@ -53,7 +53,6 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
|
||||
|
||||
enableNotification = sharedPreferences.getBoolean(SharedPreferencesUtils.ENABLE_NOTIFICATION_KEY, true);
|
||||
notificationInterval = Long.parseLong(sharedPreferences.getString(SharedPreferencesUtils.NOTIFICATION_INTERVAL_KEY, "1"));
|
||||
TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS;
|
||||
|
||||
if (enableNotification) {
|
||||
if (notificationIntervalListPreference != null) {
|
||||
@ -69,6 +68,8 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
|
||||
}
|
||||
|
||||
if (enableNotification) {
|
||||
TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS;
|
||||
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build();
|
||||
@ -94,6 +95,8 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
|
||||
notificationInterval = Long.parseLong((String) newValue);
|
||||
|
||||
if (enableNotification) {
|
||||
TimeUnit timeUnit = (notificationInterval == 15 || notificationInterval == 30) ? TimeUnit.MINUTES : TimeUnit.HOURS;
|
||||
|
||||
Constraints constraints = new Constraints.Builder()
|
||||
.setRequiredNetworkType(NetworkType.CONNECTED)
|
||||
.build();
|
||||
|
@ -1,4 +1,4 @@
|
||||
package ml.docilealligator.infinityforreddit;
|
||||
package ml.docilealligator.infinityforreddit.Utils;
|
||||
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
@ -7,10 +7,12 @@ import android.content.Context;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
import androidx.core.app.NotificationManagerCompat;
|
||||
|
||||
import ml.docilealligator.infinityforreddit.R;
|
||||
|
||||
public class NotificationUtils {
|
||||
public static final String CHANNEL_SUBMIT_POST = "Submit Post";
|
||||
static final String CHANNEL_ID_NEW_MESSAGES = "new_messages";
|
||||
static final String CHANNEL_NEW_MESSAGES = "New Messages";
|
||||
public static final String CHANNEL_ID_NEW_MESSAGES = "new_messages";
|
||||
public static final String CHANNEL_NEW_MESSAGES = "New Messages";
|
||||
public static final String CHANNEL_ID_DOWNLOAD_REDDIT_VIDEO = "download_reddit_video";
|
||||
public static final String CHANNEL_DOWNLOAD_REDDIT_VIDEO = "Download Reddit Video";
|
||||
public static final String CHANNEL_ID_DOWNLOAD_VIDEO = "download_video";
|
||||
@ -24,16 +26,17 @@ public class NotificationUtils {
|
||||
public static final int DOWNLOAD_VIDEO_NOTIFICATION_ID = 30000;
|
||||
public static final int DOWNLOAD_IMAGE_NOTIFICATION_ID = 40000;
|
||||
public static final int DOWNLOAD_GIF_NOTIFICATION_ID = 50000;
|
||||
public static final int PULL_NOTIFICATION_ALARM_RECEIVER_REQUEST_CODE = 12;
|
||||
|
||||
private static final int SUMMARY_BASE_ID_UNREAD_MESSAGE = 0;
|
||||
private static final int NOTIFICATION_BASE_ID_UNREAD_MESSAGE = 1;
|
||||
|
||||
private static final String GROUP_USER_BASE = "ml.docilealligator.infinityforreddit.";
|
||||
|
||||
static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
|
||||
Context context, String title, String content,
|
||||
String summary, String channelId, String channelName,
|
||||
String group, int color) {
|
||||
public static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
|
||||
Context context, String title, String content,
|
||||
String summary, String channelId, String channelName,
|
||||
String group, int color) {
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
@ -51,9 +54,9 @@ public class NotificationUtils {
|
||||
.setAutoCancel(true);
|
||||
}
|
||||
|
||||
static NotificationCompat.Builder buildSummaryNotification(Context context, NotificationManagerCompat notificationManager,
|
||||
String title, String content, String channelId,
|
||||
String channelName, String group, int color) {
|
||||
public static NotificationCompat.Builder buildSummaryNotification(Context context, NotificationManagerCompat notificationManager,
|
||||
String title, String content, String channelId,
|
||||
String channelName, String group, int color) {
|
||||
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
@ -70,19 +73,19 @@ public class NotificationUtils {
|
||||
.setAutoCancel(true);
|
||||
}
|
||||
|
||||
static NotificationManagerCompat getNotificationManager(Context context) {
|
||||
public static NotificationManagerCompat getNotificationManager(Context context) {
|
||||
return NotificationManagerCompat.from(context);
|
||||
}
|
||||
|
||||
static String getAccountGroupName(String accountName) {
|
||||
public static String getAccountGroupName(String accountName) {
|
||||
return GROUP_USER_BASE + accountName;
|
||||
}
|
||||
|
||||
static int getSummaryIdUnreadMessage(int accountIndex) {
|
||||
public static int getSummaryIdUnreadMessage(int accountIndex) {
|
||||
return SUMMARY_BASE_ID_UNREAD_MESSAGE + accountIndex * 1000;
|
||||
}
|
||||
|
||||
static int getNotificationIdUnreadMessage(int accountIndex, int messageIndex) {
|
||||
public static int getNotificationIdUnreadMessage(int accountIndex, int messageIndex) {
|
||||
return NOTIFICATION_BASE_ID_UNREAD_MESSAGE + accountIndex * 1000 + messageIndex;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user