Clear history of Chrome browser - android

How to clear history of chrome browser. For native browser i use following code -
Browser.clearHistory(getContentResolver());
But this does not work for chrome. How can i clear chrome browser history?
Is it possible?

It work fo me
Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
String TITLE = "title";
String VISITS = "visits";
String BOOKMARK = "bookmark";
ContentResolver resolver = getContentResolver();
try {
Log.i("DEBUG_", "ContentResolver");
Cursor c = resolver.query(
BOOKMARKS_URI,
null,
null, null, null);
c.moveToFirst();
Log.i("DEBUG_", "Cursor : " + c.toString());
resolver.delete(BOOKMARKS_URI, "bookmark == 1", null);
Log.i("DEBUG_", "ContentResolver delete ");
} catch (IllegalStateException e) {
Log.i("DEBUG_", "IllegalStateException " + e.getMessage());
e.printStackTrace();
}
or
Cursor faves = managedQuery(BOOKMARKS_URI,
requestedColumns,
null, null, null);

Yes it is posssible to clear chrome history from your application. Please see below.
/**
* Clear the browser history
*/
private void clearChromeHistory(){
ContentResolver cr = getContentResolver();
Uri historyUri = Uri.parse("content://com.android.chrome.browser/history");
deleteChromeHistoryJava(cr, historyUri, null, null);
}
/**
* Delete chrome browser hisory
* #param cr content resolver
* #param whereClause Uri of the browser history query
* #param projection projection array
* #param selection selection item
*/
private void deleteChromeHistoryJava(ContentResolver cr, Uri whereClause, String[] projection, String selection) {
Cursor mCursor = null;
try {
mCursor = cr.query(whereClause, projection, selection,
null, null);
Log.i("deleteChromeHistoryJava", " Query: " + whereClause);
if (mCursor != null) {
mCursor.moveToFirst();
int count = mCursor.getColumnCount();
String COUNT = String.valueOf(count);
Log.i("deleteChromeHistoryJava", " mCursor count" + COUNT);
String url = "";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
while (!mCursor.isAfterLast()) {
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
Log.i("deleteChromeHistoryJava", " url: " + url);
mCursor.moveToNext();
}
}
cr.delete(whereClause, selection, null);
Log.i("deleteChromeHistoryJava", " GOOD");
}
} catch (IllegalStateException e) {
Log.i("deleteChromeHistoryJava", " IllegalStateException: " + e.getMessage());
} finally {
if (mCursor != null) mCursor.close();
}
}
Add permission in manifest
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>

Related

Android programmatically delete Chrome / default browser cookies, history, searches

Like the title. I want to delete cookie, cache of browser of Android by coding in my application. (browser not webview)
Thank you!
In your Activity or Service, add
ContentResolver cR = getContentResolver();
if(Browser.canClearHistory(cR)){
Browser.clearHistory(cR);
Browser.clearSearches(cR);
}
where Browser is the android.provider.Browser class.
This will clear the default browser's history.
Yes it is possible to clear chrome history & searches from your application. Please see below.
/**
* Clear the browser history
*/
private void clearChromeHistory(){
ContentResolver cr = getContentResolver();
Uri historyUri = Uri.parse("content://com.android.chrome.browser/history");
Uri searchesUri = Uri.parse("content://com.android.chrome.browser/searches");
deleteChromeHistoryJava(cr, historyUri, null, null);
deleteChromeHistoryJava(cr, searchesUri, null, null);
}
/**
* Delete chrome browser hisory
* #param cr content resolver
* #param whereClause Uri of the browser history query
* #param projection projection array
* #param selection selection item
*/
private void deleteChromeHistoryJava(ContentResolver cr, Uri whereClause, String[] projection, String selection) {
Cursor mCursor = null;
try {
mCursor = cr.query(whereClause, projection, selection,
null, null);
Log.i("deleteChromeHistoryJava", " Query: " + whereClause);
if (mCursor != null) {
mCursor.moveToFirst();
int count = mCursor.getColumnCount();
String COUNT = String.valueOf(count);
Log.i("deleteChromeHistoryJava", " mCursor count" + COUNT);
String url = "";
if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
while (!mCursor.isAfterLast()) {
url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
Log.i("deleteChromeHistoryJava", " url: " + url);
mCursor.moveToNext();
}
}
cr.delete(whereClause, selection, null);
Log.i("deleteChromeHistoryJava", " GOOD");
}
} catch (IllegalStateException e) {
Log.i("deleteChromeHistoryJava", " IllegalStateException: " + e.getMessage());
} finally {
if (mCursor != null) mCursor.close();
}
}
Add permissions in manifest
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>
I have no idea if we can delete the cache/cookies using this, but i will post if i can get any further information.

Contact photo not updating for a select few

I was recently playing around with some code that goes through my contacts and creates an identicon for any contact that does not have a photo set. For the most part this ended up working quite well but for some reason I have a handful of contacts that will not update. Log output says it is creating the photo. The update() returns 1 indicating 1 row was updated and stepping through the code for a contact that never seems to show the new photo looks good.
The fact that only a select few are not updating is what is really bugging me and I'm guessing there must be something I am doing wrong or missing here.
private void processContacts() {
Cursor cursor = getContacts();
Log.d(TAG, "Processing " + cursor.getCount() + " contacts");
while(cursor.moveToNext()) {
final long contactId = cursor.getLong(0);
final String name = cursor.getString(1);
if (!TextUtils.isEmpty(name)) {
final Uri contactUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI,
contactId);
if(ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
contactUri, true) == null) {
Log.d(TAG, String.format("Creating identicon for %s", name));
generateIdenticon(contactId, name);
} else {
Log.i(TAG, String.format("%s already has a contact photo", name));
}
}
}
cursor.close();
}
private Cursor getContacts() {
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME };
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
return getContentResolver().query(uri, projection, null, null, sortOrder);
}
private void generateIdenticon(long contactId, String name) {
if (!TextUtils.isEmpty(name)) {
updateNotification(getString(R.string.identicons_creation_service_running_title),
String.format(getString(R.string.identicons_creation_service_contact_summary),
name));
final byte[] hash = Identicon.generateHash(name);
final byte[] identicon = Identicon.generateIdenticonByteArray(hash);
if (identicon == null) {
Log.e(TAG, "generateIdenticon() - identicon for " + name + " is null!");
} else {
if (!setContactPhoto(getContentResolver(), identicon, contactId)) {
Log.e(TAG, "Unable to save identicon for " + name);
}
}
}
}
private boolean setContactPhoto(ContentResolver resolver, byte[] bytes, long personId) {
ContentValues values = new ContentValues();
int photoRow = -1;
String where = ContactsContract.Data.RAW_CONTACT_ID + " == " +
String.valueOf(personId) + " AND " + ContactsContract.Data.MIMETYPE + "=='" +
ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'";
Cursor cursor = resolver.query(
ContactsContract.Data.CONTENT_URI,
null,
where,
null,
null);
int idIdx = cursor.getColumnIndexOrThrow(ContactsContract.Data._ID);
if(cursor.moveToFirst()){
photoRow = cursor.getInt(idIdx);
}
cursor.close();
values.put(ContactsContract.Data.RAW_CONTACT_ID, personId);
values.put(ContactsContract.Data.IS_PRIMARY, 1);
values.put(ContactsContract.Data.IS_SUPER_PRIMARY, 1);
values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, bytes);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
if (photoRow >= 0) {
final int rowsUpdated = resolver.update(ContactsContract.Data.CONTENT_URI,
values, ContactsContract.Data._ID + "=" + photoRow, null);
return rowsUpdated >= 1;
} else {
final Uri uri = resolver.insert(ContactsContract.Data.CONTENT_URI, values);
return uri != null && !TextUtils.isEmpty(uri.toString());
}
}
All this is being done inside a background service and all my contacts are synced via google. One last thing to note is that these select contacts always return null when I call ContactsContract.Contacts.openContactPhotoInputStream() to see if a photo is available (even after I've attempted to update the photo).
Any help or insight about what may be going on is greatly appreciated.

MemoryFile.finalize() called while ashmem still open - How to deal with it - Android

I have seen questions at SO regarding the same question, but none of them has an accepted answer.
What exactly is the error informing? Why does this occur?
I have few Cursors in my application, and I did close them, after their work is done. But the error still shows up. What could be the reason?
Here is the code :::
private String fetchContactIdFromPhoneNumber(String phoneNumber) {
// TODO Auto-generated method stub
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(phoneNumber));
Cursor cFetch = getContentResolver().query(uri,
new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },
null, null, null);
String contactId = "";
if (cFetch.moveToFirst()) {
cFetch.moveToFirst();
contactId = cFetch.getString(cFetch
.getColumnIndex(PhoneLookup._ID));
}
//System.out.println(contactId);
if(cFetch != null)
{
cFetch.close();
}
return contactId;
}
public Uri getPhotoUri(long contactId) {
ContentResolver contentResolver = getContentResolver();
Cursor cursor;
try {
cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
if(cursor != null)
{
cursor.close();
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
This is the code where I use cursors. I have also a contentresolver object. Should I close that as well?
And finally, what is the impact on this error on the application, because, it doesn't seem to interrupt the application's execution.

Get photo uri android 2.3.7 and android 4

i'm having some trouble about gettin photo uri between android 2.x and 4.x
The following code works perfectly on 2.x but not for 4.x
public static Uri getPhotoURIFromAddress(Context activity, String address) {
String contactId = fetchContactIdFromPhoneNumber(address,activity);
//Se non trovo il contatto il rubrica
if(contactId.equals("0")){
return null;
}
ContentResolver contentResolver = activity.getContentResolver();
try {
Cursor cursor = contentResolver
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ contactId
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cursor != null) {
if (!cursor.moveToFirst()) {
Log.i("No photo","No photo");
return null; // no photo
}
} else {
return null; // error in cursor process
}
cursor.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
How i can intregate working code for 4.x ?? Thanks

Get contact photo in Android (API 8) using LOOKUP_URI

I'm trying to get a contact image using its lookup URI.
I succeed getting the DISPLAY_NAME using this code:
Cursor c = context.getContentResolver().query(contactLookupUri,
new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null,
null, null);
But I didn't find a way to get the photo. The Photo.PHOTO option is not valid for the API I'm using, and trying to get it using an inputStream didn't work as well (perhaps I did something wrong there):
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(context.getContentResolver(),
contactUri);
Thanks,
Yoel
Eventually I solved it by getting the contact ID and using an inputStream:
public static Uri getContactLookupUri(String contactLookupKey) {
return Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_LOOKUP_URI, contactLookupKey);
}
public static Bitmap getContactImage(Context context, String contactLookupKey) {
long contactId;
try {
Uri contactLookupUri = getContactLookupUri(contactLookupKey);
Cursor c = context.getContentResolver().query(contactLookupUri,
new String[] { ContactsContract.Contacts._ID }, null, null,
null);
try {
if (c == null || c.moveToFirst() == false) {
return null;
}
contactId = c.getLong(0);
} finally {
c.close();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri contactUri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, contactId);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(context.getContentResolver(),
contactUri);
if (input != null) {
return BitmapFactory.decodeStream(input);
} else {
return null;
}
}
The below function return the image uri of your contact_id
/**
* #return the photo URI
*/
public Uri getPhotoUri() {
try {
Cursor cur = this.ctx.getContentResolver().query(
ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "
+ ContactsContract.Data.MIMETYPE + "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,
null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null; // no photo
}
} else {
return null; // error in cursor process
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long
.parseLong(getId()));
return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
Also refer this LINK

Categories

Resources