Periodic sync, reload from memory if outdated
This commit is contained in:
@@ -27,6 +27,7 @@ android {
|
|||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
//implementation "com.android.support:support-compat:28.0.0"
|
||||||
implementation 'androidx.recyclerview:recyclerview:1.0.0'
|
implementation 'androidx.recyclerview:recyclerview:1.0.0'
|
||||||
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
implementation fileTree(include: ['*.jar'], dir: 'libs')
|
||||||
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-beta02', {
|
androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0-beta02', {
|
||||||
@@ -34,7 +35,7 @@ dependencies {
|
|||||||
})
|
})
|
||||||
implementation 'androidx.preference:preference:1.0.0'
|
implementation 'androidx.preference:preference:1.0.0'
|
||||||
implementation 'com.google.android.material:material:1.0.0'
|
implementation 'com.google.android.material:material:1.0.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
|
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha3'
|
||||||
implementation 'com.android.volley:volley:1.1.0'
|
implementation 'com.android.volley:volley:1.1.0'
|
||||||
//noinspection GradleDependency
|
//noinspection GradleDependency
|
||||||
implementation 'com.google.android.gms:play-services-auth:15.0.0'
|
implementation 'com.google.android.gms:play-services-auth:15.0.0'
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package de.sebse.fuplanner;
|
package de.sebse.fuplanner;
|
||||||
|
|
||||||
|
import android.accounts.Account;
|
||||||
import android.accounts.AccountManager;
|
import android.accounts.AccountManager;
|
||||||
import android.content.ContentResolver;
|
import android.content.ContentResolver;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -48,6 +49,7 @@ import de.sebse.fuplanner.services.kvv.types.LoginToken;
|
|||||||
import de.sebse.fuplanner.services.kvv.types.Modules;
|
import de.sebse.fuplanner.services.kvv.types.Modules;
|
||||||
import de.sebse.fuplanner.services.news.NewsManager;
|
import de.sebse.fuplanner.services.news.NewsManager;
|
||||||
import de.sebse.fuplanner.tools.CustomAccountManager;
|
import de.sebse.fuplanner.tools.CustomAccountManager;
|
||||||
|
import de.sebse.fuplanner.tools.CustomNotificationManager;
|
||||||
import de.sebse.fuplanner.tools.MainActivityListener;
|
import de.sebse.fuplanner.tools.MainActivityListener;
|
||||||
import de.sebse.fuplanner.tools.NewAsyncQueue;
|
import de.sebse.fuplanner.tools.NewAsyncQueue;
|
||||||
import de.sebse.fuplanner.tools.Preferences;
|
import de.sebse.fuplanner.tools.Preferences;
|
||||||
@@ -138,12 +140,12 @@ public class MainActivity extends AppCompatActivity
|
|||||||
changeFragment(getDefaultFragmentAfterLogout());
|
changeFragment(getDefaultFragmentAfterLogout());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
ContentResolver.addPeriodicSync(
|
|
||||||
mAccountManager.getAccountByType(AccountGeneral.ACCOUNT_TYPE),
|
|
||||||
KVVContentProvider.PROVIDER_NAME,
|
|
||||||
Bundle.EMPTY,
|
|
||||||
AccountGeneral.SYNC_INTERVAL);
|
|
||||||
|
|
||||||
|
if (!Preferences.getBoolean(this, R.string.pref_set_auto_sync_on_startup)) {
|
||||||
|
registerSync();
|
||||||
|
Preferences.setBoolean(this, R.string.pref_set_auto_sync_on_startup, true);
|
||||||
|
}
|
||||||
|
CustomNotificationManager.createNotificationChannel(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -169,6 +171,8 @@ public class MainActivity extends AppCompatActivity
|
|||||||
getKVV().modules().list().reloadIfOutdated();
|
getKVV().modules().list().reloadIfOutdated();
|
||||||
}
|
}
|
||||||
isPaused = false;
|
isPaused = false;
|
||||||
|
//log.d("onResume", "send notification!");
|
||||||
|
//CustomNotificationManager.sendNotification(this, "Titel", "Neue Announcements!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -357,6 +361,20 @@ public class MainActivity extends AppCompatActivity
|
|||||||
((TextView) header.findViewById(R.id.login_mail)).setText(email);
|
((TextView) header.findViewById(R.id.login_mail)).setText(email);
|
||||||
|
|
||||||
changeFragment(newFragment);
|
changeFragment(newFragment);
|
||||||
|
registerSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void registerSync() {
|
||||||
|
Account accountByType = mAccountManager.getAccountByType(AccountGeneral.ACCOUNT_TYPE);
|
||||||
|
log.d("registerSync", accountByType);
|
||||||
|
if (accountByType != null) {
|
||||||
|
ContentResolver.setSyncAutomatically(accountByType, KVVContentProvider.PROVIDER_NAME, true);
|
||||||
|
ContentResolver.addPeriodicSync(
|
||||||
|
accountByType,
|
||||||
|
KVVContentProvider.PROVIDER_NAME,
|
||||||
|
Bundle.EMPTY,
|
||||||
|
AccountGeneral.SYNC_INTERVAL);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void changeFragment(int newFragment) {
|
private void changeFragment(int newFragment) {
|
||||||
|
|||||||
@@ -17,11 +17,13 @@ import de.sebse.fuplanner.services.kvv.KVVListener;
|
|||||||
import de.sebse.fuplanner.services.kvv.types.LoginToken;
|
import de.sebse.fuplanner.services.kvv.types.LoginToken;
|
||||||
import de.sebse.fuplanner.services.kvv.types.Modules;
|
import de.sebse.fuplanner.services.kvv.types.Modules;
|
||||||
import de.sebse.fuplanner.tools.CustomAccountManager;
|
import de.sebse.fuplanner.tools.CustomAccountManager;
|
||||||
|
import de.sebse.fuplanner.tools.NewAsyncQueue;
|
||||||
import de.sebse.fuplanner.tools.logging.Logger;
|
import de.sebse.fuplanner.tools.logging.Logger;
|
||||||
|
|
||||||
public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
|
public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
|
||||||
private KVV mKVV;
|
private KVV mKVV;
|
||||||
private Logger log = new Logger(this);
|
private Logger log = new Logger(this);
|
||||||
|
private NewAsyncQueue mQueue = new NewAsyncQueue();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set up the sync adapter
|
* Set up the sync adapter
|
||||||
@@ -53,7 +55,12 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
return accountManager;
|
return accountManager;
|
||||||
}
|
}
|
||||||
}, context);
|
}, context);
|
||||||
mKVV.account().restoreOnlineLogin(bool -> {});
|
mQueue.add(() -> {
|
||||||
|
mKVV.account().restoreOnlineLogin(bool -> {
|
||||||
|
log.d("login restored");
|
||||||
|
mQueue.next();
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -68,20 +75,29 @@ public class KVVSyncAdapter extends AbstractThreadedSyncAdapter {
|
|||||||
String authority,
|
String authority,
|
||||||
ContentProviderClient provider,
|
ContentProviderClient provider,
|
||||||
SyncResult syncResult) {
|
SyncResult syncResult) {
|
||||||
|
log.d("onPerformSync");
|
||||||
|
mQueue.add(() -> {
|
||||||
log.d("start syncing");
|
log.d("start syncing");
|
||||||
/*
|
|
||||||
* Put the data transfer code here.
|
|
||||||
*/
|
|
||||||
mKVV.modules().list().recv(success -> {
|
mKVV.modules().list().recv(success -> {
|
||||||
Iterator<Modules.Module> iterator = success.latestSemesterIterator();
|
Iterator<Modules.Module> iterator = success.latestSemesterIterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Modules.Module module = iterator.next();
|
Modules.Module module = iterator.next();
|
||||||
log.d("sync module", module.title);
|
log.d("sync module", module.title);
|
||||||
mKVV.modules().details().recv(module, success1 -> {
|
mKVV.modules().details().recv(module, success1 -> {
|
||||||
if (success1.second)
|
if (success1.second) {
|
||||||
log.d("Sync Successful for Module '"+module.title+"'!");
|
log.d("Sync Successful for Module '"+module.title+"'!");
|
||||||
}, log::e, true);
|
mQueue.next();
|
||||||
}
|
}
|
||||||
}, log::e, true);
|
}, msg -> {
|
||||||
|
log.e(msg);
|
||||||
|
mQueue.next();
|
||||||
|
}, true);
|
||||||
|
}
|
||||||
|
}, msg -> {
|
||||||
|
log.e(msg);
|
||||||
|
mQueue.next();
|
||||||
|
}, true);
|
||||||
|
log.d("finished");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package de.sebse.fuplanner.tools;
|
||||||
|
|
||||||
|
import android.app.NotificationChannel;
|
||||||
|
import android.app.NotificationManager;
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import androidx.core.app.NotificationCompat;
|
||||||
|
import androidx.core.app.NotificationManagerCompat;
|
||||||
|
import de.sebse.fuplanner.MainActivity;
|
||||||
|
import de.sebse.fuplanner.R;
|
||||||
|
|
||||||
|
public class CustomNotificationManager {
|
||||||
|
private static final String CHANNEL_ID = "fuplanner-default";
|
||||||
|
//public static ArrayList<Integer> passedNotifications = new ArrayList<>();
|
||||||
|
|
||||||
|
public static void sendNotification(Context context, String textTitle, String textContent) {
|
||||||
|
Intent intent = new Intent(context, MainActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
|
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
|
||||||
|
|
||||||
|
|
||||||
|
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
|
||||||
|
.setSmallIcon(R.drawable.ic_notification)
|
||||||
|
.setContentTitle(textTitle)
|
||||||
|
.setContentText(textContent)
|
||||||
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
|
.setContentIntent(pendingIntent)
|
||||||
|
.setAutoCancel(true);
|
||||||
|
|
||||||
|
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
|
||||||
|
int notificationId = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
|
||||||
|
notificationManager.notify(notificationId, mBuilder.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void createNotificationChannel(@NotNull Context context) {
|
||||||
|
// Create the NotificationChannel, but only on API 26+ because
|
||||||
|
// the NotificationChannel class is new and not in the support library
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
|
CharSequence name = context.getString(R.string.channel_name);
|
||||||
|
String description = context.getString(R.string.channel_description);
|
||||||
|
int importance = NotificationManager.IMPORTANCE_DEFAULT;
|
||||||
|
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
|
||||||
|
channel.setDescription(description);
|
||||||
|
// Register the channel with the system; you can't change the importance
|
||||||
|
// or other notification behaviors after this
|
||||||
|
NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
|
||||||
|
if (notificationManager != null) {
|
||||||
|
notificationManager.createNotificationChannel(channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.sebse.fuplanner.tools;
|
package de.sebse.fuplanner.tools;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import androidx.annotation.ArrayRes;
|
import androidx.annotation.ArrayRes;
|
||||||
import androidx.annotation.StringRes;
|
import androidx.annotation.StringRes;
|
||||||
import androidx.preference.PreferenceManager;
|
import androidx.preference.PreferenceManager;
|
||||||
@@ -20,4 +21,14 @@ public class Preferences {
|
|||||||
String string = context.getResources().getString(key);
|
String string = context.getResources().getString(key);
|
||||||
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(string, value).apply();
|
PreferenceManager.getDefaultSharedPreferences(context).edit().putLong(string, value).apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean getBoolean(Context context, @StringRes int key) {
|
||||||
|
String string = context.getResources().getString(key);
|
||||||
|
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(string, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setBoolean(Context context, @StringRes int key, boolean value) {
|
||||||
|
String string = context.getResources().getString(key);
|
||||||
|
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(string, value).apply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
app/src/main/res/drawable-hdpi/ic_notification.png
Normal file
BIN
app/src/main/res/drawable-hdpi/ic_notification.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 427 B |
BIN
app/src/main/res/drawable-mdpi/ic_notification.png
Normal file
BIN
app/src/main/res/drawable-mdpi/ic_notification.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 320 B |
BIN
app/src/main/res/drawable-xhdpi/ic_notification.png
Normal file
BIN
app/src/main/res/drawable-xhdpi/ic_notification.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 472 B |
BIN
app/src/main/res/drawable-xxhdpi/ic_notification.png
Normal file
BIN
app/src/main/res/drawable-xxhdpi/ic_notification.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 879 B |
BIN
app/src/main/res/drawable-xxxhdpi/ic_notification.png
Normal file
BIN
app/src/main/res/drawable-xxxhdpi/ic_notification.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.0 KiB |
@@ -92,4 +92,5 @@
|
|||||||
<string name="error_incorrect_password">Das Passwort ist nicht korrekt.</string>
|
<string name="error_incorrect_password">Das Passwort ist nicht korrekt.</string>
|
||||||
<string name="error_field_required">Pflichtfeld</string>
|
<string name="error_field_required">Pflichtfeld</string>
|
||||||
<string name="kvv_sync">KVV-Synchronisation</string>
|
<string name="kvv_sync">KVV-Synchronisation</string>
|
||||||
|
<string name="channel_name">Neue Daten verfügbar</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -8,4 +8,6 @@
|
|||||||
<string name="pref_price_group_default" translatable="false">all</string>
|
<string name="pref_price_group_default" translatable="false">all</string>
|
||||||
|
|
||||||
<string name="pref_last_visited_news" translatable="false">pref_last_visited_news</string>
|
<string name="pref_last_visited_news" translatable="false">pref_last_visited_news</string>
|
||||||
|
|
||||||
|
<string name="pref_set_auto_sync_on_startup" translatable="false">pref_set_auto_sync_on_startup</string>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -100,4 +100,6 @@
|
|||||||
<string name="error_incorrect_password">This password is incorrect</string>
|
<string name="error_incorrect_password">This password is incorrect</string>
|
||||||
<string name="error_field_required">This field is required</string>
|
<string name="error_field_required">This field is required</string>
|
||||||
<string name="kvv_sync">KVV Synchronization</string>
|
<string name="kvv_sync">KVV Synchronization</string>
|
||||||
|
<string name="channel_name">New data available</string>
|
||||||
|
<string name="channel_description">Notify when new announcements, assignments, grades or resources are available</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
|||||||
Reference in New Issue
Block a user