1 /*
2  * Copyright 2019 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 androidx.camera.integration.antelope
18 
19 import android.os.Bundle
20 import android.view.LayoutInflater
21 import android.view.View
22 import android.view.ViewGroup
23 import androidx.camera.integration.antelope.databinding.SettingsDialogBinding
24 import androidx.fragment.app.DialogFragment
25 
26 /** DialogFragment that backs the configuration for both single tests and multiple tests */
27 internal class SettingsDialog : DialogFragment() {
28 
29     private var _binding: SettingsDialogBinding? = null
30     private val binding
31         get() = _binding!!
32 
onStartnull33     override fun onStart() {
34         // If we show a dialog with a title, it doesn't take up the whole screen
35         // Adjust the window to take up the full screen
36         dialog
37             ?.window
38             ?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
39         super.onStart()
40     }
41 
onCreatenull42     override fun onCreate(savedInstanceState: Bundle?) {
43         super.onCreate(savedInstanceState)
44 
45         // Set the dialog style so we get a title bar
46         setStyle(DialogFragment.STYLE_NORMAL, R.style.SettingsDialogTheme)
47     }
48 
49     /** Set up the dialog depending on the dialog type */
onCreateViewnull50     override fun onCreateView(
51         inflater: LayoutInflater,
52         container: ViewGroup?,
53         savedInstanceState: Bundle?
54     ): View? {
55         val args = arguments
56         val type = args?.getString(DIALOG_TYPE)
57         val title = args?.getString(DIALOG_TITLE)
58         val cameraNames = args?.getStringArray(CAMERA_NAMES)
59         val cameraIds = args?.getStringArray(CAMERA_IDS)
60 
61         dialog?.setTitle(title)
62 
63         _binding = SettingsDialogBinding.inflate(inflater, container, false)
64 
65         if (null != cameraIds && null != cameraNames) {
66             when (type) {
67                 DIALOG_TYPE_MULTI -> {
68                     val settingsFragment = MultiTestSettingsFragment()
69                     val childFragmentManager = childFragmentManager
70                     val fragmentTransaction = childFragmentManager.beginTransaction()
71                     fragmentTransaction.replace(R.id.scroll_settings_dialog, settingsFragment)
72                     fragmentTransaction.commit()
73                 }
74                 else -> {
75                     val settingsFragment = SingleTestSettingsFragment(cameraNames, cameraIds)
76                     val childFragmentManager = childFragmentManager
77                     val fragmentTransaction = childFragmentManager.beginTransaction()
78                     fragmentTransaction.replace(R.id.scroll_settings_dialog, settingsFragment)
79                     fragmentTransaction.commit()
80                 }
81             }
82         }
83 
84         return binding.root
85     }
86 
87     /** When view is created, set up action buttons */
onViewCreatednull88     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
89         super.onViewCreated(view, savedInstanceState)
90 
91         val args = arguments
92         val type = args?.getString(DIALOG_TYPE)
93 
94         when (type) {
95             DIALOG_TYPE_MULTI -> {
96                 binding.buttonStart.text = getString(R.string.settings_multi_go)
97                 binding.buttonCancel.text = getString(R.string.settings_multi_cancel)
98 
99                 binding.buttonStart.setOnClickListener {
100                     (activity as MainActivity).startMultiTest()
101                     this.dismiss()
102                 }
103                 binding.buttonCancel.setOnClickListener { this.dismiss() }
104             }
105             else -> {
106                 binding.buttonStart.text = getString(R.string.settings_single_go)
107                 binding.buttonCancel.text = getString(R.string.settings_single_cancel)
108 
109                 binding.buttonStart.setOnClickListener {
110                     (activity as MainActivity).startSingleTest()
111                     this.dismiss()
112                 }
113                 binding.buttonCancel.setOnClickListener { this.dismiss() }
114             }
115         }
116     }
117 
onDestroyViewnull118     override fun onDestroyView() {
119         super.onDestroyView()
120         _binding = null
121     }
122 
123     companion object {
124         private const val DIALOG_TYPE = "DIALOG_TYPE"
125         private const val DIALOG_TITLE = "DIALOG_TITLE"
126         private const val CAMERA_NAMES = "CAMERA_NAMES"
127         private const val CAMERA_IDS = "CAMERA_IDS"
128 
129         internal const val DIALOG_TYPE_SINGLE = "DIALOG_TYPE_SINGLE"
130         internal const val DIALOG_TYPE_MULTI = "DIALOG_TYPE_MULTI"
131 
132         /**
133          * Create a new Settings dialog to configure a test run
134          *
135          * @param type Dialog type (DIALOG_TYPE_MULTI or DIALOG_TYPE_SINGLE)
136          * @param title Dialog title
137          * @param cameraNames Human readable array of camera names
138          * @param cameraIds Array of camera ids
139          */
newInstancenull140         fun newInstance(
141             type: String,
142             title: String,
143             cameraNames: Array<String>,
144             cameraIds: Array<String>
145         ): SettingsDialog {
146 
147             val args = Bundle()
148             args.putString(DIALOG_TYPE, type)
149             args.putString(DIALOG_TITLE, title)
150             args.putStringArray(CAMERA_NAMES, cameraNames)
151             args.putStringArray(CAMERA_IDS, cameraIds)
152 
153             val settingsDialog = SettingsDialog()
154             settingsDialog.arguments = args
155             return settingsDialog
156         }
157     }
158 }
159