Android g minDate=-1; 长时间; 试一试{ c=mydb.rawQuery(“从u_id=“+u_id+”;”,null的记录中选择不同的(日期)); if(c.moveToFirst()){ minDate=getLongDateTime(c.get

Android g minDate=-1; 长时间; 试一试{ c=mydb.rawQuery(“从u_id=“+u_id+”;”,null的记录中选择不同的(日期)); if(c.moveToFirst()){ minDate=getLongDateTime(c.get,android,sqlite,android-cursor,Android,Sqlite,Android Cursor,g minDate=-1; 长时间; 试一试{ c=mydb.rawQuery(“从u_id=“+u_id+”;”,null的记录中选择不同的(日期)); if(c.moveToFirst()){ minDate=getLongDateTime(c.getString(0),日期格式); while(c.moveToNext()){ tempDate=getLongDateTime(c.getString(0),日期格式); if(tempDate长日期){ longDate=tempDate;

g minDate=-1; 长时间; 试一试{ c=mydb.rawQuery(“从u_id=“+u_id+”;”,null的记录中选择不同的(日期)); if(c.moveToFirst()){ minDate=getLongDateTime(c.getString(0),日期格式); while(c.moveToNext()){ tempDate=getLongDateTime(c.getString(0),日期格式); if(tempDate长日期){ longDate=tempDate; maxDate=c.getString(0); } } } }catch(sqlitee异常){ e、 printStackTrace(); } 返回maxDate; } 私有长getLongDateTime(字符串日期时间,字符串格式){ SimpleDataFormat SimpleDataFormat=新的SimpleDataFormat(格式,Locale.US); 日期newDate; longDate=-1; 试一试{ newDate=SimpleDataFormat.parse(日期时间); longDate=newDate.getTime(); }捕获(解析异常){ e、 printStackTrace(); } 返回长日期; } POJO urlConnector(字符串con){ POJO POJO=新POJO(); 国际身份证; 字符串响应; 试一试{ URL=新URL(con); HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); BufferedReader BufferedReader=new BufferedReader(new InputStreamReader((InputStream)httpURLConnection.getContent()); response=bufferedReader.readLine(); httpURLConnection.disconnect(); JSONObject对象=新的JSONObject(响应); response=object.getString(“response”); setResponse(响应); 如果(“成功”。等于(响应)){ u_id=object.getInt(“u_id”); pojo.setU_id(u_id); } }捕获(SocketTimeoutException e){ e、 printStackTrace(); setResponse(“连接超时”); }捕获(IOE异常){ e、 printStackTrace(); pojo.setResponse(“网络不可用”); }捕获(JSONException e){ e、 printStackTrace(); pojo.setResponse(“数据不可用”); } 返回pojo; } POJO jsonReceiver(字符串con){ POJO POJO=新POJO(); 字符串名称=”,响应; int u_id=-1; 试一试{ URL=新URL(con); HttpURLConnection HttpURLConnection=(HttpURLConnection)url.openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); BufferedReader BufferedReader=new BufferedReader(new InputStreamReader((InputStream)httpURLConnection.getContent()); 字符串结果=bufferedReader.readLine(); httpURLConnection.disconnect(); JSONObject对象=新的JSONObject(结果); response=object.getString(“response”); setResponse(响应); 如果(“成功”。等于(响应)){ u_id=object.getInt(“u_id”); name=object.getString(“name”); pojo.setU_id(u_id); pojo.setNamePojo(名称); } if(object.has(“jsonarray”)){ JSONArray JSONArray=object.getJSONArray(“JSONArray”); if(DEBUG)Log.i(“######”,“JSONARRAY:“+JSONARRAY.toString()); for(int i=0;i class DatabaseHelper extends SQLiteAssetHelper { static final String AUTHORITY = "com.example.attendance.provider"; static final String ACCOUNT_TYPE = "example.com"; static final String ACCOUNT = "Lazim"; static final String WIFI_ERROR = "Please enter your office premises"; static final boolean DEBUG = true; private static final String DATE_FORMAT = "d-M-yyyy"; private static final String TIME_FORMAT = "hh:mm:ss a"; private static final String DB_NAME = "attendance.db"; private static final int DB_VERSION = 1; private static SQLiteDatabase mydb; private Cursor c; private Context context; DatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); this.context = context; mydb = getWritableDatabase(); } boolean insertTable(int u_id, String name, String username, String password) { boolean b; try { c = mydb.rawQuery("select * from login where u_id=" + u_id + ";", null); if (!c.moveToFirst()) { mydb.execSQL("insert into login (u_id,name,username,password) values(" + u_id + ",'" + name + "','" + username + "','" + password + "');"); b = true; } else { mydb.execSQL("update login set name='" + name + "',username='" + username + "',password='" + password + "' where u_id=" + u_id + ";"); mydb.execSQL("delete from records where u_id=" + u_id + ";"); b = true; } } catch (Exception e) { e.printStackTrace(); b = false; } return b; } boolean insertTable(int id, String name, String date, String checkin, int flag) { boolean b = false; try { c = mydb.rawQuery("select * from records where u_id=" + id + " and date='" + date + "' and checkin='" + checkin + "';", null); if (!c.moveToFirst()) { mydb.execSQL("insert into records (u_id,name,date,checkin,checkout,flag) values(" + id + ",'" + name + "','" + date + "','" + checkin + "',''," + flag + ");"); b = true; } } catch (SQLiteException e) { e.printStackTrace(); } return b; } Boolean deleteRecords(String username) { Boolean b = null; try { c = mydb.rawQuery("select u_id from login where username='" + username + "';", null); if (c.moveToFirst()) { int u_id = c.getInt(0); Cursor cursor = mydb.rawQuery("select * from records where u_id=" + u_id + ";", null); if (cursor.moveToFirst()) mydb.execSQL("delete from records where u_id=" + u_id + ";", null); cursor.close(); mydb.execSQL("delete from login where username='" + username + "';"); b = true; } else b = false; } catch (SQLiteException e) { e.printStackTrace(); } return b; } boolean insertOnLogin(int u_id, String name, String username, String password) { boolean b = false; try { c = mydb.rawQuery("select * from login where u_id=" + u_id + ";", null); if (!c.moveToFirst()) mydb.execSQL("insert into login (u_id,name,username,password) values(" + u_id + ",'" + name + "','" + username + "','" + password + "');"); else mydb.execSQL("update login set name='" + name + "',username='" + username + "',password='" + password + "' where u_id=" + u_id + ";"); b = true; } catch (SQLiteException e) { e.printStackTrace(); } return b; } boolean updateTable(int id, String name, String checkout, String date, int flag) { boolean b = false; int s_id, f; try { c = mydb.rawQuery("select s_id,flag from records where u_id=" + id + " and date='" + date + "' and checkin='" + getCheckin() + "';", null); if (c.moveToFirst()) { s_id = c.getInt(0); f = c.getInt(1); if (f == 0) flag = 0; mydb.execSQL("update records set flag=" + flag + ",checkout='" + checkout + "' where s_id=" + s_id + ";"); b = true; } else { mydb.execSQL("insert into records (u_id,name,date,checkin,checkout,flag) values(" + id + ",'" + name + "','" + date + "','','" + checkout + "'," + flag + ");"); b = true; } } catch (SQLiteException e) { e.printStackTrace(); } return b; } void updateFlags(Cursor c) { if (c.moveToFirst()) { do { try { mydb.execSQL("update records set flag=1 where u_id=" + c.getInt(0) + " and date='" + c.getString(2) + "' and checkin='" + c.getString(3) + "' and checkout='" + c.getString(4) + "';"); if (DEBUG) Log.i("#####", "###FLAGS UPDATED"); } catch (SQLiteException e) { e.printStackTrace(); } } while (c.moveToNext()); } } long getMinDate(int u_id) { long minDate = -1; long tempDate; try { c = mydb.rawQuery("select distinct(date) from records where u_id=" + u_id + ";", null); if (c.moveToFirst()) { minDate = getLongDateTime(c.getString(0), DATE_FORMAT); while (c.moveToNext()) { tempDate = getLongDateTime(c.getString(0), DATE_FORMAT); if (tempDate < minDate) minDate = tempDate; } } } catch (SQLiteException e) { e.printStackTrace(); } return minDate; } private String getMaxDate(int u_id, String check) { long longDate; long tempDate; String maxDate = null; try { if ("checkout".equals(check)) c = mydb.rawQuery("select distinct(date) from records where u_id=" + u_id + " and checkout!='';", null); else c = mydb.rawQuery("select distinct(date) from records where u_id=" + u_id + ";", null); if (c.moveToFirst()) { maxDate = c.getString(0); longDate = getLongDateTime(maxDate, DATE_FORMAT); while (c.moveToNext()) { tempDate = getLongDateTime(c.getString(0), DATE_FORMAT); if (tempDate > longDate) { longDate = tempDate; maxDate = c.getString(0); } } } } catch (SQLiteException e) { e.printStackTrace(); } return maxDate; } private long getLongDateTime(String datetime, String format) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, Locale.US); Date newDate; long longDate = -1; try { newDate = simpleDateFormat.parse(datetime); longDate = newDate.getTime(); } catch (ParseException e) { e.printStackTrace(); } return longDate; } POJO urlConnector(String con) { POJO pojo = new POJO(); int u_id; String response; try { URL url = new URL(con); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((InputStream) httpURLConnection.getContent())); response = bufferedReader.readLine(); httpURLConnection.disconnect(); JSONObject object = new JSONObject(response); response = object.getString("response"); pojo.setResponse(response); if ("success".equals(response)) { u_id = object.getInt("u_id"); pojo.setU_id(u_id); } } catch (SocketTimeoutException e) { e.printStackTrace(); pojo.setResponse("Connection timed out"); } catch (IOException e) { e.printStackTrace(); pojo.setResponse("Network unavailable"); } catch (JSONException e) { e.printStackTrace(); pojo.setResponse("Data unavailable"); } return pojo; } POJO jsonReceiver(String con) { POJO pojo = new POJO(); String name = "", response; int u_id = -1; try { URL url = new URL(con); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setConnectTimeout(10000); httpURLConnection.setReadTimeout(10000); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((InputStream) httpURLConnection.getContent())); String result = bufferedReader.readLine(); httpURLConnection.disconnect(); JSONObject object = new JSONObject(result); response = object.getString("response"); pojo.setResponse(response); if ("success".equals(response)) { u_id = object.getInt("u_id"); name = object.getString("name"); pojo.setU_id(u_id); pojo.setNamePojo(name); } if (object.has("jsonarray")) { JSONArray jsonArray = object.getJSONArray("jsonarray"); if (DEBUG) Log.i("#####", "JSONARRAY: " + jsonArray.toString()); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject = jsonArray.getJSONObject(i); if (DEBUG) Log.i("###########", "#####" + jsonObject); String date = jsonObject.getString("date"); String checkin = jsonObject.getString("checkin"); String checkout = jsonObject.getString("checkout"); try { c = mydb.rawQuery("select * from records where u_id=" + u_id + " and date='" + date + "' and checkin='" + checkin + "';", null); if (!c.moveToFirst()) mydb.execSQL("insert into records (u_id,name,date,checkin,checkout) values (" + u_id + ",'" + name + "','" + date + "','" + checkin + "','" + checkout + "');"); } catch (SQLiteException e) { e.printStackTrace(); } } } else if (DEBUG) Log.i("#####", "###NO JSON ARRAY"); } catch (SocketTimeoutException e) { e.printStackTrace(); pojo.setResponse("Connection timed out"); } catch (IOException e) { e.printStackTrace(); pojo.setResponse("Network unavailable"); } catch (JSONException e) { e.printStackTrace(); pojo.setResponse("Data unavailable"); } return pojo; } String uploadData(String con) { String result; try { URL url = new URL(con); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); result = reader.readLine(); connection.disconnect(); } catch (SocketTimeoutException e) { e.printStackTrace(); result = "Connection timed out"; } catch (IOException e) { e.printStackTrace(); result = "Network unavailable"; } return result; } void setCheckin(String checkin) { SharedPreferences preferences = context.getSharedPreferences("checkin", MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putString("last_checkin", checkin); editor.apply(); } String getCheckin() { SharedPreferences preferences = context.getSharedPreferences("checkin", MODE_PRIVATE); return preferences.getString("last_checkin", null); } Cursor getFileQueue() { try { c = mydb.rawQuery("select u_id,name,date,checkin,checkout from records where flag=0 order by s_id", null); return c; } catch (Exception e) { e.printStackTrace(); return null; } } private Cursor getSessionLogAM(String date, int u_id) { Cursor mergeCursorAM = null; try { c = mydb.rawQuery("select checkin,checkout from records where u_id=" + u_id + " and date='" + date + "' and checkin like '12%AM' order by checkin;", null); mergeCursorAM = mydb.rawQuery("select checkin,checkout from records where u_id=" + u_id + " and date='" + date + "' and checkin like '%AM' and checkin not like '12%' order by checkin;", null); if (c.moveToFirst()) { if (mergeCursorAM.moveToFirst()) mergeCursorAM = new MergeCursor(new Cursor[]{c, mergeCursorAM}); else mergeCursorAM = c; } } catch (SQLiteException e) { e.printStackTrace(); } return mergeCursorAM; } private Cursor getSessionLogPM(String date, int u_id) { Cursor mergeCursorPM = null; try { c = mydb.rawQuery("select checkin,checkout from records where u_id=" + u_id + " and date='" + date + "' and checkin like '12%PM' order by checkin;", null); mergeCursorPM = mydb.rawQuery("select checkin,checkout from records where u_id=" + u_id + " and date='" + date + "' and checkin like '%PM' and checkin not like '12%' order by checkin;", null); if (c.moveToFirst()) { if (mergeCursorPM.moveToFirst()) mergeCursorPM = new MergeCursor(new Cursor[]{c, mergeCursorPM}); else mergeCursorPM = c; } } catch (SQLiteException e) { e.printStackTrace(); } return mergeCursorPM; } Cursor getMergedLog(String date, int u_id) { Cursor AM = getSessionLogAM(date, u_id), PM = getSessionLogPM(date, u_id); if (AM.moveToFirst()) { if (PM.moveToFirst()) return new MergeCursor(new Cursor[]{AM, PM}); else return AM; } else if (PM.moveToFirst()) return PM; else return null; } String enString(String s) { try { return URLEncoder.encode(s, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } String getName(String username) { String name = ""; try { c = mydb.rawQuery("select name from login where username='" + username + "';", null); if (c.moveToFirst()) { do { name = c.getString(0); } while (c.moveToNext()); } } catch (SQLiteException e) { e.printStackTrace(); } return name; } int getID(String username) { int id = -1; try { c = mydb.rawQuery("select u_id from login where username='" + username + "';", null); if (c.moveToFirst()) { do { id = c.getInt(0); } while (c.moveToNext()); } } catch (SQLiteException e) { e.printStackTrace(); } return id; } String getLastLogin(int id) { String maxTime; String maxDate; maxDate = getMaxDate(id, null); if (maxDate != null) { maxTime = getMaxTime(id, maxDate, "checkin"); if (maxTime != null) return (maxTime + " " + maxDate); } return "N/A"; } String getLastLogout(int id) { String maxTime; String maxDate; maxDate = getMaxDate(id, "checkout"); if (maxDate != null) { maxTime = getMaxTime(id, maxDate, "checkout"); if (maxTime != null) return (maxTime + " " + maxDate); } return "N/A"; } private String getMaxTime(int u_id, String date, String check) { long time, tempTime; String maxTime = null; try { c = mydb.rawQuery("select distinct(" + check + ") from records where u_id=" + u_id + " and date='" + date + "';", null); if (c.moveToFirst()) { maxTime = c.getString(0); time = getLongDateTime(maxTime, TIME_FORMAT); while (c.moveToNext()) { tempTime = getLongDateTime(c.getString(0), TIME_FORMAT); if (tempTime > time) { time = tempTime; maxTime = c.getString(0); } } } } catch (SQLiteException e) { e.printStackTrace(); } return maxTime; } boolean checkWifi() { boolean b = false; WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); if (wifiManager.startScan()) { List<ScanResult> scanResults = wifiManager.getScanResults(); if (scanResults != null) for (int i = 0; i < scanResults.size(); i++) { ScanResult result = scanResults.get(i); if ("6c:72:20:fa:0b:6c".equals(result.BSSID) || "d8:fe:e3:17:93:0c".equals(result.BSSID)) if (result.level >= -67) { b = true; break; } } } return b; } void setLoggedIn(String username) { SharedPreferences sharedPreferences = context.getSharedPreferences("logged_in", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("login", true); editor.putBoolean("login_received", true); editor.putBoolean("logout_received", false); editor.putString("username", username); editor.apply(); } void setLoggedOut() { SharedPreferences sharedPreferences = context.getSharedPreferences("logged_in", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("login", false); editor.putBoolean("logout_received", true); editor.putBoolean("login_received", false); editor.apply(); } void setCheckBox(boolean b, String username, String password) { SharedPreferences preferences = context.getSharedPreferences("checkbox", MODE_PRIVATE); SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean("remember", b); if (b) { editor.putString("username", username); editor.putString("password", password); } else { if (preferences.getString("username", null) != null) editor.remove("username"); if (preferences.getString("password", null) != null) editor.remove("password"); } editor.apply(); } }