Browse Source

Start sync service after boot complete and update default freq to 30 secs.

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
1bd7f9add4
  1. 6
      app/src/main/AndroidManifest.xml
  2. 2
      app/src/main/java/awais/instagrabber/fragments/settings/DMPreferencesFragment.java
  3. 27
      app/src/main/java/awais/instagrabber/services/BootCompletedReceiver.java
  4. 2
      app/src/main/java/awais/instagrabber/services/DMSyncAlarmReceiver.java
  5. 15
      app/src/main/java/awais/instagrabber/services/DMSyncService.java

6
app/src/main/AndroidManifest.xml

@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-feature
android:name="android.hardware.camera.any"
@ -154,6 +155,11 @@
android:name=".services.DMSyncAlarmReceiver"
android:enabled="true"
android:exported="false" />
<receiver android:name=".services.BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<uses-library
android:name="org.apache.http.legacy"

2
app/src/main/java/awais/instagrabber/fragments/settings/DMPreferencesFragment.java

@ -174,7 +174,7 @@ public class DMPreferencesFragment extends BasePreferencesFragment {
private void setupNumberEditText(final Context context) {
int currentValue = settingsHelper.getInteger(PREF_ENABLE_DM_AUTO_REFRESH_FREQ_NUMBER);
if (currentValue <= 0) {
currentValue = 5;
currentValue = 30;
}
binding.freqNum.setText(String.valueOf(currentValue));
binding.freqNum.addTextChangedListener(new TextWatcherAdapter() {

27
app/src/main/java/awais/instagrabber/services/BootCompletedReceiver.java

@ -0,0 +1,27 @@
package awais.instagrabber.services;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import java.util.Objects;
import awais.instagrabber.fragments.settings.PreferenceKeys;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.CookieUtils;
import awais.instagrabber.utils.TextUtils;
import static awais.instagrabber.utils.Utils.settingsHelper;
public class BootCompletedReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (!Objects.equals(intent.getAction(), "android.intent.action.BOOT_COMPLETED")) return;
final boolean enabled = settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH);
if (!enabled) return;
final String cookie = settingsHelper.getString(Constants.COOKIE);
final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0;
if (!isLoggedIn) return;
DMSyncAlarmReceiver.setAlarm(context);
}
}

2
app/src/main/java/awais/instagrabber/services/DMSyncAlarmReceiver.java

@ -70,7 +70,7 @@ public class DMSyncAlarmReceiver extends BroadcastReceiver {
private static long getIntervalMillis() {
int amount = settingsHelper.getInteger(PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH_FREQ_NUMBER);
if (amount <= 0) {
amount = 5;
amount = 30;
}
final String unit = settingsHelper.getString(PreferenceKeys.PREF_ENABLE_DM_AUTO_REFRESH_FREQ_UNIT);
final TemporalUnit temporalUnit;

15
app/src/main/java/awais/instagrabber/services/DMSyncService.java

@ -40,9 +40,12 @@ import awais.instagrabber.repositories.responses.directmessages.DirectItem;
import awais.instagrabber.repositories.responses.directmessages.DirectThread;
import awais.instagrabber.repositories.responses.directmessages.DirectThreadLastSeenAt;
import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.CookieUtils;
import awais.instagrabber.utils.DMUtils;
import awais.instagrabber.utils.DateUtils;
import awais.instagrabber.utils.Utils;
import awais.instagrabber.utils.TextUtils;
import static awais.instagrabber.utils.Utils.settingsHelper;
public class DMSyncService extends LifecycleService {
private static final String TAG = DMSyncService.class.getSimpleName();
@ -209,7 +212,15 @@ public class DMSyncService extends LifecycleService {
@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
super.onStartCommand(intent, flags, startId);
final boolean notificationsEnabled = Utils.settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_DM_NOTIFICATIONS);
final String cookie = settingsHelper.getString(Constants.COOKIE);
final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0;
if (!isLoggedIn) {
stopSelf();
return START_NOT_STICKY;
}
// Need to setup here if service was started by the boot completed receiver
CookieUtils.setupCookies(cookie);
final boolean notificationsEnabled = settingsHelper.getBoolean(PreferenceKeys.PREF_ENABLE_DM_NOTIFICATIONS);
inboxManager.getInbox().observe(this, inboxResource -> {
if (!notificationsEnabled || inboxResource == null || inboxResource.status != Resource.Status.SUCCESS) {
stopSelf();

Loading…
Cancel
Save