parent
5b581fe7f3
commit
06af962610
@ -57,6 +57,7 @@ dependencies {
|
||||
compile "com.github.chrisbanes:PhotoView:2.1.3"
|
||||
compile "com.mikepenz:google-material-typeface:3.0.1.0.original@aar"
|
||||
compile "com.theartofdev.edmodo:android-image-cropper:2.5.1"
|
||||
compile 'com.evernote:android-job:1.2.0'
|
||||
|
||||
//room
|
||||
compile "android.arch.persistence.room:runtime:1.0.0-rc1"
|
||||
|
@ -98,7 +98,6 @@
|
||||
|
||||
<receiver android:name=".receiver.NotificationClearBroadcastReceiver" />
|
||||
|
||||
<service android:name=".service.PullNotificationService" />
|
||||
<service
|
||||
tools:targetApi="24"
|
||||
android:name="com.keylesspalace.tusky.service.TuskyTileService"
|
||||
|
@ -15,9 +15,7 @@
|
||||
|
||||
package com.keylesspalace.tusky;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
@ -25,7 +23,6 @@ import android.graphics.Color;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Bundle;
|
||||
import android.os.SystemClock;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
@ -33,20 +30,17 @@ import android.text.Spanned;
|
||||
import android.util.TypedValue;
|
||||
import android.view.Menu;
|
||||
|
||||
import com.evernote.android.job.JobManager;
|
||||
import com.evernote.android.job.JobRequest;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
||||
import com.keylesspalace.tusky.network.AuthInterceptor;
|
||||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.service.PullNotificationService;
|
||||
import com.keylesspalace.tusky.util.OkHttpUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Dispatcher;
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.logging.HttpLoggingInterceptor;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
@ -127,21 +121,9 @@ public class BaseActivity extends AppCompatActivity {
|
||||
.create();
|
||||
|
||||
OkHttpClient.Builder okBuilder =
|
||||
OkHttpUtils.getCompatibleClientBuilder().addInterceptor(new Interceptor() {
|
||||
@Override
|
||||
public Response intercept(Chain chain) throws IOException {
|
||||
Request originalRequest = chain.request();
|
||||
|
||||
Request.Builder builder = originalRequest.newBuilder();
|
||||
String accessToken = getAccessToken();
|
||||
if (accessToken != null) {
|
||||
builder.header("Authorization", String.format("Bearer %s", accessToken));
|
||||
}
|
||||
Request newRequest = builder.build();
|
||||
|
||||
return chain.proceed(newRequest);
|
||||
}
|
||||
}).dispatcher(mastodonApiDispatcher);
|
||||
OkHttpUtils.getCompatibleClientBuilder()
|
||||
.addInterceptor(new AuthInterceptor(this))
|
||||
.dispatcher(mastodonApiDispatcher);
|
||||
|
||||
if (BuildConfig.DEBUG) {
|
||||
okBuilder.addInterceptor(
|
||||
@ -187,26 +169,20 @@ public class BaseActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
protected void enablePushNotifications() {
|
||||
// Start up the PullNotificationService on a repeating interval.
|
||||
// schedule job to pull notifications
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
String minutesString = preferences.getString("pullNotificationCheckInterval", "15");
|
||||
long minutes = Long.valueOf(minutesString);
|
||||
long checkInterval = 1000 * 60 * minutes;
|
||||
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
Intent intent = new Intent(this, PullNotificationService.class);
|
||||
PendingIntent serviceAlarmIntent = PendingIntent.getService(this, SERVICE_REQUEST_CODE, intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
|
||||
checkInterval, serviceAlarmIntent);
|
||||
if (minutes < 15) {
|
||||
preferences.edit().putString("pullNotificationCheckInterval", "15").apply();
|
||||
minutes = 15;
|
||||
}
|
||||
setPullNotificationCheckInterval(minutes);
|
||||
}
|
||||
|
||||
protected void disablePushNotifications() {
|
||||
// Cancel the repeating call for "pull" notifications.
|
||||
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
Intent intent = new Intent(this, PullNotificationService.class);
|
||||
PendingIntent serviceAlarmIntent = PendingIntent.getService(this, SERVICE_REQUEST_CODE, intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
alarmManager.cancel(serviceAlarmIntent);
|
||||
JobManager.instance().cancelAllForTag(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG);
|
||||
}
|
||||
|
||||
protected void clearNotifications() {
|
||||
@ -215,17 +191,17 @@ public class BaseActivity extends AppCompatActivity {
|
||||
notificationPreferences.edit().putString("current", "[]").apply();
|
||||
|
||||
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
|
||||
manager.cancel(PullNotificationService.NOTIFY_ID);
|
||||
manager.cancel(NotificationPullJobCreator.NOTIFY_ID);
|
||||
}
|
||||
|
||||
protected void setPullNotificationCheckInterval(long minutes) {
|
||||
JobManager.instance().cancelAllForTag(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG);
|
||||
long checkInterval = 1000 * 60 * minutes;
|
||||
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||
Intent intent = new Intent(this, PullNotificationService.class);
|
||||
PendingIntent serviceAlarmIntent = PendingIntent.getService(this, SERVICE_REQUEST_CODE, intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
alarmManager.cancel(serviceAlarmIntent);
|
||||
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(),
|
||||
checkInterval, serviceAlarmIntent);
|
||||
|
||||
new JobRequest.Builder(NotificationPullJobCreator.NOTIFICATIONS_JOB_TAG)
|
||||
.setPeriodic(checkInterval)
|
||||
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
|
||||
.build()
|
||||
.schedule();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,123 @@
|
||||
package com.keylesspalace.tusky;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.Spanned;
|
||||
|
||||
import com.evernote.android.job.Job;
|
||||
import com.evernote.android.job.JobCreator;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.keylesspalace.tusky.entity.Notification;
|
||||
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
||||
import com.keylesspalace.tusky.network.AuthInterceptor;
|
||||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.util.NotificationMaker;
|
||||
import com.keylesspalace.tusky.util.OkHttpUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
/**
|
||||
* Created by charlag on 31/10/17.
|
||||
*/
|
||||
|
||||
public final class NotificationPullJobCreator implements JobCreator {
|
||||
|
||||
static final String NOTIFICATIONS_JOB_TAG = "notifications_job_tag";
|
||||
static final int NOTIFY_ID = 6; // chosen by fair dice roll, guaranteed to be random
|
||||
|
||||
private MastodonApi mastodonApi;
|
||||
private Context context;
|
||||
|
||||
NotificationPullJobCreator(Context context) {
|
||||
this.mastodonApi = createMastodonApi(context);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public Job create(@NonNull String tag) {
|
||||
if (tag.equals(NOTIFICATIONS_JOB_TAG)) {
|
||||
return new NotificationPullJob(mastodonApi, context);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private MastodonApi createMastodonApi(Context context) {
|
||||
SharedPreferences preferences = context.getSharedPreferences(
|
||||
context.getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
||||
final String domain = preferences.getString("domain", null);
|
||||
|
||||
OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder()
|
||||
.addInterceptor(new AuthInterceptor(context))
|
||||
.build();
|
||||
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
||||
.create();
|
||||
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://" + domain)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.build();
|
||||
|
||||
return retrofit.create(MastodonApi.class);
|
||||
}
|
||||
|
||||
private final static class NotificationPullJob extends Job {
|
||||
|
||||
@NonNull private MastodonApi mastodonApi;
|
||||
private Context context;
|
||||
|
||||
NotificationPullJob(@NonNull MastodonApi mastodonApi, Context context) {
|
||||
this.mastodonApi = mastodonApi;
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
protected Result onRunJob(Params params) {
|
||||
try {
|
||||
Response<List<Notification>> notifications =
|
||||
mastodonApi.notifications(null, null, null).execute();
|
||||
if (notifications.isSuccessful()) {
|
||||
onNotificationsReceived(notifications.body());
|
||||
} else {
|
||||
return Result.FAILURE;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return Result.FAILURE;
|
||||
}
|
||||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
private void onNotificationsReceived(List<Notification> notificationList) {
|
||||
SharedPreferences notificationsPreferences = context.getSharedPreferences(
|
||||
"Notifications", Context.MODE_PRIVATE);
|
||||
Set<String> currentIds = notificationsPreferences.getStringSet(
|
||||
"current_ids", new HashSet<String>());
|
||||
for (Notification notification : notificationList) {
|
||||
String id = notification.id;
|
||||
if (!currentIds.contains(id)) {
|
||||
currentIds.add(id);
|
||||
NotificationMaker.make(context, NOTIFY_ID, notification);
|
||||
}
|
||||
}
|
||||
notificationsPreferences.edit()
|
||||
.putStringSet("current_ids", currentIds)
|
||||
.apply();
|
||||
}
|
||||
}
|
||||
}
|
@ -19,6 +19,7 @@ import android.app.Application;
|
||||
import android.arch.persistence.room.Room;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.evernote.android.job.JobManager;
|
||||
import com.jakewharton.picasso.OkHttp3Downloader;
|
||||
import com.keylesspalace.tusky.db.AppDatabase;
|
||||
import com.keylesspalace.tusky.util.OkHttpUtils;
|
||||
@ -56,5 +57,7 @@ public class TuskyApplication extends Application {
|
||||
.allowMainThreadQueries()
|
||||
.addMigrations(AppDatabase.MIGRATION_2_3)
|
||||
.build();
|
||||
|
||||
JobManager.create(this).addJobCreator(new NotificationPullJobCreator(this));
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package com.keylesspalace.tusky.network;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.keylesspalace.tusky.R;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
|
||||
/**
|
||||
* Created by charlag on 31/10/17.
|
||||
*/
|
||||
|
||||
public final class AuthInterceptor implements Interceptor, SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
|
||||
private static final String TOKEN_KEY = "accessToken";
|
||||
|
||||
@Nullable
|
||||
private String token;
|
||||
|
||||
public AuthInterceptor(Context context) {
|
||||
SharedPreferences preferences = context.getSharedPreferences(
|
||||
context.getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
||||
token = preferences.getString(TOKEN_KEY, null);
|
||||
preferences.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response intercept(@NonNull Chain chain) throws IOException {
|
||||
Request originalRequest = chain.request();
|
||||
|
||||
Request.Builder builder = originalRequest.newBuilder();
|
||||
if (token != null) {
|
||||
builder.header("Authorization", String.format("Bearer %s", token));
|
||||
}
|
||||
Request newRequest = builder.build();
|
||||
|
||||
return chain.proceed(newRequest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
if (key.equals(TOKEN_KEY)) {
|
||||
token = sharedPreferences.getString(TOKEN_KEY, null);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,138 +0,0 @@
|
||||
/* Copyright 2017 Andrew Dawson
|
||||
*
|
||||
* This file is a part of Tusky.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* Tusky 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 Tusky; if not,
|
||||
* see <http://www.gnu.org/licenses>. */
|
||||
|
||||
package com.keylesspalace.tusky.service;
|
||||
|
||||
import android.app.IntentService;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.text.Spanned;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.keylesspalace.tusky.R;
|
||||
import com.keylesspalace.tusky.entity.Notification;
|
||||
import com.keylesspalace.tusky.json.SpannedTypeAdapter;
|
||||
import com.keylesspalace.tusky.network.MastodonApi;
|
||||
import com.keylesspalace.tusky.util.OkHttpUtils;
|
||||
import com.keylesspalace.tusky.util.NotificationMaker;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class PullNotificationService extends IntentService {
|
||||
public static final int NOTIFY_ID = 6; // This is an arbitrary number.
|
||||
|
||||
private MastodonApi mastodonApi;
|
||||
|
||||
public PullNotificationService() {
|
||||
super("Tusky Pull Notification Service");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onHandleIntent(Intent intent) {
|
||||
|
||||
Log.d("PullNotifications", "pulling for notification");
|
||||
|
||||
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(
|
||||
getApplicationContext());
|
||||
boolean enabled = preferences.getBoolean("notificationsEnabled", true);
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
createMastodonApi();
|
||||
|
||||
mastodonApi.notifications(null, null, null).enqueue(new Callback<List<Notification>>() {
|
||||
@Override
|
||||
public void onResponse(@NonNull Call<List<Notification>> call,
|
||||
@NonNull Response<List<Notification>> response) {
|
||||
if (response.isSuccessful()) {
|
||||
onNotificationsReceived(response.body());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(@NonNull Call<List<Notification>> call, @NonNull Throwable t) {}
|
||||
});
|
||||
}
|
||||
|
||||
private void createMastodonApi() {
|
||||
SharedPreferences preferences = getSharedPreferences(
|
||||
getString(R.string.preferences_file_key), Context.MODE_PRIVATE);
|
||||
final String domain = preferences.getString("domain", null);
|
||||
final String accessToken = preferences.getString("accessToken", null);
|
||||
|
||||
OkHttpClient okHttpClient = OkHttpUtils.getCompatibleClientBuilder()
|
||||
.addInterceptor(new Interceptor() {
|
||||
@Override
|
||||
public okhttp3.Response intercept(@NonNull Chain chain) throws IOException {
|
||||
Request originalRequest = chain.request();
|
||||
|
||||
Request.Builder builder = originalRequest.newBuilder()
|
||||
.header("Authorization", String.format("Bearer %s", accessToken));
|
||||
|
||||
Request newRequest = builder.build();
|
||||
|
||||
return chain.proceed(newRequest);
|
||||
}
|
||||
})
|
||||
.build();
|
||||
|
||||
Gson gson = new GsonBuilder()
|
||||
.registerTypeAdapter(Spanned.class, new SpannedTypeAdapter())
|
||||
.create();
|
||||
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl("https://" + domain)
|
||||
.client(okHttpClient)
|
||||
.addConverterFactory(GsonConverterFactory.create(gson))
|
||||
.build();
|
||||
|
||||
mastodonApi = retrofit.create(MastodonApi.class);
|
||||
}
|
||||
|
||||
private void onNotificationsReceived(List<Notification> notificationList) {
|
||||
SharedPreferences notificationsPreferences = getSharedPreferences(
|
||||
"Notifications", Context.MODE_PRIVATE);
|
||||
Set<String> currentIds = notificationsPreferences.getStringSet(
|
||||
"current_ids", new HashSet<String>());
|
||||
for (Notification notification : notificationList) {
|
||||
String id = notification.id;
|
||||
if (!currentIds.contains(id)) {
|
||||
currentIds.add(id);
|
||||
NotificationMaker.make(this, NOTIFY_ID, notification);
|
||||
}
|
||||
}
|
||||
notificationsPreferences.edit()
|
||||
.putStringSet("current_ids", currentIds)
|
||||
.apply();
|
||||
}
|
||||
}
|
@ -1,8 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string-array name="pull_notification_check_interval_names">
|
||||
<item>5 minutes</item>
|
||||
<item>10 minutes</item>
|
||||
<item>15 minutes</item>
|
||||
<item>20 minutes</item>
|
||||
<item>25 minutes</item>
|
||||
@ -13,8 +11,6 @@
|
||||
</string-array>
|
||||
|
||||
<string-array name="pull_notification_check_intervals" inputType="integer">
|
||||
<item>5</item>
|
||||
<item>10</item>
|
||||
<item>15</item>
|
||||
<item>20</item>
|
||||
<item>25</item>
|
||||
|
Loading…
Reference in New Issue
Block a user