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