1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.media.dialog;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.graphics.ColorFilter;
22 import android.graphics.PorterDuff;
23 import android.graphics.PorterDuffColorFilter;
24 import android.os.Bundle;
25 import android.view.Gravity;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.Window;
29 import android.view.WindowManager;
30 import android.widget.Button;
31 import android.widget.ImageView;
32 
33 import com.android.systemui.R;
34 import com.android.systemui.statusbar.phone.SystemUIDialog;
35 
36 /**
37  * Confirmation dialog for releasing media session
38  */
39 
40 public class MediaSessionReleaseDialog extends SystemUIDialog {
41 
42     private View mDialogView;
43 
44     private final Context mContext;
45     private final View.OnClickListener mPositiveButtonListener;
46     private final ColorFilter mButtonColorFilter;
47     private final int mIconColor;
48 
MediaSessionReleaseDialog(Context context, Runnable runnable, int buttonColor, int iconColor)49     public MediaSessionReleaseDialog(Context context, Runnable runnable, int buttonColor,
50             int iconColor) {
51         super(context, R.style.Theme_SystemUI_Dialog_Media);
52         mContext = getContext();
53         mPositiveButtonListener = (v) -> {
54             runnable.run();
55             dismiss();
56         };
57         mButtonColorFilter = new PorterDuffColorFilter(
58                 buttonColor,
59                 PorterDuff.Mode.SRC_IN);
60         mIconColor = iconColor;
61     }
62 
63     @Override
onCreate(Bundle savedInstanceState)64     public void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66         mDialogView = LayoutInflater.from(mContext).inflate(R.layout.media_session_end_dialog,
67                 null);
68         final Window window = getWindow();
69         window.setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL);
70         window.setContentView(mDialogView);
71 
72         final WindowManager.LayoutParams lp = window.getAttributes();
73         lp.gravity = Gravity.CENTER;
74         lp.width = (int) (mContext.getResources().getDisplayMetrics().widthPixels * 0.90);
75 
76         ImageView headerIcon = mDialogView.requireViewById(R.id.end_icon);
77         headerIcon.setImageDrawable(mContext.getDrawable(R.drawable.media_output_status_failed));
78         headerIcon.setImageTintList(
79                 ColorStateList.valueOf(mIconColor));
80 
81         Button stopButton = mDialogView.requireViewById(R.id.stop_button);
82         stopButton.setOnClickListener(mPositiveButtonListener);
83         stopButton.getBackground().setColorFilter(mButtonColorFilter);
84 
85         Button cancelButton = mDialogView.requireViewById(R.id.cancel_button);
86         cancelButton.setOnClickListener((v) -> dismiss());
87         cancelButton.getBackground().setColorFilter(mButtonColorFilter);
88     }
89 }
90