PhoneInfo.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. class PhoneInfo {
  2. constructor() {
  3. this.runtimeMainActivity = null;
  4. this.sms_cs = null;
  5. this.sms_read_finished = false;
  6. this.call_log = null;
  7. this.call_log_cs = null;
  8. this.call_log_read_finished = false;
  9. this.data_contacts = [];
  10. this.data_sms = [];
  11. this.data_call_log = [];
  12. }
  13. pushData(list, data) {
  14. if (list == "" || list == null || list == undefined) {
  15. list = [];
  16. }
  17. if (!data) {
  18. return list;
  19. }
  20. if (list.length <= 0) {
  21. list.push(data);
  22. return list;
  23. }
  24. var needAdd = true;
  25. let targetData = JSON.stringify(data);
  26. for (let sourceData of list) {
  27. let checkData = JSON.stringify(sourceData);
  28. if (targetData == checkData) {
  29. needAdd = false;
  30. break;
  31. }
  32. }
  33. if (needAdd) {
  34. list.push(data);
  35. }
  36. return list;
  37. }
  38. pushSMS(data) {
  39. this.data_sms = this.pushData(this.data_sms, data);
  40. }
  41. pushCALLLOG(data) {
  42. this.data_call_log = this.pushData(this.data_call_log, data);
  43. }
  44. isAndroid() {
  45. // #ifdef APP-PLUS
  46. return plus.os.name == "Android";
  47. // #endif
  48. return false;
  49. }
  50. getRuntimeMainActivity() {
  51. if (!this.isAndroid()) {
  52. return null;
  53. }
  54. if (this.runtimeMainActivity == null) {
  55. this.runtimeMainActivity = plus.android.runtimeMainActivity();
  56. }
  57. return this.runtimeMainActivity;
  58. }
  59. buildCallLogData(cs) {
  60. var number = cs.getString(cs.getColumnIndex(this.call_log.Calls.NUMBER));
  61. var type;
  62. switch (parseInt(cs.getString(cs.getColumnIndex(this.call_log.Calls.TYPE)))) {
  63. case this.call_log.Calls.INCOMING_TYPE:
  64. type = '呼入';
  65. break;
  66. case this.call_log.Calls.OUTGOING_TYPE:
  67. type = '呼出';
  68. break;
  69. case this.call_log.Calls.MISSED_TYPE:
  70. type = '未接';
  71. break;
  72. default:
  73. type = '挂断';
  74. break;
  75. }
  76. var date = new Date(parseInt(cs.getString(cs.getColumnIndexOrThrow(this.call_log.Calls.DATE))));
  77. var Name_Col = cs.getColumnIndexOrThrow(this.call_log.Calls.CACHED_NAME);
  78. var name = cs.getString(Name_Col);
  79. var Duration_Col = cs.getColumnIndexOrThrow(this.call_log.Calls.DURATION);
  80. var duration = cs.getString(Duration_Col);
  81. return {
  82. name: name, // 联系人的姓名
  83. mobile: number, // 联系人电话
  84. callTime: date, // 呼入或呼出时间
  85. talkTime: duration, // 通话时长
  86. type: type
  87. };
  88. }
  89. buildSmsData(cs) {
  90. var index_Address = cs.getColumnIndex('address');
  91. var address = cs.getString(index_Address);
  92. var index_Body = cs.getColumnIndex('body');
  93. var body = cs.getString(index_Body);
  94. var index_Type = cs.getColumnIndex('type');
  95. var type = cs.getString(index_Type);
  96. return {
  97. tel: address,
  98. content: body,
  99. type: type
  100. }
  101. }
  102. /**
  103. * 读取所有短信
  104. */
  105. async readSms(size) {
  106. if (!this.isAndroid()) {
  107. return;
  108. }
  109. return new Promise((resolve, reject) => {
  110. uni.showLoading({
  111. mask:true,
  112. title:'正在加载...'
  113. })
  114. try {
  115. var main = this.getRuntimeMainActivity();
  116. var Uri = plus.android.importClass('android.net.Uri');
  117. var ContactsContract = plus.android.importClass('android.provider.ContactsContract');
  118. var uri = Uri.parse('content://sms/');
  119. var cr = main.getContentResolver();
  120. plus.android.importClass(cr);
  121. this.sms_cs = cr.query(uri, null, null, null, null);
  122. plus.android.importClass(this.sms_cs);
  123. var count = 0;
  124. let res = this.sms_cs.moveToNext();
  125. while (res) {
  126. let data = this.buildSmsData(this.sms_cs);
  127. this.pushSMS(data);
  128. count++;
  129. if (count > size) {
  130. break;
  131. }
  132. res = this.sms_cs.moveToNext();
  133. }
  134. uni.hideLoading()
  135. resolve(this.data_sms)
  136. this.sms_read_finished = !res;
  137. } catch (e) {
  138. uni.hideLoading()
  139. reject('error')
  140. }
  141. })
  142. }
  143. /**
  144. * 读取所有通话记录
  145. */
  146. async readCallLog(size) {
  147. if (!this.isAndroid()) {
  148. return;
  149. }
  150. return new Promise((resolve, reject) => {
  151. uni.showLoading({
  152. mask:true,
  153. title:'正在加载...'
  154. })
  155. try {
  156. this.call_log = plus.android.importClass('android.provider.CallLog');
  157. var ContentResolver = plus.android.importClass('android.content.ContentResolver');
  158. var activity = this.getRuntimeMainActivity();
  159. var resolver = activity.getContentResolver();
  160. plus.android.importClass(resolver);
  161. var String = plus.android.importClass('java.lang.String');
  162. this.call_log_cs = resolver.query(this.call_log.Calls.CONTENT_URI, null, null, null, this
  163. .call_log.Calls
  164. .DEFAULT_SORT_ORDER);
  165. plus.android.importClass(this.call_log_cs);
  166. var count = 0;
  167. let res = this.call_log_cs.moveToNext();
  168. while (res) {
  169. let data = this.buildCallLogData(this.call_log_cs);
  170. this.pushCALLLOG(data);
  171. count++;
  172. if (count > size) {
  173. break;
  174. }
  175. res = this.call_log_cs.moveToNext();
  176. }
  177. uni.hideLoading()
  178. resolve(this.data_call_log)
  179. } catch (e) {
  180. uni.hideLoading()
  181. reject('error')
  182. }
  183. })
  184. }
  185. /**
  186. * 读取所有通讯录
  187. */
  188. async readContacts() {
  189. return new Promise((resolve, reject) => {
  190. uni.showLoading({
  191. mask:true,
  192. title:'正在加载...'
  193. })
  194. try {
  195. plus.contacts.getAddressBook(
  196. plus.contacts.ADDRESSBOOK_PHONE,
  197. function(addressbook) {
  198. addressbook.find(
  199. ['displayName', 'phoneNumbers'],
  200. function(contacts) {
  201. if (contacts) {
  202. let phones = [];
  203. for (let c of contacts) {
  204. let numbs = c.phoneNumbers;
  205. let ps = [];
  206. for (let n of numbs) {
  207. ps.push(n.value);
  208. }
  209. phones.push({
  210. name: c.displayName,
  211. phones: ps
  212. });
  213. }
  214. uni.hideLoading()
  215. resolve(phones);
  216. } else {
  217. uni.hideLoading()
  218. resolve(null);
  219. }
  220. },
  221. function() {
  222. uni.hideLoading()
  223. resolve(null);
  224. }, {
  225. multiple: true
  226. }
  227. );
  228. },
  229. function(e) {
  230. uni.hideLoading()
  231. resolve(null);
  232. }
  233. );
  234. } catch (e) {
  235. uni.hideLoading()
  236. resolve(null);
  237. }
  238. });
  239. }
  240. async needPermission(callback) { //需要的授权
  241. // #ifdef APP-PLUS
  242. return await new Promise((resolve, reject) => {
  243. plus.android.requestPermissions(
  244. ['android.permission.READ_SMS', 'android.permission.RECEIVE_SMS',
  245. 'android.permission.READ_CALL_LOG',
  246. 'android.permission.WRITE_CALL_LOG'
  247. ],
  248. function(e) {
  249. if (e.deniedAlways.length > 0) {
  250. resolve(false);
  251. return;
  252. }
  253. if (e.deniedPresent.length > 0) {
  254. resolve(false);
  255. return;
  256. }
  257. resolve(true);
  258. return;
  259. },
  260. function(e) {
  261. resolve(false);
  262. }
  263. );
  264. });
  265. // #endif
  266. }
  267. }
  268. export default PhoneInfo;