dbUtil.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. package com.usai.util;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import com.usai.redant.photo.RedAntApplication;
  5. import android.content.ContentValues;
  6. import android.content.Context;
  7. import android.database.Cursor;
  8. import android.database.sqlite.SQLiteDatabase;
  9. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  10. import android.database.sqlite.SQLiteException;
  11. import android.net.Uri;
  12. import android.provider.ContactsContract;
  13. import android.telephony.PhoneNumberUtils;
  14. import android.util.Log;
  15. public class dbUtil
  16. {
  17. // public static final String ZONECODE
  18. // public static final int FILTER_TYPE_MSG = 0;
  19. // public static final int FILTER_TYPE_BOTH = 1;
  20. // public static final int FILTER_TYPE_CALL = 2;
  21. // public static final int RECORD_TYPE_CALL = 2;
  22. // public static final int RECORD_TYPE_MSG = 1;
  23. //
  24. // public static final int NUMBER_TYPE_NULL = 0;
  25. // public static final int NUMBER_TYPE_NORMAL = 1;
  26. // public static final int NUMBER_TYPE_BLACK = 2;
  27. // public static final int NUMBER_TYPE_WHITE = 3;
  28. // public static final int NUMBER_TYPE_GRAY = 4;
  29. // public static final int NUMBER_TYPE_UNKNOWN = 7;
  30. // public static final int NUMBER_TYPE_CONTACT = 5;
  31. // public static final int NUMBER_TYPE_SUSPECT = 6;
  32. // public static final int FILTER_SOURCE_BLACKLIST = 0;
  33. // public static final int FILTER_SOURCE_SINGLERING = 1;
  34. // public static final int FILTER_SOURCE_KEYWORDS = 2;
  35. // public static final int FILTER_SOURCE_NONUMBER = 3;
  36. // public static void AddNumberToBlacklist(Context context, String Number) {
  37. // SQLiteDatabase db;
  38. // db = dbUtil.OpenDB(context, null, true);
  39. // boolean bexist = isRecordExist(db, "user_numberlist", "number = '"
  40. // + Number + "'");
  41. // if (bexist == false) {
  42. // ContentValues cv = new ContentValues();
  43. // cv.put("number", Number);
  44. // cv.put("type", dbUtil.NUMBER_TYPE_BLACK);
  45. // cv.put("burntype", 3);
  46. // cv.put("cimm", PhoneNumberUtils.toCallerIDMinMatch(Number));
  47. // db.insert("user_numberlist", null, cv);
  48. // }
  49. // dbUtil.CloseDB(db);
  50. // }
  51. // public static int KeywordsFilter(Context context, String Msg) {
  52. // int ret = -1;
  53. // boolean bKeywordsFilter = context.getSharedPreferences("PhoneAsstPref",
  54. // 0).getBoolean("KeywordsFilterPref", false);
  55. //
  56. // // = context.getSharedPreferences("PhoneAsstPref", 0)
  57. // // .getBoolean("BlockUnknownNumber", false);
  58. // if (bKeywordsFilter == false)
  59. // return ret;
  60. // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
  61. //
  62. // Cursor cursor = null;
  63. // try {
  64. //
  65. // cursor = db.query("user_keywords", new String[] { "_id", "words" },
  66. // null, null, null, null, "level desc", null);
  67. //
  68. // while (cursor.moveToNext()) {
  69. // String keywords = cursor.getString(1);
  70. // String[] spliter = keywords.split(" ");
  71. //
  72. // if (spliter.length == 0)
  73. // continue;
  74. //
  75. // int ifound = spliter.length; // 关键字个数
  76. // for (int idx = 0; idx < spliter.length; idx++) {
  77. // // 兼容以前版本关键字有多空格情况;
  78. // if (spliter[idx].equals("")) {
  79. // ifound--; // 排除误计算的空数量
  80. // continue;
  81. // }
  82. //
  83. // if (Msg.indexOf(spliter[idx]) != -1) {
  84. // ifound--;
  85. // }
  86. // }
  87. // if (ifound == 0) {
  88. // int id = cursor.getInt(0);
  89. // db.execSQL("update " + "user_keywords"
  90. // + " set level = level+1 where _id=" + id);
  91. // ret = id;
  92. // break;// break while
  93. // }
  94. //
  95. // }
  96. // } catch (SQLiteException e) {
  97. // dbgUtil.Log(Log.ERROR, "sql_err", e.toString());
  98. // } finally {
  99. // dbUtil.CloseCursor(cursor);
  100. // dbUtil.CloseDB(db);
  101. // }
  102. // return ret;
  103. // }
  104. // public static final int NUMBER_TYPE_UNKNOWN = 5;//no number ,hide number
  105. // ect;
  106. public static boolean isContentExist(Context context, Uri uri, String where)
  107. {
  108. boolean ret;
  109. Cursor cursor = context.getContentResolver().query(uri, null, where,
  110. null, null);
  111. dbgUtil.dbgCursorInfo(cursor, context);
  112. if (cursor.moveToNext())
  113. ret = true;
  114. else
  115. ret = false;
  116. dbUtil.CloseCursor(cursor);
  117. return ret;
  118. }
  119. // public static int query_NumberType(SQLiteDatabase db, String number) {
  120. // String cimm = PhoneNumberUtils.toCallerIDMinMatch(number);
  121. // int ret = -1;
  122. // Cursor cursor = db.query("user_numberlist", new String[] { "type",
  123. // "number" }, "cimm = '" + cimm + "'", null, null, null, null);
  124. // while (cursor.moveToNext()) {
  125. // if (PhoneNumberUtils.compare(number, cursor.getString(1))) {
  126. // dbgUtil.Log(
  127. // Log.ERROR,
  128. // "QUERY FROM user_numberlist",
  129. // number + "=" + cursor.getString(1) + "type="
  130. // + cursor.getInt(0));
  131. // ret = cursor.getInt(0);
  132. // break;
  133. // }
  134. // }
  135. // CloseCursor(cursor);
  136. // return ret;
  137. // }
  138. // static boolean isContact(Context context, String number) {
  139. // // String cimm=PhoneNumberUtils.toCallerIDMinMatch(number);
  140. // return dbUtil.isContentExist(context,
  141. // // ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  142. // Uri.withAppendedPath(
  143. // ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
  144. // Uri.encode(number)),
  145. // // ContactsContract.CommonDataKinds.Phone.NUMBER + "= '" +
  146. // // number+ "'");
  147. // /* "min_match = '" + cimm+ "'" */null);
  148. // }
  149. // static boolean isSuspect(SQLiteDatabase db, String number) {
  150. // return dbUtil.isRecordExist(db, "user_suspectlist", "number = '"
  151. // + number + "'");
  152. // }
  153. // public static int get_CallNumberType_old(Context context,
  154. // String incomingNumber)
  155. // {
  156. // int type = NUMBER_TYPE_NULL;
  157. // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
  158. //
  159. // boolean bBlockUnknown = context
  160. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  161. // "BlockUnknownNumber", false);
  162. // boolean bignorcontact = context
  163. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  164. // "IgnoreContact", false);
  165. // if ((incomingNumber == null || incomingNumber.equals("")))
  166. // if (bBlockUnknown)
  167. // type = NUMBER_TYPE_BLACK;
  168. // else
  169. // type = NUMBER_TYPE_NORMAL;
  170. // else
  171. // {
  172. // type = query_NumberType(db, incomingNumber);
  173. // if (type == NUMBER_TYPE_WHITE)
  174. // {
  175. //
  176. // }
  177. // else
  178. // if (bignorcontact && isContact(context, incomingNumber))
  179. // {
  180. // type = NUMBER_TYPE_CONTACT;
  181. // dbgUtil.Log(Log.ERROR,null, "number type contact");
  182. // }
  183. // else
  184. // if (type == NUMBER_TYPE_BLACK)
  185. // {
  186. // }
  187. // else
  188. // if (dbUtil.isRecordExist(db, "user_suspectlist",
  189. // "number = '" + incomingNumber + "'"))
  190. // {
  191. // type = NUMBER_TYPE_SUSPECT;
  192. // }
  193. // else
  194. // {
  195. // type = NUMBER_TYPE_NORMAL;
  196. //
  197. // }
  198. // }
  199. // dbUtil.CloseDB(db);
  200. //
  201. // dbgUtil.Log(Log.ERROR,"get_CallNumberType",
  202. // "number="+incomingNumber+" type="+type);
  203. // return type;
  204. //
  205. // }
  206. // public static NumberInfo get_NumberInfo(Context context,
  207. // String incomingNumber)
  208. // {
  209. // NumberInfo numberInfo = new NumberInfo();
  210. // numberInfo.incomingNumber = incomingNumber;
  211. // SQLiteDatabase db = dbUtil.OpenDB(context, null, true);
  212. //
  213. // // boolean bBlockUnknown = context
  214. // // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  215. // // "BlockUnknownNumber", false);
  216. // // boolean bignorcontact = context
  217. // // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  218. // // "IgnoreContact", false);
  219. //
  220. // // int type = NUMBER_TYPE_NULL;
  221. // if (incomingNumber == null || incomingNumber.length() < 3)
  222. // {
  223. // numberInfo.BaseType = NUMBER_TYPE_UNKNOWN;
  224. // }
  225. // else
  226. // {
  227. // numberInfo.BaseType = query_NumberType(db, incomingNumber);
  228. // if (numberInfo.BaseType == -1)// not in user_numberlist;
  229. // {
  230. // numberInfo.BaseType = NUMBER_TYPE_NORMAL;
  231. //
  232. // // else
  233. // // if (isSuspect(db, incomingNumber))
  234. // // {
  235. // // type = NUMBER_TYPE_SUSPECT;
  236. // //
  237. // // }
  238. // // else
  239. // // {
  240. // // type = NUMBER_TYPE_NORMAL;
  241. // // }
  242. // }
  243. // if (isContact(context, incomingNumber))
  244. // {
  245. // numberInfo.isContact = true;
  246. // // type = NUMBER_TYPE_CONTACT;
  247. // }
  248. // if (isSuspect(db, incomingNumber))
  249. // {
  250. // numberInfo.isSuspect = true;
  251. //
  252. // }
  253. //
  254. // // else
  255. // // if (type == NUMBER_TYPE_BLACK)
  256. // // {
  257. // // if (isContact(context, incomingNumber))
  258. // // {
  259. // // type = NUMBER_TYPE_CONTACT;
  260. // // }
  261. // // }
  262. // }
  263. // dbUtil.CloseDB(db);
  264. //
  265. // dbgUtil.Log(Log.ERROR, "get_NumberInfo", "number=" + incomingNumber
  266. // + " type=" + numberInfo.BaseType);
  267. // return numberInfo;
  268. //
  269. // }
  270. // public static int get_SmsNumberType(Context context, String
  271. // incomingNumber)
  272. // {
  273. // SQLiteDatabase db = dbUtil.OpenDB(context, null, false);
  274. //
  275. // boolean bBlockUnknown = context
  276. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  277. // "BlockUnknownNumber", false);
  278. // boolean bignorcontact = context
  279. // .getSharedPreferences("PhoneAsstPref", 0).getBoolean(
  280. // "IgnoreContact", false);
  281. //
  282. // int type = NUMBER_TYPE_NULL;
  283. // if ((incomingNumber == null || incomingNumber.equals("")))
  284. // if (bBlockUnknown)
  285. // type = NUMBER_TYPE_BLACK;
  286. // else
  287. // type = NUMBER_TYPE_NORMAL;
  288. // else
  289. // {
  290. // type = query_NumberType(db, incomingNumber);
  291. // if (type == NUMBER_TYPE_WHITE)
  292. // {
  293. //
  294. // }
  295. // else
  296. // if (bignorcontact && isContact(context, incomingNumber))
  297. // {
  298. // type = NUMBER_TYPE_CONTACT;
  299. // dbgUtil.Log(Log.ERROR, null, "number type contact");
  300. // }
  301. // else
  302. // if (type == NUMBER_TYPE_BLACK)
  303. // {
  304. // }
  305. // else
  306. // {
  307. // type = NUMBER_TYPE_NORMAL;
  308. //
  309. // }
  310. // }
  311. // dbUtil.CloseDB(db);
  312. //
  313. // return type;
  314. //
  315. // }
  316. public static void CloseDB(SQLiteDatabase db)
  317. {
  318. if (db == null)
  319. return;
  320. if (db.isOpen() == false)
  321. return;
  322. db.close();
  323. return;
  324. }
  325. public static void CloseCursor(Cursor c)
  326. {
  327. if (c == null)
  328. return;
  329. if (c.isClosed())
  330. return;
  331. c.close();
  332. return;
  333. }
  334. public static SQLiteDatabase OpenDB(Context context, CursorFactory factory,
  335. boolean writable)
  336. {
  337. SqlOpenHelper dbHelper = new SqlOpenHelper(context, "redantpop.db",
  338. factory, 1);
  339. dbHelper.addInitSQL("create table users (" + "_id INTEGER PRIMARY KEY,"
  340. + "name varchar(20)," + "pass varchar(20))");
  341. // dbHelper.addInitSQL("create table scan_records ("
  342. // + "_id INTEGER PRIMARY KEY," + "submit BOOLEAN DEFAULT (0),"
  343. // + "picker VARCHAR(20)," + "pid varchar(20))");
  344. dbHelper.addInitSQL("create table pics (" + "_id INTEGER PRIMARY KEY,"
  345. + "picker varchar(20)," + "server varchar(20),"+ "err_code INTEGER ,"
  346. + "pid varchar(20)," + "local_path varchar(256))");
  347. // dbHelper.addInitSQL("create table pids (" + "_id INTEGER PRIMARY KEY,"
  348. // + "picker varchar(20)," + "err_code INTEGER ,"
  349. // + "pid varchar(20))");
  350. SQLiteDatabase db = dbHelper.OpenDB(writable);
  351. return db;
  352. }
  353. public static void removeRecords(SQLiteDatabase db, String table,
  354. String whereclause)
  355. {
  356. db.execSQL("delete from " + table + " where " + whereclause);
  357. }
  358. public static long addRecordsFromCursor(SQLiteDatabase db,
  359. String tablename, Cursor source, String[] from, String[] to,
  360. boolean dumpPrimaryKey, boolean dumpOneRecord)
  361. {
  362. int ret = -1;
  363. ContentValues cv = new ContentValues();
  364. if (from == null)
  365. from = source.getColumnNames();
  366. if (to == null)
  367. to = source.getColumnNames();
  368. if (from.length != to.length)// 字段数不匹配
  369. return ret;
  370. while (source.moveToNext())
  371. {
  372. for (int i = 0; i < from.length; i++)
  373. {
  374. if (from[i].equals("_id"))
  375. {
  376. if (dumpPrimaryKey)
  377. cv.put(to[i], source.getInt(i));
  378. }
  379. else
  380. {
  381. cv.put(to[i], source.getString(i));
  382. }
  383. }
  384. db.insert(tablename, null, cv);
  385. if (dumpOneRecord)
  386. break;
  387. }
  388. ret = 1;
  389. return ret;
  390. }
  391. // public static long addRecord(SQLiteDatabase db, String table,
  392. // ContentValues record)
  393. // {
  394. // long ret = -1;
  395. // try
  396. // {
  397. // ret = db.insert(table, null, record);
  398. // }
  399. // catch (SQLiteException e)
  400. // {
  401. // dbgUtil.Log(Log.ERROR,"sql_err", e.toString());
  402. //
  403. // }
  404. // return ret;
  405. // }
  406. public static List<String> get_userlist()
  407. {
  408. List<String> ret = new ArrayList<String>();
  409. SQLiteDatabase db = OpenDB(RedAntApplication.getInstance()
  410. .getApplicationContext(), null, false);
  411. Cursor cursor = db.query(
  412. "users",
  413. new String[] { "name" },
  414. null, null, null,
  415. null, "name desc", null);
  416. while (cursor.moveToNext())
  417. {
  418. String user="";
  419. try
  420. {
  421. user = Crypto.decrypt("apexu", cursor.getString(0));
  422. }
  423. catch (Exception e)
  424. {
  425. // TODO Auto-generated catch block
  426. e.printStackTrace();
  427. }
  428. ret.add(user);
  429. }
  430. CloseCursor(cursor);
  431. CloseDB(db);
  432. return ret;
  433. }
  434. public static boolean isTableEmpty(SQLiteDatabase db, String table)
  435. {
  436. boolean ret = true;
  437. Cursor cursor = null;
  438. try
  439. {
  440. cursor = db.query(table, new String[] { "*" }, null, null, null,
  441. null, null, "1");
  442. if (cursor.moveToNext())
  443. {
  444. // cursor.close();
  445. ret = false;
  446. }
  447. else
  448. ret = true;
  449. }
  450. catch (SQLiteException e)
  451. {
  452. Log.e("sql_err", e.toString());
  453. }
  454. finally
  455. {
  456. dbUtil.CloseCursor(cursor);
  457. }
  458. return ret;
  459. }
  460. public static boolean isRecordExist(SQLiteDatabase db, String table,
  461. String where)
  462. {
  463. boolean ret = false;
  464. Cursor cursor = null;
  465. try
  466. {
  467. cursor = db.query(table, new String[] { "_id" }, where, null, null,
  468. null, null, null);
  469. if (cursor.moveToNext())
  470. ret = true;
  471. else
  472. ret = false;
  473. }
  474. catch (SQLiteException e)
  475. {
  476. Log.e("sql_err", e.toString());
  477. }
  478. finally
  479. {
  480. dbUtil.CloseCursor(cursor);
  481. }
  482. return ret;
  483. }
  484. // if(isTableEmpty(db,"block_records"))
  485. }