request camera permissions when attaching taking picture or recording video
This commit is contained in:
parent
363c39a873
commit
a283d00013
|
@ -11,6 +11,8 @@ import android.os.Build;
|
|||
import android.preference.PreferenceManager;
|
||||
import android.provider.MediaStore;
|
||||
import android.support.annotation.IdRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.StringRes;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.app.Fragment;
|
||||
import android.app.PendingIntent;
|
||||
|
@ -1166,7 +1168,11 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
|
|||
}
|
||||
|
||||
public void attachFile(final int attachmentChoice) {
|
||||
if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) {
|
||||
if (attachmentChoice == ATTACHMENT_CHOICE_TAKE_PHOTO || attachmentChoice == ATTACHMENT_CHOICE_RECORD_VIDEO) {
|
||||
if (!hasStorageAndCameraPermission(attachmentChoice)) {
|
||||
return;
|
||||
}
|
||||
} else if (attachmentChoice != ATTACHMENT_CHOICE_LOCATION) {
|
||||
if (!Config.ONLY_INTERNAL_STORAGE && !hasStoragePermission(attachmentChoice)) {
|
||||
return;
|
||||
}
|
||||
|
@ -1231,9 +1237,9 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
|
|||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
||||
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
|
||||
if (grantResults.length > 0)
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
if (allGranted(grantResults)) {
|
||||
if (requestCode == REQUEST_START_DOWNLOAD) {
|
||||
if (this.mPendingDownloadableMessage != null) {
|
||||
startDownloadable(this.mPendingDownloadableMessage);
|
||||
|
@ -1246,8 +1252,32 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
|
|||
attachFile(requestCode);
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(getActivity(), R.string.no_storage_permission, Toast.LENGTH_SHORT).show();
|
||||
@StringRes int res;
|
||||
if (Manifest.permission.CAMERA.equals(getFirstDenied(grantResults, permissions))) {
|
||||
res = R.string.no_camera_permission;
|
||||
} else {
|
||||
res = R.string.no_storage_permission;
|
||||
}
|
||||
Toast.makeText(getActivity(),res, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean allGranted(int[] grantResults) {
|
||||
for(int grantResult : grantResults) {
|
||||
if (grantResult != PackageManager.PERMISSION_GRANTED) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String getFirstDenied(int[] grantResults, String[] permissions) {
|
||||
for(int i = 0; i < grantResults.length; ++i) {
|
||||
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
|
||||
return permissions[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void startDownloadable(Message message) {
|
||||
|
@ -1319,6 +1349,26 @@ public class ConversationFragment extends XmppFragment implements EditMessage.Ke
|
|||
}
|
||||
}
|
||||
|
||||
private boolean hasStorageAndCameraPermission(int requestCode) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
List<String> missingPermissions = new ArrayList<>();
|
||||
if (!Config.ONLY_INTERNAL_STORAGE && activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
|
||||
missingPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
|
||||
}
|
||||
if (activity.checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||
missingPermissions.add(Manifest.permission.CAMERA);
|
||||
}
|
||||
if (missingPermissions.size() == 0) {
|
||||
return true;
|
||||
} else {
|
||||
requestPermissions(missingPermissions.toArray(new String[missingPermissions.size()]), requestCode);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void unmuteConversation(final Conversation conversation) {
|
||||
conversation.setMutedTill(0);
|
||||
this.activity.xmppConnectionService.updateConversation(conversation);
|
||||
|
|
|
@ -528,7 +528,8 @@
|
|||
<string name="shared_image_with_x">Shared image with %s</string>
|
||||
<string name="shared_images_with_x">Shared images with %s</string>
|
||||
<string name="shared_text_with_x">Shared text with %s</string>
|
||||
<string name="no_storage_permission">Conversations need access to external storage</string>
|
||||
<string name="no_storage_permission">Conversations needs access to external storage</string>
|
||||
<string name="no_camera_permission">Conversations needs access to the camera</string>
|
||||
<string name="sync_with_contacts">Synchronize with contacts</string>
|
||||
<string name="sync_with_contacts_long">Conversations wants to match your XMPP roster with your contacts to show their full names and avatars.\n\nConversations will only read your contacts and match them locally without uploading them to your server.\n\nYou will now be asked to grant permission to access your contacts.</string>
|
||||
<string name="certificate_information">Certificate Information</string>
|
||||
|
|
Loading…
Reference in New Issue