Dirk Vogt | 74af2bf | 2016-05-31 11:21:46 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The CyanogenMod 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 | |
| 17 | package com.fairphone.setupwizard.setup; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.app.ActivityOptions; |
| 21 | import android.app.FragmentManager; |
| 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.net.ConnectivityManager; |
| 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
| 28 | import android.os.Handler; |
| 29 | import android.provider.Settings; |
| 30 | import android.util.Log; |
| 31 | |
| 32 | import com.fairphone.setupwizard.R; |
| 33 | import com.fairphone.setupwizard.SetupWizardApp; |
| 34 | import com.fairphone.setupwizard.ui.LoadingFragment; |
| 35 | import com.fairphone.setupwizard.ui.SetupPageFragment; |
| 36 | import com.fairphone.setupwizard.util.SetupWizardUtils; |
| 37 | |
| 38 | import java.io.IOException; |
| 39 | import java.net.HttpURLConnection; |
| 40 | import java.net.MalformedURLException; |
| 41 | import java.net.URL; |
| 42 | import java.util.Random; |
| 43 | |
| 44 | public class WifiSetupPage extends SetupPage { |
| 45 | |
| 46 | public static final String TAG = "WifiSetupPage"; |
| 47 | |
| 48 | private static final String DEFAULT_SERVER = "clients3.google.com"; |
| 49 | private static final int CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS = 10000; |
| 50 | |
| 51 | private LoadingFragment mLoadingFragment; |
| 52 | |
| 53 | private URL mCaptivePortalUrl; |
| 54 | |
| 55 | private boolean mIsCaptivePortal = false; |
| 56 | |
| 57 | private final Handler mHandler = new Handler(); |
| 58 | |
| 59 | private String mResponseToken; |
| 60 | |
| 61 | private Runnable mFinishCaptivePortalCheckRunnable = new Runnable() { |
| 62 | @Override |
| 63 | public void run() { |
| 64 | if (mIsCaptivePortal) { |
| 65 | try { |
| 66 | int netId = ConnectivityManager.from(mContext) |
| 67 | .getNetworkForType(ConnectivityManager.TYPE_WIFI).netId; |
| 68 | mResponseToken = String.valueOf(new Random().nextLong()); |
| 69 | Intent intent = new Intent(); |
| 70 | intent.setData(Uri.fromParts("netid", Integer.toString(netId), |
| 71 | mResponseToken)); |
| 72 | intent.setComponent(new ComponentName("com.android.captiveportallogin", |
| 73 | "com.android.captiveportallogin.CaptivePortalLoginActivity")); |
| 74 | intent.putExtra(Intent.EXTRA_TEXT, String.valueOf(netId)); |
| 75 | intent.putExtra("status_bar_color", |
| 76 | mContext.getResources().getColor(R.color.primary_dark)); |
| 77 | intent.putExtra("action_bar_color", mContext.getResources().getColor( |
| 78 | R.color.primary_dark)); |
| 79 | intent.putExtra("progress_bar_color", mContext.getResources().getColor( |
| 80 | R.color.accent)); |
| 81 | ActivityOptions options = |
| 82 | ActivityOptions.makeCustomAnimation(mContext, |
| 83 | android.R.anim.fade_in, |
| 84 | android.R.anim.fade_out); |
| 85 | mLoadingFragment.startActivityForResult(intent, |
| 86 | SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL, |
| 87 | options.toBundle()); |
| 88 | } catch (Exception e) { |
| 89 | //Oh well |
| 90 | Log.e(TAG, "No captive portal activity found" + e); |
| 91 | if (getCallbacks().isCurrentPage(WifiSetupPage.this)) { |
| 92 | getCallbacks().onNextPage(); |
| 93 | } |
| 94 | } |
| 95 | } else { |
| 96 | if (getCallbacks().isCurrentPage(WifiSetupPage.this)) { |
| 97 | getCallbacks().onNextPage(); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | public WifiSetupPage(Context context, SetupDataCallbacks callbacks) { |
| 104 | super(context, callbacks); |
| 105 | String server = Settings.Global.getString(context.getContentResolver(), "captive_portal_server"); |
| 106 | if (server == null) server = DEFAULT_SERVER; |
| 107 | try { |
| 108 | mCaptivePortalUrl = new URL("http://" + server + "/generate_204"); |
| 109 | } catch (MalformedURLException e) { |
| 110 | Log.e(TAG, "Not a valid url" + e); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public SetupPageFragment getFragment(FragmentManager fragmentManager, int action) { |
| 116 | mLoadingFragment = (LoadingFragment)fragmentManager.findFragmentByTag(getKey()); |
| 117 | if (mLoadingFragment == null) { |
| 118 | Bundle args = new Bundle(); |
| 119 | args.putString(Page.KEY_PAGE_ARGUMENT, getKey()); |
| 120 | args.putInt(Page.KEY_PAGE_ACTION, action); |
| 121 | mLoadingFragment = new LoadingFragment(); |
| 122 | mLoadingFragment.setArguments(args); |
| 123 | } |
| 124 | return mLoadingFragment; |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public int getNextButtonTitleResId() { |
| 129 | return R.string.skip; |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public String getKey() { |
| 134 | return TAG; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int getTitleResId() { |
| 139 | return R.string.loading; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | @Override |
| 144 | public void doLoadAction(FragmentManager fragmentManager, int action) { |
| 145 | super.doLoadAction(fragmentManager, action); |
| 146 | launchWifiSetup(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean onActivityResult(int requestCode, int resultCode, Intent data) { |
| 151 | if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_WIFI) { |
| 152 | if (resultCode == Activity.RESULT_CANCELED) { |
| 153 | getCallbacks().onPreviousPage(); |
| 154 | } else if (resultCode == Activity.RESULT_OK) { |
| 155 | checkForCaptivePortal(); |
| 156 | } else { |
| 157 | getCallbacks().onNextPage(); |
| 158 | } |
| 159 | } else if (requestCode == SetupWizardApp.REQUEST_CODE_SETUP_CAPTIVE_PORTAL) { |
| 160 | if (data == null) { |
| 161 | launchWifiSetup(); |
| 162 | return true; |
| 163 | } |
| 164 | String token = data.getStringExtra("response_token"); |
| 165 | if (token != null && !token.equals(mResponseToken)) { |
| 166 | launchWifiSetup(); |
| 167 | } else { |
| 168 | if (resultCode == Activity.RESULT_CANCELED) { |
| 169 | launchWifiSetup(); |
| 170 | } else { |
| 171 | getCallbacks().onNextPage(); |
| 172 | } |
| 173 | } |
| 174 | } else { |
| 175 | return false; |
| 176 | } |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | private void checkForCaptivePortal() { |
| 181 | new Thread() { |
| 182 | @Override |
| 183 | public void run() { |
| 184 | mIsCaptivePortal = isCaptivePortal(); |
| 185 | mHandler.post(mFinishCaptivePortalCheckRunnable); |
| 186 | } |
| 187 | }.start(); |
| 188 | } |
| 189 | |
| 190 | // Don't run on UI thread |
| 191 | private boolean isCaptivePortal() { |
| 192 | if (mCaptivePortalUrl == null) return false; |
| 193 | HttpURLConnection urlConnection = null; |
| 194 | try { |
| 195 | urlConnection = (HttpURLConnection) mCaptivePortalUrl.openConnection(); |
| 196 | urlConnection.setInstanceFollowRedirects(false); |
| 197 | urlConnection.setConnectTimeout(CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS); |
| 198 | urlConnection.setReadTimeout(CAPTIVE_PORTAL_SOCKET_TIMEOUT_MS); |
| 199 | urlConnection.setUseCaches(false); |
| 200 | urlConnection.getInputStream(); |
| 201 | // We got a valid response, but not from the real google |
| 202 | final int responseCode = urlConnection.getResponseCode(); |
| 203 | if (responseCode == 408 || responseCode == 504) { |
| 204 | // If we timeout here, we'll try and go through captive portal login |
| 205 | return true; |
| 206 | } |
| 207 | return urlConnection.getResponseCode() != 204; |
| 208 | } catch (IOException e) { |
| 209 | Log.e(TAG, "Captive portal check - probably not a portal: exception " |
| 210 | + e); |
| 211 | return false; |
| 212 | } finally { |
| 213 | if (urlConnection != null) { |
| 214 | urlConnection.disconnect(); |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | private void launchWifiSetup() { |
| 220 | SetupWizardUtils.tryEnablingWifi(mContext); |
| 221 | Intent intent = new Intent(SetupWizardApp.ACTION_SETUP_WIFI); |
| 222 | if (SetupWizardUtils.hasLeanback(mContext)) { |
| 223 | intent.setComponent(SetupWizardUtils.mTvwifisettingsActivity); |
| 224 | } |
| 225 | intent.putExtra(SetupWizardApp.EXTRA_FIRST_RUN, true); |
| 226 | intent.putExtra(SetupWizardApp.EXTRA_ALLOW_SKIP, true); |
| 227 | intent.putExtra(SetupWizardApp.EXTRA_USE_IMMERSIVE, true); |
| 228 | intent.putExtra(SetupWizardApp.EXTRA_THEME, SetupWizardApp.EXTRA_MATERIAL_LIGHT); |
| 229 | intent.putExtra(SetupWizardApp.EXTRA_AUTO_FINISH, false); |
| 230 | ActivityOptions options = |
| 231 | ActivityOptions.makeCustomAnimation(mContext, |
| 232 | android.R.anim.fade_in, |
| 233 | android.R.anim.fade_out); |
| 234 | mLoadingFragment.startActivityForResult(intent, |
| 235 | SetupWizardApp.REQUEST_CODE_SETUP_WIFI, options.toBundle()); |
| 236 | } |
| 237 | } |