Is it possible to exclude SIM contacts when using Intent.ACTION_PICK? - android

I need to pick contacts in my app and would like to exclude those which are stored in my SIM card. Is it possible with ACTION_PICK?

No, it's not possible
Unfortunately, it's not possible for now.
To proof it, let's dive into source code of ContanctsListActivity.
Here's an onCreate() method of the Activity. In it, ContactApp reads Intent(ACTION_PICK) we passing to it and handles it respectively:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
mIconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
mContactsPrefs = new ContactsPreferences(this);
mPhotoLoader = new ContactPhotoLoader(this, R.drawable.ic_contact_list_picture);
// Resolve the intent
final Intent intent = getIntent();
// Allow the title to be set to a custom String using an extra on the intent
String title = intent.getStringExtra(UI.TITLE_EXTRA_KEY);
if (title != null) {
setTitle(title);
}
String action = intent.getAction();
String component = intent.getComponent().getClassName();
// When we get a FILTER_CONTACTS_ACTION, it represents search in the context
// of some other action. Let's retrieve the original action to provide proper
// context for the search queries.
if (UI.FILTER_CONTACTS_ACTION.equals(action)) {
mSearchMode = true;
mShowSearchSnippets = true;
Bundle extras = intent.getExtras();
if (extras != null) {
mInitialFilter = extras.getString(UI.FILTER_TEXT_EXTRA_KEY);
String originalAction =
extras.getString(ContactsSearchManager.ORIGINAL_ACTION_EXTRA_KEY);
if (originalAction != null) {
action = originalAction;
}
String originalComponent =
extras.getString(ContactsSearchManager.ORIGINAL_COMPONENT_EXTRA_KEY);
if (originalComponent != null) {
component = originalComponent;
}
} else {
mInitialFilter = null;
}
}
Log.i(TAG, "Called with action: " + action);
mMode = MODE_UNKNOWN;
if (UI.LIST_DEFAULT.equals(action) || UI.FILTER_CONTACTS_ACTION.equals(action)) {
.....
else if (Intent.ACTION_PICK.equals(action)) {
// XXX These should be showing the data from the URI given in
// the Intent.
final String type = intent.resolveType(this);
if (Contacts.CONTENT_TYPE.equals(type)) {
mMode = MODE_PICK_CONTACT;
} else if (People.CONTENT_TYPE.equals(type)) {
mMode = MODE_LEGACY_PICK_PERSON;
} else if (Phone.CONTENT_TYPE.equals(type)) {
mMode = MODE_PICK_PHONE;
} else if (Phones.CONTENT_TYPE.equals(type)) {
mMode = MODE_LEGACY_PICK_PHONE;
} else if (StructuredPostal.CONTENT_TYPE.equals(type)) {
mMode = MODE_PICK_POSTAL;
} else if (ContactMethods.CONTENT_POSTAL_TYPE.equals(type)) {
mMode = MODE_LEGACY_PICK_POSTAL;
}
....
// VERY LONG IF WITH DIFFERENT MODE-SELECTION
....
}
.....
if (mMode == MODE_JOIN_CONTACT) {
setContentView(R.layout.contacts_list_content_join);
} else if (mSearchMode) {
setContentView(R.layout.contacts_search_content);
} else if (mSearchResultsMode) {
setContentView(R.layout.contacts_list_search_results);
} else {
setContentView(R.layout.contacts_list_content);
}
setupListView();
...
}
It's very long method (and I also suggest to check setupListView() method), but pretty straightforward. And, as you can see, there's no parameter you can pass to specify source of contacts you want to pick from. Only thing you can configure here is the particular mMode ContactsApp to use (MODE_PICK_CONTACT, MODE_PICK_PHONE, etc.) - but unfortunately number of possible modes is very limited by 6 and non of them suits you.
(If anyone needs to pass mMode to ContanctsListActivity - use intent's setType() method, for example: intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);)
Workaround
As a workaround - as tiny sunlight's suggested in comments - render non-SIM contacts within the app and pick the one you needed from there.
How to get all android contacts but without those which are on SIM - this link looks like most promising one explaining how to query cursor with all contacts, apart from SIM ones.
I hope, it helps

Related

How can I attach an image to a "Direct Message" in twitter Android?

I programmed an app that can send a message to twitter with an image attached. It works! I tested it on several devices and asked other people to do the same. It even works for a Direct Message when a twitter friend is selected. However, it does not work when "Direct Message" is selected. This forces the user to select a friend directly instead of selecting him via "Direct Message" (which is really strange) otherwise the picture is not attached. Just have a look at the screenshot:
Here is my Xamarin Android programming code. Let me know how to fix it. Currently, all options work, even selecting my friend but not "Direct Message". I also need to tell that I do not have any issue with the twitter text I expect to see in the tweet.
public bool TweetImage(Bitmap imageToTweet)
{
var messageIntent = context.FindMessageIntent(this.twitterConstants.PackageName);
if (messageIntent == null)
{
return false;
}
string outputFileBMP = SaveBitmap(imageToTweet);
context.Tweet(messageIntent, outputFileBMP, this.twitterConstants.DefaultTwitterText, this.twitterConstants.ChooserMessage);
return true;
}
and
public static Intent FindMessageIntent(this ContextWrapper contextWrapper, params string[] packageNames)
{
Intent wantedIntent = new Intent();
wantedIntent.SetType("text/plain");
var resolveInfos = contextWrapper.PackageManager.QueryIntentActivities(wantedIntent, PackageInfoFlags.MatchDefaultOnly);
var result = (from r in resolveInfos
from p in packageNames
where p == r.ActivityInfo.PackageName
select p).FirstOrDefault();
if (result != null)
{
wantedIntent.SetPackage(result);
return wantedIntent;
}
return null;
}
and
public static void Tweet(this ContextWrapper contextWrapper, Intent messageIntent, string filePath = null, string message = null, string chooserMessage = null)
{
if (filePath != null)
{
using (var file = new Java.IO.File(filePath))
{
messageIntent.PutExtra(Intent.ExtraStream, Android.Net.Uri.FromFile(file));
}
}
if (message != null)
{
messageIntent.PutExtra(Intent.ExtraText, message);
}
if (chooserMessage != null)
{
using (var chooser = Intent.CreateChooser(messageIntent, chooserMessage))
{
contextWrapper.StartActivity(chooser);
}
return;
}
contextWrapper.StartActivity(messageIntent);
}
Please note that I am using Android and need a solution based on Android (intent based).
Sadly, Twitter don't provide API access for uploading images via DM.
If you are able to use Twitter's private API, you should be able to attach a media_id to your DM. But other than that, you're out of luck.
Sorry.

Creating TOpenDialog manually in C++ Builder XE 8 (firemonkey)

I am using C++ Builder XE8. As the TOpenDialog doesn't work on Android, I am trying to make such thing myself. My logic is very simple. It'll start to check file and folders from "/storage" and show all items on TListView. If I touch a folder (name) it'll open that folder and if I touch a file, it should show the name on a label. So I assigned a function to TListView's OnItemClick event.
Here is code. fpath is String, Label1 is showing current folder and Label2 is showing selected file.
void __fastcall TForm1::lviewitemclck(TObject * const Sender, TListViewItem * const AItem)
{
if (AItem->Text == "<< BACK") {
if (!fpath.LastDelimiter("/") == 0) {
fpath = fpath.SubString(0, fpath.LastDelimiter("/"));
Label1->Text = fpath;
Form1->showfiles(fpath);
}
}
else if ( DirectoryExists(fpath+ AItem->Text)) {
fpath = fpath+ AItem->Text;
Label1->Text = fpath;
Form1->showfiles(fpath);
}
else if (FileExists(fpath+ AItem->Text)) {
Label2->Text ="File: "+ fpath+ AItem->Text;
}
}
Below is the code of function to scan for files & folders and show them. stringlist is TStringList.
void __fastcall TForm1::showfiles (String path)
{
TSearchRec sr; // for scaning files and folders
TSearchRec fr; // to check whether the folder is accessible or not.
if (FindFirst(path+"/*", faAnyFile, sr) == 0)
{
stringlist->Clear();
stringlist->Add("<< BACK"); // being used to replace the ".."
do{
if(sr.Name != "." && sr.Name != ".."){
if (DirectoryExists(path+"/"+sr.Name)) {
if (FindFirst(path+"/"+sr.Name+"/*", faAnyFile, fr) == 0) { // to check if the folder is accessible
stringlist->Add("/"+ sr.Name);
}
FindClose(fr);
}
else{
stringlist->Add("/"+ sr.Name);
}
}
} while (FindNext(sr) == 0);
}
FindClose(sr);
stringlist->Sort();
Form1->Item->Free();
Form1->ListView1->BeginUpdate();
Form1->ListView1->ClearItems();
for( int i =0;i< stringlist->Count; i++){
Form1->Item = Form1->ListView1->Items->Add();
Form1->Item->Text = stringlist->Strings[i];
}
Form1->ListView1->EndUpdate();
}
Here the problem is, if I use ListView1->ClearItems() in TForm1::showfiles it shows me an error saying "Access violation at address (random no), accessing address 00000009". And if I dont use ClearItems() it just add more lines with already existed lines. I am a beginer, so I dont know where I am doing wrong.
You should use:
ListView1->Items->Clear
The best way I have found so far is to dynamically create TListView and delete it each time you want to add new items( or calling showfiles function). I have written a small function (named it refresh) to release already created TListView and call another function(named it create_lview ) which can create an instance again then it calls the showfiles method.
void __fastcall TForm1::refresh()
{
if (!lview1->Released()) {
try{
lview1->Release();
Form1->create_lview();
Form1->showfiles(fpath);
}
catch(...){
Label2->Text = "error in cleaning";
}
}
}
Here is the code to create the TListView whenever you want.
void __fastcall TForm1::create_lview()
{
lview1 = new TListView(Form1);
lview1->Parent = Form1;
lview1->Height = 600;
lview1->Width = 400;
lview1->Position->X = 0;
lview1->Position->Y = 0;
lview1->Visible = true;
lview1->Enabled = true;
lview1->OnItemClick = lviewitemclck;
lview1->CanSwipeDelete = false;
}
Please comment if you find any mistake or you can do it more efficiently.
I have tried another way to avoid the error by replacing the Clear method with updating the Item Text, then deletes the unused last row of the ListView within the ListView1Change event.
void __fastcall TForm1::ListView1Change(TObject *Sender)
{
//Delete last item
while (ListView1->Items->Count>stringlist->Count){
ListView1->Items->Delete(ListView1->Items->Count-1);
}
}
void __fastcall TForm1::showfiles (String path)
{
TSearchRec sr; // for scaning files and folders
TSearchRec fr; // to check whether the folder is accessible or not.
//path+PathDelim+
if (FindFirst(path+PathDelim+'*', faAnyFile, sr) == 0)
{
stringlist->Clear();
stringlist->Add("<<--BACK"); // being used to replace the ".."
do{
if(sr.Name != "." && sr.Name != ".."){
if (DirectoryExists(path+PathDelim+sr.Name)) {
if (FindFirst(path+PathDelim+sr.Name+PathDelim+"*", faAnyFile, fr) == 0) { // to check if the folder is accessible
stringlist->Add(sr.Name);
}
FindClose(fr);
}
else{
stringlist->Add(sr.Name);
}
}
} while (FindNext(sr) == 0);
}
FindClose(sr);
stringlist->Sort();
for( int i =0;i< ListView1->Items->Count; i++){
ListView1->Items->Item[i]->Text="";
}
ListView1->BeginUpdate();
try {
for( int i =0;i< stringlist->Count; i++)
{
if (ListView1->Items->Count-1<i)
{
TListViewItem* Item=ListView1->Items->Add();
Item->Text=stringlist->Strings[i];
} else
{
TListViewItem* Item=ListView1->Items->Item[i];
Item->Text=stringlist->Strings[i];
}
}
}
catch (...) {
}
ListView1->EndUpdate();
/* */
}

If statement within If statement's else

Still new to Java/android so trying to figure out the best way to code a multilevel if statement. What I'm trying to do is for a combat system that needs to check if player/npc is alive. If they are alive it then will check to see if they scored a critical hit. If they didn't critical hit then will see if they hit or missed.
combat = mydbhelper.getCombat();
startManagingCursor(combat);
if (playerCurHp == 0) {
combat.moveToPosition(11);
npcCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
} else {
if (playerCritFlag.equals("Critical")) {
combat.moveToPosition(2);
playerCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
} else {
if (playerHitFlag.equals("Hit")) {
combat.moveToPosition(1);
playerCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
}
if (playerHitFlag.equals("Miss")) {
combat.moveToPosition(3);
playerCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
}
}
}
if (npcCurHp == 0) {
combat.moveToPosition(10);
npcCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
} else {
if (npcCritFlag.equals("Critical")) {
combat.moveToPosition(5);
npcCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
} else {
if (npcHitFlag.equals("Hit")) {
combat.moveToPosition(4);
npcCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
}
if(npcHitFlag.equals("Miss")) {
combat.moveToPosition(6);
npcCombatStory = combat.getString(combat.getColumnIndex(dbhelper.KEY_COMBATDESC));
}
}
}
Is what I'm using. Was working when I had the if statements all separate. But it would check each one and do actions I don't need (If they hit, pull String, if crit pull another, then if dead pull again). Trying to make it stop when it finds the "Flag" that matches. When doing my rolls if the player hits it sets the flag to "Hit" like below code.
Random attackRandom = new Random();
int attackRoll = attackRandom.nextInt(100);
totalAtt = attackRoll + bonusAttack + weaponAtt + stanceAtt;
Random defensiveRandom = new Random();
int defenseRoll = defensiveRandom.nextInt(100);
npcDef = defenseRoll + npcDodge + npcBonusDodge;
if (totalAtt > npcDef) {
playerHitFlag = "Hit";
playerDamage();
} else {
playerHitFlag = "Miss";
npcAttack();
}
At the end it takes these playerCombatStory and npcCombatStory strings and uses them to setText to show the player what happened on that turn of combat.
I think you are looking for the else if statement:
if (condition) {
}
else if (other_condition) {
}
else if (another_condition) {
}
else {
// There can only be one else statement in a given if-else block
}
Your question isn't clear. But meaningful advice can still be offered.
Personally, I find this code hard to read. I think it'll be hard to maintain in the future as your logic becomes more complex.
I think you need to decouple the logic of what's done from how you decide. Encapsulate what's done in a Command object and use a map or state machine to look up what to do.
I would change the type of npcCritFlag to int or enum. Then use switch statement with case
This should look much better and easier to understand

destroyActivity() Bug in LocalActivityManager class in Android issue

I have a Tab Activity and with in Tab, im using Activity Group. and using LocalActivityManger,
im trying to destroy an Activty using the following function call provided in LocalActivityManger class
manager.destroyActivity(mIdList.get(index), true);
in the code. but later i found that there is a bug in Android impl for this
The exact source of the problem is in the following chunk of code in LocalActivityManager.java:
public Window destroyActivity(String id, boolean finish) {
LocalActivityRecord r = mActivities.get(id);
Window win = null;
if (r != null) {
win = performDestroy(r, finish);
if (finish) {
mActivities.remove(r);
}
}
return win;
}
The variable mActivities is the hashmap containing the activity records and it uses the id passed into startActivity() as the key. In this method, the object passed in for the key is a LocalActivityRecord instead of the id string. This results in the hashmap not finding the entry and thus not removing it.
More info refer this link. http://code.google.com/p/android/issues/detail?id=879More
and i found a work around for this issue and im using following function to fix the problem.
public boolean destroy(String id) {
if(manager != null){
manager.destroyActivity(id, false);
try {
final Field mActivitiesField = LocalActivityManager.class.getDeclaredField("mActivities");
if(mActivitiesField != null){
mActivitiesField.setAccessible(true);
#SuppressWarnings("unchecked")
final Map<String, Object> mActivities = (Map<String, Object>)mActivitiesField.get(manager);
if(mActivities != null){
mActivities.remove(id);
}
final Field mActivityArrayField = LocalActivityManager.class.getDeclaredField("mActivityArray");
if(mActivityArrayField != null){
mActivityArrayField.setAccessible(true);
#SuppressWarnings("unchecked")
final ArrayList<Object> mActivityArray = (ArrayList<Object>)mActivityArrayField.get(manager);
if(mActivityArray != null){
for(Object record : mActivityArray){
final Field idField = record.getClass().getDeclaredField("id");
if(idField != null){
idField.setAccessible(true);
final String _id = (String)idField.get(record);
if(id.equals(_id)){
mActivityArray.remove(record);
break;
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
now the problem is, this fix is working fine in Android API versions 2.1,2.2 and 2.3 but i tested in 3.0 . but it is failing there. no exceptions.
I want to know in which API version this bug has been fixed.
And also what fix can i make for this so that it will work fine in all the API versions after 2.1 .
Thank u

Android Intent getExtras

I am passing values between two activities and fetching the values like this:
Bundle extras = getIntent().getExtras();
if (extras != null)
{
initialUrl = extras.getString("initialUrl");
isFollow = extras.getString("isFollow");
}
if (isFollow == "true") {
editUrl.setText(initialUrl);
setUpWebView(initialUrl);
} else if (isFollow == "false") {
editUrl.setText("http://www.google.com");
setUpWebView("http://www.google.com");
}
the problem is I can see the values being retrieved in the debug window by adding watch to the variables but when the compiler enters the statement if(isFollow=="true"), the condition fails. The else case is also not dealt with. What else do i need to do to ensure that my if condition is satisfied properly?
You should use
isFollow.equals("true")
in your statements.
If String type of data is put in bundle then Try with the following code
String isFollow = null;
Bundle extras = getIntent().getExtras();
if (extras != null)
{
initialUrl = extras.getString("initialUrl");
isFollow = extras.getString("isFollow");
}
if (isFollow.equals("true")) {
editUrl.setText(initialUrl);
setUpWebView(initialUrl);
} else if (isFollow.equals("false")) {
editUrl.setText("http://www.google.com");
setUpWebView("http://www.google.com");
}
If Boolean type of data is put in bundle then Try with the following code
boolean isFollow = null;
Bundle extras = getIntent().getExtras();
if (extras != null)
{
initialUrl = extras.getString("initialUrl");
isFollow = extras.getBoolean("isFollow");
}
if (isFollow) {
editUrl.setText(initialUrl);
setUpWebView(initialUrl);
} else {
editUrl.setText("http://www.google.com");
setUpWebView("http://www.google.com");
}
You need to test either
isFollow.equals("true") or if it is a boolean and not a string, either
isFollow == true or just plain isFollow
(note the no quotes on the second one)
I know this is a really late answer but it would be better to do this if you are passing a Boolean into the intent from the sender:
Boolean isFollow = extras.getBoolean("isFollow");
if(isFollow) {
//Do stuff
}

Categories

Resources