14f33878f208e729cf96b708c57686555d2493129Christopher Tate/*
24f33878f208e729cf96b708c57686555d2493129Christopher Tate * Copyright (C) 2011 The Android Open Source Project
34f33878f208e729cf96b708c57686555d2493129Christopher Tate *
44f33878f208e729cf96b708c57686555d2493129Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
54f33878f208e729cf96b708c57686555d2493129Christopher Tate * you may not use this file except in compliance with the License.
64f33878f208e729cf96b708c57686555d2493129Christopher Tate * You may obtain a copy of the License at
74f33878f208e729cf96b708c57686555d2493129Christopher Tate *
84f33878f208e729cf96b708c57686555d2493129Christopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
94f33878f208e729cf96b708c57686555d2493129Christopher Tate *
104f33878f208e729cf96b708c57686555d2493129Christopher Tate * Unless required by applicable law or agreed to in writing, software
114f33878f208e729cf96b708c57686555d2493129Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
124f33878f208e729cf96b708c57686555d2493129Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f33878f208e729cf96b708c57686555d2493129Christopher Tate * See the License for the specific language governing permissions and
144f33878f208e729cf96b708c57686555d2493129Christopher Tate * limitations under the License.
154f33878f208e729cf96b708c57686555d2493129Christopher Tate */
164f33878f208e729cf96b708c57686555d2493129Christopher Tate
174f33878f208e729cf96b708c57686555d2493129Christopher Tatepackage com.android.settings;
184f33878f208e729cf96b708c57686555d2493129Christopher Tate
194f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.app.Activity;
204f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.app.backup.IBackupManager;
214f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.os.Bundle;
224f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.os.RemoteException;
234f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.os.ServiceManager;
244f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.util.Log;
254f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.view.View;
264f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.view.View.OnClickListener;
274f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.widget.Button;
284f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.widget.TextView;
294f33878f208e729cf96b708c57686555d2493129Christopher Tateimport android.widget.Toast;
304f33878f208e729cf96b708c57686555d2493129Christopher Tate
314f33878f208e729cf96b708c57686555d2493129Christopher Tatepublic class SetFullBackupPassword extends Activity {
324f33878f208e729cf96b708c57686555d2493129Christopher Tate    static final String TAG = "SetFullBackupPassword";
334f33878f208e729cf96b708c57686555d2493129Christopher Tate
344f33878f208e729cf96b708c57686555d2493129Christopher Tate    IBackupManager mBackupManager;
354f33878f208e729cf96b708c57686555d2493129Christopher Tate    TextView mCurrentPw, mNewPw, mConfirmNewPw;
364f33878f208e729cf96b708c57686555d2493129Christopher Tate    Button mCancel, mSet;
374f33878f208e729cf96b708c57686555d2493129Christopher Tate
384f33878f208e729cf96b708c57686555d2493129Christopher Tate    OnClickListener mButtonListener = new OnClickListener() {
394f33878f208e729cf96b708c57686555d2493129Christopher Tate        @Override
404f33878f208e729cf96b708c57686555d2493129Christopher Tate        public void onClick(View v) {
414f33878f208e729cf96b708c57686555d2493129Christopher Tate            if (v == mSet) {
424f33878f208e729cf96b708c57686555d2493129Christopher Tate                final String curPw = mCurrentPw.getText().toString();
434f33878f208e729cf96b708c57686555d2493129Christopher Tate                final String newPw = mNewPw.getText().toString();
444f33878f208e729cf96b708c57686555d2493129Christopher Tate                final String confirmPw = mConfirmNewPw.getText().toString();
454f33878f208e729cf96b708c57686555d2493129Christopher Tate
464f33878f208e729cf96b708c57686555d2493129Christopher Tate                if (!newPw.equals(confirmPw)) {
474f33878f208e729cf96b708c57686555d2493129Christopher Tate                    // Mismatch between new pw and its confirmation re-entry
484f33878f208e729cf96b708c57686555d2493129Christopher TateLog.i(TAG, "password mismatch");
494f33878f208e729cf96b708c57686555d2493129Christopher Tate                    Toast.makeText(SetFullBackupPassword.this,
502d6497d2d19f0cddc488ea4362221b7e08481c30Christopher Tate                            R.string.local_backup_password_toast_confirmation_mismatch,
514f33878f208e729cf96b708c57686555d2493129Christopher Tate                            Toast.LENGTH_LONG).show();
524f33878f208e729cf96b708c57686555d2493129Christopher Tate                    return;
534f33878f208e729cf96b708c57686555d2493129Christopher Tate                }
544f33878f208e729cf96b708c57686555d2493129Christopher Tate
554f33878f208e729cf96b708c57686555d2493129Christopher Tate                // TODO: should we distinguish cases of has/hasn't set a pw before?
564f33878f208e729cf96b708c57686555d2493129Christopher Tate
574f33878f208e729cf96b708c57686555d2493129Christopher Tate                if (setBackupPassword(curPw, newPw)) {
584f33878f208e729cf96b708c57686555d2493129Christopher Tate                    // success
594f33878f208e729cf96b708c57686555d2493129Christopher TateLog.i(TAG, "password set successfully");
604f33878f208e729cf96b708c57686555d2493129Christopher Tate                    Toast.makeText(SetFullBackupPassword.this,
612d6497d2d19f0cddc488ea4362221b7e08481c30Christopher Tate                            R.string.local_backup_password_toast_success,
624f33878f208e729cf96b708c57686555d2493129Christopher Tate                            Toast.LENGTH_LONG).show();
634f33878f208e729cf96b708c57686555d2493129Christopher Tate                    finish();
644f33878f208e729cf96b708c57686555d2493129Christopher Tate                } else {
654f33878f208e729cf96b708c57686555d2493129Christopher Tate                    // failure -- bad existing pw, usually
664f33878f208e729cf96b708c57686555d2493129Christopher TateLog.i(TAG, "failure; password mismatch?");
674f33878f208e729cf96b708c57686555d2493129Christopher Tate                    Toast.makeText(SetFullBackupPassword.this,
682d6497d2d19f0cddc488ea4362221b7e08481c30Christopher Tate                            R.string.local_backup_password_toast_validation_failure,
694f33878f208e729cf96b708c57686555d2493129Christopher Tate                            Toast.LENGTH_LONG).show();
704f33878f208e729cf96b708c57686555d2493129Christopher Tate                }
714f33878f208e729cf96b708c57686555d2493129Christopher Tate            } else if (v == mCancel) {
724f33878f208e729cf96b708c57686555d2493129Christopher Tate                finish();
734f33878f208e729cf96b708c57686555d2493129Christopher Tate            } else {
744f33878f208e729cf96b708c57686555d2493129Christopher Tate                Log.w(TAG, "Click on unknown view");
754f33878f208e729cf96b708c57686555d2493129Christopher Tate            }
764f33878f208e729cf96b708c57686555d2493129Christopher Tate        }
774f33878f208e729cf96b708c57686555d2493129Christopher Tate    };
784f33878f208e729cf96b708c57686555d2493129Christopher Tate
794f33878f208e729cf96b708c57686555d2493129Christopher Tate    @Override
804f33878f208e729cf96b708c57686555d2493129Christopher Tate    public void onCreate(Bundle icicle) {
814f33878f208e729cf96b708c57686555d2493129Christopher Tate        super.onCreate(icicle);
824f33878f208e729cf96b708c57686555d2493129Christopher Tate
834f33878f208e729cf96b708c57686555d2493129Christopher Tate        mBackupManager = IBackupManager.Stub.asInterface(ServiceManager.getService("backup"));
844f33878f208e729cf96b708c57686555d2493129Christopher Tate
854f33878f208e729cf96b708c57686555d2493129Christopher Tate        setContentView(R.layout.set_backup_pw);
864f33878f208e729cf96b708c57686555d2493129Christopher Tate
874f33878f208e729cf96b708c57686555d2493129Christopher Tate        mCurrentPw = (TextView) findViewById(R.id.current_backup_pw);
884f33878f208e729cf96b708c57686555d2493129Christopher Tate        mNewPw = (TextView) findViewById(R.id.new_backup_pw);
894f33878f208e729cf96b708c57686555d2493129Christopher Tate        mConfirmNewPw = (TextView) findViewById(R.id.confirm_new_backup_pw);
904f33878f208e729cf96b708c57686555d2493129Christopher Tate
914f33878f208e729cf96b708c57686555d2493129Christopher Tate        mCancel = (Button) findViewById(R.id.backup_pw_cancel_button);
924f33878f208e729cf96b708c57686555d2493129Christopher Tate        mSet = (Button) findViewById(R.id.backup_pw_set_button);
934f33878f208e729cf96b708c57686555d2493129Christopher Tate
944f33878f208e729cf96b708c57686555d2493129Christopher Tate        mCancel.setOnClickListener(mButtonListener);
954f33878f208e729cf96b708c57686555d2493129Christopher Tate        mSet.setOnClickListener(mButtonListener);
964f33878f208e729cf96b708c57686555d2493129Christopher Tate    }
974f33878f208e729cf96b708c57686555d2493129Christopher Tate
984f33878f208e729cf96b708c57686555d2493129Christopher Tate    private boolean setBackupPassword(String currentPw, String newPw) {
994f33878f208e729cf96b708c57686555d2493129Christopher Tate        try {
1004f33878f208e729cf96b708c57686555d2493129Christopher Tate            return mBackupManager.setBackupPassword(currentPw, newPw);
1014f33878f208e729cf96b708c57686555d2493129Christopher Tate        } catch (RemoteException e) {
1024f33878f208e729cf96b708c57686555d2493129Christopher Tate            Log.e(TAG, "Unable to communicate with backup manager");
1034f33878f208e729cf96b708c57686555d2493129Christopher Tate            return false;
1044f33878f208e729cf96b708c57686555d2493129Christopher Tate        }
1054f33878f208e729cf96b708c57686555d2493129Christopher Tate    }
1064f33878f208e729cf96b708c57686555d2493129Christopher Tate}
107