Petr Cermak | ed7429c | 2017-12-18 19:38:04 +0000 | [diff] [blame^] | 1 | package com.android.systemui.statusbar.policy; |
| 2 | |
| 3 | import android.app.PendingIntent; |
| 4 | import android.app.RemoteInput; |
| 5 | import android.content.Context; |
| 6 | import android.content.Intent; |
| 7 | import android.os.Bundle; |
| 8 | import android.util.AttributeSet; |
| 9 | import android.util.Log; |
| 10 | import android.view.LayoutInflater; |
| 11 | import android.view.ViewGroup; |
| 12 | import android.widget.Button; |
| 13 | import android.widget.LinearLayout; |
| 14 | |
| 15 | import com.android.systemui.R; |
| 16 | |
| 17 | /** View which displays smart reply buttons in notifications. */ |
| 18 | public 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 | } |