New design api and events handler
This commit is contained in:
parent
bcb75f2522
commit
325d51187b
|
@ -0,0 +1,131 @@
|
|||
/* Copyright 2017 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Mastalab
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
package fr.gouv.etalab.mastodon.asynctasks;
|
||||
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveStreamingInterface;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 28/08/2017.
|
||||
* Calls user streaming api
|
||||
*/
|
||||
|
||||
public class StreamingUserAsyncTask extends AsyncTask {
|
||||
|
||||
private String instance, token, acct, userId;
|
||||
private OnRetrieveStreamingInterface listener;
|
||||
|
||||
public StreamingUserAsyncTask(String instance, String token, String acct, String userId, OnRetrieveStreamingInterface onRetrieveStreamingInterface){
|
||||
this.instance = instance;
|
||||
this.token = token;
|
||||
this.acct = acct;
|
||||
this.userId = userId;
|
||||
this.listener = onRetrieveStreamingInterface;
|
||||
}
|
||||
public enum EventStreaming{
|
||||
UPDATE,
|
||||
NOTIFICATION,
|
||||
DELETE
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object doInBackground(Object[] params){
|
||||
try {
|
||||
URL url = new URL("https://" + this.instance + "/api/v1/streaming/user");
|
||||
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
|
||||
urlConnection.setRequestProperty("Content-Type", "application/json");
|
||||
urlConnection.setRequestProperty("Authorization", "Bearer "+this.token);
|
||||
urlConnection.setRequestProperty("Connection", "Keep-Alive");
|
||||
urlConnection.setRequestProperty("Keep-Alive", "header");
|
||||
Log.v(Helper.TAG, "http response: " + urlConnection.getResponseCode());
|
||||
|
||||
//Object inputStream = urlConnection.getContent();
|
||||
InputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
|
||||
Log.v(Helper.TAG, readStrem(inputStream)+"");
|
||||
} catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
Log.v(Helper.TAG, "Error on url openConnection: "+e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String readStrem(InputStream inputStream) {
|
||||
BufferedReader reader = null;
|
||||
try{
|
||||
reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
String line;
|
||||
String event;
|
||||
EventStreaming eventStreaming = null;
|
||||
while((line = reader.readLine()) != null){
|
||||
switch (line.trim()){
|
||||
case "update":
|
||||
event = reader.readLine();
|
||||
eventStreaming = EventStreaming.UPDATE;
|
||||
break;
|
||||
case "notification":
|
||||
event = reader.readLine();
|
||||
eventStreaming = EventStreaming.NOTIFICATION;
|
||||
break;
|
||||
case "delete":
|
||||
event = reader.readLine();
|
||||
eventStreaming = EventStreaming.DELETE;
|
||||
break;
|
||||
default:
|
||||
event = null;
|
||||
}
|
||||
if( event != null){
|
||||
try {
|
||||
JSONObject eventJson = new JSONObject(event);
|
||||
listener.onRetrieveStreaming(eventStreaming, eventJson, acct, userId);
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
if(reader != null){
|
||||
try{
|
||||
reader.close();
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
|
@ -188,12 +188,12 @@ public class API {
|
|||
get("/accounts/verify_credentials", null, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
account = parseAccountResponse(response);
|
||||
account = parseAccountResponse(context, response);
|
||||
}
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
|
||||
try {
|
||||
account = parseAccountResponse(response.getJSONObject(0));
|
||||
account = parseAccountResponse(context, response.getJSONObject(0));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -217,12 +217,12 @@ public class API {
|
|||
get(String.format("/accounts/%s",accountId), null, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
account = parseAccountResponse(response);
|
||||
account = parseAccountResponse(context, response);
|
||||
}
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
|
||||
try {
|
||||
account = parseAccountResponse(response.getJSONObject(0));
|
||||
account = parseAccountResponse(context, response.getJSONObject(0));
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -330,7 +330,7 @@ public class API {
|
|||
get(String.format("/accounts/%s/statuses", accountId), params, new JsonHttpResponseHandler() {
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Status status = parseStatuses(response);
|
||||
Status status = parseStatuses(context, response);
|
||||
statuses.add(status);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -365,7 +365,7 @@ public class API {
|
|||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
Status status = parseStatuses(response);
|
||||
Status status = parseStatuses(context, response);
|
||||
statuses.add(status);
|
||||
}
|
||||
@Override
|
||||
|
@ -445,7 +445,7 @@ public class API {
|
|||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Status status = parseStatuses(response);
|
||||
Status status = parseStatuses(context, response);
|
||||
statuses.add(status);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -500,7 +500,7 @@ public class API {
|
|||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Status status = parseStatuses(response);
|
||||
Status status = parseStatuses(context, response);
|
||||
statuses.add(status);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -557,7 +557,7 @@ public class API {
|
|||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Status status = parseStatuses(response);
|
||||
Status status = parseStatuses(context, response);
|
||||
statuses.add(status);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -641,7 +641,7 @@ public class API {
|
|||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
Account account = parseAccountResponse(response);
|
||||
Account account = parseAccountResponse(context, response);
|
||||
accounts.add(account);
|
||||
}
|
||||
@Override
|
||||
|
@ -692,7 +692,7 @@ public class API {
|
|||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
Account account = parseAccountResponse(response);
|
||||
Account account = parseAccountResponse(context, response);
|
||||
accounts.add(account);
|
||||
}
|
||||
@Override
|
||||
|
@ -741,7 +741,7 @@ public class API {
|
|||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Status status = parseStatuses(response);
|
||||
Status status = parseStatuses(context, response);
|
||||
statuses.add(status);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -945,7 +945,7 @@ public class API {
|
|||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Status statusreturned = parseStatuses(response);
|
||||
Status statusreturned = parseStatuses(context, response);
|
||||
statuses.add(statusreturned);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -1041,7 +1041,7 @@ public class API {
|
|||
|
||||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
Notification notification = parseNotificationResponse(response);
|
||||
Notification notification = parseNotificationResponse(context, response);
|
||||
notifications.add(notification);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -1130,7 +1130,7 @@ public class API {
|
|||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
accounts = new ArrayList<>();
|
||||
account = parseAccountResponse(response);
|
||||
account = parseAccountResponse(context, response);
|
||||
accounts.add(account);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -1170,7 +1170,7 @@ public class API {
|
|||
@Override
|
||||
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
|
||||
accounts = new ArrayList<>();
|
||||
account = parseAccountResponse(response);
|
||||
account = parseAccountResponse(context, response);
|
||||
accounts.add(account);
|
||||
apiResponse.setSince_id(findSinceId(headers));
|
||||
apiResponse.setMax_id(findMaxId(headers));
|
||||
|
@ -1236,7 +1236,7 @@ public class API {
|
|||
while (i < jsonArray.length() ){
|
||||
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
Status status = parseStatuses(resobj);
|
||||
Status status = parseStatuses(context, resobj);
|
||||
i++;
|
||||
statuses.add(status);
|
||||
}
|
||||
|
@ -1253,7 +1253,7 @@ public class API {
|
|||
* @return Status
|
||||
*/
|
||||
@SuppressWarnings("InfiniteRecursion")
|
||||
private Status parseStatuses(JSONObject resobj){
|
||||
private static Status parseStatuses(Context context, JSONObject resobj){
|
||||
Status status = new Status();
|
||||
try {
|
||||
status.setId(resobj.get("id").toString());
|
||||
|
@ -1315,14 +1315,14 @@ public class API {
|
|||
}
|
||||
status.setTags(tags);
|
||||
|
||||
status.setAccount(parseAccountResponse(resobj.getJSONObject("account")));
|
||||
status.setAccount(parseAccountResponse(context, resobj.getJSONObject("account")));
|
||||
status.setContent(resobj.get("content").toString());
|
||||
status.setFavourites_count(Integer.valueOf(resobj.get("favourites_count").toString()));
|
||||
status.setReblogs_count(Integer.valueOf(resobj.get("reblogs_count").toString()));
|
||||
status.setReblogged(Boolean.valueOf(resobj.get("reblogged").toString()));
|
||||
status.setFavourited(Boolean.valueOf(resobj.get("favourited").toString()));
|
||||
try{
|
||||
status.setReblog(parseStatuses(resobj.getJSONObject("reblog")));
|
||||
status.setReblog(parseStatuses(context, resobj.getJSONObject("reblog")));
|
||||
}catch (Exception ignored){}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -1355,7 +1355,7 @@ public class API {
|
|||
* @param resobj JSONObject
|
||||
* @return Account
|
||||
*/
|
||||
private Account parseAccountResponse(JSONObject resobj){
|
||||
private static Account parseAccountResponse(Context context, JSONObject resobj){
|
||||
|
||||
Account account = new Account();
|
||||
try {
|
||||
|
@ -1392,7 +1392,7 @@ public class API {
|
|||
int i = 0;
|
||||
while (i < jsonArray.length() ) {
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
Account account = parseAccountResponse(resobj);
|
||||
Account account = parseAccountResponse(context, resobj);
|
||||
accounts.add(account);
|
||||
i++;
|
||||
}
|
||||
|
@ -1416,7 +1416,7 @@ public class API {
|
|||
Account account = null;
|
||||
while (i < jsonArray.length() ) {
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
account = parseAccountResponse(resobj);
|
||||
account = parseAccountResponse(context, resobj);
|
||||
if( account.getAcct().contains(Helper.DEVELOPER_INSTANCE))
|
||||
accounts.add(account);
|
||||
i++;
|
||||
|
@ -1500,16 +1500,16 @@ public class API {
|
|||
* @param resobj JSONObject
|
||||
* @return Account
|
||||
*/
|
||||
private Notification parseNotificationResponse(JSONObject resobj){
|
||||
public static Notification parseNotificationResponse(Context context, JSONObject resobj){
|
||||
|
||||
Notification notification = new Notification();
|
||||
try {
|
||||
notification.setId(resobj.get("id").toString());
|
||||
notification.setType(resobj.get("type").toString());
|
||||
notification.setCreated_at(Helper.mstStringToDate(context, resobj.get("created_at").toString()));
|
||||
notification.setAccount(parseAccountResponse(resobj.getJSONObject("account")));
|
||||
notification.setAccount(parseAccountResponse(context, resobj.getJSONObject("account")));
|
||||
try{
|
||||
notification.setStatus(parseStatuses(resobj.getJSONObject("status")));
|
||||
notification.setStatus(parseStatuses(context, resobj.getJSONObject("status")));
|
||||
}catch (Exception ignored){}
|
||||
notification.setCreated_at(Helper.mstStringToDate(context, resobj.get("created_at").toString()));
|
||||
} catch (JSONException e) {
|
||||
|
@ -1531,7 +1531,7 @@ public class API {
|
|||
while (i < jsonArray.length() ) {
|
||||
|
||||
JSONObject resobj = jsonArray.getJSONObject(i);
|
||||
Notification notification = parseNotificationResponse(resobj);
|
||||
Notification notification = parseNotificationResponse(context, resobj);
|
||||
notifications.add(notification);
|
||||
i++;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
/* Copyright 2017 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Mastalab
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
package fr.gouv.etalab.mastodon.interfaces;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import fr.gouv.etalab.mastodon.asynctasks.StreamingUserAsyncTask;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 28/08/2017.
|
||||
* Interface when event from streaming api has been retrieved
|
||||
*/
|
||||
public interface OnRetrieveStreamingInterface {
|
||||
void onRetrieveStreaming(StreamingUserAsyncTask.EventStreaming event, JSONObject response, String acct, String userId);
|
||||
}
|
|
@ -0,0 +1,267 @@
|
|||
package fr.gouv.etalab.mastodon.jobs;
|
||||
/* Copyright 2017 Thomas Schneider
|
||||
*
|
||||
* This file is a part of Mastalab
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
||||
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
||||
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
||||
* Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.os.AsyncTask;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.view.View;
|
||||
|
||||
import com.evernote.android.job.Job;
|
||||
import com.evernote.android.job.JobManager;
|
||||
import com.evernote.android.job.JobRequest;
|
||||
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
|
||||
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||||
import com.nostra13.universalimageloader.core.ImageLoader;
|
||||
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
|
||||
import com.nostra13.universalimageloader.core.assist.FailReason;
|
||||
import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer;
|
||||
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
|
||||
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveNotificationsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.StreamingUserAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.APIResponse;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Notification;
|
||||
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveStreamingInterface;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import mastodon.etalab.gouv.fr.mastodon.R;
|
||||
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.INTENT_ACTION;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.NOTIFICATION_INTENT;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.PREF_KEY_ID;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.canNotify;
|
||||
import static fr.gouv.etalab.mastodon.helper.Helper.notify_user;
|
||||
|
||||
|
||||
/**
|
||||
* Created by Thomas on 29/04/2017.
|
||||
* Notifications refresh job
|
||||
*/
|
||||
|
||||
public class SteamingSyncJob extends Job implements OnRetrieveStreamingInterface {
|
||||
|
||||
static final String STREAMING = "job_streaming";
|
||||
private String message;
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Result onRunJob(Params params) {
|
||||
//Code refresh here
|
||||
callAsynchronousTask();
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
public static int schedule(boolean updateCurrent){
|
||||
|
||||
Set<JobRequest> jobRequests = JobManager.instance().getAllJobRequestsForTag(STREAMING);
|
||||
if (!jobRequests.isEmpty() && !updateCurrent) {
|
||||
return jobRequests.iterator().next().getJobId();
|
||||
}
|
||||
|
||||
return new JobRequest.Builder(SteamingSyncJob.STREAMING)
|
||||
.setPeriodic(TimeUnit.MINUTES.toMillis(Helper.MINUTES_BETWEEN_NOTIFICATIONS_REFRESH), TimeUnit.MINUTES.toMillis(5))
|
||||
.setPersisted(true)
|
||||
.setUpdateCurrent(updateCurrent)
|
||||
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
|
||||
.setRequirementsEnforced(false)
|
||||
.build()
|
||||
.schedule();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Task in background starts here.
|
||||
*/
|
||||
private void callAsynchronousTask() {
|
||||
|
||||
SQLiteDatabase db = Sqlite.getInstance(getContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
//If an Internet connection and user agrees with notification refresh
|
||||
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
//Check which notifications the user wants to see
|
||||
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
||||
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
||||
boolean notif_ask = sharedpreferences.getBoolean(Helper.SET_NOTIF_ASK, true);
|
||||
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
||||
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
||||
//User disagree with all notifications
|
||||
if( !notif_follow && !notif_add && !notif_ask && !notif_mention && !notif_share)
|
||||
return; //Nothing is done
|
||||
//No account connected, the service is stopped
|
||||
if(!Helper.isLoggedIn(getContext()))
|
||||
return;
|
||||
//If WIFI only and on WIFI OR user defined any connections to use the service.
|
||||
if(!sharedpreferences.getBoolean(Helper.SET_WIFI_ONLY, false) || Helper.isOnWIFI(getContext())) {
|
||||
List<Account> accounts = new AccountDAO(getContext(),db).getAllAccount();
|
||||
//It means there is no user in DB.
|
||||
if( accounts == null )
|
||||
return;
|
||||
//Retrieve users in db that owner has.
|
||||
for (Account account: accounts) {
|
||||
String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + account.getId(), null);
|
||||
new StreamingUserAsyncTask(account.getInstance(), account.getToken(), account.getAcct(), account.getId(), SteamingSyncJob.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onRetrieveStreaming(StreamingUserAsyncTask.EventStreaming event, JSONObject response, String acct, String userId) {
|
||||
if( response == null )
|
||||
return;
|
||||
String new_max_id = null;
|
||||
final SharedPreferences sharedpreferences = getContext().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
boolean notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true);
|
||||
boolean notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true);
|
||||
boolean notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true);
|
||||
boolean notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true);
|
||||
final String max_id = sharedpreferences.getString(Helper.LAST_NOTIFICATION_MAX_ID + userId, null);
|
||||
|
||||
|
||||
//No previous notifications in cache, so no notification will be sent
|
||||
boolean notify = false;
|
||||
|
||||
String notificationUrl = null;
|
||||
String title = null;
|
||||
if( event == StreamingUserAsyncTask.EventStreaming.NOTIFICATION){
|
||||
Notification notification = API.parseNotificationResponse(getContext(), response);
|
||||
new_max_id = notification.getId();
|
||||
switch (notification.getType()){
|
||||
case "mention":
|
||||
if(notif_mention){
|
||||
notify = true;
|
||||
notificationUrl = notification.getAccount().getAvatar();
|
||||
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
||||
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_mention));
|
||||
else
|
||||
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_mention));
|
||||
}
|
||||
break;
|
||||
case "reblog":
|
||||
if(notif_share){
|
||||
notify = true;
|
||||
notificationUrl = notification.getAccount().getAvatar();
|
||||
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
||||
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_reblog));
|
||||
else
|
||||
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_reblog));
|
||||
}
|
||||
break;
|
||||
case "favourite":
|
||||
if(notif_add){
|
||||
notify = true;
|
||||
notificationUrl = notification.getAccount().getAvatar();
|
||||
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
||||
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_favourite));
|
||||
else
|
||||
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_favourite));
|
||||
}
|
||||
break;
|
||||
case "follow":
|
||||
if(notif_follow){
|
||||
notify = true;
|
||||
notificationUrl = notification.getAccount().getAvatar();
|
||||
if( notification.getAccount().getDisplay_name() != null && notification.getAccount().getDisplay_name().length() > 0 )
|
||||
title = String.format("@%s %s", Helper.shortnameToUnicode(notification.getAccount().getDisplay_name(), true),getContext().getString(R.string.notif_follow));
|
||||
else
|
||||
title = String.format("@%s %s", notification.getAccount().getUsername(),getContext().getString(R.string.notif_follow));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, notification.getId());
|
||||
editor.apply();
|
||||
message = notification.getStatus().getContent();
|
||||
if( message!= null) {
|
||||
message = message.substring(0, 17);
|
||||
message = message + "...";
|
||||
}
|
||||
}else if ( event == StreamingUserAsyncTask.EventStreaming.UPDATE){
|
||||
|
||||
}else if( event == StreamingUserAsyncTask.EventStreaming.DELETE){
|
||||
|
||||
}
|
||||
if( new_max_id != null){
|
||||
SharedPreferences.Editor editor = sharedpreferences.edit();
|
||||
editor.putString(Helper.LAST_NOTIFICATION_MAX_ID + userId, new_max_id);
|
||||
editor.apply();
|
||||
return;
|
||||
}
|
||||
|
||||
//Check which user is connected and if activity is to front
|
||||
|
||||
|
||||
|
||||
if( notify){
|
||||
final Intent intent = new Intent(getContext(), MainActivity.class);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
|
||||
intent.putExtra(INTENT_ACTION, NOTIFICATION_INTENT);
|
||||
intent.putExtra(PREF_KEY_ID, userId);
|
||||
long notif_id = Long.parseLong(userId);
|
||||
final int notificationId = ((notif_id + 1) > 2147483647) ? (int) (2147483647 - notif_id - 1) : (int) (notif_id + 1);
|
||||
|
||||
if( notificationUrl != null){
|
||||
ImageLoader imageLoaderNoty = ImageLoader.getInstance();
|
||||
File cacheDir = new File(getContext().getCacheDir(), getContext().getString(R.string.app_name));
|
||||
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())
|
||||
.imageDownloader(new PatchBaseImageDownloader(getContext()))
|
||||
.threadPoolSize(5)
|
||||
.threadPriority(Thread.MIN_PRIORITY + 3)
|
||||
.denyCacheImageMultipleSizesInMemory()
|
||||
.diskCache(new UnlimitedDiskCache(cacheDir))
|
||||
.build();
|
||||
imageLoaderNoty.init(config);
|
||||
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new SimpleBitmapDisplayer()).cacheInMemory(false)
|
||||
.cacheOnDisk(true).resetViewBeforeLoading(true).build();
|
||||
|
||||
final String finalTitle = title;
|
||||
imageLoaderNoty.loadImage(notificationUrl, options, new SimpleImageLoadingListener(){
|
||||
@Override
|
||||
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
|
||||
super.onLoadingComplete(imageUri, view, loadedImage);
|
||||
notify_user(getContext(), intent, notificationId, loadedImage, finalTitle, message);
|
||||
|
||||
}
|
||||
@Override
|
||||
public void onLoadingFailed(String imageUri, View view, FailReason failReason){
|
||||
notify_user(getContext(), intent, notificationId, BitmapFactory.decodeResource(getContext().getResources(),
|
||||
R.drawable.mastodonlogo), finalTitle, message);
|
||||
}});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -71,6 +71,7 @@ import java.util.Locale;
|
|||
import java.util.Stack;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import fr.gouv.etalab.mastodon.asynctasks.StreamingUserAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoByIDAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.PatchBaseImageDownloader;
|
||||
|
@ -448,6 +449,8 @@ public class MainActivity extends AppCompatActivity
|
|||
navigationView.getMenu().findItem(R.id.nav_follow_request).setVisible(false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
LinearLayout owner_container = (LinearLayout) headerLayout.findViewById(R.id.owner_container);
|
||||
owner_container.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
|
Loading…
Reference in New Issue