cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The CyanogenMod Project |
Joey Rizzoli | d133188 | 2016-12-29 17:28:02 +0100 | [diff] [blame] | 3 | * Copyright (C) 2017 The LineageOS Project |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 4 | * |
| 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 Derks | e901e0c | 2017-05-22 11:15:44 +0200 | [diff] [blame] | 18 | package com.fairphone.setupwizard.setup; |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 19 | |
| 20 | import android.app.Activity; |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 21 | import android.app.ActivityOptions; |
cretin45 | 1fcde39 | 2015-01-23 14:04:04 -0800 | [diff] [blame] | 22 | import android.app.FragmentManager; |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 23 | import android.content.Context; |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
cretin45 | ca63326 | 2016-02-26 12:15:00 -0800 | [diff] [blame] | 25 | import android.net.CaptivePortal; |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 26 | import android.net.ConnectivityManager; |
cretin45 | ca63326 | 2016-02-26 12:15:00 -0800 | [diff] [blame] | 27 | import android.net.ICaptivePortal; |
cretin45 | 1fcde39 | 2015-01-23 14:04:04 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.provider.Settings; |
| 31 | import android.util.Log; |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 32 | |
Maarten Derks | e901e0c | 2017-05-22 11:15:44 +0200 | [diff] [blame] | 33 | import com.fairphone.setupwizard.R; |
| 34 | import com.fairphone.setupwizard.SetupWizardApp; |
| 35 | import com.fairphone.setupwizard.ui.LoadingFragment; |
| 36 | import com.fairphone.setupwizard.ui.SetupPageFragment; |
| 37 | import com.fairphone.setupwizard.util.SetupWizardUtils; |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 38 | |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 39 | import java.io.IOException; |
| 40 | import java.net.HttpURLConnection; |
| 41 | import java.net.MalformedURLException; |
| 42 | import java.net.URL; |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 43 | import java.util.Random; |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 44 | |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 45 | public class WifiSetupPage extends SetupPage { |
| 46 | |
| 47 | public static final String TAG = "WifiSetupPage"; |
| 48 | |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 49 | private static final String DEFAULT_SERVER = "clients3.google.com"; |
| 50 | private static final int CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS = 10000; |
| 51 | |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 52 | private LoadingFragment mLoadingFragment; |
| 53 | |
| 54 | private URL mCaptivePortalUrl; |
| 55 | |
| 56 | private boolean mIsCaptivePortal = false; |
| 57 | |
| 58 | private final Handler mHandler = new Handler(); |
| 59 | |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 60 | private String mResponseToken; |
| 61 | |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 62 | private Runnable mFinishCaptivePortalCheckRunnable = new Runnable() { |
| 63 | @Override |
| 64 | public void run() { |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 65 | if (mIsCaptivePortal) { |
| 66 | try { |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 67 | mResponseToken = String.valueOf(new Random().nextLong()); |
cretin45 | ca63326 | 2016-02-26 12:15:00 -0800 | [diff] [blame] | 68 | 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 | })); |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 79 | 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)); |
cretin45 | ad4b36e | 2015-02-19 15:11:33 -0800 | [diff] [blame] | 83 | intent.putExtra("progress_bar_color", mContext.getResources().getColor( |
| 84 | R.color.accent)); |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 85 | ActivityOptions options = |
| 86 | ActivityOptions.makeCustomAnimation(mContext, |
| 87 | android.R.anim.fade_in, |
| 88 | android.R.anim.fade_out); |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 89 | mLoadingFragment.startActivityForResult(intent, |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 90 | 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); |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 95 | if (getCallbacks().isCurrentPage(WifiSetupPage.this)) { |
cretin45 | d385c7e | 2015-02-23 15:58:21 -0800 | [diff] [blame] | 96 | getCallbacks().onNextPage(); |
| 97 | } |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 98 | } |
| 99 | } else { |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 100 | if (getCallbacks().isCurrentPage(WifiSetupPage.this)) { |
cretin45 | d385c7e | 2015-02-23 15:58:21 -0800 | [diff] [blame] | 101 | getCallbacks().onNextPage(); |
| 102 | } |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | }; |
| 106 | |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 107 | public WifiSetupPage(Context context, SetupDataCallbacks callbacks) { |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 108 | super(context, callbacks); |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 109 | 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 | } |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 119 | public SetupPageFragment getFragment(FragmentManager fragmentManager, int action) { |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 120 | mLoadingFragment = (LoadingFragment)fragmentManager.findFragmentByTag(getKey()); |
| 121 | if (mLoadingFragment == null) { |
cretin45 | 1fcde39 | 2015-01-23 14:04:04 -0800 | [diff] [blame] | 122 | Bundle args = new Bundle(); |
| 123 | args.putString(Page.KEY_PAGE_ARGUMENT, getKey()); |
| 124 | args.putInt(Page.KEY_PAGE_ACTION, action); |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 125 | mLoadingFragment = new LoadingFragment(); |
| 126 | mLoadingFragment.setArguments(args); |
cretin45 | 1fcde39 | 2015-01-23 14:04:04 -0800 | [diff] [blame] | 127 | } |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 128 | return mLoadingFragment; |
cretin45 | 1fcde39 | 2015-01-23 14:04:04 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @Override |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 132 | 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() { |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 143 | return R.string.loading; |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Joey Rizzoli | d133188 | 2016-12-29 17:28:02 +0100 | [diff] [blame] | 146 | @Override |
| 147 | public int getIconResId() { |
| 148 | return -1; |
| 149 | } |
cretin45 | dbc1ceb | 2015-03-17 15:18:16 -0700 | [diff] [blame] | 150 | |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 151 | @Override |
cretin45 | dbc1ceb | 2015-03-17 15:18:16 -0700 | [diff] [blame] | 152 | public void doLoadAction(FragmentManager fragmentManager, int action) { |
| 153 | super.doLoadAction(fragmentManager, action); |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 154 | launchWifiSetup(); |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public boolean onActivityResult(int requestCode, int resultCode, Intent data) { |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 159 | if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_WIFI) { |
| 160 | if (resultCode == Activity.RESULT_CANCELED) { |
cretin45 | ef4cd78 | 2015-03-04 13:06:26 -0800 | [diff] [blame] | 161 | getCallbacks().onPreviousPage(); |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 162 | } else if (resultCode == Activity.RESULT_OK) { |
| 163 | checkForCaptivePortal(); |
| 164 | } else { |
| 165 | getCallbacks().onNextPage(); |
| 166 | } |
| 167 | } else if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL) { |
cretin45 | 97af685 | 2015-08-26 13:18:21 -0700 | [diff] [blame] | 168 | if (data == null) { |
| 169 | launchWifiSetup(); |
| 170 | return true; |
| 171 | } |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 172 | String token = data.getStringExtra("response_token"); |
| 173 | if (token != null && !token.equals(mResponseToken)) { |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 174 | launchWifiSetup(); |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 175 | } else { |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 176 | if (resultCode == Activity.RESULT_CANCELED) { |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 177 | launchWifiSetup(); |
| 178 | } else { |
Brandon McAnsh | eff7427 | 2015-06-27 16:41:39 -0400 | [diff] [blame] | 179 | getCallbacks().onNextPage(); |
| 180 | } |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 181 | } |
| 182 | } else { |
| 183 | return false; |
cretin45 | 1fcde39 | 2015-01-23 14:04:04 -0800 | [diff] [blame] | 184 | } |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 185 | return true; |
| 186 | } |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 187 | |
| 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 |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 210 | 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 | } |
cretin45 | e001f97 | 2015-02-19 13:01:28 -0800 | [diff] [blame] | 215 | 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 | } |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 226 | |
| 227 | private void launchWifiSetup() { |
| 228 | SetupWizardUtils.tryEnablingWifi(mContext); |
| 229 | Intent intent = new Intent(SetupWizardApp.ACTION_SETUP_WIFI); |
dhacker29 | de06181 | 2015-04-23 01:30:19 -0400 | [diff] [blame] | 230 | if (SetupWizardUtils.hasLeanback(mContext)) { |
| 231 | intent.setComponent(SetupWizardUtils.mTvwifisettingsActivity); |
| 232 | } |
Bharath | 90d4451 | 2021-01-14 18:39:05 +0530 | [diff] [blame] | 233 | intent.putExtra(SetupWizardApp.EXTRA_PREFS_SHOW_BUTTON_BAR, true); |
| 234 | intent.putExtra(SetupWizardApp.EXTRA_PREFS_SET_BACK_TEXT , (String) null); |
cretin45 | 01f21da | 2015-02-23 13:52:37 -0800 | [diff] [blame] | 235 | 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 | } |
cretin45 | 0328b87 | 2015-01-15 16:04:44 -0800 | [diff] [blame] | 242 | } |