1

Hope is not a kind of stupid question but my app was working until two days ago but after that I got OTA update to upgrade it to Marshmallow. Then after that not only I cannot get list of my contacts, even default device application for showing contact list doesn't show any contact. I downloaded several Contact Management app from Play Store and even they don't display anything. I even cannot send/recieve SMS too :) I'm wondering wtf is happening.

Ok let me cut the b.s and share what I've done. I changed the code according to new dynamic permission request that Marshmallow needs. I launch the app and grant access to Contacts but nothing displays :(

This is my code, sorry I know is long but is simple:

public class MainActivity extends AppCompatActivity
{
    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 10;

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;
    private ContactsAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Log.d(TAG, "********************************");
        Log.d(TAG, "*** Contacts Remover Started ***");
        Log.d(TAG, "********************************");

        setContentView(R.layout.activity_main);

        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshScreen);
        mRefreshLayout.setOnRefreshListener(this::refreshLayout);
        mRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        mRecyclerView.setHasFixedSize(true);

        // use a linear layout manager
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);

        // create adapter
        mAdapter = new ContactsAdapter();
        mRecyclerView.setAdapter(mAdapter);

        // Check required permissions
        checkContactPermission();
    }

    private void checkContactPermission()
    {
        Log.d(TAG, "Yu");
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED ||
            ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED)
        {
            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS))
            {
                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.
            }
            else
            {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS,
                        Manifest.permission.WRITE_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS);
            }
        }
        else
        {
            Log.d(TAG, "Perms granted");
            // Get Contacts and display it
            refreshLayout();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults)
    {
        switch (requestCode)
        {
            case MY_PERMISSIONS_REQUEST_READ_CONTACTS:
            {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
                {
                    // Get Contacts and display it
                    refreshLayout();
                }
                else
                {
                    this.finish();
                }
                return;
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }

    private List<Contact> getContactList()
    {
        Log.d(TAG, "2");
        List<Contact> contactList = new ArrayList<>(100);

        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        Log.d(TAG, "3");
        if (cur != null && cur.getCount() > 0)
        {
            int i = 0;
            Log.d(TAG, "4");
            while (cur.moveToNext())
            {
                i++;
                Log.d(TAG, "-> " + i);

                Contact contact = new Contact();
                contact.setId(cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)));
                contact.setName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
                contact.setImageAddress(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI)));

                contactList.add(contact);
            }

            cur.close();
            Log.d(TAG, "5");
        }

        Log.d(TAG, String.format("Number of %s contacts found", contactList.size()));
        return contactList;
    }

    private Observable<Contact> getApps()
    {
        Log.d(TAG, "1");
        return Observable.create(subscriber -> {
            List<Contact> contactList = getContactList();
            Log.d(TAG, "6");

            if (subscriber.isUnsubscribed())
            {
                return;
            }

            Log.d(TAG, "7");

            for (Contact contact : contactList)
            {
                subscriber.onNext(contact);
            }

            Log.d(TAG, "8");

            if (!subscriber.isUnsubscribed())
            {
                subscriber.onCompleted();
            }

            Log.d(TAG, "9");
        });
    }

    private void refreshLayout()
    {
        Log.d(TAG, "0");
        if (mRefreshLayout.isRefreshing())
        {
            return;
        }

        mRefreshLayout.setRefreshing(true);

        getApps().toSortedList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Contact>>()
                {
                    @Override
                    public void onCompleted()
                    {
                        Log.d(TAG, "10");
                        Toast.makeText(MainActivity.this, "List updated!", Toast.LENGTH_LONG).show();
                        mRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onError(Throwable e)
                    {
                        Log.d(TAG, "ERROR!!!");
                        Toast.makeText(MainActivity.this, "Shit! Something went wrong :(", Toast.LENGTH_LONG).show();
                        mRefreshLayout.setRefreshing(false);
                    }

                    @Override
                    public void onNext(List<Contact> contactList)
                    {
                        Log.d(TAG, "*");
                        mAdapter.setContactList(contactList);
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }
}

And finally my log is:

10-15 01:05:46.239 1514-1514/? D/MainActivity: ********************************
10-15 01:05:46.239 1514-1514/? D/MainActivity: *** Contacts Remover Started ***
10-15 01:05:46.239 1514-1514/? D/MainActivity: ********************************
10-15 01:05:46.285 1514-1514/? D/MainActivity: Yu
10-15 01:05:46.287 1514-1514/? D/MainActivity: Perms granted
10-15 01:05:46.287 1514-1514/? D/MainActivity: 0
10-15 01:05:46.288 1514-1514/? D/MainActivity: 1
10-15 01:05:46.306 1514-14799/? D/MainActivity: 2

Something is happening in this line, Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);. There is no error, no crash, no report!!! just nothing :(

Hesam
  • 52,260
  • 74
  • 224
  • 365

1 Answers1

0

You seem to be wasting your time adjusting your code when the default contacts app on the phone also shows no contacts.

It looks as if the OTA update has either done some sort of partial factory reset, or perhaps simply dropped the account that was previously providing your device contacts. Sort that out first, so the default app shows some contacts, and I suspect that you app will also start to show contacts.

zmarties
  • 4,809
  • 22
  • 39
  • Yup, after factory reset, luckily my device set to Marshmallow again, rather than KitKat. But know every thing is fine and those two problems fixed. – Hesam Oct 15 '15 at 23:45