summaryrefslogtreecommitdiff
path: root/src/com/android/settings/wifi/RequestToggleWiFiActivity.java
blob: 6887b7802d09d0826a4e8d4ef755669a7cc52275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings.wifi;

import static android.view.WindowManager.LayoutParams.SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.IActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageItemInfo;
import android.content.pm.PackageManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.RemoteException;
import android.text.TextUtils;
import android.util.Log;

import androidx.annotation.NonNull;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.app.AlertActivity;
import com.android.settings.R;

/**
 * This activity handles requests to toggle WiFi by collecting user
 * consent and waiting until the state change is completed.
 */
public class RequestToggleWiFiActivity extends AlertActivity
        implements DialogInterface.OnClickListener {
    private static final String LOG_TAG = "RequestToggleWiFiActivity";

    private static final long TOGGLE_TIMEOUT_MILLIS = 10000; // 10 sec

    private static final int STATE_UNKNOWN = -1;
    private static final int STATE_ENABLE = 1;
    private static final int STATE_ENABLING = 2;
    private static final int STATE_DISABLE = 3;
    private static final int STATE_DISABLING = 4;

    private final StateChangeReceiver mReceiver = new StateChangeReceiver();

    private final Runnable mTimeoutCommand = () -> {
        if (!isFinishing() && !isDestroyed()) {
            finish();
        }
    };

    private @NonNull WifiManager mWiFiManager;
    private @NonNull CharSequence mAppLabel;
    @VisibleForTesting
    protected IActivityManager mActivityManager = ActivityManager.getService();

    private int mState = STATE_UNKNOWN;
    private int mLastUpdateState = STATE_UNKNOWN;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addSystemFlags(SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
        mWiFiManager = getSystemService(WifiManager.class);

        setResult(Activity.RESULT_CANCELED);

        mAppLabel = getAppLabel();
        if (TextUtils.isEmpty(mAppLabel)) {
            finish();
            return;
        }

        String action = getIntent().getAction();
        switch (action) {
            case WifiManager.ACTION_REQUEST_ENABLE: {
                mState = STATE_ENABLE;
            } break;

            case WifiManager.ACTION_REQUEST_DISABLE: {
                mState = STATE_DISABLE;
            } break;

            default: {
                finish();
            }
        }
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case DialogInterface.BUTTON_POSITIVE: {
                switch (mState) {
                    case STATE_ENABLE: {
                        mWiFiManager.setWifiEnabled(true);
                        mState = STATE_ENABLING;
                        scheduleToggleTimeout();
                        updateUi();
                    } break;

                    case STATE_DISABLE: {
                        mWiFiManager.setWifiEnabled(false);
                        mState = STATE_DISABLING;
                        scheduleToggleTimeout();
                        updateUi();
                    } break;
                }
            }
            break;
            case DialogInterface.BUTTON_NEGATIVE: {
                finish();
            }
            break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        mReceiver.register();

        final int wifiState = mWiFiManager.getWifiState();

        switch (mState) {
            case STATE_ENABLE: {
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_ENABLED: {
                        setResult(RESULT_OK);
                        finish();
                    } return;

                    case WifiManager.WIFI_STATE_ENABLING: {
                        mState = STATE_ENABLING;
                        scheduleToggleTimeout();
                    } break;
                }
            } break;

            case STATE_DISABLE: {
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_DISABLED: {
                        setResult(RESULT_OK);
                        finish();
                    }
                    return;

                    case WifiManager.WIFI_STATE_ENABLING: {
                        mState = STATE_DISABLING;
                        scheduleToggleTimeout();
                    }
                    break;
                }
            } break;

            case STATE_ENABLING: {
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_ENABLED: {
                        setResult(RESULT_OK);
                        finish();
                    } return;

                    case WifiManager.WIFI_STATE_ENABLING: {
                        scheduleToggleTimeout();
                    } break;

                    case WifiManager.WIFI_STATE_DISABLED:
                    case WifiManager.WIFI_STATE_DISABLING: {
                        mState = STATE_ENABLE;
                    } break;
                }
            } break;

            case STATE_DISABLING: {
                switch (wifiState) {
                    case WifiManager.WIFI_STATE_DISABLED: {
                        setResult(RESULT_OK);
                        finish();
                    } return;

                    case WifiManager.WIFI_STATE_DISABLING: {
                        scheduleToggleTimeout();
                    } break;

                    case WifiManager.WIFI_STATE_ENABLED:
                    case WifiManager.WIFI_STATE_ENABLING: {
                        mState = STATE_DISABLE;
                    } break;
                }
            } break;
        }

        updateUi();
    }

    @Override
    protected void onStop() {
        mReceiver.unregister();
        unscheduleToggleTimeout();
        super.onStop();
    }

    @VisibleForTesting
    protected CharSequence getAppLabel() {
        String packageName;
        try {
            packageName = mActivityManager.getLaunchedFromPackage(getActivityToken());
            if (TextUtils.isEmpty(packageName)) {
                Log.d(LOG_TAG, "Package name is null");
                return null;
            }
        } catch (RemoteException e) {
            Log.e(LOG_TAG, "Can not get the package from activity manager");
            return null;
        }

        try {
            ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(
                    packageName, 0);
            return applicationInfo.loadSafeLabel(getPackageManager(),
                    PackageItemInfo.DEFAULT_MAX_LABEL_SIZE_PX, PackageItemInfo.SAFE_LABEL_FLAG_TRIM
                            | PackageItemInfo.SAFE_LABEL_FLAG_FIRST_LINE);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(LOG_TAG, "Couldn't find app with package name " + packageName);
            return null;
        }
    }

    private void updateUi() {
        if (mLastUpdateState == mState) {
            return;
        }
        mLastUpdateState = mState;

        switch (mState) {
            case STATE_ENABLE: {
                mAlertParams.mPositiveButtonText = getString(R.string.allow);
                mAlertParams.mPositiveButtonListener = this;
                mAlertParams.mNegativeButtonText = getString(R.string.deny);
                mAlertParams.mNegativeButtonListener = this;
                mAlertParams.mMessage = getString(R.string.wifi_ask_enable, mAppLabel);
            } break;

            case STATE_ENABLING: {
                // Params set button text only if non-null, but we want a null
                // button text to hide the button, so reset the controller directly.
                mAlert.setButton(DialogInterface.BUTTON_POSITIVE, null, null, null);
                mAlert.setButton(DialogInterface.BUTTON_NEGATIVE, null, null, null);
                mAlertParams.mPositiveButtonText = null;
                mAlertParams.mPositiveButtonListener = null;
                mAlertParams.mNegativeButtonText = null;
                mAlertParams.mNegativeButtonListener = null;
                mAlertParams.mMessage = getString(R.string.wifi_starting);
            } break;

            case STATE_DISABLE: {
                mAlertParams.mPositiveButtonText = getString(R.string.allow);
                mAlertParams.mPositiveButtonListener = this;
                mAlertParams.mNegativeButtonText = getString(R.string.deny);
                mAlertParams.mNegativeButtonListener = this;
                mAlertParams.mMessage = getString(R.string.wifi_ask_disable, mAppLabel);
            } break;

            case STATE_DISABLING: {
                // Params set button text only if non-null, but we want a null
                // button text to hide the button, so reset the controller directly.
                mAlert.setButton(DialogInterface.BUTTON_POSITIVE, null, null, null);
                mAlert.setButton(DialogInterface.BUTTON_NEGATIVE, null, null, null);
                mAlertParams.mPositiveButtonText = null;
                mAlertParams.mPositiveButtonListener = null;
                mAlertParams.mNegativeButtonText = null;
                mAlertParams.mNegativeButtonListener = null;
                mAlertParams.mMessage = getString(R.string.wifi_stopping);
            } break;
        }

        setupAlert();
    }

    @Override
    public void dismiss() {
        // Clicking on the dialog buttons dismisses the dialog and finishes
        // the activity but we want to finish after the WiFi state changed.
    }

    private void scheduleToggleTimeout() {
        getWindow().getDecorView().postDelayed(mTimeoutCommand, TOGGLE_TIMEOUT_MILLIS);
    }

    private void unscheduleToggleTimeout() {
        getWindow().getDecorView().removeCallbacks(mTimeoutCommand);
    }

    private final class StateChangeReceiver extends BroadcastReceiver {
        private final IntentFilter mFilter = new IntentFilter(
                WifiManager.WIFI_STATE_CHANGED_ACTION);

        public void register() {
            registerReceiver(this, mFilter);
        }

        public void unregister() {
            unregisterReceiver(this);
        }

        public void onReceive(Context context, Intent intent) {
            Activity activity = RequestToggleWiFiActivity.this;
            if (activity.isFinishing() || activity.isDestroyed()) {
                return;
            }
            final int currentState = mWiFiManager.getWifiState();
            switch (currentState) {
                case WifiManager.WIFI_STATE_ENABLED:
                case WifiManager.WIFI_STATE_DISABLED: {
                    if (mState == STATE_ENABLING || mState == STATE_DISABLING) {
                        RequestToggleWiFiActivity.this.setResult(Activity.RESULT_OK);
                        finish();
                    }
                } break;
            }
        }
    }
}