Home | History | Annotate | Download | only in settings
      1 /*
      2  * 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.service.settings;
     17 
     18 import android.content.DialogInterface;
     19 import android.os.Bundle;
     20 import android.support.v7.app.AlertDialog;
     21 import android.support.v7.app.AppCompatActivity;
     22 import android.view.LayoutInflater;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.widget.CompoundButton;
     26 import android.widget.EditText;
     27 import android.widget.ImageView;
     28 import android.widget.Switch;
     29 import android.widget.TextView;
     30 
     31 import com.example.android.autofillframework.R;
     32 import com.example.android.autofillframework.service.datasource.LocalAutofillRepository;
     33 
     34 public class SettingsActivity extends AppCompatActivity {
     35 
     36     @Override
     37     public void onCreate(Bundle savedInstanceState) {
     38         super.onCreate(savedInstanceState);
     39 
     40         setContentView(R.layout.settings_activity);
     41         final MyPreferences preferences = MyPreferences.getInstance(this);
     42         setupSettingsSwitch(R.id.settings_auth_responses_container,
     43                 R.id.settings_auth_responses_label,
     44                 R.id.settings_auth_responses_switch,
     45                 preferences.isResponseAuth(),
     46                 new CompoundButton.OnCheckedChangeListener() {
     47                     @Override
     48                     public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
     49                         preferences.setResponseAuth(b);
     50                     }
     51                 });
     52         setupSettingsSwitch(R.id.settings_auth_datasets_container,
     53                 R.id.settings_auth_datasets_label,
     54                 R.id.settings_auth_datasets_switch,
     55                 preferences.isDatasetAuth(),
     56                 new CompoundButton.OnCheckedChangeListener() {
     57                     @Override
     58                     public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
     59                         preferences.setDatasetAuth(b);
     60                     }
     61                 });
     62         setupSettingsButton(R.id.settings_clear_data_container,
     63                 R.id.settings_clear_data_label,
     64                 R.id.settings_clear_data_icon,
     65                 new View.OnClickListener() {
     66                     @Override
     67                     public void onClick(View view) {
     68                         buildClearDataDialog().show();
     69                     }
     70                 });
     71 
     72         setupSettingsButton(R.id.settings_auth_credentials_container,
     73                 R.id.settings_auth_credentials_label,
     74                 R.id.settings_auth_credentials_icon,
     75                 new View.OnClickListener() {
     76                     @Override
     77                     public void onClick(View view) {
     78                         if (preferences.getMasterPassword() != null) {
     79                             buildCurrentCredentialsDialog().show();
     80                         } else {
     81                             buildNewCredentialsDialog().show();
     82                         }
     83                     }
     84                 });
     85     }
     86 
     87     private AlertDialog buildClearDataDialog() {
     88         return new AlertDialog.Builder(SettingsActivity.this)
     89                 .setMessage(R.string.settings_clear_data_confirmation)
     90                 .setTitle(R.string.settings_clear_data_confirmation_title)
     91                 .setNegativeButton(R.string.cancel, null)
     92                 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
     93                     @Override
     94                     public void onClick(DialogInterface dialog, int which) {
     95                         LocalAutofillRepository.getInstance
     96                                 (SettingsActivity.this).clear();
     97                         MyPreferences.getInstance(SettingsActivity.this)
     98                                 .clearCredentials();
     99                         dialog.dismiss();
    100                     }
    101                 })
    102                 .create();
    103     }
    104 
    105     private AlertDialog.Builder prepareCredentialsDialog() {
    106         return new AlertDialog.Builder(SettingsActivity.this)
    107                 .setTitle(R.string.settings_auth_change_credentials_title)
    108                 .setNegativeButton(R.string.cancel, null);
    109     }
    110 
    111     private AlertDialog buildCurrentCredentialsDialog() {
    112         final EditText currentPasswordField = LayoutInflater
    113                 .from(SettingsActivity.this)
    114                 .inflate(R.layout.settings_authentication_dialog, null)
    115                 .findViewById(R.id.master_password_field);
    116         return prepareCredentialsDialog()
    117                 .setMessage(R.string.settings_auth_enter_current_password)
    118                 .setView(currentPasswordField)
    119                 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    120                     @Override
    121                     public void onClick(DialogInterface dialog, int which) {
    122                         String password = currentPasswordField.getText().toString();
    123                         if (MyPreferences.getInstance(SettingsActivity.this).getMasterPassword()
    124                                 .equals(password)) {
    125                             buildNewCredentialsDialog().show();
    126                             dialog.dismiss();
    127                         }
    128                     }
    129                 })
    130                 .create();
    131     }
    132 
    133     private AlertDialog buildNewCredentialsDialog() {
    134         final EditText newPasswordField = LayoutInflater
    135                 .from(SettingsActivity.this)
    136                 .inflate(R.layout.settings_authentication_dialog, null)
    137                 .findViewById(R.id.master_password_field);
    138         return prepareCredentialsDialog()
    139                 .setMessage(R.string.settings_auth_enter_new_password)
    140                 .setView(newPasswordField)
    141                 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    142                     @Override
    143                     public void onClick(DialogInterface dialog, int which) {
    144                         String password = newPasswordField.getText().toString();
    145                         MyPreferences.getInstance(SettingsActivity.this).setMasterPassword(password);
    146                         dialog.dismiss();
    147                     }
    148                 })
    149                 .create();
    150     }
    151 
    152     private void setupSettingsSwitch(int containerId, int labelId, int switchId, boolean checked,
    153             CompoundButton.OnCheckedChangeListener checkedChangeListener) {
    154         ViewGroup container = (ViewGroup) findViewById(containerId);
    155         String switchLabel = ((TextView) container.findViewById(labelId)).getText().toString();
    156         final Switch switchView = container.findViewById(switchId);
    157         switchView.setContentDescription(switchLabel);
    158         switchView.setChecked(checked);
    159         container.setOnClickListener(new View.OnClickListener() {
    160             @Override
    161             public void onClick(View v) {
    162                 switchView.performClick();
    163             }
    164         });
    165         switchView.setOnCheckedChangeListener(checkedChangeListener);
    166     }
    167 
    168     private void setupSettingsButton(int containerId, int labelId, int imageViewId,
    169             final View.OnClickListener onClickListener) {
    170         ViewGroup container = (ViewGroup) findViewById(containerId);
    171         String buttonLabel = ((TextView) container.findViewById(labelId)).getText().toString();
    172         final ImageView imageView = container.findViewById(imageViewId);
    173         imageView.setContentDescription(buttonLabel);
    174         container.setOnClickListener(onClickListener);
    175     }
    176 }
    177