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.Fragment;
21import android.app.StatusBarManager;
22import android.content.Context;
23import android.content.Intent;
24import android.os.Bundle;
25import android.os.Handler;
26import android.os.IBinder;
27import android.os.ServiceManager;
28import android.os.storage.IMountService;
29import android.util.Log;
30import android.view.LayoutInflater;
31import android.view.View;
32import android.view.ViewGroup;
33import android.widget.Button;
34
35public class CryptKeeperConfirm extends Fragment {
36
37    public static class Blank extends Activity {
38        private Handler mHandler = new Handler();
39
40        @Override
41        public void onCreate(Bundle savedInstanceState) {
42            super.onCreate(savedInstanceState);
43
44            setContentView(R.layout.crypt_keeper_blank);
45
46            if (Utils.isMonkeyRunning()) {
47                finish();
48            }
49
50            StatusBarManager sbm = (StatusBarManager) getSystemService(Context.STATUS_BAR_SERVICE);
51            sbm.disable(StatusBarManager.DISABLE_EXPAND
52                    | StatusBarManager.DISABLE_NOTIFICATION_ICONS
53                    | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
54                    | StatusBarManager.DISABLE_SYSTEM_INFO
55                    | StatusBarManager.DISABLE_HOME
56                    | StatusBarManager.DISABLE_SEARCH
57                    | StatusBarManager.DISABLE_RECENT
58                    | StatusBarManager.DISABLE_BACK);
59
60            // Post a delayed message in 700 milliseconds to enable encryption.
61            // NOTE: The animation on this activity is set for 500 milliseconds
62            // I am giving it a little extra time to complete.
63            mHandler.postDelayed(new Runnable() {
64                public void run() {
65                    IBinder service = ServiceManager.getService("mount");
66                    if (service == null) {
67                        Log.e("CryptKeeper", "Failed to find the mount service");
68                        finish();
69                        return;
70                    }
71
72                    IMountService mountService = IMountService.Stub.asInterface(service);
73                    try {
74                        Bundle args = getIntent().getExtras();
75                        mountService.encryptStorage(args.getString("password"));
76                    } catch (Exception e) {
77                        Log.e("CryptKeeper", "Error while encrypting...", e);
78                    }
79                }
80            }, 700);
81        }
82    }
83
84    private View mContentView;
85    private Button mFinalButton;
86    private Button.OnClickListener mFinalClickListener = new Button.OnClickListener() {
87
88        public void onClick(View v) {
89            if (Utils.isMonkeyRunning()) {
90                return;
91            }
92
93            Intent intent = new Intent(getActivity(), Blank.class);
94            intent.putExtras(getArguments());
95
96            startActivity(intent);
97        }
98    };
99
100    private void establishFinalConfirmationState() {
101        mFinalButton = (Button) mContentView.findViewById(R.id.execute_encrypt);
102        mFinalButton.setOnClickListener(mFinalClickListener);
103    }
104
105    @Override
106    public View onCreateView(LayoutInflater inflater, ViewGroup container,
107            Bundle savedInstanceState) {
108        mContentView = inflater.inflate(R.layout.crypt_keeper_confirm, null);
109        establishFinalConfirmationState();
110        return mContentView;
111    }
112}
113