/*
* Kontalk Android client
* Copyright (C) 2017 Kontalk Devteam <devteam@kontalk.org>
* 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.
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kontalk.service;
import android.content.BroadcastReceiver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.RemoteInput;
import org.kontalk.Kontalk;
import org.kontalk.R;
import org.kontalk.data.Conversation;
import org.kontalk.provider.MessagesProvider;
import org.kontalk.ui.MessagingNotification;
/**
* Broadcast receiver for notification actions.
* @author Daniele Ricci
*/
public class NotificationActionReceiver extends BroadcastReceiver {
private static final String KEY_TEXT_REPLY = "key_text_reply";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (MessagingNotification.ACTION_NOTIFICATION_DELETED.equals(action)) {
// mark threads as old
Parcelable[] threads = intent.getParcelableArrayExtra("org.kontalk.datalist");
for (Parcelable uri : threads)
MessagesProvider.markThreadAsOld(context, ContentUris.parseId((Uri) uri));
MessagingNotification.delayedUpdateMessagesNotification(context, false);
}
else if (MessagingNotification.ACTION_NOTIFICATION_REPLY.equals(action)) {
// mark threads as read
long threadId = ContentUris.parseId(intent.getData());
MessagesProvider.markThreadAsRead(context, threadId);
// send reply
Bundle result = RemoteInput.getResultsFromIntent(intent);
if (result != null) {
Conversation conv = Conversation.loadFromId(context, threadId);
String text = result.getString(KEY_TEXT_REPLY);
Kontalk.get(context).getMessagesController()
.sendTextMessage(conv, text);
}
// TODO show notification with the reply for a short time
// https://developer.android.com/guide/topics/ui/notifiers/notifications.html#direct
MessagingNotification.delayedUpdateMessagesNotification(context, false);
}
else if (MessagingNotification.ACTION_NOTIFICATION_MARK_READ.equals(action)) {
// mark threads as read
MessagesProvider.markThreadAsRead(context, ContentUris.parseId(intent.getData()));
MessagingNotification.delayedUpdateMessagesNotification(context, false);
}
}
public static RemoteInput buildReplyInput(Context context) {
String replyLabel = context.getString(R.string.reply);
return new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel(replyLabel)
.build();
}
}