1 /*
2  * Copyright (C) 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 com.android.car.developeroptions;
18 
19 import android.app.Activity;
20 import android.app.backup.IBackupManager;
21 import android.os.Bundle;
22 import android.os.RemoteException;
23 import android.os.ServiceManager;
24 import android.text.TextUtils;
25 import android.util.Log;
26 import android.view.View;
27 import android.view.View.OnClickListener;
28 import android.widget.Button;
29 import android.widget.TextView;
30 import android.widget.Toast;
31 
32 public class SetFullBackupPassword extends Activity {
33     static final String TAG = "SetFullBackupPassword";
34 
35     IBackupManager mBackupManager;
36     TextView mCurrentPw, mNewPw, mConfirmNewPw;
37     Button mCancel, mSet;
38 
39     OnClickListener mButtonListener = new OnClickListener() {
40         @Override
41         public void onClick(View v) {
42             if (v == mSet) {
43                 final String curPw = mCurrentPw.getText().toString();
44                 final String newPw = mNewPw.getText().toString();
45                 final String confirmPw = mConfirmNewPw.getText().toString();
46 
47                 if (!newPw.equals(confirmPw)) {
48                     // Mismatch between new pw and its confirmation re-entry
49                     Log.i(TAG, "password mismatch");
50                     Toast.makeText(SetFullBackupPassword.this,
51                             R.string.local_backup_password_toast_confirmation_mismatch,
52                             Toast.LENGTH_LONG).show();
53                     return;
54                 }
55 
56                 // TODO: should we distinguish cases of has/hasn't set a pw before?
57 
58                 if (setBackupPassword(curPw, newPw)) {
59                     // success
60                     Log.i(TAG, "password set successfully");
61                     Toast.makeText(SetFullBackupPassword.this,
62                             R.string.local_backup_password_toast_success,
63                             Toast.LENGTH_LONG).show();
64                     finish();
65                 } else {
66                     // failure -- bad existing pw, usually
67                     Log.i(TAG, "failure; password mismatch?");
68                     Toast.makeText(SetFullBackupPassword.this,
69                             R.string.local_backup_password_toast_validation_failure,
70                             Toast.LENGTH_LONG).show();
71                 }
72             } else if (v == mCancel) {
73                 finish();
74             } else {
75                 Log.w(TAG, "Click on unknown view");
76             }
77         }
78     };
79 
80     @Override
onCreate(Bundle icicle)81     public void onCreate(Bundle icicle) {
82         super.onCreate(icicle);
83 
84         mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
85 
86         setContentView(R.layout.set_backup_pw);
87 
88         mCurrentPw = (TextView) findViewById(R.id.current_backup_pw);
89         mNewPw = (TextView) findViewById(R.id.new_backup_pw);
90         mConfirmNewPw = (TextView) findViewById(R.id.confirm_new_backup_pw);
91 
92         mCancel = (Button) findViewById(R.id.backup_pw_cancel_button);
93         mSet = (Button) findViewById(R.id.backup_pw_set_button);
94 
95         mCancel.setOnClickListener(mButtonListener);
96         mSet.setOnClickListener(mButtonListener);
97     }
98 
setBackupPassword(String currentPw, String newPw)99     private boolean setBackupPassword(String currentPw, String newPw) {
100         // new password can't be empty
101         if (TextUtils.isEmpty(newPw)) {
102             return false;
103         }
104 
105         try {
106             return mBackupManager.setBackupPassword(currentPw, newPw);
107         } catch (RemoteException e) {
108             Log.e(TAG, "Unable to communicate with backup manager");
109             return false;
110         }
111     }
112 }
113