Prepares service #274

This commit is contained in:
tom79 2019-08-25 14:49:20 +02:00
parent 56c34ac861
commit 723bb4e02f
4 changed files with 54 additions and 10 deletions

View File

@ -55,6 +55,8 @@
android:exported="false"/>
<service android:name="app.fedilab.android.services.BackupStatusInDataBaseService"
android:exported="false"/>
<service android:name="app.fedilab.android.services.BackupNotificationInDataBaseService"
android:exported="false"/>
<receiver android:name="app.fedilab.android.services.RestartLiveNotificationReceiver"
android:exported="false">
<intent-filter>

View File

@ -3195,6 +3195,46 @@ public class API {
}
/**
* Retrieves notifications for the authenticated account *synchronously*
* @param max_id String id max
* @return APIResponse
*/
public APIResponse getNotifications( String max_id){
HashMap<String, String> params = new HashMap<>();
if( max_id != null )
params.put("max_id", max_id);
params.put("limit","30");
if( context == null){
apiResponse = new APIResponse();
Error error = new Error();
apiResponse.setError(error);
return apiResponse;
}
List<Notification> notifications = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context, this.instance);
String response = httpsConnection.get(getAbsoluteUrl("/notifications"), 15, params, prefKeyOauthTokenT);
apiResponse.setSince_id(httpsConnection.getSince_id());
apiResponse.setMax_id(httpsConnection.getMax_id());
notifications = parseNotificationResponse(new JSONArray(response));
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
apiResponse.setNotifications(notifications);
return apiResponse;
}
/**
* Retrieves notifications for the authenticated account *synchronously*
* @param max_id String id max

View File

@ -36,9 +36,10 @@ import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.client.API;
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.Account;
import app.fedilab.android.client.Entities.Status;
import app.fedilab.android.client.Entities.Notification;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.AccountDAO;
import app.fedilab.android.sqlite.NotificationCacheDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusCacheDAO;
import es.dmoral.toasty.Toasty;
@ -120,27 +121,27 @@ public class BackupNotificationInDataBaseService extends IntentService {
//Starts from the last recorded ID
Date sinceDate = new StatusCacheDAO(BackupNotificationInDataBaseService.this, db).getLastTootDateCache(StatusCacheDAO.ARCHIVE_CACHE, userId, instance);
String max_id = null;
List<Status> backupStatus = new ArrayList<>();
List<Notification> backupNotifications = new ArrayList<>();
boolean canContinue = true;
do {
APIResponse apiResponse = api.getStatus(userId, max_id);
APIResponse apiResponse = api.getNotifications(max_id);
max_id = apiResponse.getMax_id();
List<Status> statuses = apiResponse.getStatuses();
for(Status tmpStatus : statuses) {
if(sinceDate != null && max_id != null && tmpStatus.getCreated_at().before(sinceDate)){
List<Notification> notifications = apiResponse.getNotifications();
for(Notification tmpNotification : notifications) {
if(sinceDate != null && max_id != null && tmpNotification.getCreated_at().before(sinceDate)){
canContinue = false;
break;
}
new StatusCacheDAO(BackupNotificationInDataBaseService.this, db).insertStatus(StatusCacheDAO.ARCHIVE_CACHE, tmpStatus, userId, instance);
backupStatus.add(tmpStatus);
new NotificationCacheDAO(BackupNotificationInDataBaseService.this, db).insertNotification(tmpNotification, userId, instance);
backupNotifications.add(tmpNotification);
}
}while (max_id != null && canContinue);
if(backupStatus.size() > 0){
if(backupNotifications.size() > 0){
Intent backupIntent = new Intent(Helper.INTENT_BACKUP_FINISH);
LocalBroadcastManager.getInstance(this).sendBroadcast(backupIntent);
}
message = getString(R.string.data_backup_success, String.valueOf(backupStatus.size()));
message = getString(R.string.data_backup_success, String.valueOf(backupNotifications.size()));
Intent mainActivity = new Intent(BackupNotificationInDataBaseService.this, MainActivity.class);
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.BACKUP_INTENT);
String title = getString(R.string.data_backup_toots, account.getAcct());

View File

@ -451,6 +451,7 @@
<string name="show_pinned">Show pinned</string>
<string name="filter_no_result">No matching result found!</string>
<string name="data_backup_toots">Backup toots for %1$s</string>
<string name="data_backup_notifications">Backup notifications for %1$s</string>
<string name="data_backup_success">%1$s new toots have been imported</string>
<string-array name="filter_select">
<item>No</item>