From 47ebcb29bfa61258afcb04d8d4f189acf3c7c47e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 15 Mar 2017 23:20:29 +0100 Subject: [PATCH 1/2] Share action for own toots --- app/src/main/java/com/keylesspalace/tusky/LoginActivity.java | 5 ----- app/src/main/res/menu/status_more_for_user.xml | 4 +++- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java index 88a453c33..5f7ef0303 100644 --- a/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/LoginActivity.java @@ -56,11 +56,6 @@ public class LoginActivity extends AppCompatActivity { @BindView(R.id.button_login) Button button; @BindView(R.id.no_account) TextView noAccount; - @Override - protected void createMastodonAPI() { - // Don't do this in this activity, since we don't know a domain yet - } - /** * Chain together the key-value pairs into a query string, for either appending to a URL or * as the content of an HTTP request. diff --git a/app/src/main/res/menu/status_more_for_user.xml b/app/src/main/res/menu/status_more_for_user.xml index ba2936ca4..274ce3679 100644 --- a/app/src/main/res/menu/status_more_for_user.xml +++ b/app/src/main/res/menu/status_more_for_user.xml @@ -1,6 +1,8 @@ - + \ No newline at end of file From 3a1181bd82ee5fa400523fb434a975e7671a07f0 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 15 Mar 2017 23:45:59 +0100 Subject: [PATCH 2/2] Enable/disable push notifications. Clearing notifications resets the summary of the notifications --- app/src/main/AndroidManifest.xml | 3 ++ .../com/keylesspalace/tusky/BaseActivity.java | 37 +++++++++++++++++++ .../com/keylesspalace/tusky/MainActivity.java | 26 +------------ .../tusky/MyFirebaseMessagingService.java | 11 ++++++ .../NotificationClearBroadcastReceiver.java | 16 ++++++++ .../tusky/PreferencesActivity.java | 8 ++++ app/src/main/res/values/strings.xml | 1 + app/src/main/res/xml/preferences.xml | 11 ++++-- 8 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 app/src/main/java/com/keylesspalace/tusky/NotificationClearBroadcastReceiver.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f6bbf47fa..01ee20319 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -55,11 +55,14 @@ + + + \ No newline at end of file diff --git a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java index 0fcd6ffd1..eb81596da 100644 --- a/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/BaseActivity.java @@ -28,6 +28,7 @@ import android.text.Spanned; import android.util.TypedValue; import android.view.Menu; +import com.google.firebase.iid.FirebaseInstanceId; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -38,6 +39,9 @@ import okhttp3.Interceptor; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; +import okhttp3.ResponseBody; +import retrofit2.Call; +import retrofit2.Callback; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; @@ -96,6 +100,11 @@ public class BaseActivity extends AppCompatActivity { return preferences.getString("accessToken", null); } + protected boolean arePushNotificationsEnabled() { + SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); + return preferences.getBoolean("notificationsEnabled", true); + } + protected String getBaseUrl() { SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); return "https://" + preferences.getString("domain", null); @@ -161,4 +170,32 @@ public class BaseActivity extends AppCompatActivity { } return super.onCreateOptionsMenu(menu); } + + protected void enablePushNotifications() { + tuskyAPI.register(getBaseUrl(), getAccessToken(), FirebaseInstanceId.getInstance().getToken()).enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } + + protected void disablePushNotifications() { + tuskyAPI.unregister(getBaseUrl(), getAccessToken()).enqueue(new Callback() { + @Override + public void onResponse(Call call, retrofit2.Response response) { + + } + + @Override + public void onFailure(Call call, Throwable t) { + + } + }); + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index cd93ca7bf..d5240dcba 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -39,7 +39,6 @@ import android.widget.TextView; import com.arlib.floatingsearchview.FloatingSearchView; import com.arlib.floatingsearchview.suggestions.SearchSuggestionsAdapter; import com.arlib.floatingsearchview.suggestions.model.SearchSuggestion; -import com.google.firebase.iid.FirebaseInstanceId; import com.keylesspalace.tusky.entity.Account; import com.mikepenz.google_material_typeface_library.GoogleMaterial; import com.mikepenz.materialdrawer.AccountHeader; @@ -61,7 +60,6 @@ import java.util.Stack; import butterknife.BindView; import butterknife.ButterKnife; -import okhttp3.ResponseBody; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; @@ -175,17 +173,7 @@ public class MainActivity extends BaseActivity { } // Setup push notifications - tuskyAPI.register(getBaseUrl(), getAccessToken(), FirebaseInstanceId.getInstance().getToken()).enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - Log.d(TAG, "tusky-api reponse: " + response.message()); - } - - @Override - public void onFailure(Call call, Throwable t) { - Log.d(TAG, "tusky-api failure: " + t.getMessage()); - } - }); + if (arePushNotificationsEnabled()) enablePushNotifications(); } @Override @@ -277,17 +265,7 @@ public class MainActivity extends BaseActivity { } private void logout() { - tuskyAPI.unregister(getBaseUrl(), getAccessToken()).enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) { - - } - - @Override - public void onFailure(Call call, Throwable t) { - - } - }); + if (arePushNotificationsEnabled()) disablePushNotifications(); SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); diff --git a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java index 9455a805f..aa9342516 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java +++ b/app/src/main/java/com/keylesspalace/tusky/MyFirebaseMessagingService.java @@ -55,6 +55,13 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { Log.d(TAG, notificationId); + SharedPreferences preferences = getSharedPreferences(getString(R.string.preferences_file_key), Context.MODE_PRIVATE); + boolean enabled = preferences.getBoolean("notificationsEnabled", true); + + if (!enabled) { + return; + } + createMastodonAPI(); mastodonAPI.notification(notificationId).enqueue(new Callback() { @@ -152,9 +159,13 @@ public class MyFirebaseMessagingService extends FirebaseMessagingService { stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); + Intent deleteIntent = new Intent(this, NotificationClearBroadcastReceiver.class); + PendingIntent deletePendingIntent = PendingIntent.getBroadcast(this, 0, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); + final NotificationCompat.Builder builder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_notify) .setContentIntent(resultPendingIntent) + .setDeleteIntent(deletePendingIntent) .setDefaults(0); // So it doesn't ring twice, notify only in Target callback if (currentNotifications.length() == 1) { diff --git a/app/src/main/java/com/keylesspalace/tusky/NotificationClearBroadcastReceiver.java b/app/src/main/java/com/keylesspalace/tusky/NotificationClearBroadcastReceiver.java new file mode 100644 index 000000000..fedbb70f7 --- /dev/null +++ b/app/src/main/java/com/keylesspalace/tusky/NotificationClearBroadcastReceiver.java @@ -0,0 +1,16 @@ +package com.keylesspalace.tusky; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; + +public class NotificationClearBroadcastReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + SharedPreferences notificationPreferences = context.getSharedPreferences("Notifications", Context.MODE_PRIVATE); + SharedPreferences.Editor editor = notificationPreferences.edit(); + editor.putString("current", "[]"); + editor.apply(); + } +} diff --git a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java index 17fd82c7e..5664c354b 100644 --- a/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/PreferencesActivity.java @@ -68,6 +68,14 @@ public class PreferencesActivity extends BaseActivity startActivity(intent); finish(); overridePendingTransition(R.anim.fade_in, R.anim.fade_out); + } else if (key.equals("notificationsEnabled")) { + boolean notificationsEnabled = sharedPreferences.getBoolean("notificationsEnabled", true); + + if (notificationsEnabled) { + enablePushNotifications(); + } else { + disablePushNotifications(); + } } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 38ca3db11..1a3c34c33 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -133,4 +133,5 @@ %1$s, %2$s, and %3$s %1$s and %2$s %d new interactions + Push notifications diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index ecd57b22a..323a49fda 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -1,23 +1,28 @@ - + + - -