blob: 4c039453a4092f7057ba34e0e1bdd1996e0f0351 [file] [log] [blame]
cretin450328b872015-01-15 16:04:44 -08001/*
2 * Copyright (C) 2013 The CyanogenMod Project
Joey Rizzolid1331882016-12-29 17:28:02 +01003 * Copyright (C) 2017 The LineageOS Project
cretin450328b872015-01-15 16:04:44 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Maarten Derkse901e0c2017-05-22 11:15:44 +020018package com.fairphone.setupwizard.setup;
cretin450328b872015-01-15 16:04:44 -080019
20import android.app.Activity;
cretin45e001f972015-02-19 13:01:28 -080021import android.app.ActivityOptions;
cretin451fcde392015-01-23 14:04:04 -080022import android.app.FragmentManager;
cretin4501f21da2015-02-23 13:52:37 -080023import android.content.Context;
cretin450328b872015-01-15 16:04:44 -080024import android.content.Intent;
cretin45ca633262016-02-26 12:15:00 -080025import android.net.CaptivePortal;
cretin45e001f972015-02-19 13:01:28 -080026import android.net.ConnectivityManager;
cretin45ca633262016-02-26 12:15:00 -080027import android.net.ICaptivePortal;
cretin451fcde392015-01-23 14:04:04 -080028import android.os.Bundle;
cretin45e001f972015-02-19 13:01:28 -080029import android.os.Handler;
30import android.provider.Settings;
31import android.util.Log;
cretin450328b872015-01-15 16:04:44 -080032
Maarten Derkse901e0c2017-05-22 11:15:44 +020033import com.fairphone.setupwizard.R;
34import com.fairphone.setupwizard.SetupWizardApp;
35import com.fairphone.setupwizard.ui.LoadingFragment;
36import com.fairphone.setupwizard.ui.SetupPageFragment;
37import com.fairphone.setupwizard.util.SetupWizardUtils;
cretin450328b872015-01-15 16:04:44 -080038
cretin45e001f972015-02-19 13:01:28 -080039import java.io.IOException;
40import java.net.HttpURLConnection;
41import java.net.MalformedURLException;
42import java.net.URL;
Brandon McAnsheff74272015-06-27 16:41:39 -040043import java.util.Random;
cretin45e001f972015-02-19 13:01:28 -080044
cretin450328b872015-01-15 16:04:44 -080045public class WifiSetupPage extends SetupPage {
46
47 public static final String TAG = "WifiSetupPage";
48
cretin45e001f972015-02-19 13:01:28 -080049 private static final String DEFAULT_SERVER = "clients3.google.com";
50 private static final int CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS = 10000;
51
cretin45e001f972015-02-19 13:01:28 -080052 private LoadingFragment mLoadingFragment;
53
54 private URL mCaptivePortalUrl;
55
56 private boolean mIsCaptivePortal = false;
57
58 private final Handler mHandler = new Handler();
59
Brandon McAnsheff74272015-06-27 16:41:39 -040060 private String mResponseToken;
61
cretin45e001f972015-02-19 13:01:28 -080062 private Runnable mFinishCaptivePortalCheckRunnable = new Runnable() {
63 @Override
64 public void run() {
cretin45e001f972015-02-19 13:01:28 -080065 if (mIsCaptivePortal) {
66 try {
Brandon McAnsheff74272015-06-27 16:41:39 -040067 mResponseToken = String.valueOf(new Random().nextLong());
cretin45ca633262016-02-26 12:15:00 -080068 final Intent intent = new Intent(
69 ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN);
70 intent.putExtra(Intent.EXTRA_TEXT, mResponseToken);
71 intent.putExtra(ConnectivityManager.EXTRA_NETWORK,
72 ConnectivityManager.from(mContext)
73 .getNetworkForType(ConnectivityManager.TYPE_WIFI));
74 intent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL,
75 new CaptivePortal(new ICaptivePortal.Stub() {
76 @Override
77 public void appResponse(int response) {}
78 }));
cretin45e001f972015-02-19 13:01:28 -080079 intent.putExtra("status_bar_color",
80 mContext.getResources().getColor(R.color.primary_dark));
81 intent.putExtra("action_bar_color", mContext.getResources().getColor(
82 R.color.primary_dark));
cretin45ad4b36e2015-02-19 15:11:33 -080083 intent.putExtra("progress_bar_color", mContext.getResources().getColor(
84 R.color.accent));
cretin45e001f972015-02-19 13:01:28 -080085 ActivityOptions options =
86 ActivityOptions.makeCustomAnimation(mContext,
87 android.R.anim.fade_in,
88 android.R.anim.fade_out);
cretin4501f21da2015-02-23 13:52:37 -080089 mLoadingFragment.startActivityForResult(intent,
cretin45e001f972015-02-19 13:01:28 -080090 SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL,
91 options.toBundle());
92 } catch (Exception e) {
93 //Oh well
94 Log.e(TAG, "No captive portal activity found" + e);
cretin4501f21da2015-02-23 13:52:37 -080095 if (getCallbacks().isCurrentPage(WifiSetupPage.this)) {
cretin45d385c7e2015-02-23 15:58:21 -080096 getCallbacks().onNextPage();
97 }
cretin45e001f972015-02-19 13:01:28 -080098 }
99 } else {
cretin4501f21da2015-02-23 13:52:37 -0800100 if (getCallbacks().isCurrentPage(WifiSetupPage.this)) {
cretin45d385c7e2015-02-23 15:58:21 -0800101 getCallbacks().onNextPage();
102 }
cretin45e001f972015-02-19 13:01:28 -0800103 }
104 }
105 };
106
cretin4501f21da2015-02-23 13:52:37 -0800107 public WifiSetupPage(Context context, SetupDataCallbacks callbacks) {
cretin450328b872015-01-15 16:04:44 -0800108 super(context, callbacks);
cretin45e001f972015-02-19 13:01:28 -0800109 String server = Settings.Global.getString(context.getContentResolver(), "captive_portal_server");
110 if (server == null) server = DEFAULT_SERVER;
111 try {
112 mCaptivePortalUrl = new URL("http://" + server + "/generate_204");
113 } catch (MalformedURLException e) {
114 Log.e(TAG, "Not a valid url" + e);
115 }
cretin450328b872015-01-15 16:04:44 -0800116 }
117
118 @Override
cretin4501f21da2015-02-23 13:52:37 -0800119 public SetupPageFragment getFragment(FragmentManager fragmentManager, int action) {
cretin45e001f972015-02-19 13:01:28 -0800120 mLoadingFragment = (LoadingFragment)fragmentManager.findFragmentByTag(getKey());
121 if (mLoadingFragment == null) {
cretin451fcde392015-01-23 14:04:04 -0800122 Bundle args = new Bundle();
123 args.putString(Page.KEY_PAGE_ARGUMENT, getKey());
124 args.putInt(Page.KEY_PAGE_ACTION, action);
cretin45e001f972015-02-19 13:01:28 -0800125 mLoadingFragment = new LoadingFragment();
126 mLoadingFragment.setArguments(args);
cretin451fcde392015-01-23 14:04:04 -0800127 }
cretin45e001f972015-02-19 13:01:28 -0800128 return mLoadingFragment;
cretin451fcde392015-01-23 14:04:04 -0800129 }
130
131 @Override
cretin450328b872015-01-15 16:04:44 -0800132 public int getNextButtonTitleResId() {
133 return R.string.skip;
134 }
135
136 @Override
137 public String getKey() {
138 return TAG;
139 }
140
141 @Override
142 public int getTitleResId() {
cretin45e001f972015-02-19 13:01:28 -0800143 return R.string.loading;
cretin450328b872015-01-15 16:04:44 -0800144 }
145
Joey Rizzolid1331882016-12-29 17:28:02 +0100146 @Override
147 public int getIconResId() {
148 return -1;
149 }
cretin45dbc1ceb2015-03-17 15:18:16 -0700150
cretin450328b872015-01-15 16:04:44 -0800151 @Override
cretin45dbc1ceb2015-03-17 15:18:16 -0700152 public void doLoadAction(FragmentManager fragmentManager, int action) {
153 super.doLoadAction(fragmentManager, action);
cretin4501f21da2015-02-23 13:52:37 -0800154 launchWifiSetup();
cretin450328b872015-01-15 16:04:44 -0800155 }
156
157 @Override
158 public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
cretin45e001f972015-02-19 13:01:28 -0800159 if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_WIFI) {
160 if (resultCode == Activity.RESULT_CANCELED) {
cretin45ef4cd782015-03-04 13:06:26 -0800161 getCallbacks().onPreviousPage();
cretin45e001f972015-02-19 13:01:28 -0800162 } else if (resultCode == Activity.RESULT_OK) {
163 checkForCaptivePortal();
164 } else {
165 getCallbacks().onNextPage();
166 }
167 } else if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL) {
cretin4597af6852015-08-26 13:18:21 -0700168 if (data == null) {
169 launchWifiSetup();
170 return true;
171 }
Brandon McAnsheff74272015-06-27 16:41:39 -0400172 String token = data.getStringExtra("response_token");
173 if (token != null && !token.equals(mResponseToken)) {
cretin4501f21da2015-02-23 13:52:37 -0800174 launchWifiSetup();
cretin45e001f972015-02-19 13:01:28 -0800175 } else {
Brandon McAnsheff74272015-06-27 16:41:39 -0400176 if (resultCode == Activity.RESULT_CANCELED) {
Brandon McAnsheff74272015-06-27 16:41:39 -0400177 launchWifiSetup();
178 } else {
Brandon McAnsheff74272015-06-27 16:41:39 -0400179 getCallbacks().onNextPage();
180 }
cretin45e001f972015-02-19 13:01:28 -0800181 }
182 } else {
183 return false;
cretin451fcde392015-01-23 14:04:04 -0800184 }
cretin450328b872015-01-15 16:04:44 -0800185 return true;
186 }
cretin45e001f972015-02-19 13:01:28 -0800187
188 private void checkForCaptivePortal() {
189 new Thread() {
190 @Override
191 public void run() {
192 mIsCaptivePortal = isCaptivePortal();
193 mHandler.post(mFinishCaptivePortalCheckRunnable);
194 }
195 }.start();
196 }
197
198 // Don't run on UI thread
199 private boolean isCaptivePortal() {
200 if (mCaptivePortalUrl == null) return false;
201 HttpURLConnection urlConnection = null;
202 try {
203 urlConnection = (HttpURLConnection) mCaptivePortalUrl.openConnection();
204 urlConnection.setInstanceFollowRedirects(false);
205 urlConnection.setConnectTimeout(CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS);
206 urlConnection.setReadTimeout(CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS);
207 urlConnection.setUseCaches(false);
208 urlConnection.getInputStream();
209 // We got a valid response, but not from the real google
cretin4501f21da2015-02-23 13:52:37 -0800210 final int responseCode = urlConnection.getResponseCode();
211 if (responseCode == 408 || responseCode == 504) {
212 // If we timeout here, we'll try and go through captive portal login
213 return true;
214 }
cretin45e001f972015-02-19 13:01:28 -0800215 return urlConnection.getResponseCode() != 204;
216 } catch (IOException e) {
217 Log.e(TAG, "Captive portal check - probably not a portal: exception "
218 + e);
219 return false;
220 } finally {
221 if (urlConnection != null) {
222 urlConnection.disconnect();
223 }
224 }
225 }
cretin4501f21da2015-02-23 13:52:37 -0800226
227 private void launchWifiSetup() {
228 SetupWizardUtils.tryEnablingWifi(mContext);
229 Intent intent = new Intent(SetupWizardApp.ACTION_SETUP_WIFI);
dhacker29de061812015-04-23 01:30:19 -0400230 if (SetupWizardUtils.hasLeanback(mContext)) {
231 intent.setComponent(SetupWizardUtils.mTvwifisettingsActivity);
232 }
Bharath90d44512021-01-14 18:39:05 +0530233 intent.putExtra(SetupWizardApp.EXTRA_PREFS_SHOW_BUTTON_BAR, true);
234 intent.putExtra(SetupWizardApp.EXTRA_PREFS_SET_BACK_TEXT , (String) null);
cretin4501f21da2015-02-23 13:52:37 -0800235 ActivityOptions options =
236 ActivityOptions.makeCustomAnimation(mContext,
237 android.R.anim.fade_in,
238 android.R.anim.fade_out);
239 mLoadingFragment.startActivityForResult(intent,
240 SetupWizardApp.REQUEST_CODE_SETUP_WIFI, options.toBundle());
241 }
cretin450328b872015-01-15 16:04:44 -0800242}