1 /*
2  * Copyright (C) 2018 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 package com.android.settings.wifi;
17 
18 import android.app.Activity;
19 import android.app.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.content.ActivityNotFoundException;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.Intent;
26 import android.net.wifi.WifiManager;
27 import android.os.Bundle;
28 import android.text.TextUtils;
29 import android.util.Log;
30 import android.widget.Toast;
31 
32 import androidx.annotation.VisibleForTesting;
33 import androidx.appcompat.app.AlertDialog;
34 
35 import com.android.settings.R;
36 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
37 import com.android.settingslib.HelpUtils;
38 
39 public class WifiScanningRequiredFragment extends InstrumentedDialogFragment implements
40         DialogInterface.OnClickListener {
41 
42     private static final String TAG = "WifiScanReqFrag";
43 
newInstance()44     public static WifiScanningRequiredFragment newInstance() {
45         WifiScanningRequiredFragment fragment = new WifiScanningRequiredFragment();
46         return fragment;
47     }
48 
49     @Override
onCreateDialog(Bundle savedInstanceState)50     public Dialog onCreateDialog(Bundle savedInstanceState) {
51         AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
52                 .setTitle(R.string.wifi_settings_scanning_required_title)
53                 .setView(R.layout.wifi_settings_scanning_required_view)
54                 .setPositiveButton(R.string.wifi_settings_scanning_required_turn_on, this)
55                 .setNegativeButton(R.string.cancel, null);
56         addButtonIfNeeded(builder);
57 
58         return builder.create();
59     }
60 
61     @Override
getMetricsCategory()62     public int getMetricsCategory() {
63         return SettingsEnums.WIFI_SCANNING_NEEDED_DIALOG;
64     }
65 
66     @Override
onClick(DialogInterface dialog, int which)67     public void onClick(DialogInterface dialog, int which) {
68         Context context = getContext();
69         ContentResolver contentResolver = context.getContentResolver();
70         switch(which) {
71             case DialogInterface.BUTTON_POSITIVE:
72                 context.getSystemService(WifiManager.class).setScanAlwaysAvailable(true);
73                 Toast.makeText(
74                         context,
75                         context.getString(R.string.wifi_settings_scanning_required_enabled),
76                         Toast.LENGTH_SHORT).show();
77                 getTargetFragment().onActivityResult(
78                         getTargetRequestCode(),
79                         Activity.RESULT_OK,
80                         null);
81                 break;
82             case DialogInterface.BUTTON_NEUTRAL:
83                 openHelpPage();
84                 break;
85             case DialogInterface.BUTTON_NEGATIVE:
86             default:
87                 // do nothing
88         }
89     }
90 
addButtonIfNeeded(AlertDialog.Builder builder)91     void addButtonIfNeeded(AlertDialog.Builder builder) {
92         // Only show "learn more" if there is a help page to show
93         if (!TextUtils.isEmpty(getContext().getString(R.string.help_uri_wifi_scanning_required))) {
94             builder.setNeutralButton(R.string.learn_more, this);
95         }
96     }
97 
openHelpPage()98     private void openHelpPage() {
99         Intent intent = getHelpIntent(getContext());
100         if (intent != null) {
101             try {
102                 getActivity().startActivityForResult(intent, 0);
103             } catch (ActivityNotFoundException e) {
104                 Log.e(TAG, "Activity was not found for intent, " + intent.toString());
105             }
106         }
107     }
108 
109     @VisibleForTesting
getHelpIntent(Context context)110     Intent getHelpIntent(Context context) {
111         return HelpUtils.getHelpIntent(
112                     context,
113                     context.getString(R.string.help_uri_wifi_scanning_required),
114                     context.getClass().getName());
115     }
116 }
117