blob: 57fc03cb730855badbc43a81d3e924779a00f540 [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
Petr Cermak10011fa2018-02-05 19:00:54 +000015import com.android.systemui.Dependency;
Petr Cermaked7429c2017-12-18 19:38:04 +000016import com.android.systemui.R;
17
18/** View which displays smart reply buttons in notifications. */
19public class SmartReplyView extends LinearLayout {
20
21 private static final String TAG = "SmartReplyView";
22
Petr Cermak10011fa2018-02-05 19:00:54 +000023 private final SmartReplyConstants mConstants;
24
Petr Cermaked7429c2017-12-18 19:38:04 +000025 public SmartReplyView(Context context, AttributeSet attrs) {
26 super(context, attrs);
Petr Cermak10011fa2018-02-05 19:00:54 +000027 mConstants = Dependency.get(SmartReplyConstants.class);
Petr Cermaked7429c2017-12-18 19:38:04 +000028 }
29
30 public void setRepliesFromRemoteInput(RemoteInput remoteInput, PendingIntent pendingIntent) {
31 removeAllViews();
32 if (remoteInput != null && pendingIntent != null) {
33 CharSequence[] choices = remoteInput.getChoices();
34 if (choices != null) {
35 for (CharSequence choice : choices) {
36 Button replyButton = inflateReplyButton(
37 getContext(), this, choice, remoteInput, pendingIntent);
38 addView(replyButton);
39 }
40 }
41 }
42 }
43
44 public static SmartReplyView inflate(Context context, ViewGroup root) {
45 return (SmartReplyView)
46 LayoutInflater.from(context).inflate(R.layout.smart_reply_view, root, false);
47 }
48
49 private static Button inflateReplyButton(Context context, ViewGroup root, CharSequence choice,
50 RemoteInput remoteInput, PendingIntent pendingIntent) {
51 Button b = (Button) LayoutInflater.from(context).inflate(
52 R.layout.smart_reply_button, root, false);
53 b.setText(choice);
54 b.setOnClickListener(view -> {
55 Bundle results = new Bundle();
56 results.putString(remoteInput.getResultKey(), choice.toString());
57 Intent intent = new Intent().addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
58 RemoteInput.addResultsToIntent(new RemoteInput[]{remoteInput}, intent, results);
Petr Cermak9a3380c2018-01-19 15:00:24 +000059 RemoteInput.setResultsSource(intent, RemoteInput.SOURCE_CHOICE);
Petr Cermaked7429c2017-12-18 19:38:04 +000060 try {
61 pendingIntent.send(context, 0, intent);
62 } catch (PendingIntent.CanceledException e) {
63 Log.w(TAG, "Unable to send smart reply", e);
64 }
65 });
66 return b;
67 }
68}