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.content.SharedPreferences
20 import android.os.Bundle
21 import androidx.preference.ListPreference
22 import androidx.preference.PreferenceFragmentCompat
23 
24 /** Fragment that shows the settings for the "Single test" option */
25 class SingleTestSettingsFragment(
26     internal val cameraNames: Array<String>,
27     internal val cameraIds: Array<String>
28 ) : PreferenceFragmentCompat(), SharedPreferences.OnSharedPreferenceChangeListener {
29 
onCreatePreferencesnull30     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
31         setPreferencesFromResource(R.xml.single_test_settings, rootKey)
32 
33         val cameraPref =
34             preferenceManager.findPreference<ListPreference>(
35                 getString(R.string.settings_single_test_camera_key)
36             )
37         cameraPref?.entries = cameraNames
38         cameraPref?.entryValues = cameraIds
39         if (cameraIds.isNotEmpty()) cameraPref?.setDefaultValue(cameraIds[0])
40 
41         if (null == cameraPref?.value) cameraPref?.value = cameraIds[0]
42 
43         // En/disable needed controls
44         toggleNumTests()
45         togglePreviewBuffer()
46     }
47 
onSharedPreferenceChangednull48     override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
49         if (key.equals(getString(R.string.settings_single_test_type_key))) {
50             toggleNumTests()
51             togglePreviewBuffer()
52         }
53     }
54 
onResumenull55     override fun onResume() {
56         super.onResume()
57         preferenceManager.sharedPreferences?.registerOnSharedPreferenceChangeListener(this)
58     }
59 
onPausenull60     override fun onPause() {
61         super.onPause()
62         preferenceManager.sharedPreferences?.unregisterOnSharedPreferenceChangeListener(this)
63     }
64 
65     /**
66      * Some tests do not allow for multiple repetitions, If one of these selected, disable the
67      * number of tests control.
68      */
toggleNumTestsnull69     fun toggleNumTests() {
70         val typePref =
71             preferenceManager.findPreference<ListPreference>(
72                 getString(R.string.settings_single_test_type_key)
73             )
74         val numberPref =
75             preferenceManager.findPreference<ListPreference>(
76                 getString(R.string.settings_numtests_key)
77             )
78         when (typePref?.value) {
79             "INIT",
80             "PREVIEW",
81             "SWITCH_CAMERA",
82             "PHOTO" -> {
83                 numberPref?.isEnabled = false
84             }
85             else -> {
86                 numberPref?.isEnabled = true
87             }
88         }
89     }
90 
91     /**
92      * Some tests do not require the preview stream to run, If one of these selected, disable the
93      * preview buffer control.
94      */
togglePreviewBuffernull95     fun togglePreviewBuffer() {
96         val typePref =
97             preferenceManager.findPreference<ListPreference>(
98                 getString(R.string.settings_single_test_type_key)
99             )
100         val previewPref =
101             preferenceManager.findPreference<ListPreference>(
102                 getString(R.string.settings_previewbuffer_key)
103             )
104         when (typePref?.value) {
105             "INIT" -> {
106                 previewPref?.isEnabled = false
107             }
108             else -> {
109                 previewPref?.isEnabled = true
110             }
111         }
112     }
113 }
114