| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- package com.usai.util;
- import java.util.ArrayList;
- import java.util.List;
- import com.usai.redant.photo.RedAntApplication;
- import android.content.ContentValues;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteException;
- import android.net.Uri;
- import android.provider.ContactsContract;
- import android.telephony.PhoneNumberUtils;
- import android.util.Log;
- public class dbUtil
- {
- // public static final String ZONECODE
- // public static final int FILTER_TYPE_MSG = 0;
- // public static final int FILTER_TYPE_BOTH = 1;
- // public static final int FILTER_TYPE_CALL = 2;
- // public static final int RECORD_TYPE_CALL = 2;
- // public static final int RECORD_TYPE_MSG = 1;
- //
- // public static final int NUMBER_TYPE_NULL = 0;
- // public static final int NUMBER_TYPE_NORMAL = 1;
- // public static final int NUMBER_TYPE_BLACK = 2;
- // public static final int NUMBER_TYPE_WHITE = 3;
- // public static final int NUMBER_TYPE_GRAY = 4;
- // public static final int NUMBER_TYPE_UNKNOWN = 7;
- // public static final int NUMBER_TYPE_CONTACT = 5;
- // public static final int NUMBER_TYPE_SUSPECT = 6;
- // public static final int FILTER_SOURCE_BLACKLIST = 0;
- // public static final int FILTER_SOURCE_SINGLERING = 1;
- // public static final int FILTER_SOURCE_KEYWORDS = 2;
- // public static final int FILTER_SOURCE_NONUMBER = 3;
- // public static void AddNumberToBlacklist(Context context, String Number) {
- // SQLiteDatabase db;
- // db = dbUtil.OpenDB(context, null, true);
- // boolean bexist = isRecordExist(db, "user_numberlist", "number = '"
- // + Number + "'");
- // if (bexist == false) {
- // ContentValues cv = new ContentValues();
- // cv.put("number", Number);
- // cv.put("type", dbUtil.NUMBER_TYPE_BLACK);
- // cv.put("burntype", 3);
- // cv.put("cimm", PhoneNumberUtils.toCallerIDMinMatch(Number));
- // db.insert("user_numberlist", null, cv);
- // }
- // dbUtil.CloseDB(db);
- // }
- // public static int KeywordsFilter(Context context, String Msg) {
- // int ret = -1;
- // boolean bKeywordsFilter = context.getSharedPreferences("PhoneAsstPref",
- // 0).getBoolean("KeywordsFilterPref", false);
- //
- // // = context.getSharedPreferences("PhoneAsstPref", 0)
- // // .getBoolean("BlockUnknownNumber", false);
- // if (bKeywordsFilter == false)
- // return ret;
- // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
- //
- // Cursor cursor = null;
- // try {
- //
- // cursor = db.query("user_keywords", new String[] { "_id", "words" },
- // null, null, null, null, "level desc", null);
- //
- // while (cursor.moveToNext()) {
- // String keywords = cursor.getString(1);
- // String[] spliter = keywords.split(" ");
- //
- // if (spliter.length == 0)
- // continue;
- //
- // int ifound = spliter.length; // 关键字个数
- // for (int idx = 0; idx < spliter.length; idx++) {
- // // 兼容以前版本关键字有多空格情况;
- // if (spliter[idx].equals("")) {
- // ifound--; // 排除误计算的空数量
- // continue;
- // }
- //
- // if (Msg.indexOf(spliter[idx]) != -1) {
- // ifound--;
- // }
- // }
- // if (ifound == 0) {
- // int id = cursor.getInt(0);
- // db.execSQL("update " + "user_keywords"
- // + " set level = level+1 where _id=" + id);
- // ret = id;
- // break;// break while
- // }
- //
- // }
- // } catch (SQLiteException e) {
- // dbgUtil.Log(Log.ERROR, "sql_err", e.toString());
- // } finally {
- // dbUtil.CloseCursor(cursor);
- // dbUtil.CloseDB(db);
- // }
- // return ret;
- // }
- // public static final int NUMBER_TYPE_UNKNOWN = 5;//no number ,hide number
- // ect;
- public static boolean isContentExist(Context context, Uri uri, String where)
- {
- boolean ret;
- Cursor cursor = context.getContentResolver().query(uri, null, where,
- null, null);
- dbgUtil.dbgCursorInfo(cursor, context);
- if (cursor.moveToNext())
- ret = true;
- else
- ret = false;
- dbUtil.CloseCursor(cursor);
- return ret;
- }
- // public static int query_NumberType(SQLiteDatabase db, String number) {
- // String cimm = PhoneNumberUtils.toCallerIDMinMatch(number);
- // int ret = -1;
- // Cursor cursor = db.query("user_numberlist", new String[] { "type",
- // "number" }, "cimm = '" + cimm + "'", null, null, null, null);
- // while (cursor.moveToNext()) {
- // if (PhoneNumberUtils.compare(number, cursor.getString(1))) {
- // dbgUtil.Log(
- // Log.ERROR,
- // "QUERY FROM user_numberlist",
- // number + "=" + cursor.getString(1) + "type="
- // + cursor.getInt(0));
- // ret = cursor.getInt(0);
- // break;
- // }
- // }
- // CloseCursor(cursor);
- // return ret;
- // }
- // static boolean isContact(Context context, String number) {
- // // String cimm=PhoneNumberUtils.toCallerIDMinMatch(number);
- // return dbUtil.isContentExist(context,
- // // ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- // Uri.withAppendedPath(
- // ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
- // Uri.encode(number)),
- // // ContactsContract.CommonDataKinds.Phone.NUMBER + "= '" +
- // // number+ "'");
- // /* "min_match = '" + cimm+ "'" */null);
- // }
- // static boolean isSuspect(SQLiteDatabase db, String number) {
- // return dbUtil.isRecordExist(db, "user_suspectlist", "number = '"
- // + number + "'");
- // }
- // public static int get_CallNumberType_old(Context context,
- // String incomingNumber)
- // {
- // int type = NUMBER_TYPE_NULL;
- // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
- //
- // boolean bBlockUnknown = context
- // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
- // "BlockUnknownNumber", false);
- // boolean bignorcontact = context
- // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
- // "IgnoreContact", false);
- // if ((incomingNumber == null || incomingNumber.equals("")))
- // if (bBlockUnknown)
- // type = NUMBER_TYPE_BLACK;
- // else
- // type = NUMBER_TYPE_NORMAL;
- // else
- // {
- // type = query_NumberType(db, incomingNumber);
- // if (type == NUMBER_TYPE_WHITE)
- // {
- //
- // }
- // else
- // if (bignorcontact && isContact(context, incomingNumber))
- // {
- // type = NUMBER_TYPE_CONTACT;
- // dbgUtil.Log(Log.ERROR,null, "number type contact");
- // }
- // else
- // if (type == NUMBER_TYPE_BLACK)
- // {
- // }
- // else
- // if (dbUtil.isRecordExist(db, "user_suspectlist",
- // "number = '" + incomingNumber + "'"))
- // {
- // type = NUMBER_TYPE_SUSPECT;
- // }
- // else
- // {
- // type = NUMBER_TYPE_NORMAL;
- //
- // }
- // }
- // dbUtil.CloseDB(db);
- //
- // dbgUtil.Log(Log.ERROR,"get_CallNumberType",
- // "number="+incomingNumber+" type="+type);
- // return type;
- //
- // }
- // public static NumberInfo get_NumberInfo(Context context,
- // String incomingNumber)
- // {
- // NumberInfo numberInfo = new NumberInfo();
- // numberInfo.incomingNumber = incomingNumber;
- // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
- //
- // // boolean bBlockUnknown = context
- // // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
- // // "BlockUnknownNumber", false);
- // // boolean bignorcontact = context
- // // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
- // // "IgnoreContact", false);
- //
- // // int type = NUMBER_TYPE_NULL;
- // if (incomingNumber == null || incomingNumber.length() < 3)
- // {
- // numberInfo.BaseType = NUMBER_TYPE_UNKNOWN;
- // }
- // else
- // {
- // numberInfo.BaseType = query_NumberType(db, incomingNumber);
- // if (numberInfo.BaseType == -1)// not in user_numberlist;
- // {
- // numberInfo.BaseType = NUMBER_TYPE_NORMAL;
- //
- // // else
- // // if (isSuspect(db, incomingNumber))
- // // {
- // // type = NUMBER_TYPE_SUSPECT;
- // //
- // // }
- // // else
- // // {
- // // type = NUMBER_TYPE_NORMAL;
- // // }
- // }
- // if (isContact(context, incomingNumber))
- // {
- // numberInfo.isContact = true;
- // // type = NUMBER_TYPE_CONTACT;
- // }
- // if (isSuspect(db, incomingNumber))
- // {
- // numberInfo.isSuspect = true;
- //
- // }
- //
- // // else
- // // if (type == NUMBER_TYPE_BLACK)
- // // {
- // // if (isContact(context, incomingNumber))
- // // {
- // // type = NUMBER_TYPE_CONTACT;
- // // }
- // // }
- // }
- // dbUtil.CloseDB(db);
- //
- // dbgUtil.Log(Log.ERROR, "get_NumberInfo", "number=" + incomingNumber
- // + " type=" + numberInfo.BaseType);
- // return numberInfo;
- //
- // }
- // public static int get_SmsNumberType(Context context, String
- // incomingNumber)
- // {
- // SQLiteDatabase db = dbUtil.OpenDB(context, null, false);
- //
- // boolean bBlockUnknown = context
- // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
- // "BlockUnknownNumber", false);
- // boolean bignorcontact = context
- // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
- // "IgnoreContact", false);
- //
- // int type = NUMBER_TYPE_NULL;
- // if ((incomingNumber == null || incomingNumber.equals("")))
- // if (bBlockUnknown)
- // type = NUMBER_TYPE_BLACK;
- // else
- // type = NUMBER_TYPE_NORMAL;
- // else
- // {
- // type = query_NumberType(db, incomingNumber);
- // if (type == NUMBER_TYPE_WHITE)
- // {
- //
- // }
- // else
- // if (bignorcontact && isContact(context, incomingNumber))
- // {
- // type = NUMBER_TYPE_CONTACT;
- // dbgUtil.Log(Log.ERROR, null, "number type contact");
- // }
- // else
- // if (type == NUMBER_TYPE_BLACK)
- // {
- // }
- // else
- // {
- // type = NUMBER_TYPE_NORMAL;
- //
- // }
- // }
- // dbUtil.CloseDB(db);
- //
- // return type;
- //
- // }
- public static void CloseDB(SQLiteDatabase db)
- {
- if (db == null)
- return;
- if (db.isOpen() == false)
- return;
- db.close();
- return;
- }
- public static void CloseCursor(Cursor c)
- {
- if (c == null)
- return;
- if (c.isClosed())
- return;
- c.close();
- return;
- }
- public static SQLiteDatabase OpenDB(Context context, CursorFactory factory,
- boolean writable)
- {
- SqlOpenHelper dbHelper = new SqlOpenHelper(context, "redantpop.db",
- factory, 1);
- dbHelper.addInitSQL("create table users (" + "_id INTEGER PRIMARY KEY,"
- + "name varchar(20)," + "pass varchar(20))");
- // dbHelper.addInitSQL("create table scan_records ("
- // + "_id INTEGER PRIMARY KEY," + "submit BOOLEAN DEFAULT (0),"
- // + "picker VARCHAR(20)," + "pid varchar(20))");
- dbHelper.addInitSQL("create table pics (" + "_id INTEGER PRIMARY KEY,"
- + "picker varchar(20)," + "server varchar(20),"+ "err_code INTEGER ,"
- + "pid varchar(20)," + "local_path varchar(256))");
-
- // dbHelper.addInitSQL("create table pids (" + "_id INTEGER PRIMARY KEY,"
- // + "picker varchar(20)," + "err_code INTEGER ,"
- // + "pid varchar(20))");
- SQLiteDatabase db = dbHelper.OpenDB(writable);
- return db;
- }
- public static void removeRecords(SQLiteDatabase db, String table,
- String whereclause)
- {
- db.execSQL("delete from " + table + " where " + whereclause);
- }
- public static long addRecordsFromCursor(SQLiteDatabase db,
- String tablename, Cursor source, String[] from, String[] to,
- boolean dumpPrimaryKey, boolean dumpOneRecord)
- {
- int ret = -1;
- ContentValues cv = new ContentValues();
- if (from == null)
- from = source.getColumnNames();
- if (to == null)
- to = source.getColumnNames();
- if (from.length != to.length)// 字段数不匹配
- return ret;
- while (source.moveToNext())
- {
- for (int i = 0; i < from.length; i++)
- {
- if (from[i].equals("_id"))
- {
- if (dumpPrimaryKey)
- cv.put(to[i], source.getInt(i));
- }
- else
- {
- cv.put(to[i], source.getString(i));
- }
- }
- db.insert(tablename, null, cv);
- if (dumpOneRecord)
- break;
- }
- ret = 1;
- return ret;
- }
- // public static long addRecord(SQLiteDatabase db, String table,
- // ContentValues record)
- // {
- // long ret = -1;
- // try
- // {
- // ret = db.insert(table, null, record);
- // }
- // catch (SQLiteException e)
- // {
- // dbgUtil.Log(Log.ERROR,"sql_err", e.toString());
- //
- // }
- // return ret;
- // }
- public static List<String> get_userlist()
- {
- List<String> ret = new ArrayList<String>();
- SQLiteDatabase db = OpenDB(RedAntApplication.getInstance()
- .getApplicationContext(), null, false);
- Cursor cursor = db.query(
- "users",
- new String[] { "name" },
- null, null, null,
- null, "name desc", null);
- while (cursor.moveToNext())
- {
- String user="";
- try
- {
- user = Crypto.decrypt("apexu", cursor.getString(0));
- }
- catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- ret.add(user);
- }
- CloseCursor(cursor);
- CloseDB(db);
- return ret;
- }
- public static boolean isTableEmpty(SQLiteDatabase db, String table)
- {
- boolean ret = true;
- Cursor cursor = null;
- try
- {
- cursor = db.query(table, new String[] { "*" }, null, null, null,
- null, null, "1");
- if (cursor.moveToNext())
- {
- // cursor.close();
- ret = false;
- }
- else
- ret = true;
- }
- catch (SQLiteException e)
- {
- Log.e("sql_err", e.toString());
- }
- finally
- {
- dbUtil.CloseCursor(cursor);
- }
- return ret;
- }
- public static boolean isRecordExist(SQLiteDatabase db, String table,
- String where)
- {
- boolean ret = false;
- Cursor cursor = null;
- try
- {
- cursor = db.query(table, new String[] { "_id" }, where, null, null,
- null, null, null);
- if (cursor.moveToNext())
- ret = true;
- else
- ret = false;
- }
- catch (SQLiteException e)
- {
- Log.e("sql_err", e.toString());
- }
- finally
- {
- dbUtil.CloseCursor(cursor);
- }
- return ret;
- }
- // if(isTableEmpty(db,"block_records"))
- }
|