1 /*
<lambda>null2  * Copyright (C) 2017 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 package com.example.android.autofillframework.multidatasetservice.settings
17 
18 import android.os.Bundle
19 import android.support.v7.app.AlertDialog
20 import android.support.v7.app.AppCompatActivity
21 import android.view.LayoutInflater
22 import android.view.View
23 import android.view.ViewGroup
24 import android.widget.CompoundButton
25 import android.widget.EditText
26 import android.widget.ImageView
27 import android.widget.Switch
28 import android.widget.TextView
29 import com.example.android.autofillframework.R
30 import com.example.android.autofillframework.multidatasetservice.datasource.SharedPrefsAutofillRepository
31 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_credentials_container
32 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_credentials_icon
33 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_credentials_label
34 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_datasets_container
35 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_datasets_label
36 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_datasets_switch
37 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_responses_container
38 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_responses_label
39 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_auth_responses_switch
40 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_clear_data_container
41 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_clear_data_icon
42 import kotlinx.android.synthetic.main.multidataset_service_settings_activity.settings_clear_data_label
43 
44 class SettingsActivity : AppCompatActivity() {
45 
46     public override fun onCreate(savedInstanceState: Bundle?) {
47         super.onCreate(savedInstanceState)
48 
49         setContentView(R.layout.multidataset_service_settings_activity)
50         setupSettingsSwitch(settings_auth_responses_container,
51                 settings_auth_responses_label,
52                 settings_auth_responses_switch,
53                 MyPreferences.isResponseAuth(this),
54                 CompoundButton.OnCheckedChangeListener { compoundButton, b ->
55                     MyPreferences.setResponseAuth(this@SettingsActivity, b)
56                 })
57         setupSettingsSwitch(settings_auth_datasets_container,
58                 settings_auth_datasets_label,
59                 settings_auth_datasets_switch,
60                 MyPreferences.isDatasetAuth(this),
61                 CompoundButton.OnCheckedChangeListener { compoundButton, b ->
62                     MyPreferences.setDatasetAuth(this@SettingsActivity, b)
63                 })
64         setupSettingsButton(settings_clear_data_container,
65                 settings_clear_data_label,
66                 settings_clear_data_icon,
67                 View.OnClickListener { buildClearDataDialog().show() })
68 
69         setupSettingsButton(settings_auth_credentials_container,
70                 settings_auth_credentials_label,
71                 settings_auth_credentials_icon,
72                 View.OnClickListener {
73                     if (MyPreferences.getMainPassword(this@SettingsActivity) != null) {
74                         buildCurrentCredentialsDialog().show()
75                     } else {
76                         buildNewCredentialsDialog().show()
77                     }
78                 })
79     }
80 
81     private fun buildClearDataDialog(): AlertDialog {
82         return AlertDialog.Builder(this@SettingsActivity)
83                 .setMessage(R.string.settings_clear_data_confirmation)
84                 .setTitle(R.string.settings_clear_data_confirmation_title)
85                 .setNegativeButton(R.string.cancel, null)
86                 .setPositiveButton(R.string.ok) { dialog, which ->
87                     SharedPrefsAutofillRepository.clear(this@SettingsActivity)
88                     MyPreferences.clearCredentials(this@SettingsActivity)
89                     dialog.dismiss()
90                 }
91                 .create()
92     }
93 
94     private fun prepareCredentialsDialog(): AlertDialog.Builder {
95         return AlertDialog.Builder(this@SettingsActivity)
96                 .setTitle(R.string.settings_auth_change_credentials_title)
97                 .setNegativeButton(R.string.cancel, null)
98     }
99 
100     private fun buildCurrentCredentialsDialog(): AlertDialog {
101         val currentPasswordField = LayoutInflater
102                 .from(this@SettingsActivity)
103                 .inflate(R.layout.multidataset_service_settings_authentication_dialog, null)
104                 .findViewById<EditText>(R.id.main_password_field)
105         return prepareCredentialsDialog()
106                 .setMessage(R.string.settings_auth_enter_current_password)
107                 .setView(currentPasswordField)
108                 .setPositiveButton(R.string.ok) { dialog, which ->
109                     val password = currentPasswordField.text.toString()
110                     if (MyPreferences.getMainPassword(this@SettingsActivity) == password) {
111                         buildNewCredentialsDialog().show()
112                         dialog.dismiss()
113                     }
114                 }
115                 .create()
116     }
117 
118     private fun buildNewCredentialsDialog(): AlertDialog {
119         val newPasswordField = LayoutInflater
120                 .from(this@SettingsActivity)
121                 .inflate(R.layout.multidataset_service_settings_authentication_dialog, null)
122                 .findViewById<EditText>(R.id.main_password_field)
123         return prepareCredentialsDialog()
124                 .setMessage(R.string.settings_auth_enter_new_password)
125                 .setView(newPasswordField)
126                 .setPositiveButton(R.string.ok) { dialog, which ->
127                     val password = newPasswordField.text.toString()
128                     MyPreferences.setMainPassword(this@SettingsActivity, password)
129                     dialog.dismiss()
130                 }
131                 .create()
132     }
133 
134     private fun setupSettingsSwitch(container: ViewGroup, switchLabelView: TextView, switchView: Switch, checked: Boolean,
135             checkedChangeListener: CompoundButton.OnCheckedChangeListener) {
136         val switchLabel = switchLabelView.text.toString()
137         with(switchView) {
138             contentDescription = switchLabel
139             isChecked = checked
140             setOnCheckedChangeListener(checkedChangeListener)
141         }
142         container.setOnClickListener { switchView.performClick() }
143     }
144 
145     private fun setupSettingsButton(container: ViewGroup, buttonLabelView: TextView, imageView: ImageView,
146             onClickListener: View.OnClickListener) {
147         val buttonLabel = buttonLabelView.text.toString()
148         imageView.contentDescription = buttonLabel
149         container.setOnClickListener(onClickListener)
150     }
151 }
152