Back to project page AndroidWifiServer.
The source code is released under:
Apache License
If you think the Android project AndroidWifiServer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jp.maju.wifiserver; /*from w ww . j av a2 s. c o m*/ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import jp.maju.wifidetecter.utils.Logger; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Pair; /* * Copyright {2014} {Matsuda Jumpei} * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ public class DBAdapter { private static final String TAG = DBAdapter.class.getSimpleName(); private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "times"; private static final String TABLE_TIMES_NAME = "timetables"; private DatabaseHelper mDatabaseHelper; private SQLiteDatabase mOpenedDatabase; public DBAdapter(Context context) { mDatabaseHelper = new DatabaseHelper(context); } public DBAdapter open() { if (mOpenedDatabase == null) { mOpenedDatabase = mDatabaseHelper.getWritableDatabase(); } return this; } public void close() { mDatabaseHelper.close(); } public void login(String username, long time) { ContentValues cv = new ContentValues(); cv.put("username", username); cv.put("login_time", time); mOpenedDatabase.insert(TABLE_TIMES_NAME, null, cv); } public void logout(String username, long time) { Logger.d(TAG, "time is "+time); ContentValues cv = new ContentValues(); cv.put("logout_time", time); try { mOpenedDatabase.beginTransaction(); Cursor c = mOpenedDatabase.rawQuery("SELECT * from " + TABLE_TIMES_NAME+" WHERE username='" + username + "' ORDER BY _id DESC LIMIT 1", null); if (c != null) { long id = -1L; if (c.moveToFirst()) { id = c.getLong(c.getColumnIndex("_id")); } c.close(); if (id != -1L) { mOpenedDatabase.update(TABLE_TIMES_NAME, cv, "_id=" + id, null); } } mOpenedDatabase.setTransactionSuccessful(); } finally { mOpenedDatabase.endTransaction(); } } public List<Long> getLoginList(String username) { List<Long> timeList = new ArrayList<Long>(); Cursor c = mOpenedDatabase.query(TABLE_TIMES_NAME, null, "username = '" + username + "'", null, null, null, null); if (c != null) { if (c.moveToFirst()) { do { long time = c.getLong(c.getColumnIndex("login_time")); timeList.add(time); } while (c.moveToNext()); } c.close(); } return timeList; } public Map<String, List<Pair<Long, Long>>> getAllLoginList() { Map<String, List<Pair<Long, Long>>> result = new HashMap<String, List<Pair<Long, Long>>>(); Cursor c = mOpenedDatabase.query(TABLE_TIMES_NAME, null, null, null, null, null, null); if (c != null) { if (c.moveToFirst()) { do { String username = c.getString(c.getColumnIndex("username")); long loginTime = c.getLong(c.getColumnIndex("login_time")); Long logoutTime = null; if (!c.isNull(c.getColumnIndex("logout_time"))) { logoutTime = c.getLong(c.getColumnIndex("logout_time")); } List<Pair<Long, Long>> timeList = result.get(username); Pair<Long, Long> pair = new Pair<Long, Long>(loginTime, logoutTime); if (timeList == null) { timeList = new ArrayList<Pair<Long, Long>>(); } timeList.add(pair); result.put(username, timeList); } while (c.moveToNext()); } c.close(); } return result; } private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { if (createTimeTables(db)) { } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } private boolean createTimeTables(SQLiteDatabase db) { String[] columns = { "username TEXT NOT NULL", "login_time INTEGER NOT NULL", "logout_time INTEGER" }; return execCreateSql(db, TABLE_TIMES_NAME, columns); } private boolean execCreateSql(SQLiteDatabase db, String tableName, String... columns) { String sql = getCreateSql(tableName, columns); if (sql != null) { db.execSQL(sql); return true; } else { return false; } } private String getCreateSql(String tableName, String... columns) { if (columns == null || columns.length == 0) { return null; } String sqlBody = "CREATE TABLE " + tableName + " (" + "_id integer primary key autoincrement,"; int length = columns.length; for (int i = 0; i < length; i++) { sqlBody += columns[i]; if (i != length - 1) { sqlBody += ","; } else { sqlBody += ");"; } } return sqlBody.toString(); } } }