1/*
2 * Copyright (C) 2013 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.wifi;
18
19import com.android.settings.R;
20
21import android.app.Activity;
22import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.DialogFragment;
25import android.content.DialogInterface;
26import android.content.Intent;
27import android.content.pm.ApplicationInfo;
28import android.content.pm.PackageManager;
29import android.net.wifi.WifiManager;
30import android.os.Bundle;
31import android.provider.Settings;
32
33/**
34 * This activity requests users permission to allow scanning even when Wi-Fi is turned off
35 */
36public class WifiScanModeActivity extends Activity {
37    private DialogFragment mDialog;
38    private String mApp;
39
40    @Override
41    protected void onCreate(Bundle savedInstanceState) {
42        super.onCreate(savedInstanceState);
43        Intent intent = getIntent();
44        if (savedInstanceState == null) {
45            if (intent != null && intent.getAction()
46                    .equals(WifiManager.ACTION_REQUEST_SCAN_ALWAYS_AVAILABLE)) {
47                ApplicationInfo ai;
48                mApp = getCallingPackage();
49                try {
50                    PackageManager pm = getPackageManager();
51                    ai = pm.getApplicationInfo(mApp, 0);
52                    mApp = (String)pm.getApplicationLabel(ai);
53                } catch (PackageManager.NameNotFoundException e) { }
54            } else {
55                finish();
56                return;
57            }
58        } else {
59            mApp = savedInstanceState.getString("app");
60        }
61        createDialog();
62    }
63
64    private void createDialog() {
65        if (mDialog == null) {
66            mDialog = AlertDialogFragment.newInstance(mApp);
67            mDialog.show(getFragmentManager(), "dialog");
68        }
69    }
70
71    private void dismissDialog() {
72        if (mDialog != null) {
73            mDialog.dismiss();
74            mDialog = null;
75        }
76    }
77
78    private void doPositiveClick() {
79        Settings.Global.putInt(getContentResolver(),
80                Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 1);
81        setResult(RESULT_OK);
82        finish();
83    }
84
85    private void doNegativeClick() {
86        setResult(RESULT_CANCELED);
87        finish();
88    }
89
90    @Override
91    public void onSaveInstanceState(Bundle outState) {
92        super.onSaveInstanceState(outState);
93        outState.putString("app", mApp);
94    }
95
96    @Override
97    public void onPause() {
98        super.onPause();
99        dismissDialog();
100    }
101
102    public void onResume() {
103        super.onResume();
104        createDialog();
105    }
106
107    public static class AlertDialogFragment extends DialogFragment {
108        static AlertDialogFragment newInstance(String app) {
109            AlertDialogFragment frag = new AlertDialogFragment(app);
110            return frag;
111        }
112
113        private final String mApp;
114        public AlertDialogFragment(String app) {
115            super();
116            mApp = app;
117        }
118
119        public AlertDialogFragment() {
120            super();
121            mApp = null;
122        }
123
124        @Override
125        public Dialog onCreateDialog(Bundle savedInstanceState) {
126            return new AlertDialog.Builder(getActivity())
127                    .setMessage(getString(R.string.wifi_scan_always_turnon_message, mApp))
128                    .setPositiveButton(R.string.wifi_scan_always_confirm_allow,
129                            new DialogInterface.OnClickListener() {
130                                public void onClick(DialogInterface dialog, int whichButton) {
131                                    ((WifiScanModeActivity) getActivity()).doPositiveClick();
132                                }
133                            }
134                    )
135                    .setNegativeButton(R.string.wifi_scan_always_confirm_deny,
136                            new DialogInterface.OnClickListener() {
137                                public void onClick(DialogInterface dialog, int whichButton) {
138                                    ((WifiScanModeActivity) getActivity()).doNegativeClick();
139                                }
140                            }
141                    )
142                    .create();
143        }
144        @Override
145        public void onCancel(DialogInterface dialog) {
146            ((WifiScanModeActivity) getActivity()).doNegativeClick();
147        }
148    }
149}
150