Skip to content

Commit

Permalink
always check Contacts permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Aug 24, 2019
1 parent 36a8de0 commit 53d030c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
37 changes: 31 additions & 6 deletions app/src/main/java/co/tinode/tindroid/FindFragment.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package co.tinode.tindroid;

import android.Manifest;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.provider.ContactsContract;
Expand All @@ -15,6 +17,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.SearchView;

import androidx.appcompat.widget.ShareActionProvider;
import android.widget.Toast;

Expand Down Expand Up @@ -49,6 +52,8 @@ public class FindFragment extends Fragment {

private static final String TAG = "FindFragment";

private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;

// Delay in milliseconds between the last keystroke and time when the query is sent to the server.
private static final int SEARCH_REQUEST_DELAY = 1000;

Expand All @@ -64,6 +69,9 @@ public class FindFragment extends Fragment {
private ImageLoader mImageLoader; // Handles loading the contact image in a background thread
private FindAdapter mAdapter = null;

// Limit the number of times permissions are requested per session.
private boolean mPermissionsAlreadyRequested = false;

// Callback which receives notifications of contacts loading status;
private ContactsLoaderCallback mContactsLoaderCallback;

Expand Down Expand Up @@ -167,6 +175,7 @@ public PromisedReply<ServerMessage> onFailure(Exception err) {
Toast.makeText(fragment.getContext(), R.string.action_failed, Toast.LENGTH_LONG).show();
}

mPermissionsAlreadyRequested = false;
mAdapter.resetFound(getActivity(), mSearchTerm);
// Refresh cursor.
restartLoader(mSearchTerm);
Expand Down Expand Up @@ -391,9 +400,9 @@ public void run() {
}

// TODO: Add onBackPressed handing to parent Activity.
public boolean onBackPressed() {
return false;
}
// public boolean onBackPressed() {
// return false;
//}

private class FndListener extends FndTopic.FndListener<VxCard> {
@Override
Expand All @@ -417,8 +426,24 @@ private void restartLoader(String searchTerm) {
return;
}

Bundle args = new Bundle();
args.putString(ContactsLoaderCallback.ARG_SEARCH_TERM, searchTerm);
LoaderManager.getInstance(activity).restartLoader(LOADER_ID, args, mContactsLoaderCallback);
if (UiUtils.isPermissionGranted(activity, Manifest.permission.READ_CONTACTS)) {
Bundle args = new Bundle();
args.putString(ContactsLoaderCallback.ARG_SEARCH_TERM, searchTerm);
LoaderManager.getInstance(activity).restartLoader(LOADER_ID, args, mContactsLoaderCallback);
} else if (!mPermissionsAlreadyRequested) {
mPermissionsAlreadyRequested = true;
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, PERMISSIONS_REQUEST_READ_CONTACTS);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_READ_CONTACTS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission is granted
restartLoader(mSearchTerm);
}
}
}
}
6 changes: 3 additions & 3 deletions app/src/main/java/co/tinode/tindroid/LoginActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,13 @@ private void requestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ArrayList<String> permissions = new ArrayList<>();

if (!UiUtils.checkPermission(this, Manifest.permission.GET_ACCOUNTS)) {
if (!UiUtils.isPermissionGranted(this, Manifest.permission.GET_ACCOUNTS)) {
permissions.add(Manifest.permission.GET_ACCOUNTS);
}
if (!UiUtils.checkPermission(this, Manifest.permission.READ_CONTACTS)) {
if (!UiUtils.isPermissionGranted(this, Manifest.permission.READ_CONTACTS)) {
permissions.add(Manifest.permission.READ_CONTACTS);
}
if (!UiUtils.checkPermission(this, Manifest.permission.WRITE_CONTACTS)) {
if (!UiUtils.isPermissionGranted(this, Manifest.permission.WRITE_CONTACTS)) {
permissions.add(Manifest.permission.WRITE_CONTACTS);
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/co/tinode/tindroid/MessagesAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ public void run() {
*/
private void verifyStoragePermissions() {
// Check if we have write permission
if (!UiUtils.checkPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
if (!UiUtils.isPermissionGranted(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// We don't have permission so prompt the user
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
Expand Down
7 changes: 3 additions & 4 deletions app/src/main/java/co/tinode/tindroid/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public void run() {
activity.finish();
}

@SuppressWarnings("BooleanMethodIsAlwaysInverted")
static boolean checkPermission(Context context, String permission) {
static boolean isPermissionGranted(Context context, String permission) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
ActivityCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED;
}
Expand Down Expand Up @@ -336,7 +335,7 @@ static Account getSavedAccount(final Activity activity, final AccountManager acc
Account account = null;

// Run-time check for permission to GET_ACCOUNTS
if (!UiUtils.checkPermission(activity, android.Manifest.permission.GET_ACCOUNTS)) {
if (!UiUtils.isPermissionGranted(activity, android.Manifest.permission.GET_ACCOUNTS)) {
// Don't have permission. It's the first launch or the user denied access.
// Fail and go to full login. We should not ask for permission on the splash screen.
Log.d(TAG, "NO permission to get accounts");
Expand Down Expand Up @@ -415,7 +414,7 @@ static void requestAvatar(@Nullable Fragment fragment) {
return;
}

if (!checkPermission(activity, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
if (!isPermissionGranted(activity, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
READ_EXTERNAL_STORAGE_PERMISSION);
} else {
Expand Down
8 changes: 7 additions & 1 deletion tinodesdk/src/main/java/co/tinode/tinodesdk/Tinode.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ private boolean loadTopics() {
}

private void setTopicsUpdated(Date date) {
if (date == null) {
return;
}

if (mTopicsUpdated == null || mTopicsUpdated.before(date)) {
mTopicsUpdated = date;
}
Expand Down Expand Up @@ -565,7 +569,9 @@ private void dispatchPacket(String message) throws Exception {

if (topic != null) {
topic.routeMeta(pkt.meta);
setTopicsUpdated(topic.getUpdated());
if (!topic.isFndType() && !topic.isMeType()) {
setTopicsUpdated(topic.getUpdated());
}
}

if (mListener != null) {
Expand Down

0 comments on commit 53d030c

Please sign in to comment.