Home | History | Annotate | Download | only in users
      1 /*
      2  * Copyright (C) 2018 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 com.android.car.settings.users;
     18 
     19 import static android.content.DialogInterface.BUTTON_NEGATIVE;
     20 import static android.content.DialogInterface.BUTTON_POSITIVE;
     21 
     22 import android.app.Dialog;
     23 import android.content.DialogInterface;
     24 import android.os.Bundle;
     25 import android.support.v4.app.DialogFragment;
     26 import android.support.v4.app.Fragment;
     27 
     28 import androidx.car.app.CarAlertDialog;
     29 
     30 import com.android.car.settings.R;
     31 
     32 /**
     33  * Dialog to confirm creation of new user.
     34  */
     35 public class ConfirmCreateNewUserDialog extends DialogFragment implements
     36         DialogInterface.OnClickListener {
     37     private static final String DIALOG_TAG = "ConfirmCreateNewUserDialog";
     38     private ConfirmCreateNewUserListener mCreateListener;
     39     private CancelCreateNewUserListener mCancelListener;
     40 
     41     /**
     42      * Interface for listeners that want to receive a callback when user confirms new user creation
     43      * in the dialog.
     44      */
     45     public interface ConfirmCreateNewUserListener {
     46         void onCreateNewUserConfirmed();
     47     }
     48 
     49     /**
     50      * Interface for listeners that want to receive a callback when user cancels new user creation
     51      * in the dialog.
     52      */
     53     public interface CancelCreateNewUserListener {
     54         void onCreateNewUserCancelled();
     55     }
     56 
     57     /**
     58      * Shows the dialog.
     59      *
     60      * @param parent Fragment associated with the dialog.
     61      */
     62     public void show(Fragment parent) {
     63         setTargetFragment(parent, 0);
     64         show(parent.getFragmentManager(), DIALOG_TAG);
     65     }
     66 
     67     /**
     68      * Sets a listener for OnCreateNewUserConfirmed that will get called if user confirms
     69      * the dialog.
     70      *
     71      * @param listener Instance of {@link ConfirmCreateNewUserListener} to call when confirmed.
     72      */
     73     public void setConfirmCreateNewUserListener(ConfirmCreateNewUserListener listener) {
     74         mCreateListener = listener;
     75     }
     76 
     77     /**
     78      * Sets a listener for OnCancelNewUserConfirmed that will get called if user cancels
     79      * the dialog.
     80      *
     81      * @param listener Instance of {@link CancelCreateNewUserListener} to call when user presses
     82      * cancel.
     83      */
     84     public void setCancelCreateNewUserListener(CancelCreateNewUserListener listener) {
     85         mCancelListener = listener;
     86     }
     87 
     88     @Override
     89     public Dialog onCreateDialog(Bundle savedInstanceState) {
     90         String message = getString(R.string.user_add_user_message_setup)
     91                 .concat(System.getProperty("line.separator"))
     92                 .concat(System.getProperty("line.separator"))
     93                 .concat(getString(R.string.user_add_user_message_update));
     94 
     95         return new CarAlertDialog.Builder(getContext())
     96                 .setTitle(R.string.user_add_user_title)
     97                 .setBody(message)
     98                 .setNegativeButton(android.R.string.cancel, this)
     99                 .setPositiveButton(android.R.string.ok, this)
    100                 .create();
    101     }
    102 
    103     @Override
    104     public void onClick(DialogInterface dialog, int which) {
    105         if (which == BUTTON_POSITIVE) {
    106             if (mCreateListener != null) {
    107                 mCreateListener.onCreateNewUserConfirmed();
    108             }
    109         } else if (which == BUTTON_NEGATIVE) {
    110             if (mCancelListener != null) {
    111                 mCancelListener.onCreateNewUserCancelled();
    112             }
    113         }
    114 
    115         dialog.dismiss();
    116     }
    117 }