blob: 1dcdf63258098fc6c98c7c998fadbcd925db363c [file] [log] [blame]
Petr Cermaked7429c2017-12-18 19:38:04 +00001package com.android.systemui.statusbar.policy;
2
3import android.app.PendingIntent;
4import android.app.RemoteInput;
5import android.content.Context;
6import android.content.Intent;
7import android.os.Bundle;
8import android.util.AttributeSet;
9import android.util.Log;
10import android.view.LayoutInflater;
11import android.view.ViewGroup;
12import android.widget.Button;
13import android.widget.LinearLayout;
14
15import com.android.systemui.R;
16
17/** View which displays smart reply buttons in notifications. */
18public class SmartReplyView extends LinearLayout {
19
20 private static final String TAG = "SmartReplyView";
21
22 public SmartReplyView(Context context, AttributeSet attrs) {
23 super(context, attrs);
24 }
25
26 public void setRepliesFromRemoteInput(RemoteInput remoteInput, PendingIntent pendingIntent) {
27 removeAllViews();
28 if (remoteInput != null && pendingIntent != null) {
29 CharSequence[] choices = remoteInput.getChoices();
30 if (choices != null) {
31 for (CharSequence choice : choices) {
32 Button replyButton = inflateReplyButton(
33 getContext(), this, choice, remoteInput, pendingIntent);
34 addView(replyButton);
35 }
36 }
37 }
38 }
39
40 public static SmartReplyView inflate(Context context, ViewGroup root) {
41 return (SmartReplyView)
42 LayoutInflater.from(context).inflate(R.layout.smart_reply_view, root, false);
43 }
44
45 private static Button inflateReplyButton(Context context, ViewGroup root, CharSequence choice,
46 RemoteInput remoteInput, PendingIntent pendingIntent) {
47 Button b = (Button) LayoutInflater.from(context).inflate(
48 R.layout.smart_reply_button, root, false);
49 b.setText(choice);
50 b.setOnClickListener(view -> {
51 Bundle results = new Bundle();
52 results.putString(remoteInput.getResultKey(), choice.toString());
53 Intent intent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
54 RemoteInput.addResultsToIntent(new RemoteInput[]{remoteInput}, intent, results);
55 try {
56 pendingIntent.send(context, 0, intent);
57 } catch (PendingIntent.CanceledException e) {
58 Log.w(TAG, "Unable to send smart reply", e);
59 }
60 });
61 return b;
62 }
63}