Skip to content

Commit

Permalink
make camera appear in image chooser
Browse files Browse the repository at this point in the history
  • Loading branch information
or-else committed Sep 1, 2022
1 parent f29f841 commit e8a1294
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
9 changes: 6 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<!-- Needed for adding an avatar pic when registering new accounts and reading attachments and for video calls. -->
<!-- Needed for video calls, adding an avatar pic when registering new accounts reading attachments. -->
<uses-permission android:name="android.permission.CAMERA"/>
<!-- To disable installation on devices without the camera -->
<uses-feature android:name="android.hardware.camera.any" />
<!-- Enable installation on devices without the camera: some features won't be available -->
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- Needed for saving attachments -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
Expand Down Expand Up @@ -48,6 +48,9 @@
<data android:mimeType="vnd.android.cursor.item/contact" />
<category android:name="android.intent.category.DEFAULT" />
</intent>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>

<application
Expand Down
39 changes: 27 additions & 12 deletions app/src/main/java/co/tinode/tindroid/UiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
Expand Down Expand Up @@ -537,26 +538,39 @@ static Intent avatarSelectorIntent(@NonNull final Activity activity,
}
}

// Option 1: pick image from the gallery.
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/jpeg", "image/png", "image/gif"});
// Option 1: take a photo.
List<Intent> cameraIntents = buildIntentList(activity, new Intent(MediaStore.ACTION_IMAGE_CAPTURE));

// Option 2: take a photo.
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Make sure camera is available.
if (cameraIntent.resolveActivity(activity.getPackageManager()) == null) {
cameraIntent = null;
}
// Option 2: pick image from the gallery.
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
galleryIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[]{"image/jpeg", "image/png", "image/gif"});

// Pack two intents into a chooser.
Intent chooserIntent = Intent.createChooser(galleryIntent, activity.getString(R.string.select_image));
if (cameraIntent != null) {
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{cameraIntent});
if (!cameraIntents.isEmpty()) {
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
}

return chooserIntent;
}

// Given an intent, find all packages which support this intent and build a list with intent
// for each of the found packages.
private static List<Intent> buildIntentList(Context context, Intent intent) {
List<Intent> list = new ArrayList<>();
List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_ALL);
Log.i(TAG, "Got list<ResolveInfo>.isEmpty=" + resInfo.isEmpty());
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedIntent = new Intent(intent);
targetedIntent.setPackage(packageName);
list.add(targetedIntent);
Log.i(TAG, "Camera intent: " + intent.getAction() + " package: " + packageName);
}
return list;
}

static ActivityResultLauncher<Intent> avatarPickerLauncher(@NonNull Fragment fragment,
@NonNull AvatarPreviewer previewer) {
return fragment.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
Expand Down Expand Up @@ -1068,7 +1082,8 @@ PromisedReply<ServerMessage> updateAvatar(final T topic, Bitmap bmp) {
pub = new VxCard();
}

return AttachmentHandler.uploadAvatar(pub, bmp).thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
return AttachmentHandler.uploadAvatar(pub, bmp, topic.getName())
.thenApply(new PromisedReply.SuccessListener<ServerMessage>() {
@Override
public PromisedReply<ServerMessage> onSuccess(ServerMessage result) {
String[] attachments = null;
Expand Down

0 comments on commit e8a1294

Please sign in to comment.