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