<commit_before>package com.cedar.autologin;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class LoginTask extends AsyncTask<BasicNameValuePair, Integer, Boolean> {

	final static String version = "2.2";
	
	Context context;
	String account;
	String passwd;
	int retrys = 0;
	Boolean exceedError = false;
	Boolean passwdError = false;
	Boolean alreadyLoggedIn = false;
	Boolean retry = false;
	
	Boolean foreground = false;
	
	public LoginTask(Context context) {

	    this.context = context;
	}
	
	protected Boolean doInBackground(BasicNameValuePair... params) {
		for (BasicNameValuePair param : params) {
			if (param.getName().equals("retrys")) {
				try {
					retrys = Integer.parseInt(param.getValue()) + 1;
				} catch (NumberFormatException e) {
					Log.d("autologin", "Integer.parseInt error " + e.toString());
					return false;
				}
			}
			else if (param.getName().equals("foreground")) {
				foreground = true;
			}
		}
		if (checkLogin())
			return false;
		else if (retry) {
			retry();
			return false;
		}
		else {
			SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
			account = sp.getString("account", "");  
			passwd = sp.getString("passwd", "");
			if (account.equals("") || passwd.equals("")) {
				Log.d("autologin", "account or passwd is empty");
				return false;
			}
			if (login()) {
				Log.d("autologin", "login succeed");
				return true;
			} else if (retry) {
				retry();
				return false;
			} else if (exceedError) {
				MainActivity.NicFragment.NicTask nicTask = MainActivity.NicFragment.newInstance(0).new NicTask(context, "offlineCurrentAndLogin");
				nicTask.foreground = foreground;
				nicTask.execute();
				return false;
			} else {
				Log.d("autologin", "login failed");
				return false;
			}
		}
	}

	protected void onPostExecute(Boolean result) {
		if (retry)
			return;
		SQLiteHelper db = new SQLiteHelper(context);
		
		if (result) {
			db.addLog("¼ɹ");
			Toast.makeText(context.getApplicationContext(),
					"AutoLogin: ¼ɹ~", Toast.LENGTH_LONG)
					.show();
			WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
			sp.edit().putString("lastssid", wifi_service.getConnectionInfo().getSSID()).apply();
		} else if (retrys >= 3){
			Log.d("autologin", "retrys too many times");
			db.addLog("¼ʧ");
			if (foreground)
				Toast.makeText(context.getApplicationContext(),
						"AutoLogin: ¼ʧ!", Toast.LENGTH_LONG).show();
			else
				showNotification("AutoLogin: ¼ʧ");
		} else if (exceedError) {
			/* -> moved to MainActivity.NicFragment.NicTask offlineCurrentAndLogin
			db.addLog("¼ ");
			Toast.makeText(context.getApplicationContext(),
					"AutoLogin: ¼ !", Toast.LENGTH_LONG)
					.show();
					*/
		} else if (passwdError) {
			db.addLog("û ");
			if (foreground)
				Toast.makeText(context.getApplicationContext(),
						"AutoLogin: û !", Toast.LENGTH_LONG).show();
			else
				showNotification("AutoLogin: û ");
		} else if (alreadyLoggedIn) {
			db.addLog("ѵ¼ ");
			
			SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
			String lastssid = sp.getString("lastssid", "");

			WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			String currentssid = wifi_service.getConnectionInfo().getSSID();
			
			if (!currentssid.equals(lastssid)) {
				Toast.makeText(context.getApplicationContext(),
						"AutoLogin: ѵ¼~", Toast.LENGTH_LONG)
						.show();
				sp.edit().putString("lastssid", currentssid).apply();
			}
		}
	}

	public Boolean checkLogin() {
		try {
			HttpClient client = new DefaultHttpClient();
			URI website = new URI("https://w.seu.edu.cn/portal/init.php");
			HttpGet request = new HttpGet();
			request.setHeader("User-Agent", "Mozilla/5.0 (Android " + android.os.Build.VERSION.RELEASE + ") AutoLogin/" + version);
			request.setURI(website);
			HttpResponse response = client.execute(request);
			// int statusCode = response.getStatusLine().getStatusCode();

			String responseStr = EntityUtils.toString(response.getEntity());
			//Log.d("autologin", responseStr);
			if (responseStr.contains("login_username")) {
				alreadyLoggedIn = true;
				Log.d("autologin", "already logged in");
				return true;
			} else {
				// Log.d("autologin", "have not login");
				return false;
			}

		} catch (Exception e) {
			Log.d("checkLogin", "Error in http connection " + e.toString());
			retry = true;
			return false;
		}
	}
<commit_msg>check for 403 forbidden error<commit_after>package com.cedar.autologin;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiManager;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;

public class LoginTask extends AsyncTask<BasicNameValuePair, Integer, Boolean> {

	final static String version = "2.2";
	
	Context context;
	String account;
	String passwd;
	int retrys = 0;
	Boolean exceedError = false;
	Boolean passwdError = false;
	Boolean alreadyLoggedIn = false;
	Boolean retry = false;
	Boolean forbidden = false;
	
	Boolean foreground = false;
	
	public LoginTask(Context context) {

	    this.context = context;
	}
	
	protected Boolean doInBackground(BasicNameValuePair... params) {
		for (BasicNameValuePair param : params) {
			if (param.getName().equals("retrys")) {
				try {
					retrys = Integer.parseInt(param.getValue()) + 1;
				} catch (NumberFormatException e) {
					Log.d("autologin", "Integer.parseInt error " + e.toString());
					return false;
				}
			}
			else if (param.getName().equals("foreground")) {
				foreground = true;
			}
		}
		if (checkLogin())
			return false;
		else if (retry) {
			retry();
			return false;
		}
		else {
			SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
			account = sp.getString("account", "");  
			passwd = sp.getString("passwd", "");
			if (account.equals("") || passwd.equals("")) {
				Log.d("autologin", "account or passwd is empty");
				return false;
			}
			if (login()) {
				Log.d("autologin", "login succeed");
				return true;
			} else if (retry) {
				retry();
				return false;
			} else if (exceedError) {
				MainActivity.NicFragment.NicTask nicTask = MainActivity.NicFragment.newInstance(0).new NicTask(context, "offlineCurrentAndLogin");
				nicTask.foreground = foreground;
				nicTask.execute();
				return false;
			} else {
				Log.d("autologin", "login failed");
				return false;
			}
		}
	}

	protected void onPostExecute(Boolean result) {
		if (retry)
			return;
		SQLiteHelper db = new SQLiteHelper(context);
		
		if (result) {
			db.addLog("¼ɹ");
			Toast.makeText(context.getApplicationContext(),
					"AutoLogin: ¼ɹ~", Toast.LENGTH_LONG)
					.show();
			WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
			sp.edit().putString("lastssid", wifi_service.getConnectionInfo().getSSID()).apply();
		} else if (retrys >= 3){
			Log.d("autologin", "retrys too many times");
			db.addLog("¼ʧ");
			if (foreground)
				Toast.makeText(context.getApplicationContext(),
						"AutoLogin: ¼ʧ!", Toast.LENGTH_LONG).show();
			else
				showNotification("AutoLogin: ¼ʧ");
		} else if (exceedError) {
			/* -> moved to MainActivity.NicFragment.NicTask offlineCurrentAndLogin
			db.addLog("¼ ");
			Toast.makeText(context.getApplicationContext(),
					"AutoLogin: ¼ !", Toast.LENGTH_LONG)
					.show();
					*/
		} else if (passwdError) {
			db.addLog("û ");
			if (foreground)
				Toast.makeText(context.getApplicationContext(),
						"AutoLogin: û !", Toast.LENGTH_LONG).show();
			else
				showNotification("AutoLogin: û ");
		} else if (alreadyLoggedIn) {
			db.addLog("ѵ¼ ");
			
			SharedPreferences sp = context.getSharedPreferences("userInfo", Context.MODE_PRIVATE);
			String lastssid = sp.getString("lastssid", "");

			WifiManager wifi_service = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
			String currentssid = wifi_service.getConnectionInfo().getSSID();
			
			if (!currentssid.equals(lastssid)) {
				Toast.makeText(context.getApplicationContext(),
						"AutoLogin: ѵ¼~", Toast.LENGTH_LONG)
						.show();
				sp.edit().putString("lastssid", currentssid).apply();
			}
		} else if (forbidden) {
			db.addLog("ûзȨ");
			Toast.makeText(context.getApplicationContext(),
					"AutoLogin: ~ûзʸҳȨŶ\nϢ~Ϣһ£", Toast.LENGTH_LONG)
					.show();
		}
	}

	public Boolean checkLogin() {
		try {
			HttpClient client = new DefaultHttpClient();
			URI website = new URI("https://w.seu.edu.cn/portal/init.php");
			HttpGet request = new HttpGet();
			request.setHeader("User-Agent", "Mozilla/5.0 (Android " + android.os.Build.VERSION.RELEASE + ") AutoLogin/" + version);
			request.setURI(website);
			HttpResponse response = client.execute(request);
			// int statusCode = response.getStatusLine().getStatusCode();

			String responseStr = EntityUtils.toString(response.getEntity());
			//Log.d("autologin", responseStr);
			if (responseStr.contains("login_username")) {
				alreadyLoggedIn = true;
				Log.d("autologin", "already logged in");
				return true;
			} else if (response.getStatusLine().getStatusCode() == 403) {
				forbidden = true;
				Log.d("autologin", "403 forbidden");
				return true;
			} else {
				// Log.d("autologin", "have not login");
				return false;
			}

		} catch (Exception e) {
			Log.d("checkLogin", "Error in http connection " + e.toString());
			retry = true;
			return false;
		}
	}

{'fraction_non_alphanumeric': 0.13942568177713563, 'fraction_numerical': 0.0024381433989525015, 'mean_word_length': 4.020398912058024, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3297615', 'n_tokens_mistral': 4150, 'n_tokens_neox': 3518, 'n_words': 711}