From dc66b27c8ae3ad68538ce0920b48a248c4e8be94 Mon Sep 17 00:00:00 2001 From: LucasGGamerM Date: Sun, 11 Jun 2023 16:48:19 -0300 Subject: [PATCH] feat: add camera shortcut to compose screen --- mastodon/src/main/AndroidManifest.xml | 1 + .../android/fragments/ComposeFragment.java | 40 +++++++++++++++++-- .../drawable/ic_fluent_camera_24_regular.xml | 9 +++++ mastodon/src/main/res/menu/attach.xml | 4 ++ mastodon/src/main/res/values/strings_mo.xml | 1 + 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 mastodon/src/main/res/drawable/ic_fluent_camera_24_regular.xml diff --git a/mastodon/src/main/AndroidManifest.xml b/mastodon/src/main/AndroidManifest.xml index 54255565b..b648d1445 100644 --- a/mastodon/src/main/AndroidManifest.xml +++ b/mastodon/src/main/AndroidManifest.xml @@ -5,6 +5,7 @@ + diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java index fd9afde61..ccc58b6fc 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/ComposeFragment.java @@ -1,7 +1,5 @@ package org.joinmastodon.android.fragments; -import static android.os.ext.SdkExtensions.getExtensionVersion; - import static org.joinmastodon.android.GlobalUserPreferences.recentLanguages; import static org.joinmastodon.android.api.requests.statuses.CreateStatus.DRAFTS_AFTER_INSTANT; import static org.joinmastodon.android.api.requests.statuses.CreateStatus.getDraftInstant; @@ -9,6 +7,7 @@ import static org.joinmastodon.android.ui.utils.UiUtils.isPhotoPickerAvailable; import static org.joinmastodon.android.utils.MastodonLanguage.allLanguages; import static org.joinmastodon.android.utils.MastodonLanguage.defaultRecentLanguages; +import android.Manifest; import android.animation.ObjectAnimator; import android.annotation.SuppressLint; import android.app.Activity; @@ -17,6 +16,7 @@ import android.app.TimePickerDialog; import android.content.ClipData; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.database.Cursor; import android.graphics.Bitmap; @@ -29,7 +29,6 @@ import android.graphics.drawable.LayerDrawable; import android.icu.text.BreakIterator; import android.media.MediaMetadataRetriever; import android.net.Uri; -import android.opengl.Visibility; import android.os.Build; import android.os.Bundle; import android.os.Parcelable; @@ -70,6 +69,8 @@ import android.widget.ScrollView; import android.widget.TextView; import android.widget.Toast; +import androidx.annotation.NonNull; + import com.github.bottomSoftwareFoundation.bottom.Bottom; import com.twitter.twittertext.TwitterTextEmojiRegex; @@ -161,6 +162,8 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr private static final String GLITCH_LOCAL_ONLY_SUFFIX = "👁"; private static final Pattern GLITCH_LOCAL_ONLY_PATTERN = Pattern.compile("[\\s\\S]*" + GLITCH_LOCAL_ONLY_SUFFIX + "[\uFE00-\uFE0F]*"); private static final String TAG="ComposeFragment"; + private static final int CAMERA_PERMISSION_CODE = 626938; + private static final int CAMERA_PIC_REQUEST_CODE = 6242069; public static final Pattern MENTION_PATTERN=Pattern.compile("(^|[^\\/\\w])@(([a-z0-9_]+)@[a-z0-9\\.\\-]+[a-z0-9]+)", Pattern.CASE_INSENSITIVE); @@ -369,7 +372,17 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr PopupMenu attachPopup = new PopupMenu(getContext(), mediaBtn); attachPopup.inflate(R.menu.attach); attachPopup.setOnMenuItemClickListener(i -> { - openFilePicker(i.getItemId() == R.id.media); + if (i.getItemId() == R.id.camera){ + if (getContext().checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) { + Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST_CODE); + } else { + getActivity().requestPermissions(new String[] { Manifest.permission.CAMERA }, CAMERA_PERMISSION_CODE); + } + + } else { + openFilePicker(i.getItemId() == R.id.media); + } return true; }); UiUtils.enablePopupMenuIcons(getContext(), attachPopup); @@ -569,6 +582,19 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr if (scheduledStatus != null) outState.putParcelable("scheduledStatus", Parcels.wrap(scheduledStatus)); } + + @Override + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + + if (requestCode == CAMERA_PERMISSION_CODE && (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)) { + Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); + startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST_CODE); + } else { + Toast.makeText(getContext(), R.string.permission_required, Toast.LENGTH_SHORT); + } + } + @Override public void onResume(){ super.onResume(); @@ -1427,6 +1453,12 @@ public class ComposeFragment extends MastodonToolbarFragment implements OnBackPr } } } + + if(requestCode==CAMERA_PIC_REQUEST_CODE && resultCode==Activity.RESULT_OK){ + Bitmap image = (Bitmap) data.getExtras().get("data"); + String path = MediaStore.Images.Media.insertImage(getContext().getContentResolver(), image, null, null); + addMediaAttachment(Uri.parse(path), null); + } } private boolean addMediaAttachment(Uri uri, String description){ diff --git a/mastodon/src/main/res/drawable/ic_fluent_camera_24_regular.xml b/mastodon/src/main/res/drawable/ic_fluent_camera_24_regular.xml new file mode 100644 index 000000000..f48869406 --- /dev/null +++ b/mastodon/src/main/res/drawable/ic_fluent_camera_24_regular.xml @@ -0,0 +1,9 @@ + + + diff --git a/mastodon/src/main/res/menu/attach.xml b/mastodon/src/main/res/menu/attach.xml index 6e3fa27a0..f1511e99d 100644 --- a/mastodon/src/main/res/menu/attach.xml +++ b/mastodon/src/main/res/menu/attach.xml @@ -8,4 +8,8 @@ android:id="@+id/file" android:title="@string/sk_attach_file" android:icon="@drawable/ic_fluent_document_24_regular" /> + \ No newline at end of file diff --git a/mastodon/src/main/res/values/strings_mo.xml b/mastodon/src/main/res/values/strings_mo.xml index 45f10ad35..b55cfe836 100644 --- a/mastodon/src/main/res/values/strings_mo.xml +++ b/mastodon/src/main/res/values/strings_mo.xml @@ -32,6 +32,7 @@ Reply as \'Unlisted\' by default Composer\'s Behavior Manage Notifications + Take picture Add new poll option