blob: d1b405256f39e8a1243f6b6ef04295124a89e97c [file] [log] [blame]
Evan Laird31ca5472020-04-08 17:45:24 -04001/*
2 * Copyright (C) 2020 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
17package com.android.systemui.statusbar.notification.row
18
19import android.app.Dialog
20import android.content.Context
21import android.graphics.Color
22import android.graphics.PixelFormat
23import android.graphics.drawable.ColorDrawable
24import android.view.Gravity
25import android.view.View
26import android.view.View.GONE
27import android.view.ViewGroup.LayoutParams.MATCH_PARENT
28import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
29import android.view.Window
30import android.view.WindowInsets.Type.statusBars
31import android.view.WindowManager
32import android.widget.LinearLayout
33import android.widget.TextView
34import com.android.systemui.Prefs
35import com.android.systemui.R
36import java.lang.IllegalStateException
37import javax.inject.Inject
38
39/**
40 * Controller to handle presenting the priority conversations onboarding dialog
41 */
42class PriorityOnboardingDialogController @Inject constructor(
43 val view: View,
44 val context: Context,
45 val ignoresDnd: Boolean,
46 val showsAsBubble: Boolean
47) {
48
49 private lateinit var dialog: Dialog
50
51 fun init() {
52 initDialog()
53 }
54
55 fun show() {
56 dialog.show()
57 }
58
59 private fun done() {
60 // Log that the user has seen the onboarding
61 Prefs.putBoolean(context, Prefs.Key.HAS_SEEN_PRIORITY_ONBOARDING, true)
62 dialog.dismiss()
63 }
64
65 class Builder @Inject constructor() {
66 private lateinit var view: View
67 private lateinit var context: Context
68 private var ignoresDnd = false
69 private var showAsBubble = false
70
71 fun setView(v: View): Builder {
72 view = v
73 return this
74 }
75
76 fun setContext(c: Context): Builder {
77 context = c
78 return this
79 }
80
81 fun setIgnoresDnd(ignore: Boolean): Builder {
82 ignoresDnd = ignore
83 return this
84 }
85
86 fun setShowsAsBubble(bubble: Boolean): Builder {
87 showAsBubble = bubble
88 return this
89 }
90
91 fun build(): PriorityOnboardingDialogController {
92 val controller = PriorityOnboardingDialogController(
93 view, context, ignoresDnd, showAsBubble)
94 return controller
95 }
96 }
97
98 private fun initDialog() {
99 dialog = Dialog(context)
100
101 if (dialog.window == null) {
102 throw IllegalStateException("Need a window for the onboarding dialog to show")
103 }
104
105 dialog.window?.requestFeature(Window.FEATURE_NO_TITLE)
106 // Prevent a11y readers from reading the first element in the dialog twice
107 dialog.setTitle("\u00A0")
108 dialog.apply {
109 setContentView(view)
110 setCanceledOnTouchOutside(true)
111
112 findViewById<TextView>(R.id.done_button)?.setOnClickListener {
113 done()
114 }
115
116 if (!ignoresDnd) {
117 findViewById<LinearLayout>(R.id.ignore_dnd_tip).visibility = GONE
118 }
119
120 if (!showsAsBubble) {
121 findViewById<LinearLayout>(R.id.floating_bubble_tip).visibility = GONE
122 }
123
124 window?.apply {
125 setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
126 addFlags(wmFlags)
127 setType(WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL)
128 setWindowAnimations(com.android.internal.R.style.Animation_InputMethod)
129
130 attributes = attributes.apply {
131 format = PixelFormat.TRANSLUCENT
132 title = ChannelEditorDialogController::class.java.simpleName
133 gravity = Gravity.BOTTOM or Gravity.CENTER_HORIZONTAL
134 fitInsetsTypes = attributes.fitInsetsTypes and statusBars().inv()
135 width = MATCH_PARENT
136 height = WRAP_CONTENT
137 }
138 }
139 }
140 }
141
142 private val wmFlags = (WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS
143 or WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
144 or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
145 or WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
146}