Guest User

Untitled

a guest
Mar 23rd, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. public int insertAPN(String name){
  2.  
  3. //Set the URIs and variables
  4. int id = -1;
  5. boolean existing = false;
  6. final Uri APN_TABLE_URI = Uri.parse("content://telephony/carriers");
  7. final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
  8.  
  9. //Check if the specified APN is already in the APN table, if so skip the insertion
  10. Cursor parser = getContentResolver().query(APN_TABLE_URI, null, null, null, null);
  11. parser.moveToLast();
  12. while (parser.isBeforeFirst() == false){
  13. int index = parser.getColumnIndex("name");
  14. String n = parser.getString(index);
  15. if (n.equals(name)){
  16. existing = true;
  17. Toast.makeText(getApplicationContext(), "APN already configured.",Toast.LENGTH_SHORT).show();
  18. break;
  19. }
  20. parser.moveToPrevious();
  21. }
  22.  
  23. //if the entry doesn't already exist, insert it into the APN table
  24. if (!existing){
  25.  
  26. //Initialize the Content Resolver and Content Provider
  27. ContentResolver resolver = this.getContentResolver();
  28. ContentValues values = new ContentValues();
  29.  
  30. //Capture all the existing field values excluding name
  31. Cursor apu = getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
  32. apu.moveToFirst();
  33. int index;
  34.  
  35. index = apu.getColumnIndex("apn");
  36. String apn = apu.getString(index);
  37. index = apu.getColumnIndex("type");
  38. String type = apu.getString(index);
  39. index = apu.getColumnIndex("proxy");
  40. String proxy = apu.getString(index);
  41. index = apu.getColumnIndex("port");
  42. String port = apu.getString(index);
  43. index = apu.getColumnIndex("user");
  44. String user = apu.getString(index);
  45. index = apu.getColumnIndex("password");
  46. String password = apu.getString(index);
  47. index = apu.getColumnIndex("server");
  48. String server = apu.getString(index);
  49. index = apu.getColumnIndex("mmsc");
  50. String mmsc = apu.getString(index);
  51. index = apu.getColumnIndex("mmsproxy");
  52. String mmsproxy = apu.getString(index);
  53. index = apu.getColumnIndex("mmsport");
  54. String mmsport = apu.getString(index);
  55. index = apu.getColumnIndex("mcc");
  56. String mcc = apu.getString(index);
  57. index = apu.getColumnIndex("mnc");
  58. String mnc = apu.getString(index);
  59. index = apu.getColumnIndex("numeric");
  60. String numeric = apu.getString(index);
  61.  
  62. //Assign them to the ContentValue object
  63. values.put("name", name); //the method parameter
  64. values.put("apn", apn);
  65. values.put("type", type);
  66. values.put("proxy", proxy);
  67. values.put("port", port);
  68. values.put("user", user);
  69. values.put("password", password);
  70. values.put("server", server);
  71. values.put("mmsc", mmsc);
  72. values.put("mmsproxy", mmsproxy);
  73. values.put("mmsport", mmsport);
  74. values.put("mcc", mcc);
  75. values.put("mnc", mnc);
  76. values.put("numeric", numeric);
  77.  
  78. //Actual insertion into table
  79. Cursor c = null;
  80. try{
  81. Uri newRow = resolver.insert(APN_TABLE_URI, values);
  82.  
  83. if(newRow != null){
  84. c = resolver.query(newRow, null, null, null, null);
  85. int idindex = c.getColumnIndex("_id");
  86. c.moveToFirst();
  87. id = c.getShort(idindex);
  88. }
  89. }
  90. catch(SQLException e){}
  91. if(c !=null ) c.close();
  92. }
  93.  
  94. return id;
  95. }
  96.  
  97. //Takes the ID of the new record generated in InsertAPN and sets that particular record the default preferred APN configuration
  98. public boolean setPreferredAPN(int id){
  99.  
  100. //If the id is -1, that means the record was found in the APN table before insertion, thus, no action required
  101. if (id == -1){
  102. return false;
  103. }
  104.  
  105. Uri.parse("content://telephony/carriers");
  106. final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
  107.  
  108. boolean res = false;
  109. ContentResolver resolver = this.getContentResolver();
  110. ContentValues values = new ContentValues();
  111.  
  112. values.put("apn_id", id);
  113. try{
  114. resolver.update(PREFERRED_APN_URI, values, null, null);
  115. Cursor c = resolver.query(PREFERRED_APN_URI, new String[]{"name", "apn"}, "_id="+id, null, null);
  116. if(c != null){
  117. res = true;
  118. c.close();
  119. }
  120. }
  121. catch (SQLException e){}
  122. return res;
  123. }
  124.  
  125. int identity = insertAPN("tmobile");
  126. setPreferredAPN(identity);
Add Comment
Please, Sign In to add comment