I have an application that runs under system account and one of the features is create custom APNs depending the client's configurations.
The code below works fine, but just create the APN and set it as default for simcard 1. O would like to set it depending on simcards if the software is running on Android 5 or above. I could not find any documentation about it. Anyone knows something else about APNs?
public static int createNewApn(Context context, APN apn, boolean setAsDefaultAPN) {
int apnid = -1;
try {
if (apn != null) {
Uri APN_URI = Uri.parse("content://telephony/carriers");
ContentResolver resolver = context.getContentResolver();
ContentValues values = prepareValues(apn);
Cursor c = null;
Uri newRow = resolver.insert(APN_URI, values);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
int tableIndex = c.getColumnIndex("_id");
c.moveToFirst();
apnid = c.getShort(tableIndex);
}
if (c != null) {
c.close();
}
if (apnid > -1 && setAsDefaultAPN) {
ContentValues v = new ContentValues(1);
v.put("apn_id", apnid);
context.getContentResolver().update(APN_PREFER_URI, v, null, null);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return apnid;
}
Related
I am developing an Android Application to wipe all data from Android including photos, videos, contacts, messages, apps,call logs, application data etc. programmatically. I wish to achieve this with an Android Application only and not through adb.
The approach I am thinking of somehow I manage to Clear data from phone and rewrite the memory with some junk files or binary data 1010 say. (Its just an idea!)
All I have managed to do is delete photos,videos,images,contacts,call logs,messages from Device using Cursor and getContentResolver. But we can recover that using third party Data Recover Softwares.
I have also reset the device using device administrator but the cons of this is the data can be recovered through it.
So, all i need is which approach to follow for hard resetting the device.
I have used below code .
public static void deleteAllContacts(Context activity) {
ContentResolver contentResolver = activity.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
contentResolver.delete(uri, null, null);
}
}
public static void deleteAllCallLog(Context activity) {
activity.getContentResolver().delete(CallLog.Calls.CONTENT_URI, null, null);
}
/* public static void deleteAllMessages(Context activity) {
ContentResolver localContentResolver = activity.getContentResolver();
Uri localUri = Uri.parse("content://sms");
for (int i = localContentResolver.delete(Telephony.Sms.CONTENT_URI, null, null); i == 1; i = localContentResolver.delete(localUri, null, null)) {
}
}*/
public static void deleteVideoFiles(Activity activity) {
String[] videoProjection = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE};
Cursor videoCursor = activity.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoProjection, null, null, null);
int count = videoCursor.getCount();
for (int i = 0; i < count; i++) {
int indexID = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
videoCursor.moveToPosition(i);
String path = getVideoFilePath(videoCursor.getString(indexID), activity);
Log.d("Video Path", path);
File file = new File(path);
boolean isDeleted = file.delete();
Log.d("isDELETED", String.valueOf(isDeleted));
}
videoCursor.close();
}
public static void deleteAudioFiles(Activity activity) {
String[] proj = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.SIZE};
Cursor audioCursor = activity.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
int indexId = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
String path = getAudioFilePath(audioCursor.getString(indexId), activity);
Log.d("Audio Path", path);
File file = new File(path);
boolean isDeleted = file.delete();
Log.d("isDELETED", String.valueOf(isDeleted));
} while (audioCursor.moveToNext());
}
}
if (audioCursor != null) audioCursor.close();
}
public static void deleteImageFiles(Activity activity) {
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.ImageColumns.DATA};
Cursor c = null;
SortedSet<String> dirList = new TreeSet<String>();
String[] directories = null;
if (u != null) {
c = activity.managedQuery(u, projection, null, null, null);
}
if ((c != null) && (c.moveToFirst())) {
do {
String tempDir = c.getString(0);
tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
try {
dirList.add(tempDir);
} catch (Exception e) {
}
}
while (c.moveToNext());
directories = new String[dirList.size()];
dirList.toArray(directories);
}
for (int i = 0; i < dirList.size(); i++) {
File imageDir = new File(directories[i]);
File[] imageList = imageDir.listFiles();
if (imageList == null)
continue;
for (File imagePath : imageList) {
try {
if (imagePath.isDirectory()) {
imageList = imagePath.listFiles();
}
if (imagePath.getName().contains(".jpg") || imagePath.getName().contains(".JPG")
|| imagePath.getName().contains(".jpeg") || imagePath.getName().contains(".JPEG")
|| imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG")
|| imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF")
|| imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP")
) {
Log.d("Absolute Path", imagePath.getAbsolutePath());
File file = new File(imagePath.getAbsolutePath());
boolean isDeleted = file.delete();
Log.d("iSDELETED", String.valueOf(isDeleted));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static String getAudioFilePath(String contentId, Activity activity) {
Uri theMediaUri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentId);
String[] projection = {MediaStore.Audio.Media.DATA};
Cursor mCur = activity.getContentResolver().query(theMediaUri, projection, null, null, null);
int column_index = mCur.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
mCur.moveToFirst();
return mCur.getString(column_index);
}
public static String getVideoFilePath(String contentId, Activity activity) {
Uri theMediaUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentId);
String[] projection = {MediaStore.Video.Media.DATA};
Cursor mCur = activity.getContentResolver().query(theMediaUri, projection, null, null, null);
int column_index = mCur.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
mCur.moveToFirst();
return mCur.getString(column_index);
}
public static void deleteSMS(Context context) {
/* try {
Uri uriSms = Uri.parse("content://sms/inbox");
Cursor c = context.getContentResolver().query(uriSms, new String[]{"_id", "body"}, null, null, null);
if (c != null && c.moveToFirst()) {
do {
long id = c.getLong(0);
Log.d("Body : ", c.getString(1));
context.getContentResolver().delete(
Uri.parse("content://sms/" + id), null, null);
Log.e("Message:", "Message is Deleted successfully");
} while (c.moveToNext());
}
if (c != null) {
c.close();
}
} catch (Exception e) {
Log.e("Exception", e.toString());
}*/
Cursor c =context.getContentResolver().query(Uri.parse("content://sms/"), new String[]{"_id", "body"}, null, null,null);
try {
while (c.moveToNext()) {
int id = c.getInt(0);
int isDeleted = context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);
Log.d("IS DELETED" , String.valueOf(isDeleted));
}
}catch(Exception e){
Log.e("Delete Sms","Error deleting sms",e);
}finally {
c.close();
}
}
I am unable to update calendar event details programmatically. Here is the code sample. Any help should be greatly appreciated.
Uri CALENDAR_URI = Uri.parse("content://com.android.calendar/events");
Cursor c = context.getContentResolver().query(CALENDAR_URI, null, null, null, null);
String[] s = c.getColumnNames();
if (c.moveToFirst()) {
while (c.moveToNext()) {
String _id = c.getString(c.getColumnIndex("_id"));
String CalId = c.getString(c.getColumnIndex("calendar_id"));
if ((_id == null) && (CalId == null)) {
return false;
} else {
if (_id.equals(eventId) && CalId.equals(cal_Id)) {
Uri uri = ContentUris.withAppendedId(CALENDAR_URI, Integer.parseInt(_id));
ContentValues event = new ContentValues();
event.put(CalendarContract.Events.SELF_ATTENDEE_STATUS, statusId);
context.getContentResolver().update(uri, event, null, null);// need to give your data here
return true;
}
}
}
}
Seems to me that accessing contacts on Android is so slow, and needlessly involves cursors. How can it be sped up?
1.When the first time you read the contact, you may reduce some property.
2.When reading a type of property, you may use a cursor.
private static final String[] CONTACTOR_ION = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
};
Cursor phones = null;
ContentResolver cr = getContentResolver();
try {
phones = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
, CONTACTOR_ION, null, null, "sort_key");
if (phones != null) {
final int contactIdIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
final int displayNameIndex = phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
final int phoneIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneString, displayNameString, contactIdString;
while (phones.moveToNext()) {
phoneString = phones.getString(phoneIndex);
displayNameString = phones.getString(displayNameIndex);
contactIdString = phones.getString(contactIdIndex);
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
} finally {
if (phones != null)
phones.close();
}
I was wondering how Android is doing that.
Example:
WAP Push indicates an incoming MMS.
Change APN to MMS.
Download MMS.
Restore connectivity to default APN.
So how is this change of APN done?
Why:
I want to use an other APN to connect to the internet instead of the default or MMS APN.
Following Activity solves your problem of changing default APN and than revert back to original.
package com.slk.apnapp;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
public class NewAPNActivity extends Activity {
/*
* Information of all APNs Details can be found in
* com.android.providers.telephony.TelephonyProvider
*/
public static final Uri APN_TABLE_URI = Uri
.parse("content://telephony/carriers");
/*
* Information of the preferred APN
*/
public static final Uri PREFERRED_APN_URI = Uri
.parse("content://telephony/carriers/preferapn");
private static final String TAG = "CHANGE_APN";
public static final String NEW_APN = "NewAPN";
private int getDafaultAPN() {
Cursor c = this.getContentResolver().query(PREFERRED_APN_URI,
new String[] { "_id", "name" }, null, null, null);
int id = -1;
if (c != null) {
try {
if (c.moveToFirst())
id = c.getInt(c.getColumnIndex("_id"));
} catch (SQLException e) {
Log.d(TAG, e.getMessage());
}
c.close();
}
return id;
}
/*
* Set an apn to be the default apn for web traffic Require an input of the
* apn id to be set
*/
public boolean setDefaultAPN(int id) {
boolean res = false;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
// See /etc/apns-conf.xml. The TelephonyProvider uses this file to
// provide
// content://telephony/carriers/preferapn URI mapping
values.put("apn_id", id);
try {
resolver.update(PREFERRED_APN_URI, values, null, null);
Cursor c = resolver.query(PREFERRED_APN_URI, new String[] { "name",
"apn" }, "_id=" + id, null, null);
if (c != null) {
res = true;
c.close();
}
} catch (SQLException e) {
Log.d(TAG, e.getMessage());
}
return res;
}
private int checkNewAPN() {
int id = -1;
Cursor c = this.getContentResolver().query(APN_TABLE_URI,
new String[] { "_id", "name" }, "name=?",
new String[] { NEW_APN }, null);
if (c == null) {
id = -1;
} else {
int record_cnt = c.getCount();
if (record_cnt == 0) {
id = -1;
} else if (c.moveToFirst()) {
if (c.getString(c.getColumnIndex("name")).equalsIgnoreCase(
NEW_APN)) {
id = c.getInt(c.getColumnIndex("_id"));
}
}
c.close();
}
return id;
}
public int addNewAPN() {
int id = -1;
ContentResolver resolver = this.getContentResolver();
ContentValues values = new ContentValues();
values.put("name", NEW_APN);
values.put("apn", NEW_APN);
/*
* The following three field values are for testing in Android emulator
* only The APN setting page UI will ONLY display APNs whose 'numeric'
* filed is TelephonyProperties.PROPERTY_SIM_OPERATOR_NUMERIC. On
* Android emulator, this value is 310260, where 310 is mcc, and 260
* mnc. With these field values, the newly added apn will appear in
* system UI.
*/
values.put("mcc", "310");
values.put("mnc", "260");
values.put("numeric", "310260");
Cursor c = null;
try {
Uri newRow = resolver.insert(APN_TABLE_URI, values);
if (newRow != null) {
c = resolver.query(newRow, null, null, null, null);
// Obtain the apn id
int idindex = c.getColumnIndex("_id");
c.moveToFirst();
id = c.getShort(idindex);
Log.d(TAG, "New ID: " + id + ": Inserting new APN succeeded!");
}
} catch (SQLException e) {
Log.d(TAG, e.getMessage());
}
if (c != null)
c.close();
return id;
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int id = checkNewAPN();
int default_id = getDafaultAPN();
if (id == -1) {
id = addNewAPN();
}
if (setDefaultAPN(id)) {
Log.i(TAG, NEW_APN
+ " set new default APN successfully and Default id is "
+ id);
}
if (setDefaultAPN(default_id)) {
Log.i(TAG,
NEW_APN
+ " set previous default APN successfully and Default id is "
+ default_id);
}
}
}
I have an application that adds an event to the calendar on the device. I have the following URLs for the Calendar ContentProvider:
Pre Froyo: content://calendar/calendars
Froyo: content://com.android.calendar/calendars
These urls work fine for Nexus One but do not return any calendars on HTC Desire/Incredible/Hero. Probably all phones with a Sense UI. This happens on 2.1 and 2.2.
Has anybody run into this problem before and has any workarounds?
use this code to get the URI for your platform
private String getCalendarUriBase() {
String calendarUriBase = null;
Uri calendars = Uri.parse("content://calendar/calendars");
Cursor managedCursor = null;
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://calendar/";
} else {
calendars = Uri.parse("content://com.android.calendar/calendars");
try {
managedCursor = managedQuery(calendars, null, null, null, null);
} catch (Exception e) {
}
if (managedCursor != null) {
calendarUriBase = "content://com.android.calendar/";
}
}
return calendarUriBase;
}